Skip to content

Commit

Permalink
Merge pull request #5564 from filecoin-project/feat/ethrpc-block-tx
Browse files Browse the repository at this point in the history
feat: ethrpc: implement EthBlock and EthTx structs
  • Loading branch information
diwufeiwen authored Dec 14, 2022
2 parents 8b108ba + d72ea21 commit 884f067
Show file tree
Hide file tree
Showing 24 changed files with 473 additions and 138 deletions.
2 changes: 1 addition & 1 deletion app/node/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ 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 nd.eth, err = eth.NewEthSubModule(nd.chain); err != nil {
if nd.eth, err = eth.NewEthSubModule(nd.chain, nd.repo.Config().NetworkParams); err != nil {
return nil, err
}

Expand Down
3 changes: 3 additions & 0 deletions app/node/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,7 @@ func aliasETHAPI(rpcServer *jsonrpc.RPCServer) {

rpcServer.AliasMethod("net_version", "Filecoin.NetVersion")
rpcServer.AliasMethod("net_listening", "Filecoin.NetListening")

rpcServer.AliasMethod("eth_estimateGas", "Filecoin.EthEstimateGas")
rpcServer.AliasMethod("eth_call", "Filecoin.EthCall")
}
155 changes: 151 additions & 4 deletions app/submodule/eth/eth_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strconv"

"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/venus/pkg/constants"
v1 "github.com/filecoin-project/venus/venus-shared/api/chain/v1"
"github.com/filecoin-project/venus/venus-shared/types"
)
Expand Down Expand Up @@ -57,15 +58,34 @@ func (a *ethAPI) EthGetBlockTransactionCountByHash(ctx context.Context, blkHash
}

func (a *ethAPI) EthGetBlockByHash(ctx context.Context, blkHash types.EthHash, fullTxInfo bool) (types.EthBlock, error) {
return types.EthBlock{}, nil
ts, err := a.em.chainModule.ChainReader.GetTipSetByCid(ctx, blkHash.ToCid())
if err != nil {
return types.EthBlock{}, fmt.Errorf("error loading tipset %s: %w", ts, err)
}
return a.ethBlockFromFilecoinTipSet(ctx, ts, fullTxInfo)
}

func (a *ethAPI) EthGetBlockByNumber(ctx context.Context, blkNum types.EthInt, fullTxInfo bool) (types.EthBlock, error) {
return types.EthBlock{}, nil
ts, err := a.em.chainModule.ChainReader.GetTipSetByHeight(ctx, nil, abi.ChainEpoch(blkNum), false)
if err != nil {
return types.EthBlock{}, fmt.Errorf("error loading tipset %s: %w", ts, err)
}
return a.ethBlockFromFilecoinTipSet(ctx, ts, fullTxInfo)
}

func (a *ethAPI) EthGetTransactionByHash(ctx context.Context, txHash types.EthHash) (types.EthTx, error) {
return types.EthTx{}, nil
cid := txHash.ToCid()

msgLookup, err := a.chain.StateSearchMsg(ctx, types.EmptyTSK, cid, constants.LookbackNoLimit, true)
if err != nil {
return types.EthTx{}, nil
}

tx, err := a.ethTxFromFilecoinMessageLookup(ctx, msgLookup)
if err != nil {
return types.EthTx{}, err
}
return tx, nil
}

func (a *ethAPI) EthGetTransactionCount(ctx context.Context, sender types.EthAddress, blkParam string) (types.EthInt, error) {
Expand Down Expand Up @@ -108,7 +128,7 @@ func (a *ethAPI) EthGetBalance(ctx context.Context, address types.EthAddress, bl
}

func (a *ethAPI) EthChainId(ctx context.Context) (types.EthInt, error) {
return types.EthInt(0), nil
return types.EthInt(a.em.networkCfg.Eip155ChainID), nil
}

func (a *ethAPI) NetVersion(ctx context.Context) (string, error) {
Expand Down Expand Up @@ -136,4 +156,131 @@ func (a *ethAPI) EthGasPrice(ctx context.Context) (types.EthInt, error) {
return types.EthInt(0), nil
}

func (a *ethAPI) EthEstimateGas(ctx context.Context, tx types.EthCall, blkParam string) (types.EthInt, error) {
return types.EthInt(0), nil
}

func (a *ethAPI) EthCall(ctx context.Context, tx types.EthCall, blkParam string) (string, error) {
return "", nil
}

func (a *ethAPI) ethBlockFromFilecoinTipSet(ctx context.Context, ts *types.TipSet, fullTxInfo bool) (types.EthBlock, error) {
parent, err := a.chain.ChainGetTipSet(ctx, ts.Parents())
if err != nil {
return types.EthBlock{}, err
}
parentKeyCid, err := parent.Key().Cid()
if err != nil {
return types.EthBlock{}, err
}
parentBlkHash, err := types.EthHashFromCid(parentKeyCid)
if err != nil {
return types.EthBlock{}, err
}

// blkMsgs, err := a.Chain.BlockMsgsForTipset(ctx, ts)
// if err != nil {
// return types.EthBlock{}, fmt.Errorf("error loading messages for tipset: %v: %w", ts, err)
// }
msgs, err := a.em.chainModule.MessageStore.MessagesForTipset(ts)
if err != nil {
return types.EthBlock{}, fmt.Errorf("error loading messages for tipset: %v: %w", ts, err)
}

block := types.NewEthBlock()

// this seems to be a very expensive way to get gasUsed of the block. may need to find an efficient way to do it
gasUsed := int64(0)
for _, msg := range msgs {
msgLookup, err := a.chain.StateSearchMsg(ctx, types.EmptyTSK, msg.Cid(), constants.LookbackNoLimit, true)
if err != nil {
return types.EthBlock{}, nil
}
gasUsed += msgLookup.Receipt.GasUsed

if fullTxInfo {
tx, err := a.ethTxFromFilecoinMessageLookup(ctx, msgLookup)
if err != nil {
return types.EthBlock{}, nil
}
block.Transactions = append(block.Transactions, tx)
} else {
hash, err := types.EthHashFromCid(msg.Cid())
if err != nil {
return types.EthBlock{}, err
}
block.Transactions = append(block.Transactions, hash.String())
}
}

block.Number = types.EthInt(ts.Height())
block.ParentHash = parentBlkHash
block.Timestamp = types.EthInt(ts.Blocks()[0].Timestamp)
block.BaseFeePerGas = types.EthBigInt{Int: ts.Blocks()[0].ParentBaseFee.Int}
block.GasUsed = types.EthInt(gasUsed)
return block, nil
}

func (a *ethAPI) ethTxFromFilecoinMessageLookup(ctx context.Context, msgLookup *types.MsgLookup) (types.EthTx, error) {
cid := msgLookup.Message
txHash, err := types.EthHashFromCid(cid)
if err != nil {
return types.EthTx{}, err
}

tsCid, err := msgLookup.TipSet.Cid()
if err != nil {
return types.EthTx{}, err
}

blkHash, err := types.EthHashFromCid(tsCid)
if err != nil {
return types.EthTx{}, err
}

msg, err := a.chain.ChainGetMessage(ctx, msgLookup.Message)
if err != nil {
return types.EthTx{}, err
}

fromFilIDAddr, err := a.chain.StateLookupID(ctx, msg.From, types.EmptyTSK)
if err != nil {
return types.EthTx{}, err
}

fromEthAddr, err := types.EthAddressFromFilecoinIDAddress(fromFilIDAddr)
if err != nil {
return types.EthTx{}, err
}

toFilAddr, err := a.chain.StateLookupID(ctx, msg.From, types.EmptyTSK)
if err != nil {
return types.EthTx{}, err
}

toEthAddr, err := types.EthAddressFromFilecoinIDAddress(toFilAddr)
if err != nil {
return types.EthTx{}, err
}

tx := types.EthTx{
ChainID: types.EthInt(a.em.networkCfg.Eip155ChainID),
Hash: txHash,
BlockHash: blkHash,
BlockNumber: types.EthInt(msgLookup.Height),
From: fromEthAddr,
To: toEthAddr,
Value: types.EthBigInt(msg.Value),
Type: types.EthInt(2),
Gas: types.EthInt(msg.GasLimit),
MaxFeePerGas: types.EthBigInt(msg.GasFeeCap),
MaxPriorityFeePerGas: types.EthBigInt(msg.GasPremium),
V: types.EthBigIntZero,
R: types.EthBigIntZero,
S: types.EthBigIntZero,
// TODO: Input:
}
return tx, nil
}

var _ v1.IETH = (*ethAPI)(nil)
5 changes: 4 additions & 1 deletion app/submodule/eth/eth_submodule.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@ package eth

import (
"github.com/filecoin-project/venus/app/submodule/chain"
"github.com/filecoin-project/venus/pkg/config"
v1api "github.com/filecoin-project/venus/venus-shared/api/chain/v1"
)

func NewEthSubModule(chainModule *chain.ChainSubmodule) (*EthSubModule, error) {
func NewEthSubModule(chainModule *chain.ChainSubmodule, networkCfg *config.NetworkParamsConfig) (*EthSubModule, error) {
return &EthSubModule{
chainModule: chainModule,
networkCfg: networkCfg,
}, nil
}

type EthSubModule struct { // nolint
chainModule *chain.ChainSubmodule
networkCfg *config.NetworkParamsConfig
}

func (em *EthSubModule) API() v1api.IETH {
Expand Down
1 change: 1 addition & 0 deletions fixtures/networks/butterfly.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func ButterflySnapNet() *NetworkConf {
DrandSchedule: map[abi.ChainEpoch]config.DrandEnum{0: 1},
AddressNetwork: address.Testnet,
PropagationDelaySecs: 6,
Eip155ChainID: 3141592,
},
}
}
1 change: 1 addition & 0 deletions fixtures/networks/calibration.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func Calibration() *NetworkConf {
DrandSchedule: map[abi.ChainEpoch]config.DrandEnum{0: 1},
AddressNetwork: address.Testnet,
PropagationDelaySecs: 10,
Eip155ChainID: 314159,
},
}
}
1 change: 1 addition & 0 deletions fixtures/networks/forcenet.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func ForceNet() *NetworkConf {
DrandSchedule: map[abi.ChainEpoch]config.DrandEnum{0: config.DrandMainnet},
AddressNetwork: address.Testnet,
PropagationDelaySecs: 1,
Eip155ChainID: 31415926,
},
}
}
1 change: 1 addition & 0 deletions fixtures/networks/integrationtestnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func IntegrationNet() *NetworkConf {
DrandSchedule: map[abi.ChainEpoch]config.DrandEnum{0: 5, 51000: 1},
AddressNetwork: address.Testnet,
PropagationDelaySecs: 10,
Eip155ChainID: 314,
},
}
}
1 change: 1 addition & 0 deletions fixtures/networks/interopnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func InteropNet() *NetworkConf {
DrandSchedule: map[abi.ChainEpoch]config.DrandEnum{0: 1},
AddressNetwork: address.Testnet,
PropagationDelaySecs: 6,
Eip155ChainID: 3141592,
},
}
}
1 change: 1 addition & 0 deletions fixtures/networks/mainnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ func Mainnet() *NetworkConf {
DrandSchedule: map[abi.ChainEpoch]config.DrandEnum{0: 5, 51000: 1},
AddressNetwork: address.Mainnet,
PropagationDelaySecs: 10,
Eip155ChainID: 314,
},
}
}
1 change: 1 addition & 0 deletions fixtures/networks/net_2k.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func Net2k() *NetworkConf {
DrandSchedule: map[abi.ChainEpoch]config.DrandEnum{0: 1},
AddressNetwork: address.Testnet,
PropagationDelaySecs: 1,
Eip155ChainID: 31415926,
},
}
}
4 changes: 4 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,9 @@ type NetworkParamsConfig struct {
ForkUpgradeParam *ForkUpgradeConfig `json:"-"`
PreCommitChallengeDelay abi.ChainEpoch `json:"-"`
PropagationDelaySecs uint64 `json:"-"`
// ChainId defines the chain ID used in the Ethereum JSON-RPC endpoint.
// As per https://github.com/ethereum-lists/chains
Eip155ChainID uint64 `json:"-"`
}

// ForkUpgradeConfig record upgrade parameters
Expand Down Expand Up @@ -332,6 +335,7 @@ func newDefaultNetworkParamsConfig() *NetworkParamsConfig {
DrandSchedule: map[abi.ChainEpoch]DrandEnum{0: 5, -1: 1},
ForkUpgradeParam: &defaultParams,
PropagationDelaySecs: 10,
Eip155ChainID: 314,
}
}

Expand Down
4 changes: 1 addition & 3 deletions venus-devtool/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/filecoin-project/go-fil-markets v1.25.2
github.com/filecoin-project/go-jsonrpc v0.1.9
github.com/filecoin-project/go-state-types v0.10.0-alpha-2
github.com/filecoin-project/lotus v1.18.2-0.20221208080513-0026ad8c09be
github.com/filecoin-project/lotus v1.18.0
github.com/filecoin-project/venus v0.0.0-00010101000000-000000000000
github.com/google/uuid v1.3.0
github.com/ipfs/go-block-format v0.0.3
Expand Down Expand Up @@ -61,7 +61,6 @@ require (
github.com/filecoin-project/go-cbor-util v0.0.1 // indirect
github.com/filecoin-project/go-commp-utils v0.1.3 // indirect
github.com/filecoin-project/go-commp-utils/nonffi v0.0.0-20220905160352-62059082a837 // indirect
github.com/filecoin-project/go-crypto v0.0.1 // indirect
github.com/filecoin-project/go-fil-commcid v0.1.0 // indirect
github.com/filecoin-project/go-hamt-ipld v0.1.5 // indirect
github.com/filecoin-project/go-hamt-ipld/v2 v2.0.0 // indirect
Expand Down Expand Up @@ -128,7 +127,6 @@ require (
github.com/ipld/go-car/v2 v2.5.0 // indirect
github.com/ipld/go-codec-dagpb v1.5.0 // indirect
github.com/ipld/go-ipld-prime v0.18.0 // indirect
github.com/ipsn/go-secp256k1 v0.0.0-20180726113642-9d62b9f0bc52 // indirect
github.com/jbenet/goprocess v0.1.4 // indirect
github.com/jessevdk/go-flags v1.4.0 // indirect
github.com/jpillora/backoff v1.0.0 // indirect
Expand Down
6 changes: 3 additions & 3 deletions venus-devtool/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -338,8 +338,8 @@ github.com/filecoin-project/go-statemachine v1.0.2 h1:421SSWBk8GIoCoWYYTE/d+qCWc
github.com/filecoin-project/go-statestore v0.2.0 h1:cRRO0aPLrxKQCZ2UOQbzFGn4WDNdofHZoGPjfNaAo5Q=
github.com/filecoin-project/go-statestore v0.2.0/go.mod h1:8sjBYbS35HwPzct7iT4lIXjLlYyPor80aU7t7a/Kspo=
github.com/filecoin-project/index-provider v0.9.1 h1:Jnh9dviIHvQxZ2baNoYu3n8z6F9O62ksnVlyREgPyyM=
github.com/filecoin-project/lotus v1.18.2-0.20221208080513-0026ad8c09be h1:RLpPeCMqxkBheRcsS12u+DCZ0//XKuWzvPQZcX4Cr88=
github.com/filecoin-project/lotus v1.18.2-0.20221208080513-0026ad8c09be/go.mod h1:z74AZBdJsi90u12Ue5v6LubEudCHLsUexlDpDB4k2dg=
github.com/filecoin-project/lotus v1.18.0 h1:HxdShHMEZT703n9KlQTgPVoUF/ocidMC/d3TzwxzTP8=
github.com/filecoin-project/lotus v1.18.0/go.mod h1:jJih5ApnJZssc/wWsLJm+IWnfy8YaCyaDbvs/wTIVDk=
github.com/filecoin-project/pubsub v1.0.0 h1:ZTmT27U07e54qV1mMiQo4HDr0buo8I1LDHBYLXlsNXM=
github.com/filecoin-project/pubsub v1.0.0/go.mod h1:GkpB33CcUtUNrLPhJgfdy4FDx4OMNR9k+46DHx/Lqrg=
github.com/filecoin-project/specs-actors v0.9.13/go.mod h1:TS1AW/7LbG+615j4NsjMK1qlpAwaFsG9w0V2tg2gSao=
Expand Down Expand Up @@ -1700,7 +1700,7 @@ go.opentelemetry.io/otel/metric v0.20.0/go.mod h1:598I5tYlH1vzBjn+BTuhzTCSb/9deb
go.opentelemetry.io/otel/oteltest v0.20.0/go.mod h1:L7bgKf9ZB7qCwT9Up7i9/pn0PWIa9FqQ2IQ8LoxiGnw=
go.opentelemetry.io/otel/sdk v0.20.0/go.mod h1:g/IcepuwNsoiX5Byy2nNV0ySUF1em498m7hBWC279Yc=
go.opentelemetry.io/otel/sdk v1.2.0/go.mod h1:jNN8QtpvbsKhgaC6V5lHiejMoKD+V8uadoSafgHPx1U=
go.opentelemetry.io/otel/sdk v1.11.1 h1:F7KmQgoHljhUuJyA+9BiU+EkJfyX5nVVF4wyzWZpKxs=
go.opentelemetry.io/otel/sdk v1.10.0 h1:jZ6K7sVn04kk/3DNUdJ4mqRlGDiXAVuIG+MMENpTNdY=
go.opentelemetry.io/otel/trace v0.20.0/go.mod h1:6GjCW8zgDjwGHGa6GkyeB8+/5vjT16gUEi0Nf1iBdgw=
go.opentelemetry.io/otel/trace v1.2.0/go.mod h1:N5FLswTubnxKxOJHM7XZC074qpeEdLy3CgAVsdMucK0=
go.opentelemetry.io/otel/trace v1.11.1 h1:ofxdnzsNrGBYXbP7t7zpUK281+go5rF7dvdIZXF8gdQ=
Expand Down
4 changes: 3 additions & 1 deletion venus-shared/api/chain/v1/eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ type IETH interface {
NetVersion(ctx context.Context) (string, error) //perm:read
NetListening(ctx context.Context) (bool, error) //perm:read
EthProtocolVersion(ctx context.Context) (types.EthInt, error) //perm:read
EthMaxPriorityFeePerGas(ctx context.Context) (types.EthInt, error) //perm:read
EthGasPrice(ctx context.Context) (types.EthInt, error) //perm:read
EthMaxPriorityFeePerGas(ctx context.Context) (types.EthInt, error) //perm:read
EthEstimateGas(ctx context.Context, tx types.EthCall, blkParam string) (types.EthInt, error) //perm:read
EthCall(ctx context.Context, tx types.EthCall, blkParam string) (string, error) //perm:read
}
Loading

0 comments on commit 884f067

Please sign in to comment.