github.com/igggame/nebulas-go@v2.1.0+incompatible/core/state/account_state_test.go (about)

     1  // Copyright (C) 2017 go-nebulas authors
     2  //
     3  // This file is part of the go-nebulas library.
     4  //
     5  // the go-nebulas library is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // the go-nebulas library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  // GNU General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU General Public License
    16  // along with the go-nebulas library.  If not, see <http://www.gnu.org/licenses/>.
    17  //
    18  
    19  package state
    20  
    21  import (
    22  	"testing"
    23  
    24  	"github.com/nebulasio/go-nebulas/common/trie"
    25  	"github.com/nebulasio/go-nebulas/storage"
    26  	"github.com/nebulasio/go-nebulas/util"
    27  	"github.com/stretchr/testify/assert"
    28  )
    29  
    30  func TestAccount_ToBytes(t *testing.T) {
    31  	stor, _ := storage.NewMemoryStorage()
    32  	vars, _ := trie.NewTrie(nil, stor, false)
    33  	acc := &account{
    34  		balance:    util.NewUint128(),
    35  		nonce:      0,
    36  		variables:  vars,
    37  		birthPlace: []byte("0x0"),
    38  	}
    39  	bytes, _ := acc.ToBytes()
    40  	a := &account{}
    41  	a.FromBytes(bytes, stor)
    42  	assert.Equal(t, acc, a)
    43  }
    44  
    45  func TestAccountState(t *testing.T) {
    46  	stor, err := storage.NewMemoryStorage()
    47  	assert.Nil(t, err)
    48  	as, err := NewAccountState(nil, stor)
    49  	assert.Nil(t, err)
    50  	accAddr1 := []byte("accAddr1")
    51  	acc1, err := as.GetOrCreateUserAccount(accAddr1)
    52  	assert.Nil(t, err)
    53  	assert.Equal(t, acc1.Balance(), util.NewUint128())
    54  	assert.Equal(t, acc1.Nonce(), uint64(0))
    55  	value, _ := util.NewUint128FromInt(16)
    56  	acc1.AddBalance(value)
    57  	acc1.IncrNonce()
    58  	acc1.Put([]byte("var0"), []byte("value0"))
    59  
    60  	asClone, err := as.Clone()
    61  	assert.Nil(t, err)
    62  	acc1Clone, err := asClone.GetOrCreateUserAccount(accAddr1)
    63  	assert.Nil(t, err)
    64  	value0, err := acc1Clone.Get([]byte("var0"))
    65  	assert.Nil(t, err)
    66  	assert.Equal(t, value0, []byte("value0"))
    67  	asRoot := as.RootHash()
    68  	assert.Nil(t, err)
    69  	asCloneRoot := asClone.RootHash()
    70  	assert.Nil(t, err)
    71  	assert.Equal(t, asRoot, asCloneRoot)
    72  	assert.Equal(t, acc1Clone.VarsHash(), acc1.VarsHash())
    73  	accAddr2 := []byte("accAddr2")
    74  	acc2, err := as.GetOrCreateUserAccount(accAddr2)
    75  	assert.Nil(t, err)
    76  	acc2.Put([]byte("var1"), []byte("value1"))
    77  	accAddr3 := []byte("accAddr3")
    78  	acc3, err := as.GetOrCreateUserAccount(accAddr3)
    79  	assert.Nil(t, err)
    80  	acc3.Put([]byte("var2"), []byte("value2"))
    81  }