github.com/johnathanhowell/sia@v0.5.1-beta.0.20160524050156-83dcc3d37c94/modules/miner/update_test.go (about)

     1  package miner
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/NebulousLabs/Sia/modules"
     7  )
     8  
     9  // TestIntegrationBlockHeightReorg checks that the miner has the correct block
    10  // height after a series of reorgs that go as far as the genesis block.
    11  func TestIntegrationBlockHeightReorg(t *testing.T) {
    12  	if testing.Short() {
    13  		t.SkipNow()
    14  	}
    15  
    16  	// Create 3 miner testers that will be used to cause eachother to reorg.
    17  	mt1, err := createMinerTester("TestIntegrationBlockHeightReorg - 1")
    18  	if err != nil {
    19  		t.Fatal(err)
    20  	}
    21  	mt2, err := createMinerTester("TestIntegrationBlockHeightReorg - 2")
    22  	if err != nil {
    23  		t.Fatal(err)
    24  	}
    25  	mt3, err := createMinerTester("TestIntegrationBlockHeightReorg - 3")
    26  	if err != nil {
    27  		t.Fatal(err)
    28  	}
    29  
    30  	// Put one ahead of the other multiple times, which should thrash around
    31  	// the height calculation and cause problems by dipping down to the genesis
    32  	// block repeatedly.
    33  	for i := 0; i < 2; i++ {
    34  		b, err := mt1.miner.AddBlock()
    35  		if err != nil {
    36  			t.Fatal(err)
    37  		}
    38  		mt1.minedBlocks = append(mt1.minedBlocks, b)
    39  	}
    40  	for i := 0; i < 3; i++ {
    41  		b, err := mt2.miner.AddBlock()
    42  		if err != nil {
    43  			t.Fatal(err)
    44  		}
    45  		mt2.minedBlocks = append(mt2.minedBlocks, b)
    46  	}
    47  	for _, block := range mt2.minedBlocks {
    48  		err = mt1.cs.AcceptBlock(block)
    49  		if err != nil && err != modules.ErrNonExtendingBlock {
    50  			t.Fatal(err)
    51  		}
    52  	}
    53  	if mt1.cs.CurrentBlock().ID() != mt2.cs.CurrentBlock().ID() {
    54  		t.Fatal("mt1 and mt2 should have the same current block")
    55  	}
    56  	for i := 0; i < 2; i++ {
    57  		b, err := mt1.miner.AddBlock()
    58  		if err != nil {
    59  			t.Fatal(err)
    60  		}
    61  		mt1.minedBlocks = append(mt1.minedBlocks, b)
    62  	}
    63  	for i := 0; i < 3; i++ {
    64  		b, err := mt2.miner.AddBlock()
    65  		if err != nil {
    66  			t.Fatal(err)
    67  		}
    68  		mt2.minedBlocks = append(mt2.minedBlocks, b)
    69  	}
    70  	for _, block := range mt2.minedBlocks {
    71  		err = mt1.cs.AcceptBlock(block)
    72  		if err != nil && err != modules.ErrNonExtendingBlock && err != modules.ErrBlockKnown {
    73  			t.Fatal(err)
    74  		}
    75  	}
    76  	if mt1.cs.CurrentBlock().ID() != mt2.cs.CurrentBlock().ID() {
    77  		t.Fatal("mt1 and mt2 should have the same current block")
    78  	}
    79  	for i := 0; i < 7; i++ {
    80  		b, err := mt3.miner.AddBlock()
    81  		if err != nil {
    82  			t.Fatal(err)
    83  		}
    84  		mt3.minedBlocks = append(mt3.minedBlocks, b)
    85  	}
    86  	for _, block := range mt3.minedBlocks {
    87  		err = mt1.cs.AcceptBlock(block)
    88  		if err != nil && err != modules.ErrNonExtendingBlock {
    89  			t.Fatal(err)
    90  		}
    91  	}
    92  	if mt1.cs.CurrentBlock().ID() == mt2.cs.CurrentBlock().ID() {
    93  		t.Fatal("mt1 and mt2 should not have the same block height")
    94  	}
    95  	if mt1.cs.CurrentBlock().ID() != mt3.cs.CurrentBlock().ID() {
    96  		t.Fatal("mt1 and mt3 should have the same current block")
    97  	}
    98  }
    99  
   100  // TestMinerHeightForcefulMismatch checks that the miner, when not running in
   101  // debug mode, does forceful self-correcting for height calculation.
   102  func TestMinerHeightForcefulMismatch(t *testing.T) {
   103  	if testing.Short() {
   104  		t.SkipNow()
   105  	}
   106  	mt, err := createMinerTester("TestMinerHeightForcefulMismatch")
   107  	if err != nil {
   108  		t.Fatal(err)
   109  	}
   110  	b, err := mt.miner.FindBlock()
   111  	if err != nil {
   112  		t.Fatal(err)
   113  	}
   114  
   115  	// A panic should be triggered when AcceptBlock is called on the miner.
   116  	mt.miner.persist.Height--
   117  	defer func() {
   118  		r := recover()
   119  		if r == nil {
   120  			t.Fatal("expecting a panic upon adding a block")
   121  		}
   122  	}()
   123  	err = mt.cs.AcceptBlock(b)
   124  	if err != nil {
   125  		t.Fatal(err)
   126  	}
   127  }