Skip to content

Commit 9ff4bb6

Browse files
Correctly return block and receipt status in rpc calls (#828)
Co-authored-by: Ömer Faruk IRMAK <[email protected]>
1 parent 6ab8bc9 commit 9ff4bb6

File tree

4 files changed

+211
-23
lines changed

4 files changed

+211
-23
lines changed

blockchain/blockchain.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ type Reader interface {
1919
Height() (height uint64, err error)
2020

2121
Head() (head *core.Block, err error)
22+
L1Head() (*core.L1Head, error)
2223
BlockByNumber(number uint64) (block *core.Block, err error)
2324
BlockByHash(hash *felt.Felt) (block *core.Block, err error)
2425

@@ -270,10 +271,18 @@ func (b *Blockchain) Receipt(hash *felt.Felt) (*core.TransactionReceipt, *felt.F
270271

271272
func (b *Blockchain) L1Head() (*core.L1Head, error) {
272273
var update *core.L1Head
273-
if err := b.database.View(func(txn db.Transaction) error {
274-
return txn.Get(db.L1Height.Key(), func(updateBytes []byte) error {
275-
return encoder.Unmarshal(updateBytes, &update)
276-
})
274+
275+
return update, b.database.View(func(txn db.Transaction) error {
276+
var err error
277+
update, err = l1Head(txn)
278+
return err
279+
})
280+
}
281+
282+
func l1Head(txn db.Transaction) (*core.L1Head, error) {
283+
var update *core.L1Head
284+
if err := txn.Get(db.L1Height.Key(), func(updateBytes []byte) error {
285+
return encoder.Unmarshal(updateBytes, &update)
277286
}); err != nil {
278287
return nil, err
279288
}

mocks/mock_blockchain.go

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rpc/handlers.go

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/NethermindEth/juno/blockchain"
99
"github.com/NethermindEth/juno/core"
1010
"github.com/NethermindEth/juno/core/felt"
11+
"github.com/NethermindEth/juno/db"
1112
"github.com/NethermindEth/juno/jsonrpc"
1213
"github.com/NethermindEth/juno/sync"
1314
"github.com/NethermindEth/juno/utils"
@@ -110,17 +111,41 @@ func (h *Handler) BlockWithTxHashes(id BlockID) (*BlockWithTxHashes, *jsonrpc.Er
110111
txnHashes[index] = txn.Hash()
111112
}
112113

114+
l1H, jsonErr := h.l1Head()
115+
if jsonErr != nil {
116+
return nil, jsonErr
117+
}
118+
113119
status := StatusAcceptedL2
114120
if id.Pending {
115121
status = StatusPending
122+
} else if isL1Verified(block.Number, l1H) {
123+
status = StatusAcceptedL1
116124
}
125+
117126
return &BlockWithTxHashes{
118127
Status: status,
119128
BlockHeader: adaptBlockHeader(block.Header),
120129
TxnHashes: txnHashes,
121130
}, nil
122131
}
123132

133+
func (h *Handler) l1Head() (*core.L1Head, *jsonrpc.Error) {
134+
l1Head, err := h.bcReader.L1Head()
135+
if err != nil && !errors.Is(err, db.ErrKeyNotFound) {
136+
return nil, jsonrpc.Err(jsonrpc.InternalError, err.Error())
137+
}
138+
// nil is returned if l1 head doesn't exist
139+
return l1Head, nil
140+
}
141+
142+
func isL1Verified(n uint64, l1 *core.L1Head) bool {
143+
if l1 != nil && l1.BlockNumber >= n {
144+
return true
145+
}
146+
return false
147+
}
148+
124149
func adaptBlockHeader(header *core.Header) BlockHeader {
125150
var blockNumber *uint64
126151
// if header.Hash == nil it's a pending block
@@ -153,10 +178,18 @@ func (h *Handler) BlockWithTxs(id BlockID) (*BlockWithTxs, *jsonrpc.Error) {
153178
txs[index] = adaptTransaction(txn)
154179
}
155180

181+
l1H, jsonErr := h.l1Head()
182+
if jsonErr != nil {
183+
return nil, jsonErr
184+
}
185+
156186
status := StatusAcceptedL2
157187
if id.Pending {
158188
status = StatusPending
189+
} else if isL1Verified(block.Number, l1H) {
190+
status = StatusAcceptedL1
159191
}
192+
160193
return &BlockWithTxs{
161194
Status: status,
162195
BlockHeader: adaptBlockHeader(block.Header),
@@ -383,11 +416,26 @@ func (h *Handler) TransactionReceiptByHash(hash felt.Felt) (*TransactionReceipt,
383416
}
384417

385418
var receiptBlockNumber *uint64
419+
status := StatusAcceptedL2
420+
386421
if blockHash != nil {
387422
receiptBlockNumber = &blockNumber
423+
424+
l1H, jsonErr := h.l1Head()
425+
if jsonErr != nil {
426+
return nil, jsonErr
427+
}
428+
429+
if isL1Verified(blockNumber, l1H) {
430+
status = StatusAcceptedL1
431+
}
432+
} else {
433+
// Todo: Remove after starknet v0.12.0 is released. As Pending status will be removed from Transactions and only exist for blocks
434+
status = StatusPending
388435
}
436+
389437
return &TransactionReceipt{
390-
Status: StatusAcceptedL2, // todo
438+
Status: status,
391439
Type: txn.Type,
392440
Hash: txn.Hash,
393441
ActualFee: receipt.Fee,

0 commit comments

Comments
 (0)