github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/core/state/managed_state_test.go (about) 1 2 //<developer> 3 // <name>linapex 曹一峰</name> 4 // <email>linapex@163.com</email> 5 // <wx>superexc</wx> 6 // <qqgroup>128148617</qqgroup> 7 // <url>https://jsq.ink</url> 8 // <role>pku engineer</role> 9 // <date>2019-03-16 19:16:35</date> 10 //</624450080170053632> 11 12 13 package state 14 15 import ( 16 "testing" 17 18 "github.com/ethereum/go-ethereum/common" 19 "github.com/ethereum/go-ethereum/ethdb" 20 ) 21 22 var addr = common.BytesToAddress([]byte("test")) 23 24 func create() (*ManagedState, *account) { 25 statedb, _ := New(common.Hash{}, NewDatabase(ethdb.NewMemDatabase())) 26 ms := ManageState(statedb) 27 ms.StateDB.SetNonce(addr, 100) 28 ms.accounts[addr] = newAccount(ms.StateDB.getStateObject(addr)) 29 return ms, ms.accounts[addr] 30 } 31 32 func TestNewNonce(t *testing.T) { 33 ms, _ := create() 34 35 nonce := ms.NewNonce(addr) 36 if nonce != 100 { 37 t.Error("expected nonce 100. got", nonce) 38 } 39 40 nonce = ms.NewNonce(addr) 41 if nonce != 101 { 42 t.Error("expected nonce 101. got", nonce) 43 } 44 } 45 46 func TestRemove(t *testing.T) { 47 ms, account := create() 48 49 nn := make([]bool, 10) 50 for i := range nn { 51 nn[i] = true 52 } 53 account.nonces = append(account.nonces, nn...) 54 55 i := uint64(5) 56 ms.RemoveNonce(addr, account.nstart+i) 57 if len(account.nonces) != 5 { 58 t.Error("expected", i, "'th index to be false") 59 } 60 } 61 62 func TestReuse(t *testing.T) { 63 ms, account := create() 64 65 nn := make([]bool, 10) 66 for i := range nn { 67 nn[i] = true 68 } 69 account.nonces = append(account.nonces, nn...) 70 71 i := uint64(5) 72 ms.RemoveNonce(addr, account.nstart+i) 73 nonce := ms.NewNonce(addr) 74 if nonce != 105 { 75 t.Error("expected nonce to be 105. got", nonce) 76 } 77 } 78 79 func TestRemoteNonceChange(t *testing.T) { 80 ms, account := create() 81 nn := make([]bool, 10) 82 for i := range nn { 83 nn[i] = true 84 } 85 account.nonces = append(account.nonces, nn...) 86 ms.NewNonce(addr) 87 88 ms.StateDB.stateObjects[addr].data.Nonce = 200 89 nonce := ms.NewNonce(addr) 90 if nonce != 200 { 91 t.Error("expected nonce after remote update to be", 200, "got", nonce) 92 } 93 ms.NewNonce(addr) 94 ms.NewNonce(addr) 95 ms.NewNonce(addr) 96 ms.StateDB.stateObjects[addr].data.Nonce = 200 97 nonce = ms.NewNonce(addr) 98 if nonce != 204 { 99 t.Error("expected nonce after remote update to be", 204, "got", nonce) 100 } 101 } 102 103 func TestSetNonce(t *testing.T) { 104 ms, _ := create() 105 106 var addr common.Address 107 ms.SetNonce(addr, 10) 108 109 if ms.GetNonce(addr) != 10 { 110 t.Error("Expected nonce of 10, got", ms.GetNonce(addr)) 111 } 112 113 addr[0] = 1 114 ms.StateDB.SetNonce(addr, 1) 115 116 if ms.GetNonce(addr) != 1 { 117 t.Error("Expected nonce of 1, got", ms.GetNonce(addr)) 118 } 119 } 120