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