2022-09-09 17:59:00 +00:00
|
|
|
package full
|
|
|
|
|
|
|
|
import (
|
2022-09-29 22:55:13 +00:00
|
|
|
"bytes"
|
2022-09-09 17:59:00 +00:00
|
|
|
"context"
|
2022-11-10 11:27:58 +00:00
|
|
|
"errors"
|
2022-09-29 20:46:59 +00:00
|
|
|
"fmt"
|
2022-09-09 17:59:00 +00:00
|
|
|
"strconv"
|
2022-11-10 11:27:58 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
2022-09-09 17:59:00 +00:00
|
|
|
|
2022-11-10 11:27:58 +00:00
|
|
|
"github.com/google/uuid"
|
2022-09-29 22:55:13 +00:00
|
|
|
"github.com/ipfs/go-cid"
|
|
|
|
cbg "github.com/whyrusleeping/cbor-gen"
|
2022-09-09 17:59:00 +00:00
|
|
|
"go.uber.org/fx"
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
2022-09-29 20:46:59 +00:00
|
|
|
"github.com/filecoin-project/go-address"
|
2022-09-09 17:59:00 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
2022-09-28 14:58:24 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/big"
|
2022-09-29 20:46:59 +00:00
|
|
|
builtintypes "github.com/filecoin-project/go-state-types/builtin"
|
2022-11-09 17:54:07 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/builtin/v10/eam"
|
|
|
|
"github.com/filecoin-project/go-state-types/builtin/v10/evm"
|
2022-09-09 17:59:00 +00:00
|
|
|
|
|
|
|
"github.com/filecoin-project/lotus/api"
|
2022-09-12 21:46:15 +00:00
|
|
|
"github.com/filecoin-project/lotus/build"
|
2022-09-29 20:46:59 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/actors"
|
2022-11-10 11:27:58 +00:00
|
|
|
builtinactors "github.com/filecoin-project/lotus/chain/actors/builtin"
|
|
|
|
"github.com/filecoin-project/lotus/chain/events/filter"
|
2022-09-28 14:58:24 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/messagepool"
|
|
|
|
"github.com/filecoin-project/lotus/chain/stmgr"
|
2022-09-09 17:59:00 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/store"
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
2022-09-28 14:58:24 +00:00
|
|
|
"github.com/filecoin-project/lotus/node/modules/dtypes"
|
2022-09-09 17:59:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type EthModuleAPI interface {
|
2022-09-29 22:55:13 +00:00
|
|
|
EthBlockNumber(ctx context.Context) (api.EthUint64, error)
|
2022-09-09 17:59:00 +00:00
|
|
|
EthAccounts(ctx context.Context) ([]api.EthAddress, error)
|
2022-09-29 22:55:13 +00:00
|
|
|
EthGetBlockTransactionCountByNumber(ctx context.Context, blkNum api.EthUint64) (api.EthUint64, error)
|
|
|
|
EthGetBlockTransactionCountByHash(ctx context.Context, blkHash api.EthHash) (api.EthUint64, error)
|
2022-09-09 17:59:00 +00:00
|
|
|
EthGetBlockByHash(ctx context.Context, blkHash api.EthHash, fullTxInfo bool) (api.EthBlock, error)
|
2022-09-29 20:46:59 +00:00
|
|
|
EthGetBlockByNumber(ctx context.Context, blkNum string, fullTxInfo bool) (api.EthBlock, error)
|
|
|
|
EthGetTransactionByHash(ctx context.Context, txHash *api.EthHash) (*api.EthTx, error)
|
2022-09-29 22:55:13 +00:00
|
|
|
EthGetTransactionCount(ctx context.Context, sender api.EthAddress, blkOpt string) (api.EthUint64, error)
|
2022-09-29 20:46:59 +00:00
|
|
|
EthGetTransactionReceipt(ctx context.Context, txHash api.EthHash) (*api.EthTxReceipt, error)
|
2022-09-29 22:55:13 +00:00
|
|
|
EthGetTransactionByBlockHashAndIndex(ctx context.Context, blkHash api.EthHash, txIndex api.EthUint64) (api.EthTx, error)
|
|
|
|
EthGetTransactionByBlockNumberAndIndex(ctx context.Context, blkNum api.EthUint64, txIndex api.EthUint64) (api.EthTx, error)
|
2022-10-21 11:15:24 +00:00
|
|
|
EthGetCode(ctx context.Context, address api.EthAddress, blkOpt string) (api.EthBytes, error)
|
2022-09-29 22:55:13 +00:00
|
|
|
EthGetStorageAt(ctx context.Context, address api.EthAddress, position api.EthBytes, blkParam string) (api.EthBytes, error)
|
2022-09-09 17:59:00 +00:00
|
|
|
EthGetBalance(ctx context.Context, address api.EthAddress, blkParam string) (api.EthBigInt, error)
|
2022-10-22 19:12:54 +00:00
|
|
|
EthFeeHistory(ctx context.Context, blkCount api.EthUint64, newestBlk string, rewardPercentiles []int64) (api.EthFeeHistory, error)
|
2022-09-29 22:55:13 +00:00
|
|
|
EthChainId(ctx context.Context) (api.EthUint64, error)
|
2022-09-09 17:59:00 +00:00
|
|
|
NetVersion(ctx context.Context) (string, error)
|
|
|
|
NetListening(ctx context.Context) (bool, error)
|
2022-09-29 22:55:13 +00:00
|
|
|
EthProtocolVersion(ctx context.Context) (api.EthUint64, error)
|
2022-09-28 14:58:24 +00:00
|
|
|
EthGasPrice(ctx context.Context) (api.EthBigInt, error)
|
2022-09-29 22:55:13 +00:00
|
|
|
EthEstimateGas(ctx context.Context, tx api.EthCall) (api.EthUint64, error)
|
2022-09-28 14:58:24 +00:00
|
|
|
EthCall(ctx context.Context, tx api.EthCall, blkParam string) (api.EthBytes, error)
|
|
|
|
EthMaxPriorityFeePerGas(ctx context.Context) (api.EthBigInt, error)
|
|
|
|
EthSendRawTransaction(ctx context.Context, rawTx api.EthBytes) (api.EthHash, error)
|
2022-09-09 17:59:00 +00:00
|
|
|
}
|
|
|
|
|
2022-11-10 11:27:58 +00:00
|
|
|
type EthEventAPI interface {
|
|
|
|
EthGetLogs(ctx context.Context, filter *api.EthFilterSpec) (*api.EthFilterResult, error)
|
|
|
|
EthGetFilterChanges(ctx context.Context, id api.EthFilterID) (*api.EthFilterResult, error)
|
|
|
|
EthGetFilterLogs(ctx context.Context, id api.EthFilterID) (*api.EthFilterResult, error)
|
|
|
|
EthNewFilter(ctx context.Context, filter *api.EthFilterSpec) (api.EthFilterID, error)
|
|
|
|
EthNewBlockFilter(ctx context.Context) (api.EthFilterID, error)
|
|
|
|
EthNewPendingTransactionFilter(ctx context.Context) (api.EthFilterID, error)
|
|
|
|
EthUninstallFilter(ctx context.Context, id api.EthFilterID) (bool, error)
|
2022-11-16 12:16:19 +00:00
|
|
|
EthSubscribe(ctx context.Context, eventType string, params *api.EthSubscriptionParams) (<-chan api.EthSubscriptionResponse, error)
|
2022-11-10 11:27:58 +00:00
|
|
|
EthUnsubscribe(ctx context.Context, id api.EthSubscriptionID) (bool, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
_ EthModuleAPI = *new(api.FullNode)
|
|
|
|
_ EthEventAPI = *new(api.FullNode)
|
|
|
|
)
|
2022-09-09 17:59:00 +00:00
|
|
|
|
|
|
|
// EthModule provides a default implementation of EthModuleAPI.
|
|
|
|
// It can be swapped out with another implementation through Dependency
|
|
|
|
// Injection (for example with a thin RPC client).
|
|
|
|
type EthModule struct {
|
|
|
|
fx.In
|
|
|
|
|
2022-09-28 14:58:24 +00:00
|
|
|
Chain *store.ChainStore
|
|
|
|
Mpool *messagepool.MessagePool
|
|
|
|
StateManager *stmgr.StateManager
|
2022-09-12 21:46:15 +00:00
|
|
|
|
|
|
|
ChainAPI
|
2022-09-28 14:58:24 +00:00
|
|
|
MpoolAPI
|
2022-09-09 17:59:00 +00:00
|
|
|
StateAPI
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ EthModuleAPI = (*EthModule)(nil)
|
|
|
|
|
2022-11-10 11:27:58 +00:00
|
|
|
type EthEvent struct {
|
|
|
|
Chain *store.ChainStore
|
|
|
|
EventFilterManager *filter.EventFilterManager
|
|
|
|
TipSetFilterManager *filter.TipSetFilterManager
|
|
|
|
MemPoolFilterManager *filter.MemPoolFilterManager
|
|
|
|
FilterStore filter.FilterStore
|
2022-11-16 12:57:03 +00:00
|
|
|
SubManager *EthSubscriptionManager
|
2022-11-10 11:27:58 +00:00
|
|
|
MaxFilterHeightRange abi.ChainEpoch
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ EthEventAPI = (*EthEvent)(nil)
|
|
|
|
|
2022-09-09 17:59:00 +00:00
|
|
|
type EthAPI struct {
|
|
|
|
fx.In
|
|
|
|
|
|
|
|
Chain *store.ChainStore
|
|
|
|
|
|
|
|
EthModuleAPI
|
2022-11-10 11:27:58 +00:00
|
|
|
EthEventAPI
|
2022-09-09 17:59:00 +00:00
|
|
|
}
|
|
|
|
|
2022-09-28 14:58:24 +00:00
|
|
|
func (a *EthModule) StateNetworkName(ctx context.Context) (dtypes.NetworkName, error) {
|
|
|
|
return stmgr.GetNetworkName(ctx, a.StateManager, a.Chain.GetHeaviestTipSet().ParentState())
|
|
|
|
}
|
|
|
|
|
2022-09-29 22:55:13 +00:00
|
|
|
func (a *EthModule) EthBlockNumber(context.Context) (api.EthUint64, error) {
|
2022-09-09 17:59:00 +00:00
|
|
|
height := a.Chain.GetHeaviestTipSet().Height()
|
2022-09-29 22:55:13 +00:00
|
|
|
return api.EthUint64(height), nil
|
2022-09-09 17:59:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *EthModule) EthAccounts(context.Context) ([]api.EthAddress, error) {
|
|
|
|
// The lotus node is not expected to hold manage accounts, so we'll always return an empty array
|
|
|
|
return []api.EthAddress{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *EthModule) countTipsetMsgs(ctx context.Context, ts *types.TipSet) (int, error) {
|
|
|
|
blkMsgs, err := a.Chain.BlockMsgsForTipset(ctx, ts)
|
|
|
|
if err != nil {
|
|
|
|
return 0, xerrors.Errorf("error loading messages for tipset: %v: %w", ts, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
count := 0
|
|
|
|
for _, blkMsg := range blkMsgs {
|
|
|
|
// TODO: may need to run canonical ordering and deduplication here
|
|
|
|
count += len(blkMsg.BlsMessages) + len(blkMsg.SecpkMessages)
|
|
|
|
}
|
|
|
|
return count, nil
|
|
|
|
}
|
|
|
|
|
2022-09-29 22:55:13 +00:00
|
|
|
func (a *EthModule) EthGetBlockTransactionCountByNumber(ctx context.Context, blkNum api.EthUint64) (api.EthUint64, error) {
|
2022-09-09 17:59:00 +00:00
|
|
|
ts, err := a.Chain.GetTipsetByHeight(ctx, abi.ChainEpoch(blkNum), nil, false)
|
|
|
|
if err != nil {
|
2022-09-29 22:55:13 +00:00
|
|
|
return api.EthUint64(0), xerrors.Errorf("error loading tipset %s: %w", ts, err)
|
2022-09-09 17:59:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
count, err := a.countTipsetMsgs(ctx, ts)
|
2022-09-29 22:55:13 +00:00
|
|
|
return api.EthUint64(count), err
|
2022-09-09 17:59:00 +00:00
|
|
|
}
|
|
|
|
|
2022-09-29 22:55:13 +00:00
|
|
|
func (a *EthModule) EthGetBlockTransactionCountByHash(ctx context.Context, blkHash api.EthHash) (api.EthUint64, error) {
|
2022-09-09 17:59:00 +00:00
|
|
|
ts, err := a.Chain.GetTipSetByCid(ctx, blkHash.ToCid())
|
|
|
|
if err != nil {
|
2022-09-29 22:55:13 +00:00
|
|
|
return api.EthUint64(0), xerrors.Errorf("error loading tipset %s: %w", ts, err)
|
2022-09-09 17:59:00 +00:00
|
|
|
}
|
|
|
|
count, err := a.countTipsetMsgs(ctx, ts)
|
2022-09-29 22:55:13 +00:00
|
|
|
return api.EthUint64(count), err
|
2022-09-09 17:59:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *EthModule) EthGetBlockByHash(ctx context.Context, blkHash api.EthHash, fullTxInfo bool) (api.EthBlock, error) {
|
2022-09-12 21:46:15 +00:00
|
|
|
ts, err := a.Chain.GetTipSetByCid(ctx, blkHash.ToCid())
|
|
|
|
if err != nil {
|
|
|
|
return api.EthBlock{}, xerrors.Errorf("error loading tipset %s: %w", ts, err)
|
|
|
|
}
|
2022-11-16 20:26:57 +00:00
|
|
|
return newEthBlockFromFilecoinTipSet(ctx, ts, fullTxInfo, a.Chain, a.ChainAPI, a.StateAPI)
|
2022-09-09 17:59:00 +00:00
|
|
|
}
|
|
|
|
|
2022-09-29 20:46:59 +00:00
|
|
|
func (a *EthModule) EthGetBlockByNumber(ctx context.Context, blkNum string, fullTxInfo bool) (api.EthBlock, error) {
|
2022-09-29 22:55:13 +00:00
|
|
|
var num api.EthUint64
|
2022-09-29 20:46:59 +00:00
|
|
|
err := num.UnmarshalJSON([]byte(`"` + blkNum + `"`))
|
|
|
|
if err != nil {
|
2022-09-29 22:55:13 +00:00
|
|
|
num = api.EthUint64(a.Chain.GetHeaviestTipSet().Height())
|
2022-09-29 20:46:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ts, err := a.Chain.GetTipsetByHeight(ctx, abi.ChainEpoch(num), nil, false)
|
2022-09-12 21:46:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return api.EthBlock{}, xerrors.Errorf("error loading tipset %s: %w", ts, err)
|
|
|
|
}
|
2022-11-16 20:26:57 +00:00
|
|
|
return newEthBlockFromFilecoinTipSet(ctx, ts, fullTxInfo, a.Chain, a.ChainAPI, a.StateAPI)
|
2022-09-09 17:59:00 +00:00
|
|
|
}
|
|
|
|
|
2022-09-29 20:46:59 +00:00
|
|
|
func (a *EthModule) EthGetTransactionByHash(ctx context.Context, txHash *api.EthHash) (*api.EthTx, error) {
|
|
|
|
// Ethereum's behavior is to return null when the txHash is invalid, so we use nil to check if txHash is valid
|
|
|
|
if txHash == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2022-09-12 21:46:15 +00:00
|
|
|
cid := txHash.ToCid()
|
|
|
|
|
|
|
|
msgLookup, err := a.StateAPI.StateSearchMsg(ctx, types.EmptyTSK, cid, api.LookbackNoLimit, true)
|
|
|
|
if err != nil {
|
2022-09-29 20:46:59 +00:00
|
|
|
return nil, nil
|
2022-09-12 21:46:15 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
tx, err := newEthTxFromFilecoinMessageLookup(ctx, msgLookup, a.Chain, a.ChainAPI, a.StateAPI)
|
2022-09-12 21:46:15 +00:00
|
|
|
if err != nil {
|
2022-09-29 20:46:59 +00:00
|
|
|
return nil, nil
|
2022-09-12 21:46:15 +00:00
|
|
|
}
|
2022-09-29 20:46:59 +00:00
|
|
|
return &tx, nil
|
2022-09-09 17:59:00 +00:00
|
|
|
}
|
|
|
|
|
2022-09-29 22:55:13 +00:00
|
|
|
func (a *EthModule) EthGetTransactionCount(ctx context.Context, sender api.EthAddress, blkParam string) (api.EthUint64, error) {
|
2022-09-28 14:58:24 +00:00
|
|
|
addr, err := sender.ToFilecoinAddress()
|
|
|
|
if err != nil {
|
2022-10-05 16:32:08 +00:00
|
|
|
return api.EthUint64(0), nil
|
2022-09-28 14:58:24 +00:00
|
|
|
}
|
|
|
|
nonce, err := a.Mpool.GetNonce(ctx, addr, types.EmptyTSK)
|
|
|
|
if err != nil {
|
2022-10-05 16:32:08 +00:00
|
|
|
return api.EthUint64(0), nil
|
2022-09-28 14:58:24 +00:00
|
|
|
}
|
2022-09-29 22:55:13 +00:00
|
|
|
return api.EthUint64(nonce), nil
|
2022-09-09 17:59:00 +00:00
|
|
|
}
|
|
|
|
|
2022-09-29 20:46:59 +00:00
|
|
|
func (a *EthModule) EthGetTransactionReceipt(ctx context.Context, txHash api.EthHash) (*api.EthTxReceipt, error) {
|
2022-09-28 14:58:24 +00:00
|
|
|
cid := txHash.ToCid()
|
|
|
|
|
|
|
|
msgLookup, err := a.StateAPI.StateSearchMsg(ctx, types.EmptyTSK, cid, api.LookbackNoLimit, true)
|
|
|
|
if err != nil {
|
2022-10-21 11:15:24 +00:00
|
|
|
return nil, nil
|
2022-09-28 14:58:24 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
tx, err := newEthTxFromFilecoinMessageLookup(ctx, msgLookup, a.Chain, a.ChainAPI, a.StateAPI)
|
2022-09-28 14:58:24 +00:00
|
|
|
if err != nil {
|
2022-10-21 11:15:24 +00:00
|
|
|
return nil, nil
|
2022-09-28 14:58:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
replay, err := a.StateAPI.StateReplay(ctx, types.EmptyTSK, cid)
|
|
|
|
if err != nil {
|
2022-10-21 11:15:24 +00:00
|
|
|
return nil, nil
|
2022-09-28 14:58:24 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 13:53:27 +00:00
|
|
|
var events []types.Event
|
|
|
|
if rct := replay.MsgRct; rct != nil && rct.EventsRoot != nil {
|
|
|
|
events, err = a.ChainAPI.ChainGetEvents(ctx, *rct.EventsRoot)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
receipt, err := newEthTxReceipt(ctx, tx, msgLookup, replay, events, a.StateAPI)
|
2022-09-28 14:58:24 +00:00
|
|
|
if err != nil {
|
2022-10-21 11:15:24 +00:00
|
|
|
return nil, nil
|
2022-09-28 14:58:24 +00:00
|
|
|
}
|
2022-11-16 14:15:29 +00:00
|
|
|
|
2022-09-29 20:46:59 +00:00
|
|
|
return &receipt, nil
|
2022-09-09 17:59:00 +00:00
|
|
|
}
|
|
|
|
|
2022-09-29 22:55:13 +00:00
|
|
|
func (a *EthModule) EthGetTransactionByBlockHashAndIndex(ctx context.Context, blkHash api.EthHash, txIndex api.EthUint64) (api.EthTx, error) {
|
2022-09-09 17:59:00 +00:00
|
|
|
return api.EthTx{}, nil
|
|
|
|
}
|
|
|
|
|
2022-09-29 22:55:13 +00:00
|
|
|
func (a *EthModule) EthGetTransactionByBlockNumberAndIndex(ctx context.Context, blkNum api.EthUint64, txIndex api.EthUint64) (api.EthTx, error) {
|
2022-09-09 17:59:00 +00:00
|
|
|
return api.EthTx{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// EthGetCode returns string value of the compiled bytecode
|
2022-10-21 11:15:24 +00:00
|
|
|
func (a *EthModule) EthGetCode(ctx context.Context, ethAddr api.EthAddress, blkOpt string) (api.EthBytes, error) {
|
2022-09-29 22:55:13 +00:00
|
|
|
to, err := ethAddr.ToFilecoinAddress()
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("cannot get Filecoin address: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// use the system actor as the caller
|
|
|
|
from, err := address.NewIDAddress(0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to construct system sender address: %w", err)
|
|
|
|
}
|
|
|
|
msg := &types.Message{
|
|
|
|
From: from,
|
|
|
|
To: to,
|
|
|
|
Value: big.Zero(),
|
2022-10-21 10:59:09 +00:00
|
|
|
Method: builtintypes.MethodsEVM.GetBytecode,
|
2022-09-29 22:55:13 +00:00
|
|
|
Params: nil,
|
|
|
|
GasLimit: build.BlockGasLimit,
|
|
|
|
GasFeeCap: big.Zero(),
|
|
|
|
GasPremium: big.Zero(),
|
|
|
|
}
|
|
|
|
|
|
|
|
ts := a.Chain.GetHeaviestTipSet()
|
|
|
|
|
|
|
|
// Try calling until we find a height with no migration.
|
|
|
|
var res *api.InvocResult
|
|
|
|
for {
|
|
|
|
res, err = a.StateManager.Call(ctx, msg, ts)
|
|
|
|
if err != stmgr.ErrExpensiveFork {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
ts, err = a.Chain.GetTipSetFromKey(ctx, ts.Parents())
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("getting parent tipset: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2022-10-22 17:53:59 +00:00
|
|
|
// if the call resulted in error, this is not an EVM smart contract;
|
|
|
|
// return no bytecode.
|
|
|
|
return nil, nil
|
2022-09-29 22:55:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if res.MsgRct == nil {
|
|
|
|
return nil, fmt.Errorf("no message receipt")
|
|
|
|
}
|
|
|
|
|
|
|
|
if res.MsgRct.ExitCode.IsError() {
|
|
|
|
return nil, xerrors.Errorf("message execution failed: exit %s, reason: %s", res.MsgRct.ExitCode, res.Error)
|
|
|
|
}
|
|
|
|
|
|
|
|
var bytecodeCid cbg.CborCid
|
|
|
|
if err := bytecodeCid.UnmarshalCBOR(bytes.NewReader(res.MsgRct.Return)); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to decode EVM bytecode CID: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
blk, err := a.Chain.StateBlockstore().Get(ctx, cid.Cid(bytecodeCid))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to get EVM bytecode: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return blk.RawData(), nil
|
2022-09-09 17:59:00 +00:00
|
|
|
}
|
|
|
|
|
2022-09-29 22:55:13 +00:00
|
|
|
func (a *EthModule) EthGetStorageAt(ctx context.Context, ethAddr api.EthAddress, position api.EthBytes, blkParam string) (api.EthBytes, error) {
|
|
|
|
l := len(position)
|
|
|
|
if l > 32 {
|
|
|
|
return nil, fmt.Errorf("supplied storage key is too long")
|
|
|
|
}
|
|
|
|
|
|
|
|
// pad with zero bytes if smaller than 32 bytes
|
|
|
|
position = append(make([]byte, 32-l, 32-l), position...)
|
|
|
|
|
|
|
|
to, err := ethAddr.ToFilecoinAddress()
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("cannot get Filecoin address: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// use the system actor as the caller
|
|
|
|
from, err := address.NewIDAddress(0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to construct system sender address: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO super duper hack (raulk). The EVM runtime actor uses the U256 parameter type in
|
|
|
|
// GetStorageAtParams, which serializes as a hex-encoded string. It should serialize
|
|
|
|
// as bytes. We didn't get to fix in time for Iron, so for now we just pass
|
|
|
|
// through the hex-encoded value passed through the Eth JSON-RPC API, by remarshalling it.
|
|
|
|
// We don't fix this at origin (builtin-actors) because we are not updating the bundle
|
|
|
|
// for Iron.
|
|
|
|
tmp, err := position.MarshalJSON()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
params, err := actors.SerializeParams(&evm.GetStorageAtParams{
|
|
|
|
StorageKey: tmp[1 : len(tmp)-1], // TODO strip the JSON-encoding quotes -- yuck
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to serialize parameters: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
msg := &types.Message{
|
|
|
|
From: from,
|
|
|
|
To: to,
|
|
|
|
Value: big.Zero(),
|
2022-10-21 10:59:09 +00:00
|
|
|
Method: builtintypes.MethodsEVM.GetStorageAt,
|
2022-09-29 22:55:13 +00:00
|
|
|
Params: params,
|
|
|
|
GasLimit: build.BlockGasLimit,
|
|
|
|
GasFeeCap: big.Zero(),
|
|
|
|
GasPremium: big.Zero(),
|
|
|
|
}
|
|
|
|
|
|
|
|
ts := a.Chain.GetHeaviestTipSet()
|
|
|
|
|
|
|
|
// Try calling until we find a height with no migration.
|
|
|
|
var res *api.InvocResult
|
|
|
|
for {
|
|
|
|
res, err = a.StateManager.Call(ctx, msg, ts)
|
|
|
|
if err != stmgr.ErrExpensiveFork {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
ts, err = a.Chain.GetTipSetFromKey(ctx, ts.Parents())
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("getting parent tipset: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("Call failed: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if res.MsgRct == nil {
|
|
|
|
return nil, fmt.Errorf("no message receipt")
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.MsgRct.Return, nil
|
2022-09-09 17:59:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *EthModule) EthGetBalance(ctx context.Context, address api.EthAddress, blkParam string) (api.EthBigInt, error) {
|
|
|
|
filAddr, err := address.ToFilecoinAddress()
|
|
|
|
if err != nil {
|
|
|
|
return api.EthBigInt{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
actor, err := a.StateGetActor(ctx, filAddr, types.EmptyTSK)
|
2022-10-22 15:27:09 +00:00
|
|
|
if xerrors.Is(err, types.ErrActorNotFound) {
|
|
|
|
return api.EthBigIntZero, nil
|
|
|
|
} else if err != nil {
|
2022-09-09 17:59:00 +00:00
|
|
|
return api.EthBigInt{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return api.EthBigInt{Int: actor.Balance.Int}, nil
|
|
|
|
}
|
|
|
|
|
2022-09-29 22:55:13 +00:00
|
|
|
func (a *EthModule) EthChainId(ctx context.Context) (api.EthUint64, error) {
|
|
|
|
return api.EthUint64(build.Eip155ChainId), nil
|
2022-09-09 17:59:00 +00:00
|
|
|
}
|
|
|
|
|
2022-10-22 19:12:54 +00:00
|
|
|
func (a *EthModule) EthFeeHistory(ctx context.Context, blkCount api.EthUint64, newestBlkNum string, rewardPercentiles []int64) (api.EthFeeHistory, error) {
|
2022-10-22 15:31:58 +00:00
|
|
|
if blkCount > 1024 {
|
|
|
|
return api.EthFeeHistory{}, fmt.Errorf("block count should be smaller than 1024")
|
|
|
|
}
|
|
|
|
|
|
|
|
newestBlkHeight := uint64(a.Chain.GetHeaviestTipSet().Height())
|
|
|
|
|
|
|
|
// TODO https://github.com/filecoin-project/ref-fvm/issues/1016
|
|
|
|
var blkNum api.EthUint64
|
|
|
|
err := blkNum.UnmarshalJSON([]byte(`"` + newestBlkNum + `"`))
|
|
|
|
if err == nil && uint64(blkNum) < newestBlkHeight {
|
|
|
|
newestBlkHeight = uint64(blkNum)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deal with the case that the chain is shorter than the number of
|
|
|
|
// requested blocks.
|
|
|
|
oldestBlkHeight := uint64(1)
|
2022-10-22 19:12:54 +00:00
|
|
|
if uint64(blkCount) <= newestBlkHeight {
|
|
|
|
oldestBlkHeight = newestBlkHeight - uint64(blkCount) + 1
|
2022-10-22 15:31:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ts, err := a.Chain.GetTipsetByHeight(ctx, abi.ChainEpoch(newestBlkHeight), nil, false)
|
|
|
|
if err != nil {
|
|
|
|
return api.EthFeeHistory{}, fmt.Errorf("cannot load find block height: %v", newestBlkHeight)
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: baseFeePerGas should include the next block after the newest of the returned range, because this
|
|
|
|
// can be inferred from the newest block. we use the newest block's baseFeePerGas for now but need to fix it
|
|
|
|
// In other words, due to deferred execution, we might not be returning the most useful value here for the client.
|
|
|
|
baseFeeArray := []api.EthBigInt{api.EthBigInt(ts.Blocks()[0].ParentBaseFee)}
|
|
|
|
gasUsedRatioArray := []float64{}
|
|
|
|
|
|
|
|
for ts.Height() >= abi.ChainEpoch(oldestBlkHeight) {
|
|
|
|
// Unfortunately we need to rebuild the full message view so we can
|
|
|
|
// totalize gas used in the tipset.
|
2022-11-16 20:26:57 +00:00
|
|
|
block, err := newEthBlockFromFilecoinTipSet(ctx, ts, false, a.Chain, a.ChainAPI, a.StateAPI)
|
2022-10-22 15:31:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return api.EthFeeHistory{}, fmt.Errorf("cannot create eth block: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// both arrays should be reversed at the end
|
|
|
|
baseFeeArray = append(baseFeeArray, api.EthBigInt(ts.Blocks()[0].ParentBaseFee))
|
|
|
|
gasUsedRatioArray = append(gasUsedRatioArray, float64(block.GasUsed)/float64(build.BlockGasLimit))
|
|
|
|
|
|
|
|
parentTsKey := ts.Parents()
|
|
|
|
ts, err = a.Chain.LoadTipSet(ctx, parentTsKey)
|
|
|
|
if err != nil {
|
|
|
|
return api.EthFeeHistory{}, fmt.Errorf("cannot load tipset key: %v", parentTsKey)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reverse the arrays; we collected them newest to oldest; the client expects oldest to newest.
|
|
|
|
|
|
|
|
for i, j := 0, len(baseFeeArray)-1; i < j; i, j = i+1, j-1 {
|
|
|
|
baseFeeArray[i], baseFeeArray[j] = baseFeeArray[j], baseFeeArray[i]
|
|
|
|
}
|
|
|
|
for i, j := 0, len(gasUsedRatioArray)-1; i < j; i, j = i+1, j-1 {
|
|
|
|
gasUsedRatioArray[i], gasUsedRatioArray[j] = gasUsedRatioArray[j], gasUsedRatioArray[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
return api.EthFeeHistory{
|
|
|
|
OldestBlock: oldestBlkHeight,
|
|
|
|
BaseFeePerGas: baseFeeArray,
|
|
|
|
GasUsedRatio: gasUsedRatioArray,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2022-09-09 17:59:00 +00:00
|
|
|
func (a *EthModule) NetVersion(ctx context.Context) (string, error) {
|
|
|
|
// Note that networkId is not encoded in hex
|
|
|
|
nv, err := a.StateNetworkVersion(ctx, types.EmptyTSK)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return strconv.FormatUint(uint64(nv), 10), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *EthModule) NetListening(ctx context.Context) (bool, error) {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2022-09-29 22:55:13 +00:00
|
|
|
func (a *EthModule) EthProtocolVersion(ctx context.Context) (api.EthUint64, error) {
|
2022-09-28 14:58:24 +00:00
|
|
|
height := a.Chain.GetHeaviestTipSet().Height()
|
2022-09-29 22:55:13 +00:00
|
|
|
return api.EthUint64(a.StateManager.GetNetworkVersion(ctx, height)), nil
|
2022-09-28 14:58:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *EthModule) EthMaxPriorityFeePerGas(ctx context.Context) (api.EthBigInt, error) {
|
2022-11-10 11:27:58 +00:00
|
|
|
gasPremium, err := a.GasAPI.GasEstimateGasPremium(ctx, 0, builtinactors.SystemActorAddr, 10000, types.EmptyTSK)
|
2022-09-28 14:58:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return api.EthBigInt(big.Zero()), err
|
|
|
|
}
|
|
|
|
return api.EthBigInt(gasPremium), nil
|
2022-09-09 17:59:00 +00:00
|
|
|
}
|
|
|
|
|
2022-09-28 14:58:24 +00:00
|
|
|
func (a *EthModule) EthGasPrice(ctx context.Context) (api.EthBigInt, error) {
|
|
|
|
// According to Geth's implementation, eth_gasPrice should return base + tip
|
|
|
|
// Ref: https://github.com/ethereum/pm/issues/328#issuecomment-853234014
|
|
|
|
|
|
|
|
ts := a.Chain.GetHeaviestTipSet()
|
|
|
|
baseFee := ts.Blocks()[0].ParentBaseFee
|
|
|
|
|
|
|
|
premium, err := a.EthMaxPriorityFeePerGas(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return api.EthBigInt(big.Zero()), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
gasPrice := big.Add(baseFee, big.Int(premium))
|
|
|
|
return api.EthBigInt(gasPrice), nil
|
2022-09-09 17:59:00 +00:00
|
|
|
}
|
|
|
|
|
2022-09-28 14:58:24 +00:00
|
|
|
func (a *EthModule) EthSendRawTransaction(ctx context.Context, rawTx api.EthBytes) (api.EthHash, error) {
|
2022-09-29 20:46:59 +00:00
|
|
|
txArgs, err := api.ParseEthTxArgs(rawTx)
|
|
|
|
if err != nil {
|
|
|
|
return api.EmptyEthHash, err
|
|
|
|
}
|
2022-09-09 17:59:00 +00:00
|
|
|
|
2022-09-29 20:46:59 +00:00
|
|
|
smsg, err := txArgs.ToSignedMessage()
|
2022-09-28 14:58:24 +00:00
|
|
|
if err != nil {
|
2022-09-29 20:46:59 +00:00
|
|
|
return api.EmptyEthHash, err
|
2022-09-28 14:58:24 +00:00
|
|
|
}
|
2022-09-29 20:46:59 +00:00
|
|
|
|
|
|
|
cid, err := a.MpoolAPI.MpoolPush(ctx, smsg)
|
2022-09-28 14:58:24 +00:00
|
|
|
if err != nil {
|
2022-09-29 20:46:59 +00:00
|
|
|
return api.EmptyEthHash, err
|
|
|
|
}
|
2022-11-12 17:44:52 +00:00
|
|
|
return api.NewEthHashFromCid(cid)
|
2022-09-29 20:46:59 +00:00
|
|
|
}
|
|
|
|
|
2022-10-22 19:07:32 +00:00
|
|
|
func (a *EthModule) ethCallToFilecoinMessage(ctx context.Context, tx api.EthCall) (*types.Message, error) {
|
2022-10-26 17:19:29 +00:00
|
|
|
var err error
|
|
|
|
var from address.Address
|
|
|
|
if tx.From == nil {
|
|
|
|
// Send from the filecoin "system" address.
|
|
|
|
from, err = (api.EthAddress{}).ToFilecoinAddress()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to construct the ethereum system address: %w", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// The from address must be translatable to an f4 address.
|
|
|
|
from, err = tx.From.ToFilecoinAddress()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to translate sender address (%s): %w", tx.From.String(), err)
|
|
|
|
}
|
|
|
|
if p := from.Protocol(); p != address.Delegated {
|
|
|
|
return nil, fmt.Errorf("expected a class 4 address, got: %d: %w", p, err)
|
|
|
|
}
|
2022-09-29 20:46:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var params []byte
|
|
|
|
var to address.Address
|
2022-10-22 15:27:09 +00:00
|
|
|
var method abi.MethodNum
|
2022-09-29 20:46:59 +00:00
|
|
|
if tx.To == nil {
|
2022-10-22 15:27:09 +00:00
|
|
|
// this is a contract creation
|
|
|
|
to = builtintypes.EthereumAddressManagerActorAddr
|
|
|
|
|
|
|
|
nonce, err := a.Mpool.GetNonce(ctx, from, types.EmptyTSK)
|
2022-09-29 20:46:59 +00:00
|
|
|
if err != nil {
|
2022-10-22 15:27:09 +00:00
|
|
|
nonce = 0 // assume a zero nonce on error (e.g. sender doesn't exist).
|
2022-09-29 20:46:59 +00:00
|
|
|
}
|
|
|
|
|
2022-10-26 00:24:41 +00:00
|
|
|
params2, err := actors.SerializeParams(&eam.CreateParams{
|
2022-10-22 15:27:09 +00:00
|
|
|
Initcode: tx.Data,
|
2022-10-26 00:24:41 +00:00
|
|
|
Nonce: nonce,
|
2022-09-29 20:46:59 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
2022-10-26 00:24:41 +00:00
|
|
|
return nil, fmt.Errorf("failed to serialize Create params: %w", err)
|
2022-09-29 20:46:59 +00:00
|
|
|
}
|
2022-10-22 15:27:09 +00:00
|
|
|
params = params2
|
2022-10-26 00:24:41 +00:00
|
|
|
method = builtintypes.MethodsEAM.Create
|
2022-09-29 20:46:59 +00:00
|
|
|
} else {
|
|
|
|
addr, err := tx.To.ToFilecoinAddress()
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("cannot get Filecoin address: %w", err)
|
|
|
|
}
|
|
|
|
to = addr
|
2022-10-22 15:27:09 +00:00
|
|
|
|
|
|
|
if len(tx.Data) > 0 {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
if err := cbg.WriteByteArray(&buf, tx.Data); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to encode tx input into a cbor byte-string")
|
|
|
|
}
|
|
|
|
params = buf.Bytes()
|
|
|
|
method = builtintypes.MethodsEVM.InvokeContract
|
|
|
|
} else {
|
|
|
|
method = builtintypes.MethodSend
|
2022-10-21 10:59:09 +00:00
|
|
|
}
|
2022-09-28 14:58:24 +00:00
|
|
|
}
|
2022-09-29 20:46:59 +00:00
|
|
|
|
2022-10-22 19:07:32 +00:00
|
|
|
return &types.Message{
|
2022-09-28 14:58:24 +00:00
|
|
|
From: from,
|
|
|
|
To: to,
|
|
|
|
Value: big.Int(tx.Value),
|
2022-10-22 15:27:09 +00:00
|
|
|
Method: method,
|
2022-09-29 20:46:59 +00:00
|
|
|
Params: params,
|
2022-09-28 14:58:24 +00:00
|
|
|
GasLimit: build.BlockGasLimit,
|
|
|
|
GasFeeCap: big.Zero(),
|
|
|
|
GasPremium: big.Zero(),
|
2022-10-22 19:07:32 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *EthModule) applyMessage(ctx context.Context, msg *types.Message) (res *api.InvocResult, err error) {
|
2022-09-28 14:58:24 +00:00
|
|
|
ts := a.Chain.GetHeaviestTipSet()
|
|
|
|
|
|
|
|
// Try calling until we find a height with no migration.
|
|
|
|
for {
|
|
|
|
res, err = a.StateManager.CallWithGas(ctx, msg, []types.ChainMsg{}, ts)
|
|
|
|
if err != stmgr.ErrExpensiveFork {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
ts, err = a.Chain.GetTipSetFromKey(ctx, ts.Parents())
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("getting parent tipset: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("CallWithGas failed: %w", err)
|
|
|
|
}
|
2022-09-29 22:55:13 +00:00
|
|
|
if res.MsgRct.ExitCode.IsError() {
|
2022-09-28 14:58:24 +00:00
|
|
|
return nil, xerrors.Errorf("message execution failed: exit %s, reason: %s", res.MsgRct.ExitCode, res.Error)
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
2022-09-09 17:59:00 +00:00
|
|
|
|
2022-09-29 22:55:13 +00:00
|
|
|
func (a *EthModule) EthEstimateGas(ctx context.Context, tx api.EthCall) (api.EthUint64, error) {
|
2022-10-22 19:07:32 +00:00
|
|
|
msg, err := a.ethCallToFilecoinMessage(ctx, tx)
|
2022-09-28 14:58:24 +00:00
|
|
|
if err != nil {
|
2022-09-29 22:55:13 +00:00
|
|
|
return api.EthUint64(0), err
|
2022-09-28 14:58:24 +00:00
|
|
|
}
|
2022-10-22 19:07:32 +00:00
|
|
|
|
2022-10-29 09:57:57 +00:00
|
|
|
// Set the gas limit to the zero sentinel value, which makes
|
|
|
|
// gas estimation actually run.
|
|
|
|
msg.GasLimit = 0
|
|
|
|
|
2022-10-22 19:07:32 +00:00
|
|
|
msg, err = a.GasAPI.GasEstimateMessageGas(ctx, msg, nil, types.EmptyTSK)
|
|
|
|
if err != nil {
|
|
|
|
return api.EthUint64(0), err
|
|
|
|
}
|
|
|
|
|
|
|
|
return api.EthUint64(msg.GasLimit), nil
|
2022-09-12 21:46:15 +00:00
|
|
|
}
|
|
|
|
|
2022-09-28 14:58:24 +00:00
|
|
|
func (a *EthModule) EthCall(ctx context.Context, tx api.EthCall, blkParam string) (api.EthBytes, error) {
|
2022-10-22 19:07:32 +00:00
|
|
|
msg, err := a.ethCallToFilecoinMessage(ctx, tx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
invokeResult, err := a.applyMessage(ctx, msg)
|
2022-09-28 14:58:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(invokeResult.MsgRct.Return) > 0 {
|
2022-10-21 10:59:09 +00:00
|
|
|
return cbg.ReadByteArray(bytes.NewReader(invokeResult.MsgRct.Return), uint64(len(invokeResult.MsgRct.Return)))
|
2022-09-28 14:58:24 +00:00
|
|
|
}
|
|
|
|
return api.EthBytes{}, nil
|
2022-09-12 21:46:15 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
func (e *EthEvent) EthGetLogs(ctx context.Context, filterSpec *api.EthFilterSpec) (*api.EthFilterResult, error) {
|
|
|
|
if e.EventFilterManager == nil {
|
|
|
|
return nil, api.ErrNotSupported
|
2022-09-12 21:46:15 +00:00
|
|
|
}
|
2022-11-16 20:26:57 +00:00
|
|
|
|
|
|
|
// Create a temporary filter
|
|
|
|
f, err := e.installEthFilterSpec(ctx, filterSpec)
|
2022-09-12 21:46:15 +00:00
|
|
|
if err != nil {
|
2022-11-16 20:26:57 +00:00
|
|
|
return nil, err
|
2022-09-12 21:46:15 +00:00
|
|
|
}
|
2022-11-16 20:26:57 +00:00
|
|
|
ces := f.TakeCollectedEvents(ctx)
|
|
|
|
|
|
|
|
_ = e.uninstallFilter(ctx, f)
|
|
|
|
|
|
|
|
return ethFilterResultFromEvents(ces)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *EthEvent) EthGetFilterChanges(ctx context.Context, id api.EthFilterID) (*api.EthFilterResult, error) {
|
|
|
|
if e.FilterStore == nil {
|
|
|
|
return nil, api.ErrNotSupported
|
2022-11-12 17:44:52 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
f, err := e.FilterStore.Get(ctx, string(id))
|
2022-11-12 17:44:52 +00:00
|
|
|
if err != nil {
|
2022-11-16 20:26:57 +00:00
|
|
|
return nil, err
|
2022-11-12 17:44:52 +00:00
|
|
|
}
|
2022-11-16 20:26:57 +00:00
|
|
|
|
|
|
|
switch fc := f.(type) {
|
|
|
|
case filterEventCollector:
|
|
|
|
return ethFilterResultFromEvents(fc.TakeCollectedEvents(ctx))
|
|
|
|
case filterTipSetCollector:
|
|
|
|
return ethFilterResultFromTipSets(fc.TakeCollectedTipSets(ctx))
|
|
|
|
case filterMessageCollector:
|
|
|
|
return ethFilterResultFromMessages(fc.TakeCollectedMessages(ctx))
|
2022-09-12 21:46:15 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
return nil, xerrors.Errorf("unknown filter type")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *EthEvent) EthGetFilterLogs(ctx context.Context, id api.EthFilterID) (*api.EthFilterResult, error) {
|
|
|
|
if e.FilterStore == nil {
|
|
|
|
return nil, api.ErrNotSupported
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := e.FilterStore.Get(ctx, string(id))
|
2022-09-12 21:46:15 +00:00
|
|
|
if err != nil {
|
2022-11-16 20:26:57 +00:00
|
|
|
return nil, err
|
2022-09-12 21:46:15 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
switch fc := f.(type) {
|
|
|
|
case filterEventCollector:
|
|
|
|
return ethFilterResultFromEvents(fc.TakeCollectedEvents(ctx))
|
|
|
|
}
|
2022-09-12 21:46:15 +00:00
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
return nil, xerrors.Errorf("wrong filter type")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *EthEvent) installEthFilterSpec(ctx context.Context, filterSpec *api.EthFilterSpec) (*filter.EventFilter, error) {
|
|
|
|
var (
|
|
|
|
minHeight abi.ChainEpoch
|
|
|
|
maxHeight abi.ChainEpoch
|
|
|
|
tipsetCid cid.Cid
|
|
|
|
addresses []address.Address
|
|
|
|
keys = map[string][][]byte{}
|
|
|
|
)
|
|
|
|
|
|
|
|
if filterSpec.BlockHash != nil {
|
|
|
|
if filterSpec.FromBlock != nil || filterSpec.ToBlock != nil {
|
|
|
|
return nil, xerrors.Errorf("must not specify block hash and from/to block")
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: derive a tipset hash from eth hash - might need to push this down into the EventFilterManager
|
|
|
|
} else {
|
|
|
|
if filterSpec.FromBlock == nil || *filterSpec.FromBlock == "latest" {
|
|
|
|
ts := e.Chain.GetHeaviestTipSet()
|
|
|
|
minHeight = ts.Height()
|
|
|
|
} else if *filterSpec.FromBlock == "earliest" {
|
|
|
|
minHeight = 0
|
|
|
|
} else if *filterSpec.FromBlock == "pending" {
|
|
|
|
return nil, api.ErrNotSupported
|
|
|
|
} else {
|
|
|
|
epoch, err := api.EthUint64FromHex(*filterSpec.FromBlock)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("invalid epoch")
|
2022-09-12 21:46:15 +00:00
|
|
|
}
|
2022-11-16 20:26:57 +00:00
|
|
|
minHeight = abi.ChainEpoch(epoch)
|
|
|
|
}
|
2022-09-12 21:46:15 +00:00
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
if filterSpec.ToBlock == nil || *filterSpec.ToBlock == "latest" {
|
|
|
|
// here latest means the latest at the time
|
|
|
|
maxHeight = -1
|
|
|
|
} else if *filterSpec.ToBlock == "earliest" {
|
|
|
|
maxHeight = 0
|
|
|
|
} else if *filterSpec.ToBlock == "pending" {
|
|
|
|
return nil, api.ErrNotSupported
|
|
|
|
} else {
|
|
|
|
epoch, err := api.EthUint64FromHex(*filterSpec.FromBlock)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("invalid epoch")
|
2022-09-12 21:46:15 +00:00
|
|
|
}
|
2022-11-16 20:26:57 +00:00
|
|
|
maxHeight = abi.ChainEpoch(epoch)
|
2022-09-12 21:46:15 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
// Validate height ranges are within limits set by node operator
|
|
|
|
if minHeight == -1 && maxHeight > 0 {
|
|
|
|
// Here the client is looking for events between the head and some future height
|
|
|
|
ts := e.Chain.GetHeaviestTipSet()
|
|
|
|
if maxHeight-ts.Height() > e.MaxFilterHeightRange {
|
|
|
|
return nil, xerrors.Errorf("invalid epoch range")
|
|
|
|
}
|
|
|
|
} else if minHeight >= 0 && maxHeight == -1 {
|
|
|
|
// Here the client is looking for events between some time in the past and the current head
|
|
|
|
ts := e.Chain.GetHeaviestTipSet()
|
|
|
|
if ts.Height()-minHeight > e.MaxFilterHeightRange {
|
|
|
|
return nil, xerrors.Errorf("invalid epoch range")
|
|
|
|
}
|
2022-09-12 21:46:15 +00:00
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
} else if minHeight >= 0 && maxHeight >= 0 {
|
|
|
|
if minHeight > maxHeight || maxHeight-minHeight > e.MaxFilterHeightRange {
|
|
|
|
return nil, xerrors.Errorf("invalid epoch range")
|
|
|
|
}
|
|
|
|
}
|
2022-10-21 11:13:16 +00:00
|
|
|
|
|
|
|
}
|
2022-11-16 20:26:57 +00:00
|
|
|
|
|
|
|
// Convert all addresses to filecoin f4 addresses
|
|
|
|
for _, ea := range filterSpec.Address {
|
|
|
|
a, err := ea.ToFilecoinAddress()
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("invalid address %x", ea)
|
2022-10-21 11:13:16 +00:00
|
|
|
}
|
2022-11-16 20:26:57 +00:00
|
|
|
addresses = append(addresses, a)
|
2022-10-21 11:13:16 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
for idx, vals := range filterSpec.Topics {
|
|
|
|
// Ethereum topics are emitted using `LOG{0..4}` opcodes resulting in topics1..4
|
|
|
|
key := fmt.Sprintf("topic%d", idx+1)
|
|
|
|
keyvals := make([][]byte, len(vals))
|
|
|
|
for i, v := range vals {
|
|
|
|
keyvals[i] = v[:]
|
|
|
|
}
|
|
|
|
keys[key] = keyvals
|
2022-10-21 11:13:16 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
return e.EventFilterManager.Install(ctx, minHeight, maxHeight, tipsetCid, addresses, keys)
|
2022-10-21 11:13:16 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
func (e *EthEvent) EthNewFilter(ctx context.Context, filterSpec *api.EthFilterSpec) (api.EthFilterID, error) {
|
|
|
|
if e.FilterStore == nil || e.EventFilterManager == nil {
|
|
|
|
return "", api.ErrNotSupported
|
2022-09-12 21:46:15 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
f, err := e.installEthFilterSpec(ctx, filterSpec)
|
2022-10-21 11:15:24 +00:00
|
|
|
if err != nil {
|
2022-11-16 20:26:57 +00:00
|
|
|
return "", err
|
2022-10-21 11:15:24 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
if err := e.FilterStore.Add(ctx, f); err != nil {
|
|
|
|
// Could not record in store, attempt to delete filter to clean up
|
|
|
|
err2 := e.TipSetFilterManager.Remove(ctx, f.ID())
|
|
|
|
if err2 != nil {
|
|
|
|
return "", xerrors.Errorf("encountered error %v while removing new filter due to %v", err2, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", err
|
2022-10-21 11:15:24 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
return api.EthFilterID(f.ID()), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *EthEvent) EthNewBlockFilter(ctx context.Context) (api.EthFilterID, error) {
|
|
|
|
if e.FilterStore == nil || e.TipSetFilterManager == nil {
|
|
|
|
return "", api.ErrNotSupported
|
2022-09-12 21:46:15 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
f, err := e.TipSetFilterManager.Install(ctx)
|
2022-09-12 21:46:15 +00:00
|
|
|
if err != nil {
|
2022-11-16 20:26:57 +00:00
|
|
|
return "", err
|
2022-09-12 21:46:15 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
if err := e.FilterStore.Add(ctx, f); err != nil {
|
|
|
|
// Could not record in store, attempt to delete filter to clean up
|
|
|
|
err2 := e.TipSetFilterManager.Remove(ctx, f.ID())
|
|
|
|
if err2 != nil {
|
|
|
|
return "", xerrors.Errorf("encountered error %v while removing new filter due to %v", err2, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", err
|
2022-09-12 21:46:15 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
return api.EthFilterID(f.ID()), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *EthEvent) EthNewPendingTransactionFilter(ctx context.Context) (api.EthFilterID, error) {
|
|
|
|
if e.FilterStore == nil || e.MemPoolFilterManager == nil {
|
|
|
|
return "", api.ErrNotSupported
|
2022-09-12 21:46:15 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
f, err := e.MemPoolFilterManager.Install(ctx)
|
2022-09-12 21:46:15 +00:00
|
|
|
if err != nil {
|
2022-11-16 20:26:57 +00:00
|
|
|
return "", err
|
2022-09-12 21:46:15 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
if err := e.FilterStore.Add(ctx, f); err != nil {
|
|
|
|
// Could not record in store, attempt to delete filter to clean up
|
|
|
|
err2 := e.MemPoolFilterManager.Remove(ctx, f.ID())
|
|
|
|
if err2 != nil {
|
|
|
|
return "", xerrors.Errorf("encountered error %v while removing new filter due to %v", err2, err)
|
2022-10-21 10:59:09 +00:00
|
|
|
}
|
2022-09-28 14:58:24 +00:00
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
return "", err
|
2022-09-12 21:46:15 +00:00
|
|
|
}
|
2022-11-16 20:26:57 +00:00
|
|
|
|
|
|
|
return api.EthFilterID(f.ID()), nil
|
2022-09-12 21:46:15 +00:00
|
|
|
}
|
2022-11-10 11:27:58 +00:00
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
func (e *EthEvent) EthUninstallFilter(ctx context.Context, id api.EthFilterID) (bool, error) {
|
|
|
|
if e.FilterStore == nil {
|
|
|
|
return false, api.ErrNotSupported
|
2022-11-16 14:15:29 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
f, err := e.FilterStore.Get(ctx, string(id))
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, filter.ErrFilterNotFound) {
|
|
|
|
return false, nil
|
2022-11-16 14:15:29 +00:00
|
|
|
}
|
2022-11-16 20:26:57 +00:00
|
|
|
return false, err
|
2022-11-16 14:15:29 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
if err := e.uninstallFilter(ctx, f); err != nil {
|
|
|
|
return false, err
|
2022-11-16 14:15:29 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
return true, nil
|
|
|
|
}
|
2022-11-16 14:15:29 +00:00
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
func (e *EthEvent) uninstallFilter(ctx context.Context, f filter.Filter) error {
|
|
|
|
switch f.(type) {
|
|
|
|
case *filter.EventFilter:
|
|
|
|
err := e.EventFilterManager.Remove(ctx, f.ID())
|
|
|
|
if err != nil && !errors.Is(err, filter.ErrFilterNotFound) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case *filter.TipSetFilter:
|
|
|
|
err := e.TipSetFilterManager.Remove(ctx, f.ID())
|
|
|
|
if err != nil && !errors.Is(err, filter.ErrFilterNotFound) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case *filter.MemPoolFilter:
|
|
|
|
err := e.MemPoolFilterManager.Remove(ctx, f.ID())
|
|
|
|
if err != nil && !errors.Is(err, filter.ErrFilterNotFound) {
|
|
|
|
return err
|
2022-11-16 14:15:29 +00:00
|
|
|
}
|
2022-11-16 20:26:57 +00:00
|
|
|
default:
|
|
|
|
return xerrors.Errorf("unknown filter type")
|
2022-11-16 14:15:29 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
return e.FilterStore.Remove(ctx, f.ID())
|
2022-11-16 14:15:29 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
const (
|
|
|
|
EthSubscribeEventTypeHeads = "newHeads"
|
|
|
|
EthSubscribeEventTypeLogs = "logs"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (e *EthEvent) EthSubscribe(ctx context.Context, eventType string, params *api.EthSubscriptionParams) (<-chan api.EthSubscriptionResponse, error) {
|
|
|
|
if e.SubManager == nil {
|
2022-11-15 14:10:48 +00:00
|
|
|
return nil, api.ErrNotSupported
|
|
|
|
}
|
2022-11-16 20:26:57 +00:00
|
|
|
// Note that go-jsonrpc will set the method field of the response to "xrpc.ch.val" but the ethereum api expects the name of the
|
|
|
|
// method to be "eth_subscription". This probably doesn't matter in practice.
|
2022-11-15 14:10:48 +00:00
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
sub, err := e.SubManager.StartSubscription(ctx)
|
2022-11-15 14:10:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
switch eventType {
|
|
|
|
case EthSubscribeEventTypeHeads:
|
|
|
|
f, err := e.TipSetFilterManager.Install(ctx)
|
|
|
|
if err != nil {
|
|
|
|
// clean up any previous filters added and stop the sub
|
|
|
|
_, _ = e.EthUnsubscribe(ctx, api.EthSubscriptionID(sub.id))
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
sub.addFilter(ctx, f)
|
2022-11-15 14:10:48 +00:00
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
case EthSubscribeEventTypeLogs:
|
|
|
|
keys := map[string][][]byte{}
|
|
|
|
if params != nil {
|
|
|
|
for idx, vals := range params.Topics {
|
|
|
|
// Ethereum topics are emitted using `LOG{0..4}` opcodes resulting in topics1..4
|
|
|
|
key := fmt.Sprintf("topic%d", idx+1)
|
|
|
|
keyvals := make([][]byte, len(vals))
|
|
|
|
for i, v := range vals {
|
|
|
|
keyvals[i] = v[:]
|
|
|
|
}
|
|
|
|
keys[key] = keyvals
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := e.EventFilterManager.Install(ctx, -1, -1, cid.Undef, []address.Address{}, keys)
|
|
|
|
if err != nil {
|
|
|
|
// clean up any previous filters added and stop the sub
|
|
|
|
_, _ = e.EthUnsubscribe(ctx, api.EthSubscriptionID(sub.id))
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
sub.addFilter(ctx, f)
|
|
|
|
default:
|
|
|
|
return nil, xerrors.Errorf("unsupported event type: %s", eventType)
|
|
|
|
}
|
|
|
|
|
|
|
|
return sub.out, nil
|
2022-11-10 11:27:58 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
func (e *EthEvent) EthUnsubscribe(ctx context.Context, id api.EthSubscriptionID) (bool, error) {
|
|
|
|
if e.SubManager == nil {
|
|
|
|
return false, api.ErrNotSupported
|
2022-11-10 11:27:58 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
filters, err := e.SubManager.StopSubscription(ctx, string(id))
|
2022-11-10 11:27:58 +00:00
|
|
|
if err != nil {
|
2022-11-16 20:26:57 +00:00
|
|
|
return false, nil
|
2022-11-10 11:27:58 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
for _, f := range filters {
|
|
|
|
if err := e.uninstallFilter(ctx, f); err != nil {
|
|
|
|
// this will leave the filter a zombie, collecting events up to the maximum allowed
|
|
|
|
log.Warnf("failed to remove filter when unsubscribing: %v", err)
|
|
|
|
}
|
2022-11-10 11:27:58 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
return true, nil
|
2022-11-10 11:27:58 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
// GC runs a garbage collection loop, deleting filters that have not been used within the ttl window
|
|
|
|
func (e *EthEvent) GC(ctx context.Context, ttl time.Duration) {
|
2022-11-10 11:27:58 +00:00
|
|
|
if e.FilterStore == nil {
|
2022-11-16 20:26:57 +00:00
|
|
|
return
|
2022-11-10 11:27:58 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
tt := time.NewTicker(time.Minute * 30)
|
|
|
|
defer tt.Stop()
|
2022-11-10 11:27:58 +00:00
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
case <-tt.C:
|
|
|
|
fs := e.FilterStore.NotTakenSince(time.Now().Add(-ttl))
|
|
|
|
for _, f := range fs {
|
|
|
|
if err := e.uninstallFilter(ctx, f); err != nil {
|
|
|
|
log.Warnf("Failed to remove actor event filter during garbage collection: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-11-10 11:27:58 +00:00
|
|
|
}
|
2022-11-16 20:26:57 +00:00
|
|
|
}
|
2022-11-10 11:27:58 +00:00
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
type filterEventCollector interface {
|
|
|
|
TakeCollectedEvents(context.Context) []*filter.CollectedEvent
|
2022-11-10 11:27:58 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
type filterMessageCollector interface {
|
|
|
|
TakeCollectedMessages(context.Context) []cid.Cid
|
|
|
|
}
|
2022-11-10 11:27:58 +00:00
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
type filterTipSetCollector interface {
|
|
|
|
TakeCollectedTipSets(context.Context) []types.TipSetKey
|
|
|
|
}
|
2022-11-10 11:27:58 +00:00
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
func ethFilterResultFromEvents(evs []*filter.CollectedEvent) (*api.EthFilterResult, error) {
|
|
|
|
res := &api.EthFilterResult{}
|
|
|
|
for _, ev := range evs {
|
|
|
|
log := api.EthLog{
|
|
|
|
Removed: ev.Reverted,
|
|
|
|
LogIndex: api.EthUint64(ev.EventIdx),
|
|
|
|
TransactionIndex: api.EthUint64(ev.MsgIdx),
|
|
|
|
BlockNumber: api.EthUint64(ev.Height),
|
2022-11-10 11:27:58 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
var err error
|
2022-11-10 11:27:58 +00:00
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
for _, entry := range ev.Entries {
|
|
|
|
hash := api.EthHashData(entry.Value)
|
|
|
|
if entry.Key == api.EthTopic1 || entry.Key == api.EthTopic2 || entry.Key == api.EthTopic3 || entry.Key == api.EthTopic4 {
|
|
|
|
log.Topics = append(log.Topics, hash)
|
|
|
|
} else {
|
2022-11-16 20:51:28 +00:00
|
|
|
log.Data = hash
|
2022-11-10 11:27:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
log.Address, err = api.EthAddressFromFilecoinAddress(ev.EmitterAddr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-11-15 13:02:11 +00:00
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
log.TransactionHash, err = api.NewEthHashFromCid(ev.MsgCid)
|
2022-11-10 11:27:58 +00:00
|
|
|
if err != nil {
|
2022-11-16 20:26:57 +00:00
|
|
|
return nil, err
|
2022-11-10 11:27:58 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
c, err := ev.TipSetKey.Cid()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-11-10 11:27:58 +00:00
|
|
|
}
|
2022-11-16 20:26:57 +00:00
|
|
|
log.BlockHash, err = api.NewEthHashFromCid(c)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
res.Results = append(res.Results, log)
|
2022-11-10 11:27:58 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
return res, nil
|
2022-11-15 14:10:48 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
func ethFilterResultFromTipSets(tsks []types.TipSetKey) (*api.EthFilterResult, error) {
|
|
|
|
res := &api.EthFilterResult{}
|
2022-11-10 11:27:58 +00:00
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
for _, tsk := range tsks {
|
|
|
|
c, err := tsk.Cid()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
hash, err := api.NewEthHashFromCid(c)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-11-10 11:27:58 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
res.Results = append(res.Results, hash)
|
2022-11-10 11:27:58 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
return res, nil
|
2022-11-10 11:27:58 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
func ethFilterResultFromMessages(cs []cid.Cid) (*api.EthFilterResult, error) {
|
|
|
|
res := &api.EthFilterResult{}
|
2022-11-10 11:27:58 +00:00
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
for _, c := range cs {
|
|
|
|
hash, err := api.NewEthHashFromCid(c)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-11-10 11:27:58 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
res.Results = append(res.Results, hash)
|
2022-11-10 11:27:58 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
return res, nil
|
2022-11-10 11:27:58 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
type EthSubscriptionManager struct {
|
|
|
|
Chain *store.ChainStore
|
|
|
|
StateAPI StateAPI
|
|
|
|
ChainAPI ChainAPI
|
|
|
|
mu sync.Mutex
|
|
|
|
subs map[string]*ethSubscription
|
|
|
|
}
|
2022-11-10 11:27:58 +00:00
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
func (e *EthSubscriptionManager) StartSubscription(ctx context.Context) (*ethSubscription, error) {
|
|
|
|
id, err := uuid.NewRandom()
|
2022-11-10 11:27:58 +00:00
|
|
|
if err != nil {
|
2022-11-16 20:26:57 +00:00
|
|
|
return nil, xerrors.Errorf("new uuid: %w", err)
|
2022-11-10 11:27:58 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
ctx, quit := context.WithCancel(ctx)
|
2022-11-10 11:27:58 +00:00
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
sub := ðSubscription{
|
|
|
|
Chain: e.Chain,
|
|
|
|
StateAPI: e.StateAPI,
|
|
|
|
ChainAPI: e.ChainAPI,
|
|
|
|
id: id.String(),
|
|
|
|
in: make(chan interface{}, 200),
|
|
|
|
out: make(chan api.EthSubscriptionResponse, 20),
|
|
|
|
quit: quit,
|
2022-11-10 11:27:58 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
e.mu.Lock()
|
|
|
|
if e.subs == nil {
|
|
|
|
e.subs = make(map[string]*ethSubscription)
|
2022-11-10 11:27:58 +00:00
|
|
|
}
|
2022-11-16 20:26:57 +00:00
|
|
|
e.subs[sub.id] = sub
|
|
|
|
e.mu.Unlock()
|
2022-11-10 11:27:58 +00:00
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
go sub.start(ctx)
|
2022-11-10 11:27:58 +00:00
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
return sub, nil
|
2022-11-10 11:27:58 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
func (e *EthSubscriptionManager) StopSubscription(ctx context.Context, id string) ([]filter.Filter, error) {
|
|
|
|
e.mu.Lock()
|
|
|
|
defer e.mu.Unlock()
|
|
|
|
|
|
|
|
sub, ok := e.subs[id]
|
|
|
|
if !ok {
|
|
|
|
return nil, xerrors.Errorf("subscription not found")
|
2022-11-10 11:27:58 +00:00
|
|
|
}
|
2022-11-16 20:26:57 +00:00
|
|
|
sub.stop()
|
|
|
|
delete(e.subs, id)
|
2022-11-10 11:27:58 +00:00
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
return sub.filters, nil
|
2022-11-10 11:27:58 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
type ethSubscription struct {
|
|
|
|
Chain *store.ChainStore
|
|
|
|
StateAPI StateAPI
|
|
|
|
ChainAPI ChainAPI
|
|
|
|
id string
|
|
|
|
in chan interface{}
|
|
|
|
out chan api.EthSubscriptionResponse
|
2022-11-10 11:27:58 +00:00
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
mu sync.Mutex
|
|
|
|
filters []filter.Filter
|
|
|
|
quit func()
|
|
|
|
}
|
2022-11-10 11:27:58 +00:00
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
func (e *ethSubscription) addFilter(ctx context.Context, f filter.Filter) {
|
|
|
|
e.mu.Lock()
|
|
|
|
defer e.mu.Unlock()
|
2022-11-10 11:27:58 +00:00
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
f.SetSubChannel(e.in)
|
|
|
|
e.filters = append(e.filters, f)
|
|
|
|
}
|
2022-11-10 11:27:58 +00:00
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
func (e *ethSubscription) start(ctx context.Context) {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
case v := <-e.in:
|
|
|
|
resp := api.EthSubscriptionResponse{
|
|
|
|
SubscriptionID: api.EthSubscriptionID(e.id),
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
switch vt := v.(type) {
|
|
|
|
case *filter.CollectedEvent:
|
|
|
|
resp.Result, err = ethFilterResultFromEvents([]*filter.CollectedEvent{vt})
|
|
|
|
case *types.TipSet:
|
|
|
|
eb, err := newEthBlockFromFilecoinTipSet(ctx, vt, true, e.Chain, e.ChainAPI, e.StateAPI)
|
|
|
|
if err != nil {
|
|
|
|
break
|
2022-11-10 11:27:58 +00:00
|
|
|
}
|
2022-11-16 20:26:57 +00:00
|
|
|
|
|
|
|
resp.Result = eb
|
|
|
|
default:
|
|
|
|
log.Warnf("unexpected subscription value type: %T", vt)
|
2022-11-10 11:27:58 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case e.out <- resp:
|
|
|
|
default:
|
|
|
|
// Skip if client is not reading responses
|
|
|
|
}
|
2022-11-10 11:27:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
func (e *ethSubscription) stop() {
|
|
|
|
e.mu.Lock()
|
|
|
|
defer e.mu.Unlock()
|
|
|
|
|
|
|
|
if e.quit != nil {
|
|
|
|
e.quit()
|
|
|
|
close(e.out)
|
|
|
|
e.quit = nil
|
2022-11-16 12:57:03 +00:00
|
|
|
}
|
2022-11-16 20:26:57 +00:00
|
|
|
}
|
2022-11-16 12:57:03 +00:00
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
func newEthBlockFromFilecoinTipSet(ctx context.Context, ts *types.TipSet, fullTxInfo bool, cs *store.ChainStore, ca ChainAPI, sa StateAPI) (api.EthBlock, error) {
|
|
|
|
parent, err := cs.LoadTipSet(ctx, ts.Parents())
|
2022-11-10 11:27:58 +00:00
|
|
|
if err != nil {
|
2022-11-16 20:26:57 +00:00
|
|
|
return api.EthBlock{}, err
|
2022-11-10 11:27:58 +00:00
|
|
|
}
|
2022-11-16 20:26:57 +00:00
|
|
|
parentKeyCid, err := parent.Key().Cid()
|
|
|
|
if err != nil {
|
|
|
|
return api.EthBlock{}, err
|
2022-11-10 11:27:58 +00:00
|
|
|
}
|
2022-11-16 20:26:57 +00:00
|
|
|
parentBlkHash, err := api.NewEthHashFromCid(parentKeyCid)
|
|
|
|
if err != nil {
|
|
|
|
return api.EthBlock{}, err
|
2022-11-10 11:27:58 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
blkCid, err := ts.Key().Cid()
|
|
|
|
if err != nil {
|
|
|
|
return api.EthBlock{}, err
|
|
|
|
}
|
|
|
|
blkHash, err := api.NewEthHashFromCid(blkCid)
|
|
|
|
if err != nil {
|
|
|
|
return api.EthBlock{}, err
|
2022-11-10 11:27:58 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
blkMsgs, err := cs.BlockMsgsForTipset(ctx, ts)
|
|
|
|
if err != nil {
|
|
|
|
return api.EthBlock{}, xerrors.Errorf("error loading messages for tipset: %v: %w", ts, err)
|
|
|
|
}
|
2022-11-10 11:27:58 +00:00
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
block := api.NewEthBlock()
|
2022-11-10 11:27:58 +00:00
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
// 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 _, blkMsg := range blkMsgs {
|
|
|
|
for _, msg := range append(blkMsg.BlsMessages, blkMsg.SecpkMessages...) {
|
|
|
|
msgLookup, err := sa.StateSearchMsg(ctx, types.EmptyTSK, msg.Cid(), api.LookbackNoLimit, true)
|
|
|
|
if err != nil || msgLookup == nil {
|
|
|
|
return api.EthBlock{}, nil
|
|
|
|
}
|
|
|
|
gasUsed += msgLookup.Receipt.GasUsed
|
2022-11-10 11:27:58 +00:00
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
if fullTxInfo {
|
|
|
|
tx, err := newEthTxFromFilecoinMessageLookup(ctx, msgLookup, cs, ca, sa)
|
|
|
|
if err != nil {
|
|
|
|
return api.EthBlock{}, nil
|
|
|
|
}
|
|
|
|
block.Transactions = append(block.Transactions, tx)
|
2022-11-10 11:27:58 +00:00
|
|
|
} else {
|
2022-11-16 20:26:57 +00:00
|
|
|
hash, err := api.NewEthHashFromCid(msg.Cid())
|
|
|
|
if err != nil {
|
|
|
|
return api.EthBlock{}, err
|
|
|
|
}
|
|
|
|
block.Transactions = append(block.Transactions, hash.String())
|
2022-11-10 11:27:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
block.Hash = blkHash
|
|
|
|
block.Number = api.EthUint64(ts.Height())
|
|
|
|
block.ParentHash = parentBlkHash
|
|
|
|
block.Timestamp = api.EthUint64(ts.Blocks()[0].Timestamp)
|
|
|
|
block.BaseFeePerGas = api.EthBigInt{Int: ts.Blocks()[0].ParentBaseFee.Int}
|
|
|
|
block.GasUsed = api.EthUint64(gasUsed)
|
|
|
|
return block, nil
|
2022-11-10 11:27:58 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
// lookupEthAddress makes its best effort at finding the Ethereum address for a
|
|
|
|
// Filecoin address. It does the following:
|
|
|
|
//
|
|
|
|
// 1. If the supplied address is an f410 address, we return its payload as the EthAddress.
|
|
|
|
// 2. Otherwise (f0, f1, f2, f3), we look up the actor on the state tree. If it has a predictable address, we return it if it's f410 address.
|
|
|
|
// 3. Otherwise, we fall back to returning a masked ID Ethereum address. If the supplied address is an f0 address, we
|
|
|
|
// use that ID to form the masked ID address.
|
|
|
|
// 4. Otherwise, we fetch the actor's ID from the state tree and form the masked ID with it.
|
|
|
|
func lookupEthAddress(ctx context.Context, addr address.Address, sa StateAPI) (api.EthAddress, error) {
|
|
|
|
// Attempt to convert directly.
|
|
|
|
if ethAddr, ok, err := api.TryEthAddressFromFilecoinAddress(addr, false); err != nil {
|
|
|
|
return api.EthAddress{}, err
|
|
|
|
} else if ok {
|
|
|
|
return ethAddr, nil
|
2022-11-10 11:27:58 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
// Lookup on the target actor.
|
|
|
|
actor, err := sa.StateGetActor(ctx, addr, types.EmptyTSK)
|
|
|
|
if err != nil {
|
|
|
|
return api.EthAddress{}, err
|
|
|
|
}
|
|
|
|
if actor.Address != nil {
|
|
|
|
if ethAddr, ok, err := api.TryEthAddressFromFilecoinAddress(*actor.Address, false); err != nil {
|
|
|
|
return api.EthAddress{}, err
|
|
|
|
} else if ok {
|
|
|
|
return ethAddr, nil
|
2022-11-10 11:27:58 +00:00
|
|
|
}
|
2022-11-16 20:26:57 +00:00
|
|
|
}
|
2022-11-10 11:27:58 +00:00
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
// Check if we already have an ID addr, and use it if possible.
|
|
|
|
if ethAddr, ok, err := api.TryEthAddressFromFilecoinAddress(addr, true); err != nil {
|
|
|
|
return api.EthAddress{}, err
|
|
|
|
} else if ok {
|
|
|
|
return ethAddr, nil
|
2022-11-10 11:27:58 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
// Otherwise, resolve the ID addr.
|
|
|
|
idAddr, err := sa.StateLookupID(ctx, addr, types.EmptyTSK)
|
|
|
|
if err != nil {
|
|
|
|
return api.EthAddress{}, err
|
|
|
|
}
|
|
|
|
return api.EthAddressFromFilecoinAddress(idAddr)
|
2022-11-10 11:27:58 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
func newEthTxFromFilecoinMessageLookup(ctx context.Context, msgLookup *api.MsgLookup, cs *store.ChainStore, ca ChainAPI, sa StateAPI) (api.EthTx, error) {
|
|
|
|
if msgLookup == nil {
|
|
|
|
return api.EthTx{}, fmt.Errorf("msg does not exist")
|
|
|
|
}
|
|
|
|
cid := msgLookup.Message
|
|
|
|
txHash, err := api.NewEthHashFromCid(cid)
|
|
|
|
if err != nil {
|
|
|
|
return api.EthTx{}, err
|
|
|
|
}
|
2022-11-10 11:27:58 +00:00
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
ts, err := cs.LoadTipSet(ctx, msgLookup.TipSet)
|
2022-11-10 11:27:58 +00:00
|
|
|
if err != nil {
|
2022-11-16 20:26:57 +00:00
|
|
|
return api.EthTx{}, err
|
2022-11-10 11:27:58 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
// This tx is located in the parent tipset
|
|
|
|
parentTs, err := cs.LoadTipSet(ctx, ts.Parents())
|
|
|
|
if err != nil {
|
|
|
|
return api.EthTx{}, err
|
|
|
|
}
|
2022-11-10 11:27:58 +00:00
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
parentTsCid, err := parentTs.Key().Cid()
|
|
|
|
if err != nil {
|
|
|
|
return api.EthTx{}, err
|
2022-11-10 11:27:58 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
blkHash, err := api.NewEthHashFromCid(parentTsCid)
|
|
|
|
if err != nil {
|
|
|
|
return api.EthTx{}, err
|
2022-11-10 11:27:58 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
msg, err := ca.ChainGetMessage(ctx, msgLookup.Message)
|
|
|
|
if err != nil {
|
|
|
|
return api.EthTx{}, err
|
|
|
|
}
|
2022-11-10 11:27:58 +00:00
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
fromEthAddr, err := lookupEthAddress(ctx, msg.From, sa)
|
|
|
|
if err != nil {
|
|
|
|
return api.EthTx{}, err
|
|
|
|
}
|
2022-11-10 11:27:58 +00:00
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
toEthAddr, err := lookupEthAddress(ctx, msg.To, sa)
|
|
|
|
if err != nil {
|
|
|
|
return api.EthTx{}, err
|
|
|
|
}
|
2022-11-10 11:27:58 +00:00
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
toAddr := &toEthAddr
|
|
|
|
input := msg.Params
|
|
|
|
// Check to see if we need to decode as contract deployment.
|
|
|
|
// We don't need to resolve the to address, because there's only one form (an ID).
|
|
|
|
if msg.To == builtintypes.EthereumAddressManagerActorAddr {
|
|
|
|
switch msg.Method {
|
|
|
|
case builtintypes.MethodsEAM.Create:
|
|
|
|
toAddr = nil
|
|
|
|
var params eam.CreateParams
|
|
|
|
err = params.UnmarshalCBOR(bytes.NewReader(msg.Params))
|
|
|
|
input = params.Initcode
|
|
|
|
case builtintypes.MethodsEAM.Create2:
|
|
|
|
toAddr = nil
|
|
|
|
var params eam.Create2Params
|
|
|
|
err = params.UnmarshalCBOR(bytes.NewReader(msg.Params))
|
|
|
|
input = params.Initcode
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return api.EthTx{}, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Otherwise, try to decode as a cbor byte array.
|
|
|
|
// TODO: Actually check if this is an ethereum call. This code will work for demo purposes, but is not correct.
|
|
|
|
if toAddr != nil {
|
|
|
|
if decodedParams, err := cbg.ReadByteArray(bytes.NewReader(msg.Params), uint64(len(msg.Params))); err == nil {
|
|
|
|
input = decodedParams
|
|
|
|
}
|
2022-11-10 11:27:58 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
tx := api.EthTx{
|
|
|
|
ChainID: api.EthUint64(build.Eip155ChainId),
|
|
|
|
Hash: txHash,
|
|
|
|
BlockHash: blkHash,
|
|
|
|
BlockNumber: api.EthUint64(parentTs.Height()),
|
|
|
|
From: fromEthAddr,
|
|
|
|
To: toAddr,
|
|
|
|
Value: api.EthBigInt(msg.Value),
|
|
|
|
Type: api.EthUint64(2),
|
|
|
|
Gas: api.EthUint64(msg.GasLimit),
|
|
|
|
MaxFeePerGas: api.EthBigInt(msg.GasFeeCap),
|
|
|
|
MaxPriorityFeePerGas: api.EthBigInt(msg.GasPremium),
|
|
|
|
V: api.EthBytes{},
|
|
|
|
R: api.EthBytes{},
|
|
|
|
S: api.EthBytes{},
|
|
|
|
Input: input,
|
|
|
|
}
|
|
|
|
return tx, nil
|
2022-11-10 11:27:58 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
func newEthTxReceipt(ctx context.Context, tx api.EthTx, lookup *api.MsgLookup, replay *api.InvocResult, events []types.Event, sa StateAPI) (api.EthTxReceipt, error) {
|
|
|
|
receipt := api.EthTxReceipt{
|
|
|
|
TransactionHash: tx.Hash,
|
|
|
|
TransactionIndex: tx.TransactionIndex,
|
|
|
|
BlockHash: tx.BlockHash,
|
|
|
|
BlockNumber: tx.BlockNumber,
|
|
|
|
From: tx.From,
|
|
|
|
To: tx.To,
|
|
|
|
StateRoot: api.EmptyEthHash,
|
|
|
|
LogsBloom: []byte{0},
|
|
|
|
}
|
2022-11-10 11:27:58 +00:00
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
if receipt.To == nil && lookup.Receipt.ExitCode.IsSuccess() {
|
|
|
|
// Create and Create2 return the same things.
|
|
|
|
var ret eam.CreateReturn
|
|
|
|
if err := ret.UnmarshalCBOR(bytes.NewReader(lookup.Receipt.Return)); err != nil {
|
|
|
|
return api.EthTxReceipt{}, xerrors.Errorf("failed to parse contract creation result: %w", err)
|
|
|
|
}
|
|
|
|
addr := api.EthAddress(ret.EthAddress)
|
|
|
|
receipt.ContractAddress = &addr
|
|
|
|
}
|
2022-11-10 11:27:58 +00:00
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
if lookup.Receipt.ExitCode.IsSuccess() {
|
|
|
|
receipt.Status = 1
|
|
|
|
}
|
|
|
|
if lookup.Receipt.ExitCode.IsError() {
|
|
|
|
receipt.Status = 0
|
|
|
|
}
|
2022-11-10 11:27:58 +00:00
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
if len(events) > 0 {
|
|
|
|
receipt.Logs = make([]api.EthLog, 0, len(events))
|
|
|
|
for i, evt := range events {
|
|
|
|
l := api.EthLog{
|
|
|
|
Removed: false,
|
|
|
|
LogIndex: api.EthUint64(i),
|
|
|
|
TransactionIndex: tx.TransactionIndex,
|
|
|
|
TransactionHash: tx.Hash,
|
|
|
|
BlockHash: tx.BlockHash,
|
|
|
|
BlockNumber: tx.BlockNumber,
|
2022-11-10 11:27:58 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
for _, entry := range evt.Entries {
|
|
|
|
hash := api.EthHashData(entry.Value)
|
|
|
|
if entry.Key == api.EthTopic1 || entry.Key == api.EthTopic2 || entry.Key == api.EthTopic3 || entry.Key == api.EthTopic4 {
|
|
|
|
l.Topics = append(l.Topics, hash)
|
|
|
|
} else {
|
2022-11-16 20:51:28 +00:00
|
|
|
l.Data = hash
|
2022-11-16 12:57:03 +00:00
|
|
|
}
|
2022-11-10 11:27:58 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
addr, err := address.NewIDAddress(uint64(evt.Emitter))
|
2022-11-10 11:27:58 +00:00
|
|
|
if err != nil {
|
2022-11-16 20:26:57 +00:00
|
|
|
return api.EthTxReceipt{}, xerrors.Errorf("failed to create ID address: %w", err)
|
2022-11-10 11:27:58 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
l.Address, err = lookupEthAddress(ctx, addr, sa)
|
|
|
|
if err != nil {
|
|
|
|
return api.EthTxReceipt{}, xerrors.Errorf("failed to resolve Ethereum address: %w", err)
|
2022-11-10 11:27:58 +00:00
|
|
|
}
|
2022-11-16 20:26:57 +00:00
|
|
|
|
|
|
|
receipt.Logs = append(receipt.Logs, l)
|
2022-11-10 11:27:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
receipt.GasUsed = api.EthUint64(lookup.Receipt.GasUsed)
|
2022-11-10 11:27:58 +00:00
|
|
|
|
2022-11-16 20:26:57 +00:00
|
|
|
// TODO: handle CumulativeGasUsed
|
|
|
|
receipt.CumulativeGasUsed = api.EmptyEthInt
|
|
|
|
|
|
|
|
effectiveGasPrice := big.Div(replay.GasCost.TotalCost, big.NewInt(lookup.Receipt.GasUsed))
|
|
|
|
receipt.EffectiveGasPrice = api.EthBigInt(effectiveGasPrice)
|
|
|
|
|
|
|
|
return receipt, nil
|
2022-11-10 11:27:58 +00:00
|
|
|
}
|