github.com/arieschain/arieschain@v0.0.0-20191023063405-37c074544356/core/state/managed_state_test.go (about)

     1  package state
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/quickchainproject/quickchain/common"
     7  	"github.com/quickchainproject/quickchain/qctdb"
     8  )
     9  
    10  var addr = common.BytesToAddress([]byte("test"))
    11  
    12  func create() (*ManagedState, *account) {
    13  	db, _ := qctdb.NewMemDatabase()
    14  	statedb, _ := New(common.Hash{}, NewDatabase(db))
    15  	ms := ManageState(statedb)
    16  	ms.StateDB.SetNonce(addr, 100)
    17  	ms.accounts[addr] = newAccount(ms.StateDB.getStateObject(addr))
    18  	return ms, ms.accounts[addr]
    19  }
    20  
    21  func TestNewNonce(t *testing.T) {
    22  	ms, _ := create()
    23  
    24  	nonce := ms.NewNonce(addr)
    25  	if nonce != 100 {
    26  		t.Error("expected nonce 100. got", nonce)
    27  	}
    28  
    29  	nonce = ms.NewNonce(addr)
    30  	if nonce != 101 {
    31  		t.Error("expected nonce 101. got", nonce)
    32  	}
    33  }
    34  
    35  func TestRemove(t *testing.T) {
    36  	ms, account := create()
    37  
    38  	nn := make([]bool, 10)
    39  	for i := range nn {
    40  		nn[i] = true
    41  	}
    42  	account.nonces = append(account.nonces, nn...)
    43  
    44  	i := uint64(5)
    45  	ms.RemoveNonce(addr, account.nstart+i)
    46  	if len(account.nonces) != 5 {
    47  		t.Error("expected", i, "'th index to be false")
    48  	}
    49  }
    50  
    51  func TestReuse(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  	nonce := ms.NewNonce(addr)
    63  	if nonce != 105 {
    64  		t.Error("expected nonce to be 105. got", nonce)
    65  	}
    66  }
    67  
    68  func TestRemoteNonceChange(t *testing.T) {
    69  	ms, account := create()
    70  	nn := make([]bool, 10)
    71  	for i := range nn {
    72  		nn[i] = true
    73  	}
    74  	account.nonces = append(account.nonces, nn...)
    75  	ms.NewNonce(addr)
    76  
    77  	ms.StateDB.stateObjects[addr].data.Nonce = 200
    78  	nonce := ms.NewNonce(addr)
    79  	if nonce != 200 {
    80  		t.Error("expected nonce after remote update to be", 200, "got", nonce)
    81  	}
    82  	ms.NewNonce(addr)
    83  	ms.NewNonce(addr)
    84  	ms.NewNonce(addr)
    85  	ms.StateDB.stateObjects[addr].data.Nonce = 200
    86  	nonce = ms.NewNonce(addr)
    87  	if nonce != 204 {
    88  		t.Error("expected nonce after remote update to be", 204, "got", nonce)
    89  	}
    90  }
    91  
    92  func TestSetNonce(t *testing.T) {
    93  	ms, _ := create()
    94  
    95  	var addr common.Address
    96  	ms.SetNonce(addr, 10)
    97  
    98  	if ms.GetNonce(addr) != 10 {
    99  		t.Error("Expected nonce of 10, got", ms.GetNonce(addr))
   100  	}
   101  
   102  	addr[0] = 1
   103  	ms.StateDB.SetNonce(addr, 1)
   104  
   105  	if ms.GetNonce(addr) != 1 {
   106  		t.Error("Expected nonce of 1, got", ms.GetNonce(addr))
   107  	}
   108  }