Skip to content

Commit

Permalink
Added test cases, fixed unexpected behaviour when empty account addre…
Browse files Browse the repository at this point in the history
…sses
  • Loading branch information
AndriiDiachuk committed Mar 29, 2024
1 parent 2482500 commit 897b34b
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 20 deletions.
40 changes: 33 additions & 7 deletions engine/access/state_stream/account_status_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,37 @@ const (
CoreEventInboxValueClaimed = "flow.InboxValueClaimed"
)

var defaultCoreEventsMap map[string]struct{}
var defaultCoreEventsMap map[string]map[string]struct{}

func init() {
defaultCoreEventsMap = make(map[string]struct{}, len(DefaultCoreEvents))
for i := range DefaultCoreEvents {
defaultCoreEventsMap[DefaultCoreEvents[i]] = struct{}{}
defaultCoreEventsMap = make(map[string]map[string]struct{}, len(DefaultCoreEvents))

addFilter := func(eventType, field string) {
if _, ok := defaultCoreEventsMap[eventType]; !ok {
defaultCoreEventsMap[eventType] = make(map[string]struct{})
}
defaultCoreEventsMap[eventType][field] = struct{}{}
}

for _, eventType := range DefaultCoreEvents {

switch eventType {
case CoreEventAccountCreated,
CoreEventAccountKeyAdded,
CoreEventAccountKeyRemoved,
CoreEventAccountContractAdded,
CoreEventAccountContractUpdated,
CoreEventAccountContractRemoved:
addFilter(eventType, "address")
case CoreEventInboxValuePublished,
CoreEventInboxValueClaimed:
addFilter(eventType, "provider")
addFilter(eventType, "recipient")
case CoreEventInboxValueUnpublished:
addFilter(eventType, "provider")
default:
panic(fmt.Errorf("unsupported event type: %s", eventType))
}
}
}

Expand Down Expand Up @@ -119,7 +144,7 @@ func NewAccountStatusFilter(
// If there are non-core event types at this stage, it returns an error from `addCoreEventFieldFilter`.
for _, eventType := range eventTypes {
// use the hex with prefix address to make sure it will match the cadence address
err = accountStatusFilter.addCoreEventFieldFilter(flow.EventType(eventType), addr.HexWithPrefix(), chain)
err = accountStatusFilter.addCoreEventFieldFilter(flow.EventType(eventType), addr.HexWithPrefix())
if err != nil {
return AccountStatusFilter{}, err
}
Expand Down Expand Up @@ -150,7 +175,8 @@ func (f *AccountStatusFilter) GroupCoreEventsByAccountAddress(events flow.Events
continue
}

accountField := f.EventFieldFilters[event.Type]
//accountField := f.EventFieldFilters[event.Type]
accountField := defaultCoreEventsMap[string(event.Type)]
for i, field := range fields {
_, ok := accountField[field.Identifier]
if ok {
Expand All @@ -164,7 +190,7 @@ func (f *AccountStatusFilter) GroupCoreEventsByAccountAddress(events flow.Events
}

// addCoreEventFieldFilter adds a field filter for each core event type
func (f *AccountStatusFilter) addCoreEventFieldFilter(eventType flow.EventType, address string, chain flow.Chain) error {
func (f *AccountStatusFilter) addCoreEventFieldFilter(eventType flow.EventType, address string) error {
addFilter := func(field, value string) {
if _, ok := f.EventFieldFilters[eventType]; !ok {
f.EventFieldFilters[eventType] = make(FieldFilter)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,28 @@ func (s *BackendAccountStatusesSuite) generateFiltersForTestCases(baseTests []te
)
require.NoError(s.T(), err)
tests = append(tests, t3)

t4 := test
t4.name = fmt.Sprintf("%s - no events, no addresses", test.name)
t4.filters, err = state_stream.NewAccountStatusFilter(
state_stream.DefaultEventFilterConfig,
chainID.Chain(),
[]string{},
[]string{},
)
require.NoError(s.T(), err)
tests = append(tests, t4)

t5 := test
t5.name = fmt.Sprintf("%s - some events, no addresses", test.name)
t5.filters, err = state_stream.NewAccountStatusFilter(
state_stream.DefaultEventFilterConfig,
chainID.Chain(),
[]string{"flow.AccountKeyAdded"},
[]string{},
)
require.NoError(s.T(), err)
tests = append(tests, t5)
}

return tests
Expand Down
3 changes: 0 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -318,8 +318,5 @@ require (
rsc.io/tmplfunc v0.0.3 // indirect
)

// Using custom protobuf until https://github.com/onflow/flow/pull/1433 will me berged
replace github.com/onflow/flow/protobuf/go/flow v0.3.7-0.20240305102946-3efec6679252 => github.com/AndriiDiachuk/flow/protobuf/go/flow v0.0.0-20240315141441-bb17cf02e8cb

// Using custom fork until https://github.com/onflow/flow-go/issues/5338 is resolved
replace github.com/ipfs/boxo => github.com/onflow/boxo v0.0.0-20240201202436-f2477b92f483
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,6 @@ dmitri.shuralyov.com/state v0.0.0-20180228185332-28bcc343414c/go.mod h1:0PRwlb0D
git.apache.org/thrift.git v0.0.0-20180902110319-2566ecd5d999/go.mod h1:fPE2ZNJGynbRyZ4dJvy6G277gSllfV2HJqblrnkyeyg=
github.com/AndreasBriese/bbloom v0.0.0-20180913140656-343706a395b7/go.mod h1:bOvUY6CB00SOBii9/FifXqc0awNKxLFCL/+pkDPuyl8=
github.com/AndreasBriese/bbloom v0.0.0-20190306092124-e2d15f34fcf9/go.mod h1:bOvUY6CB00SOBii9/FifXqc0awNKxLFCL/+pkDPuyl8=
github.com/AndriiDiachuk/flow/protobuf/go/flow v0.0.0-20240315141441-bb17cf02e8cb h1:rKmrzLMRMENZ0/sEZFR7HCyl6ERG2zGZgPSXBP4OeGg=
github.com/AndriiDiachuk/flow/protobuf/go/flow v0.0.0-20240315141441-bb17cf02e8cb/go.mod h1:NA2pX2nw8zuaxfKphhKsk00kWLwfd+tv8mS23YXO4Sk=
github.com/Azure/azure-pipeline-go v0.2.1/go.mod h1:UGSo8XybXnIGZ3epmeBw7Jdz+HiUVpqIlpz/HKHylF4=
github.com/Azure/azure-pipeline-go v0.2.2/go.mod h1:4rQ/NZncSvGqNkkOsNpOU1tgoNuIlp9AfUH5G1tvCHc=
github.com/Azure/azure-storage-blob-go v0.7.0/go.mod h1:f9YQKtsG1nMisotuTPpO0tjNuEjKRYAcJU8/ydDI++4=
Expand Down Expand Up @@ -1372,6 +1370,8 @@ github.com/onflow/flow-go/crypto v0.21.3/go.mod h1:vI6V4CY3R6c4JKBxdcRiR/AnjBfL8
github.com/onflow/flow-nft/lib/go/contracts v1.1.0 h1:rhUDeD27jhLwOqQKI/23008CYfnqXErrJvc4EFRP2a0=
github.com/onflow/flow-nft/lib/go/contracts v1.1.0/go.mod h1:YsvzYng4htDgRB9sa9jxdwoTuuhjK8WYWXTyLkIigZY=
github.com/onflow/flow/protobuf/go/flow v0.2.2/go.mod h1:gQxYqCfkI8lpnKsmIjwtN2mV/N2PIwc1I+RUK4HPIc8=
github.com/onflow/flow/protobuf/go/flow v0.3.7-0.20240305102946-3efec6679252 h1:W0xm80Qc5RkFJw7yQIj7OiMacCZw3et/tx/5N9rN2qk=
github.com/onflow/flow/protobuf/go/flow v0.3.7-0.20240305102946-3efec6679252/go.mod h1:NA2pX2nw8zuaxfKphhKsk00kWLwfd+tv8mS23YXO4Sk=
github.com/onflow/go-ethereum v1.13.4 h1:iNO86fm8RbBbhZ87ZulblInqCdHnAQVY8okBrNsTevc=
github.com/onflow/go-ethereum v1.13.4/go.mod h1:cE/gEUkAffhwbVmMJYz+t1dAfVNHNwZCgc3BWtZxBGY=
github.com/onflow/sdks v0.5.0 h1:2HCRibwqDaQ1c9oUApnkZtEAhWiNY2GTpRD5+ftdkN8=
Expand Down
2 changes: 0 additions & 2 deletions insecure/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -304,5 +304,3 @@ require (
)

replace github.com/onflow/flow-go => ../

replace github.com/onflow/flow/protobuf/go/flow v0.3.7-0.20240305102946-3efec6679252 => github.com/AndriiDiachuk/flow/protobuf/go/flow v0.0.0-20240315141441-bb17cf02e8cb
4 changes: 2 additions & 2 deletions insecure/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ dmitri.shuralyov.com/state v0.0.0-20180228185332-28bcc343414c/go.mod h1:0PRwlb0D
git.apache.org/thrift.git v0.0.0-20180902110319-2566ecd5d999/go.mod h1:fPE2ZNJGynbRyZ4dJvy6G277gSllfV2HJqblrnkyeyg=
github.com/AndreasBriese/bbloom v0.0.0-20180913140656-343706a395b7/go.mod h1:bOvUY6CB00SOBii9/FifXqc0awNKxLFCL/+pkDPuyl8=
github.com/AndreasBriese/bbloom v0.0.0-20190306092124-e2d15f34fcf9/go.mod h1:bOvUY6CB00SOBii9/FifXqc0awNKxLFCL/+pkDPuyl8=
github.com/AndriiDiachuk/flow/protobuf/go/flow v0.0.0-20240315141441-bb17cf02e8cb h1:rKmrzLMRMENZ0/sEZFR7HCyl6ERG2zGZgPSXBP4OeGg=
github.com/AndriiDiachuk/flow/protobuf/go/flow v0.0.0-20240315141441-bb17cf02e8cb/go.mod h1:NA2pX2nw8zuaxfKphhKsk00kWLwfd+tv8mS23YXO4Sk=
github.com/Azure/azure-pipeline-go v0.2.1/go.mod h1:UGSo8XybXnIGZ3epmeBw7Jdz+HiUVpqIlpz/HKHylF4=
github.com/Azure/azure-pipeline-go v0.2.2/go.mod h1:4rQ/NZncSvGqNkkOsNpOU1tgoNuIlp9AfUH5G1tvCHc=
github.com/Azure/azure-storage-blob-go v0.7.0/go.mod h1:f9YQKtsG1nMisotuTPpO0tjNuEjKRYAcJU8/ydDI++4=
Expand Down Expand Up @@ -1335,6 +1333,8 @@ github.com/onflow/flow-go/crypto v0.21.3/go.mod h1:vI6V4CY3R6c4JKBxdcRiR/AnjBfL8
github.com/onflow/flow-nft/lib/go/contracts v1.1.0 h1:rhUDeD27jhLwOqQKI/23008CYfnqXErrJvc4EFRP2a0=
github.com/onflow/flow-nft/lib/go/contracts v1.1.0/go.mod h1:YsvzYng4htDgRB9sa9jxdwoTuuhjK8WYWXTyLkIigZY=
github.com/onflow/flow/protobuf/go/flow v0.2.2/go.mod h1:gQxYqCfkI8lpnKsmIjwtN2mV/N2PIwc1I+RUK4HPIc8=
github.com/onflow/flow/protobuf/go/flow v0.3.7-0.20240305102946-3efec6679252 h1:W0xm80Qc5RkFJw7yQIj7OiMacCZw3et/tx/5N9rN2qk=
github.com/onflow/flow/protobuf/go/flow v0.3.7-0.20240305102946-3efec6679252/go.mod h1:NA2pX2nw8zuaxfKphhKsk00kWLwfd+tv8mS23YXO4Sk=
github.com/onflow/go-ethereum v1.13.4 h1:iNO86fm8RbBbhZ87ZulblInqCdHnAQVY8okBrNsTevc=
github.com/onflow/go-ethereum v1.13.4/go.mod h1:cE/gEUkAffhwbVmMJYz+t1dAfVNHNwZCgc3BWtZxBGY=
github.com/onflow/sdks v0.5.0 h1:2HCRibwqDaQ1c9oUApnkZtEAhWiNY2GTpRD5+ftdkN8=
Expand Down
2 changes: 0 additions & 2 deletions integration/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -361,5 +361,3 @@ require (
replace github.com/onflow/flow-go => ../

replace github.com/onflow/flow-go/insecure => ../insecure

replace github.com/onflow/flow/protobuf/go/flow v0.3.7-0.20240305102946-3efec6679252 => github.com/AndriiDiachuk/flow/protobuf/go/flow v0.0.0-20240315141441-bb17cf02e8cb
4 changes: 2 additions & 2 deletions integration/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,6 @@ dmitri.shuralyov.com/state v0.0.0-20180228185332-28bcc343414c/go.mod h1:0PRwlb0D
git.apache.org/thrift.git v0.0.0-20180902110319-2566ecd5d999/go.mod h1:fPE2ZNJGynbRyZ4dJvy6G277gSllfV2HJqblrnkyeyg=
github.com/AndreasBriese/bbloom v0.0.0-20180913140656-343706a395b7/go.mod h1:bOvUY6CB00SOBii9/FifXqc0awNKxLFCL/+pkDPuyl8=
github.com/AndreasBriese/bbloom v0.0.0-20190306092124-e2d15f34fcf9/go.mod h1:bOvUY6CB00SOBii9/FifXqc0awNKxLFCL/+pkDPuyl8=
github.com/AndriiDiachuk/flow/protobuf/go/flow v0.0.0-20240315141441-bb17cf02e8cb h1:rKmrzLMRMENZ0/sEZFR7HCyl6ERG2zGZgPSXBP4OeGg=
github.com/AndriiDiachuk/flow/protobuf/go/flow v0.0.0-20240315141441-bb17cf02e8cb/go.mod h1:NA2pX2nw8zuaxfKphhKsk00kWLwfd+tv8mS23YXO4Sk=
github.com/Azure/azure-pipeline-go v0.2.1/go.mod h1:UGSo8XybXnIGZ3epmeBw7Jdz+HiUVpqIlpz/HKHylF4=
github.com/Azure/azure-pipeline-go v0.2.2/go.mod h1:4rQ/NZncSvGqNkkOsNpOU1tgoNuIlp9AfUH5G1tvCHc=
github.com/Azure/azure-storage-blob-go v0.7.0/go.mod h1:f9YQKtsG1nMisotuTPpO0tjNuEjKRYAcJU8/ydDI++4=
Expand Down Expand Up @@ -1427,6 +1425,8 @@ github.com/onflow/flow-go/crypto v0.21.3/go.mod h1:vI6V4CY3R6c4JKBxdcRiR/AnjBfL8
github.com/onflow/flow-nft/lib/go/contracts v1.1.0 h1:rhUDeD27jhLwOqQKI/23008CYfnqXErrJvc4EFRP2a0=
github.com/onflow/flow-nft/lib/go/contracts v1.1.0/go.mod h1:YsvzYng4htDgRB9sa9jxdwoTuuhjK8WYWXTyLkIigZY=
github.com/onflow/flow/protobuf/go/flow v0.2.2/go.mod h1:gQxYqCfkI8lpnKsmIjwtN2mV/N2PIwc1I+RUK4HPIc8=
github.com/onflow/flow/protobuf/go/flow v0.3.7-0.20240305102946-3efec6679252 h1:W0xm80Qc5RkFJw7yQIj7OiMacCZw3et/tx/5N9rN2qk=
github.com/onflow/flow/protobuf/go/flow v0.3.7-0.20240305102946-3efec6679252/go.mod h1:NA2pX2nw8zuaxfKphhKsk00kWLwfd+tv8mS23YXO4Sk=
github.com/onflow/go-ethereum v1.13.4 h1:iNO86fm8RbBbhZ87ZulblInqCdHnAQVY8okBrNsTevc=
github.com/onflow/go-ethereum v1.13.4/go.mod h1:cE/gEUkAffhwbVmMJYz+t1dAfVNHNwZCgc3BWtZxBGY=
github.com/onflow/nft-storefront/lib/go/contracts v0.0.0-20221222181731-14b90207cead h1:2j1Unqs76Z1b95Gu4C3Y28hzNUHBix7wL490e61SMSw=
Expand Down

0 comments on commit 897b34b

Please sign in to comment.