Skip to content

[evm] store contract by erigon #4551

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions action/protocol/execution/evm/contract_adapter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package evm

import (
erigonstate "github.com/erigontech/erigon/core/state"
"github.com/iotexproject/go-pkgs/hash"
"github.com/pkg/errors"

"github.com/iotexproject/iotex-core/v2/action/protocol"
"github.com/iotexproject/iotex-core/v2/db/trie"
"github.com/iotexproject/iotex-core/v2/state"
)

type contractReader interface {
GetCommittedState(hash.Hash256) ([]byte, error)
GetState(hash.Hash256) ([]byte, error)
SetState(hash.Hash256, []byte) error
GetCode() ([]byte, error)
SelfState() *state.Account
}

type contractAdapter struct {
contractReader
trie Contract
erigon Contract
}

func newContractAdapter(addr hash.Hash160, account *state.Account, sm protocol.StateManager, intra *erigonstate.IntraBlockState, enableAsync bool) (Contract, error) {
v1, err := newContract(addr, account, sm, enableAsync)
if err != nil {
return nil, errors.Wrap(err, "failed to create contract")
}
v2, err := newContractErigon(addr, account, intra)
if err != nil {
return nil, errors.Wrap(err, "failed to create contractV2")
}
c := &contractAdapter{
contractReader: v1,
trie: v1,
erigon: v2,
}
return c, nil
}

func (c *contractAdapter) SetState(key hash.Hash256, value []byte) error {
if err := c.trie.SetState(key, value); err != nil {
return err
}
return c.erigon.SetState(key, value)
}

func (c *contractAdapter) SetCode(hash hash.Hash256, code []byte) {
c.trie.SetCode(hash, code)
c.erigon.SetCode(hash, code)
}

func (c *contractAdapter) Commit() error {
if err := c.trie.Commit(); err != nil {
return err
}
return c.erigon.Commit()
}

func (c *contractAdapter) LoadRoot() error {
if err := c.trie.LoadRoot(); err != nil {
return err
}
return c.erigon.LoadRoot()
}

// Iterator is only for debug
func (c *contractAdapter) Iterator() (trie.Iterator, error) {
return nil, errors.New("not supported")
}

func (c *contractAdapter) Snapshot() Contract {
v1 := c.trie.Snapshot()
v2 := c.erigon.Snapshot()
return &contractAdapter{
contractReader: v1,
trie: v1,
erigon: v2,
}
}
90 changes: 90 additions & 0 deletions action/protocol/execution/evm/contract_erigon.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package evm

import (
"math/big"

libcommon "github.com/erigontech/erigon-lib/common"
erigonstate "github.com/erigontech/erigon/core/state"
"github.com/holiman/uint256"
"github.com/iotexproject/go-pkgs/hash"
"github.com/pkg/errors"

"github.com/iotexproject/iotex-core/v2/db/trie"
"github.com/iotexproject/iotex-core/v2/pkg/log"
"github.com/iotexproject/iotex-core/v2/state"
)

type contractErigon struct {
*state.Account
intra *erigonstate.IntraBlockState
addr hash.Hash160
}

func newContractErigon(addr hash.Hash160, account *state.Account, intra *erigonstate.IntraBlockState) (Contract, error) {
c := &contractErigon{
Account: account,
intra: intra,
addr: addr,
}
return c, nil
}

func (c *contractErigon) GetCommittedState(key hash.Hash256) ([]byte, error) {
k := libcommon.Hash(key)
v := uint256.NewInt(0)
c.intra.GetCommittedState(libcommon.Address(c.addr), &k, v)
return v.Bytes(), nil
}

func (c *contractErigon) GetState(key hash.Hash256) ([]byte, error) {
k := libcommon.Hash(key)
v := uint256.NewInt(0)
c.intra.GetState(libcommon.Address(c.addr), &k, v)
return v.Bytes(), nil
}

func (c *contractErigon) SetState(key hash.Hash256, value []byte) error {
k := libcommon.Hash(key)
c.intra.SetState(libcommon.Address(c.addr), &k, *uint256.MustFromBig(big.NewInt(0).SetBytes(value)))
return nil
}

func (c *contractErigon) GetCode() ([]byte, error) {
return c.intra.GetCode(libcommon.Address(c.addr)), nil
}

func (c *contractErigon) SetCode(hash hash.Hash256, code []byte) {
c.intra.SetCode(libcommon.Address(c.addr), code)
eh := c.intra.GetCodeHash(libcommon.Address(c.addr))
log.L().Debug("SetCode", log.Hex("erigonhash", eh[:]), log.Hex("iotexhash", hash[:]))
}

func (c *contractErigon) SelfState() *state.Account {
acc := &state.Account{}
acc.SetPendingNonce(c.intra.GetNonce(libcommon.Address(c.addr)))
acc.AddBalance(c.intra.GetBalance(libcommon.Address(c.addr)).ToBig())
codeHash := c.intra.GetCodeHash(libcommon.Address(c.addr))
acc.CodeHash = codeHash[:]
return acc
}

func (c *contractErigon) Commit() error {
return nil
}

func (c *contractErigon) LoadRoot() error {
return nil
}

// Iterator is only for debug
func (c *contractErigon) Iterator() (trie.Iterator, error) {
return nil, errors.New("not supported")
}

func (c *contractErigon) Snapshot() Contract {
return &contractErigon{
Account: c.Account.Clone(),
intra: c.intra,
addr: c.addr,
}
}
14 changes: 13 additions & 1 deletion action/protocol/execution/evm/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"math/big"
"time"

erigonstate "github.com/erigontech/erigon/core/state"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/vm"
Expand Down Expand Up @@ -247,7 +248,7 @@ func ExecuteContract(
) ([]byte, *action.Receipt, error) {
ctx, span := tracer.NewSpan(ctx, "evm.ExecuteContract")
defer span.End()

var stateDB stateDB
stateDB, err := prepareStateDB(ctx, sm)
if err != nil {
return nil, nil, err
Expand All @@ -256,6 +257,17 @@ func ExecuteContract(
if err != nil {
return nil, nil, err
}
if erigonsm, ok := sm.(interface {
Erigon() (*erigonstate.IntraBlockState, bool)
}); ok {
if in, dryrun := erigonsm.Erigon(); in != nil {
if dryrun {
stateDB = NewErigonStateDBAdapterDryrun(stateDB.(*StateDBAdapter), in)
} else {
stateDB = NewErigonStateDBAdapter(stateDB.(*StateDBAdapter), in)
}
}
}
retval, depositGas, remainingGas, contractAddress, statusCode, err := executeInEVM(ctx, ps, stateDB)
if err != nil {
return nil, nil, err
Expand Down
Loading