2022-12-13 17:14:03 +00:00
|
|
|
package gateway
|
|
|
|
|
|
|
|
import (
|
2023-01-05 13:00:34 +00:00
|
|
|
"bytes"
|
2022-12-13 17:14:03 +00:00
|
|
|
"context"
|
2023-01-16 14:28:55 +00:00
|
|
|
"encoding/json"
|
2023-01-05 13:00:34 +00:00
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
"time"
|
2022-12-13 17:14:03 +00:00
|
|
|
|
2023-02-02 12:25:50 +00:00
|
|
|
"github.com/ipfs/go-cid"
|
2023-01-05 13:00:34 +00:00
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
2023-01-16 14:28:55 +00:00
|
|
|
"github.com/filecoin-project/go-jsonrpc"
|
2023-01-05 13:00:34 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
2022-12-13 17:14:03 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/big"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/lotus/api"
|
2023-08-09 19:12:30 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/events/filter"
|
2023-01-05 13:00:34 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
|
|
|
"github.com/filecoin-project/lotus/chain/types/ethtypes"
|
2022-12-13 17:14:03 +00:00
|
|
|
)
|
|
|
|
|
2023-02-02 12:25:50 +00:00
|
|
|
func (gw *Node) Web3ClientVersion(ctx context.Context) (string, error) {
|
2023-03-07 17:49:07 +00:00
|
|
|
if err := gw.limit(ctx, basicRateLimitTokens); err != nil {
|
2023-02-02 12:25:50 +00:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return gw.target.Web3ClientVersion(ctx)
|
|
|
|
}
|
|
|
|
|
2023-01-05 13:00:34 +00:00
|
|
|
func (gw *Node) EthAccounts(ctx context.Context) ([]ethtypes.EthAddress, error) {
|
2022-12-13 17:14:03 +00:00
|
|
|
// gateway provides public API, so it can't hold user accounts
|
2023-01-05 13:00:34 +00:00
|
|
|
return []ethtypes.EthAddress{}, nil
|
2022-12-13 17:14:03 +00:00
|
|
|
}
|
|
|
|
|
2023-01-05 13:00:34 +00:00
|
|
|
func (gw *Node) EthBlockNumber(ctx context.Context) (ethtypes.EthUint64, error) {
|
2023-03-07 17:49:07 +00:00
|
|
|
if err := gw.limit(ctx, chainRateLimitTokens); err != nil {
|
2022-12-13 17:14:03 +00:00
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2023-01-05 13:00:34 +00:00
|
|
|
return gw.target.EthBlockNumber(ctx)
|
2022-12-13 17:14:03 +00:00
|
|
|
}
|
|
|
|
|
2023-01-05 13:00:34 +00:00
|
|
|
func (gw *Node) EthGetBlockTransactionCountByNumber(ctx context.Context, blkNum ethtypes.EthUint64) (ethtypes.EthUint64, error) {
|
2022-12-13 17:14:03 +00:00
|
|
|
if err := gw.limit(ctx, stateRateLimitTokens); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2023-01-05 13:00:34 +00:00
|
|
|
head, err := gw.target.ChainHead(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
if err := gw.checkTipsetHeight(head, abi.ChainEpoch(blkNum)); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return gw.target.EthGetBlockTransactionCountByNumber(ctx, blkNum)
|
2022-12-13 17:14:03 +00:00
|
|
|
}
|
|
|
|
|
2023-01-05 13:00:34 +00:00
|
|
|
func (gw *Node) tskByEthHash(ctx context.Context, blkHash ethtypes.EthHash) (types.TipSetKey, error) {
|
|
|
|
tskCid := blkHash.ToCid()
|
|
|
|
tskBlk, err := gw.target.ChainReadObj(ctx, tskCid)
|
|
|
|
if err != nil {
|
|
|
|
return types.EmptyTSK, err
|
|
|
|
}
|
|
|
|
tsk := new(types.TipSetKey)
|
|
|
|
if err := tsk.UnmarshalCBOR(bytes.NewReader(tskBlk)); err != nil {
|
|
|
|
return types.EmptyTSK, xerrors.Errorf("cannot unmarshal block into tipset key: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return *tsk, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gw *Node) checkBlkHash(ctx context.Context, blkHash ethtypes.EthHash) error {
|
|
|
|
tsk, err := gw.tskByEthHash(ctx, blkHash)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return gw.checkTipsetKey(ctx, tsk)
|
|
|
|
}
|
|
|
|
|
2023-06-21 17:36:04 +00:00
|
|
|
func (gw *Node) checkEthBlockParam(ctx context.Context, blkParam ethtypes.EthBlockNumberOrHash, lookback ethtypes.EthUint64) error {
|
|
|
|
// first check if its a predefined block or a block number
|
|
|
|
if blkParam.PredefinedBlock != nil || blkParam.BlockNumber != nil {
|
|
|
|
head, err := gw.target.ChainHead(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-05-03 17:24:36 +00:00
|
|
|
|
2023-06-21 17:36:04 +00:00
|
|
|
var num ethtypes.EthUint64 = 0
|
|
|
|
if blkParam.PredefinedBlock != nil {
|
|
|
|
if *blkParam.PredefinedBlock == "earliest" {
|
|
|
|
return fmt.Errorf("block param \"earliest\" is not supported")
|
|
|
|
} else if *blkParam.PredefinedBlock == "pending" || *blkParam.PredefinedBlock == "latest" {
|
|
|
|
// Head is always ok.
|
|
|
|
if lookback == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if lookback <= ethtypes.EthUint64(head.Height()) {
|
|
|
|
num = ethtypes.EthUint64(head.Height()) - lookback
|
|
|
|
}
|
2023-05-03 17:24:36 +00:00
|
|
|
}
|
2023-06-21 17:36:04 +00:00
|
|
|
} else {
|
|
|
|
num = *blkParam.BlockNumber
|
2023-05-03 17:24:36 +00:00
|
|
|
}
|
2023-06-21 17:36:04 +00:00
|
|
|
|
|
|
|
return gw.checkTipsetHeight(head, abi.ChainEpoch(num))
|
2023-05-03 17:24:36 +00:00
|
|
|
}
|
|
|
|
|
2023-06-21 17:36:04 +00:00
|
|
|
// otherwise its a block hash
|
|
|
|
if blkParam.BlockHash != nil {
|
|
|
|
return gw.checkBlkHash(ctx, *blkParam.BlockHash)
|
|
|
|
}
|
2023-05-03 17:24:36 +00:00
|
|
|
|
2023-06-21 17:36:04 +00:00
|
|
|
return fmt.Errorf("invalid block param")
|
2023-05-03 17:24:36 +00:00
|
|
|
}
|
|
|
|
|
2023-03-13 23:36:48 +00:00
|
|
|
func (gw *Node) checkBlkParam(ctx context.Context, blkParam string, lookback ethtypes.EthUint64) error {
|
2023-01-05 13:00:34 +00:00
|
|
|
if blkParam == "earliest" {
|
|
|
|
// also not supported in node impl
|
|
|
|
return fmt.Errorf("block param \"earliest\" is not supported")
|
|
|
|
}
|
|
|
|
|
2023-03-13 23:36:48 +00:00
|
|
|
head, err := gw.target.ChainHead(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var num ethtypes.EthUint64
|
2023-01-05 13:00:34 +00:00
|
|
|
switch blkParam {
|
|
|
|
case "pending", "latest":
|
2023-03-13 23:36:48 +00:00
|
|
|
// Head is always ok.
|
|
|
|
if lookback == 0 {
|
|
|
|
return nil
|
2023-01-05 13:00:34 +00:00
|
|
|
}
|
2023-03-13 23:36:48 +00:00
|
|
|
// Can't look beyond 0 anyways.
|
|
|
|
if lookback > ethtypes.EthUint64(head.Height()) {
|
|
|
|
break
|
2023-01-05 13:00:34 +00:00
|
|
|
}
|
2023-03-13 23:36:48 +00:00
|
|
|
num = ethtypes.EthUint64(head.Height()) - lookback
|
|
|
|
default:
|
|
|
|
if err := num.UnmarshalJSON([]byte(`"` + blkParam + `"`)); err != nil {
|
|
|
|
return fmt.Errorf("cannot parse block number: %v", err)
|
2023-01-05 13:00:34 +00:00
|
|
|
}
|
|
|
|
|
2023-03-13 23:36:48 +00:00
|
|
|
}
|
|
|
|
return gw.checkTipsetHeight(head, abi.ChainEpoch(num))
|
2023-01-05 13:00:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (gw *Node) EthGetBlockTransactionCountByHash(ctx context.Context, blkHash ethtypes.EthHash) (ethtypes.EthUint64, error) {
|
2023-03-07 17:49:07 +00:00
|
|
|
if err := gw.limit(ctx, chainRateLimitTokens); err != nil {
|
2023-01-05 13:00:34 +00:00
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return gw.target.EthGetBlockTransactionCountByHash(ctx, blkHash)
|
2022-12-13 17:14:03 +00:00
|
|
|
}
|
|
|
|
|
2023-01-05 13:00:34 +00:00
|
|
|
func (gw *Node) EthGetBlockByHash(ctx context.Context, blkHash ethtypes.EthHash, fullTxInfo bool) (ethtypes.EthBlock, error) {
|
2022-12-13 17:14:03 +00:00
|
|
|
if err := gw.limit(ctx, stateRateLimitTokens); err != nil {
|
2023-01-05 13:00:34 +00:00
|
|
|
return ethtypes.EthBlock{}, err
|
2022-12-13 17:14:03 +00:00
|
|
|
}
|
|
|
|
|
2023-01-05 13:00:34 +00:00
|
|
|
if err := gw.checkBlkHash(ctx, blkHash); err != nil {
|
|
|
|
return ethtypes.EthBlock{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return gw.target.EthGetBlockByHash(ctx, blkHash, fullTxInfo)
|
2022-12-13 17:14:03 +00:00
|
|
|
}
|
|
|
|
|
2023-01-05 13:00:34 +00:00
|
|
|
func (gw *Node) EthGetBlockByNumber(ctx context.Context, blkNum string, fullTxInfo bool) (ethtypes.EthBlock, error) {
|
2022-12-13 17:14:03 +00:00
|
|
|
if err := gw.limit(ctx, stateRateLimitTokens); err != nil {
|
2023-01-05 13:00:34 +00:00
|
|
|
return ethtypes.EthBlock{}, err
|
|
|
|
}
|
|
|
|
|
2023-03-13 23:36:48 +00:00
|
|
|
if err := gw.checkBlkParam(ctx, blkNum, 0); err != nil {
|
2023-01-05 13:00:34 +00:00
|
|
|
return ethtypes.EthBlock{}, err
|
2022-12-13 17:14:03 +00:00
|
|
|
}
|
|
|
|
|
2023-01-05 13:00:34 +00:00
|
|
|
return gw.target.EthGetBlockByNumber(ctx, blkNum, fullTxInfo)
|
2022-12-13 17:14:03 +00:00
|
|
|
}
|
|
|
|
|
2023-01-05 13:00:34 +00:00
|
|
|
func (gw *Node) EthGetTransactionByHash(ctx context.Context, txHash *ethtypes.EthHash) (*ethtypes.EthTx, error) {
|
2023-03-14 15:45:56 +00:00
|
|
|
return gw.target.EthGetTransactionByHashLimited(ctx, txHash, api.LookbackNoLimit)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gw *Node) EthGetTransactionByHashLimited(ctx context.Context, txHash *ethtypes.EthHash, limit abi.ChainEpoch) (*ethtypes.EthTx, error) {
|
2022-12-13 17:14:03 +00:00
|
|
|
if err := gw.limit(ctx, stateRateLimitTokens); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-03-14 15:45:56 +00:00
|
|
|
if limit == api.LookbackNoLimit {
|
|
|
|
limit = gw.stateWaitLookbackLimit
|
|
|
|
}
|
|
|
|
if gw.stateWaitLookbackLimit != api.LookbackNoLimit && limit > gw.stateWaitLookbackLimit {
|
|
|
|
limit = gw.stateWaitLookbackLimit
|
|
|
|
}
|
2022-12-13 17:14:03 +00:00
|
|
|
|
2023-03-14 15:45:56 +00:00
|
|
|
return gw.target.EthGetTransactionByHashLimited(ctx, txHash, limit)
|
2022-12-13 17:14:03 +00:00
|
|
|
}
|
|
|
|
|
2023-02-02 12:25:50 +00:00
|
|
|
func (gw *Node) EthGetTransactionHashByCid(ctx context.Context, cid cid.Cid) (*ethtypes.EthHash, error) {
|
|
|
|
if err := gw.limit(ctx, stateRateLimitTokens); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return gw.target.EthGetTransactionHashByCid(ctx, cid)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gw *Node) EthGetMessageCidByTransactionHash(ctx context.Context, txHash *ethtypes.EthHash) (*cid.Cid, error) {
|
2023-03-07 17:49:07 +00:00
|
|
|
if err := gw.limit(ctx, chainRateLimitTokens); err != nil {
|
2023-02-02 12:25:50 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return gw.target.EthGetMessageCidByTransactionHash(ctx, txHash)
|
|
|
|
}
|
|
|
|
|
2023-06-21 17:36:04 +00:00
|
|
|
func (gw *Node) EthGetTransactionCount(ctx context.Context, sender ethtypes.EthAddress, blkParam ethtypes.EthBlockNumberOrHash) (ethtypes.EthUint64, error) {
|
2022-12-13 17:14:03 +00:00
|
|
|
if err := gw.limit(ctx, stateRateLimitTokens); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2023-01-05 13:00:34 +00:00
|
|
|
|
2023-06-21 17:36:04 +00:00
|
|
|
if err := gw.checkEthBlockParam(ctx, blkParam, 0); err != nil {
|
2023-01-05 13:00:34 +00:00
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2023-05-03 17:24:36 +00:00
|
|
|
return gw.target.EthGetTransactionCount(ctx, sender, blkParam)
|
2022-12-13 17:14:03 +00:00
|
|
|
}
|
|
|
|
|
2023-01-05 13:00:34 +00:00
|
|
|
func (gw *Node) EthGetTransactionReceipt(ctx context.Context, txHash ethtypes.EthHash) (*api.EthTxReceipt, error) {
|
2023-03-14 15:45:56 +00:00
|
|
|
return gw.EthGetTransactionReceiptLimited(ctx, txHash, api.LookbackNoLimit)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gw *Node) EthGetTransactionReceiptLimited(ctx context.Context, txHash ethtypes.EthHash, limit abi.ChainEpoch) (*api.EthTxReceipt, error) {
|
2022-12-13 17:14:03 +00:00
|
|
|
if err := gw.limit(ctx, stateRateLimitTokens); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-03-14 15:45:56 +00:00
|
|
|
if limit == api.LookbackNoLimit {
|
|
|
|
limit = gw.stateWaitLookbackLimit
|
|
|
|
}
|
|
|
|
if gw.stateWaitLookbackLimit != api.LookbackNoLimit && limit > gw.stateWaitLookbackLimit {
|
|
|
|
limit = gw.stateWaitLookbackLimit
|
|
|
|
}
|
2022-12-13 17:14:03 +00:00
|
|
|
|
2023-03-14 15:45:56 +00:00
|
|
|
return gw.target.EthGetTransactionReceiptLimited(ctx, txHash, limit)
|
2022-12-13 17:14:03 +00:00
|
|
|
}
|
|
|
|
|
2023-06-21 17:36:04 +00:00
|
|
|
func (gw *Node) EthGetCode(ctx context.Context, address ethtypes.EthAddress, blkParam ethtypes.EthBlockNumberOrHash) (ethtypes.EthBytes, error) {
|
2022-12-13 17:14:03 +00:00
|
|
|
if err := gw.limit(ctx, stateRateLimitTokens); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-06-21 17:36:04 +00:00
|
|
|
if err := gw.checkEthBlockParam(ctx, blkParam, 0); err != nil {
|
2023-01-05 13:00:34 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-05-03 17:24:36 +00:00
|
|
|
return gw.target.EthGetCode(ctx, address, blkParam)
|
2022-12-13 17:14:03 +00:00
|
|
|
}
|
|
|
|
|
2023-06-21 17:36:04 +00:00
|
|
|
func (gw *Node) EthGetStorageAt(ctx context.Context, address ethtypes.EthAddress, position ethtypes.EthBytes, blkParam ethtypes.EthBlockNumberOrHash) (ethtypes.EthBytes, error) {
|
2022-12-13 17:14:03 +00:00
|
|
|
if err := gw.limit(ctx, stateRateLimitTokens); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-06-21 17:36:04 +00:00
|
|
|
if err := gw.checkEthBlockParam(ctx, blkParam, 0); err != nil {
|
2023-01-05 13:00:34 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return gw.target.EthGetStorageAt(ctx, address, position, blkParam)
|
2022-12-13 17:14:03 +00:00
|
|
|
}
|
|
|
|
|
2023-06-21 17:36:04 +00:00
|
|
|
func (gw *Node) EthGetBalance(ctx context.Context, address ethtypes.EthAddress, blkParam ethtypes.EthBlockNumberOrHash) (ethtypes.EthBigInt, error) {
|
2022-12-13 17:14:03 +00:00
|
|
|
if err := gw.limit(ctx, stateRateLimitTokens); err != nil {
|
2023-01-05 13:00:34 +00:00
|
|
|
return ethtypes.EthBigInt(big.Zero()), err
|
2022-12-13 17:14:03 +00:00
|
|
|
}
|
|
|
|
|
2023-06-21 17:36:04 +00:00
|
|
|
if err := gw.checkEthBlockParam(ctx, blkParam, 0); err != nil {
|
2023-01-05 13:00:34 +00:00
|
|
|
return ethtypes.EthBigInt(big.Zero()), err
|
|
|
|
}
|
|
|
|
|
|
|
|
return gw.target.EthGetBalance(ctx, address, blkParam)
|
2022-12-13 17:14:03 +00:00
|
|
|
}
|
|
|
|
|
2023-01-05 13:00:34 +00:00
|
|
|
func (gw *Node) EthChainId(ctx context.Context) (ethtypes.EthUint64, error) {
|
2023-03-07 17:49:07 +00:00
|
|
|
if err := gw.limit(ctx, basicRateLimitTokens); err != nil {
|
2022-12-13 17:14:03 +00:00
|
|
|
return 0, err
|
|
|
|
}
|
2023-01-05 13:00:34 +00:00
|
|
|
|
|
|
|
return gw.target.EthChainId(ctx)
|
2022-12-13 17:14:03 +00:00
|
|
|
}
|
|
|
|
|
2023-04-21 12:02:40 +00:00
|
|
|
func (gw *Node) EthSyncing(ctx context.Context) (ethtypes.EthSyncingResult, error) {
|
|
|
|
if err := gw.limit(ctx, basicRateLimitTokens); err != nil {
|
|
|
|
return ethtypes.EthSyncingResult{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return gw.target.EthSyncing(ctx)
|
|
|
|
}
|
|
|
|
|
2022-12-13 17:14:03 +00:00
|
|
|
func (gw *Node) NetVersion(ctx context.Context) (string, error) {
|
|
|
|
if err := gw.limit(ctx, stateRateLimitTokens); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2023-01-05 13:00:34 +00:00
|
|
|
return gw.target.NetVersion(ctx)
|
2022-12-13 17:14:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (gw *Node) NetListening(ctx context.Context) (bool, error) {
|
2023-03-07 17:49:07 +00:00
|
|
|
if err := gw.limit(ctx, basicRateLimitTokens); err != nil {
|
2022-12-13 17:14:03 +00:00
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2023-01-05 13:00:34 +00:00
|
|
|
return gw.target.NetListening(ctx)
|
2022-12-13 17:14:03 +00:00
|
|
|
}
|
|
|
|
|
2023-01-05 13:00:34 +00:00
|
|
|
func (gw *Node) EthProtocolVersion(ctx context.Context) (ethtypes.EthUint64, error) {
|
2022-12-13 17:14:03 +00:00
|
|
|
if err := gw.limit(ctx, stateRateLimitTokens); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2023-01-05 13:00:34 +00:00
|
|
|
|
|
|
|
return gw.target.EthProtocolVersion(ctx)
|
2022-12-13 17:14:03 +00:00
|
|
|
}
|
|
|
|
|
2023-01-05 13:00:34 +00:00
|
|
|
func (gw *Node) EthGasPrice(ctx context.Context) (ethtypes.EthBigInt, error) {
|
2023-03-07 17:49:07 +00:00
|
|
|
if err := gw.limit(ctx, chainRateLimitTokens); err != nil {
|
2023-01-05 13:00:34 +00:00
|
|
|
return ethtypes.EthBigInt(big.Zero()), err
|
2022-12-13 17:14:03 +00:00
|
|
|
}
|
2023-01-05 13:00:34 +00:00
|
|
|
|
|
|
|
return gw.target.EthGasPrice(ctx)
|
2022-12-13 17:14:03 +00:00
|
|
|
}
|
|
|
|
|
2023-01-05 13:00:34 +00:00
|
|
|
var EthFeeHistoryMaxBlockCount = 128 // this seems to be expensive; todo: figure out what is a good number that works with everything
|
|
|
|
|
2023-02-10 18:33:59 +00:00
|
|
|
func (gw *Node) EthFeeHistory(ctx context.Context, p jsonrpc.RawParams) (ethtypes.EthFeeHistory, error) {
|
|
|
|
params, err := jsonrpc.DecodeParams[ethtypes.EthFeeHistoryParams](p)
|
|
|
|
if err != nil {
|
|
|
|
return ethtypes.EthFeeHistory{}, xerrors.Errorf("decoding params: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-12-13 17:14:03 +00:00
|
|
|
if err := gw.limit(ctx, stateRateLimitTokens); err != nil {
|
2023-01-05 13:00:34 +00:00
|
|
|
return ethtypes.EthFeeHistory{}, err
|
2022-12-13 17:14:03 +00:00
|
|
|
}
|
|
|
|
|
2023-03-13 23:36:48 +00:00
|
|
|
if err := gw.checkBlkParam(ctx, params.NewestBlkNum, params.BlkCount); err != nil {
|
2023-01-05 13:00:34 +00:00
|
|
|
return ethtypes.EthFeeHistory{}, err
|
|
|
|
}
|
|
|
|
|
2023-02-10 18:33:59 +00:00
|
|
|
if params.BlkCount > ethtypes.EthUint64(EthFeeHistoryMaxBlockCount) {
|
2023-01-05 13:00:34 +00:00
|
|
|
return ethtypes.EthFeeHistory{}, fmt.Errorf("block count too high")
|
|
|
|
}
|
|
|
|
|
2023-02-10 18:33:59 +00:00
|
|
|
return gw.target.EthFeeHistory(ctx, p)
|
2022-12-13 17:14:03 +00:00
|
|
|
}
|
|
|
|
|
2023-01-05 13:00:34 +00:00
|
|
|
func (gw *Node) EthMaxPriorityFeePerGas(ctx context.Context) (ethtypes.EthBigInt, error) {
|
2023-03-07 17:49:07 +00:00
|
|
|
if err := gw.limit(ctx, chainRateLimitTokens); err != nil {
|
2023-01-05 13:00:34 +00:00
|
|
|
return ethtypes.EthBigInt(big.Zero()), err
|
2022-12-13 17:14:03 +00:00
|
|
|
}
|
2023-01-05 13:00:34 +00:00
|
|
|
|
|
|
|
return gw.target.EthMaxPriorityFeePerGas(ctx)
|
2022-12-13 17:14:03 +00:00
|
|
|
}
|
|
|
|
|
2023-01-05 13:00:34 +00:00
|
|
|
func (gw *Node) EthEstimateGas(ctx context.Context, tx ethtypes.EthCall) (ethtypes.EthUint64, error) {
|
2022-12-13 17:14:03 +00:00
|
|
|
if err := gw.limit(ctx, stateRateLimitTokens); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2023-01-05 13:00:34 +00:00
|
|
|
|
|
|
|
// todo limit gas? to what?
|
|
|
|
return gw.target.EthEstimateGas(ctx, tx)
|
2022-12-13 17:14:03 +00:00
|
|
|
}
|
|
|
|
|
2023-06-21 17:36:04 +00:00
|
|
|
func (gw *Node) EthCall(ctx context.Context, tx ethtypes.EthCall, blkParam ethtypes.EthBlockNumberOrHash) (ethtypes.EthBytes, error) {
|
2022-12-13 17:14:03 +00:00
|
|
|
if err := gw.limit(ctx, stateRateLimitTokens); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-06-21 17:36:04 +00:00
|
|
|
if err := gw.checkEthBlockParam(ctx, blkParam, 0); err != nil {
|
2023-01-05 13:00:34 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// todo limit gas? to what?
|
|
|
|
return gw.target.EthCall(ctx, tx, blkParam)
|
2022-12-13 17:14:03 +00:00
|
|
|
}
|
|
|
|
|
2023-01-05 13:00:34 +00:00
|
|
|
func (gw *Node) EthSendRawTransaction(ctx context.Context, rawTx ethtypes.EthBytes) (ethtypes.EthHash, error) {
|
2022-12-13 17:14:03 +00:00
|
|
|
if err := gw.limit(ctx, stateRateLimitTokens); err != nil {
|
2023-01-05 13:00:34 +00:00
|
|
|
return ethtypes.EthHash{}, err
|
2022-12-13 17:14:03 +00:00
|
|
|
}
|
|
|
|
|
2023-01-05 13:00:34 +00:00
|
|
|
return gw.target.EthSendRawTransaction(ctx, rawTx)
|
2022-12-13 17:14:03 +00:00
|
|
|
}
|
|
|
|
|
2023-01-05 13:00:34 +00:00
|
|
|
func (gw *Node) EthGetLogs(ctx context.Context, filter *ethtypes.EthFilterSpec) (*ethtypes.EthFilterResult, error) {
|
2022-12-13 17:14:03 +00:00
|
|
|
if err := gw.limit(ctx, stateRateLimitTokens); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-01-05 13:00:34 +00:00
|
|
|
if filter.FromBlock != nil {
|
2023-03-13 23:36:48 +00:00
|
|
|
if err := gw.checkBlkParam(ctx, *filter.FromBlock, 0); err != nil {
|
2023-01-05 13:00:34 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if filter.ToBlock != nil {
|
2023-03-13 23:36:48 +00:00
|
|
|
if err := gw.checkBlkParam(ctx, *filter.ToBlock, 0); err != nil {
|
2023-01-05 13:00:34 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if filter.BlockHash != nil {
|
|
|
|
if err := gw.checkBlkHash(ctx, *filter.BlockHash); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return gw.target.EthGetLogs(ctx, filter)
|
2022-12-13 17:14:03 +00:00
|
|
|
}
|
|
|
|
|
2023-01-05 13:00:34 +00:00
|
|
|
/* FILTERS: Those are stateful.. figure out how to properly either bind them to users, or time out? */
|
|
|
|
|
|
|
|
func (gw *Node) EthGetFilterChanges(ctx context.Context, id ethtypes.EthFilterID) (*ethtypes.EthFilterResult, error) {
|
2022-12-13 17:14:03 +00:00
|
|
|
if err := gw.limit(ctx, stateRateLimitTokens); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-01-16 14:28:55 +00:00
|
|
|
ft := statefulCallFromContext(ctx)
|
2023-01-05 13:00:34 +00:00
|
|
|
ft.lk.Lock()
|
|
|
|
_, ok := ft.userFilters[id]
|
|
|
|
ft.lk.Unlock()
|
|
|
|
|
|
|
|
if !ok {
|
2023-08-09 19:12:30 +00:00
|
|
|
return nil, filter.ErrFilterNotFound
|
2023-01-05 13:00:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return gw.target.EthGetFilterChanges(ctx, id)
|
2022-12-13 17:14:03 +00:00
|
|
|
}
|
|
|
|
|
2023-01-05 13:00:34 +00:00
|
|
|
func (gw *Node) EthGetFilterLogs(ctx context.Context, id ethtypes.EthFilterID) (*ethtypes.EthFilterResult, error) {
|
2022-12-13 17:14:03 +00:00
|
|
|
if err := gw.limit(ctx, stateRateLimitTokens); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-01-16 14:28:55 +00:00
|
|
|
ft := statefulCallFromContext(ctx)
|
2023-01-05 13:00:34 +00:00
|
|
|
ft.lk.Lock()
|
|
|
|
_, ok := ft.userFilters[id]
|
|
|
|
ft.lk.Unlock()
|
2022-12-13 17:14:03 +00:00
|
|
|
|
2023-01-05 13:00:34 +00:00
|
|
|
if !ok {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2022-12-13 17:14:03 +00:00
|
|
|
|
2023-01-05 13:00:34 +00:00
|
|
|
return gw.target.EthGetFilterLogs(ctx, id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gw *Node) EthNewFilter(ctx context.Context, filter *ethtypes.EthFilterSpec) (ethtypes.EthFilterID, error) {
|
2022-12-13 17:14:03 +00:00
|
|
|
if err := gw.limit(ctx, stateRateLimitTokens); err != nil {
|
2023-01-05 13:00:34 +00:00
|
|
|
return ethtypes.EthFilterID{}, err
|
2022-12-13 17:14:03 +00:00
|
|
|
}
|
|
|
|
|
2023-01-05 13:00:34 +00:00
|
|
|
return addUserFilterLimited(ctx, func() (ethtypes.EthFilterID, error) {
|
|
|
|
return gw.target.EthNewFilter(ctx, filter)
|
|
|
|
})
|
2022-12-13 17:14:03 +00:00
|
|
|
}
|
|
|
|
|
2023-01-05 13:00:34 +00:00
|
|
|
func (gw *Node) EthNewBlockFilter(ctx context.Context) (ethtypes.EthFilterID, error) {
|
2022-12-13 17:14:03 +00:00
|
|
|
if err := gw.limit(ctx, stateRateLimitTokens); err != nil {
|
2023-01-05 13:00:34 +00:00
|
|
|
return ethtypes.EthFilterID{}, err
|
2022-12-13 17:14:03 +00:00
|
|
|
}
|
|
|
|
|
2023-01-05 13:00:34 +00:00
|
|
|
return addUserFilterLimited(ctx, func() (ethtypes.EthFilterID, error) {
|
|
|
|
return gw.target.EthNewBlockFilter(ctx)
|
|
|
|
})
|
2022-12-13 17:14:03 +00:00
|
|
|
}
|
|
|
|
|
2023-01-05 13:00:34 +00:00
|
|
|
func (gw *Node) EthNewPendingTransactionFilter(ctx context.Context) (ethtypes.EthFilterID, error) {
|
2022-12-13 17:14:03 +00:00
|
|
|
if err := gw.limit(ctx, stateRateLimitTokens); err != nil {
|
2023-01-05 13:00:34 +00:00
|
|
|
return ethtypes.EthFilterID{}, err
|
2022-12-13 17:14:03 +00:00
|
|
|
}
|
|
|
|
|
2023-01-05 13:00:34 +00:00
|
|
|
return addUserFilterLimited(ctx, func() (ethtypes.EthFilterID, error) {
|
|
|
|
return gw.target.EthNewPendingTransactionFilter(ctx)
|
|
|
|
})
|
2022-12-13 17:14:03 +00:00
|
|
|
}
|
|
|
|
|
2023-01-05 13:00:34 +00:00
|
|
|
func (gw *Node) EthUninstallFilter(ctx context.Context, id ethtypes.EthFilterID) (bool, error) {
|
2022-12-13 17:14:03 +00:00
|
|
|
if err := gw.limit(ctx, stateRateLimitTokens); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2023-01-05 13:00:34 +00:00
|
|
|
// check if the filter belongs to this connection
|
2023-01-16 14:28:55 +00:00
|
|
|
ft := statefulCallFromContext(ctx)
|
2023-01-05 13:00:34 +00:00
|
|
|
ft.lk.Lock()
|
|
|
|
defer ft.lk.Unlock()
|
2022-12-13 17:14:03 +00:00
|
|
|
|
2023-01-05 13:00:34 +00:00
|
|
|
if _, ok := ft.userFilters[id]; !ok {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ok, err := gw.target.EthUninstallFilter(ctx, id)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
2022-12-13 17:14:03 +00:00
|
|
|
}
|
|
|
|
|
2023-01-05 13:00:34 +00:00
|
|
|
delete(ft.userFilters, id)
|
|
|
|
return ok, nil
|
2022-12-13 17:14:03 +00:00
|
|
|
}
|
|
|
|
|
2023-01-31 10:00:15 +00:00
|
|
|
func (gw *Node) EthSubscribe(ctx context.Context, p jsonrpc.RawParams) (ethtypes.EthSubscriptionID, error) {
|
|
|
|
// validate params
|
|
|
|
_, err := jsonrpc.DecodeParams[ethtypes.EthSubscribeParams](p)
|
|
|
|
if err != nil {
|
|
|
|
return ethtypes.EthSubscriptionID{}, xerrors.Errorf("decoding params: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-01-16 14:28:55 +00:00
|
|
|
if err := gw.limit(ctx, stateRateLimitTokens); err != nil {
|
|
|
|
return ethtypes.EthSubscriptionID{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if gw.subHnd == nil {
|
|
|
|
return ethtypes.EthSubscriptionID{}, xerrors.New("subscription support not enabled")
|
|
|
|
}
|
|
|
|
|
|
|
|
ethCb, ok := jsonrpc.ExtractReverseClient[api.EthSubscriberMethods](ctx)
|
|
|
|
if !ok {
|
|
|
|
return ethtypes.EthSubscriptionID{}, xerrors.Errorf("connection doesn't support callbacks")
|
|
|
|
}
|
|
|
|
|
|
|
|
ft := statefulCallFromContext(ctx)
|
|
|
|
ft.lk.Lock()
|
|
|
|
defer ft.lk.Unlock()
|
|
|
|
|
|
|
|
if len(ft.userSubscriptions) >= EthMaxFiltersPerConn {
|
|
|
|
return ethtypes.EthSubscriptionID{}, fmt.Errorf("too many subscriptions")
|
|
|
|
}
|
|
|
|
|
2023-01-31 10:00:15 +00:00
|
|
|
sub, err := gw.target.EthSubscribe(ctx, p)
|
2023-01-16 14:28:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return ethtypes.EthSubscriptionID{}, err
|
|
|
|
}
|
|
|
|
|
2023-01-26 14:20:49 +00:00
|
|
|
err = gw.subHnd.AddSub(ctx, sub, func(ctx context.Context, response *ethtypes.EthSubscriptionResponse) error {
|
2023-01-16 14:28:55 +00:00
|
|
|
outParam, err := json.Marshal(response)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ethCb.EthSubscription(ctx, outParam)
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return ethtypes.EthSubscriptionID{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ft.userSubscriptions[sub] = time.Now()
|
|
|
|
|
|
|
|
return sub, err
|
2023-01-05 13:00:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (gw *Node) EthUnsubscribe(ctx context.Context, id ethtypes.EthSubscriptionID) (bool, error) {
|
2023-01-16 14:28:55 +00:00
|
|
|
if err := gw.limit(ctx, stateRateLimitTokens); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if the filter belongs to this connection
|
|
|
|
ft := statefulCallFromContext(ctx)
|
|
|
|
ft.lk.Lock()
|
|
|
|
defer ft.lk.Unlock()
|
|
|
|
|
|
|
|
if _, ok := ft.userSubscriptions[id]; !ok {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ok, err := gw.target.EthUnsubscribe(ctx, id)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(ft.userSubscriptions, id)
|
|
|
|
|
|
|
|
if gw.subHnd != nil {
|
2023-01-26 14:20:49 +00:00
|
|
|
gw.subHnd.RemoveSub(id)
|
2023-01-16 14:28:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ok, nil
|
2023-01-05 13:00:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var EthMaxFiltersPerConn = 16 // todo make this configurable
|
|
|
|
|
|
|
|
func addUserFilterLimited(ctx context.Context, cb func() (ethtypes.EthFilterID, error)) (ethtypes.EthFilterID, error) {
|
2023-01-16 14:28:55 +00:00
|
|
|
ft := statefulCallFromContext(ctx)
|
2023-01-05 13:00:34 +00:00
|
|
|
ft.lk.Lock()
|
|
|
|
defer ft.lk.Unlock()
|
|
|
|
|
|
|
|
if len(ft.userFilters) >= EthMaxFiltersPerConn {
|
|
|
|
return ethtypes.EthFilterID{}, fmt.Errorf("too many filters")
|
|
|
|
}
|
|
|
|
|
|
|
|
id, err := cb()
|
|
|
|
if err != nil {
|
|
|
|
return id, err
|
2022-12-13 17:14:03 +00:00
|
|
|
}
|
|
|
|
|
2023-01-05 13:00:34 +00:00
|
|
|
ft.userFilters[id] = time.Now()
|
|
|
|
|
|
|
|
return id, nil
|
|
|
|
}
|
|
|
|
|
2023-01-16 14:28:55 +00:00
|
|
|
func statefulCallFromContext(ctx context.Context) *statefulCallTracker {
|
|
|
|
return ctx.Value(statefulCallTrackerKey).(*statefulCallTracker)
|
2023-01-05 13:00:34 +00:00
|
|
|
}
|
|
|
|
|
2023-01-16 14:28:55 +00:00
|
|
|
type statefulCallTracker struct {
|
2023-01-05 13:00:34 +00:00
|
|
|
lk sync.Mutex
|
|
|
|
|
2023-01-16 14:28:55 +00:00
|
|
|
userFilters map[ethtypes.EthFilterID]time.Time
|
|
|
|
userSubscriptions map[ethtypes.EthSubscriptionID]time.Time
|
2023-01-05 13:00:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// called per request (ws connection)
|
2023-01-16 14:28:55 +00:00
|
|
|
func newStatefulCallTracker() *statefulCallTracker {
|
|
|
|
return &statefulCallTracker{
|
|
|
|
userFilters: make(map[ethtypes.EthFilterID]time.Time),
|
|
|
|
userSubscriptions: make(map[ethtypes.EthSubscriptionID]time.Time),
|
2023-01-05 13:00:34 +00:00
|
|
|
}
|
2022-12-13 17:14:03 +00:00
|
|
|
}
|