Skip to content

Commit

Permalink
Merge branch 'master' into datacopy
Browse files Browse the repository at this point in the history
  • Loading branch information
dustinxie committed Sep 17, 2021
2 parents 8740308 + 44e0a68 commit 8e77b1f
Show file tree
Hide file tree
Showing 24 changed files with 179 additions and 310 deletions.
7 changes: 3 additions & 4 deletions action/protocol/account/protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"

"github.com/iotexproject/go-pkgs/hash"
"github.com/iotexproject/iotex-address/address"
"github.com/iotexproject/iotex-core/action/protocol"
accountutil "github.com/iotexproject/iotex-core/action/protocol/account/util"
Expand Down Expand Up @@ -60,13 +59,13 @@ func TestLoadOrCreateAccountState(t *testing.T) {
}).AnyTimes()

addrv1 := identityset.Address(27)
s, err := accountutil.LoadAccount(sm, hash.BytesToHash160(addrv1.Bytes()))
s, err := accountutil.LoadAccount(sm, addrv1)
require.NoError(err)
require.Equal(s.Balance, state.EmptyAccount().Balance)

// create account
require.NoError(createAccount(sm, addrv1.String(), big.NewInt(5)))
s, err = accountutil.LoadAccount(sm, hash.BytesToHash160(addrv1.Bytes()))
s, err = accountutil.LoadAccount(sm, addrv1)
require.NoError(err)
require.Equal("5", s.Balance.String())
require.Equal(uint64(0x0), s.Nonce)
Expand Down Expand Up @@ -128,7 +127,7 @@ func TestProtocol_Initialize(t *testing.T) {
)

require.Error(createAccount(sm, identityset.Address(0).String(), big.NewInt(0)))
acc0, err := accountutil.LoadAccount(sm, hash.BytesToHash160(identityset.Address(0).Bytes()))
acc0, err := accountutil.LoadAccount(sm, identityset.Address(0))
require.NoError(err)
require.Equal(big.NewInt(100), acc0.Balance)
}
Expand Down
3 changes: 1 addition & 2 deletions action/protocol/account/transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"context"
"math/big"

"github.com/iotexproject/go-pkgs/hash"
"github.com/iotexproject/iotex-address/address"
"github.com/pkg/errors"

Expand Down Expand Up @@ -69,7 +68,7 @@ func (p *Protocol) handleTransfer(ctx context.Context, act action.Action, sm pro
if err != nil {
return nil, errors.Wrapf(err, "failed to decode recipient address %s", tsf.Recipient())
}
recipientAcct, err := accountutil.LoadAccount(sm, hash.BytesToHash160(recipientAddr.Bytes()))
recipientAcct, err := accountutil.LoadAccount(sm, recipientAddr)
if err == nil && recipientAcct.IsContract() {
// update sender Nonce
accountutil.SetNonce(tsf, sender)
Expand Down
9 changes: 7 additions & 2 deletions action/protocol/account/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,13 @@ func LoadOrCreateAccount(sm protocol.StateManager, encodedAddr string) (*state.A
return nil, err
}

// LoadAccount loads an account state
func LoadAccount(sr protocol.StateReader, addrHash hash.Hash160) (*state.Account, error) {
// LoadAccount loads an account state by address.Address
func LoadAccount(sr protocol.StateReader, addr address.Address) (*state.Account, error) {
return LoadAccountByHash160(sr, hash.BytesToHash160(addr.Bytes()))
}

// LoadAccountByHash160 loads an account state by 20-byte address
func LoadAccountByHash160(sr protocol.StateReader, addrHash hash.Hash160) (*state.Account, error) {
var account state.Account
if _, err := sr.State(&account, protocol.LegacyKeyOption(addrHash)); err != nil {
if errors.Cause(err) == state.ErrStateNotExist {
Expand Down
3 changes: 0 additions & 3 deletions action/protocol/execution/evm/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,9 +270,6 @@ func getChainConfig(g genesis.Blockchain, height uint64) *params.ChainConfig {
if g.IsIceland(height) {
chainConfig.ChainID = new(big.Int).SetUint64(uint64(config.EVMNetworkID()))
}
// enable Berlin + London at Jutland
chainConfig.BerlinBlock = new(big.Int).SetUint64(g.JutlandBlockHeight)
chainConfig.LondonBlock = new(big.Int).SetUint64(g.JutlandBlockHeight)
return &chainConfig
}

Expand Down
12 changes: 6 additions & 6 deletions action/protocol/execution/evm/evm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,16 +217,16 @@ func TestConstantinople(t *testing.T) {
} else {
require.Nil(evmChainConfig.ChainID)
}
require.Equal(big.NewInt(int64(g.IcelandBlockHeight)), evmChainConfig.MuirGlacierBlock)
require.Equal(big.NewInt(int64(g.IcelandBlockHeight)), evmChainConfig.IstanbulBlock)
require.Equal(isIceland, evmChainConfig.IsIstanbul(evm.Context.BlockNumber))
require.Equal(isIceland, evmChainConfig.IsMuirGlacier(evm.Context.BlockNumber))
require.Equal(isIceland, chainRules.IsIstanbul)

// jutland = enable Berlin + London
isJutland := g.IsJutland(e.height)
require.Equal(isJutland, evmChainConfig.IsBerlin(evm.Context.BlockNumber))
require.Equal(isJutland, evmChainConfig.IsLondon(evm.Context.BlockNumber))
require.Equal(isJutland, chainRules.IsBerlin)
require.Equal(isJutland, chainRules.IsLondon)
require.False(evmChainConfig.IsBerlin(evm.Context.BlockNumber))
require.False(evmChainConfig.IsLondon(evm.Context.BlockNumber))
require.False(chainRules.IsBerlin)
require.False(chainRules.IsLondon)
}
}

Expand Down
49 changes: 9 additions & 40 deletions action/protocol/execution/evm/evmstatedbadapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ type (
suicideSnapshot map[int]deleteAccount // snapshots of suicide accounts
preimages preimageMap
preimageSnapshot map[int]preimageMap
accessList *accessList // Per-transaction access list
accessListSnapshot map[int]*accessList
notFixTopicCopyBug bool
asyncContractTrie bool
sortCachedContracts bool
Expand Down Expand Up @@ -111,8 +109,6 @@ func NewStateDBAdapter(
suicideSnapshot: make(map[int]deleteAccount),
preimages: make(preimageMap),
preimageSnapshot: make(map[int]preimageMap),
accessList: newAccessList(),
accessListSnapshot: make(map[int]*accessList),
notFixTopicCopyBug: notFixTopicCopyBug,
asyncContractTrie: asyncContractTrie,
}
Expand Down Expand Up @@ -385,43 +381,25 @@ func (stateDB *StateDBAdapter) Exist(evmAddr common.Address) bool {
//
// This method should only be called if Berlin/2929+2930 is applicable at the current number.
func (stateDB *StateDBAdapter) PrepareAccessList(sender common.Address, dst *common.Address, precompiles []common.Address, list types.AccessList) {
stateDB.AddAddressToAccessList(sender)
if dst != nil {
stateDB.AddAddressToAccessList(*dst)
// If it's a create-tx, the destination will be added inside evm.create
}
for _, addr := range precompiles {
stateDB.AddAddressToAccessList(addr)
}
for _, el := range list {
stateDB.AddAddressToAccessList(el.Address)
for _, key := range el.StorageKeys {
stateDB.AddSlotToAccessList(el.Address, key)
}
}
}

// AddressInAccessList returns true if the given address is in the access list
func (stateDB *StateDBAdapter) AddressInAccessList(addr common.Address) bool {
return stateDB.accessList.ContainsAddress(addr)
return false
}

// SlotInAccessList returns true if the given (address, slot)-tuple is in the access list
func (stateDB *StateDBAdapter) SlotInAccessList(addr common.Address, slot common.Hash) (addressOk bool, slotOk bool) {
return stateDB.accessList.Contains(addr, slot)
return false, false
}

// AddAddressToAccessList adds the given address to the access list. This operation is safe to perform
// even if the feature/fork is not active yet
func (stateDB *StateDBAdapter) AddAddressToAccessList(addr common.Address) {
stateDB.accessList.AddAddress(addr)
}
func (stateDB *StateDBAdapter) AddAddressToAccessList(addr common.Address) {}

// AddSlotToAccessList adds the given (address,slot) to the access list. This operation is safe to perform
// even if the feature/fork is not active yet
func (stateDB *StateDBAdapter) AddSlotToAccessList(addr common.Address, slot common.Hash) {
stateDB.accessList.AddSlot(addr, slot)
}
func (stateDB *StateDBAdapter) AddSlotToAccessList(addr common.Address, slot common.Hash) {}

// Empty returns true if the the contract is empty
func (stateDB *StateDBAdapter) Empty(evmAddr common.Address) bool {
Expand Down Expand Up @@ -471,9 +449,6 @@ func (stateDB *StateDBAdapter) RevertToSnapshot(snapshot int) {
// restore preimages
stateDB.preimages = nil
stateDB.preimages = stateDB.preimageSnapshot[snapshot]
// restore access list
stateDB.accessList = nil
stateDB.accessList = stateDB.accessListSnapshot[snapshot]
}

func (stateDB *StateDBAdapter) cachedContractAddrs() []hash.Hash160 {
Expand Down Expand Up @@ -514,8 +489,6 @@ func (stateDB *StateDBAdapter) Snapshot() int {
p[k] = v
}
stateDB.preimageSnapshot[sn] = p
// save a copy of access list
stateDB.accessListSnapshot[sn] = stateDB.accessList.Copy()
return sn
}

Expand Down Expand Up @@ -622,7 +595,7 @@ func (stateDB *StateDBAdapter) AccountState(encodedAddr string) (*state.Account,
if contract, ok := stateDB.cachedContract[addrHash]; ok {
return contract.SelfState(), nil
}
return accountutil.LoadAccount(stateDB.sm, addrHash)
return accountutil.LoadAccountByHash160(stateDB.sm, addrHash)
}

//======================================
Expand All @@ -637,7 +610,7 @@ func (stateDB *StateDBAdapter) GetCodeHash(evmAddr common.Address) common.Hash {
copy(codeHash[:], contract.SelfState().CodeHash)
return codeHash
}
account, err := accountutil.LoadAccount(stateDB.sm, addr)
account, err := accountutil.LoadAccountByHash160(stateDB.sm, addr)
if err != nil {
log.L().Error("Failed to get code hash.", zap.Error(err))
// TODO (zhi) not all err should be logged
Expand All @@ -659,7 +632,7 @@ func (stateDB *StateDBAdapter) GetCode(evmAddr common.Address) []byte {
}
return code
}
account, err := accountutil.LoadAccount(stateDB.sm, addr)
account, err := accountutil.LoadAccountByHash160(stateDB.sm, addr)
if err != nil {
log.L().Error("Failed to load account state for address.", log.Hex("addrHash", addr[:]))
return nil
Expand Down Expand Up @@ -706,7 +679,7 @@ func (stateDB *StateDBAdapter) GetCommittedState(evmAddr common.Address, k commo
}
v, err := contract.GetCommittedState(hash.BytesToHash256(k[:]))
if err != nil {
log.L().Error("Failed to get committed state.", zap.Error(err))
log.L().Debug("Failed to get committed state.", zap.Error(err))
stateDB.logError(err)
return common.Hash{}
}
Expand Down Expand Up @@ -829,7 +802,7 @@ func (stateDB *StateDBAdapter) getContract(addr hash.Hash160) (Contract, error)
}

func (stateDB *StateDBAdapter) getNewContract(addr hash.Hash160) (Contract, error) {
account, err := accountutil.LoadAccount(stateDB.sm, addr)
account, err := accountutil.LoadAccountByHash160(stateDB.sm, addr)
if err != nil {
return nil, errors.Wrapf(err, "failed to load account state for address %x", addr)
}
Expand All @@ -850,14 +823,10 @@ func (stateDB *StateDBAdapter) clear() {
stateDB.suicideSnapshot = nil
stateDB.preimages = nil
stateDB.preimageSnapshot = nil
stateDB.accessList = nil
stateDB.accessListSnapshot = nil
stateDB.cachedContract = make(contractMap)
stateDB.contractSnapshot = make(map[int]contractMap)
stateDB.suicided = make(deleteAccount)
stateDB.suicideSnapshot = make(map[int]deleteAccount)
stateDB.preimages = make(preimageMap)
stateDB.preimageSnapshot = make(map[int]preimageMap)
stateDB.accessList = newAccessList()
stateDB.accessListSnapshot = make(map[int]*accessList)
}
55 changes: 0 additions & 55 deletions action/protocol/execution/evm/evmstatedbadapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,6 @@ func TestSnapshotRevertAndCommit(t *testing.T) {
{common.BytesToHash(v1[:]), []byte("cat")},
{common.BytesToHash(v2[:]), []byte("dog")},
},
[]access{
{c1, []common.Hash{k1, k2}, []common.Hash{k3, k4}, false},
},
},
{
[]bal{
Expand All @@ -233,10 +230,6 @@ func TestSnapshotRevertAndCommit(t *testing.T) {
[]image{
{common.BytesToHash(v3[:]), []byte("hen")},
},
[]access{
{c1, []common.Hash{k3, k4}, nil, true},
{c2, []common.Hash{k1, k3}, []common.Hash{k2, k4}, false},
},
},
{
nil,
Expand All @@ -251,9 +244,6 @@ func TestSnapshotRevertAndCommit(t *testing.T) {
[]image{
{common.BytesToHash(v4[:]), []byte("fox")},
},
[]access{
{c2, []common.Hash{k2, k4}, nil, true},
},
},
}

Expand Down Expand Up @@ -281,25 +271,6 @@ func TestSnapshotRevertAndCommit(t *testing.T) {
for _, e := range test.preimage {
stateDB.AddPreimage(e.hash, e.v)
}
// set access list
for _, e := range test.accessList {
require.Equal(e.exist, stateDB.AddressInAccessList(e.addr))
for _, slot := range e.slots {
aOk, sOk := stateDB.SlotInAccessList(e.addr, slot)
require.Equal(e.exist, aOk)
require.False(sOk)
stateDB.AddSlotToAccessList(e.addr, slot)
e.exist = true
aOk, sOk = stateDB.SlotInAccessList(e.addr, slot)
require.True(aOk)
require.True(sOk)
}
for _, slot := range e.nx {
aOk, sOk := stateDB.SlotInAccessList(e.addr, slot)
require.True(aOk)
require.False(sOk)
}
}
require.Equal(i, stateDB.Snapshot())
}

Expand Down Expand Up @@ -328,10 +299,6 @@ func TestSnapshotRevertAndCommit(t *testing.T) {
{common.BytesToHash(v3[:]), []byte("hen")},
{common.BytesToHash(v4[:]), []byte("fox")},
},
[]access{
{c1, []common.Hash{k1, k2, k3, k4}, nil, true},
{c2, []common.Hash{k1, k2, k3, k4}, nil, true},
},
},
{
[]bal{
Expand All @@ -352,10 +319,6 @@ func TestSnapshotRevertAndCommit(t *testing.T) {
{common.BytesToHash(v3[:]), []byte("hen")},
{common.BytesToHash(v4[:]), []byte(nil)},
},
[]access{
{c1, []common.Hash{k1, k2, k3, k4}, nil, true},
{c2, []common.Hash{k1, k3}, []common.Hash{k2, k4}, true},
},
},
{
[]bal{
Expand All @@ -380,10 +343,6 @@ func TestSnapshotRevertAndCommit(t *testing.T) {
{common.BytesToHash(v3[:]), []byte(nil)},
{common.BytesToHash(v4[:]), []byte(nil)},
},
[]access{
{c1, []common.Hash{k1, k2}, []common.Hash{k3, k4}, true},
{c2, nil, []common.Hash{k1, k2, k3, k4}, false},
},
},
}

Expand All @@ -410,20 +369,6 @@ func TestSnapshotRevertAndCommit(t *testing.T) {
v := stateDB.preimages[e.hash]
require.Equal(e.v, []byte(v))
}
// test access list
for _, e := range test.accessList {
require.Equal(e.exist, stateDB.AddressInAccessList(e.addr))
for _, slot := range e.slots {
aOk, sOk := stateDB.SlotInAccessList(e.addr, slot)
require.Equal(e.exist, aOk)
require.True(sOk)
}
for _, slot := range e.nx {
aOk, sOk := stateDB.SlotInAccessList(e.addr, slot)
require.Equal(e.exist, aOk)
require.False(sOk)
}
}
}

// commit snapshot 0's state
Expand Down
17 changes: 5 additions & 12 deletions action/protocol/execution/evm/testdata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,12 @@ type (
hash common.Hash
v []byte
}
access struct {
addr common.Address
slots []common.Hash
nx []common.Hash
exist bool
}
stateDBTest struct {
balance []bal
codes []code
states []evmSet
suicide []sui
preimage []image
accessList []access
balance []bal
codes []code
states []evmSet
suicide []sui
preimage []image
}
)

Expand Down
2 changes: 1 addition & 1 deletion action/protocol/execution/protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (eb *ExpectedBalance) Balance() *big.Int {

func readCode(sr protocol.StateReader, addr []byte) ([]byte, error) {
var c evm.SerializableBytes
account, err := accountutil.LoadAccount(sr, hash.BytesToHash160(addr))
account, err := accountutil.LoadAccountByHash160(sr, hash.BytesToHash160(addr))
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit 8e77b1f

Please sign in to comment.