From 275e814271a393f09d61ebd2581436411e19f9db Mon Sep 17 00:00:00 2001 From: Ekaterina Pavlova Date: Tue, 12 Dec 2023 13:59:03 +0300 Subject: [PATCH] rpc: change the type of BlockFilter.Primary from int to byte BlockFilter has PrinaryIndex of int type while block.Block structure itself has PrimaryIndex of byte. It's needed to prevent changing filters field type and all associated subscriptions logic on server side. Refs #3241. Signed-off-by: Ekaterina Pavlova --- docs/notifications.md | 10 +++++----- pkg/neorpc/filters.go | 4 ++-- pkg/neorpc/filters_test.go | 4 ++-- pkg/neorpc/rpcevent/filter.go | 2 +- pkg/neorpc/rpcevent/filter_test.go | 4 ++-- pkg/rpcclient/wsclient_test.go | 24 ++++++++++++------------ pkg/services/rpcsrv/client_test.go | 4 ++-- 7 files changed, 26 insertions(+), 26 deletions(-) diff --git a/docs/notifications.md b/docs/notifications.md index 119130ed1..cd47e841b 100644 --- a/docs/notifications.md +++ b/docs/notifications.md @@ -67,11 +67,11 @@ omitted if empty). Recognized stream names: * `block_added` - Filter: `primary` as an integer with primary (speaker) node index from - ConsensusData and/or `since` field as an integer value with block - index starting from which new block notifications will be received and/or - `till` field as an integer values containing block index till which new - block notifications will be received. + Filter: `primary` as an integer with a valid range of 0-255 with + primary (speaker) node index from ConsensusData and/or `since` field as + an integer value with block index starting from which new block + notifications will be received and/or `till` field as an integer values + containing block index till which new block notifications will be received. * `header_of_added_block` Filter: `primary` as an integer with primary (speaker) node index from ConsensusData and/or `since` field as an integer value with header diff --git a/pkg/neorpc/filters.go b/pkg/neorpc/filters.go index 255d94e86..a85bfd88c 100644 --- a/pkg/neorpc/filters.go +++ b/pkg/neorpc/filters.go @@ -11,7 +11,7 @@ type ( // since/till the specified index inclusively). nil value treated as missing // filter. BlockFilter struct { - Primary *int `json:"primary,omitempty"` + Primary *byte `json:"primary,omitempty"` Since *uint32 `json:"since,omitempty"` Till *uint32 `json:"till,omitempty"` } @@ -56,7 +56,7 @@ func (f *BlockFilter) Copy() *BlockFilter { } var res = new(BlockFilter) if f.Primary != nil { - res.Primary = new(int) + res.Primary = new(byte) *res.Primary = *f.Primary } if f.Since != nil { diff --git a/pkg/neorpc/filters_test.go b/pkg/neorpc/filters_test.go index 8f725a588..e38591c5d 100644 --- a/pkg/neorpc/filters_test.go +++ b/pkg/neorpc/filters_test.go @@ -16,12 +16,12 @@ func TestBlockFilterCopy(t *testing.T) { tf = bf.Copy() require.Equal(t, bf, tf) - bf.Primary = new(int) + bf.Primary = new(byte) *bf.Primary = 42 tf = bf.Copy() require.Equal(t, bf, tf) - *bf.Primary = 100500 + *bf.Primary = 100 require.NotEqual(t, bf, tf) bf.Since = new(uint32) diff --git a/pkg/neorpc/rpcevent/filter.go b/pkg/neorpc/rpcevent/filter.go index f2be233ad..90696a2f0 100644 --- a/pkg/neorpc/rpcevent/filter.go +++ b/pkg/neorpc/rpcevent/filter.go @@ -42,7 +42,7 @@ func Matches(f Comparator, r Container) bool { } else { b = &r.EventPayload().(*block.Block).Header } - primaryOk := filt.Primary == nil || *filt.Primary == int(b.PrimaryIndex) + primaryOk := filt.Primary == nil || *filt.Primary == b.PrimaryIndex sinceOk := filt.Since == nil || *filt.Since <= b.Index tillOk := filt.Till == nil || b.Index <= *filt.Till return primaryOk && sinceOk && tillOk diff --git a/pkg/neorpc/rpcevent/filter_test.go b/pkg/neorpc/rpcevent/filter_test.go index 6d8d33aad..956ab2686 100644 --- a/pkg/neorpc/rpcevent/filter_test.go +++ b/pkg/neorpc/rpcevent/filter_test.go @@ -40,8 +40,8 @@ func (c testContainer) EventPayload() any { } func TestMatches(t *testing.T) { - primary := 1 - badPrimary := 2 + primary := byte(1) + badPrimary := byte(2) index := uint32(5) badHigherIndex := uint32(6) badLowerIndex := index - 1 diff --git a/pkg/rpcclient/wsclient_test.go b/pkg/rpcclient/wsclient_test.go index 8247901ac..a983e0609 100644 --- a/pkg/rpcclient/wsclient_test.go +++ b/pkg/rpcclient/wsclient_test.go @@ -381,7 +381,7 @@ func TestWSFilteredSubscriptions(t *testing.T) { }{ {"block header primary", func(t *testing.T, wsc *WSClient) { - primary := 3 + primary := byte(3) _, err := wsc.ReceiveHeadersOfAddedBlocks(&neorpc.BlockFilter{Primary: &primary}, make(chan *block.Header)) require.NoError(t, err) }, @@ -389,7 +389,7 @@ func TestWSFilteredSubscriptions(t *testing.T) { param := p.Value(1) filt := new(neorpc.BlockFilter) require.NoError(t, json.Unmarshal(param.RawMessage, filt)) - require.Equal(t, 3, *filt.Primary) + require.Equal(t, byte(3), *filt.Primary) require.Equal(t, (*uint32)(nil), filt.Since) require.Equal(t, (*uint32)(nil), filt.Till) }, @@ -404,7 +404,7 @@ func TestWSFilteredSubscriptions(t *testing.T) { param := p.Value(1) filt := new(neorpc.BlockFilter) require.NoError(t, json.Unmarshal(param.RawMessage, filt)) - require.Equal(t, (*int)(nil), filt.Primary) + require.Equal(t, (*byte)(nil), filt.Primary) require.Equal(t, uint32(3), *filt.Since) require.Equal(t, (*uint32)(nil), filt.Till) }, @@ -419,7 +419,7 @@ func TestWSFilteredSubscriptions(t *testing.T) { param := p.Value(1) filt := new(neorpc.BlockFilter) require.NoError(t, json.Unmarshal(param.RawMessage, filt)) - require.Equal(t, (*int)(nil), filt.Primary) + require.Equal(t, (*byte)(nil), filt.Primary) require.Equal(t, (*uint32)(nil), filt.Since) require.Equal(t, (uint32)(3), *filt.Till) }, @@ -428,7 +428,7 @@ func TestWSFilteredSubscriptions(t *testing.T) { func(t *testing.T, wsc *WSClient) { var ( since uint32 = 3 - primary = 2 + primary = byte(2) till uint32 = 5 ) _, err := wsc.ReceiveHeadersOfAddedBlocks(&neorpc.BlockFilter{ @@ -442,14 +442,14 @@ func TestWSFilteredSubscriptions(t *testing.T) { param := p.Value(1) filt := new(neorpc.BlockFilter) require.NoError(t, json.Unmarshal(param.RawMessage, filt)) - require.Equal(t, 2, *filt.Primary) + require.Equal(t, byte(2), *filt.Primary) require.Equal(t, uint32(3), *filt.Since) require.Equal(t, uint32(5), *filt.Till) }, }, {"blocks primary", func(t *testing.T, wsc *WSClient) { - primary := 3 + primary := byte(3) _, err := wsc.ReceiveBlocks(&neorpc.BlockFilter{Primary: &primary}, make(chan *block.Block)) require.NoError(t, err) }, @@ -457,7 +457,7 @@ func TestWSFilteredSubscriptions(t *testing.T) { param := p.Value(1) filt := new(neorpc.BlockFilter) require.NoError(t, json.Unmarshal(param.RawMessage, filt)) - require.Equal(t, 3, *filt.Primary) + require.Equal(t, byte(3), *filt.Primary) require.Equal(t, (*uint32)(nil), filt.Since) require.Equal(t, (*uint32)(nil), filt.Till) }, @@ -472,7 +472,7 @@ func TestWSFilteredSubscriptions(t *testing.T) { param := p.Value(1) filt := new(neorpc.BlockFilter) require.NoError(t, json.Unmarshal(param.RawMessage, filt)) - require.Equal(t, (*int)(nil), filt.Primary) + require.Equal(t, (*byte)(nil), filt.Primary) require.Equal(t, uint32(3), *filt.Since) require.Equal(t, (*uint32)(nil), filt.Till) }, @@ -487,7 +487,7 @@ func TestWSFilteredSubscriptions(t *testing.T) { param := p.Value(1) filt := new(neorpc.BlockFilter) require.NoError(t, json.Unmarshal(param.RawMessage, filt)) - require.Equal(t, (*int)(nil), filt.Primary) + require.Equal(t, (*byte)(nil), filt.Primary) require.Equal(t, (*uint32)(nil), filt.Since) require.Equal(t, (uint32)(3), *filt.Till) }, @@ -496,7 +496,7 @@ func TestWSFilteredSubscriptions(t *testing.T) { func(t *testing.T, wsc *WSClient) { var ( since uint32 = 3 - primary = 2 + primary = byte(2) till uint32 = 5 ) _, err := wsc.ReceiveBlocks(&neorpc.BlockFilter{ @@ -510,7 +510,7 @@ func TestWSFilteredSubscriptions(t *testing.T) { param := p.Value(1) filt := new(neorpc.BlockFilter) require.NoError(t, json.Unmarshal(param.RawMessage, filt)) - require.Equal(t, 2, *filt.Primary) + require.Equal(t, byte(2), *filt.Primary) require.Equal(t, uint32(3), *filt.Since) require.Equal(t, uint32(5), *filt.Till) }, diff --git a/pkg/services/rpcsrv/client_test.go b/pkg/services/rpcsrv/client_test.go index b77b44b0f..8cdc51689 100644 --- a/pkg/services/rpcsrv/client_test.go +++ b/pkg/services/rpcsrv/client_test.go @@ -2113,9 +2113,9 @@ func TestWSClient_SubscriptionsCompat(t *testing.T) { blocks := getTestBlocks(t) bCount := uint32(0) - getData := func(t *testing.T) (*block.Block, int, util.Uint160, string, string) { + getData := func(t *testing.T) (*block.Block, byte, util.Uint160, string, string) { b1 := blocks[bCount] - primary := int(b1.PrimaryIndex) + primary := b1.PrimaryIndex tx := b1.Transactions[0] sender := tx.Sender() ntfName := "Transfer"