Skip to content

Commit a9f794f

Browse files
authored
Handle pending block case in traceTransaction methods (#1833)
1 parent c04fa8e commit a9f794f

File tree

4 files changed

+268
-15
lines changed

4 files changed

+268
-15
lines changed

.github/workflows/starknet-js-tests.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ jobs:
2929
run: npm ci
3030

3131
- name: Run tests
32-
run: npm test -- rpcProvider.test.ts transactionReceipt.test.ts rpcChannel.test.ts defaultProvider.test.ts contract.test.ts cairo1v2.test.ts cairo1v2_typed.test.ts cairo1.test.ts account.test.ts account.starknetId.test.ts --testNamePattern="^(?!.*traceTransaction|.*declare Sierra 1.5.0).*$"
32+
run: npm test -- rpcProvider.test.ts transactionReceipt.test.ts rpcChannel.test.ts defaultProvider.test.ts contract.test.ts cairo1v2.test.ts cairo1v2_typed.test.ts cairo1.test.ts account.test.ts account.starknetId.test.ts --testNamePattern="^(?!.*declare Sierra 1.5.0).*$"
3333
env:
3434
TEST_RPC_URL: ${{ secrets.TEST_RPC_URL }}
3535
TEST_ACCOUNT_ADDRESS: ${{ secrets.TEST_ACCOUNT_ADDRESS }}

clients/gateway/gateway_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ func TestAddInvokeTx(t *testing.T) {
1414
client := gateway.NewTestClient(t)
1515

1616
t.Run("Correct request", func(t *testing.T) {
17-
invokeTx := "{\"max_fee\":\"0x1\",\"version\":\"0x1\",\"signature\":[],\"nonce\":\"0x1\",\"type\":\"INVOKE\",\"sender_address\":\"0x326e3db4580b94948ca9d1d87fa359f2fa047a31a34757734a86aa4231fb9bb\",\"calldata\":[]}"
17+
invokeTx := `{"max_fee":"0x1","version":"0x1","signature":[],"nonce":"0x1","type":"INVOKE","sender_address":"0x326e3db4580b94948ca9d1d87fa359f2fa047a31a34757734a86aa4231fb9bb","calldata":[]}`
1818

1919
invokeTxByte, err := json.Marshal(invokeTx)
2020
require.NoError(t, err)

rpc/trace.go

+17-4
Original file line numberDiff line numberDiff line change
@@ -136,14 +136,27 @@ func (h *Handler) TraceTransactionV0_6(ctx context.Context, hash felt.Felt) (*vm
136136
}
137137

138138
func (h *Handler) traceTransaction(ctx context.Context, hash *felt.Felt, v0_6Response bool) (*vm.TransactionTrace, *jsonrpc.Error) {
139-
_, _, blockNumber, err := h.bcReader.Receipt(hash)
139+
_, blockHash, _, err := h.bcReader.Receipt(hash)
140140
if err != nil {
141141
return nil, ErrTxnHashNotFound
142142
}
143143

144-
block, err := h.bcReader.BlockByNumber(blockNumber)
145-
if err != nil {
146-
return nil, ErrBlockNotFound
144+
var block *core.Block
145+
isPendingBlock := blockHash == nil
146+
if isPendingBlock {
147+
var pending blockchain.Pending
148+
pending, err = h.bcReader.Pending()
149+
if err != nil {
150+
// for traceTransaction handlers there is no block not found error
151+
return nil, ErrTxnHashNotFound
152+
}
153+
block = pending.Block
154+
} else {
155+
block, err = h.bcReader.BlockByHash(blockHash)
156+
if err != nil {
157+
// for traceTransaction handlers there is no block not found error
158+
return nil, ErrTxnHashNotFound
159+
}
147160
}
148161

149162
txIndex := slices.IndexFunc(block.Transactions, func(tx core.Transaction) bool {

0 commit comments

Comments
 (0)