2017-03-25 01:11:50 +01:00
|
|
|
package spvchain_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2017-04-11 21:24:26 +02:00
|
|
|
"github.com/aakselrod/btctestlog"
|
2017-03-25 01:11:50 +01:00
|
|
|
"github.com/btcsuite/btcd/chaincfg"
|
|
|
|
"github.com/btcsuite/btcd/rpctest"
|
|
|
|
"github.com/btcsuite/btcd/wire"
|
2017-04-11 21:24:26 +02:00
|
|
|
"github.com/btcsuite/btclog"
|
2017-03-25 01:11:50 +01:00
|
|
|
"github.com/btcsuite/btcwallet/spvsvc/spvchain"
|
|
|
|
"github.com/btcsuite/btcwallet/waddrmgr"
|
|
|
|
"github.com/btcsuite/btcwallet/walletdb"
|
|
|
|
_ "github.com/btcsuite/btcwallet/walletdb/bdb"
|
|
|
|
)
|
|
|
|
|
2017-04-11 21:24:26 +02:00
|
|
|
var logLevel = btclog.TraceLvl
|
|
|
|
|
2017-03-25 01:11:50 +01:00
|
|
|
func TestSetup(t *testing.T) {
|
|
|
|
// Create a btcd SimNet node and generate 500 blocks
|
|
|
|
h1, err := rpctest.New(&chaincfg.SimNetParams, nil, nil)
|
|
|
|
if err != nil {
|
2017-03-28 06:45:02 +02:00
|
|
|
t.Fatalf("Couldn't create harness: %s", err)
|
2017-03-25 01:11:50 +01:00
|
|
|
}
|
|
|
|
defer h1.TearDown()
|
|
|
|
err = h1.SetUp(false, 0)
|
|
|
|
if err != nil {
|
2017-03-28 06:45:02 +02:00
|
|
|
t.Fatalf("Couldn't set up harness: %s", err)
|
2017-03-25 01:11:50 +01:00
|
|
|
}
|
|
|
|
_, err = h1.Node.Generate(500)
|
|
|
|
if err != nil {
|
2017-03-28 06:45:02 +02:00
|
|
|
t.Fatalf("Couldn't generate blocks: %s", err)
|
2017-03-25 01:11:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a second btcd SimNet node
|
|
|
|
h2, err := rpctest.New(&chaincfg.SimNetParams, nil, nil)
|
|
|
|
if err != nil {
|
2017-03-28 06:45:02 +02:00
|
|
|
t.Fatalf("Couldn't create harness: %s", err)
|
2017-03-25 01:11:50 +01:00
|
|
|
}
|
|
|
|
defer h2.TearDown()
|
|
|
|
err = h2.SetUp(false, 0)
|
|
|
|
if err != nil {
|
2017-03-28 06:45:02 +02:00
|
|
|
t.Fatalf("Couldn't set up harness: %s", err)
|
2017-03-25 01:11:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a third btcd SimNet node and generate 900 blocks
|
|
|
|
h3, err := rpctest.New(&chaincfg.SimNetParams, nil, nil)
|
|
|
|
if err != nil {
|
2017-03-28 06:45:02 +02:00
|
|
|
t.Fatalf("Couldn't create harness: %s", err)
|
2017-03-25 01:11:50 +01:00
|
|
|
}
|
|
|
|
defer h3.TearDown()
|
|
|
|
err = h3.SetUp(false, 0)
|
|
|
|
if err != nil {
|
2017-03-28 06:45:02 +02:00
|
|
|
t.Fatalf("Couldn't set up harness: %s", err)
|
2017-03-25 01:11:50 +01:00
|
|
|
}
|
|
|
|
_, err = h3.Node.Generate(900)
|
|
|
|
if err != nil {
|
2017-03-28 06:45:02 +02:00
|
|
|
t.Fatalf("Couldn't generate blocks: %s", err)
|
2017-03-25 01:11:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Connect, sync, and disconnect h1 and h2
|
|
|
|
err = csd([]*rpctest.Harness{h1, h2})
|
|
|
|
if err != nil {
|
2017-03-28 06:45:02 +02:00
|
|
|
t.Fatalf("Couldn't connect/sync/disconnect h1 and h2: %s", err)
|
2017-03-25 01:11:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Generate 300 blocks on the first node and 350 on the second
|
|
|
|
_, err = h1.Node.Generate(300)
|
|
|
|
if err != nil {
|
2017-03-28 06:45:02 +02:00
|
|
|
t.Fatalf("Couldn't generate blocks: %s", err)
|
2017-03-25 01:11:50 +01:00
|
|
|
}
|
|
|
|
_, err = h2.Node.Generate(350)
|
|
|
|
if err != nil {
|
2017-03-28 06:45:02 +02:00
|
|
|
t.Fatalf("Couldn't generate blocks: %s", err)
|
2017-03-25 01:11:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now we have a node with 800 blocks (h1), 850 blocks (h2), and
|
|
|
|
// 900 blocks (h3). The chains of nodes h1 and h2 match up to block
|
|
|
|
// 500. By default, a synchronizing wallet connected to all three
|
|
|
|
// should synchronize to h3. However, we're going to take checkpoints
|
|
|
|
// from h1 at 111, 333, 555, and 777, and add those to the
|
|
|
|
// synchronizing wallet's chain parameters so that it should
|
|
|
|
// disconnect from h3 at block 111, and from h2 at block 555, and
|
|
|
|
// then synchronize to block 800 from h1. Order of connection is
|
|
|
|
// unfortunately not guaranteed, so the reorg may not happen with every
|
|
|
|
// test.
|
|
|
|
|
|
|
|
// Copy parameters and insert checkpoints
|
|
|
|
modParams := chaincfg.SimNetParams
|
|
|
|
for _, height := range []int64{111, 333, 555, 777} {
|
|
|
|
hash, err := h1.Node.GetBlockHash(height)
|
|
|
|
if err != nil {
|
2017-03-28 06:45:02 +02:00
|
|
|
t.Fatalf("Couldn't get block hash for height %d: %s",
|
2017-03-25 01:11:50 +01:00
|
|
|
height, err)
|
|
|
|
}
|
|
|
|
modParams.Checkpoints = append(modParams.Checkpoints,
|
|
|
|
chaincfg.Checkpoint{
|
|
|
|
Hash: hash,
|
|
|
|
Height: int32(height),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a temporary directory, initialize an empty walletdb with an
|
|
|
|
// SPV chain namespace, and create a configuration for the ChainService.
|
|
|
|
tempDir, err := ioutil.TempDir("", "spvchain")
|
|
|
|
if err != nil {
|
2017-03-28 06:45:02 +02:00
|
|
|
t.Fatalf("Failed to create temporary directory: %s", err)
|
2017-03-25 01:11:50 +01:00
|
|
|
}
|
|
|
|
defer os.RemoveAll(tempDir)
|
|
|
|
db, err := walletdb.Create("bdb", tempDir+"/weks.db")
|
|
|
|
defer db.Close()
|
|
|
|
if err != nil {
|
2017-03-28 06:45:02 +02:00
|
|
|
t.Fatalf("Error opening DB: %s\n", err)
|
2017-03-25 01:11:50 +01:00
|
|
|
}
|
|
|
|
ns, err := db.Namespace([]byte("weks"))
|
|
|
|
if err != nil {
|
2017-03-28 06:45:02 +02:00
|
|
|
t.Fatalf("Error geting namespace: %s\n", err)
|
2017-03-25 01:11:50 +01:00
|
|
|
}
|
|
|
|
config := spvchain.Config{
|
|
|
|
DataDir: tempDir,
|
|
|
|
Namespace: ns,
|
|
|
|
ChainParams: modParams,
|
|
|
|
AddPeers: []string{
|
|
|
|
h3.P2PAddress(),
|
|
|
|
h2.P2PAddress(),
|
|
|
|
h1.P2PAddress(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
spvchain.Services = 0
|
|
|
|
spvchain.MaxPeers = 3
|
2017-04-11 21:24:26 +02:00
|
|
|
spvchain.BanDuration = 5 * time.Second
|
2017-03-25 01:11:50 +01:00
|
|
|
spvchain.RequiredServices = wire.SFNodeNetwork
|
2017-04-11 21:24:26 +02:00
|
|
|
logger, err := btctestlog.NewTestLogger(t)
|
2017-03-25 01:11:50 +01:00
|
|
|
if err != nil {
|
2017-03-28 06:45:02 +02:00
|
|
|
t.Fatalf("Could not set up logger: %s", err)
|
2017-03-25 01:11:50 +01:00
|
|
|
}
|
|
|
|
chainLogger := btclog.NewSubsystemLogger(logger, "CHAIN: ")
|
2017-04-11 21:24:26 +02:00
|
|
|
chainLogger.SetLevel(logLevel)
|
|
|
|
spvchain.UseLogger(chainLogger)
|
2017-03-25 01:11:50 +01:00
|
|
|
svc, err := spvchain.NewChainService(config)
|
|
|
|
if err != nil {
|
2017-03-28 06:45:02 +02:00
|
|
|
t.Fatalf("Error creating ChainService: %s", err)
|
2017-03-25 01:11:50 +01:00
|
|
|
}
|
|
|
|
svc.Start()
|
|
|
|
defer svc.Stop()
|
|
|
|
|
|
|
|
// Make sure the client synchronizes with the correct node
|
|
|
|
err = waitForSync(t, svc, h1, time.Second, 30*time.Second)
|
|
|
|
if err != nil {
|
2017-03-28 06:45:02 +02:00
|
|
|
t.Fatalf("Couldn't sync ChainService: %s", err)
|
2017-03-25 01:11:50 +01:00
|
|
|
}
|
|
|
|
|
2017-03-27 21:30:23 +02:00
|
|
|
// Generate 125 blocks on h1 to make sure it reorgs the other nodes.
|
2017-03-25 01:11:50 +01:00
|
|
|
// Ensure the ChainService instance stays caught up.
|
2017-03-27 21:30:23 +02:00
|
|
|
h1.Node.Generate(125)
|
2017-03-25 01:11:50 +01:00
|
|
|
err = waitForSync(t, svc, h1, time.Second, 30*time.Second)
|
|
|
|
if err != nil {
|
2017-03-28 06:45:02 +02:00
|
|
|
t.Fatalf("Couldn't sync ChainService: %s", err)
|
2017-03-25 01:11:50 +01:00
|
|
|
}
|
|
|
|
|
2017-03-27 21:30:23 +02:00
|
|
|
// Connect/sync/disconnect h2 to make it reorg to the h1 chain.
|
|
|
|
err = csd([]*rpctest.Harness{h1, h2})
|
2017-03-25 01:11:50 +01:00
|
|
|
if err != nil {
|
2017-03-28 06:45:02 +02:00
|
|
|
t.Fatalf("Couldn't sync h2 to h1: %s", err)
|
2017-03-27 21:30:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Generate 3 blocks on h1, one at a time, to make sure the
|
|
|
|
// ChainService instance stays caught up.
|
|
|
|
for i := 0; i < 3; i++ {
|
|
|
|
h1.Node.Generate(1)
|
|
|
|
err = waitForSync(t, svc, h1, time.Second, 30*time.Second)
|
|
|
|
if err != nil {
|
2017-03-28 06:45:02 +02:00
|
|
|
t.Fatalf("Couldn't sync ChainService: %s", err)
|
2017-03-27 21:30:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-28 06:45:02 +02:00
|
|
|
// Generate 5 blocks on h2 and wait for ChainService to sync to the
|
|
|
|
// newly-best chain on h2.
|
2017-03-27 21:30:23 +02:00
|
|
|
h2.Node.Generate(5)
|
|
|
|
err = waitForSync(t, svc, h2, time.Second, 30*time.Second)
|
|
|
|
if err != nil {
|
2017-03-28 06:45:02 +02:00
|
|
|
t.Fatalf("Couldn't sync ChainService: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate 7 blocks on h1 and wait for ChainService to sync to the
|
|
|
|
// newly-best chain on h1.
|
2017-04-14 04:38:38 +02:00
|
|
|
/*h1.Node.Generate(7)
|
2017-03-28 06:45:02 +02:00
|
|
|
err = waitForSync(t, svc, h1, time.Second, 30*time.Second)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Couldn't sync ChainService: %s", err)
|
2017-04-14 04:38:38 +02:00
|
|
|
}*/
|
2017-03-25 01:11:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// csd does a connect-sync-disconnect between nodes in order to support
|
|
|
|
// reorg testing. It brings up and tears down a temporary node, otherwise the
|
|
|
|
// nodes try to reconnect to each other which results in unintended reorgs.
|
|
|
|
func csd(harnesses []*rpctest.Harness) error {
|
|
|
|
hTemp, err := rpctest.New(&chaincfg.SimNetParams, nil, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Tear down node at the end of the function.
|
|
|
|
defer hTemp.TearDown()
|
|
|
|
err = hTemp.SetUp(false, 0)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, harness := range harnesses {
|
|
|
|
err = rpctest.ConnectNode(hTemp, harness)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rpctest.JoinNodes(harnesses, rpctest.Blocks)
|
|
|
|
}
|
|
|
|
|
|
|
|
// waitForSync waits for the ChainService to sync to the current chain state.
|
|
|
|
func waitForSync(t *testing.T, svc *spvchain.ChainService,
|
|
|
|
correctSyncNode *rpctest.Harness, checkInterval,
|
|
|
|
timeout time.Duration) error {
|
|
|
|
knownBestHash, knownBestHeight, err :=
|
|
|
|
correctSyncNode.Node.GetBestBlock()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-04-11 21:24:26 +02:00
|
|
|
if logLevel != btclog.Off {
|
|
|
|
t.Logf("Syncing to %d (%s)", knownBestHeight, knownBestHash)
|
|
|
|
}
|
2017-03-25 01:11:50 +01:00
|
|
|
var haveBest *waddrmgr.BlockStamp
|
|
|
|
haveBest, err = svc.BestSnapshot()
|
|
|
|
if err != nil {
|
2017-04-14 04:38:38 +02:00
|
|
|
return fmt.Errorf("Couldn't get best snapshot from "+
|
|
|
|
"ChainService: %s", err)
|
2017-03-25 01:11:50 +01:00
|
|
|
}
|
|
|
|
var total time.Duration
|
|
|
|
for haveBest.Hash != *knownBestHash {
|
|
|
|
if total > timeout {
|
2017-04-14 04:38:38 +02:00
|
|
|
return fmt.Errorf("Timed out after %v waiting for "+
|
|
|
|
"header synchronization.", timeout)
|
2017-03-25 01:11:50 +01:00
|
|
|
}
|
|
|
|
if haveBest.Height > knownBestHeight {
|
|
|
|
return fmt.Errorf("Synchronized to the wrong chain.")
|
|
|
|
}
|
|
|
|
time.Sleep(checkInterval)
|
|
|
|
total += checkInterval
|
|
|
|
haveBest, err = svc.BestSnapshot()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Couldn't get best snapshot from "+
|
2017-03-28 06:45:02 +02:00
|
|
|
"ChainService: %s", err)
|
2017-03-25 01:11:50 +01:00
|
|
|
}
|
2017-04-11 21:24:26 +02:00
|
|
|
if logLevel != btclog.Off {
|
|
|
|
t.Logf("Synced to %d (%s)", haveBest.Height,
|
|
|
|
haveBest.Hash)
|
|
|
|
}
|
2017-03-25 01:11:50 +01:00
|
|
|
}
|
2017-04-14 04:38:38 +02:00
|
|
|
// Check if we're current.
|
2017-03-25 01:11:50 +01:00
|
|
|
if !svc.IsCurrent() {
|
|
|
|
return fmt.Errorf("ChainService doesn't see itself as current!")
|
|
|
|
}
|
2017-04-14 04:38:38 +02:00
|
|
|
// Check if we have all of the cfheaders.
|
|
|
|
knownBasicHeader, err := correctSyncNode.Node.GetCFilterHeader(
|
|
|
|
knownBestHash, false)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Couldn't get latest basic header from "+
|
|
|
|
"%s: %s", correctSyncNode.P2PAddress(), err)
|
|
|
|
}
|
|
|
|
knownExtHeader, err := correctSyncNode.Node.GetCFilterHeader(
|
|
|
|
knownBestHash, true)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Couldn't get latest extended header from "+
|
|
|
|
"%s: %s", correctSyncNode.P2PAddress(), err)
|
|
|
|
}
|
|
|
|
for total <= timeout {
|
|
|
|
time.Sleep(checkInterval)
|
|
|
|
total += checkInterval
|
|
|
|
haveBasicHeader, err := svc.GetBasicHeader(*knownBestHash)
|
|
|
|
if err != nil {
|
|
|
|
t.Logf("Basic header unknown.")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
haveExtHeader, err := svc.GetExtHeader(*knownBestHash)
|
|
|
|
if err != nil {
|
|
|
|
t.Logf("Extended header unknown.")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if *knownBasicHeader.HeaderHashes[0] != *haveBasicHeader {
|
|
|
|
return fmt.Errorf("Known basic header doesn't match "+
|
|
|
|
"the basic header the ChainService has. Known:"+
|
|
|
|
" %s, ChainService: %s",
|
|
|
|
knownBasicHeader.HeaderHashes[0],
|
|
|
|
haveBasicHeader)
|
|
|
|
}
|
|
|
|
if *knownExtHeader.HeaderHashes[0] != *haveExtHeader {
|
|
|
|
return fmt.Errorf("Known extended header doesn't "+
|
|
|
|
"match the extended header the ChainService "+
|
|
|
|
"has. Known: %s, ChainService: %s",
|
|
|
|
knownExtHeader.HeaderHashes[0], haveExtHeader)
|
|
|
|
}
|
|
|
|
t.Logf("Synced cfheaders to %d (%s)", haveBest.Height,
|
|
|
|
haveBest.Hash)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return fmt.Errorf("Timeout waiting for cfheaders synchronization after"+
|
|
|
|
" %v", timeout)
|
2017-03-25 01:11:50 +01:00
|
|
|
}
|