github.com/fozzysec/SiaPrime@v0.0.0-20190612043147-66c8e8d11fe3/modules/wallet/update_test.go (about) 1 package wallet 2 3 import ( 4 "testing" 5 6 "SiaPrime/modules" 7 "SiaPrime/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 20 // Mine a few more blocks to get past the hardfork height. 21 for i := types.BlockHeight(0); i <= 2; i++ { 22 b, _ := wt.miner.FindBlock() 23 err := wt.cs.AcceptBlock(b) 24 if err != nil { 25 t.Fatal(err) 26 } 27 } 28 29 // mine a block and add it to the consensus set 30 b, err := wt.miner.FindBlock() 31 if err != nil { 32 t.Fatal(err) 33 } 34 if err := wt.cs.AcceptBlock(b); err != nil { 35 t.Fatal(err) 36 } 37 // since the miner is mining into a wallet address, the wallet should have 38 // added a new transaction 39 _, ok, err := wt.wallet.Transaction(types.TransactionID(b.ID())) 40 if err != nil { 41 t.Fatal(err) 42 } 43 if !ok { 44 t.Fatal("no record of miner transaction") 45 } 46 47 // revert the block 48 wt.wallet.ProcessConsensusChange(modules.ConsensusChange{ 49 RevertedBlocks: []types.Block{b}, 50 }) 51 // transaction should no longer be present 52 _, ok, err = wt.wallet.Transaction(types.TransactionID(b.ID())) 53 if err != nil { 54 t.Fatal(err) 55 } 56 if ok { 57 t.Fatal("miner transaction was not removed after block was reverted") 58 } 59 60 // create a transaction 61 addr, _ := wt.wallet.NextAddress() 62 txnSet, err := wt.wallet.SendSiacoins(types.SiacoinPrecision.Mul64(10), addr.UnlockHash()) 63 if err != nil { 64 t.Fatal(err) 65 } 66 67 // mine blocks until transaction is confirmed, while building up a cc that will revert all the blocks we add 68 var revertCC modules.ConsensusChange 69 for i := types.BlockHeight(0); i <= types.MaturityDelay; i++ { 70 b, _ := wt.miner.FindBlock() 71 if err := wt.cs.AcceptBlock(b); err != nil { 72 t.Fatal(err) 73 } 74 revertCC.RevertedBlocks = append([]types.Block{b}, revertCC.RevertedBlocks...) 75 } 76 77 // transaction should be present 78 _, ok, err = wt.wallet.Transaction(txnSet[0].ID()) 79 if err != nil { 80 t.Fatal(err) 81 } 82 if !ok { 83 t.Fatal("no record of transaction") 84 } 85 86 // revert all the blocks 87 wt.wallet.ProcessConsensusChange(revertCC) 88 _, ok, err = wt.wallet.Transaction(txnSet[0].ID()) 89 if err != nil { 90 t.Fatal(err) 91 } 92 if ok { 93 t.Fatal("transaction was not removed") 94 } 95 }