github.com/annchain/OG@v0.0.9/arefactor_core/core/state/statedb_test.go (about)

     1  package state_test
     2  
     3  import (
     4  	"github.com/annchain/OG/common"
     5  	"testing"
     6  
     7  	"github.com/annchain/OG/common/crypto"
     8  	"github.com/annchain/OG/common/math"
     9  	"github.com/annchain/OG/core/state"
    10  	"github.com/annchain/OG/ogdb"
    11  )
    12  
    13  func newTestStateDB(t *testing.T) *state.StateDB {
    14  	db := ogdb.NewMemDatabase()
    15  	initRoot := common.Hash{}
    16  	stdb, err := state.NewStateDB(state.DefaultStateDBConfig(), state.NewDatabase(db), initRoot)
    17  	if err != nil {
    18  		t.Errorf("create StateDB error: %v", err)
    19  	}
    20  	return stdb
    21  }
    22  
    23  func TestStateDB(t *testing.T) {
    24  
    25  }
    26  
    27  var (
    28  	storageKey1   = crypto.Keccak256Hash([]byte("key1"))
    29  	storageValue1 = crypto.Keccak256Hash([]byte("value1"))
    30  	storageKey2   = crypto.Keccak256Hash([]byte("key2"))
    31  	storageValue2 = crypto.Keccak256Hash([]byte("value2"))
    32  	storageKey3   = crypto.Keccak256Hash([]byte("key3"))
    33  	storageValue3 = crypto.Keccak256Hash([]byte("value3"))
    34  )
    35  
    36  func TestStateStorage(t *testing.T) {
    37  	t.Parallel()
    38  
    39  	stdb := newTestStateDB(t)
    40  	addr := common.HexToAddress(testAddress)
    41  
    42  	stdb.SetState(addr, storageKey1, storageValue1)
    43  	stdb.SetState(addr, storageKey2, storageValue2)
    44  	stdb.SetState(addr, storageKey3, storageValue3)
    45  
    46  	_, err := stdb.Commit()
    47  	if err != nil {
    48  		t.Fatalf("commit storage error: %v", err)
    49  	}
    50  	// general test
    51  	st1 := stdb.GetState(addr, storageKey1)
    52  	if st1.Hex() != storageValue1.Hex() {
    53  		t.Fatalf("value1 is not committed, should be %s, get %s", st1.Hex(), storageValue1.Hex())
    54  	}
    55  
    56  	// check if is successfully committed into trie db.
    57  	stobj := stdb.GetStateObject(addr)
    58  	if stobj == nil {
    59  		t.Fatalf("get stateobject error, stobj is nil")
    60  	}
    61  	stobj.Uncache()
    62  	st2 := stdb.GetState(addr, storageKey2)
    63  	if st2.Hex() != storageValue2.Hex() {
    64  		t.Fatalf("value2 is not committed, should be %s, get %s", st2.Hex(), storageValue2.Hex())
    65  	}
    66  
    67  }
    68  
    69  func TestStateWorkFlow(t *testing.T) {
    70  	t.Parallel()
    71  
    72  	addr := common.HexToAddress(testAddress)
    73  	testnonce := uint64(123456)
    74  	testblc := int64(666)
    75  
    76  	stdb := newTestStateDB(t)
    77  	stdb.CreateAccount(addr)
    78  
    79  	stobj := stdb.GetStateObject(addr)
    80  	stobj.SetNonce(testnonce)
    81  	stobj.SetBalance(1, math.NewBigInt(testblc))
    82  
    83  	blcInStateDB := stdb.GetTokenBalance(addr, 1)
    84  	if blcInStateDB.GetInt64() != testblc {
    85  		t.Fatalf("the balance in statedb is not correct. shoud be: %d, get: %d", blcInStateDB.GetInt64(), testblc)
    86  	}
    87  	stdb.AddTokenBalance(addr, 2, math.NewBigInt(66))
    88  	blcInStateDB = stdb.GetTokenBalance(addr, 2)
    89  	//testblc = 666+32
    90  	if blcInStateDB.GetInt64() != int64(66) {
    91  		t.Fatalf("the balance in statedb is not correct. shoud be: %d, get: %d", 66, blcInStateDB.GetInt64())
    92  	}
    93  
    94  	root, err := stdb.Commit()
    95  	if err != nil {
    96  		t.Fatalf("commit statedb error: %v", err)
    97  	}
    98  	err = stdb.Database().TrieDB().Commit(root, true)
    99  	if err != nil {
   100  		t.Fatalf("commit triedb error: %v", err)
   101  	}
   102  
   103  	blcInStateDB = stdb.GetTokenBalance(addr, 1)
   104  	if blcInStateDB.GetInt64() != testblc {
   105  		t.Fatalf("the balance in statedb is not correct. shoud be: %d, get: %d", blcInStateDB.GetInt64(), testblc)
   106  	}
   107  
   108  }