Skip to content

Commit 7c22412

Browse files
authored
feat(rpc): make v10 self contained (part 2) (#3541)
* feat: copy ChainID method from v9 into v10 + tests * feat: copy BlockNumber, BlockHashAndNumber, and BlockTransactionCount method from v9 into v10 + tests * feat: copy Syncing method from v9 into v10 + tests * feat: copy Nonce method from v9 into v10 + tests * feat: copy ClassHashAt, Class, and ClassAt methods from v9 into v10 + tests * feat: copy CompiledCasm method from v9 into v10 + tests * feat: copy GetMessageStatus method from v9 into v10 + tests * feat: copy StorageProof method from v9 into v10 + tests * fix: linter errors
1 parent 1c01ebe commit 7c22412

19 files changed

Lines changed: 3253 additions & 144 deletions

rpc/handlers.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -144,15 +144,15 @@ func (h *Handler) MethodsV0_10() ([]jsonrpc.Method, string) {
144144
return []jsonrpc.Method{
145145
{
146146
Name: "starknet_chainId",
147-
Handler: h.rpcv6Handler.ChainID,
147+
Handler: h.rpcv10Handler.ChainID,
148148
},
149149
{
150150
Name: "starknet_blockNumber",
151-
Handler: h.rpcv6Handler.BlockNumber,
151+
Handler: h.rpcv10Handler.BlockNumber,
152152
},
153153
{
154154
Name: "starknet_blockHashAndNumber",
155-
Handler: h.rpcv6Handler.BlockHashAndNumber,
155+
Handler: h.rpcv10Handler.BlockHashAndNumber,
156156
},
157157
{
158158
Name: "starknet_getBlockWithTxHashes",
@@ -183,7 +183,7 @@ func (h *Handler) MethodsV0_10() ([]jsonrpc.Method, string) {
183183
{
184184
Name: "starknet_getBlockTransactionCount",
185185
Params: []jsonrpc.Parameter{{Name: "block_id"}},
186-
Handler: h.rpcv9Handler.BlockTransactionCount,
186+
Handler: h.rpcv10Handler.BlockTransactionCount,
187187
},
188188
{
189189
Name: "starknet_getTransactionByBlockIdAndIndex",
@@ -204,12 +204,12 @@ func (h *Handler) MethodsV0_10() ([]jsonrpc.Method, string) {
204204
},
205205
{
206206
Name: "starknet_syncing",
207-
Handler: h.rpcv6Handler.Syncing,
207+
Handler: h.rpcv10Handler.Syncing,
208208
},
209209
{
210210
Name: "starknet_getNonce",
211211
Params: []jsonrpc.Parameter{{Name: "block_id"}, {Name: "contract_address"}},
212-
Handler: h.rpcv9Handler.Nonce,
212+
Handler: h.rpcv10Handler.Nonce,
213213
},
214214
{
215215
Name: "starknet_getStorageAt",
@@ -224,17 +224,17 @@ func (h *Handler) MethodsV0_10() ([]jsonrpc.Method, string) {
224224
{
225225
Name: "starknet_getClassHashAt",
226226
Params: []jsonrpc.Parameter{{Name: "block_id"}, {Name: "contract_address"}},
227-
Handler: h.rpcv9Handler.ClassHashAt,
227+
Handler: h.rpcv10Handler.ClassHashAt,
228228
},
229229
{
230230
Name: "starknet_getClass",
231231
Params: []jsonrpc.Parameter{{Name: "block_id"}, {Name: "class_hash"}},
232-
Handler: h.rpcv9Handler.Class,
232+
Handler: h.rpcv10Handler.Class,
233233
},
234234
{
235235
Name: "starknet_getClassAt",
236236
Params: []jsonrpc.Parameter{{Name: "block_id"}, {Name: "contract_address"}},
237-
Handler: h.rpcv9Handler.ClassAt,
237+
Handler: h.rpcv10Handler.ClassAt,
238238
},
239239
{
240240
Name: "starknet_addInvokeTransaction",
@@ -359,12 +359,12 @@ func (h *Handler) MethodsV0_10() ([]jsonrpc.Method, string) {
359359
{
360360
Name: "starknet_getCompiledCasm",
361361
Params: []jsonrpc.Parameter{{Name: "class_hash"}},
362-
Handler: h.rpcv9Handler.CompiledCasm,
362+
Handler: h.rpcv10Handler.CompiledCasm,
363363
},
364364
{
365365
Name: "starknet_getMessagesStatus",
366366
Params: []jsonrpc.Parameter{{Name: "transaction_hash"}},
367-
Handler: h.rpcv9Handler.GetMessageStatus,
367+
Handler: h.rpcv10Handler.GetMessageStatus,
368368
},
369369
{
370370
Name: "starknet_getStorageProof",
@@ -374,7 +374,7 @@ func (h *Handler) MethodsV0_10() ([]jsonrpc.Method, string) {
374374
{Name: "contract_addresses", Optional: true},
375375
{Name: "contracts_storage_keys", Optional: true},
376376
},
377-
Handler: h.rpcv9Handler.StorageProof,
377+
Handler: h.rpcv10Handler.StorageProof,
378378
},
379379
}, "/v0_10"
380380
}

rpc/v10/block.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,54 @@ type BlockWithReceipts struct {
8484
Transactions []TransactionWithReceipt `json:"transactions"`
8585
}
8686

87+
// https://github.com/starkware-libs/starknet-specs/blob/release/v0.10.2/api/starknet_api_openrpc.json#L830-L848
88+
type BlockHashAndNumber struct {
89+
Hash *felt.Felt `json:"block_hash"`
90+
Number uint64 `json:"block_number"`
91+
}
92+
93+
/****************************************************
94+
Block Handlers
95+
*****************************************************/
96+
97+
// BlockNumber returns the latest synced block number.
98+
//
99+
// It follows the specification defined here:
100+
// https://github.com/starkware-libs/starknet-specs/blob/release/v0.10.2/api/starknet_api_openrpc.json#L809
101+
func (h *Handler) BlockNumber() (uint64, *jsonrpc.Error) {
102+
num, err := h.bcReader.Height()
103+
if err != nil {
104+
return 0, rpccore.ErrNoBlock
105+
}
106+
107+
return num, nil
108+
}
109+
110+
// BlockHashAndNumber returns the block hash and number of the latest synced block.
111+
//
112+
// It follows the specification defined here:
113+
// https://github.com/starkware-libs/starknet-specs/blob/release/v0.10.2/api/starknet_api_openrpc.json#L827
114+
func (h *Handler) BlockHashAndNumber() (*BlockHashAndNumber, *jsonrpc.Error) {
115+
block, err := h.bcReader.Head()
116+
if err != nil {
117+
return nil, rpccore.ErrNoBlock
118+
}
119+
return &BlockHashAndNumber{Number: block.Number, Hash: block.Hash}, nil
120+
}
121+
122+
// BlockTransactionCount returns the number of transactions in a block
123+
// identified by the given BlockID.
124+
//
125+
// It follows the specification defined here:
126+
// https://github.com/starkware-libs/starknet-specs/blob/release/v0.10.2/api/starknet_api_openrpc.json#L622
127+
func (h *Handler) BlockTransactionCount(id *BlockID) (uint64, *jsonrpc.Error) {
128+
header, rpcErr := h.blockHeaderByID(id)
129+
if rpcErr != nil {
130+
return 0, rpcErr
131+
}
132+
return header.TransactionCount, nil
133+
}
134+
87135
// BlockWithTxHashes returns the block information with transaction hashes given a block ID.
88136
//
89137
// It follows the specification defined here:

0 commit comments

Comments
 (0)