Skip to content

Commit

Permalink
Add hash.BytesToHash256/160()
Browse files Browse the repository at this point in the history
  • Loading branch information
dustinxie authored and zjshen14 committed Mar 7, 2019
1 parent 055e694 commit 124d71a
Show file tree
Hide file tree
Showing 35 changed files with 131 additions and 169 deletions.
6 changes: 3 additions & 3 deletions action/protocol/account/protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"github.com/iotexproject/iotex-core/address"
"github.com/iotexproject/iotex-core/config"
"github.com/iotexproject/iotex-core/db"
"github.com/iotexproject/iotex-core/pkg/util/byteutil"
"github.com/iotexproject/iotex-core/pkg/hash"
"github.com/iotexproject/iotex-core/state"
"github.com/iotexproject/iotex-core/state/factory"
"github.com/iotexproject/iotex-core/test/identityset"
Expand All @@ -37,13 +37,13 @@ func TestLoadOrCreateAccountState(t *testing.T) {
ws, err := sf.NewWorkingSet()
require.NoError(err)
addrv1 := testaddress.Addrinfo["producer"]
s, err := accountutil.LoadAccount(ws, byteutil.BytesTo20B(addrv1.Bytes()))
s, err := accountutil.LoadAccount(ws, hash.BytesToHash160(addrv1.Bytes()))
require.NoError(err)
require.Equal(s.Balance, state.EmptyAccount().Balance)
require.Equal(s.VotingWeight, state.EmptyAccount().VotingWeight)
s, err = accountutil.LoadOrCreateAccount(ws, addrv1.String(), big.NewInt(5))
require.NoError(err)
s, err = accountutil.LoadAccount(ws, byteutil.BytesTo20B(addrv1.Bytes()))
s, err = accountutil.LoadAccount(ws, hash.BytesToHash160(addrv1.Bytes()))
require.NoError(err)
require.Equal(uint64(0x0), s.Nonce)
require.Equal("5", s.Balance.String())
Expand Down
10 changes: 5 additions & 5 deletions action/protocol/account/transfer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"github.com/iotexproject/iotex-core/action"
"github.com/iotexproject/iotex-core/action/protocol"
"github.com/iotexproject/iotex-core/config"
"github.com/iotexproject/iotex-core/pkg/util/byteutil"
"github.com/iotexproject/iotex-core/pkg/hash"
"github.com/iotexproject/iotex-core/state"
"github.com/iotexproject/iotex-core/state/factory"
"github.com/iotexproject/iotex-core/test/testaddress"
Expand Down Expand Up @@ -52,10 +52,10 @@ func TestProtocol_HandleTransfer(t *testing.T) {
account3 := state.Account{
VotingWeight: big.NewInt(5),
}
pubKeyHash1 := byteutil.BytesTo20B(testaddress.Addrinfo["alfa"].Bytes())
pubKeyHash2 := byteutil.BytesTo20B(testaddress.Addrinfo["bravo"].Bytes())
pubKeyHash3 := byteutil.BytesTo20B(testaddress.Addrinfo["charlie"].Bytes())
pubKeyHash4 := byteutil.BytesTo20B(testaddress.Addrinfo["delta"].Bytes())
pubKeyHash1 := hash.BytesToHash160(testaddress.Addrinfo["alfa"].Bytes())
pubKeyHash2 := hash.BytesToHash160(testaddress.Addrinfo["bravo"].Bytes())
pubKeyHash3 := hash.BytesToHash160(testaddress.Addrinfo["charlie"].Bytes())
pubKeyHash4 := hash.BytesToHash160(testaddress.Addrinfo["delta"].Bytes())

require.NoError(ws.PutState(pubKeyHash1, &account1))
require.NoError(ws.PutState(pubKeyHash2, &account2))
Expand Down
7 changes: 3 additions & 4 deletions action/protocol/account/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/iotexproject/iotex-core/action/protocol"
"github.com/iotexproject/iotex-core/address"
"github.com/iotexproject/iotex-core/pkg/hash"
"github.com/iotexproject/iotex-core/pkg/util/byteutil"
"github.com/iotexproject/iotex-core/state"
)

Expand All @@ -37,7 +36,7 @@ func LoadOrCreateAccount(sm protocol.StateManager, encodedAddr string, init *big
account = state.EmptyAccount()
return &account, errors.Wrap(err, "failed to get address public key hash from encoded address")
}
addrHash := byteutil.BytesTo20B(addr.Bytes())
addrHash := hash.BytesToHash160(addr.Bytes())
err = sm.State(addrHash, &account)
if err == nil {
return &account, nil
Expand Down Expand Up @@ -72,14 +71,14 @@ func StoreAccount(sm protocol.StateManager, encodedAddr string, account *state.A
if err != nil {
return errors.Wrap(err, "failed to get address public key hash from encoded address")
}
addrHash := byteutil.BytesTo20B(addr.Bytes())
addrHash := hash.BytesToHash160(addr.Bytes())
return sm.PutState(addrHash, account)
}

// Recorded tests if an account has been actually stored
func Recorded(sm protocol.StateManager, addr address.Address) (bool, error) {
var account state.Account
err := sm.State(byteutil.BytesTo20B(addr.Bytes()), &account)
err := sm.State(hash.BytesToHash160(addr.Bytes()), &account)
if err == nil {
return true, nil
}
Expand Down
5 changes: 2 additions & 3 deletions action/protocol/execution/evm/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/iotexproject/iotex-core/db"
"github.com/iotexproject/iotex-core/db/trie"
"github.com/iotexproject/iotex-core/pkg/hash"
"github.com/iotexproject/iotex-core/pkg/util/byteutil"
"github.com/iotexproject/iotex-core/state"
)

Expand Down Expand Up @@ -72,7 +71,7 @@ func (c *contract) GetState(key hash.Hash256) ([]byte, error) {
func (c *contract) SetState(key hash.Hash256, value []byte) error {
c.dirtyState = true
err := c.trie.Upsert(key[:], value)
c.Account.Root = byteutil.BytesTo32B(c.trie.RootHash())
c.Account.Root = hash.BytesToHash256(c.trie.RootHash())
return err
}

Expand Down Expand Up @@ -100,7 +99,7 @@ func (c *contract) SelfState() *state.Account {
func (c *contract) Commit() error {
if c.dirtyState {
// record the new root hash, global account trie will Commit all pending writes to DB
c.Account.Root = byteutil.BytesTo32B(c.trie.RootHash())
c.Account.Root = hash.BytesToHash256(c.trie.RootHash())
c.dirtyState = false
}
if c.dirtyCode {
Expand Down
27 changes: 13 additions & 14 deletions action/protocol/execution/evm/contract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"github.com/iotexproject/iotex-core/db"
"github.com/iotexproject/iotex-core/db/trie"
"github.com/iotexproject/iotex-core/pkg/hash"
"github.com/iotexproject/iotex-core/pkg/util/byteutil"
"github.com/iotexproject/iotex-core/state"
"github.com/iotexproject/iotex-core/state/factory"
"github.com/iotexproject/iotex-core/test/testaddress"
Expand Down Expand Up @@ -152,8 +151,8 @@ func TestLoadStoreContract(t *testing.T) {
v1 := hash.Hash256b([]byte("cat"))
k2 := hash.Hash256b([]byte("dog"))
v2 := hash.Hash256b([]byte("dog"))
require.Nil(stateDB.setContractState(byteutil.BytesTo20B(contract), k1, v1))
require.Nil(stateDB.setContractState(byteutil.BytesTo20B(contract), k2, v2))
require.Nil(stateDB.setContractState(hash.BytesToHash160(contract), k1, v1))
require.Nil(stateDB.setContractState(hash.BytesToHash160(contract), k2, v2))

code1 := []byte("2nd contract creation")
addr1 := testaddress.Addrinfo["bravo"]
Expand All @@ -172,8 +171,8 @@ func TestLoadStoreContract(t *testing.T) {
v3 := hash.Hash256b([]byte("egg"))
k4 := hash.Hash256b([]byte("hen"))
v4 := hash.Hash256b([]byte("hen"))
require.Nil(stateDB.setContractState(byteutil.BytesTo20B(contract1), k3, v3))
require.Nil(stateDB.setContractState(byteutil.BytesTo20B(contract1), k4, v4))
require.Nil(stateDB.setContractState(hash.BytesToHash160(contract1), k3, v3))
require.Nil(stateDB.setContractState(hash.BytesToHash160(contract1), k4, v4))
require.NoError(stateDB.commitContracts())
stateDB.clear()

Expand Down Expand Up @@ -203,26 +202,26 @@ func TestLoadStoreContract(t *testing.T) {
cb: ws.GetCachedBatch(),
}

w, err := stateDB.getContractState(byteutil.BytesTo20B(contract), k1)
w, err := stateDB.getContractState(hash.BytesToHash160(contract), k1)
require.Nil(err)
require.Equal(v1, w)
w, err = stateDB.getContractState(byteutil.BytesTo20B(contract), k2)
w, err = stateDB.getContractState(hash.BytesToHash160(contract), k2)
require.Nil(err)
require.Equal(v2, w)
_, err = stateDB.getContractState(byteutil.BytesTo20B(contract), k3)
_, err = stateDB.getContractState(hash.BytesToHash160(contract), k3)
require.Equal(trie.ErrNotExist, errors.Cause(err))
_, err = stateDB.getContractState(byteutil.BytesTo20B(contract), k4)
_, err = stateDB.getContractState(hash.BytesToHash160(contract), k4)
require.Equal(trie.ErrNotExist, errors.Cause(err))
// query second contract
w, err = stateDB.getContractState(byteutil.BytesTo20B(contract1), k3)
w, err = stateDB.getContractState(hash.BytesToHash160(contract1), k3)
require.Nil(err)
require.Equal(v3, w)
w, err = stateDB.getContractState(byteutil.BytesTo20B(contract1), k4)
w, err = stateDB.getContractState(hash.BytesToHash160(contract1), k4)
require.Nil(err)
require.Equal(v4, w)
_, err = stateDB.getContractState(byteutil.BytesTo20B(contract1), k1)
_, err = stateDB.getContractState(hash.BytesToHash160(contract1), k1)
require.Equal(trie.ErrNotExist, errors.Cause(err))
_, err = stateDB.getContractState(byteutil.BytesTo20B(contract1), k2)
_, err = stateDB.getContractState(hash.BytesToHash160(contract1), k2)
require.Equal(trie.ErrNotExist, errors.Cause(err))
require.Nil(sf.Stop(context.Background()))
}
Expand All @@ -240,7 +239,7 @@ func TestSnapshot(t *testing.T) {
v2 := hash.Hash256b([]byte("dog"))

c1, err := newContract(
byteutil.BytesTo20B(testaddress.Addrinfo["alfa"].Bytes()),
hash.BytesToHash160(testaddress.Addrinfo["alfa"].Bytes()),
s,
db.NewMemKVStore(),
db.NewCachedBatch(),
Expand Down
25 changes: 12 additions & 13 deletions action/protocol/execution/evm/evmstatedbadapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"github.com/iotexproject/iotex-core/db"
"github.com/iotexproject/iotex-core/pkg/hash"
"github.com/iotexproject/iotex-core/pkg/log"
"github.com/iotexproject/iotex-core/pkg/util/byteutil"
"github.com/iotexproject/iotex-core/state"
)

Expand Down Expand Up @@ -251,7 +250,7 @@ func (stateDB *StateDBAdapter) Suicide(evmAddr common.Address) bool {
// clears the account balance
s.Balance = nil
s.Balance = big.NewInt(0)
addrHash := byteutil.BytesTo20B(evmAddr.Bytes())
addrHash := hash.BytesToHash160(evmAddr.Bytes())
if err := stateDB.sm.PutState(addrHash, s); err != nil {
log.L().Error("Failed to kill contract.", zap.Error(err))
return false
Expand All @@ -263,7 +262,7 @@ func (stateDB *StateDBAdapter) Suicide(evmAddr common.Address) bool {

// HasSuicided returns whether the contract has been killed
func (stateDB *StateDBAdapter) HasSuicided(evmAddr common.Address) bool {
addrHash := byteutil.BytesTo20B(evmAddr.Bytes())
addrHash := hash.BytesToHash160(evmAddr.Bytes())
_, ok := stateDB.suicided[addrHash]
return ok
}
Expand All @@ -276,7 +275,7 @@ func (stateDB *StateDBAdapter) Exist(evmAddr common.Address) bool {
return false
}
log.L().Debug("Check existence.", zap.String("address", addr.String()), log.Hex("addrHash", evmAddr[:]))
addrHash := byteutil.BytesTo20B(addr.Bytes())
addrHash := hash.BytesToHash160(addr.Bytes())
if _, ok := stateDB.cachedContract[addrHash]; ok {
return true
}
Expand Down Expand Up @@ -407,7 +406,7 @@ func (stateDB *StateDBAdapter) AddPreimage(hash common.Hash, preimage []byte) {

// ForEachStorage loops each storage
func (stateDB *StateDBAdapter) ForEachStorage(addr common.Address, cb func(common.Hash, common.Hash) bool) {
ctt, err := stateDB.Contract(byteutil.BytesTo20B(addr[:]))
ctt, err := stateDB.Contract(hash.BytesToHash160(addr[:]))
if err != nil {
// stateDB.err = err
return
Expand Down Expand Up @@ -437,7 +436,7 @@ func (stateDB *StateDBAdapter) AccountState(encodedAddr string) (*state.Account,
if err != nil {
return nil, errors.Wrap(err, "failed to get public key hash from encoded address")
}
addrHash := byteutil.BytesTo20B(addr.Bytes())
addrHash := hash.BytesToHash160(addr.Bytes())
if contract, ok := stateDB.cachedContract[addrHash]; ok {
return contract.SelfState(), nil
}
Expand All @@ -450,7 +449,7 @@ func (stateDB *StateDBAdapter) AccountState(encodedAddr string) (*state.Account,

// GetCodeHash returns contract's code hash
func (stateDB *StateDBAdapter) GetCodeHash(evmAddr common.Address) common.Hash {
addr := byteutil.BytesTo20B(evmAddr[:])
addr := hash.BytesToHash160(evmAddr[:])
codeHash := common.Hash{}
if contract, ok := stateDB.cachedContract[addr]; ok {
copy(codeHash[:], contract.SelfState().CodeHash)
Expand All @@ -469,7 +468,7 @@ func (stateDB *StateDBAdapter) GetCodeHash(evmAddr common.Address) common.Hash {

// GetCode returns contract's code
func (stateDB *StateDBAdapter) GetCode(evmAddr common.Address) []byte {
addr := byteutil.BytesTo20B(evmAddr[:])
addr := hash.BytesToHash160(evmAddr[:])
if contract, ok := stateDB.cachedContract[addr]; ok {
code, err := contract.GetCode()
if err != nil {
Expand Down Expand Up @@ -504,7 +503,7 @@ func (stateDB *StateDBAdapter) GetCodeSize(evmAddr common.Address) int {

// SetCode sets contract's code
func (stateDB *StateDBAdapter) SetCode(evmAddr common.Address, code []byte) {
addr := byteutil.BytesTo20B(evmAddr[:])
addr := hash.BytesToHash160(evmAddr[:])
if contract, ok := stateDB.cachedContract[addr]; ok {
contract.SetCode(hash.Hash256b(code), code)
return
Expand All @@ -519,7 +518,7 @@ func (stateDB *StateDBAdapter) SetCode(evmAddr common.Address, code []byte) {
// GetState gets state
func (stateDB *StateDBAdapter) GetState(evmAddr common.Address, k common.Hash) common.Hash {
storage := common.Hash{}
v, err := stateDB.getContractState(byteutil.BytesTo20B(evmAddr[:]), byteutil.BytesTo32B(k[:]))
v, err := stateDB.getContractState(hash.BytesToHash160(evmAddr[:]), hash.BytesToHash256(k[:]))
if err != nil {
log.L().Error("Failed to get state.", zap.Error(err))
return storage
Expand All @@ -531,7 +530,7 @@ func (stateDB *StateDBAdapter) GetState(evmAddr common.Address, k common.Hash) c

// SetState sets state
func (stateDB *StateDBAdapter) SetState(evmAddr common.Address, k, v common.Hash) {
if err := stateDB.setContractState(byteutil.BytesTo20B(evmAddr[:]), byteutil.BytesTo32B(k[:]), byteutil.BytesTo32B(v[:])); err != nil {
if err := stateDB.setContractState(hash.BytesToHash160(evmAddr[:]), hash.BytesToHash256(k[:]), hash.BytesToHash256(v[:])); err != nil {
log.L().Error("Failed to set state.", zap.Error(err))
return
}
Expand All @@ -542,14 +541,14 @@ func (stateDB *StateDBAdapter) SetState(evmAddr common.Address, k, v common.Hash
func (stateDB *StateDBAdapter) getContractState(addr hash.Hash160, key hash.Hash256) (hash.Hash256, error) {
if contract, ok := stateDB.cachedContract[addr]; ok {
v, err := contract.GetState(key)
return byteutil.BytesTo32B(v), err
return hash.BytesToHash256(v), err
}
contract, err := stateDB.getContract(addr)
if err != nil {
return hash.ZeroHash256, errors.Wrapf(err, "failed to GetContractState for contract %x", addr)
}
v, err := contract.GetState(key)
return byteutil.BytesTo32B(v), err
return hash.BytesToHash256(v), err
}

// setContractState writes contract's storage value
Expand Down
Loading

0 comments on commit 124d71a

Please sign in to comment.