Skip to content

Commit

Permalink
Stop using go-ipfs-blocksutil (#656)
Browse files Browse the repository at this point in the history
- Replace use of go-ipfs-blocksutil with go-test/random
- Remove remaining references to go-blockservice
  • Loading branch information
gammazero authored Aug 8, 2024
1 parent 635e175 commit 88beadf
Show file tree
Hide file tree
Showing 13 changed files with 75 additions and 110 deletions.
38 changes: 15 additions & 23 deletions bitswap/bitswap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@ import (
blocks "github.com/ipfs/go-block-format"
cid "github.com/ipfs/go-cid"
detectrace "github.com/ipfs/go-detect-race"
blocksutil "github.com/ipfs/go-ipfs-blocksutil"
delay "github.com/ipfs/go-ipfs-delay"
ipld "github.com/ipfs/go-ipld-format"
"github.com/ipfs/go-test/random"
tu "github.com/libp2p/go-libp2p-testing/etc"
p2ptestutil "github.com/libp2p/go-libp2p-testing/netutil"
peer "github.com/libp2p/go-libp2p/core/peer"
)

const blockSize = 4

func isCI() bool {
// https://github.blog/changelog/2020-04-15-github-actions-sets-the-ci-environment-variable-to-true/
return os.Getenv("CI") != ""
Expand All @@ -52,9 +54,7 @@ func TestClose(t *testing.T) {
vnet := tn.VirtualNetwork(mockrouting.NewServer(), delay.Fixed(kNetworkDelay))
ig := testinstance.NewTestInstanceGenerator(vnet, nil, nil)
defer ig.Close()
bgen := blocksutil.NewBlockGenerator()

block := bgen.Next()
block := random.BlocksOfSize(1, blockSize)[0]
bitswap := ig.Next()

bitswap.Exchange.Close()
Expand Down Expand Up @@ -187,7 +187,6 @@ func TestUnwantedBlockNotAdded(t *testing.T) {
func TestPendingBlockAdded(t *testing.T) {
ctx := context.Background()
net := tn.VirtualNetwork(mockrouting.NewServer(), delay.Fixed(kNetworkDelay))
bg := blocksutil.NewBlockGenerator()
sessionBroadcastWantCapacity := 4

ig := testinstance.NewTestInstanceGenerator(net, nil, nil)
Expand All @@ -202,7 +201,7 @@ func TestPendingBlockAdded(t *testing.T) {
// Request enough blocks to exceed the session's broadcast want list
// capacity (by one block). The session will put the remaining block
// into the "tofetch" queue
blks := bg.Blocks(sessionBroadcastWantCapacity + 1)
blks := random.BlocksOfSize(sessionBroadcastWantCapacity+1, blockSize)
ks := make([]cid.Cid, 0, len(blks))
for _, b := range blks {
ks = append(ks, b.Cid())
Expand Down Expand Up @@ -285,10 +284,9 @@ func PerformDistributionTest(t *testing.T, numInstances, numBlocks int) {
bitswap.MaxOutstandingBytesPerPeer(1 << 20),
})
defer ig.Close()
bg := blocksutil.NewBlockGenerator()

instances := ig.Instances(numInstances)
blocks := bg.Blocks(numBlocks)
blocks := random.BlocksOfSize(numBlocks, blockSize)

t.Log("Give the blocks to the first instance")

Expand Down Expand Up @@ -338,7 +336,6 @@ func TestSendToWantingPeer(t *testing.T) {
net := tn.VirtualNetwork(mockrouting.NewServer(), delay.Fixed(kNetworkDelay))
ig := testinstance.NewTestInstanceGenerator(net, nil, nil)
defer ig.Close()
bg := blocksutil.NewBlockGenerator()

peers := ig.Instances(2)
peerA := peers[0]
Expand All @@ -349,7 +346,7 @@ func TestSendToWantingPeer(t *testing.T) {

waitTime := time.Second * 5

alpha := bg.Next()
alpha := random.BlocksOfSize(1, blockSize)[0]
// peerA requests and waits for block alpha
ctx, cancel := context.WithTimeout(context.Background(), waitTime)
defer cancel()
Expand Down Expand Up @@ -409,12 +406,11 @@ func TestBasicBitswap(t *testing.T) {
net := tn.VirtualNetwork(mockrouting.NewServer(), delay.Fixed(kNetworkDelay))
ig := testinstance.NewTestInstanceGenerator(net, nil, nil)
defer ig.Close()
bg := blocksutil.NewBlockGenerator()

t.Log("Test a one node trying to get one block from another")

instances := ig.Instances(3)
blocks := bg.Blocks(1)
blocks := random.BlocksOfSize(1, blockSize)

// First peer has block
addBlock(t, context.Background(), instances[0], blocks[0])
Expand Down Expand Up @@ -481,12 +477,11 @@ func TestDoubleGet(t *testing.T) {
net := tn.VirtualNetwork(mockrouting.NewServer(), delay.Fixed(kNetworkDelay))
ig := testinstance.NewTestInstanceGenerator(net, nil, nil)
defer ig.Close()
bg := blocksutil.NewBlockGenerator()

t.Log("Test a one node trying to get one block from another")

instances := ig.Instances(2)
blocks := bg.Blocks(1)
blocks := random.BlocksOfSize(1, blockSize)

// NOTE: A race condition can happen here where these GetBlocks requests go
// through before the peers even get connected. This is okay, bitswap
Expand Down Expand Up @@ -546,12 +541,11 @@ func TestWantlistCleanup(t *testing.T) {
net := tn.VirtualNetwork(mockrouting.NewServer(), delay.Fixed(kNetworkDelay))
ig := testinstance.NewTestInstanceGenerator(net, nil, nil)
defer ig.Close()
bg := blocksutil.NewBlockGenerator()

instances := ig.Instances(2)
instance := instances[0]
bswap := instance.Exchange
blocks := bg.Blocks(20)
blocks := random.BlocksOfSize(20, blockSize)

var keys []cid.Cid
for _, b := range blocks {
Expand Down Expand Up @@ -668,12 +662,11 @@ func TestBitswapLedgerOneWay(t *testing.T) {
net := tn.VirtualNetwork(mockrouting.NewServer(), delay.Fixed(kNetworkDelay))
ig := testinstance.NewTestInstanceGenerator(net, nil, nil)
defer ig.Close()
bg := blocksutil.NewBlockGenerator()

t.Log("Test ledgers match when one peer sends block to another")

instances := ig.Instances(2)
blocks := bg.Blocks(1)
blocks := random.BlocksOfSize(1, blockSize)
addBlock(t, context.Background(), instances[0], blocks[0])

ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
Expand All @@ -693,12 +686,12 @@ func TestBitswapLedgerOneWay(t *testing.T) {
}

// check that receipts have intended values
ratest := newReceipt(1, 0, 1)
ratest := newReceipt(blockSize, 0, 1)
err = assertLedgerEqual(ratest, ra)
if err != nil {
t.Fatal(err)
}
rbtest := newReceipt(0, 1, 1)
rbtest := newReceipt(0, blockSize, 1)
err = assertLedgerEqual(rbtest, rb)
if err != nil {
t.Fatal(err)
Expand All @@ -717,12 +710,11 @@ func TestBitswapLedgerTwoWay(t *testing.T) {
net := tn.VirtualNetwork(mockrouting.NewServer(), delay.Fixed(kNetworkDelay))
ig := testinstance.NewTestInstanceGenerator(net, nil, nil)
defer ig.Close()
bg := blocksutil.NewBlockGenerator()

t.Log("Test ledgers match when two peers send one block to each other")

instances := ig.Instances(2)
blocks := bg.Blocks(2)
blocks := random.BlocksOfSize(2, blockSize)
addBlock(t, context.Background(), instances[0], blocks[0])
addBlock(t, context.Background(), instances[1], blocks[1])

Expand Down Expand Up @@ -750,7 +742,7 @@ func TestBitswapLedgerTwoWay(t *testing.T) {
}

// check that receipts have intended values
rtest := newReceipt(1, 1, 2)
rtest := newReceipt(blockSize, blockSize, 2)
err = assertLedgerEqual(rtest, ra)
if err != nil {
t.Fatal(err)
Expand Down
31 changes: 12 additions & 19 deletions bitswap/client/bitswap_with_sessions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ import (
mockrouting "github.com/ipfs/boxo/routing/mock"
blocks "github.com/ipfs/go-block-format"
cid "github.com/ipfs/go-cid"
blocksutil "github.com/ipfs/go-ipfs-blocksutil"
delay "github.com/ipfs/go-ipfs-delay"
"github.com/ipfs/go-test/random"
tu "github.com/libp2p/go-libp2p-testing/etc"
"github.com/libp2p/go-libp2p/core/peer"
)

const blockSize = 4

func getVirtualNetwork() tn.Network {
// FIXME: the tests are really sensitive to the network delay. fix them to work
// well under varying conditions
Expand All @@ -46,9 +48,8 @@ func TestBasicSessions(t *testing.T) {
vnet := getVirtualNetwork()
ig := testinstance.NewTestInstanceGenerator(vnet, nil, nil)
defer ig.Close()
bgen := blocksutil.NewBlockGenerator()

block := bgen.Next()
block := random.BlocksOfSize(1, blockSize)[0]
inst := ig.Instances(2)

a := inst[0]
Expand Down Expand Up @@ -113,12 +114,11 @@ func TestSessionBetweenPeers(t *testing.T) {
vnet := tn.VirtualNetwork(mockrouting.NewServer(), delay.Fixed(time.Millisecond))
ig := testinstance.NewTestInstanceGenerator(vnet, nil, []bitswap.Option{bitswap.SetSimulateDontHavesOnTimeout(false)})
defer ig.Close()
bgen := blocksutil.NewBlockGenerator()

inst := ig.Instances(10)

// Add 101 blocks to Peer A
blks := bgen.Blocks(101)
blks := random.BlocksOfSize(101, blockSize)
if err := inst[0].Blockstore().PutMany(ctx, blks); err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -173,12 +173,11 @@ func TestSessionSplitFetch(t *testing.T) {
vnet := getVirtualNetwork()
ig := testinstance.NewTestInstanceGenerator(vnet, nil, nil)
defer ig.Close()
bgen := blocksutil.NewBlockGenerator()

inst := ig.Instances(11)

// Add 10 distinct blocks to each of 10 peers
blks := bgen.Blocks(100)
blks := random.BlocksOfSize(100, blockSize)
for i := 0; i < 10; i++ {
if err := inst[i].Blockstore().PutMany(ctx, blks[i*10:(i+1)*10]); err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -217,12 +216,11 @@ func TestFetchNotConnected(t *testing.T) {
vnet := getVirtualNetwork()
ig := testinstance.NewTestInstanceGenerator(vnet, nil, []bitswap.Option{bitswap.ProviderSearchDelay(10 * time.Millisecond)})
defer ig.Close()
bgen := blocksutil.NewBlockGenerator()

other := ig.Next()

// Provide 10 blocks on Peer A
blks := bgen.Blocks(10)
blks := random.BlocksOfSize(10, blockSize)
for _, block := range blks {
addBlock(t, ctx, other, block)
}
Expand Down Expand Up @@ -263,14 +261,13 @@ func TestFetchAfterDisconnect(t *testing.T) {
bitswap.RebroadcastDelay(delay.Fixed(15 * time.Millisecond)),
})
defer ig.Close()
bgen := blocksutil.NewBlockGenerator()

inst := ig.Instances(2)
peerA := inst[0]
peerB := inst[1]

// Provide 5 blocks on Peer A
blks := bgen.Blocks(10)
blks := random.BlocksOfSize(10, blockSize)
var cids []cid.Cid
for _, blk := range blks {
cids = append(cids, blk.Cid())
Expand Down Expand Up @@ -338,9 +335,8 @@ func TestInterestCacheOverflow(t *testing.T) {
vnet := getVirtualNetwork()
ig := testinstance.NewTestInstanceGenerator(vnet, nil, nil)
defer ig.Close()
bgen := blocksutil.NewBlockGenerator()

blks := bgen.Blocks(2049)
blks := random.BlocksOfSize(2049, blockSize)
inst := ig.Instances(2)

a := inst[0]
Expand Down Expand Up @@ -388,9 +384,8 @@ func TestPutAfterSessionCacheEvict(t *testing.T) {
vnet := getVirtualNetwork()
ig := testinstance.NewTestInstanceGenerator(vnet, nil, nil)
defer ig.Close()
bgen := blocksutil.NewBlockGenerator()

blks := bgen.Blocks(2500)
blks := random.BlocksOfSize(2500, blockSize)
inst := ig.Instances(1)

a := inst[0]
Expand Down Expand Up @@ -426,9 +421,8 @@ func TestMultipleSessions(t *testing.T) {
vnet := getVirtualNetwork()
ig := testinstance.NewTestInstanceGenerator(vnet, nil, nil)
defer ig.Close()
bgen := blocksutil.NewBlockGenerator()

blk := bgen.Blocks(1)[0]
blk := random.BlocksOfSize(1, blockSize)[0]
inst := ig.Instances(2)

a := inst[0]
Expand Down Expand Up @@ -467,9 +461,8 @@ func TestWantlistClearsOnCancel(t *testing.T) {
vnet := getVirtualNetwork()
ig := testinstance.NewTestInstanceGenerator(vnet, nil, nil)
defer ig.Close()
bgen := blocksutil.NewBlockGenerator()

blks := bgen.Blocks(10)
blks := random.BlocksOfSize(10, blockSize)
var cids []cid.Cid
for _, blk := range blks {
cids = append(cids, blk.Cid())
Expand Down
2 changes: 1 addition & 1 deletion bitswap/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ func (bs *Client) IsOnline() bool {
// block requests in a row. The session returned will have it's own GetBlocks
// method, but the session will use the fact that the requests are related to
// be more efficient in its requests to peers. If you are using a session
// from go-blockservice, it will create a bitswap session automatically.
// from blockservice, it will create a bitswap session automatically.
func (bs *Client) NewSession(ctx context.Context) exchange.Fetcher {
ctx, span := internal.StartSpan(ctx, "NewSession")
defer span.End()
Expand Down
7 changes: 4 additions & 3 deletions bitswap/client/internal/notifications/notifications_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import (

blocks "github.com/ipfs/go-block-format"
cid "github.com/ipfs/go-cid"
blocksutil "github.com/ipfs/go-ipfs-blocksutil"
"github.com/ipfs/go-test/random"
"github.com/libp2p/go-libp2p/core/peer"
)

const blockSize = 4

func TestDuplicates(t *testing.T) {
var zero peer.ID // this test doesn't check the peer id

Expand Down Expand Up @@ -152,13 +154,12 @@ func TestCarryOnWhenDeadlineExpires(t *testing.T) {
func TestDoesNotDeadLockIfContextCancelledBeforePublish(t *testing.T) {
var zero peer.ID // this test doesn't check the peer id

g := blocksutil.NewBlockGenerator()
ctx, cancel := context.WithCancel(context.Background())
n := New()
defer n.Shutdown()

t.Log("generate a large number of blocks. exceed default buffer")
bs := g.Blocks(1000)
bs := random.BlocksOfSize(1000, blockSize)
ks := func() []cid.Cid {
var keys []cid.Cid
for _, b := range bs {
Expand Down
Loading

0 comments on commit 88beadf

Please sign in to comment.