diff --git a/action/protocol/account/protocol_test.go b/action/protocol/account/protocol_test.go index c2d3830993..d57651bb12 100644 --- a/action/protocol/account/protocol_test.go +++ b/action/protocol/account/protocol_test.go @@ -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" @@ -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()) diff --git a/action/protocol/account/transfer_test.go b/action/protocol/account/transfer_test.go index 27f4d53450..cbe7950561 100644 --- a/action/protocol/account/transfer_test.go +++ b/action/protocol/account/transfer_test.go @@ -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" @@ -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)) diff --git a/action/protocol/account/util/util.go b/action/protocol/account/util/util.go index ebdb533d91..5b1e68bf95 100644 --- a/action/protocol/account/util/util.go +++ b/action/protocol/account/util/util.go @@ -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" ) @@ -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 @@ -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 } diff --git a/action/protocol/execution/evm/contract.go b/action/protocol/execution/evm/contract.go index 9ded91c9bf..1c0bc4e92d 100644 --- a/action/protocol/execution/evm/contract.go +++ b/action/protocol/execution/evm/contract.go @@ -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" ) @@ -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 } @@ -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 { diff --git a/action/protocol/execution/evm/contract_test.go b/action/protocol/execution/evm/contract_test.go index 84a788bc9b..21a6d51750 100644 --- a/action/protocol/execution/evm/contract_test.go +++ b/action/protocol/execution/evm/contract_test.go @@ -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" @@ -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"] @@ -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() @@ -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())) } @@ -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(), diff --git a/action/protocol/execution/evm/evmstatedbadapter.go b/action/protocol/execution/evm/evmstatedbadapter.go index 51de2acec3..1dc0b60d8a 100644 --- a/action/protocol/execution/evm/evmstatedbadapter.go +++ b/action/protocol/execution/evm/evmstatedbadapter.go @@ -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" ) @@ -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 @@ -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 } @@ -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 } @@ -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 @@ -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 } @@ -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) @@ -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 { @@ -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 @@ -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 @@ -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 } @@ -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 diff --git a/action/protocol/execution/evm/evmstatedbadapter_test.go b/action/protocol/execution/evm/evmstatedbadapter_test.go index 3f8dc57be9..874c9538d0 100644 --- a/action/protocol/execution/evm/evmstatedbadapter_test.go +++ b/action/protocol/execution/evm/evmstatedbadapter_test.go @@ -20,7 +20,6 @@ import ( "github.com/iotexproject/iotex-core/config" "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/mock/mock_chainmanager" @@ -206,9 +205,9 @@ func TestSnapshotAndRevert(t *testing.T) { stateDB.SetCode(cntr1, code) v := stateDB.GetCode(cntr1) require.Equal(code, v) - require.NoError(stateDB.setContractState(byteutil.BytesTo20B(cntr1[:]), k1, v1)) - require.NoError(stateDB.setContractState(byteutil.BytesTo20B(cntr1[:]), k2, v2)) - require.NoError(stateDB.setContractState(byteutil.BytesTo20B(cntr3[:]), k3, v4)) + require.NoError(stateDB.setContractState(hash.BytesToHash160(cntr1[:]), k1, v1)) + require.NoError(stateDB.setContractState(hash.BytesToHash160(cntr1[:]), k2, v2)) + require.NoError(stateDB.setContractState(hash.BytesToHash160(cntr3[:]), k3, v4)) require.False(stateDB.Suicide(cntr2)) require.False(stateDB.Exist(cntr2)) require.False(stateDB.Suicide(cntr4)) @@ -218,13 +217,13 @@ func TestSnapshotAndRevert(t *testing.T) { require.Equal(0, stateDB.Snapshot()) stateDB.AddBalance(addr1, addAmount) - require.NoError(stateDB.setContractState(byteutil.BytesTo20B(cntr1[:]), k1, v3)) - require.NoError(stateDB.setContractState(byteutil.BytesTo20B(cntr1[:]), k2, v4)) + require.NoError(stateDB.setContractState(hash.BytesToHash160(cntr1[:]), k1, v3)) + require.NoError(stateDB.setContractState(hash.BytesToHash160(cntr1[:]), k2, v4)) stateDB.SetCode(cntr2, code) v = stateDB.GetCode(cntr2) require.Equal(code, v) - require.NoError(stateDB.setContractState(byteutil.BytesTo20B(cntr2[:]), k3, v3)) - require.NoError(stateDB.setContractState(byteutil.BytesTo20B(cntr2[:]), k4, v4)) + require.NoError(stateDB.setContractState(hash.BytesToHash160(cntr2[:]), k3, v3)) + require.NoError(stateDB.setContractState(hash.BytesToHash160(cntr2[:]), k4, v4)) // kill contract 1 and 3 require.True(stateDB.Suicide(cntr1)) require.True(stateDB.Exist(cntr1)) @@ -233,8 +232,8 @@ func TestSnapshotAndRevert(t *testing.T) { stateDB.AddPreimage(common.BytesToHash(v3[:]), []byte("hen")) require.Equal(1, stateDB.Snapshot()) - require.NoError(stateDB.setContractState(byteutil.BytesTo20B(cntr2[:]), k3, v1)) - require.NoError(stateDB.setContractState(byteutil.BytesTo20B(cntr2[:]), k4, v2)) + require.NoError(stateDB.setContractState(hash.BytesToHash160(cntr2[:]), k3, v1)) + require.NoError(stateDB.setContractState(hash.BytesToHash160(cntr2[:]), k4, v2)) require.True(stateDB.Suicide(addr1)) require.True(stateDB.Exist(addr1)) stateDB.AddPreimage(common.BytesToHash(v4[:]), []byte("fox")) @@ -246,16 +245,16 @@ func TestSnapshotAndRevert(t *testing.T) { require.True(stateDB.Exist(cntr1)) require.True(stateDB.HasSuicided(cntr3)) require.True(stateDB.Exist(cntr3)) - w, _ := stateDB.getContractState(byteutil.BytesTo20B(cntr1[:]), k1) + w, _ := stateDB.getContractState(hash.BytesToHash160(cntr1[:]), k1) require.Equal(v3, w) - w, _ = stateDB.getContractState(byteutil.BytesTo20B(cntr1[:]), k2) + w, _ = stateDB.getContractState(hash.BytesToHash160(cntr1[:]), k2) require.Equal(v4, w) // cntr2 still normal require.False(stateDB.HasSuicided(cntr2)) require.True(stateDB.Exist(cntr2)) - w, _ = stateDB.getContractState(byteutil.BytesTo20B(cntr2[:]), k3) + w, _ = stateDB.getContractState(hash.BytesToHash160(cntr2[:]), k3) require.Equal(v1, w) - w, _ = stateDB.getContractState(byteutil.BytesTo20B(cntr2[:]), k4) + w, _ = stateDB.getContractState(hash.BytesToHash160(cntr2[:]), k4) require.Equal(v2, w) // addr1 also killed require.True(stateDB.HasSuicided(addr1)) @@ -277,16 +276,16 @@ func TestSnapshotAndRevert(t *testing.T) { require.True(stateDB.Exist(cntr1)) require.True(stateDB.HasSuicided(cntr3)) require.True(stateDB.Exist(cntr3)) - w, _ = stateDB.getContractState(byteutil.BytesTo20B(cntr1[:]), k1) + w, _ = stateDB.getContractState(hash.BytesToHash160(cntr1[:]), k1) require.Equal(v3, w) - w, _ = stateDB.getContractState(byteutil.BytesTo20B(cntr1[:]), k2) + w, _ = stateDB.getContractState(hash.BytesToHash160(cntr1[:]), k2) require.Equal(v4, w) // cntr2 is normal require.False(stateDB.HasSuicided(cntr2)) require.True(stateDB.Exist(cntr2)) - w, _ = stateDB.getContractState(byteutil.BytesTo20B(cntr2[:]), k3) + w, _ = stateDB.getContractState(hash.BytesToHash160(cntr2[:]), k3) require.Equal(v3, w) - w, _ = stateDB.getContractState(byteutil.BytesTo20B(cntr2[:]), k4) + w, _ = stateDB.getContractState(hash.BytesToHash160(cntr2[:]), k4) require.Equal(v4, w) // addr1 has balance 80000 require.False(stateDB.HasSuicided(addr1)) @@ -308,9 +307,9 @@ func TestSnapshotAndRevert(t *testing.T) { require.True(stateDB.Exist(cntr1)) require.False(stateDB.HasSuicided(cntr3)) require.True(stateDB.Exist(cntr3)) - w, _ = stateDB.getContractState(byteutil.BytesTo20B(cntr1[:]), k1) + w, _ = stateDB.getContractState(hash.BytesToHash160(cntr1[:]), k1) require.Equal(v1, w) - w, _ = stateDB.getContractState(byteutil.BytesTo20B(cntr1[:]), k2) + w, _ = stateDB.getContractState(hash.BytesToHash160(cntr1[:]), k2) require.Equal(v2, w) // cntr2 and 4 does not exist require.False(stateDB.Exist(cntr2)) diff --git a/action/protocol/execution/protocol_test.go b/action/protocol/execution/protocol_test.go index 89a31cb67c..a265d50b8d 100644 --- a/action/protocol/execution/protocol_test.go +++ b/action/protocol/execution/protocol_test.go @@ -660,11 +660,9 @@ func TestProtocol_Handle(t *testing.T) { log.S().Info("======= Transfer to alfa") data, _ = hex.DecodeString("a9059cbb") - alfa := hash.ZeroHash256 addr, _ := address.FromString(testaddress.Addrinfo["alfa"].String()) to := addr.Bytes() - - alfa.SetBytes(to) + alfa := hash.BytesToHash256(to) value := hash.ZeroHash256 // send 10000 token to Alfa h = value[24:] @@ -684,10 +682,9 @@ func TestProtocol_Handle(t *testing.T) { // send 20000 token to bravo data, _ = hex.DecodeString("a9059cbb") - bravo := hash.ZeroHash256 addr, _ = address.FromString(testaddress.Addrinfo["bravo"].String()) to = addr.Bytes() - bravo.SetBytes(to) + bravo := hash.BytesToHash256(to) value = hash.ZeroHash256 h = value[24:] binary.BigEndian.PutUint64(h, 20000) @@ -844,9 +841,8 @@ func TestERC20(t *testing.T) { executions: []execCfg{ func() execCfg { data, _ := hex.DecodeString("a9059cbb") - alfa := hash.ZeroHash256 to := testaddress.Addrinfo["alfa"].Bytes() - alfa.SetBytes(to) + alfa := hash.BytesToHash256(to) value := hash.ZeroHash256 // send 10000 token to Alfa h := value[24:] @@ -866,9 +862,8 @@ func TestERC20(t *testing.T) { }(), func() execCfg { data, _ := hex.DecodeString("a9059cbb") - bravo := hash.ZeroHash256 to := testaddress.Addrinfo["bravo"].Bytes() - bravo.SetBytes(to) + bravo := hash.BytesToHash256(to) value := hash.ZeroHash256 h := value[24:] binary.BigEndian.PutUint64(h, 20000) @@ -887,9 +882,8 @@ func TestERC20(t *testing.T) { }(), func() execCfg { data, _ := hex.DecodeString("a9059cbb") - bravo := hash.ZeroHash256 to := testaddress.Addrinfo["bravo"].Bytes() - bravo.SetBytes(to) + bravo := hash.BytesToHash256(to) value := hash.ZeroHash256 h := value[24:] binary.BigEndian.PutUint64(h, 2000) @@ -908,9 +902,8 @@ func TestERC20(t *testing.T) { }(), func() execCfg { data, _ := hex.DecodeString("70a08231") - alfa := hash.ZeroHash256 to := testaddress.Addrinfo["alfa"].Bytes() - alfa.SetBytes(to) + alfa := hash.BytesToHash256(to) data = append(data, alfa[:]...) retval, _ := hex.DecodeString("0000000000000000000000000000000000000000000000000000000000001f40") return execCfg{ diff --git a/action/protocol/multichain/mainchain/createdeposit.go b/action/protocol/multichain/mainchain/createdeposit.go index 21e550878f..41070da6d8 100644 --- a/action/protocol/multichain/mainchain/createdeposit.go +++ b/action/protocol/multichain/mainchain/createdeposit.go @@ -19,7 +19,6 @@ import ( "github.com/iotexproject/iotex-core/pkg/enc" "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" ) @@ -110,7 +109,7 @@ func (p *Protocol) mutateDeposit( } depositIndex := subChain.DepositCount subChain.DepositCount++ - if err := sm.PutState(byteutil.BytesTo20B(addr.Bytes()), subChain); err != nil { + if err := sm.PutState(hash.BytesToHash160(addr.Bytes()), subChain); err != nil { return nil, err } diff --git a/action/protocol/multichain/mainchain/datamodel.go b/action/protocol/multichain/mainchain/datamodel.go index 6cb666f625..1869d0d704 100644 --- a/action/protocol/multichain/mainchain/datamodel.go +++ b/action/protocol/multichain/mainchain/datamodel.go @@ -15,7 +15,6 @@ import ( "github.com/iotexproject/iotex-core/action/protocol/multichain/mainchain/mainchainpb" "github.com/iotexproject/iotex-core/pkg/hash" "github.com/iotexproject/iotex-core/pkg/keypair" - "github.com/iotexproject/iotex-core/pkg/util/byteutil" ) // SubChain represents the state of a sub-chain in the state factory @@ -127,7 +126,7 @@ func (bp *BlockProof) Deserialize(data []byte) error { for i, v := range gen.Roots { r[i] = MerkleRoot{ Name: v.Name, - Value: byteutil.BytesTo32B(v.Value), + Value: hash.BytesToHash256(v.Value), } } *bp = BlockProof{ diff --git a/action/protocol/multichain/mainchain/datamodel_test.go b/action/protocol/multichain/mainchain/datamodel_test.go index f415e3ece9..d1cbfd54c7 100644 --- a/action/protocol/multichain/mainchain/datamodel_test.go +++ b/action/protocol/multichain/mainchain/datamodel_test.go @@ -14,7 +14,6 @@ import ( "github.com/stretchr/testify/require" "github.com/iotexproject/iotex-core/pkg/hash" - "github.com/iotexproject/iotex-core/pkg/util/byteutil" "github.com/iotexproject/iotex-core/test/testaddress" ) @@ -49,11 +48,11 @@ func TestBlockProofState(t *testing.T) { Roots: []MerkleRoot{ { Name: "abc", - Value: byteutil.BytesTo32B([]byte("10002")), + Value: hash.BytesToHash256([]byte("10002")), }, { Name: "abd", - Value: byteutil.BytesTo32B([]byte("1000d")), + Value: hash.BytesToHash256([]byte("1000d")), }, }, ProducerPublicKey: testaddress.Keyinfo["producer"].PubKey, diff --git a/action/protocol/multichain/mainchain/protocol.go b/action/protocol/multichain/mainchain/protocol.go index feab576ab9..23ef1c2140 100644 --- a/action/protocol/multichain/mainchain/protocol.go +++ b/action/protocol/multichain/mainchain/protocol.go @@ -20,7 +20,6 @@ import ( "github.com/iotexproject/iotex-core/pkg/hash" "github.com/iotexproject/iotex-core/pkg/log" "github.com/iotexproject/iotex-core/pkg/unit" - "github.com/iotexproject/iotex-core/pkg/util/byteutil" "github.com/iotexproject/iotex-core/state" "github.com/iotexproject/iotex-core/state/factory" ) @@ -118,7 +117,7 @@ func (p *Protocol) account(sender string, sm protocol.StateManager) (*state.Acco if err != nil { return nil, errors.Wrap(err, "failed to convert address to public key hash") } - addrHash := byteutil.BytesTo20B(addr.Bytes()) + addrHash := hash.BytesToHash160(addr.Bytes()) return accountutil.LoadAccount(sm, addrHash) } @@ -159,13 +158,13 @@ func srcAddressPKHash(srcAddr string) (hash.Hash160, error) { if err != nil { return hash.ZeroHash160, errors.Wrapf(err, "cannot get the public key hash of address %s", srcAddr) } - return byteutil.BytesTo20B(addr.Bytes()), nil + return hash.BytesToHash160(addr.Bytes()), nil } // SubChain returns the confirmed sub-chain state func (p *Protocol) SubChain(addr address.Address) (*SubChain, error) { var subChain SubChain - if err := p.sf.State(byteutil.BytesTo20B(addr.Bytes()), &subChain); err != nil { + if err := p.sf.State(hash.BytesToHash160(addr.Bytes()), &subChain); err != nil { return nil, errors.Wrapf(err, "error when loading state of %x", addr.Bytes()) } return &subChain, nil diff --git a/action/protocol/multichain/mainchain/protocol_test.go b/action/protocol/multichain/mainchain/protocol_test.go index db4a74483d..9ec42d2c4d 100644 --- a/action/protocol/multichain/mainchain/protocol_test.go +++ b/action/protocol/multichain/mainchain/protocol_test.go @@ -23,7 +23,6 @@ import ( "github.com/iotexproject/iotex-core/config" "github.com/iotexproject/iotex-core/pkg/hash" "github.com/iotexproject/iotex-core/pkg/unit" - "github.com/iotexproject/iotex-core/pkg/util/byteutil" "github.com/iotexproject/iotex-core/test/testaddress" ) @@ -67,7 +66,7 @@ func TestAddSubChainActions(t *testing.T) { require.NoError(t, ap.Add(selp)) roots := make(map[string]hash.Hash256) - roots["10002"] = byteutil.BytesTo32B([]byte("10002")) + roots["10002"] = hash.BytesToHash256([]byte("10002")) putBlock := action.NewPutBlock( 2, testaddress.Addrinfo["producer"].String(), diff --git a/action/protocol/multichain/mainchain/putblock.go b/action/protocol/multichain/mainchain/putblock.go index 7e74beaa90..f62f69a0e2 100644 --- a/action/protocol/multichain/mainchain/putblock.go +++ b/action/protocol/multichain/mainchain/putblock.go @@ -19,7 +19,6 @@ import ( "github.com/iotexproject/iotex-core/pkg/enc" "github.com/iotexproject/iotex-core/pkg/hash" "github.com/iotexproject/iotex-core/pkg/log" - "github.com/iotexproject/iotex-core/pkg/util/byteutil" ) func (p *Protocol) handlePutBlock(ctx context.Context, pb *action.PutBlock, sm protocol.StateManager) error { @@ -36,7 +35,7 @@ func (p *Protocol) handlePutBlock(ctx context.Context, pb *action.PutBlock, sm p return err } // Update the block producer's nonce - addrHash := byteutil.BytesTo20B(raCtx.Caller.Bytes()) + addrHash := hash.BytesToHash160(raCtx.Caller.Bytes()) acct, err := accountutil.LoadAccount(sm, addrHash) if err != nil { return err diff --git a/action/protocol/multichain/mainchain/putblock_test.go b/action/protocol/multichain/mainchain/putblock_test.go index c07e264347..933a0c1344 100644 --- a/action/protocol/multichain/mainchain/putblock_test.go +++ b/action/protocol/multichain/mainchain/putblock_test.go @@ -21,7 +21,6 @@ import ( "github.com/iotexproject/iotex-core/config" "github.com/iotexproject/iotex-core/pkg/hash" "github.com/iotexproject/iotex-core/pkg/unit" - "github.com/iotexproject/iotex-core/pkg/util/byteutil" "github.com/iotexproject/iotex-core/state/factory" "github.com/iotexproject/iotex-core/test/mock/mock_blockchain" "github.com/iotexproject/iotex-core/test/testaddress" @@ -73,7 +72,7 @@ func TestHandlePutBlock(t *testing.T) { p := NewProtocol(chain) roots := make(map[string]hash.Hash256) - roots["10002"] = byteutil.BytesTo32B([]byte("10002")) + roots["10002"] = hash.BytesToHash256([]byte("10002")) pb := action.NewPutBlock( 1, addr.String(), @@ -108,7 +107,7 @@ func TestHandlePutBlock(t *testing.T) { assert.Equal(t, bp.Roots[0].Value, roots["10002"]) // put new one - roots["10002"] = byteutil.BytesTo32B([]byte("10003")) + roots["10002"] = hash.BytesToHash256([]byte("10003")) pb2 := action.NewPutBlock( 1, addr.String(), diff --git a/action/protocol/multichain/subchain/protocol.go b/action/protocol/multichain/subchain/protocol.go index d7a38e7872..7892ebeaa7 100644 --- a/action/protocol/multichain/subchain/protocol.go +++ b/action/protocol/multichain/subchain/protocol.go @@ -16,12 +16,10 @@ import ( "github.com/iotexproject/iotex-core/action" "github.com/iotexproject/iotex-core/action/protocol" accountutil "github.com/iotexproject/iotex-core/action/protocol/account/util" - "github.com/iotexproject/iotex-core/address" "github.com/iotexproject/iotex-core/blockchain" "github.com/iotexproject/iotex-core/explorer/idl/explorer" "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" "github.com/iotexproject/iotex-core/state/factory" ) @@ -146,11 +144,3 @@ func (p *Protocol) mutateDeposit(ctx context.Context, deposit *action.SettleDepo func depositAddress(index uint64) hash.Hash160 { return hash.Hash160b([]byte(fmt.Sprintf("depositToSubChain.%d", index))) } - -func srcAddressPKHash(srcAddr string) (hash.Hash160, error) { - addr, err := address.FromString(srcAddr) - if err != nil { - return hash.ZeroHash160, err - } - return byteutil.BytesTo20B(addr.Bytes()), nil -} diff --git a/action/protocol/poll/protocol_test.go b/action/protocol/poll/protocol_test.go index b4ccc46a09..8ae083376a 100644 --- a/action/protocol/poll/protocol_test.go +++ b/action/protocol/poll/protocol_test.go @@ -19,7 +19,7 @@ import ( "github.com/iotexproject/iotex-core/action/protocol/vote/candidatesutil" "github.com/iotexproject/iotex-core/address" "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" ) @@ -61,7 +61,7 @@ func TestInitialize(t *testing.T) { operator := string(d.OperatorAddress()) addr, err := address.FromString(operator) require.NoError(err) - c, ok := candidates[byteutil.BytesTo20B(addr.Bytes())] + c, ok := candidates[hash.BytesToHash160(addr.Bytes())] require.True(ok) require.Equal(addr.String(), c.Address) } diff --git a/action/protocol/rewarding/fund.go b/action/protocol/rewarding/fund.go index 49f9fa8d68..81ebbf4189 100644 --- a/action/protocol/rewarding/fund.go +++ b/action/protocol/rewarding/fund.go @@ -16,8 +16,8 @@ import ( "github.com/iotexproject/iotex-core/action/protocol" accountutil "github.com/iotexproject/iotex-core/action/protocol/account/util" "github.com/iotexproject/iotex-core/action/protocol/rewarding/rewardingpb" + "github.com/iotexproject/iotex-core/pkg/hash" "github.com/iotexproject/iotex-core/pkg/log" - "github.com/iotexproject/iotex-core/pkg/util/byteutil" ) // fund stores the balance of the rewarding fund. The difference between total and available balance should be @@ -111,7 +111,7 @@ func (p *Protocol) assertEnoughBalance( sm protocol.StateManager, amount *big.Int, ) error { - acc, err := accountutil.LoadAccount(sm, byteutil.BytesTo20B(raCtx.Caller.Bytes())) + acc, err := accountutil.LoadAccount(sm, hash.BytesToHash160(raCtx.Caller.Bytes())) if err != nil { return err } diff --git a/action/protocol/rewarding/fund_test.go b/action/protocol/rewarding/fund_test.go index d02f007f8f..e8862e05d5 100644 --- a/action/protocol/rewarding/fund_test.go +++ b/action/protocol/rewarding/fund_test.go @@ -16,7 +16,7 @@ import ( "github.com/iotexproject/iotex-core/action/protocol" accountutil "github.com/iotexproject/iotex-core/action/protocol/account/util" - "github.com/iotexproject/iotex-core/pkg/util/byteutil" + "github.com/iotexproject/iotex-core/pkg/hash" "github.com/iotexproject/iotex-core/state/factory" ) @@ -39,7 +39,7 @@ func TestProtocol_Fund(t *testing.T) { availableBalance, err := p.AvailableBalance(ctx, ws) require.NoError(t, err) assert.Equal(t, big.NewInt(5), availableBalance) - acc, err := accountutil.LoadAccount(ws, byteutil.BytesTo20B(raCtx.Caller.Bytes())) + acc, err := accountutil.LoadAccount(ws, hash.BytesToHash160(raCtx.Caller.Bytes())) require.NoError(t, err) assert.Equal(t, big.NewInt(995), acc.Balance) diff --git a/action/protocol/rewarding/reward_test.go b/action/protocol/rewarding/reward_test.go index 0023374254..c37e8379aa 100644 --- a/action/protocol/rewarding/reward_test.go +++ b/action/protocol/rewarding/reward_test.go @@ -18,7 +18,7 @@ import ( "github.com/iotexproject/iotex-core/action/protocol" accountutil "github.com/iotexproject/iotex-core/action/protocol/account/util" - "github.com/iotexproject/iotex-core/pkg/util/byteutil" + "github.com/iotexproject/iotex-core/pkg/hash" "github.com/iotexproject/iotex-core/state/factory" ) @@ -144,7 +144,7 @@ func TestProtocol_ClaimReward(t *testing.T) { unclaimedBalance, err := p.UnclaimedBalance(ctx, ws, raCtx.Producer) require.NoError(t, err) assert.Equal(t, big.NewInt(5), unclaimedBalance) - primAcc, err := accountutil.LoadAccount(ws, byteutil.BytesTo20B(raCtx.Producer.Bytes())) + primAcc, err := accountutil.LoadAccount(ws, hash.BytesToHash160(raCtx.Producer.Bytes())) require.NoError(t, err) assert.Equal(t, big.NewInt(5), primAcc.Balance) @@ -162,7 +162,7 @@ func TestProtocol_ClaimReward(t *testing.T) { unclaimedBalance, err = p.UnclaimedBalance(ctx, ws, raCtx.Producer) require.NoError(t, err) assert.Equal(t, big.NewInt(0), unclaimedBalance) - primAcc, err = accountutil.LoadAccount(ws, byteutil.BytesTo20B(raCtx.Producer.Bytes())) + primAcc, err = accountutil.LoadAccount(ws, hash.BytesToHash160(raCtx.Producer.Bytes())) require.NoError(t, err) assert.Equal(t, big.NewInt(10), primAcc.Balance) diff --git a/action/protocol/vote/candidatesutil/candidatesutil.go b/action/protocol/vote/candidatesutil/candidatesutil.go index 0f827b3d75..5d0f556847 100644 --- a/action/protocol/vote/candidatesutil/candidatesutil.go +++ b/action/protocol/vote/candidatesutil/candidatesutil.go @@ -44,7 +44,7 @@ func LoadAndDeleteCandidates(sm protocol.StateManager, blkHeight uint64, encoded if err != nil { return errors.Wrap(err, "failed to convert address to public key hash") } - addrHash := byteutil.BytesTo20B(addr.Bytes()) + addrHash := hash.BytesToHash160(addr.Bytes()) if _, ok := candidateMap[addrHash]; ok { delete(candidateMap, addrHash) } @@ -96,7 +96,7 @@ func addCandidate(candidateMap map[hash.Hash160]*state.Candidate, encodedAddr st if err != nil { return errors.Wrap(err, "failed to get public key hash from account address") } - addrHash := byteutil.BytesTo20B(addr.Bytes()) + addrHash := hash.BytesToHash160(addr.Bytes()) if _, ok := candidateMap[addrHash]; !ok { candidateMap[addrHash] = &state.Candidate{ Address: encodedAddr, @@ -118,7 +118,7 @@ func updateCandidate( if err != nil { return errors.Wrap(err, "failed to get public key hash from account address") } - addrHash := byteutil.BytesTo20B(addr.Bytes()) + addrHash := hash.BytesToHash160(addr.Bytes()) // Candidate was added when self-nomination, always exist in cachedCandidates candidate := candidateMap[addrHash] candidate.Votes = totalWeight diff --git a/action/protocol/vote/protocol_test.go b/action/protocol/vote/protocol_test.go index 8bf52c7cb6..7873b6c18f 100644 --- a/action/protocol/vote/protocol_test.go +++ b/action/protocol/vote/protocol_test.go @@ -19,7 +19,7 @@ import ( accountutil "github.com/iotexproject/iotex-core/action/protocol/account/util" "github.com/iotexproject/iotex-core/action/protocol/vote/candidatesutil" "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" @@ -49,9 +49,9 @@ func TestProtocol_Handle(t *testing.T) { k1 := testaddress.Keyinfo["alfa"] k2 := testaddress.Keyinfo["bravo"] k3 := testaddress.Keyinfo["charlie"] - pkHash1 := byteutil.BytesTo20B(testaddress.Addrinfo["alfa"].Bytes()) - pkHash2 := byteutil.BytesTo20B(testaddress.Addrinfo["bravo"].Bytes()) - pkHash3 := byteutil.BytesTo20B(testaddress.Addrinfo["charlie"].Bytes()) + pkHash1 := hash.BytesToHash160(testaddress.Addrinfo["alfa"].Bytes()) + pkHash2 := hash.BytesToHash160(testaddress.Addrinfo["bravo"].Bytes()) + pkHash3 := hash.BytesToHash160(testaddress.Addrinfo["charlie"].Bytes()) _, err = accountutil.LoadOrCreateAccount(ws, addr1, big.NewInt(100)) require.NoError(err) diff --git a/action/putblock.go b/action/putblock.go index 4d9efa093e..87e157ccbc 100644 --- a/action/putblock.go +++ b/action/putblock.go @@ -70,7 +70,7 @@ func (pb *PutBlock) LoadProto(putBlockPb *iotextypes.PutBlock) error { pb.roots = make(map[string]hash.Hash256) for _, r := range putBlockPb.Roots { - pb.roots[r.Name] = byteutil.BytesTo32B(r.Value) + pb.roots[r.Name] = hash.BytesToHash256(r.Value) } return nil } diff --git a/action/putblock_test.go b/action/putblock_test.go index 2ba2139369..a647ffb600 100644 --- a/action/putblock_test.go +++ b/action/putblock_test.go @@ -14,7 +14,6 @@ import ( "github.com/stretchr/testify/require" "github.com/iotexproject/iotex-core/pkg/hash" - "github.com/iotexproject/iotex-core/pkg/util/byteutil" "github.com/iotexproject/iotex-core/pkg/version" "github.com/iotexproject/iotex-core/test/testaddress" ) @@ -26,12 +25,12 @@ func TestPutBlock(t *testing.T) { assert.Equal(t, uint64(1), pb.Nonce()) assert.Equal(t, addr2.String(), pb.SubChainAddress()) assert.Equal(t, uint64(10001), pb.Height()) - assert.Equal(t, byteutil.BytesTo32B([]byte("10002")), pb.Roots()["10002"]) + assert.Equal(t, hash.BytesToHash256([]byte("10002")), pb.Roots()["10002"]) assert.Equal(t, uint64(10003), pb.GasLimit()) assert.Equal(t, big.NewInt(10004), pb.GasPrice()) } roots := make(map[string]hash.Hash256) - roots["10002"] = byteutil.BytesTo32B([]byte("10002")) + roots["10002"] = hash.BytesToHash256([]byte("10002")) pb := NewPutBlock( 1, addr2.String(), @@ -47,7 +46,7 @@ func TestPutBlock(t *testing.T) { func TestPutBlockProto(t *testing.T) { addr2 := testaddress.Addrinfo["echo"] roots := make(map[string]hash.Hash256) - roots["10002"] = byteutil.BytesTo32B([]byte("10002")) + roots["10002"] = hash.BytesToHash256([]byte("10002")) pb := &PutBlock{ subChainAddress: addr2.String(), height: 10001, @@ -56,7 +55,7 @@ func TestPutBlockProto(t *testing.T) { assertPB := func(pb *PutBlock) { assert.Equal(t, addr2.String(), pb.SubChainAddress()) assert.Equal(t, uint64(10001), pb.Height()) - assert.Equal(t, byteutil.BytesTo32B([]byte("10002")), pb.Roots()["10002"]) + assert.Equal(t, hash.BytesToHash256([]byte("10002")), pb.Roots()["10002"]) } putBlockPb := pb.Proto() require.NotNil(t, putBlockPb) @@ -69,10 +68,10 @@ func TestPutBlockProto(t *testing.T) { func TestPutBlockByteStream(t *testing.T) { addr := testaddress.Addrinfo["producer"] roots := make(map[string]hash.Hash256) - roots["10002"] = byteutil.BytesTo32B([]byte("10002")) - roots["10003"] = byteutil.BytesTo32B([]byte("10003")) - roots["10004"] = byteutil.BytesTo32B([]byte("10004")) - roots["10005"] = byteutil.BytesTo32B([]byte("10005")) + roots["10002"] = hash.BytesToHash256([]byte("10002")) + roots["10003"] = hash.BytesToHash256([]byte("10003")) + roots["10004"] = hash.BytesToHash256([]byte("10004")) + roots["10005"] = hash.BytesToHash256([]byte("10005")) pb := NewPutBlock( 1, addr.String(), diff --git a/address/address_v1.go b/address/address_v1.go index 09205e72dd..16f378b657 100644 --- a/address/address_v1.go +++ b/address/address_v1.go @@ -39,10 +39,8 @@ func (v *v1) FromBytes(bytes []byte) (*AddrV1, error) { if len(bytes) != v.AddressLength { return nil, errors.Wrapf(ErrInvalidAddr, "invalid address length in bytes: %d", len(bytes)) } - var payload hash.Hash160 - copy(payload[:], bytes) return &AddrV1{ - payload: payload, + payload: hash.BytesToHash160(bytes), }, nil } diff --git a/cli/ioctl/cmd/node/node.go b/cli/ioctl/cmd/node/node.go index 08b6261ab8..8f9ad97c2a 100644 --- a/cli/ioctl/cmd/node/node.go +++ b/cli/ioctl/cmd/node/node.go @@ -25,5 +25,5 @@ var NodeCmd = &cobra.Command{ func init() { NodeCmd.AddCommand(nodeDelegateCmd) NodeCmd.AddCommand(nodeProductivityCmd) - nodeProductivityCmd.Flags().Uint64VarP(&epochNum,"epoch-num","e",0,"query specific epoch") + nodeProductivityCmd.Flags().Uint64VarP(&epochNum, "epoch-num", "e", 0, "query specific epoch") } diff --git a/explorer/explorer_test.go b/explorer/explorer_test.go index fe8752b3b2..527772fd07 100644 --- a/explorer/explorer_test.go +++ b/explorer/explorer_test.go @@ -40,7 +40,6 @@ import ( "github.com/iotexproject/iotex-core/consensus/scheme" "github.com/iotexproject/iotex-core/explorer/idl/explorer" "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/mock/mock_blockchain" @@ -807,7 +806,7 @@ func TestServiceSendAction(t *testing.T) { require.NotNil(err) roots := make(map[string]hash.Hash256) - roots["10002"] = byteutil.BytesTo32B([]byte("10002")) + roots["10002"] = hash.BytesToHash256([]byte("10002")) pb := action.NewPutBlock( 1, ta.Addrinfo["producer"].String(), @@ -1133,7 +1132,7 @@ func TestService_GetDeposits(t *testing.T) { }, )) require.NoError(ws.PutState( - byteutil.BytesTo20B(subChainAddr.Bytes()), + hash.BytesToHash160(subChainAddr.Bytes()), &mainchain.SubChain{ DepositCount: 2, OwnerPublicKey: ta.Keyinfo["producer"].PubKey, diff --git a/pkg/hash/hash.go b/pkg/hash/hash.go index a5ff179322..69d7c5a52d 100644 --- a/pkg/hash/hash.go +++ b/pkg/hash/hash.go @@ -42,10 +42,22 @@ func Hash256b(input []byte) Hash256 { return hash } -// SetBytes copies the byte slice into hash -func (h *Hash256) SetBytes(b []byte) { - if len(b) > len(h) { +// BytesToHash256 copies the byte slice into hash +func BytesToHash256(b []byte) Hash256 { + var h Hash256 + if len(b) > 32 { b = b[len(b)-32:] } copy(h[32-len(b):], b) + return h +} + +// BytesToHash160 copies the byte slice into hash +func BytesToHash160(b []byte) Hash160 { + var h Hash160 + if len(b) > 20 { + b = b[len(b)-20:] + } + copy(h[20-len(b):], b) + return h } diff --git a/pkg/util/byteutil/byteutil.go b/pkg/util/byteutil/byteutil.go index 2ed818fbd9..59c626d644 100644 --- a/pkg/util/byteutil/byteutil.go +++ b/pkg/util/byteutil/byteutil.go @@ -8,7 +8,6 @@ package byteutil import ( "github.com/iotexproject/iotex-core/pkg/enc" - "github.com/iotexproject/iotex-core/pkg/hash" ) // Uint32ToBytes converts a uint32 to 4 bytes with the machine endian @@ -30,20 +29,6 @@ func BytesToUint64(value []byte) uint64 { return enc.MachineEndian.Uint64(value) } -// BytesTo20B converts a byte slice to 20-Byte array -func BytesTo20B(b []byte) hash.Hash160 { - var h hash.Hash160 - copy(h[:], b) - return h -} - -// BytesTo32B converts a byte slice to 32-Byte array -func BytesTo32B(b []byte) hash.Hash256 { - var h hash.Hash256 - copy(h[:], b) - return h -} - // Must is a helper wraps a call to a function returing ([]byte, error) and panics if the error is not nil. func Must(d []byte, err error) []byte { if err != nil { diff --git a/state/candidate.go b/state/candidate.go index 364817bb33..bf3e033f9a 100644 --- a/state/candidate.go +++ b/state/candidate.go @@ -15,7 +15,6 @@ import ( "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/protogen/iotextypes" ) @@ -155,7 +154,7 @@ func CandidatesToMap(candidates CandidateList) (map[hash.Hash160]*Candidate, err if err != nil { return nil, errors.Wrap(err, "cannot get the hash of the address") } - pkHash := byteutil.BytesTo20B(addr.Bytes()) + pkHash := hash.BytesToHash160(addr.Bytes()) candidateMap[pkHash] = candidate } return candidateMap, nil diff --git a/state/candidate_test.go b/state/candidate_test.go index 39bc939233..21f65926d9 100644 --- a/state/candidate_test.go +++ b/state/candidate_test.go @@ -15,7 +15,6 @@ import ( "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/test/testaddress" ) @@ -37,15 +36,15 @@ func TestCandidate(t *testing.T) { cand1Addr, err := address.FromString(cand1.Address) require.NoError(err) - cand1Hash := byteutil.BytesTo20B(cand1Addr.Bytes()) + cand1Hash := hash.BytesToHash160(cand1Addr.Bytes()) cand2Addr, err := address.FromString(cand2.Address) require.NoError(err) - cand2Hash := byteutil.BytesTo20B(cand2Addr.Bytes()) + cand2Hash := hash.BytesToHash160(cand2Addr.Bytes()) cand3Addr, err := address.FromString(cand3.Address) require.NoError(err) - cand3Hash := byteutil.BytesTo20B(cand3Addr.Bytes()) + cand3Hash := hash.BytesToHash160(cand3Addr.Bytes()) candidateMap := make(map[hash.Hash160]*Candidate) candidateMap[cand1Hash] = cand1 diff --git a/state/factory/factory.go b/state/factory/factory.go index 0157d347a9..160c3c90ff 100644 --- a/state/factory/factory.go +++ b/state/factory/factory.go @@ -321,7 +321,7 @@ func (sf *factory) State(addr hash.Hash160, state interface{}) error { //====================================== func (sf *factory) rootHash() hash.Hash256 { - return byteutil.BytesTo32B(sf.accountTrie.RootHash()) + return hash.BytesToHash256(sf.accountTrie.RootHash()) } func (sf *factory) state(addr hash.Hash160, s interface{}) error { @@ -344,7 +344,7 @@ func (sf *factory) accountState(encodedAddr string) (*state.Account, error) { if err != nil { return nil, errors.Wrap(err, "error when getting the pubkey hash") } - pkHash := byteutil.BytesTo20B(addr.Bytes()) + pkHash := hash.BytesToHash160(addr.Bytes()) var account state.Account if err := sf.state(pkHash, &account); err != nil { if errors.Cause(err) == state.ErrStateNotExist { diff --git a/state/factory/factory_test.go b/state/factory/factory_test.go index 3620ce3bab..67c30ff455 100644 --- a/state/factory/factory_test.go +++ b/state/factory/factory_test.go @@ -112,7 +112,7 @@ func testSnapshot(ws WorkingSet, t *testing.T) { addr := testaddress.Addrinfo["alfa"].String() _, err := accountutil.LoadOrCreateAccount(ws, addr, big.NewInt(5)) require.NoError(err) - sHash := byteutil.BytesTo20B(testaddress.Addrinfo["alfa"].Bytes()) + sHash := hash.BytesToHash160(testaddress.Addrinfo["alfa"].Bytes()) s, err := accountutil.LoadAccount(ws, sHash) require.NoError(err) @@ -131,7 +131,7 @@ func testSnapshot(ws WorkingSet, t *testing.T) { addr = testaddress.Addrinfo["bravo"].String() _, err = accountutil.LoadOrCreateAccount(ws, addr, big.NewInt(7)) require.NoError(err) - tHash := byteutil.BytesTo20B(testaddress.Addrinfo["bravo"].Bytes()) + tHash := hash.BytesToHash160(testaddress.Addrinfo["bravo"].Bytes()) s, err = accountutil.LoadAccount(ws, tHash) require.NoError(err) @@ -883,7 +883,7 @@ func testState(sf Factory, t *testing.T) { var testAccount state.Account accountA, err := sf.AccountState(a) require.NoError(t, err) - sHash := byteutil.BytesTo20B(testaddress.Addrinfo["alfa"].Bytes()) + sHash := hash.BytesToHash160(testaddress.Addrinfo["alfa"].Bytes()) err = sf.State(sHash, &testAccount) require.NoError(t, err) require.Equal(t, accountA, &testAccount) @@ -1264,7 +1264,7 @@ func testCachedBatch(ws WorkingSet, t *testing.T, chechCachedBatchHash bool) { } // test PutState() - hashA := byteutil.BytesTo20B(testaddress.Addrinfo["alfa"].Bytes()) + hashA := hash.BytesToHash160(testaddress.Addrinfo["alfa"].Bytes()) accountA := state.EmptyAccount() accountA.Balance = big.NewInt(70) accountA.VotingWeight = big.NewInt(70) diff --git a/state/factory/statedb.go b/state/factory/statedb.go index 55b4340fa6..031b4a1372 100644 --- a/state/factory/statedb.go +++ b/state/factory/statedb.go @@ -254,7 +254,7 @@ func (sdb *stateDB) accountState(encodedAddr string) (*state.Account, error) { if err != nil { return nil, err } - pkHash := byteutil.BytesTo20B(addr.Bytes()) + pkHash := hash.BytesToHash160(addr.Bytes()) if err != nil { return nil, errors.Wrap(err, "error when getting the pubkey hash") } diff --git a/state/factory/workingset.go b/state/factory/workingset.go index 2b88e7877a..406e19ab67 100644 --- a/state/factory/workingset.go +++ b/state/factory/workingset.go @@ -112,7 +112,7 @@ func NewWorkingSet( // RootHash returns the hash of the root node of the accountTrie func (ws *workingSet) RootHash() hash.Hash256 { - return byteutil.BytesTo32B(ws.accountTrie.RootHash()) + return hash.BytesToHash256(ws.accountTrie.RootHash()) } // Digest returns the delta state digest @@ -214,7 +214,7 @@ func (ws *workingSet) UpdateBlockLevelInfo(blockHeight uint64) hash.Hash256 { func (ws *workingSet) Snapshot() int { s := ws.cb.Snapshot() - ws.trieRoots[s] = byteutil.BytesTo32B(ws.accountTrie.RootHash()) + ws.trieRoots[s] = hash.BytesToHash256(ws.accountTrie.RootHash()) return s }