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

Commit

Permalink
test: WIP add TestPeerBlockFilterMutability
Browse files Browse the repository at this point in the history
  • Loading branch information
laurentsenta committed Mar 3, 2022
1 parent b34ef19 commit dbe1555
Showing 1 changed file with 167 additions and 0 deletions.
167 changes: 167 additions & 0 deletions internal/decision/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1279,6 +1279,173 @@ func TestPeerBlockFilter(t *testing.T) {
}
}

func TestPeerBlockFilterMutability(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()

// Generate a few keys
keys := []string{"a", "b", "c"}
blks := make([]blocks.Block, 0, len(keys))
for _, letter := range keys {
block := blocks.NewBlock([]byte(letter))
blks = append(blks, block)
}

// Generate a few partner peers
partnerID := libp2ptest.RandPeerIDFatal(t)

// Setup the main peer
fpt := &fakePeerTagger{}
sl := NewTestScoreLedger(shortTerm, nil, clock.New())
bs := blockstore.NewBlockstore(dssync.MutexWrap(ds.NewMapDatastore()))
if err := bs.PutMany(ctx, blks); err != nil {
t.Fatal(err)
}

filterAllowList := make(map[cid.Cid]bool)

// use a single task worker so that the order of outgoing messages is deterministic
engineTaskWorkerCount := 1
e := newEngineForTesting(ctx, bs, 4, engineTaskWorkerCount, defaults.BitswapMaxOutstandingBytesPerPeer, fpt, "localhost", 0, sl,
WithPeerBlockRequestFilter(func(p peer.ID, c cid.Cid) bool {
return filterAllowList[c]
}),
)
e.StartWorkers(ctx, process.WithTeardown(func() error { return nil }))

// Setup the test
type testCaseEntry struct {
allowList string
wantBlks string
wantHaves string
sendDontHave bool
}

type testCaseExp struct {
blks string
haves string
dontHaves string
}

type testCase struct {
only bool
wls []testCaseEntry
exps []testCaseExp
}

testCases := []testCase{
{
wls: []testCaseEntry{
{
// Peer has no accesses
allowList: "",
wantBlks: "a",
sendDontHave: true,
},
{
// Then Peer is allowed access to a
allowList: "a",
wantBlks: "a",
sendDontHave: true,
},
// If you run the code below, the test will fail with a sigsev,
// because the response is nil, the node pretty much ignores the request.
// {
// // Peer has no accesses
// allowList: "",
// wantBlks: "a",
// sendDontHave: true,
// },
},
exps: []testCaseExp{
{
dontHaves: "a",
},
{
blks: "a",
},
{
dontHaves: "a",
},
},
},
{
wls: []testCaseEntry{
{
// Peer has access to bc
allowList: "bc",
wantHaves: "bc",
sendDontHave: true,
},
{
// Then Peer loses access to b
allowList: "c",
// Note: We request block here to force a response from the node
wantBlks: "bc",
sendDontHave: true,
},
},
exps: []testCaseExp{
{
haves: "bc",
},
{
blks: "c",
dontHaves: "b",
},
},
},
}

var onlyTestCases []testCase
for _, testCase := range testCases {
if testCase.only {
onlyTestCases = append(onlyTestCases, testCase)
}
}
if len(onlyTestCases) > 0 {
testCases = onlyTestCases
}

for i, testCase := range testCases {
for j := range testCase.wls {
wl := testCase.wls[j]
exp := testCase.exps[j]

// Create wants requests
t.Logf("test case %v, %v: allow-list '%s' / want-blocks '%s' / want-haves '%s' / sendDontHave %t",
i, j, wl.allowList, wl.wantBlks, wl.wantHaves, wl.sendDontHave)

allowList := strings.Split(wl.allowList, "")
wantBlks := strings.Split(wl.wantBlks, "")
wantHaves := strings.Split(wl.wantHaves, "")

// Update the allow list
filterAllowList = make(map[cid.Cid]bool)
for _, letter := range allowList {
block := blocks.NewBlock([]byte(letter))
filterAllowList[block.Cid()] = true
}

// Send the request
partnerWantBlocksHaves(e, wantBlks, wantHaves, wl.sendDontHave, partnerID)

// Check result
next := <-e.Outbox()
envelope := <-next

expBlks := strings.Split(exp.blks, "")
expHaves := strings.Split(exp.haves, "")
expDontHaves := strings.Split(exp.dontHaves, "")

err := checkOutput(t, e, envelope, expBlks, expHaves, expDontHaves)
if err != nil {
t.Fatal(err)
}
}
}
}

func TestTaggingPeers(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
Expand Down

0 comments on commit dbe1555

Please sign in to comment.