gitlab.com/SiaPrime/SiaPrime@v1.4.1/modules/miner/update_test.go (about)

     1  package miner
     2  
     3  import (
     4  	"testing"
     5  
     6  	"gitlab.com/SiaPrime/SiaPrime/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 each other to reorg.
    17  	mt1, err := createMinerTester(t.Name() + "1")
    18  	if err != nil {
    19  		t.Fatal(err)
    20  	}
    21  	mt2, err := createMinerTester(t.Name() + "2")
    22  	if err != nil {
    23  		t.Fatal(err)
    24  	}
    25  	mt3, err := createMinerTester(t.Name() + "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  }