github.com/avahowell/sia@v0.5.1-beta.0.20160524050156-83dcc3d37c94/modules/consensus/fork_test.go (about)

     1  package consensus
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/NebulousLabs/Sia/modules"
     7  )
     8  
     9  // TestBacktrackToCurrentPath probes the backtrackToCurrentPath method of the
    10  // consensus set.
    11  func TestBacktrackToCurrentPath(t *testing.T) {
    12  	if testing.Short() {
    13  		t.SkipNow()
    14  	}
    15  	cst, err := createConsensusSetTester("TestBacktrackToCurrentPath")
    16  	if err != nil {
    17  		t.Fatal(err)
    18  	}
    19  	pb := cst.cs.dbCurrentProcessedBlock()
    20  
    21  	// Backtrack from the current node to the blockchain.
    22  	nodes := cst.cs.dbBacktrackToCurrentPath(pb)
    23  	if len(nodes) != 1 {
    24  		t.Fatal("backtracking to the current node gave incorrect result")
    25  	}
    26  	if nodes[0].Block.ID() != pb.Block.ID() {
    27  		t.Error("backtrack returned the wrong node")
    28  	}
    29  
    30  	// Backtrack from a node that has diverted from the current blockchain.
    31  	child0, _ := cst.miner.FindBlock()
    32  	child1, _ := cst.miner.FindBlock() // Is the block not on hte current path.
    33  	err = cst.cs.AcceptBlock(child0)
    34  	if err != nil {
    35  		t.Fatal(err)
    36  	}
    37  	err = cst.cs.AcceptBlock(child1)
    38  	if err != modules.ErrNonExtendingBlock {
    39  		t.Fatal(err)
    40  	}
    41  	pb, err = cst.cs.dbGetBlockMap(child1.ID())
    42  	if err != nil {
    43  		t.Fatal(err)
    44  	}
    45  	nodes = cst.cs.dbBacktrackToCurrentPath(pb)
    46  	if len(nodes) != 2 {
    47  		t.Error("backtracking grabbed wrong number of nodes")
    48  	}
    49  	parent, err := cst.cs.dbGetBlockMap(pb.Block.ParentID)
    50  	if err != nil {
    51  		t.Fatal(err)
    52  	}
    53  	if nodes[0].Block.ID() != parent.Block.ID() {
    54  		t.Error("grabbed the wrong block as the common block")
    55  	}
    56  	if nodes[1].Block.ID() != pb.Block.ID() {
    57  		t.Error("backtracked from the wrong node")
    58  	}
    59  }
    60  
    61  // TestRevertToNode probes the revertToBlock method of the consensus set.
    62  func TestRevertToNode(t *testing.T) {
    63  	if testing.Short() {
    64  		t.SkipNow()
    65  	}
    66  	cst, err := createConsensusSetTester("TestBacktrackToCurrentPath")
    67  	if err != nil {
    68  		t.Fatal(err)
    69  	}
    70  	pb := cst.cs.dbCurrentProcessedBlock()
    71  
    72  	// Revert to a grandparent and verify the returned array is correct.
    73  	parent, err := cst.cs.dbGetBlockMap(pb.Block.ParentID)
    74  	if err != nil {
    75  		t.Fatal(err)
    76  	}
    77  	grandParent, err := cst.cs.dbGetBlockMap(parent.Block.ParentID)
    78  	if err != nil {
    79  		t.Fatal(err)
    80  	}
    81  	revertedNodes := cst.cs.dbRevertToNode(grandParent)
    82  	if len(revertedNodes) != 2 {
    83  		t.Error("wrong number of nodes reverted")
    84  	}
    85  	if revertedNodes[0].Block.ID() != pb.Block.ID() {
    86  		t.Error("wrong composition of reverted nodes")
    87  	}
    88  	if revertedNodes[1].Block.ID() != parent.Block.ID() {
    89  		t.Error("wrong composition of reverted nodes")
    90  	}
    91  
    92  	// Trigger a panic by trying to revert to a node outside of the current
    93  	// path.
    94  	defer func() {
    95  		r := recover()
    96  		if r != errExternalRevert {
    97  			t.Error(r)
    98  		}
    99  	}()
   100  	cst.cs.dbRevertToNode(pb)
   101  }