Skip to content

Commit

Permalink
review
Browse files Browse the repository at this point in the history
  • Loading branch information
aarshkshah1992 committed Feb 7, 2024
1 parent 4cfd4f0 commit 8e71abf
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 45 deletions.
10 changes: 4 additions & 6 deletions api/api_full.go
Original file line number Diff line number Diff line change
Expand Up @@ -888,17 +888,15 @@ type FullNode interface {

// Actor events

// GetActorEvents returns all FEVM Actor events that match the given filter.
// This is a request/response API. Please see the docs for `ActorEventFilter` for a detailed
// description of the filter options.
// GetActorEvents returns all FVM and built-in Actor events that match the given filter.
// This is a request/response API.
GetActorEvents(ctx context.Context, filter *types.ActorEventFilter) ([]*types.ActorEvent, error) //perm:read

// SubscribeActorEvents returns a long-lived stream of all FEVM Actor events that match the given filter.
// Events that match the given filter are written to the stream in real-time as they are emitted from the FEVM.
// SubscribeActorEvents returns a long-lived stream of all FVM and built-in Actor events that match the given filter.
// Events that match the given filter are written to the stream in real-time as they are emitted from the FVM.
// The response stream is closed when the client disconnects or if there is an error while writing an event to the stream.
// This API also allows clients to read all historical events matching the given filter before
// any real-time events are written to the response stream.
// Please see the docs for `SubActorEventFilter` for a detailed description of the filter options.
// NOTE: THIS API IS ONLY SUPPORTED OVER WEBSOCKETS FOR NOW
SubscribeActorEvents(ctx context.Context, filter *types.SubActorEventFilter) (<-chan *types.ActorEvent, error) //perm:read
}
Expand Down
8 changes: 4 additions & 4 deletions api/docgen/docgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,8 +430,8 @@ func init() {
},
},
},
FromBlock: pstring("2301220"),
ToBlock: pstring("latest"),
FromEpoch: "earliest",
ToEpoch: "latest",
})

addExample(&types.SubActorEventFilter{
Expand All @@ -445,8 +445,8 @@ func init() {
},
},
},
FromBlock: pstring("2301220"),
ToBlock: pstring("latest"),
FromEpoch: "earliest",
ToEpoch: "latest",
},
Prefill: true,
})
Expand Down
Binary file modified build/openrpc/full.json.gz
Binary file not shown.
Binary file modified build/openrpc/gateway.json.gz
Binary file not shown.
8 changes: 5 additions & 3 deletions chain/types/actor_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ type SubActorEventFilter struct {

// If true, all available matching historical events will be written to the response stream
// before any new real-time events that match the given filter are written.
// If `Prefill` is true and `FromEpoch` is set to latest, the pre-fill operation will become a no-op.
// if `Prefill` is false and `FromEpoch` is set to earliest, historical events will still be sent to the client.
Prefill bool `json:"prefill"`
}

Expand All @@ -37,14 +39,14 @@ type ActorEventFilter struct {

// Interpreted as an epoch (in hex) or one of "latest" for last mined block, "earliest" for first,
// Optional, default: "latest".
FromBlock string `json:"fromBlock,omitempty"`
FromEpoch string `json:"fromEpoch,omitempty"`

// Interpreted as an epoch (in hex) or one of "latest" for last mined block, "earliest" for first,
// Optional, default: "latest".
ToBlock string `json:"toBlock,omitempty"`
ToEpoch string `json:"toEpoch,omitempty"`

// Restricts events returned to those emitted from messages contained in this tipset.
// If `TipSetKey` is present in the filter criteria, then neither `FromBlock` nor `ToBlock` are allowed.
// If `TipSetCid` is present in the filter criteria, then neither `FromEpoch` nor `ToEpoch` are allowed.
TipSetCid *cid.Cid `json:"tipsetCid,omitempty"`
}

Expand Down
8 changes: 4 additions & 4 deletions chain/types/actor_event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,16 @@ func TestSubActorEventFilterJson(t *testing.T) {
},
},
},
FromBlock: &from,
ToBlock: &to,
TipSetKey: &c,
FromEpoch: from,
ToEpoch: to,
TipSetCid: &c,
}

bz, err := json.Marshal(f)
require.NoError(t, err)
require.NotEmpty(t, bz)

s := `{"address":["f410fagkp3qx2f76maqot74jaiw3tzbxe76k76zrkl3xifk67isrnbn2sll3yua","f410fagkp3qx2f76maqot74jaiw3tzbxe76k76zrkl3xifk67isrnbn2sll3yua"],"fields":{"key1":[{"codec":81,"value":"dmFsdWUx"}],"key2":[{"codec":82,"value":"dmFsdWUy"}]},"fromBlock":"earliest","toBlock":"latest","tipset_cid":{"/":"bafkqacqbst64f6rp7taeduy"}}`
s := `{"addresses":["f410fagkp3qx2f76maqot74jaiw3tzbxe76k76zrkl3xifk67isrnbn2sll3yua","f410fagkp3qx2f76maqot74jaiw3tzbxe76k76zrkl3xifk67isrnbn2sll3yua"],"fields":{"key1":[{"codec":81,"value":"dmFsdWUx"}],"key2":[{"codec":82,"value":"dmFsdWUy"}]},"fromEpoch":"earliest","toEpoch":"latest","tipsetCid":{"/":"bafkqacqbst64f6rp7taeduy"}}`
var out ActorEventFilter
err = json.Unmarshal([]byte(s), &out)
require.NoError(t, err)
Expand Down
22 changes: 10 additions & 12 deletions documentation/en/api-v1-unstable-methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -3392,9 +3392,8 @@ Response:


### GetActorEvents
GetActorEvents returns all FEVM Actor events that match the given filter.
This is a request/response API. Please see the docs for `ActorEventFilter` for a detailed
description of the filter options.
GetActorEvents returns all FVM and built-in Actor events that match the given filter.
This is a request/response API.


Perms: read
Expand All @@ -3403,7 +3402,7 @@ Inputs:
```json
[
{
"address": [
"addresses": [
"f01234"
],
"fields": {
Expand All @@ -3414,8 +3413,8 @@ Inputs:
}
]
},
"fromBlock": "2301220",
"toBlock": "latest"
"fromEpoch": "earliest",
"toEpoch": "latest"
}
]
```
Expand Down Expand Up @@ -8714,12 +8713,11 @@ Response:


### SubscribeActorEvents
SubscribeActorEvents returns a long-lived stream of all FEVM Actor events that match the given filter.
Events that match the given filter are written to the stream in real-time as they are emitted from the FEVM.
SubscribeActorEvents returns a long-lived stream of all FVM and built-in Actor events that match the given filter.
Events that match the given filter are written to the stream in real-time as they are emitted from the FVM.
The response stream is closed when the client disconnects or if there is an error while writing an event to the stream.
This API also allows clients to read all historical events matching the given filter before
any real-time events are written to the response stream.
Please see the docs for `SubActorEventFilter` for a detailed description of the filter options.
NOTE: THIS API IS ONLY SUPPORTED OVER WEBSOCKETS FOR NOW


Expand All @@ -8730,7 +8728,7 @@ Inputs:
[
{
"filter": {
"address": [
"addresses": [
"f01234"
],
"fields": {
Expand All @@ -8741,8 +8739,8 @@ Inputs:
}
]
},
"fromBlock": "2301220",
"toBlock": "latest"
"fromEpoch": "earliest",
"toEpoch": "latest"
},
"prefill": true
}
Expand Down
12 changes: 7 additions & 5 deletions itests/direct_data_onboard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,14 +328,14 @@ func TestOnboardRawPieceVerified(t *testing.T) {
require.NoError(t, err)
require.Len(t, allocations, 0)
eventsFromMessages := buildActorEventsFromMessages(ctx, t, miner.FullNode)
fmt.Println("eventsFromMessages", eventsFromMessages)
writeEventsToFile(ctx, t, miner.FullNode, eventsFromMessages)

/* --- Tests for the Actor events API --- */
pstring := func(s string) *string { return &s }
// Match events from Get API and receipts
allEvtsFromGetAPI, err := miner.FullNode.GetActorEvents(ctx, &types.ActorEventFilter{
FromBlock: pstring("earliest"),
ToBlock: pstring("latest"),
FromEpoch: "earliest",
ToEpoch: "latest",
})
require.NoError(t, err)
matchEvents(t, eventsFromMessages, getEventsArray(allEvtsFromGetAPI))
Expand Down Expand Up @@ -379,8 +379,8 @@ func TestOnboardRawPieceVerified(t *testing.T) {
// Match pre-filled events from subscription channel and events obtained from receipts
allEvtsCh, err := miner.FullNode.SubscribeActorEvents(ctx, &types.SubActorEventFilter{
Filter: types.ActorEventFilter{
FromBlock: pstring("earliest"),
ToBlock: pstring("latest"),
FromEpoch: "earliest",
ToEpoch: "latest",
},
Prefill: true,
})
Expand Down Expand Up @@ -427,6 +427,8 @@ func matchEvents(t *testing.T, exp []types.ActorEvent, actual []types.ActorEvent
sort.Slice(bz2, func(i, j int) bool {
return bz2[i] <= bz2[j]
})
fmt.Println("bz1", string(bz1))
fmt.Println("bz2", string(bz2))
require.True(t, bytes.Equal(bz1, bz2))
}

Expand Down
31 changes: 22 additions & 9 deletions node/impl/full/actor_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package full
import (
"context"
"fmt"
"strings"

"github.com/ipfs/go-cid"
"go.uber.org/fx"
Expand Down Expand Up @@ -49,7 +50,7 @@ func (a *ActorEvent) GetActorEvents(ctx context.Context, filter *types.ActorEven
}

// Create a temporary filter
f, err := a.EventFilterManager.Install(ctx, params.MinHeight, params.MaxHeight, params.TipSetKey, filter.Addresses, filter.Fields, false)
f, err := a.EventFilterManager.Install(ctx, params.MinHeight, params.MaxHeight, params.TipSetCid, filter.Addresses, filter.Fields, false)
if err != nil {
return nil, err
}
Expand All @@ -62,29 +63,41 @@ func (a *ActorEvent) GetActorEvents(ctx context.Context, filter *types.ActorEven
type filterParams struct {
MinHeight abi.ChainEpoch
MaxHeight abi.ChainEpoch
TipSetKey cid.Cid
TipSetCid cid.Cid
}

func (a *ActorEvent) parseFilter(f *types.ActorEventFilter) (*filterParams, error) {
if f.TipSetKey != nil {
if f.FromBlock != nil || f.ToBlock != nil {
return nil, fmt.Errorf("cannot specify both TipSetKey and FromBlock/ToBlock")
if f.TipSetCid != nil {
if len(f.FromEpoch) != 0 || len(f.ToEpoch) != 0 {
return nil, fmt.Errorf("cannot specify both TipSetCid and FromEpoch/ToEpoch")
}

return &filterParams{
MinHeight: 0,
MaxHeight: 0,
TipSetKey: *f.TipSetKey,
TipSetCid: *f.TipSetCid,
}, nil
}
min, max, err := parseBlockRange(a.Chain.GetHeaviestTipSet().Height(), f.FromBlock, f.ToBlock, a.MaxFilterHeightRange)

from := f.FromEpoch
if len(from) != 0 && from != "latest" && from != "earliest" && !strings.HasPrefix(from, "0x") {
from = "0x" + from
}

to := f.ToEpoch
if len(to) != 0 && to != "latest" && to != "earliest" && !strings.HasPrefix(to, "0x") {
to = "0x" + to
}

min, max, err := parseBlockRange(a.Chain.GetHeaviestTipSet().Height(), &from, &to, a.MaxFilterHeightRange)
if err != nil {
return nil, err
}

return &filterParams{
MinHeight: min,
MaxHeight: max,
TipSetKey: cid.Undef,
TipSetCid: cid.Undef,
}, nil
}

Expand All @@ -98,7 +111,7 @@ func (a *ActorEvent) SubscribeActorEvents(ctx context.Context, f *types.SubActor
return nil, err
}

fm, err := a.EventFilterManager.Install(ctx, params.MinHeight, params.MaxHeight, params.TipSetKey, f.Filter.Addresses, f.Filter.Fields, false)
fm, err := a.EventFilterManager.Install(ctx, params.MinHeight, params.MaxHeight, params.TipSetCid, f.Filter.Addresses, f.Filter.Fields, false)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions node/impl/full/eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -1264,7 +1264,7 @@ func (e *EthEvent) EthGetFilterLogs(ctx context.Context, id ethtypes.EthFilterID
}

func parseBlockRange(heaviest abi.ChainEpoch, fromBlock, toBlock *string, maxRange abi.ChainEpoch) (minHeight abi.ChainEpoch, maxHeight abi.ChainEpoch, err error) {
if fromBlock == nil || *fromBlock == "latest" {
if fromBlock == nil || *fromBlock == "latest" || len(*fromBlock) == 0 {
minHeight = heaviest
} else if *fromBlock == "earliest" {
minHeight = 0
Expand All @@ -1279,7 +1279,7 @@ func parseBlockRange(heaviest abi.ChainEpoch, fromBlock, toBlock *string, maxRan
minHeight = abi.ChainEpoch(epoch)
}

if toBlock == nil || *toBlock == "latest" {
if toBlock == nil || *toBlock == "latest" || len(*toBlock) == 0 {
// here latest means the latest at the time
maxHeight = -1
} else if *toBlock == "earliest" {
Expand Down

0 comments on commit 8e71abf

Please sign in to comment.