|  | 
| 4 | 4 | 	"context" | 
| 5 | 5 | 	"encoding/json" | 
| 6 | 6 | 	"errors" | 
|  | 7 | +	"fmt" | 
| 7 | 8 | 	"net/http" | 
| 8 | 9 | 	"slices" | 
| 9 | 10 | 	"strconv" | 
| @@ -72,46 +73,130 @@ type FunctionInvocation struct { | 
| 72 | 73 | // | 
| 73 | 74 | // It follows the specification defined here: | 
| 74 | 75 | // https://github.com/starkware-libs/starknet-specs/blob/9377851884da5c81f757b6ae0ed47e84f9e7c058/api/starknet_trace_api_openrpc.json#L11 | 
| 75 |  | -func (h *Handler) TraceTransaction(ctx context.Context, hash felt.Felt) (*TransactionTrace, http.Header, *jsonrpc.Error) { | 
|  | 76 | +func (h *Handler) TraceTransaction(ctx context.Context, hash felt.Felt) (TransactionTrace, http.Header, *jsonrpc.Error) { | 
| 76 | 77 | 	_, blockHash, _, err := h.bcReader.Receipt(&hash) | 
| 77 | 78 | 	httpHeader := http.Header{} | 
| 78 | 79 | 	httpHeader.Set(ExecutionStepsHeader, "0") | 
| 79 | 80 | 
 | 
| 80 | 81 | 	if err != nil && !errors.Is(err, db.ErrKeyNotFound) { | 
| 81 |  | -		return nil, httpHeader, rpccore.ErrTxnHashNotFound | 
|  | 82 | +		return TransactionTrace{}, httpHeader, rpccore.ErrTxnHashNotFound | 
| 82 | 83 | 	} | 
| 83 | 84 | 
 | 
| 84 |  | -	var block *core.Block | 
| 85 |  | -	isPendingBlock := blockHash == nil | 
| 86 |  | -	if isPendingBlock { | 
| 87 |  | -		var pending core.PendingData | 
| 88 |  | -		pending, err = h.PendingData() | 
|  | 85 | +	isPreConfirmed := blockHash == nil | 
|  | 86 | +	if !isPreConfirmed { | 
|  | 87 | +		block, err := h.bcReader.BlockByHash(blockHash) | 
| 89 | 88 | 		if err != nil { | 
| 90 | 89 | 			// for traceTransaction handlers there is no block not found error | 
| 91 |  | -			return nil, httpHeader, rpccore.ErrTxnHashNotFound | 
|  | 90 | +			return TransactionTrace{}, httpHeader, rpccore.ErrTxnHashNotFound | 
| 92 | 91 | 		} | 
| 93 |  | -		block = pending.GetBlock() | 
| 94 |  | -	} else { | 
| 95 |  | -		block, err = h.bcReader.BlockByHash(blockHash) | 
| 96 |  | -		if err != nil { | 
| 97 |  | -			// for traceTransaction handlers there is no block not found error | 
| 98 |  | -			return nil, httpHeader, rpccore.ErrTxnHashNotFound | 
|  | 92 | + | 
|  | 93 | +		txIndex := slices.IndexFunc(block.Transactions, func(tx core.Transaction) bool { | 
|  | 94 | +			return tx.Hash().Equal(&hash) | 
|  | 95 | +		}) | 
|  | 96 | + | 
|  | 97 | +		if txIndex == -1 { | 
|  | 98 | +			return TransactionTrace{}, httpHeader, rpccore.ErrTxnHashNotFound | 
|  | 99 | +		} | 
|  | 100 | + | 
|  | 101 | +		blockTraces, httphttpHeader, rpcErr := h.traceBlockTransactions(ctx, block) | 
|  | 102 | +		if rpcErr != nil { | 
|  | 103 | +			return TransactionTrace{}, httphttpHeader, rpcErr | 
| 99 | 104 | 		} | 
|  | 105 | +		return *blockTraces[txIndex].TraceRoot, httphttpHeader, nil | 
|  | 106 | +	} | 
|  | 107 | + | 
|  | 108 | +	var pendingData core.PendingData | 
|  | 109 | +	pendingData, err = h.PendingData() | 
|  | 110 | +	if err != nil { | 
|  | 111 | +		// for traceTransaction handlers there is no block not found error | 
|  | 112 | +		return TransactionTrace{}, httpHeader, rpccore.ErrTxnHashNotFound | 
| 100 | 113 | 	} | 
|  | 114 | +	block := pendingData.GetBlock() | 
| 101 | 115 | 
 | 
| 102 | 116 | 	txIndex := slices.IndexFunc(block.Transactions, func(tx core.Transaction) bool { | 
| 103 | 117 | 		return tx.Hash().Equal(&hash) | 
| 104 | 118 | 	}) | 
| 105 | 119 | 	if txIndex == -1 { | 
| 106 |  | -		return nil, httpHeader, rpccore.ErrTxnHashNotFound | 
|  | 120 | +		return TransactionTrace{}, httpHeader, rpccore.ErrTxnHashNotFound | 
|  | 121 | +	} | 
|  | 122 | + | 
|  | 123 | +	switch v := pendingData.Variant(); v { | 
|  | 124 | +	case core.PendingBlockVariant: | 
|  | 125 | +		blockTraces, httphttpHeader, rpcErr := h.traceBlockTransactions(ctx, block) | 
|  | 126 | +		if rpcErr != nil { | 
|  | 127 | +			return TransactionTrace{}, httphttpHeader, rpcErr | 
|  | 128 | +		} | 
|  | 129 | +		return *blockTraces[txIndex].TraceRoot, httphttpHeader, nil | 
|  | 130 | +	case core.PreConfirmedBlockVariant: | 
|  | 131 | +		return h.tracePreConfirmedTransaction(block, txIndex) | 
|  | 132 | +	default: | 
|  | 133 | +		panic(fmt.Errorf("unknown pending data variant: %v", v)) | 
|  | 134 | +	} | 
|  | 135 | +} | 
|  | 136 | + | 
|  | 137 | +func (h *Handler) tracePreConfirmedTransaction(block *core.Block, txIndex int) (TransactionTrace, http.Header, *jsonrpc.Error) { | 
|  | 138 | +	httpHeader := http.Header{} | 
|  | 139 | +	httpHeader.Set(ExecutionStepsHeader, "0") | 
|  | 140 | +	state, stateCloser, err := h.syncReader.PendingStateBeforeIndex(txIndex) | 
|  | 141 | +	if err != nil { | 
|  | 142 | +		return TransactionTrace{}, httpHeader, jsonrpc.Err(jsonrpc.InternalError, err.Error()) | 
| 107 | 143 | 	} | 
|  | 144 | +	defer h.callAndLogErr(stateCloser, "Failed to close head state in TraceTransaction") | 
|  | 145 | + | 
|  | 146 | +	var classes []core.Class | 
|  | 147 | +	paidFeesOnL1 := []*felt.Felt{} | 
|  | 148 | + | 
|  | 149 | +	transaction := block.Transactions[txIndex] | 
|  | 150 | +	switch tx := transaction.(type) { | 
|  | 151 | +	/// TODO(Ege): decide what to do with this, should be an edge case | 
|  | 152 | +	case *core.DeclareTransaction: | 
|  | 153 | +		class, stateErr := state.Class(tx.ClassHash) | 
|  | 154 | +		if stateErr != nil { | 
|  | 155 | +			return TransactionTrace{}, httpHeader, jsonrpc.Err(jsonrpc.InternalError, stateErr.Error()) | 
|  | 156 | +		} | 
|  | 157 | +		classes = append(classes, class.Class) | 
|  | 158 | +	case *core.L1HandlerTransaction: | 
|  | 159 | +		var fee felt.Felt | 
|  | 160 | +		paidFeesOnL1 = append(paidFeesOnL1, fee.SetUint64(1)) | 
|  | 161 | +	} | 
|  | 162 | + | 
|  | 163 | +	blockHashToBeRevealed, err := h.getRevealedBlockHash(block.Number) | 
|  | 164 | +	if err != nil { | 
|  | 165 | +		return TransactionTrace{}, httpHeader, rpccore.ErrInternal.CloneWithData(err) | 
|  | 166 | +	} | 
|  | 167 | +	network := h.bcReader.Network() | 
|  | 168 | +	header := block.Header | 
|  | 169 | +	blockInfo := vm.BlockInfo{ | 
|  | 170 | +		Header:                header, | 
|  | 171 | +		BlockHashToBeRevealed: blockHashToBeRevealed, | 
|  | 172 | +	} | 
|  | 173 | + | 
|  | 174 | +	executionResult, err := h.vm.Execute([]core.Transaction{transaction}, classes, paidFeesOnL1, | 
|  | 175 | +		&blockInfo, state, network, false, false, false, true, false) | 
|  | 176 | + | 
|  | 177 | +	httpHeader.Set(ExecutionStepsHeader, strconv.FormatUint(executionResult.NumSteps, 10)) | 
|  | 178 | + | 
|  | 179 | +	if err != nil { | 
|  | 180 | +		if errors.Is(err, utils.ErrResourceBusy) { | 
|  | 181 | +			return TransactionTrace{}, httpHeader, rpccore.ErrInternal.CloneWithData(rpccore.ThrottledVMErr) | 
|  | 182 | +		} | 
|  | 183 | +		// Since we are tracing an existing block, we know that there should be no errors during execution. If we encounter any, | 
|  | 184 | +		// report them as unexpected errors | 
|  | 185 | +		return TransactionTrace{}, httpHeader, rpccore.ErrUnexpectedError.CloneWithData(err.Error()) | 
|  | 186 | +	} | 
|  | 187 | + | 
|  | 188 | +	// Adapt vm transaction trace to rpc v9 trace and add root level execution resources | 
|  | 189 | +	trace := AdaptVMTransactionTrace(&executionResult.Traces[0]) | 
| 108 | 190 | 
 | 
| 109 |  | -	traceResults, header, traceBlockErr := h.traceBlockTransactions(ctx, block) | 
| 110 |  | -	if traceBlockErr != nil { | 
| 111 |  | -		return nil, header, traceBlockErr | 
|  | 191 | +	trace.ExecutionResources = &ExecutionResources{ | 
|  | 192 | +		InnerExecutionResources: InnerExecutionResources{ | 
|  | 193 | +			L1Gas: executionResult.GasConsumed[0].L1Gas, | 
|  | 194 | +			L2Gas: executionResult.GasConsumed[0].L2Gas, | 
|  | 195 | +		}, | 
|  | 196 | +		L1DataGas: executionResult.GasConsumed[0].L1DataGas, | 
| 112 | 197 | 	} | 
| 113 | 198 | 
 | 
| 114 |  | -	return traceResults[txIndex].TraceRoot, header, nil | 
|  | 199 | +	return trace, httpHeader, nil | 
| 115 | 200 | } | 
| 116 | 201 | 
 | 
| 117 | 202 | // TraceBlockTransactions returns the trace for a given blockID | 
|  | 
0 commit comments