github.com/NebulousLabs/Sia@v1.3.7/modules/consensus/changelog_test.go (about)

     1  package consensus
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/NebulousLabs/Sia/modules"
     7  	"github.com/NebulousLabs/Sia/types"
     8  )
     9  
    10  // TestIntegrationChangeLog does a general test of the changelog by creating a
    11  // subscriber that subscribes partway into startup and checking that the
    12  // correct ordering of blocks are provided.
    13  func TestIntegrationChangeLog(t *testing.T) {
    14  	if testing.Short() {
    15  		t.SkipNow()
    16  	}
    17  	t.Parallel()
    18  	// Get a blank consensus set tester so that the mocked subscriber can join
    19  	// immediately after genesis.
    20  	cst, err := blankConsensusSetTester(t.Name(), modules.ProdDependencies)
    21  	if err != nil {
    22  		t.Fatal(err)
    23  	}
    24  	defer cst.Close()
    25  
    26  	// Add a mocked subscriber and check that it receives the correct number of
    27  	// blocks.
    28  	ms := newMockSubscriber()
    29  	cst.cs.ConsensusSetSubscribe(&ms, modules.ConsensusChangeBeginning, cst.cs.tg.StopChan())
    30  	if ms.updates[0].AppliedBlocks[0].ID() != cst.cs.blockRoot.Block.ID() {
    31  		t.Fatal("subscription did not correctly receive the genesis block")
    32  	}
    33  	if len(ms.updates) != 1 {
    34  		t.Fatal("subscription resulted in the wrong number of blocks being sent")
    35  	}
    36  
    37  	// Create a copy of the subscriber that will subscribe to the consensus at
    38  	// the tail of the updates.
    39  	tailSubscriber := ms.copySub()
    40  	cst.cs.ConsensusSetSubscribe(&tailSubscriber, tailSubscriber.updates[len(tailSubscriber.updates)-1].ID, cst.cs.tg.StopChan())
    41  	if len(tailSubscriber.updates) != 1 {
    42  		t.Fatal("subscription resulted in the wrong number of blocks being sent")
    43  	}
    44  
    45  	// Create a copy of the subscriber that will join when it is not at 0, but it is behind.
    46  	behindSubscriber := ms.copySub()
    47  	cst.addSiafunds()
    48  	cst.mineSiacoins()
    49  	cst.cs.ConsensusSetSubscribe(&behindSubscriber, behindSubscriber.updates[len(behindSubscriber.updates)-1].ID, cst.cs.tg.StopChan())
    50  	if types.BlockHeight(len(behindSubscriber.updates)) != cst.cs.dbBlockHeight()+1 {
    51  		t.Fatal("subscription resulted in the wrong number of blocks being sent")
    52  	}
    53  	if len(ms.updates) != len(tailSubscriber.updates) {
    54  		t.Error("subscribers have inconsistent update chains")
    55  	}
    56  	if len(ms.updates) != len(behindSubscriber.updates) {
    57  		t.Error("subscribers have inconsistent update chains")
    58  	}
    59  }