2015-02-25 14:34:12 +02:00
|
|
|
package blockpool
|
|
|
|
|
|
|
|
import (
|
2015-04-12 22:25:09 +03:00
|
|
|
"flag"
|
2015-02-25 14:34:12 +02:00
|
|
|
"math/big"
|
|
|
|
"testing"
|
2015-03-20 00:53:15 +02:00
|
|
|
"time"
|
2015-02-25 14:34:12 +02:00
|
|
|
|
2015-03-20 00:53:15 +02:00
|
|
|
"github.com/ethereum/go-ethereum/core"
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2015-04-12 22:25:09 +03:00
|
|
|
"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")
|
2015-02-25 14:34:12 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// the actual tests
|
|
|
|
func TestAddPeer(t *testing.T) {
|
2015-04-12 22:25:09 +03:00
|
|
|
glog.V(logger.Error).Infoln("logging...")
|
2015-03-25 13:28:17 +02:00
|
|
|
hashPool, blockPool, blockPoolTester := newTestBlockPool(t)
|
2015-04-12 22:25:09 +03:00
|
|
|
peer0 := blockPoolTester.newPeer("peer0", 2, 2)
|
|
|
|
peer1 := blockPoolTester.newPeer("peer1", 4, 4)
|
|
|
|
peer2 := blockPoolTester.newPeer("peer2", 6, 6)
|
2015-02-25 14:34:12 +02:00
|
|
|
var bestpeer *peer
|
|
|
|
|
|
|
|
blockPool.Start()
|
|
|
|
|
|
|
|
// pool
|
|
|
|
best := peer0.AddPeer()
|
|
|
|
if !best {
|
2015-04-12 22:25:09 +03:00
|
|
|
t.Errorf("peer0 (TD=2) not accepted as best")
|
|
|
|
return
|
2015-02-25 14:34:12 +02:00
|
|
|
}
|
|
|
|
if blockPool.peers.best.id != "peer0" {
|
2015-04-12 22:25:09 +03:00
|
|
|
t.Errorf("peer0 (TD=2) not set as best")
|
|
|
|
return
|
2015-02-25 14:34:12 +02:00
|
|
|
}
|
2015-04-12 22:25:09 +03:00
|
|
|
peer0.serveBlocks(1, 2)
|
2015-02-25 14:34:12 +02:00
|
|
|
|
|
|
|
best = peer2.AddPeer()
|
|
|
|
if !best {
|
2015-04-12 22:25:09 +03:00
|
|
|
t.Errorf("peer2 (TD=6) not accepted as best")
|
|
|
|
return
|
2015-02-25 14:34:12 +02:00
|
|
|
}
|
|
|
|
if blockPool.peers.best.id != "peer2" {
|
2015-04-12 22:25:09 +03:00
|
|
|
t.Errorf("peer2 (TD=6) not set as best")
|
|
|
|
return
|
2015-02-25 14:34:12 +02:00
|
|
|
}
|
2015-04-12 22:25:09 +03:00
|
|
|
peer2.serveBlocks(5, 6)
|
2015-02-25 14:34:12 +02:00
|
|
|
|
|
|
|
best = peer1.AddPeer()
|
|
|
|
if best {
|
2015-04-12 22:25:09 +03:00
|
|
|
t.Errorf("peer1 (TD=4) accepted as best")
|
|
|
|
return
|
2015-02-25 14:34:12 +02:00
|
|
|
}
|
|
|
|
if blockPool.peers.best.id != "peer2" {
|
2015-04-12 22:25:09 +03:00
|
|
|
t.Errorf("peer2 (TD=6) not set any more as best")
|
|
|
|
return
|
2015-02-25 14:34:12 +02:00
|
|
|
}
|
2015-04-12 22:25:09 +03:00
|
|
|
if blockPool.peers.best.td.Cmp(big.NewInt(int64(6))) != 0 {
|
|
|
|
t.Errorf("peer2 TD=6 not set")
|
|
|
|
return
|
2015-02-25 14:34:12 +02:00
|
|
|
}
|
|
|
|
|
2015-04-12 22:25:09 +03:00
|
|
|
peer2.td = 8
|
|
|
|
peer2.currentBlock = 8
|
2015-02-25 14:34:12 +02:00
|
|
|
best = peer2.AddPeer()
|
|
|
|
if !best {
|
2015-04-12 22:25:09 +03:00
|
|
|
t.Errorf("peer2 (TD=8) not accepted as best")
|
|
|
|
return
|
2015-02-25 14:34:12 +02:00
|
|
|
}
|
|
|
|
if blockPool.peers.best.id != "peer2" {
|
2015-04-12 22:25:09 +03:00
|
|
|
t.Errorf("peer2 (TD=8) not set as best")
|
|
|
|
return
|
2015-02-25 14:34:12 +02:00
|
|
|
}
|
2015-04-12 22:25:09 +03:00
|
|
|
if blockPool.peers.best.td.Cmp(big.NewInt(int64(8))) != 0 {
|
|
|
|
t.Errorf("peer2 TD = 8 not updated")
|
|
|
|
return
|
2015-02-25 14:34:12 +02:00
|
|
|
}
|
|
|
|
|
2015-04-12 22:25:09 +03:00
|
|
|
peer1.td = 6
|
|
|
|
peer1.currentBlock = 6
|
2015-02-25 14:34:12 +02:00
|
|
|
best = peer1.AddPeer()
|
|
|
|
if best {
|
2015-04-12 22:25:09 +03:00
|
|
|
t.Errorf("peer1 (TD=6) should not be set as best")
|
|
|
|
return
|
2015-02-25 14:34:12 +02:00
|
|
|
}
|
|
|
|
if blockPool.peers.best.id == "peer1" {
|
2015-04-12 22:25:09 +03:00
|
|
|
t.Errorf("peer1 (TD=6) should not be set as best")
|
|
|
|
return
|
2015-02-25 14:34:12 +02:00
|
|
|
}
|
|
|
|
bestpeer, best = blockPool.peers.getPeer("peer1")
|
2015-04-12 22:25:09 +03:00
|
|
|
if bestpeer.td.Cmp(big.NewInt(int64(6))) != 0 {
|
|
|
|
t.Errorf("peer1 TD=6 should be updated")
|
|
|
|
return
|
2015-02-25 14:34:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
blockPool.RemovePeer("peer2")
|
|
|
|
bestpeer, best = blockPool.peers.getPeer("peer2")
|
|
|
|
if bestpeer != nil {
|
|
|
|
t.Errorf("peer2 not removed")
|
2015-04-12 22:25:09 +03:00
|
|
|
return
|
2015-02-25 14:34:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if blockPool.peers.best.id != "peer1" {
|
2015-04-12 22:25:09 +03:00
|
|
|
t.Errorf("existing peer1 (TD=6) should be set as best peer")
|
|
|
|
return
|
2015-02-25 14:34:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
blockPool.RemovePeer("peer1")
|
|
|
|
bestpeer, best = blockPool.peers.getPeer("peer1")
|
|
|
|
if bestpeer != nil {
|
|
|
|
t.Errorf("peer1 not removed")
|
2015-04-12 22:25:09 +03:00
|
|
|
return
|
2015-02-25 14:34:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if blockPool.peers.best.id != "peer0" {
|
2015-04-12 22:25:09 +03:00
|
|
|
t.Errorf("existing peer0 (TD=2) should be set as best peer")
|
|
|
|
return
|
2015-02-25 14:34:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
blockPool.RemovePeer("peer0")
|
|
|
|
bestpeer, best = blockPool.peers.getPeer("peer0")
|
|
|
|
if bestpeer != nil {
|
2015-04-12 22:25:09 +03:00
|
|
|
t.Errorf("peer0 not removed")
|
|
|
|
return
|
2015-02-25 14:34:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// adding back earlier peer ok
|
2015-04-12 22:25:09 +03:00
|
|
|
peer0.currentBlock = 5
|
|
|
|
peer0.td = 5
|
2015-02-25 14:34:12 +02:00
|
|
|
best = peer0.AddPeer()
|
|
|
|
if !best {
|
2015-04-12 22:25:09 +03:00
|
|
|
t.Errorf("peer0 (TD=5) should be set as best")
|
|
|
|
return
|
2015-02-25 14:34:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if blockPool.peers.best.id != "peer0" {
|
2015-04-12 22:25:09 +03:00
|
|
|
t.Errorf("peer0 (TD=5) should be set as best")
|
|
|
|
return
|
2015-02-25 14:34:12 +02:00
|
|
|
}
|
2015-04-12 22:25:09 +03:00
|
|
|
peer0.serveBlocks(4, 5)
|
2015-02-25 14:34:12 +02:00
|
|
|
|
2015-04-12 22:25:09 +03:00
|
|
|
hash := hashPool.IndexesToHashes([]int{6})[0]
|
|
|
|
newblock := &types.Block{Td: big.NewInt(int64(6)), HeaderHash: hash}
|
2015-03-20 00:53:15 +02:00
|
|
|
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")
|
2015-04-12 22:25:09 +03:00
|
|
|
return
|
2015-03-20 00:53:15 +02:00
|
|
|
}
|
|
|
|
best = peer1.AddPeer()
|
|
|
|
if blockPool.peers.best != nil {
|
2015-04-12 22:25:09 +03:00
|
|
|
t.Errorf("after peer1 (TD=6) still no peer should be ahead of self")
|
|
|
|
return
|
2015-03-20 00:53:15 +02:00
|
|
|
}
|
2015-02-25 14:34:12 +02:00
|
|
|
|
2015-03-20 00:53:15 +02:00
|
|
|
best = peer2.AddPeer()
|
|
|
|
if !best {
|
2015-04-12 22:25:09 +03:00
|
|
|
t.Errorf("peer2 (TD=8) not accepted as best")
|
|
|
|
return
|
2015-03-20 00:53:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
blockPool.RemovePeer("peer2")
|
|
|
|
if blockPool.peers.best != nil {
|
|
|
|
t.Errorf("no peer should be ahead of self")
|
2015-04-12 22:25:09 +03:00
|
|
|
return
|
2015-03-20 00:53:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
blockPool.Stop()
|
2015-02-25 14:34:12 +02:00
|
|
|
}
|
2015-03-20 01:14:08 +02:00
|
|
|
|
2015-04-08 05:34:20 +03:00
|
|
|
func TestPeerPromotionByTdOnBlock(t *testing.T) {
|
2015-03-20 01:14:08 +02:00
|
|
|
_, blockPool, blockPoolTester := newTestBlockPool(t)
|
|
|
|
blockPoolTester.blockChain[0] = nil
|
|
|
|
blockPoolTester.initRefBlockChain(4)
|
|
|
|
peer0 := blockPoolTester.newPeer("peer0", 2, 2)
|
|
|
|
peer1 := blockPoolTester.newPeer("peer1", 1, 1)
|
|
|
|
peer2 := blockPoolTester.newPeer("peer2", 4, 4)
|
|
|
|
|
|
|
|
blockPool.Start()
|
|
|
|
|
|
|
|
peer0.AddPeer()
|
|
|
|
peer0.serveBlocks(1, 2)
|
|
|
|
best := peer1.AddPeer()
|
|
|
|
// this tests that peer1 is not promoted over peer0 yet
|
|
|
|
if best {
|
|
|
|
t.Errorf("peer1 (TD=1) should not be set as best")
|
2015-04-10 18:31:00 +03:00
|
|
|
return
|
2015-03-20 01:14:08 +02:00
|
|
|
}
|
|
|
|
best = peer2.AddPeer()
|
|
|
|
peer2.serveBlocks(3, 4)
|
|
|
|
peer2.serveBlockHashes(4, 3, 2, 1)
|
2015-04-10 18:31:00 +03:00
|
|
|
peer1.sendBlocks(3, 4)
|
2015-03-20 01:14:08 +02:00
|
|
|
|
|
|
|
blockPool.RemovePeer("peer2")
|
|
|
|
if blockPool.peers.best.id != "peer1" {
|
|
|
|
t.Errorf("peer1 (TD=3) should be set as best")
|
2015-04-10 18:31:00 +03:00
|
|
|
return
|
2015-03-20 01:14:08 +02:00
|
|
|
}
|
2015-04-10 18:31:00 +03:00
|
|
|
peer1.serveBlocks(0, 1, 2, 3)
|
2015-03-20 01:14:08 +02:00
|
|
|
|
|
|
|
blockPool.Wait(waitTimeout)
|
|
|
|
blockPool.Stop()
|
|
|
|
blockPoolTester.refBlockChain[4] = []int{}
|
|
|
|
blockPoolTester.checkBlockChain(blockPoolTester.refBlockChain)
|
|
|
|
}
|