Skip to content
This repository has been archived by the owner on Feb 1, 2023. It is now read-only.

Added WithScoreLedger Bitswap option #430

Merged
merged 5 commits into from
Sep 3, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions bitswap.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

delay "github.com/ipfs/go-ipfs-delay"

deciface "github.com/ipfs/go-bitswap/decision"
bsbpm "github.com/ipfs/go-bitswap/internal/blockpresencemanager"
decision "github.com/ipfs/go-bitswap/internal/decision"
bsgetter "github.com/ipfs/go-bitswap/internal/getter"
Expand Down Expand Up @@ -95,6 +96,13 @@ func SetSendDontHaves(send bool) Option {
}
}

// Configures the engine to use the given score decision logic.
func WithScoreLedger(scoreLedger deciface.ScoreLedger) Option {
return func(bs *Bitswap) {
bs.engine.UseScoreLedger(scoreLedger)
}
}

// New initializes a BitSwap instance that communicates over the provided
// BitSwapNetwork. This function registers the returned instance as the network
// delegate. Runs until context is cancelled or bitswap.Close is called.
Expand Down
61 changes: 60 additions & 1 deletion bitswap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ import (
"time"

bitswap "github.com/ipfs/go-bitswap"
deciface "github.com/ipfs/go-bitswap/decision"
decision "github.com/ipfs/go-bitswap/internal/decision"
bssession "github.com/ipfs/go-bitswap/internal/session"
"github.com/ipfs/go-bitswap/message"
testinstance "github.com/ipfs/go-bitswap/testinstance"
tn "github.com/ipfs/go-bitswap/testnet"
"github.com/ipfs/go-bitswap/message"
blocks "github.com/ipfs/go-block-format"
cid "github.com/ipfs/go-cid"
detectrace "github.com/ipfs/go-detect-race"
Expand Down Expand Up @@ -803,3 +804,61 @@ func TestBitswapLedgerTwoWay(t *testing.T) {
}
}
}

type testingScoreLedger struct {
initialized bool
started chan struct{}
closed chan struct{}
}

func newTestingScoreLedger() *testingScoreLedger {
return &testingScoreLedger{
false,
make(chan struct{}),
make(chan struct{}),
}
}

func (tsl *testingScoreLedger) Init(scorePeer deciface.ScorePeerFunc) {
tsl.initialized = true
}
func (tsl *testingScoreLedger) GetReceipt(p peer.ID) *deciface.Receipt {
return nil
}
func (tsl *testingScoreLedger) AddToSentBytes(p peer.ID, n int) {}
func (tsl *testingScoreLedger) AddToReceivedBytes(p peer.ID, n int) {}
func (tsl *testingScoreLedger) PeerConnected(p peer.ID) {}
func (tsl *testingScoreLedger) PeerDisconnected(p peer.ID) {}
func (tsl *testingScoreLedger) Start() {
close(tsl.started)
}
func (tsl *testingScoreLedger) Close() {
close(tsl.closed)
}

// Tests start and stop of a custom decision logic
func TestWithScoreLedger(t *testing.T) {
tsl := newTestingScoreLedger()
net := tn.VirtualNetwork(mockrouting.NewServer(), delay.Fixed(kNetworkDelay))
bsOpts := []bitswap.Option{bitswap.WithScoreLedger(tsl)}
ig := testinstance.NewTestInstanceGenerator(net, nil, bsOpts)
defer ig.Close()
i := ig.Next()
defer i.Exchange.Close()

select {
case <-tsl.started:
if !tsl.initialized {
t.Fatal("Expected the score ledger to be initialized")
}
case <-time.After(time.Second * 5):
t.Fatal("Expected the score ledger to be started within 5s")
}

i.Exchange.Close()
select {
case <-tsl.closed:
case <-time.After(time.Second * 5):
t.Fatal("Expected the score ledger to be closed within 5s")
}
}
8 changes: 7 additions & 1 deletion decision/decision.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,11 @@ package decision

import intdec "github.com/ipfs/go-bitswap/internal/decision"

// Expose type externally
// Expose Receipt externally
type Receipt = intdec.Receipt

// Expose ScoreLedger externally
type ScoreLedger = intdec.ScoreLedger

// Expose ScorePeerFunc externally
type ScorePeerFunc = intdec.ScorePeerFunc
Loading