github.com/SmartMeshFoundation/Spectrum@v0.0.0-20220621030607-452a266fee1e/core/state/managed_state_test.go (about) 1 // Copyright 2015 The Spectrum Authors 2 // This file is part of the Spectrum library. 3 // 4 // The Spectrum 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 Spectrum 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 Spectrum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package state 18 19 import ( 20 "testing" 21 22 "github.com/SmartMeshFoundation/Spectrum/common" 23 "github.com/SmartMeshFoundation/Spectrum/ethdb" 24 ) 25 26 var addr = common.BytesToAddress([]byte("test")) 27 28 func create() (*ManagedState, *account) { 29 db, _ := ethdb.NewMemDatabase() 30 statedb, _ := New(common.Hash{}, NewDatabase(db)) 31 ms := ManageState(statedb) 32 ms.StateDB.SetNonce(addr, 100) 33 ms.accounts[addr] = newAccount(ms.StateDB.getStateObject(addr)) 34 return ms, ms.accounts[addr] 35 } 36 37 func TestNewNonce(t *testing.T) { 38 ms, _ := create() 39 40 nonce := ms.NewNonce(addr) 41 if nonce != 100 { 42 t.Error("expected nonce 100. got", nonce) 43 } 44 45 nonce = ms.NewNonce(addr) 46 if nonce != 101 { 47 t.Error("expected nonce 101. got", nonce) 48 } 49 } 50 51 func TestRemove(t *testing.T) { 52 ms, account := create() 53 54 nn := make([]bool, 10) 55 for i := range nn { 56 nn[i] = true 57 } 58 account.nonces = append(account.nonces, nn...) 59 60 i := uint64(5) 61 ms.RemoveNonce(addr, account.nstart+i) 62 if len(account.nonces) != 5 { 63 t.Error("expected", i, "'th index to be false") 64 } 65 } 66 67 func TestReuse(t *testing.T) { 68 ms, account := create() 69 70 nn := make([]bool, 10) 71 for i := range nn { 72 nn[i] = true 73 } 74 account.nonces = append(account.nonces, nn...) 75 76 i := uint64(5) 77 ms.RemoveNonce(addr, account.nstart+i) 78 nonce := ms.NewNonce(addr) 79 if nonce != 105 { 80 t.Error("expected nonce to be 105. got", nonce) 81 } 82 } 83 84 func TestRemoteNonceChange(t *testing.T) { 85 ms, account := create() 86 nn := make([]bool, 10) 87 for i := range nn { 88 nn[i] = true 89 } 90 account.nonces = append(account.nonces, nn...) 91 ms.NewNonce(addr) 92 93 ms.StateDB.stateObjects[addr].data.Nonce = 200 94 nonce := ms.NewNonce(addr) 95 if nonce != 200 { 96 t.Error("expected nonce after remote update to be", 200, "got", nonce) 97 } 98 ms.NewNonce(addr) 99 ms.NewNonce(addr) 100 ms.NewNonce(addr) 101 ms.StateDB.stateObjects[addr].data.Nonce = 200 102 nonce = ms.NewNonce(addr) 103 if nonce != 204 { 104 t.Error("expected nonce after remote update to be", 204, "got", nonce) 105 } 106 } 107 108 func TestSetNonce(t *testing.T) { 109 ms, _ := create() 110 111 var addr common.Address 112 ms.SetNonce(addr, 10) 113 114 if ms.GetNonce(addr) != 10 { 115 t.Error("Expected nonce of 10, got", ms.GetNonce(addr)) 116 } 117 118 addr[0] = 1 119 ms.StateDB.SetNonce(addr, 1) 120 121 if ms.GetNonce(addr) != 1 { 122 t.Error("Expected nonce of 1, got", ms.GetNonce(addr)) 123 } 124 }