aboutsummaryrefslogtreecommitdiffstats
path: root/blockpool/peers_test.go
diff options
context:
space:
mode:
authorzelig <viktor.tron@gmail.com>2015-04-13 03:25:09 +0800
committerzelig <viktor.tron@gmail.com>2015-04-13 20:13:55 +0800
commit3d57e377a4e95941fd3f572b42e073b40d10d27c (patch)
tree95807260bab2e43c6929d6d4b5c05144ed2fd5c9 /blockpool/peers_test.go
parentfaa2747809ddce7e7b121926ae7dece8fbecae52 (diff)
downloaddexon-3d57e377a4e95941fd3f572b42e073b40d10d27c.tar.gz
dexon-3d57e377a4e95941fd3f572b42e073b40d10d27c.tar.zst
dexon-3d57e377a4e95941fd3f572b42e073b40d10d27c.zip
blockpool stability fixes:
- follow up locks and fix them - chainManager: call SetQueued for parentErr future blocks, uncomment TD checks, unskip test - make ErrIncorrectTD non-fatal to be forgiving to genuine mistaken nodes (temp) but demote them to guard against stuck best peers. - add purging to bounded nodeCache (config nodeCacheSize) - use nodeCache when creating blockpool entries and let non-best peers add blocks (performance boost) - minor error in addError - reduce idleBestPeerTimeout to 1 minute - correct status counts and unskip status passing status test - glogified logging
Diffstat (limited to 'blockpool/peers_test.go')
-rw-r--r--blockpool/peers_test.go113
1 files changed, 71 insertions, 42 deletions
diff --git a/blockpool/peers_test.go b/blockpool/peers_test.go
index e5788e379..639abbc26 100644
--- a/blockpool/peers_test.go
+++ b/blockpool/peers_test.go
@@ -1,23 +1,30 @@
package blockpool
import (
+ "flag"
"math/big"
"testing"
"time"
- "github.com/ethereum/go-ethereum/blockpool/test"
- "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/types"
+ "github.com/ethereum/go-ethereum/logger"
+ "github.com/ethereum/go-ethereum/logger/glog"
+)
+
+var (
+ _ = flag.Set("alsologtostderr", "true")
+ // _ = flag.Set("log_dir", ".")
+ _ = flag.Set("v", "5")
)
// the actual tests
func TestAddPeer(t *testing.T) {
- test.LogInit()
+ glog.V(logger.Error).Infoln("logging...")
hashPool, blockPool, blockPoolTester := newTestBlockPool(t)
- peer0 := blockPoolTester.newPeer("peer0", 1, 1)
- peer1 := blockPoolTester.newPeer("peer1", 2, 2)
- peer2 := blockPoolTester.newPeer("peer2", 3, 3)
+ peer0 := blockPoolTester.newPeer("peer0", 2, 2)
+ peer1 := blockPoolTester.newPeer("peer1", 4, 4)
+ peer2 := blockPoolTester.newPeer("peer2", 6, 6)
var bestpeer *peer
blockPool.Start()
@@ -25,127 +32,149 @@ func TestAddPeer(t *testing.T) {
// pool
best := peer0.AddPeer()
if !best {
- t.Errorf("peer0 (TD=1) not accepted as best")
+ t.Errorf("peer0 (TD=2) not accepted as best")
+ return
}
if blockPool.peers.best.id != "peer0" {
- t.Errorf("peer0 (TD=1) not set as best")
+ t.Errorf("peer0 (TD=2) not set as best")
+ return
}
+ peer0.serveBlocks(1, 2)
best = peer2.AddPeer()
if !best {
- t.Errorf("peer2 (TD=3) not accepted as best")
+ t.Errorf("peer2 (TD=6) not accepted as best")
+ return
}
if blockPool.peers.best.id != "peer2" {
- t.Errorf("peer2 (TD=3) not set as best")
+ t.Errorf("peer2 (TD=6) not set as best")
+ return
}
- peer2.waitBlocksRequests(3)
+ peer2.serveBlocks(5, 6)
best = peer1.AddPeer()
if best {
- t.Errorf("peer1 (TD=2) accepted as best")
+ t.Errorf("peer1 (TD=4) accepted as best")
+ return
}
if blockPool.peers.best.id != "peer2" {
- t.Errorf("peer2 (TD=3) not set any more as best")
+ t.Errorf("peer2 (TD=6) not set any more as best")
+ return
}
- if blockPool.peers.best.td.Cmp(big.NewInt(int64(3))) != 0 {
- t.Errorf("peer1 TD not set")
+ if blockPool.peers.best.td.Cmp(big.NewInt(int64(6))) != 0 {
+ t.Errorf("peer2 TD=6 not set")
+ return
}
- peer2.td = 4
- peer2.currentBlock = 4
+ peer2.td = 8
+ peer2.currentBlock = 8
best = peer2.AddPeer()
if !best {
- t.Errorf("peer2 (TD=4) not accepted as best")
+ t.Errorf("peer2 (TD=8) not accepted as best")
+ return
}
if blockPool.peers.best.id != "peer2" {
- t.Errorf("peer2 (TD=4) not set as best")
+ t.Errorf("peer2 (TD=8) not set as best")
+ return
}
- if blockPool.peers.best.td.Cmp(big.NewInt(int64(4))) != 0 {
- t.Errorf("peer2 TD not updated")
+ if blockPool.peers.best.td.Cmp(big.NewInt(int64(8))) != 0 {
+ t.Errorf("peer2 TD = 8 not updated")
+ return
}
- peer2.waitBlocksRequests(4)
- peer1.td = 3
- peer1.currentBlock = 3
+ peer1.td = 6
+ peer1.currentBlock = 6
best = peer1.AddPeer()
if best {
- t.Errorf("peer1 (TD=3) should not be set as best")
+ t.Errorf("peer1 (TD=6) should not be set as best")
+ return
}
if blockPool.peers.best.id == "peer1" {
- t.Errorf("peer1 (TD=3) should not be set as best")
+ t.Errorf("peer1 (TD=6) should not be set as best")
+ return
}
bestpeer, best = blockPool.peers.getPeer("peer1")
- if bestpeer.td.Cmp(big.NewInt(int64(3))) != 0 {
- t.Errorf("peer1 TD should be updated")
+ if bestpeer.td.Cmp(big.NewInt(int64(6))) != 0 {
+ t.Errorf("peer1 TD=6 should be updated")
+ return
}
blockPool.RemovePeer("peer2")
bestpeer, best = blockPool.peers.getPeer("peer2")
if bestpeer != nil {
t.Errorf("peer2 not removed")
+ return
}
if blockPool.peers.best.id != "peer1" {
- t.Errorf("existing peer1 (TD=3) should be set as best peer")
+ t.Errorf("existing peer1 (TD=6) should be set as best peer")
+ return
}
- peer1.waitBlocksRequests(3)
blockPool.RemovePeer("peer1")
bestpeer, best = blockPool.peers.getPeer("peer1")
if bestpeer != nil {
t.Errorf("peer1 not removed")
+ return
}
if blockPool.peers.best.id != "peer0" {
- t.Errorf("existing peer0 (TD=1) should be set as best peer")
+ t.Errorf("existing peer0 (TD=2) should be set as best peer")
+ return
}
- peer0.waitBlocksRequests(1)
blockPool.RemovePeer("peer0")
bestpeer, best = blockPool.peers.getPeer("peer0")
if bestpeer != nil {
- t.Errorf("peer1 not removed")
+ t.Errorf("peer0 not removed")
+ return
}
// adding back earlier peer ok
- peer0.currentBlock = 3
+ peer0.currentBlock = 5
+ peer0.td = 5
best = peer0.AddPeer()
if !best {
- t.Errorf("peer0 (TD=1) should be set as best")
+ t.Errorf("peer0 (TD=5) should be set as best")
+ return
}
if blockPool.peers.best.id != "peer0" {
- t.Errorf("peer0 (TD=1) should be set as best")
+ t.Errorf("peer0 (TD=5) should be set as best")
+ return
}
- peer0.waitBlocksRequests(3)
+ peer0.serveBlocks(4, 5)
- hash := hashPool.IndexesToHashes([]int{0})[0]
- newblock := &types.Block{Td: common.Big3, HeaderHash: hash}
+ hash := hashPool.IndexesToHashes([]int{6})[0]
+ newblock := &types.Block{Td: big.NewInt(int64(6)), HeaderHash: hash}
blockPool.chainEvents.Post(core.ChainHeadEvent{newblock})
time.Sleep(100 * time.Millisecond)
if blockPool.peers.best != nil {
t.Errorf("no peer should be ahead of self")
+ return
}
best = peer1.AddPeer()
if blockPool.peers.best != nil {
- t.Errorf("still no peer should be ahead of self")
+ t.Errorf("after peer1 (TD=6) still no peer should be ahead of self")
+ return
}
best = peer2.AddPeer()
if !best {
- t.Errorf("peer2 (TD=4) not accepted as best")
+ t.Errorf("peer2 (TD=8) not accepted as best")
+ return
}
blockPool.RemovePeer("peer2")
if blockPool.peers.best != nil {
t.Errorf("no peer should be ahead of self")
+ return
}
blockPool.Stop()
}
func TestPeerPromotionByTdOnBlock(t *testing.T) {
- test.LogInit()
_, blockPool, blockPoolTester := newTestBlockPool(t)
blockPoolTester.blockChain[0] = nil
blockPoolTester.initRefBlockChain(4)