Skip to content

Commit 66f700c

Browse files
authored
Refactor: Reuse v6 BlockTransactionCount handler for v7 and v8 (#2488)
1 parent 382ffb9 commit 66f700c

File tree

6 files changed

+2
-191
lines changed

6 files changed

+2
-191
lines changed

rpc/handlers.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ func (h *Handler) MethodsV0_8() ([]jsonrpc.Method, string) { //nolint: funlen
124124
{
125125
Name: "starknet_getBlockTransactionCount",
126126
Params: []jsonrpc.Parameter{{Name: "block_id"}},
127-
Handler: h.rpcv8Handler.BlockTransactionCount,
127+
Handler: h.rpcv6Handler.BlockTransactionCount,
128128
},
129129
{
130130
Name: "starknet_getTransactionByBlockIdAndIndex",
@@ -318,7 +318,7 @@ func (h *Handler) MethodsV0_7() ([]jsonrpc.Method, string) { //nolint: funlen
318318
{
319319
Name: "starknet_getBlockTransactionCount",
320320
Params: []jsonrpc.Parameter{{Name: "block_id"}},
321-
Handler: h.rpcv7Handler.BlockTransactionCount,
321+
Handler: h.rpcv6Handler.BlockTransactionCount,
322322
},
323323
{
324324
Name: "starknet_getTransactionByBlockIdAndIndex",

rpc/v7/block.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -116,19 +116,6 @@ func (h *Handler) BlockWithTxHashes(id BlockID) (*BlockWithTxHashes, *jsonrpc.Er
116116
}, nil
117117
}
118118

119-
// BlockTransactionCount returns the number of transactions in a block
120-
// identified by the given BlockID.
121-
//
122-
// It follows the specification defined here:
123-
// https://github.com/starkware-libs/starknet-specs/blob/a789ccc3432c57777beceaa53a34a7ae2f25fda0/api/starknet_api_openrpc.json#L373
124-
func (h *Handler) BlockTransactionCount(id BlockID) (uint64, *jsonrpc.Error) {
125-
header, rpcErr := h.blockHeaderByID(&id)
126-
if rpcErr != nil {
127-
return 0, rpcErr
128-
}
129-
return header.TransactionCount, nil
130-
}
131-
132119
func (h *Handler) BlockWithReceipts(id BlockID) (*BlockWithReceipts, *jsonrpc.Error) {
133120
block, rpcErr := h.blockByID(&id)
134121
if rpcErr != nil {

rpc/v7/block_test.go

Lines changed: 0 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -91,85 +91,6 @@ func TestBlockId(t *testing.T) {
9191
}
9292
}
9393

94-
func TestBlockTransactionCount(t *testing.T) {
95-
mockCtrl := gomock.NewController(t)
96-
t.Cleanup(mockCtrl.Finish)
97-
98-
n := utils.Ptr(utils.Sepolia)
99-
mockReader := mocks.NewMockReader(mockCtrl)
100-
mockSyncReader := mocks.NewMockSyncReader(mockCtrl)
101-
handler := rpcv7.New(mockReader, mockSyncReader, nil, "", n, nil)
102-
103-
client := feeder.NewTestClient(t, n)
104-
gw := adaptfeeder.New(client)
105-
106-
latestBlockNumber := uint64(56377)
107-
latestBlock, err := gw.BlockByNumber(context.Background(), latestBlockNumber)
108-
require.NoError(t, err)
109-
latestBlockHash := latestBlock.Hash
110-
expectedCount := latestBlock.TransactionCount
111-
112-
t.Run("empty blockchain", func(t *testing.T) {
113-
mockReader.EXPECT().HeadsHeader().Return(nil, db.ErrKeyNotFound)
114-
115-
count, rpcErr := handler.BlockTransactionCount(rpcv7.BlockID{Latest: true})
116-
assert.Equal(t, uint64(0), count)
117-
assert.Equal(t, rpccore.ErrBlockNotFound, rpcErr)
118-
})
119-
120-
t.Run("non-existent block hash", func(t *testing.T) {
121-
mockReader.EXPECT().BlockHeaderByHash(gomock.Any()).Return(nil, db.ErrKeyNotFound)
122-
123-
count, rpcErr := handler.BlockTransactionCount(rpcv7.BlockID{Hash: new(felt.Felt).SetBytes([]byte("random"))})
124-
assert.Equal(t, uint64(0), count)
125-
assert.Equal(t, rpccore.ErrBlockNotFound, rpcErr)
126-
})
127-
128-
t.Run("non-existent block number", func(t *testing.T) {
129-
mockReader.EXPECT().BlockHeaderByNumber(gomock.Any()).Return(nil, db.ErrKeyNotFound)
130-
131-
count, rpcErr := handler.BlockTransactionCount(rpcv7.BlockID{Number: uint64(328476)})
132-
assert.Equal(t, uint64(0), count)
133-
assert.Equal(t, rpccore.ErrBlockNotFound, rpcErr)
134-
})
135-
136-
t.Run("blockID - latest", func(t *testing.T) {
137-
mockReader.EXPECT().HeadsHeader().Return(latestBlock.Header, nil)
138-
139-
count, rpcErr := handler.BlockTransactionCount(rpcv7.BlockID{Latest: true})
140-
require.Nil(t, rpcErr)
141-
assert.Equal(t, expectedCount, count)
142-
})
143-
144-
t.Run("blockID - hash", func(t *testing.T) {
145-
mockReader.EXPECT().BlockHeaderByHash(latestBlockHash).Return(latestBlock.Header, nil)
146-
147-
count, rpcErr := handler.BlockTransactionCount(rpcv7.BlockID{Hash: latestBlockHash})
148-
require.Nil(t, rpcErr)
149-
assert.Equal(t, expectedCount, count)
150-
})
151-
152-
t.Run("blockID - number", func(t *testing.T) {
153-
mockReader.EXPECT().BlockHeaderByNumber(latestBlockNumber).Return(latestBlock.Header, nil)
154-
155-
count, rpcErr := handler.BlockTransactionCount(rpcv7.BlockID{Number: latestBlockNumber})
156-
require.Nil(t, rpcErr)
157-
assert.Equal(t, expectedCount, count)
158-
})
159-
160-
t.Run("blockID - pending", func(t *testing.T) {
161-
latestBlock.Hash = nil
162-
latestBlock.GlobalStateRoot = nil
163-
mockSyncReader.EXPECT().Pending().Return(&sync.Pending{
164-
Block: latestBlock,
165-
}, nil)
166-
167-
count, rpcErr := handler.BlockTransactionCount(rpcv7.BlockID{Pending: true})
168-
require.Nil(t, rpcErr)
169-
assert.Equal(t, expectedCount, count)
170-
})
171-
}
172-
17394
func TestBlockWithTxHashes(t *testing.T) {
17495
errTests := map[string]rpcv7.BlockID{
17596
"latest": {Latest: true},

rpc/v8/block.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -122,19 +122,6 @@ func (h *Handler) BlockWithTxHashes(id BlockID) (*BlockWithTxHashes, *jsonrpc.Er
122122
}, nil
123123
}
124124

125-
// BlockTransactionCount returns the number of transactions in a block
126-
// identified by the given BlockID.
127-
//
128-
// It follows the specification defined here:
129-
// https://github.com/starkware-libs/starknet-specs/blob/a789ccc3432c57777beceaa53a34a7ae2f25fda0/api/starknet_api_openrpc.json#L373
130-
func (h *Handler) BlockTransactionCount(id BlockID) (uint64, *jsonrpc.Error) {
131-
header, rpcErr := h.blockHeaderByID(&id)
132-
if rpcErr != nil {
133-
return 0, rpcErr
134-
}
135-
return header.TransactionCount, nil
136-
}
137-
138125
func (h *Handler) BlockWithReceipts(id BlockID) (*BlockWithReceipts, *jsonrpc.Error) {
139126
block, rpcErr := h.blockByID(&id)
140127
if rpcErr != nil {

rpc/v8/block_test.go

Lines changed: 0 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -91,85 +91,6 @@ func TestBlockId(t *testing.T) {
9191
}
9292
}
9393

94-
func TestBlockTransactionCount(t *testing.T) {
95-
mockCtrl := gomock.NewController(t)
96-
t.Cleanup(mockCtrl.Finish)
97-
98-
n := utils.Ptr(utils.Sepolia)
99-
mockReader := mocks.NewMockReader(mockCtrl)
100-
mockSyncReader := mocks.NewMockSyncReader(mockCtrl)
101-
handler := rpcv8.New(mockReader, mockSyncReader, nil, "", nil)
102-
103-
client := feeder.NewTestClient(t, n)
104-
gw := adaptfeeder.New(client)
105-
106-
latestBlockNumber := uint64(56377)
107-
latestBlock, err := gw.BlockByNumber(context.Background(), latestBlockNumber)
108-
require.NoError(t, err)
109-
latestBlockHash := latestBlock.Hash
110-
expectedCount := latestBlock.TransactionCount
111-
112-
t.Run("empty blockchain", func(t *testing.T) {
113-
mockReader.EXPECT().HeadsHeader().Return(nil, db.ErrKeyNotFound)
114-
115-
count, rpcErr := handler.BlockTransactionCount(rpcv8.BlockID{Latest: true})
116-
assert.Equal(t, uint64(0), count)
117-
assert.Equal(t, rpccore.ErrBlockNotFound, rpcErr)
118-
})
119-
120-
t.Run("non-existent block hash", func(t *testing.T) {
121-
mockReader.EXPECT().BlockHeaderByHash(gomock.Any()).Return(nil, db.ErrKeyNotFound)
122-
123-
count, rpcErr := handler.BlockTransactionCount(rpcv8.BlockID{Hash: new(felt.Felt).SetBytes([]byte("random"))})
124-
assert.Equal(t, uint64(0), count)
125-
assert.Equal(t, rpccore.ErrBlockNotFound, rpcErr)
126-
})
127-
128-
t.Run("non-existent block number", func(t *testing.T) {
129-
mockReader.EXPECT().BlockHeaderByNumber(gomock.Any()).Return(nil, db.ErrKeyNotFound)
130-
131-
count, rpcErr := handler.BlockTransactionCount(rpcv8.BlockID{Number: uint64(328476)})
132-
assert.Equal(t, uint64(0), count)
133-
assert.Equal(t, rpccore.ErrBlockNotFound, rpcErr)
134-
})
135-
136-
t.Run("blockID - latest", func(t *testing.T) {
137-
mockReader.EXPECT().HeadsHeader().Return(latestBlock.Header, nil)
138-
139-
count, rpcErr := handler.BlockTransactionCount(rpcv8.BlockID{Latest: true})
140-
require.Nil(t, rpcErr)
141-
assert.Equal(t, expectedCount, count)
142-
})
143-
144-
t.Run("blockID - hash", func(t *testing.T) {
145-
mockReader.EXPECT().BlockHeaderByHash(latestBlockHash).Return(latestBlock.Header, nil)
146-
147-
count, rpcErr := handler.BlockTransactionCount(rpcv8.BlockID{Hash: latestBlockHash})
148-
require.Nil(t, rpcErr)
149-
assert.Equal(t, expectedCount, count)
150-
})
151-
152-
t.Run("blockID - number", func(t *testing.T) {
153-
mockReader.EXPECT().BlockHeaderByNumber(latestBlockNumber).Return(latestBlock.Header, nil)
154-
155-
count, rpcErr := handler.BlockTransactionCount(rpcv8.BlockID{Number: latestBlockNumber})
156-
require.Nil(t, rpcErr)
157-
assert.Equal(t, expectedCount, count)
158-
})
159-
160-
t.Run("blockID - pending", func(t *testing.T) {
161-
latestBlock.Hash = nil
162-
latestBlock.GlobalStateRoot = nil
163-
mockSyncReader.EXPECT().Pending().Return(&sync.Pending{
164-
Block: latestBlock,
165-
}, nil)
166-
167-
count, rpcErr := handler.BlockTransactionCount(rpcv8.BlockID{Pending: true})
168-
require.Nil(t, rpcErr)
169-
assert.Equal(t, expectedCount, count)
170-
})
171-
}
172-
17394
func TestBlockWithTxHashes(t *testing.T) {
17495
errTests := map[string]rpcv8.BlockID{
17596
"latest": {Latest: true},

rpc/v8/handlers.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -172,11 +172,6 @@ func (h *Handler) methods() ([]jsonrpc.Method, string) { //nolint: funlen
172172
Params: []jsonrpc.Parameter{{Name: "transaction_hash"}},
173173
Handler: h.TransactionReceiptByHash,
174174
},
175-
{
176-
Name: "starknet_getBlockTransactionCount",
177-
Params: []jsonrpc.Parameter{{Name: "block_id"}},
178-
Handler: h.BlockTransactionCount,
179-
},
180175
{
181176
Name: "starknet_getTransactionByBlockIdAndIndex",
182177
Params: []jsonrpc.Parameter{{Name: "block_id"}, {Name: "index"}},

0 commit comments

Comments
 (0)