Merge pull request #10180 from filecoin-project/feat/ethsub-addr

feat: ethrpc: Support filtering by address in subscribe
This commit is contained in:
Łukasz Magiera 2023-02-02 18:23:43 +01:00 committed by GitHub
commit 0c33eab20a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 81 additions and 18 deletions

View File

@ -660,6 +660,12 @@ type EthSubscriptionParams struct {
// List of topics to be matched. // List of topics to be matched.
// Optional, default: empty list // Optional, default: empty list
Topics EthTopicSpec `json:"topics,omitempty"` Topics EthTopicSpec `json:"topics,omitempty"`
// Actor address or a list of addresses from which event logs should originate.
// Optional, default nil.
// The JSON decoding must treat a string as equivalent to an array with one value, for example
// "0x8888f1f195afa192cfee86069858" must be decoded as [ "0x8888f1f195afa192cfee86069858" ]
Address EthAddressList `json:"address"`
} }
type EthSubscriptionResponse struct { type EthSubscriptionResponse struct {

View File

@ -7,9 +7,9 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/chain/types/ethtypes" "github.com/filecoin-project/lotus/chain/types/ethtypes"
"github.com/filecoin-project/lotus/itests/kit" "github.com/filecoin-project/lotus/itests/kit"
"github.com/filecoin-project/lotus/node/impl/full"
) )
func TestEthFilterAPIDisabledViaConfig(t *testing.T) { func TestEthFilterAPIDisabledViaConfig(t *testing.T) {
@ -22,41 +22,41 @@ func TestEthFilterAPIDisabledViaConfig(t *testing.T) {
_, err := client.EthNewPendingTransactionFilter(ctx) _, err := client.EthNewPendingTransactionFilter(ctx)
require.NotNil(t, err) require.NotNil(t, err)
require.Equal(t, err.Error(), api.ErrNotSupported.Error()) require.Equal(t, err.Error(), full.ErrModuleDisabled.Error())
_, err = client.EthGetLogs(ctx, &ethtypes.EthFilterSpec{}) _, err = client.EthGetLogs(ctx, &ethtypes.EthFilterSpec{})
require.NotNil(t, err) require.NotNil(t, err)
require.Equal(t, err.Error(), api.ErrNotSupported.Error()) require.Equal(t, err.Error(), full.ErrModuleDisabled.Error())
_, err = client.EthGetFilterChanges(ctx, ethtypes.EthFilterID{}) _, err = client.EthGetFilterChanges(ctx, ethtypes.EthFilterID{})
require.NotNil(t, err) require.NotNil(t, err)
require.Equal(t, err.Error(), api.ErrNotSupported.Error()) require.Equal(t, err.Error(), full.ErrModuleDisabled.Error())
_, err = client.EthGetFilterLogs(ctx, ethtypes.EthFilterID{}) _, err = client.EthGetFilterLogs(ctx, ethtypes.EthFilterID{})
require.NotNil(t, err) require.NotNil(t, err)
require.Equal(t, err.Error(), api.ErrNotSupported.Error()) require.Equal(t, err.Error(), full.ErrModuleDisabled.Error())
_, err = client.EthNewFilter(ctx, &ethtypes.EthFilterSpec{}) _, err = client.EthNewFilter(ctx, &ethtypes.EthFilterSpec{})
require.NotNil(t, err) require.NotNil(t, err)
require.Equal(t, err.Error(), api.ErrNotSupported.Error()) require.Equal(t, err.Error(), full.ErrModuleDisabled.Error())
_, err = client.EthNewBlockFilter(ctx) _, err = client.EthNewBlockFilter(ctx)
require.NotNil(t, err) require.NotNil(t, err)
require.Equal(t, err.Error(), api.ErrNotSupported.Error()) require.Equal(t, err.Error(), full.ErrModuleDisabled.Error())
_, err = client.EthNewPendingTransactionFilter(ctx) _, err = client.EthNewPendingTransactionFilter(ctx)
require.NotNil(t, err) require.NotNil(t, err)
require.Equal(t, err.Error(), api.ErrNotSupported.Error()) require.Equal(t, err.Error(), full.ErrModuleDisabled.Error())
_, err = client.EthUninstallFilter(ctx, ethtypes.EthFilterID{}) _, err = client.EthUninstallFilter(ctx, ethtypes.EthFilterID{})
require.NotNil(t, err) require.NotNil(t, err)
require.Equal(t, err.Error(), api.ErrNotSupported.Error()) require.Equal(t, err.Error(), full.ErrModuleDisabled.Error())
_, err = client.EthSubscribe(ctx, "newHeads", nil) _, err = client.EthSubscribe(ctx, []byte("{}"))
require.NotNil(t, err) require.NotNil(t, err)
require.Equal(t, err.Error(), api.ErrNotSupported.Error()) require.Equal(t, err.Error(), full.ErrModuleDisabled.Error())
_, err = client.EthUnsubscribe(ctx, ethtypes.EthSubscriptionID{}) _, err = client.EthUnsubscribe(ctx, ethtypes.EthSubscriptionID{})
require.NotNil(t, err) require.NotNil(t, err)
require.Equal(t, err.Error(), api.ErrNotSupported.Error()) require.Equal(t, err.Error(), full.ErrModuleDisabled.Error())
} }

View File

@ -575,7 +575,10 @@ func TestEthSubscribeLogs(t *testing.T) {
// subscribe to topics in filter // subscribe to topics in filter
subParam, err := json.Marshal(ethtypes.EthSubscribeParams{ subParam, err := json.Marshal(ethtypes.EthSubscribeParams{
EventType: "logs", EventType: "logs",
Params: &ethtypes.EthSubscriptionParams{Topics: tc.spec.Topics}, Params: &ethtypes.EthSubscriptionParams{
Topics: tc.spec.Topics,
Address: tc.spec.Address,
},
}) })
require.NoError(err) require.NoError(err)

View File

@ -261,10 +261,14 @@ func ConfigFullNode(c interface{}) Option {
// in lite-mode Eth api is provided by gateway // in lite-mode Eth api is provided by gateway
ApplyIf(isFullNode, ApplyIf(isFullNode,
If(cfg.Fevm.EnableEthRPC,
Override(new(full.EthModuleAPI), modules.EthModuleAPI(cfg.Fevm)),
Override(new(full.EthEventAPI), modules.EthEventAPI(cfg.Fevm)), Override(new(full.EthEventAPI), modules.EthEventAPI(cfg.Fevm)),
),
If(cfg.Fevm.EnableEthRPC, Override(new(full.EthModuleAPI), modules.EthModuleAPI(cfg.Fevm))), If(!cfg.Fevm.EnableEthRPC,
If(!cfg.Fevm.EnableEthRPC, Override(new(full.EthModuleAPI), &full.EthModuleDummy{})), Override(new(full.EthModuleAPI), &full.EthModuleDummy{}),
Override(new(full.EthEventAPI), &full.EthModuleDummy{}),
),
), ),
) )
} }

View File

@ -6,6 +6,8 @@ import (
"github.com/ipfs/go-cid" "github.com/ipfs/go-cid"
"github.com/filecoin-project/go-jsonrpc"
"github.com/filecoin-project/lotus/api" "github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/chain/types/ethtypes" "github.com/filecoin-project/lotus/chain/types/ethtypes"
) )
@ -122,4 +124,41 @@ func (e *EthModuleDummy) Web3ClientVersion(ctx context.Context) (string, error)
return "", ErrModuleDisabled return "", ErrModuleDisabled
} }
func (e *EthModuleDummy) EthGetLogs(ctx context.Context, filter *ethtypes.EthFilterSpec) (*ethtypes.EthFilterResult, error) {
return &ethtypes.EthFilterResult{}, ErrModuleDisabled
}
func (e *EthModuleDummy) EthGetFilterChanges(ctx context.Context, id ethtypes.EthFilterID) (*ethtypes.EthFilterResult, error) {
return &ethtypes.EthFilterResult{}, ErrModuleDisabled
}
func (e *EthModuleDummy) EthGetFilterLogs(ctx context.Context, id ethtypes.EthFilterID) (*ethtypes.EthFilterResult, error) {
return &ethtypes.EthFilterResult{}, ErrModuleDisabled
}
func (e *EthModuleDummy) EthNewFilter(ctx context.Context, filter *ethtypes.EthFilterSpec) (ethtypes.EthFilterID, error) {
return ethtypes.EthFilterID{}, ErrModuleDisabled
}
func (e *EthModuleDummy) EthNewBlockFilter(ctx context.Context) (ethtypes.EthFilterID, error) {
return ethtypes.EthFilterID{}, ErrModuleDisabled
}
func (e *EthModuleDummy) EthNewPendingTransactionFilter(ctx context.Context) (ethtypes.EthFilterID, error) {
return ethtypes.EthFilterID{}, ErrModuleDisabled
}
func (e *EthModuleDummy) EthUninstallFilter(ctx context.Context, id ethtypes.EthFilterID) (bool, error) {
return false, ErrModuleDisabled
}
func (e *EthModuleDummy) EthSubscribe(ctx context.Context, params jsonrpc.RawParams) (ethtypes.EthSubscriptionID, error) {
return ethtypes.EthSubscriptionID{}, ErrModuleDisabled
}
func (e *EthModuleDummy) EthUnsubscribe(ctx context.Context, id ethtypes.EthSubscriptionID) (bool, error) {
return false, ErrModuleDisabled
}
var _ EthModuleAPI = &EthModuleDummy{} var _ EthModuleAPI = &EthModuleDummy{}
var _ EthEventAPI = &EthModuleDummy{}

View File

@ -1147,7 +1147,18 @@ func (e *EthEvent) EthSubscribe(ctx context.Context, p jsonrpc.RawParams) (ethty
} }
} }
f, err := e.EventFilterManager.Install(ctx, -1, -1, cid.Undef, []address.Address{}, keys) var addresses []address.Address
if params.Params != nil {
for _, ea := range params.Params.Address {
a, err := ea.ToFilecoinAddress()
if err != nil {
return ethtypes.EthSubscriptionID{}, xerrors.Errorf("invalid address %x", ea)
}
addresses = append(addresses, a)
}
}
f, err := e.EventFilterManager.Install(ctx, -1, -1, cid.Undef, addresses, keys)
if err != nil { if err != nil {
// clean up any previous filters added and stop the sub // clean up any previous filters added and stop the sub
_, _ = e.EthUnsubscribe(ctx, sub.id) _, _ = e.EthUnsubscribe(ctx, sub.id)