github.com/ZuluSpl0it/Sia@v1.3.7/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.ProdDependencies)
    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, err := wt.wallet.Transaction(types.TransactionID(b.ID()))
    30  	if err != nil {
    31  		t.Fatal(err)
    32  	}
    33  	if !ok {
    34  		t.Fatal("no record of miner transaction")
    35  	}
    36  
    37  	// revert the block
    38  	wt.wallet.ProcessConsensusChange(modules.ConsensusChange{
    39  		RevertedBlocks: []types.Block{b},
    40  	})
    41  	// transaction should no longer be present
    42  	_, ok, err = wt.wallet.Transaction(types.TransactionID(b.ID()))
    43  	if err != nil {
    44  		t.Fatal(err)
    45  	}
    46  	if ok {
    47  		t.Fatal("miner transaction was not removed after block was reverted")
    48  	}
    49  
    50  	// create a transaction
    51  	addr, _ := wt.wallet.NextAddress()
    52  	txnSet, err := wt.wallet.SendSiacoins(types.SiacoinPrecision.Mul64(10), addr.UnlockHash())
    53  	if err != nil {
    54  		t.Fatal(err)
    55  	}
    56  
    57  	// mine blocks until transaction is confirmed, while building up a cc that will revert all the blocks we add
    58  	var revertCC modules.ConsensusChange
    59  	for i := types.BlockHeight(0); i <= types.MaturityDelay; i++ {
    60  		b, _ := wt.miner.FindBlock()
    61  		if err := wt.cs.AcceptBlock(b); err != nil {
    62  			t.Fatal(err)
    63  		}
    64  		revertCC.RevertedBlocks = append([]types.Block{b}, revertCC.RevertedBlocks...)
    65  	}
    66  
    67  	// transaction should be present
    68  	_, ok, err = wt.wallet.Transaction(txnSet[0].ID())
    69  	if err != nil {
    70  		t.Fatal(err)
    71  	}
    72  	if !ok {
    73  		t.Fatal("no record of transaction")
    74  	}
    75  
    76  	// revert all the blocks
    77  	wt.wallet.ProcessConsensusChange(revertCC)
    78  	_, ok, err = wt.wallet.Transaction(txnSet[0].ID())
    79  	if err != nil {
    80  		t.Fatal(err)
    81  	}
    82  	if ok {
    83  		t.Fatal("transaction was not removed")
    84  	}
    85  }