github.com/bcskill/bcschain/v3@v3.4.9-beta2/core/state/managed_state_test.go (about) 1 // Copyright 2015 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package state 18 19 import ( 20 "testing" 21 22 "github.com/bcskill/bcschain/v3/common" 23 "github.com/bcskill/bcschain/v3/ethdb" 24 ) 25 26 var addr = common.BytesToAddress([]byte("test")) 27 28 func create(t *testing.T) (*ManagedState, *account) { 29 db := ethdb.NewMemDatabase() 30 statedb, _ := New(common.Hash{}, NewDatabase(db)) 31 ms := ManageState(statedb) 32 ms.StateDB.SetNonce(addr, 100) 33 so, err := ms.StateDB.getStateObject(addr) 34 if err != nil { 35 t.Fatal(err) 36 } 37 ms.accounts[addr] = newAccount(so) 38 return ms, ms.accounts[addr] 39 } 40 41 func TestNewNonce(t *testing.T) { 42 ms, _ := create(t) 43 44 nonce := ms.NewNonce(addr) 45 if nonce != 100 { 46 t.Error("expected nonce 100. got", nonce) 47 } 48 49 nonce = ms.NewNonce(addr) 50 if nonce != 101 { 51 t.Error("expected nonce 101. got", nonce) 52 } 53 } 54 55 func TestRemove(t *testing.T) { 56 ms, account := create(t) 57 58 nn := make([]bool, 10) 59 for i := range nn { 60 nn[i] = true 61 } 62 account.nonces = append(account.nonces, nn...) 63 64 i := uint64(5) 65 ms.RemoveNonce(addr, account.nstart+i) 66 if len(account.nonces) != 5 { 67 t.Error("expected", i, "'th index to be false") 68 } 69 } 70 71 func TestReuse(t *testing.T) { 72 ms, account := create(t) 73 74 nn := make([]bool, 10) 75 for i := range nn { 76 nn[i] = true 77 } 78 account.nonces = append(account.nonces, nn...) 79 80 i := uint64(5) 81 ms.RemoveNonce(addr, account.nstart+i) 82 nonce := ms.NewNonce(addr) 83 if nonce != 105 { 84 t.Error("expected nonce to be 105. got", nonce) 85 } 86 } 87 88 func TestRemoteNonceChange(t *testing.T) { 89 ms, account := create(t) 90 nn := make([]bool, 10) 91 for i := range nn { 92 nn[i] = true 93 } 94 account.nonces = append(account.nonces, nn...) 95 ms.NewNonce(addr) 96 97 ms.StateDB.stateObjects[addr].data.Nonce = 200 98 nonce := ms.NewNonce(addr) 99 if nonce != 200 { 100 t.Error("expected nonce after remote update to be", 200, "got", nonce) 101 } 102 ms.NewNonce(addr) 103 ms.NewNonce(addr) 104 ms.NewNonce(addr) 105 ms.StateDB.stateObjects[addr].data.Nonce = 200 106 nonce = ms.NewNonce(addr) 107 if nonce != 204 { 108 t.Error("expected nonce after remote update to be", 204, "got", nonce) 109 } 110 } 111 112 func TestSetNonce(t *testing.T) { 113 ms, _ := create(t) 114 115 var addr common.Address 116 ms.SetNonce(addr, 10) 117 118 if nonce := ms.GetNonce(addr); nonce != 10 { 119 t.Error("Expected nonce of 10, got", nonce) 120 } 121 122 addr[0] = 1 123 ms.StateDB.SetNonce(addr, 1) 124 125 if nonce := ms.GetNonce(addr); nonce != 1 { 126 t.Error("Expected nonce of 1, got", nonce) 127 } 128 }