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