Skip to content

Commit

Permalink
Can't call subscribeEvents on pending block
Browse files Browse the repository at this point in the history
  • Loading branch information
IronGauntlets committed Nov 22, 2024
1 parent 26ad026 commit 62c6df8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
1 change: 1 addition & 0 deletions rpc/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ var (
ErrUnsupportedContractClassVersion = &jsonrpc.Error{Code: 62, Message: "the contract class version is not supported"}
ErrUnexpectedError = &jsonrpc.Error{Code: 63, Message: "An unexpected error occurred"}
ErrTooManyBlocksBack = &jsonrpc.Error{Code: 68, Message: "Cannot go back more than 1024 blocks"}
ErrCallOnPending = &jsonrpc.Error{Code: 69, Message: "This method does not support being called on the pending block"}

// These errors can be only be returned by Juno-specific methods.
ErrSubscriptionNotFound = &jsonrpc.Error{Code: 100, Message: "Subscription not found"}
Expand Down
5 changes: 4 additions & 1 deletion rpc/subscriptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,16 @@ func (h *Handler) SubscribeEvents(ctx context.Context, fromAddr *felt.Felt, keys
if blockID == nil {
requestedHeader = headHeader
} else {
if blockID.Pending {
return nil, ErrCallOnPending
}

var rpcErr *jsonrpc.Error
requestedHeader, rpcErr = h.blockHeaderByID(blockID)
if rpcErr != nil {
return nil, rpcErr
}

// Todo: should the pending block be included in the head count?
if headHeader.Number >= maxBlocksBack && requestedHeader.Number <= headHeader.Number-maxBlocksBack {
return nil, ErrTooManyBlocksBack
}
Expand Down
31 changes: 29 additions & 2 deletions rpc/subscriptions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (fc *fakeConn) Equal(other jsonrpc.Conn) bool {
func TestSubscribeEvents(t *testing.T) {
log := utils.NewNopZapLogger()

t.Run("Too many keys in filter", func(t *testing.T) {
t.Run("Return error if too many keys in filter", func(t *testing.T) {
mockCtrl := gomock.NewController(t)
t.Cleanup(mockCtrl.Finish)

Expand All @@ -68,7 +68,34 @@ func TestSubscribeEvents(t *testing.T) {
assert.Equal(t, ErrTooManyKeysInFilter, rpcErr)
})

t.Run("Too many blocks back", func(t *testing.T) {
t.Run("Return error if called on pending block", func(t *testing.T) {
mockCtrl := gomock.NewController(t)
t.Cleanup(mockCtrl.Finish)

mockChain := mocks.NewMockReader(mockCtrl)
mockSyncer := mocks.NewMockSyncReader(mockCtrl)
handler := New(mockChain, mockSyncer, nil, "", log)

keys := make([][]felt.Felt, 1)
fromAddr := new(felt.Felt).SetBytes([]byte("from_address"))
blockID := &BlockID{Pending: true}

serverConn, clientConn := net.Pipe()
t.Cleanup(func() {
require.NoError(t, serverConn.Close())
require.NoError(t, clientConn.Close())
})

subCtx := context.WithValue(context.Background(), jsonrpc.ConnKey{}, &fakeConn{w: serverConn})

mockChain.EXPECT().HeadsHeader().Return(&core.Header{Number: 1}, nil)

id, rpcErr := handler.SubscribeEvents(subCtx, fromAddr, keys, blockID)
assert.Zero(t, id)
assert.Equal(t, ErrCallOnPending, rpcErr)
})

t.Run("Return error if block is too far back", func(t *testing.T) {
mockCtrl := gomock.NewController(t)
t.Cleanup(mockCtrl.Finish)

Expand Down

0 comments on commit 62c6df8

Please sign in to comment.