feat: ethrpc: Support filtering by address in subscribe

This commit is contained in:
Łukasz Magiera 2023-02-02 14:26:56 +01:00
parent 3d207de6aa
commit fe1e0974cb
4 changed files with 21 additions and 3 deletions

View File

@ -660,6 +660,12 @@ type EthSubscriptionParams struct {
// List of topics to be matched.
// Optional, default: empty list
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 {

View File

@ -52,7 +52,7 @@ func TestEthFilterAPIDisabledViaConfig(t *testing.T) {
require.NotNil(t, err)
require.Equal(t, err.Error(), api.ErrNotSupported.Error())
_, err = client.EthSubscribe(ctx, "newHeads", nil)
_, err = client.EthSubscribe(ctx, []byte{})
require.NotNil(t, err)
require.Equal(t, err.Error(), api.ErrNotSupported.Error())

View File

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

View File

@ -1147,7 +1147,16 @@ 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
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 {
// clean up any previous filters added and stop the sub
_, _ = e.EthUnsubscribe(ctx, sub.id)