Skip to content

Commit

Permalink
fixup! chore(eth): refactor eth API module into separate pieces in ne…
Browse files Browse the repository at this point in the history
…w pkg
  • Loading branch information
rvagg committed Dec 19, 2024
1 parent 83a1f1a commit 75b2534
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 82 deletions.
2 changes: 1 addition & 1 deletion api/api_full.go
Original file line number Diff line number Diff line change
Expand Up @@ -768,7 +768,7 @@ type FullNode interface {
// MethodGroup: Eth
// These methods are used for Ethereum-compatible JSON-RPC calls
//
// # Execution model reconciliation
// ### Execution model reconciliation
//
// Ethereum relies on an immediate block-based execution model. The block that includes
// a transaction is also the block that executes it. Each block specifies the state root
Expand Down
2 changes: 1 addition & 1 deletion documentation/en/api-v1-unstable-methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -1326,7 +1326,7 @@ Response: `{}`
## Eth
These methods are used for Ethereum-compatible JSON-RPC calls

# Execution model reconciliation
### Execution model reconciliation

Ethereum relies on an immediate block-based execution model. The block that includes
a transaction is also the block that executes it. Each block specifies the state root
Expand Down
9 changes: 4 additions & 5 deletions node/impl/eth/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ package eth

import (
"context"
"errors"
"fmt"
"strconv"

"github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/build"
"github.com/filecoin-project/lotus/build/buildconstants"
"github.com/filecoin-project/lotus/chain/types/ethtypes"
"golang.org/x/xerrors"
)

type EthBasic interface {
Expand Down Expand Up @@ -69,11 +68,11 @@ func (e *ethBasic) EthProtocolVersion(ctx context.Context) (ethtypes.EthUint64,
func (e *ethBasic) EthSyncing(ctx context.Context) (ethtypes.EthSyncingResult, error) {
state, err := e.syncAPI.SyncState(ctx)
if err != nil {
return ethtypes.EthSyncingResult{}, fmt.Errorf("failed calling SyncState: %w", err)
return ethtypes.EthSyncingResult{}, xerrors.Errorf("failed calling SyncState: %w", err)
}

if len(state.ActiveSyncs) == 0 {
return ethtypes.EthSyncingResult{}, errors.New("no active syncs, try again")
return ethtypes.EthSyncingResult{}, xerrors.New("no active syncs, try again")
}

working := -1
Expand All @@ -89,7 +88,7 @@ func (e *ethBasic) EthSyncing(ctx context.Context) (ethtypes.EthSyncingResult, e

ss := state.ActiveSyncs[working]
if ss.Base == nil || ss.Target == nil {
return ethtypes.EthSyncingResult{}, errors.New("missing syncing information, try again")
return ethtypes.EthSyncingResult{}, xerrors.New("missing syncing information, try again")
}

res := ethtypes.EthSyncingResult{
Expand Down
18 changes: 9 additions & 9 deletions node/impl/eth/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ func (e *ethEvents) EthGetFilterChanges(ctx context.Context, id ethtypes.EthFilt
return ethFilterResultFromMessages(fc.TakeCollectedMessages(ctx))
}

return nil, xerrors.Errorf("unknown filter type")
return nil, xerrors.New("unknown filter type")
}

func (e *ethEvents) EthGetFilterLogs(ctx context.Context, id ethtypes.EthFilterID) (*ethtypes.EthFilterResult, error) {
Expand All @@ -247,7 +247,7 @@ func (e *ethEvents) EthGetFilterLogs(ctx context.Context, id ethtypes.EthFilterI
return ethFilterResultFromEvents(ctx, fc.TakeCollectedEvents(ctx), e.chainStore, e.stateManager)
}

return nil, xerrors.Errorf("wrong filter type")
return nil, xerrors.New("wrong filter type")
}

func (e *ethEvents) EthSubscribe(ctx context.Context, p jsonrpc.RawParams) (ethtypes.EthSubscriptionID, error) {
Expand All @@ -262,7 +262,7 @@ func (e *ethEvents) EthSubscribe(ctx context.Context, p jsonrpc.RawParams) (etht

ethCb, ok := jsonrpc.ExtractReverseClient[api.EthSubscriberMethods](ctx)
if !ok {
return ethtypes.EthSubscriptionID{}, xerrors.Errorf("connection doesn't support callbacks")
return ethtypes.EthSubscriptionID{}, xerrors.New("connection doesn't support callbacks")
}

sub, err := e.subscriptionManager.StartSubscription(e.subscribtionCtx, ethCb.EthSubscription, e.uninstallFilter)
Expand Down Expand Up @@ -615,11 +615,11 @@ func parseBlockRange(heaviest abi.ChainEpoch, fromBlock, toBlock *string, maxRan
minHeight = 0
} else {
if !strings.HasPrefix(*fromBlock, "0x") {
return 0, 0, xerrors.Errorf("FromBlock is not a hex")
return 0, 0, xerrors.New("FromBlock is not a hex")
}
epoch, err := ethtypes.EthUint64FromHex(*fromBlock)
if err != nil {
return 0, 0, xerrors.Errorf("invalid epoch")
return 0, 0, xerrors.New("invalid epoch")
}
minHeight = abi.ChainEpoch(epoch)

Check failure

Code scanning / CodeQL

Incorrect conversion between integer types High

Incorrect conversion of an unsigned 64-bit integer from
strconv.ParseUint
to a lower bit size type int64 without an upper bound check.
}
Expand All @@ -631,11 +631,11 @@ func parseBlockRange(heaviest abi.ChainEpoch, fromBlock, toBlock *string, maxRan
maxHeight = 0
} else {
if !strings.HasPrefix(*toBlock, "0x") {
return 0, 0, xerrors.Errorf("ToBlock is not a hex")
return 0, 0, xerrors.New("ToBlock is not a hex")
}
epoch, err := ethtypes.EthUint64FromHex(*toBlock)
if err != nil {
return 0, 0, xerrors.Errorf("invalid epoch")
return 0, 0, xerrors.New("invalid epoch")
}
maxHeight = abi.ChainEpoch(epoch)

Check failure

Code scanning / CodeQL

Incorrect conversion between integer types High

Incorrect conversion of an unsigned 64-bit integer from
strconv.ParseUint
to a lower bit size type int64 without an upper bound check.
}
Expand Down Expand Up @@ -680,7 +680,7 @@ func (e *ethEvents) parseEthFilterSpec(filterSpec *ethtypes.EthFilterSpec) (*par

if filterSpec.BlockHash != nil {
if filterSpec.FromBlock != nil || filterSpec.ToBlock != nil {
return nil, xerrors.Errorf("must not specify block hash and from/to block")
return nil, xerrors.New("must not specify block hash and from/to block")
}

tipsetCid = filterSpec.BlockHash.ToCid()
Expand Down Expand Up @@ -747,7 +747,7 @@ func (e *ethEvents) uninstallFilter(ctx context.Context, f filter.Filter) error
return err
}
default:
return xerrors.Errorf("unknown filter type")
return xerrors.New("unknown filter type")
}

return e.filterStore.Remove(ctx, f.ID())
Expand Down
11 changes: 5 additions & 6 deletions node/impl/eth/gas.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"context"
"errors"
"fmt"
"os"
"sort"

Expand Down Expand Up @@ -81,21 +80,21 @@ func (e *ethGas) EthFeeHistory(ctx context.Context, p jsonrpc.RawParams) (ethtyp
return ethtypes.EthFeeHistory{}, xerrors.Errorf("decoding params: %w", err)
}
if params.BlkCount > 1024 {
return ethtypes.EthFeeHistory{}, fmt.Errorf("block count should be smaller than 1024")
return ethtypes.EthFeeHistory{}, xerrors.New("block count should be smaller than 1024")
}
rewardPercentiles := make([]float64, 0)
if params.RewardPercentiles != nil {
if len(*params.RewardPercentiles) > maxEthFeeHistoryRewardPercentiles {
return ethtypes.EthFeeHistory{}, errors.New("length of the reward percentile array cannot be greater than 100")
return ethtypes.EthFeeHistory{}, xerrors.New("length of the reward percentile array cannot be greater than 100")
}
rewardPercentiles = append(rewardPercentiles, *params.RewardPercentiles...)
}
for i, rp := range rewardPercentiles {
if rp < 0 || rp > 100 {
return ethtypes.EthFeeHistory{}, fmt.Errorf("invalid reward percentile: %f should be between 0 and 100", rp)
return ethtypes.EthFeeHistory{}, xerrors.Errorf("invalid reward percentile: %f should be between 0 and 100", rp)
}
if i > 0 && rp < rewardPercentiles[i-1] {
return ethtypes.EthFeeHistory{}, fmt.Errorf("invalid reward percentile: %f should be larger than %f", rp, rewardPercentiles[i-1])
return ethtypes.EthFeeHistory{}, xerrors.Errorf("invalid reward percentile: %f should be larger than %f", rp, rewardPercentiles[i-1])
}
}

Expand Down Expand Up @@ -147,7 +146,7 @@ func (e *ethGas) EthFeeHistory(ctx context.Context, p jsonrpc.RawParams) (ethtyp
parentTsKey := ts.Parents()
ts, err = e.chainStore.LoadTipSet(ctx, parentTsKey)
if err != nil {
return ethtypes.EthFeeHistory{}, fmt.Errorf("cannot load tipset key: %v", parentTsKey)
return ethtypes.EthFeeHistory{}, xerrors.Errorf("cannot load tipset key: %v", parentTsKey)
}
}

Expand Down
23 changes: 11 additions & 12 deletions node/impl/eth/lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"context"
"errors"
"fmt"

"golang.org/x/xerrors"

Expand Down Expand Up @@ -69,7 +68,7 @@ func (e *ethLookup) EthGetCode(ctx context.Context, ethAddr ethtypes.EthAddress,

// StateManager.Call will panic if there is no parent
if ts.Height() == 0 {
return nil, xerrors.Errorf("block param must not specify genesis block")
return nil, xerrors.New("block param must not specify genesis block")
}

actor, err := e.stateManager.LoadActor(ctx, to, ts)
Expand Down Expand Up @@ -115,7 +114,7 @@ func (e *ethLookup) EthGetCode(ctx context.Context, ethAddr ethtypes.EthAddress,
}

if res.MsgRct == nil {
return nil, fmt.Errorf("no message receipt")
return nil, xerrors.New("no message receipt")
}

if res.MsgRct.ExitCode.IsError() {
Expand All @@ -124,7 +123,7 @@ func (e *ethLookup) EthGetCode(ctx context.Context, ethAddr ethtypes.EthAddress,

var getBytecodeReturn evm.GetBytecodeReturn
if err := getBytecodeReturn.UnmarshalCBOR(bytes.NewReader(res.MsgRct.Return)); err != nil {
return nil, fmt.Errorf("failed to decode EVM bytecode CID: %w", err)
return nil, xerrors.Errorf("failed to decode EVM bytecode CID: %w", err)
}

// The contract has selfdestructed, so the code is "empty".
Expand All @@ -134,7 +133,7 @@ func (e *ethLookup) EthGetCode(ctx context.Context, ethAddr ethtypes.EthAddress,

blk, err := e.stateBlockstore.Get(ctx, *getBytecodeReturn.Cid)
if err != nil {
return nil, fmt.Errorf("failed to get EVM bytecode: %w", err)
return nil, xerrors.Errorf("failed to get EVM bytecode: %w", err)
}

return blk.RawData(), nil
Expand All @@ -148,7 +147,7 @@ func (e *ethLookup) EthGetStorageAt(ctx context.Context, ethAddr ethtypes.EthAdd

pl := len(position)
if pl > 32 {
return nil, fmt.Errorf("supplied storage key is too long")
return nil, xerrors.New("supplied storage key is too long")
}

// pad with zero bytes if smaller than 32 bytes
Expand All @@ -162,7 +161,7 @@ func (e *ethLookup) EthGetStorageAt(ctx context.Context, ethAddr ethtypes.EthAdd
// 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)
return nil, xerrors.Errorf("failed to construct system sender address: %w", err)
}

actor, err := e.stateManager.LoadActor(ctx, to, ts)
Expand All @@ -181,7 +180,7 @@ func (e *ethLookup) EthGetStorageAt(ctx context.Context, ethAddr ethtypes.EthAdd
StorageKey: *(*[32]byte)(position),
})
if err != nil {
return nil, fmt.Errorf("failed to serialize parameters: %w", err)
return nil, xerrors.Errorf("failed to serialize parameters: %w", err)
}

msg := &types.Message{
Expand Down Expand Up @@ -213,7 +212,7 @@ func (e *ethLookup) EthGetStorageAt(ctx context.Context, ethAddr ethtypes.EthAdd
}

if res.MsgRct == nil {
return nil, xerrors.Errorf("no message receipt")
return nil, xerrors.New("no message receipt")
}

if res.MsgRct.ExitCode.IsError() {
Expand Down Expand Up @@ -264,11 +263,11 @@ func (e *ethLookup) EthChainId(ctx context.Context) (ethtypes.EthUint64, error)
func (e *ethLookup) EthSyncing(ctx context.Context) (ethtypes.EthSyncingResult, error) {
state, err := e.syncAPI.SyncState(ctx)
if err != nil {
return ethtypes.EthSyncingResult{}, fmt.Errorf("failed calling SyncState: %w", err)
return ethtypes.EthSyncingResult{}, xerrors.Errorf("failed calling SyncState: %w", err)
}

if len(state.ActiveSyncs) == 0 {
return ethtypes.EthSyncingResult{}, errors.New("no active syncs, try again")
return ethtypes.EthSyncingResult{}, xerrors.New("no active syncs, try again")
}

working := -1
Expand All @@ -284,7 +283,7 @@ func (e *ethLookup) EthSyncing(ctx context.Context) (ethtypes.EthSyncingResult,

ss := state.ActiveSyncs[working]
if ss.Base == nil || ss.Target == nil {
return ethtypes.EthSyncingResult{}, errors.New("missing syncing information, try again")
return ethtypes.EthSyncingResult{}, xerrors.New("missing syncing information, try again")
}

res := ethtypes.EthSyncingResult{
Expand Down
2 changes: 1 addition & 1 deletion node/impl/eth/subscriptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (e *EthSubscriptionManager) StopSubscription(ctx context.Context, id ethtyp

sub, ok := e.subs[id]
if !ok {
return xerrors.Errorf("subscription not found")
return xerrors.New("subscription not found")
}
sub.stop()
delete(e.subs, id)
Expand Down
Loading

0 comments on commit 75b2534

Please sign in to comment.