diff options
author | zelig <viktor.tron@gmail.com> | 2015-02-25 20:34:12 +0800 |
---|---|---|
committer | zelig <viktor.tron@gmail.com> | 2015-02-25 20:34:12 +0800 |
commit | 422490d75cf9a2406430f2d7c0d7dd77ede18f7c (patch) | |
tree | 63860f0914370bec71cac6f1708476da4f7533cc /blockpool/peers_test.go | |
parent | d46c7bcaf9268a191f0156d36abf394df5374795 (diff) | |
download | dexon-422490d75cf9a2406430f2d7c0d7dd77ede18f7c.tar.gz dexon-422490d75cf9a2406430f2d7c0d7dd77ede18f7c.tar.zst dexon-422490d75cf9a2406430f2d7c0d7dd77ede18f7c.zip |
major rewrite, reorg of blockpool + new features
- blockpool moves to its own package
- uses errs pkg for its own coded errors
- publicly settable config of params (time intervals and batchsizes)
- test helpers in subpackage
- optional TD in blocks used now to update peers chain info
- major improvement in algorithm
- fix fragility and sync/parallelisation bugs
- implement status for reporting on sync status (peers/hashes/blocks etc)
- several tests added and further corner cases covered
Diffstat (limited to 'blockpool/peers_test.go')
-rw-r--r-- | blockpool/peers_test.go | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/blockpool/peers_test.go b/blockpool/peers_test.go new file mode 100644 index 000000000..e53d7160b --- /dev/null +++ b/blockpool/peers_test.go @@ -0,0 +1,120 @@ +package blockpool + +import ( + "math/big" + "testing" + + "github.com/ethereum/go-ethereum/blockpool/test" +) + +// the actual tests +func TestAddPeer(t *testing.T) { + test.LogInit() + _, blockPool, blockPoolTester := newTestBlockPool(t) + peer0 := blockPoolTester.newPeer("peer0", 1, 0) + peer1 := blockPoolTester.newPeer("peer1", 2, 1) + peer2 := blockPoolTester.newPeer("peer2", 3, 2) + var bestpeer *peer + + blockPool.Start() + + // pool + best := peer0.AddPeer() + if !best { + t.Errorf("peer0 (TD=1) not accepted as best") + } + if blockPool.peers.best.id != "peer0" { + t.Errorf("peer0 (TD=1) not set as best") + } + + best = peer2.AddPeer() + if !best { + t.Errorf("peer2 (TD=3) not accepted as best") + } + if blockPool.peers.best.id != "peer2" { + t.Errorf("peer2 (TD=3) not set as best") + } + peer2.waitBlocksRequests(2) + + best = peer1.AddPeer() + if best { + t.Errorf("peer1 (TD=2) accepted as best") + } + if blockPool.peers.best.id != "peer2" { + t.Errorf("peer2 (TD=3) not set any more as best") + } + if blockPool.peers.best.td.Cmp(big.NewInt(int64(3))) != 0 { + t.Errorf("peer1 TD not set") + } + + peer2.td = 4 + peer2.currentBlock = 3 + best = peer2.AddPeer() + if !best { + t.Errorf("peer2 (TD=4) not accepted as best") + } + if blockPool.peers.best.id != "peer2" { + t.Errorf("peer2 (TD=4) not set as best") + } + if blockPool.peers.best.td.Cmp(big.NewInt(int64(4))) != 0 { + t.Errorf("peer2 TD not updated") + } + peer2.waitBlocksRequests(3) + + peer1.td = 3 + peer1.currentBlock = 2 + best = peer1.AddPeer() + if best { + t.Errorf("peer1 (TD=3) should not be set as best") + } + if blockPool.peers.best.id == "peer1" { + t.Errorf("peer1 (TD=3) should not be set as best") + } + bestpeer, best = blockPool.peers.getPeer("peer1") + if bestpeer.td.Cmp(big.NewInt(int64(3))) != 0 { + t.Errorf("peer1 TD should be updated") + } + + blockPool.RemovePeer("peer2") + bestpeer, best = blockPool.peers.getPeer("peer2") + if bestpeer != nil { + t.Errorf("peer2 not removed") + } + + if blockPool.peers.best.id != "peer1" { + t.Errorf("existing peer1 (TD=3) should be set as best peer") + } + peer1.waitBlocksRequests(2) + + blockPool.RemovePeer("peer1") + bestpeer, best = blockPool.peers.getPeer("peer1") + if bestpeer != nil { + t.Errorf("peer1 not removed") + } + + if blockPool.peers.best.id != "peer0" { + t.Errorf("existing peer0 (TD=1) should be set as best peer") + } + peer0.waitBlocksRequests(0) + + blockPool.RemovePeer("peer0") + bestpeer, best = blockPool.peers.getPeer("peer0") + if bestpeer != nil { + t.Errorf("peer1 not removed") + } + + // adding back earlier peer ok + peer0.currentBlock = 3 + best = peer0.AddPeer() + if !best { + t.Errorf("peer0 (TD=1) should be set as best") + } + + if blockPool.peers.best.id != "peer0" { + t.Errorf("peer0 (TD=1) should be set as best") + } + peer0.waitBlocksRequests(3) + + blockPool.Stop() + +} |