github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/core/state/managed_state_test.go (about) 1 // This file is part of the go-sberex library. The go-sberex library is 2 // free software: you can redistribute it and/or modify it under the terms 3 // of the GNU Lesser General Public License as published by the Free 4 // Software Foundation, either version 3 of the License, or (at your option) 5 // any later version. 6 // 7 // The go-sberex library is distributed in the hope that it will be useful, 8 // but WITHOUT ANY WARRANTY; without even the implied warranty of 9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 10 // General Public License <http://www.gnu.org/licenses/> for more details. 11 12 package state 13 14 import ( 15 "testing" 16 17 "github.com/Sberex/go-sberex/common" 18 "github.com/Sberex/go-sberex/ethdb" 19 ) 20 21 var addr = common.BytesToAddress([]byte("test")) 22 23 func create() (*ManagedState, *account) { 24 db, _ := ethdb.NewMemDatabase() 25 statedb, _ := New(common.Hash{}, NewDatabase(db)) 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 }