2015-02-25 14:34:12 +02:00
|
|
|
package blockpool
|
|
|
|
|
|
|
|
import (
|
2015-03-23 23:34:05 +02:00
|
|
|
"fmt"
|
2015-02-25 14:34:12 +02:00
|
|
|
"testing"
|
2015-03-19 02:02:39 +02:00
|
|
|
"time"
|
2015-02-25 14:34:12 +02:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/blockpool/test"
|
|
|
|
)
|
|
|
|
|
|
|
|
var statusFields = []string{
|
|
|
|
"BlockHashes",
|
|
|
|
"BlockHashesInPool",
|
|
|
|
"Blocks",
|
|
|
|
"BlocksInPool",
|
|
|
|
"BlocksInChain",
|
|
|
|
"NewBlocks",
|
|
|
|
"Forks",
|
|
|
|
"LongestChain",
|
|
|
|
"Peers",
|
|
|
|
"LivePeers",
|
|
|
|
"ActivePeers",
|
|
|
|
"BestPeers",
|
|
|
|
"BadPeers",
|
|
|
|
}
|
|
|
|
|
|
|
|
func getStatusValues(s *Status) []int {
|
|
|
|
return []int{
|
|
|
|
s.BlockHashes,
|
|
|
|
s.BlockHashesInPool,
|
|
|
|
s.Blocks,
|
|
|
|
s.BlocksInPool,
|
|
|
|
s.BlocksInChain,
|
|
|
|
s.NewBlocks,
|
|
|
|
s.Forks,
|
|
|
|
s.LongestChain,
|
|
|
|
s.Peers,
|
|
|
|
s.LivePeers,
|
|
|
|
s.ActivePeers,
|
|
|
|
s.BestPeers,
|
|
|
|
s.BadPeers,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkStatus(t *testing.T, bp *BlockPool, syncing bool, expected []int) (err error) {
|
|
|
|
s := bp.Status()
|
|
|
|
if s.Syncing != syncing {
|
2015-03-23 23:34:05 +02:00
|
|
|
err = fmt.Errorf("status for Syncing incorrect. expected %v, got %v", syncing, s.Syncing)
|
|
|
|
return
|
2015-02-25 14:34:12 +02:00
|
|
|
}
|
|
|
|
got := getStatusValues(s)
|
|
|
|
for i, v := range expected {
|
|
|
|
err = test.CheckInt(statusFields[i], got[i], v, t)
|
|
|
|
if err != nil {
|
2015-03-23 23:34:05 +02:00
|
|
|
return
|
2015-02-25 14:34:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-03-19 02:02:39 +02:00
|
|
|
func TestBlockPoolStatus(t *testing.T) {
|
2015-03-23 23:34:05 +02:00
|
|
|
var err error
|
|
|
|
n := 3
|
|
|
|
for n > 0 {
|
|
|
|
n--
|
|
|
|
err = testBlockPoolStatus(t)
|
|
|
|
if err != nil {
|
|
|
|
t.Log(err)
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("no pass out of 3: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testBlockPoolStatus(t *testing.T) (err error) {
|
|
|
|
|
2015-03-19 02:02:39 +02:00
|
|
|
_, blockPool, blockPoolTester := newTestBlockPool(t)
|
|
|
|
blockPoolTester.blockChain[0] = nil
|
|
|
|
blockPoolTester.initRefBlockChain(12)
|
|
|
|
blockPoolTester.refBlockChain[3] = []int{4, 7}
|
2015-04-12 22:25:09 +03:00
|
|
|
blockPoolTester.refBlockChain[5] = []int{10}
|
|
|
|
blockPoolTester.refBlockChain[6] = []int{11}
|
|
|
|
blockPoolTester.refBlockChain[9] = []int{6}
|
|
|
|
delete(blockPoolTester.refBlockChain, 10)
|
2015-03-19 02:02:39 +02:00
|
|
|
|
|
|
|
blockPool.Start()
|
2015-04-07 20:29:35 +03:00
|
|
|
|
2015-04-12 22:25:09 +03:00
|
|
|
peer1 := blockPoolTester.newPeer("peer1", 9, 9)
|
|
|
|
peer2 := blockPoolTester.newPeer("peer2", 10, 10)
|
|
|
|
peer3 := blockPoolTester.newPeer("peer3", 11, 11)
|
|
|
|
peer4 := blockPoolTester.newPeer("peer4", 9, 9)
|
2015-03-19 02:02:39 +02:00
|
|
|
peer2.blocksRequestsMap = peer1.blocksRequestsMap
|
|
|
|
|
|
|
|
var expected []int
|
|
|
|
expected = []int{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
|
2015-03-23 23:34:05 +02:00
|
|
|
err = checkStatus(nil, blockPool, false, expected)
|
2015-03-19 02:02:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
peer1.AddPeer()
|
2015-04-07 20:29:35 +03:00
|
|
|
|
2015-03-19 02:02:39 +02:00
|
|
|
expected = []int{0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 0}
|
2015-03-23 23:34:05 +02:00
|
|
|
err = checkStatus(nil, blockPool, true, expected)
|
2015-03-19 02:02:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
peer1.serveBlocks(8, 9)
|
2015-03-23 23:34:05 +02:00
|
|
|
expected = []int{1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0}
|
|
|
|
err = checkStatus(nil, blockPool, true, expected)
|
2015-03-19 02:02:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
peer1.serveBlockHashes(9, 8, 7, 3, 2)
|
2015-04-12 22:25:09 +03:00
|
|
|
expected = []int{5, 5, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0}
|
2015-03-23 23:34:05 +02:00
|
|
|
err = checkStatus(nil, blockPool, true, expected)
|
2015-03-19 02:02:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
peer1.serveBlocks(3, 7, 8)
|
2015-04-12 22:25:09 +03:00
|
|
|
expected = []int{5, 5, 3, 3, 0, 1, 0, 0, 1, 1, 1, 1, 0}
|
2015-03-23 23:34:05 +02:00
|
|
|
err = checkStatus(nil, blockPool, true, expected)
|
2015-03-19 02:02:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
peer1.serveBlocks(2, 3)
|
2015-04-12 22:25:09 +03:00
|
|
|
expected = []int{5, 5, 4, 4, 0, 1, 0, 0, 1, 1, 1, 1, 0}
|
2015-03-23 23:34:05 +02:00
|
|
|
err = checkStatus(nil, blockPool, true, expected)
|
2015-03-19 02:02:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
peer4.AddPeer()
|
2015-04-12 22:25:09 +03:00
|
|
|
expected = []int{5, 5, 4, 4, 0, 2, 0, 0, 2, 2, 1, 1, 0}
|
2015-03-23 23:34:05 +02:00
|
|
|
err = checkStatus(nil, blockPool, true, expected)
|
2015-03-19 02:02:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
peer2.AddPeer()
|
2015-04-12 22:25:09 +03:00
|
|
|
expected = []int{5, 5, 4, 4, 0, 3, 0, 0, 3, 3, 1, 2, 0}
|
2015-03-23 23:34:05 +02:00
|
|
|
err = checkStatus(nil, blockPool, true, expected)
|
2015-03-19 02:02:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-04-12 22:25:09 +03:00
|
|
|
peer2.serveBlocks(5, 10)
|
|
|
|
peer2.serveBlockHashes(10, 5, 4, 3, 2)
|
|
|
|
expected = []int{8, 8, 5, 5, 0, 3, 1, 0, 3, 3, 2, 2, 0}
|
2015-03-23 23:34:05 +02:00
|
|
|
err = checkStatus(nil, blockPool, true, expected)
|
2015-03-19 02:02:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
peer2.serveBlocks(2, 3, 4)
|
2015-04-12 22:25:09 +03:00
|
|
|
expected = []int{8, 8, 6, 6, 0, 3, 1, 0, 3, 3, 2, 2, 0}
|
2015-03-23 23:34:05 +02:00
|
|
|
err = checkStatus(nil, blockPool, true, expected)
|
2015-03-19 02:02:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
blockPool.RemovePeer("peer2")
|
2015-04-12 22:25:09 +03:00
|
|
|
expected = []int{8, 8, 6, 6, 0, 3, 1, 0, 3, 2, 2, 2, 0}
|
2015-03-23 23:34:05 +02:00
|
|
|
err = checkStatus(nil, blockPool, true, expected)
|
2015-03-19 02:02:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
peer1.serveBlockHashes(2, 1, 0)
|
2015-04-12 22:25:09 +03:00
|
|
|
expected = []int{9, 9, 6, 6, 0, 3, 1, 0, 3, 2, 2, 2, 0}
|
2015-03-23 23:34:05 +02:00
|
|
|
err = checkStatus(nil, blockPool, true, expected)
|
2015-03-19 02:02:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
peer1.serveBlocks(1, 2)
|
2015-04-12 22:25:09 +03:00
|
|
|
expected = []int{9, 9, 7, 7, 0, 3, 1, 0, 3, 2, 2, 2, 0}
|
2015-03-23 23:34:05 +02:00
|
|
|
err = checkStatus(nil, blockPool, true, expected)
|
2015-03-19 02:02:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
peer1.serveBlocks(4, 5)
|
2015-04-12 22:25:09 +03:00
|
|
|
expected = []int{9, 9, 8, 8, 0, 3, 1, 0, 3, 2, 2, 2, 0}
|
2015-03-23 23:34:05 +02:00
|
|
|
err = checkStatus(nil, blockPool, true, expected)
|
2015-03-19 02:02:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
peer3.AddPeer()
|
2015-04-12 22:25:09 +03:00
|
|
|
expected = []int{9, 9, 8, 8, 0, 4, 1, 0, 4, 3, 2, 3, 0}
|
2015-03-23 23:34:05 +02:00
|
|
|
err = checkStatus(nil, blockPool, true, expected)
|
2015-03-19 02:02:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-04-12 22:25:09 +03:00
|
|
|
peer3.serveBlocks(6, 11)
|
|
|
|
expected = []int{10, 9, 9, 9, 0, 4, 1, 0, 4, 3, 3, 3, 0}
|
2015-03-23 23:34:05 +02:00
|
|
|
err = checkStatus(nil, blockPool, true, expected)
|
2015-03-19 02:02:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-04-12 22:25:09 +03:00
|
|
|
peer3.serveBlockHashes(11, 6, 9)
|
|
|
|
expected = []int{11, 11, 9, 9, 0, 4, 1, 0, 4, 3, 3, 3, 0}
|
2015-03-23 23:34:05 +02:00
|
|
|
err = checkStatus(nil, blockPool, true, expected)
|
2015-03-19 02:02:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
peer4.sendBlocks(11, 12)
|
2015-04-12 22:25:09 +03:00
|
|
|
expected = []int{11, 11, 9, 9, 0, 4, 1, 0, 4, 3, 4, 3, 0}
|
2015-03-23 23:34:05 +02:00
|
|
|
err = checkStatus(nil, blockPool, true, expected)
|
2015-03-19 02:02:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2015-04-12 22:25:09 +03:00
|
|
|
peer3.serveBlocks(9, 6)
|
|
|
|
expected = []int{11, 11, 10, 10, 0, 4, 1, 0, 4, 3, 4, 3, 0}
|
2015-03-23 23:34:05 +02:00
|
|
|
err = checkStatus(nil, blockPool, true, expected)
|
2015-03-19 02:02:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
peer3.serveBlocks(0, 1)
|
|
|
|
blockPool.Wait(waitTimeout)
|
|
|
|
time.Sleep(200 * time.Millisecond)
|
2015-04-07 20:29:35 +03:00
|
|
|
|
2015-04-12 22:25:09 +03:00
|
|
|
expected = []int{11, 3, 11, 3, 8, 4, 1, 8, 4, 3, 4, 3, 0}
|
2015-03-23 23:34:05 +02:00
|
|
|
err = checkStatus(nil, blockPool, false, expected)
|
2015-04-12 22:25:09 +03:00
|
|
|
blockPool.Stop()
|
|
|
|
|
2015-03-19 02:02:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2015-03-23 23:34:05 +02:00
|
|
|
return nil
|
2015-03-19 02:02:39 +02:00
|
|
|
}
|