gitlab.com/jokerrs1/Sia@v1.3.2/modules/wallet/update_test.go (about)

     1  package wallet
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/NebulousLabs/Sia/modules"
     7  	"github.com/NebulousLabs/Sia/types"
     8  )
     9  
    10  // TestUpdate tests that the wallet processes consensus updates properly.
    11  func TestUpdate(t *testing.T) {
    12  	if testing.Short() {
    13  		t.SkipNow()
    14  	}
    15  	wt, err := createWalletTester("TestUpdate", &modules.ProductionDependencies{})
    16  	if err != nil {
    17  		t.Fatal(err)
    18  	}
    19  	// mine a block and add it to the consensus set
    20  	b, err := wt.miner.FindBlock()
    21  	if err != nil {
    22  		t.Fatal(err)
    23  	}
    24  	if err := wt.cs.AcceptBlock(b); err != nil {
    25  		t.Fatal(err)
    26  	}
    27  	// since the miner is mining into a wallet address, the wallet should have
    28  	// added a new transaction
    29  	_, ok := wt.wallet.Transaction(types.TransactionID(b.ID()))
    30  	if !ok {
    31  		t.Fatal("no record of miner transaction")
    32  	}
    33  
    34  	// revert the block
    35  	wt.wallet.ProcessConsensusChange(modules.ConsensusChange{
    36  		RevertedBlocks: []types.Block{b},
    37  	})
    38  	// transaction should no longer be present
    39  	_, ok = wt.wallet.Transaction(types.TransactionID(b.ID()))
    40  	if ok {
    41  		t.Fatal("miner transaction was not removed after block was reverted")
    42  	}
    43  
    44  	// create a transaction
    45  	addr, _ := wt.wallet.NextAddress()
    46  	txnSet, err := wt.wallet.SendSiacoins(types.SiacoinPrecision.Mul64(10), addr.UnlockHash())
    47  	if err != nil {
    48  		t.Fatal(err)
    49  	}
    50  
    51  	// mine blocks until transaction is confirmed, while building up a cc that will revert all the blocks we add
    52  	var revertCC modules.ConsensusChange
    53  	for i := types.BlockHeight(0); i <= types.MaturityDelay; i++ {
    54  		b, _ := wt.miner.FindBlock()
    55  		if err := wt.cs.AcceptBlock(b); err != nil {
    56  			t.Fatal(err)
    57  		}
    58  		revertCC.RevertedBlocks = append([]types.Block{b}, revertCC.RevertedBlocks...)
    59  	}
    60  
    61  	// transaction should be present
    62  	_, ok = wt.wallet.Transaction(txnSet[0].ID())
    63  	if !ok {
    64  		t.Fatal("no record of transaction")
    65  	}
    66  
    67  	// revert all the blocks
    68  	wt.wallet.ProcessConsensusChange(revertCC)
    69  	_, ok = wt.wallet.Transaction(txnSet[0].ID())
    70  	if ok {
    71  		t.Fatal("transaction was not removed")
    72  	}
    73  }