Skip to content

Commit

Permalink
feat: add eth api dummy
Browse files Browse the repository at this point in the history
  • Loading branch information
simlecode committed Jan 31, 2023
1 parent b85b5ec commit 3c77fad
Show file tree
Hide file tree
Showing 8 changed files with 264 additions and 103 deletions.
14 changes: 6 additions & 8 deletions app/node/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,14 +166,12 @@ func (b *Builder) build(ctx context.Context) (*Node, error) {
blockDelay := b.repo.Config().NetworkParams.BlockDelay
nd.common = common.NewCommonModule(nd.chain, nd.network, blockDelay)

if b.repo.Config().FevmConfig.EnableEthPRC {
txHashDBPath, err := b.repo.SqlitePath()
if err != nil {
return nil, err
}
if nd.eth, err = eth.NewEthSubModule(b.repo.Config(), nd.chain, nd.mpool, txHashDBPath); err != nil {
return nil, err
}
txHashDBPath, err := b.repo.SqlitePath()
if err != nil {
return nil, err
}
if nd.eth, err = eth.NewEthSubModule(b.repo.Config(), nd.chain, nd.mpool, txHashDBPath); err != nil {
return nil, err
}

apiBuilder := NewBuilder()
Expand Down
134 changes: 134 additions & 0 deletions app/submodule/eth/dummy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package eth

import (
"context"
"errors"

"github.com/ipfs/go-cid"

v1 "github.com/filecoin-project/venus/venus-shared/api/chain/v1"
"github.com/filecoin-project/venus/venus-shared/types"
)

var ErrModuleDisabled = errors.New("module disabled, enable with Fevm.EnableEthRPC")

type ethAPIDummy struct{}

func (e *ethAPIDummy) EthGetMessageCidByTransactionHash(ctx context.Context, txHash *types.EthHash) (*cid.Cid, error) {
return nil, ErrModuleDisabled
}

func (e *ethAPIDummy) EthGetTransactionHashByCid(ctx context.Context, cid cid.Cid) (*types.EthHash, error) {
return nil, ErrModuleDisabled
}

func (e *ethAPIDummy) EthBlockNumber(ctx context.Context) (types.EthUint64, error) {
return 0, ErrModuleDisabled
}

func (e *ethAPIDummy) EthAccounts(ctx context.Context) ([]types.EthAddress, error) {
return nil, ErrModuleDisabled
}

func (e *ethAPIDummy) EthGetBlockTransactionCountByNumber(ctx context.Context, blkNum types.EthUint64) (types.EthUint64, error) {
return 0, ErrModuleDisabled
}

func (e *ethAPIDummy) EthGetBlockTransactionCountByHash(ctx context.Context, blkHash types.EthHash) (types.EthUint64, error) {
return 0, ErrModuleDisabled
}

func (e *ethAPIDummy) EthGetBlockByHash(ctx context.Context, blkHash types.EthHash, fullTxInfo bool) (types.EthBlock, error) {
return types.EthBlock{}, ErrModuleDisabled
}

func (e *ethAPIDummy) EthGetBlockByNumber(ctx context.Context, blkNum string, fullTxInfo bool) (types.EthBlock, error) {
return types.EthBlock{}, ErrModuleDisabled
}

func (e *ethAPIDummy) EthGetTransactionByHash(ctx context.Context, txHash *types.EthHash) (*types.EthTx, error) {
return nil, ErrModuleDisabled
}

func (e *ethAPIDummy) EthGetTransactionCount(ctx context.Context, sender types.EthAddress, blkOpt string) (types.EthUint64, error) {
return 0, ErrModuleDisabled
}

func (e *ethAPIDummy) EthGetTransactionReceipt(ctx context.Context, txHash types.EthHash) (*types.EthTxReceipt, error) {
return nil, ErrModuleDisabled
}

func (e *ethAPIDummy) EthGetTransactionByBlockHashAndIndex(ctx context.Context, blkHash types.EthHash, txIndex types.EthUint64) (types.EthTx, error) {
return types.EthTx{}, ErrModuleDisabled
}

func (e *ethAPIDummy) EthGetTransactionByBlockNumberAndIndex(ctx context.Context, blkNum types.EthUint64, txIndex types.EthUint64) (types.EthTx, error) {
return types.EthTx{}, ErrModuleDisabled
}

func (e *ethAPIDummy) EthGetCode(ctx context.Context, address types.EthAddress, blkOpt string) (types.EthBytes, error) {
return nil, ErrModuleDisabled
}

func (e *ethAPIDummy) EthGetStorageAt(ctx context.Context, address types.EthAddress, position types.EthBytes, blkParam string) (types.EthBytes, error) {
return nil, ErrModuleDisabled
}

func (e *ethAPIDummy) EthGetBalance(ctx context.Context, address types.EthAddress, blkParam string) (types.EthBigInt, error) {
return types.EthBigIntZero, ErrModuleDisabled
}

func (e *ethAPIDummy) EthFeeHistory(ctx context.Context, blkCount types.EthUint64, newestBlk string, rewardPercentiles []float64) (types.EthFeeHistory, error) {
return types.EthFeeHistory{}, ErrModuleDisabled
}

func (e *ethAPIDummy) EthChainId(ctx context.Context) (types.EthUint64, error) {
return 0, ErrModuleDisabled
}

func (e *ethAPIDummy) NetVersion(ctx context.Context) (string, error) {
return "", ErrModuleDisabled
}

func (e *ethAPIDummy) NetListening(ctx context.Context) (bool, error) {
return false, ErrModuleDisabled
}

func (e *ethAPIDummy) EthProtocolVersion(ctx context.Context) (types.EthUint64, error) {
return 0, ErrModuleDisabled
}

func (e *ethAPIDummy) EthGasPrice(ctx context.Context) (types.EthBigInt, error) {
return types.EthBigIntZero, ErrModuleDisabled
}

func (e *ethAPIDummy) EthEstimateGas(ctx context.Context, tx types.EthCall) (types.EthUint64, error) {
return 0, ErrModuleDisabled
}

func (e *ethAPIDummy) EthCall(ctx context.Context, tx types.EthCall, blkParam string) (types.EthBytes, error) {
return nil, ErrModuleDisabled
}

func (e *ethAPIDummy) EthMaxPriorityFeePerGas(ctx context.Context) (types.EthBigInt, error) {
return types.EthBigIntZero, ErrModuleDisabled
}

func (e *ethAPIDummy) EthSendRawTransaction(ctx context.Context, rawTx types.EthBytes) (types.EthHash, error) {
return types.EthHash{}, ErrModuleDisabled
}

func (e *ethAPIDummy) Web3ClientVersion(ctx context.Context) (string, error) {
return "", ErrModuleDisabled
}

func (e *ethAPIDummy) start(_ context.Context) error {
return nil
}

func (e *ethAPIDummy) close() error {
return nil
}

var _ v1.IETH = &ethAPIDummy{}
var _ ehtAPIAdapter = &ethAPIDummy{}
52 changes: 46 additions & 6 deletions app/submodule/eth/eth_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"errors"
"fmt"
"path/filepath"
"strconv"
"time"

Expand All @@ -19,6 +20,7 @@ import (
"github.com/filecoin-project/venus/pkg/constants"
"github.com/filecoin-project/venus/pkg/crypto"
"github.com/filecoin-project/venus/pkg/ethhashlookup"
"github.com/filecoin-project/venus/pkg/events"
"github.com/filecoin-project/venus/pkg/fork"
"github.com/filecoin-project/venus/pkg/vm"
"github.com/filecoin-project/venus/pkg/vm/vmcontext"
Expand All @@ -33,13 +35,24 @@ import (

var log = logging.Logger("eth_api")

func newEthAPI(em *EthSubModule) *ethAPI {
return &ethAPI{
em: em,
chain: em.chainModule.API(),
mpool: em.mpoolModule.API(),
ethTxHashManager: em.ethTxHashManager,
func newEthAPI(em *EthSubModule) (*ethAPI, error) {
a := &ethAPI{
em: em,
chain: em.chainModule.API(),
mpool: em.mpoolModule.API(),
}

transactionHashLookup, err := ethhashlookup.NewTransactionHashLookup(filepath.Join(a.em.txHashDBPath, "txhash.db"))
if err != nil {
return nil, err
}

a.ethTxHashManager = &ethTxHashManager{
chainAPI: a.chain,
TransactionHashLookup: transactionHashLookup,
}

return a, nil
}

type ethAPI struct {
Expand All @@ -49,6 +62,30 @@ type ethAPI struct {
ethTxHashManager *ethTxHashManager
}

func (a *ethAPI) start(ctx context.Context) error {
const ChainHeadConfidence = 1
ev, err := events.NewEventsWithConfidence(ctx, a.chain, ChainHeadConfidence)
if err != nil {
return err
}

// Tipset listener
_ = ev.Observe(a.ethTxHashManager)

ch, err := a.em.mpoolModule.MPool.Updates(ctx)
if err != nil {
return err
}
go waitForMpoolUpdates(ctx, ch, a.ethTxHashManager)
go ethTxHashGC(ctx, a.em.cfg.FevmConfig.EthTxHashMappingLifetimeDays, a.ethTxHashManager)

return nil
}

func (a *ethAPI) close() error {
return a.ethTxHashManager.TransactionHashLookup.Close()
}

func (a *ethAPI) StateNetworkName(ctx context.Context) (types.NetworkName, error) {
return a.chain.StateNetworkName(ctx)
}
Expand Down Expand Up @@ -1223,3 +1260,6 @@ func ethTxHashGC(ctx context.Context, retentionDays int, manager *ethTxHashManag
time.Sleep(gcPeriod)
}
}

var _ v1.IETH = &ethAPI{}
var _ ehtAPIAdapter = &ethAPI{}
59 changes: 21 additions & 38 deletions app/submodule/eth/eth_submodule.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@ package eth
import (
"context"
"fmt"
"path/filepath"

"github.com/filecoin-project/venus/app/submodule/chain"
"github.com/filecoin-project/venus/app/submodule/mpool"
"github.com/filecoin-project/venus/pkg/config"
"github.com/filecoin-project/venus/pkg/ethhashlookup"
"github.com/filecoin-project/venus/pkg/events"
v1api "github.com/filecoin-project/venus/venus-shared/api/chain/v1"
)

Expand All @@ -30,6 +27,14 @@ func NewEthSubModule(cfg *config.Config,
}
em.ethEventAPI = ee

em.ehtAdapter = &ethAPIDummy{}
if em.cfg.FevmConfig.EnableEthRPC {
em.ehtAdapter, err = newEthAPI(em)
if err != nil {
return nil, err
}
}

return em, nil
}

Expand All @@ -39,64 +44,42 @@ type EthSubModule struct { // nolint
mpoolModule *mpool.MessagePoolSubmodule
txHashDBPath string

ethEventAPI *ethEventAPI
ethTxHashManager *ethTxHashManager
ethEventAPI *ethEventAPI
ehtAdapter ehtAPIAdapter
}

func (em *EthSubModule) Start(ctx context.Context) error {
if err := em.ethEventAPI.Start(ctx); err != nil {
return err
}

transactionHashLookup, err := ethhashlookup.NewTransactionHashLookup(filepath.Join(em.txHashDBPath, "txhash.db"))
if err != nil {
return err
}

ethTxHashManager := ethTxHashManager{
chainAPI: em.chainModule.API(),
TransactionHashLookup: transactionHashLookup,
}
em.ethTxHashManager = &ethTxHashManager

const ChainHeadConfidence = 1
ev, err := events.NewEventsWithConfidence(ctx, em.ethTxHashManager.chainAPI, ChainHeadConfidence)
if err != nil {
return err
}

// Tipset listener
_ = ev.Observe(&ethTxHashManager)

ch, err := em.mpoolModule.MPool.Updates(ctx)
if err != nil {
return err
}
go waitForMpoolUpdates(ctx, ch, &ethTxHashManager)
go ethTxHashGC(ctx, em.cfg.FevmConfig.EthTxHashMappingLifetimeDays, &ethTxHashManager)

return nil

return em.ehtAdapter.start(ctx)
}

func (em *EthSubModule) Close(ctx context.Context) error {
if err := em.ethEventAPI.Close(ctx); err != nil {
return err
}

return em.ethTxHashManager.TransactionHashLookup.Close()
return em.ehtAdapter.close()
}

type ehtAPIAdapter interface {
v1api.IETH
start(ctx context.Context) error
close() error
}

type fullETHAPI struct {
*ethAPI
v1api.IETH
*ethEventAPI
}

var _ v1api.IETH = (*fullETHAPI)(nil)

func (em *EthSubModule) API() v1api.IETH {
func (em *EthSubModule) API() v1api.FullETH {
return &fullETHAPI{
ethAPI: newEthAPI(em),
IETH: em.ehtAdapter,
ethEventAPI: em.ethEventAPI,
}
}
6 changes: 3 additions & 3 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,16 +417,16 @@ func newActorEventConfig() *ActorEventConfig {
}

type FevmConfig struct {
//EnableEthPRC enables eth_rpc, and enables storing a mapping of eth transaction hashes to filecoin message Cids.
EnableEthPRC bool
//EnableEthRPC enables eth_rpc, and enables storing a mapping of eth transaction hashes to filecoin message Cids.
EnableEthRPC bool
// EthTxHashMappingLifetimeDays the transaction hash lookup database will delete mappings that have been stored for more than x days
// Set to 0 to keep all mappings
EthTxHashMappingLifetimeDays int
}

func newFevmConfig() *FevmConfig {
return &FevmConfig{
EnableEthPRC: false,
EnableEthRPC: false,
EthTxHashMappingLifetimeDays: 0,
}
}
Expand Down
7 changes: 5 additions & 2 deletions venus-shared/api/chain/v1/eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import (
"github.com/ipfs/go-cid"
)

type FullETH interface {
IETH
IETHEvent
}

type IETH interface {
// These methods are used for Ethereum-compatible JSON-RPC calls
//
Expand Down Expand Up @@ -44,8 +49,6 @@ type IETH interface {
EthCall(ctx context.Context, tx types.EthCall, blkParam string) (types.EthBytes, error) //perm:read

EthSendRawTransaction(ctx context.Context, rawTx types.EthBytes) (types.EthHash, error) //perm:read

IETHEvent
}

type IETHEvent interface {
Expand Down
2 changes: 1 addition & 1 deletion venus-shared/api/chain/v1/fullnode.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ type FullNode interface {
ISyncer
IWallet
ICommon
IETH
FullETH
}
Loading

0 comments on commit 3c77fad

Please sign in to comment.