diff options
author | Mission Liao <mission.liao@dexon.org> | 2019-04-02 22:04:28 +0800 |
---|---|---|
committer | Wei-Ning Huang <w@dexon.org> | 2019-04-09 21:32:59 +0800 |
commit | e02415d6224060ca9d7c3cef9005c729ac6b6c05 (patch) | |
tree | 9a15cf979ba16ea9990af921ff518dc6882fc0fb /dex/protocol_test.go | |
parent | 5d3ac45f9300821560509be0e3be38be55ca992a (diff) | |
download | dexon-e02415d6224060ca9d7c3cef9005c729ac6b6c05.tar dexon-e02415d6224060ca9d7c3cef9005c729ac6b6c05.tar.gz dexon-e02415d6224060ca9d7c3cef9005c729ac6b6c05.tar.bz2 dexon-e02415d6224060ca9d7c3cef9005c729ac6b6c05.tar.lz dexon-e02415d6224060ca9d7c3cef9005c729ac6b6c05.tar.xz dexon-e02415d6224060ca9d7c3cef9005c729ac6b6c05.tar.zst dexon-e02415d6224060ca9d7c3cef9005c729ac6b6c05.zip |
dex: try to reduce the chance to pull non-finalized blocks (#327)
* Send non-finalized blocks to notary set only
* Update randomness field for blocks in cache upon receiving agreement
result
* Filter AgreementResult by its position
* Avoid overwriting finalized blocks with non-finalized ones
* Add blocks to finalized cache when pulling
* Update to finalized cache when we have corresponding element in
non-finalized one.
Diffstat (limited to 'dex/protocol_test.go')
-rw-r--r-- | dex/protocol_test.go | 79 |
1 files changed, 64 insertions, 15 deletions
diff --git a/dex/protocol_test.go b/dex/protocol_test.go index d6bebc18a..51bd32c72 100644 --- a/dex/protocol_test.go +++ b/dex/protocol_test.go @@ -364,10 +364,7 @@ func TestRecvCoreBlocks(t *testing.T) { func TestSendCoreBlocks(t *testing.T) { pm, _ := newTestProtocolManagerMust(t, downloader.FullSync, 0, nil, nil) pm.SetReceiveCoreMessage(true) - - p, _ := newTestPeer("peer", dex64, pm, true) defer pm.Stop() - defer p.close() block := coreTypes.Block{ ProposerID: coreTypes.NodeID{coreCommon.Hash{1, 2, 3}}, @@ -394,23 +391,75 @@ func TestSendCoreBlocks(t *testing.T) { }, } - waitForRegister(pm, 1) - pm.BroadcastCoreBlock(&block) - msg, err := p.app.ReadMsg() - if err != nil { - t.Errorf("%v: read error: %v", p.Peer, err) - } else if msg.Code != CoreBlockMsg { - t.Errorf("%v: got code %d, want %d", p.Peer, msg.Code, CoreBlockMsg) + var wg sync.WaitGroup + checkBlock := func(p *testPeer, isReceiver bool) { + defer wg.Done() + defer p.close() + if !isReceiver { + go func() { + time.Sleep(100 * time.Millisecond) + p.close() + }() + } + + msg, err := p.app.ReadMsg() + if !isReceiver { + if err != p2p.ErrPipeClosed { + t.Errorf("err mismatch: got %v, want %v (not receiver peer)", + err, p2p.ErrPipeClosed) + } + return + } + if err != nil { + t.Errorf("%v: read error: %v", p.Peer, err) + } else if msg.Code != CoreBlockMsg { + t.Errorf("%v: got code %d, want %d", p.Peer, msg.Code, CoreBlockMsg) + } + + var bs []*coreTypes.Block + if err := msg.Decode(&bs); err != nil { + t.Errorf("%v: %v", p.Peer, err) + } + + if !reflect.DeepEqual(bs, []*coreTypes.Block{&block}) { + t.Errorf("block mismatch") + } } - var bs []*coreTypes.Block - if err := msg.Decode(&bs); err != nil { - t.Errorf("%v: %v", p.Peer, err) + testPeers := []struct { + label *peerLabel + isReceiver bool + }{ + { + label: &peerLabel{set: notaryset, round: 12}, + isReceiver: true, + }, + { + label: nil, + isReceiver: false, + }, + { + label: &peerLabel{set: notaryset, round: 11}, + isReceiver: false, + }, } - if !reflect.DeepEqual(bs, []*coreTypes.Block{&block}) { - t.Errorf("block mismatch") + pm.peers.label2Nodes = make(map[peerLabel]map[string]*enode.Node) + for i, tt := range testPeers { + p, _ := newTestPeer(fmt.Sprintf("peer #%d", i), dex64, pm, true) + if tt.label != nil { + if pm.peers.label2Nodes[*tt.label] == nil { + pm.peers.label2Nodes[*tt.label] = make(map[string]*enode.Node) + } + pm.peers.label2Nodes[*tt.label][p.ID().String()] = p.Node() + pm.peers.addDirectPeer(p.ID().String(), *tt.label) + } + wg.Add(1) + go checkBlock(p, tt.isReceiver) } + waitForRegister(pm, len(testPeers)) + pm.BroadcastCoreBlock(&block) + wg.Wait() } func TestRecvVotes(t *testing.T) { |