Skip to content

Commit 68d7e8e

Browse files
authored
Merge pull request #3820 from nspcc-dev/getblocknotifications
rpcclient: fix getblocknotifications filter handling
2 parents 64e3b6a + a227572 commit 68d7e8e

File tree

4 files changed

+122
-3
lines changed

4 files changed

+122
-3
lines changed

pkg/rpcclient/rpc.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -974,9 +974,15 @@ func (c *Client) GetRawNotaryPool() (*result.RawNotaryPool, error) {
974974
}
975975

976976
// GetBlockNotifications returns notifications from a block organized by trigger type.
977-
func (c *Client) GetBlockNotifications(blockHash util.Uint256, filters ...*neorpc.NotificationFilter) (*result.BlockNotifications, error) {
978-
var resp = &result.BlockNotifications{}
979-
if err := c.performRequest("getblocknotifications", []any{blockHash.StringLE(), filters}, resp); err != nil {
977+
func (c *Client) GetBlockNotifications(blockHash util.Uint256, filter *neorpc.NotificationFilter) (*result.BlockNotifications, error) {
978+
var (
979+
resp = &result.BlockNotifications{}
980+
params = []any{blockHash.StringLE()}
981+
)
982+
if filter != nil {
983+
params = append(params, *filter)
984+
}
985+
if err := c.performRequest("getblocknotifications", params, resp); err != nil {
980986
return nil, err
981987
}
982988
return resp, nil

pkg/rpcclient/rpc_test.go

+72
Original file line numberDiff line numberDiff line change
@@ -1335,6 +1335,72 @@ var rpcClientTestCases = map[string][]rpcClientTestCase{
13351335
},
13361336
},
13371337
},
1338+
"getblocknotifications": {
1339+
{
1340+
name: "positive, nil filter",
1341+
invoke: func(c *Client) (any, error) {
1342+
hash, err := util.Uint256DecodeStringLE("0f8fb4e17d2ab9f3097af75ca7fd16064160fb8043db94909e00dd4e257b9dc4")
1343+
if err != nil {
1344+
return nil, err
1345+
}
1346+
return c.GetBlockNotifications(hash, nil)
1347+
},
1348+
serverResponse: `{
1349+
"jsonrpc": "2.0",
1350+
"id": 1,
1351+
"result": {
1352+
"onpersist": [],
1353+
"application": [
1354+
{
1355+
"container": "0x0f8fb4e17d2ab9f3097af75ca7fd16064160fb8043db94909e00dd4e257b9dc4",
1356+
"contract": "0xfffdc93764dbaddd97c48f252a53ea4643faa3fd",
1357+
"eventname": "Deploy",
1358+
"state": {
1359+
"type": "Array",
1360+
"value": [
1361+
{
1362+
"type": "ByteString",
1363+
"value": "/aP6Q0bqUyolj8SX3a3bZDfJ/f8="
1364+
}
1365+
]
1366+
}
1367+
}
1368+
],
1369+
"postpersist": []
1370+
}
1371+
}`,
1372+
result: func(c *Client) any {
1373+
blockHash, _ := util.Uint256DecodeStringLE("0f8fb4e17d2ab9f3097af75ca7fd16064160fb8043db94909e00dd4e257b9dc4")
1374+
contract, _ := util.Uint160DecodeStringBE("fffdc93764dbaddd97c48f252a53ea4643faa3fd")
1375+
expectedBytes, _ := base64.StdEncoding.DecodeString("/aP6Q0bqUyolj8SX3a3bZDfJ/f8=")
1376+
return &result.BlockNotifications{
1377+
OnPersist: []state.ContainedNotificationEvent{},
1378+
Application: []state.ContainedNotificationEvent{
1379+
{
1380+
Container: blockHash,
1381+
NotificationEvent: state.NotificationEvent{
1382+
ScriptHash: contract,
1383+
Name: "Deploy",
1384+
Item: stackitem.NewArray([]stackitem.Item{stackitem.NewByteArray(expectedBytes)}),
1385+
},
1386+
},
1387+
},
1388+
PostPersist: []state.ContainedNotificationEvent{},
1389+
}
1390+
},
1391+
check: func(t *testing.T, c *Client, res any) {
1392+
bn, ok := res.(*result.BlockNotifications)
1393+
require.True(t, ok)
1394+
require.NotEmpty(t, bn)
1395+
require.Len(t, bn.Application, 1)
1396+
n := bn.Application[0]
1397+
require.Equal(t, "Deploy", n.Name)
1398+
require.Equal(t, "0f8fb4e17d2ab9f3097af75ca7fd16064160fb8043db94909e00dd4e257b9dc4", n.Container.StringLE())
1399+
require.Equal(t, "fffdc93764dbaddd97c48f252a53ea4643faa3fd", n.ScriptHash.StringLE())
1400+
require.Len(t, n.Item.Value().([]stackitem.Item), 1)
1401+
},
1402+
},
1403+
},
13381404
}
13391405

13401406
type rpcClientErrorCase struct {
@@ -1859,6 +1925,12 @@ var rpcClientErrorCases = map[string][]rpcClientErrorCase{
18591925
return nil, c.ValidateAddress("")
18601926
},
18611927
},
1928+
{
1929+
name: "getblocknotifications_unmarshalling_error",
1930+
invoke: func(c *Client) (any, error) {
1931+
return c.GetBlockNotifications(util.Uint256{}, nil)
1932+
},
1933+
},
18621934
},
18631935
}
18641936

pkg/services/rpcsrv/client_test.go

+35
Original file line numberDiff line numberDiff line change
@@ -2488,3 +2488,38 @@ func TestClient_NEP24(t *testing.T) {
24882488
}
24892489
})
24902490
}
2491+
2492+
func TestGetBlockNotifications(t *testing.T) {
2493+
chain, _, httpSrv := initServerWithInMemoryChain(t)
2494+
c, err := rpcclient.New(context.Background(), httpSrv.URL, rpcclient.Options{})
2495+
require.NoError(t, err)
2496+
t.Cleanup(c.Close)
2497+
require.NoError(t, c.Init())
2498+
2499+
t.Run("nil filter", func(t *testing.T) {
2500+
bn, err := c.GetBlockNotifications(chain.GetHeaderHash(1), nil)
2501+
require.NoError(t, err)
2502+
require.NotEmpty(t, bn)
2503+
})
2504+
t.Run("empty filter", func(t *testing.T) {
2505+
bn, err := c.GetBlockNotifications(chain.GetHeaderHash(1), &neorpc.NotificationFilter{})
2506+
require.NoError(t, err)
2507+
require.NotEmpty(t, bn)
2508+
})
2509+
t.Run("bad filter", func(t *testing.T) {
2510+
badName := "Transfer1"
2511+
bn, err := c.GetBlockNotifications(chain.GetHeaderHash(1), &neorpc.NotificationFilter{
2512+
Name: &badName,
2513+
})
2514+
require.NoError(t, err)
2515+
require.Empty(t, bn)
2516+
})
2517+
t.Run("good", func(t *testing.T) {
2518+
name := "Transfer"
2519+
bn, err := c.GetBlockNotifications(chain.GetHeaderHash(1), &neorpc.NotificationFilter{
2520+
Name: &name,
2521+
})
2522+
require.NoError(t, err)
2523+
require.NotEmpty(t, bn)
2524+
})
2525+
}

pkg/services/rpcsrv/server_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -2324,6 +2324,12 @@ var rpcTestCases = map[string][]rpcTestCase{
23242324
fail: true,
23252325
errCode: neorpc.InvalidParamsCode,
23262326
},
2327+
{
2328+
name: "empty filter",
2329+
params: `["` + genesisBlockHash + `", []]`,
2330+
fail: true,
2331+
errCode: neorpc.InvalidParamsCode,
2332+
},
23272333
},
23282334
}
23292335

0 commit comments

Comments
 (0)