Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ type Reader interface {

TransactionByHash(hash *felt.Felt) (transaction core.Transaction, err error)
TransactionByBlockNumberAndIndex(blockNumber, index uint64) (transaction core.Transaction, err error)
TransactionsByBlockNumber(blockNumber uint64) (transactions []core.Transaction, err error)

Receipt(hash *felt.Felt) (receipt *core.TransactionReceipt, blockHash *felt.Felt, blockNumber uint64, err error)
StateUpdateByNumber(number uint64) (update *core.StateUpdate, err error)
StateUpdateByHash(hash *felt.Felt) (update *core.StateUpdate, err error)
Expand Down Expand Up @@ -199,6 +201,13 @@ func (b *Blockchain) TransactionByHash(hash *felt.Felt) (core.Transaction, error
return core.GetTxByHash(b.database, (*felt.TransactionHash)(hash))
}

// TransactionsByBlockNumber gets all transactions for a given block number
func (b *Blockchain) TransactionsByBlockNumber(number uint64) ([]core.Transaction, error) {
b.listener.OnRead("TransactionsByBlockNumber")
txn := b.database.NewIndexedBatch()
return core.GetTxsByBlockNum(txn, number)
}

// Receipt gets the transaction receipt for a given transaction hash.
// TODO: Return TransactionReceipt instead of *TransactionReceipt.
func (b *Blockchain) Receipt(hash *felt.Felt) (*core.TransactionReceipt, *felt.Felt, uint64, error) {
Expand Down
8 changes: 8 additions & 0 deletions mocks/mock_blockchain.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 23 additions & 13 deletions rpc/v9/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,24 +241,29 @@ func (h *Handler) BlockTransactionCount(id *BlockID) (uint64, *jsonrpc.Error) {
// It follows the specification defined here:
// https://github.com/starkware-libs/starknet-specs/blob/9377851884da5c81f757b6ae0ed47e84f9e7c058/api/starknet_api_openrpc.json#L25
func (h *Handler) BlockWithTxHashes(id *BlockID) (*BlockWithTxHashes, *jsonrpc.Error) {
block, rpcErr := h.blockByID(id)
header, rpcErr := h.blockHeaderByID(id)
if rpcErr != nil {
return nil, rpcErr
}

txnHashes := make([]*felt.Felt, len(block.Transactions))
for index, txn := range block.Transactions {
blockTxns, rpcErr := h.blockTxnsByNumber(header.Number)
if rpcErr != nil {
return nil, rpcErr
}

txnHashes := make([]*felt.Felt, header.TransactionCount)
for index, txn := range blockTxns {
txnHashes[index] = txn.Hash()
}

status, rpcErr := h.blockStatus(id, block)
status, rpcErr := h.blockStatus(id, header.Number)
if rpcErr != nil {
return nil, rpcErr
}

return &BlockWithTxHashes{
Status: status,
BlockHeader: AdaptBlockHeader(block.Header),
BlockHeader: AdaptBlockHeader(header),
TxnHashes: txnHashes,
}, nil
}
Expand All @@ -273,7 +278,7 @@ func (h *Handler) BlockWithReceipts(id *BlockID) (*BlockWithReceipts, *jsonrpc.E
return nil, rpcErr
}

blockStatus, rpcErr := h.blockStatus(id, block)
blockStatus, rpcErr := h.blockStatus(id, block.Number)
if rpcErr != nil {
return nil, rpcErr
}
Expand Down Expand Up @@ -319,29 +324,34 @@ func (h *Handler) BlockWithReceipts(id *BlockID) (*BlockWithReceipts, *jsonrpc.E
// It follows the specification defined here:
// https://github.com/starkware-libs/starknet-specs/blob/9377851884da5c81f757b6ae0ed47e84f9e7c058/api/starknet_api_openrpc.json#L62
func (h *Handler) BlockWithTxs(blockID *BlockID) (*BlockWithTxs, *jsonrpc.Error) {
block, rpcErr := h.blockByID(blockID)
header, rpcErr := h.blockHeaderByID(blockID)
if rpcErr != nil {
return nil, rpcErr
}

txs := make([]*Transaction, len(block.Transactions))
for index, txn := range block.Transactions {
blockTxns, rpcErr := h.blockTxnsByNumber(header.Number)
if rpcErr != nil {
return nil, rpcErr
}

txs := make([]*Transaction, header.TransactionCount)
for index, txn := range blockTxns {
txs[index] = AdaptTransaction(txn)
}

status, rpcErr := h.blockStatus(blockID, block)
status, rpcErr := h.blockStatus(blockID, header.Number)
if rpcErr != nil {
return nil, rpcErr
}

return &BlockWithTxs{
Status: status,
BlockHeader: AdaptBlockHeader(block.Header),
BlockHeader: AdaptBlockHeader(header),
Transactions: txs,
}, nil
}

func (h *Handler) blockStatus(id *BlockID, block *core.Block) (BlockStatus, *jsonrpc.Error) {
func (h *Handler) blockStatus(id *BlockID, blockNumber uint64) (BlockStatus, *jsonrpc.Error) {
l1H, jsonErr := h.l1Head()
if jsonErr != nil {
return 0, jsonErr
Expand All @@ -350,7 +360,7 @@ func (h *Handler) blockStatus(id *BlockID, block *core.Block) (BlockStatus, *jso
status := BlockAcceptedL2
if id.IsPreConfirmed() {
status = BlockPreConfirmed
} else if isL1Verified(block.Number, l1H) {
} else if isL1Verified(blockNumber, l1H) {
status = BlockAcceptedL1
}

Expand Down
12 changes: 12 additions & 0 deletions rpc/v9/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,18 @@ func (h *Handler) blockByID(blockID *BlockID) (*core.Block, *jsonrpc.Error) {
return block, nil
}

func (h *Handler) blockTxnsByNumber(number uint64) ([]core.Transaction, *jsonrpc.Error) {
txns, err := h.bcReader.TransactionsByBlockNumber(number)
if err != nil {
if errors.Is(err, db.ErrKeyNotFound) || errors.Is(err, core.ErrPendingDataNotFound) {
return nil, rpccore.ErrBlockNotFound
}
return nil, rpccore.ErrInternal.CloneWithData(err)
}

return txns, nil
}

func (h *Handler) blockHeaderByID(blockID *BlockID) (*core.Header, *jsonrpc.Error) {
var header *core.Header
var err error
Expand Down
Loading