github.com/xfond/eth-implementation@v1.8.9-0.20180514135602-f6bc65fc6811/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/ethereum/go-ethereum/common"
    23  	"github.com/ethereum/go-ethereum/ethdb"
    24  )
    25  
    26  var addr = common.BytesToAddress([]byte("test"))
    27  
    28  func create() (*ManagedState, *account) {
    29  	statedb, _ := New(common.Hash{}, NewDatabase(ethdb.NewMemDatabase()))
    30  	ms := ManageState(statedb)
    31  	ms.StateDB.SetNonce(addr, 100)
    32  	ms.accounts[addr] = newAccount(ms.StateDB.getStateObject(addr))
    33  	return ms, ms.accounts[addr]
    34  }
    35  
    36  func TestNewNonce(t *testing.T) {
    37  	ms, _ := create()
    38  
    39  	nonce := ms.NewNonce(addr)
    40  	if nonce != 100 {
    41  		t.Error("expected nonce 100. got", nonce)
    42  	}
    43  
    44  	nonce = ms.NewNonce(addr)
    45  	if nonce != 101 {
    46  		t.Error("expected nonce 101. got", nonce)
    47  	}
    48  }
    49  
    50  func TestRemove(t *testing.T) {
    51  	ms, account := create()
    52  
    53  	nn := make([]bool, 10)
    54  	for i := range nn {
    55  		nn[i] = true
    56  	}
    57  	account.nonces = append(account.nonces, nn...)
    58  
    59  	i := uint64(5)
    60  	ms.RemoveNonce(addr, account.nstart+i)
    61  	if len(account.nonces) != 5 {
    62  		t.Error("expected", i, "'th index to be false")
    63  	}
    64  }
    65  
    66  func TestReuse(t *testing.T) {
    67  	ms, account := create()
    68  
    69  	nn := make([]bool, 10)
    70  	for i := range nn {
    71  		nn[i] = true
    72  	}
    73  	account.nonces = append(account.nonces, nn...)
    74  
    75  	i := uint64(5)
    76  	ms.RemoveNonce(addr, account.nstart+i)
    77  	nonce := ms.NewNonce(addr)
    78  	if nonce != 105 {
    79  		t.Error("expected nonce to be 105. got", nonce)
    80  	}
    81  }
    82  
    83  func TestRemoteNonceChange(t *testing.T) {
    84  	ms, account := create()
    85  	nn := make([]bool, 10)
    86  	for i := range nn {
    87  		nn[i] = true
    88  	}
    89  	account.nonces = append(account.nonces, nn...)
    90  	ms.NewNonce(addr)
    91  
    92  	ms.StateDB.stateObjects[addr].data.Nonce = 200
    93  	nonce := ms.NewNonce(addr)
    94  	if nonce != 200 {
    95  		t.Error("expected nonce after remote update to be", 200, "got", nonce)
    96  	}
    97  	ms.NewNonce(addr)
    98  	ms.NewNonce(addr)
    99  	ms.NewNonce(addr)
   100  	ms.StateDB.stateObjects[addr].data.Nonce = 200
   101  	nonce = ms.NewNonce(addr)
   102  	if nonce != 204 {
   103  		t.Error("expected nonce after remote update to be", 204, "got", nonce)
   104  	}
   105  }
   106  
   107  func TestSetNonce(t *testing.T) {
   108  	ms, _ := create()
   109  
   110  	var addr common.Address
   111  	ms.SetNonce(addr, 10)
   112  
   113  	if ms.GetNonce(addr) != 10 {
   114  		t.Error("Expected nonce of 10, got", ms.GetNonce(addr))
   115  	}
   116  
   117  	addr[0] = 1
   118  	ms.StateDB.SetNonce(addr, 1)
   119  
   120  	if ms.GetNonce(addr) != 1 {
   121  		t.Error("Expected nonce of 1, got", ms.GetNonce(addr))
   122  	}
   123  }