Skip to content

Commit

Permalink
Update trace api according to spec v0.5.0 (#1230)
Browse files Browse the repository at this point in the history
  • Loading branch information
kirugan authored Sep 25, 2023
1 parent ecf4b57 commit d289239
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 50 deletions.
16 changes: 10 additions & 6 deletions rpc/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -1244,15 +1244,19 @@ func (h *Handler) SimulateTransactions(id BlockID, transactions []BroadcastedTra
return result, nil
}

func (h *Handler) TraceBlockTransactions(blockHash felt.Felt) ([]TracedBlockTransaction, *jsonrpc.Error) {
block, err := h.bcReader.BlockByHash(&blockHash)
if err != nil {
return nil, ErrInvalidBlockHash
func (h *Handler) TraceBlockTransactions(id BlockID) ([]TracedBlockTransaction, *jsonrpc.Error) {
block, err := h.blockByID(&id)
if block == nil || err != nil {
return nil, ErrBlockNotFound
}

return h.traceBlockTransactions(block, len(block.Transactions))
}

func (h *Handler) LegacyTraceBlockTransactions(hash felt.Felt) ([]TracedBlockTransaction, *jsonrpc.Error) {
return h.TraceBlockTransactions(BlockID{Hash: &hash})
}

func (h *Handler) traceBlockTransactions(block *core.Block, numTxns int) ([]TracedBlockTransaction, *jsonrpc.Error) {
isPending := block.Hash == nil

Expand Down Expand Up @@ -1473,7 +1477,7 @@ func (h *Handler) Methods() ([]jsonrpc.Method, string) { //nolint: funlen
},
{
Name: "starknet_traceBlockTransactions",
Params: []jsonrpc.Parameter{{Name: "block_hash"}},
Params: []jsonrpc.Parameter{{Name: "block_id"}},
Handler: h.TraceBlockTransactions,
},
{
Expand Down Expand Up @@ -1622,7 +1626,7 @@ func (h *Handler) LegacyMethods() ([]jsonrpc.Method, string) { //nolint: funlen
{
Name: "starknet_traceBlockTransactions",
Params: []jsonrpc.Parameter{{Name: "block_hash"}},
Handler: h.TraceBlockTransactions,
Handler: h.LegacyTraceBlockTransactions,
},
}, "/v0_4"
}
8 changes: 4 additions & 4 deletions rpc/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2163,8 +2163,8 @@ func TestTraceBlockTransactions(t *testing.T) {
blockHash := utils.HexToFelt(t, "0x0001")
mockReader.EXPECT().BlockByHash(blockHash).Return(nil, errors.New("some new err"))

result, err := handler.TraceBlockTransactions(*blockHash)
require.Equal(t, rpc.ErrInvalidBlockHash, err)
result, err := handler.TraceBlockTransactions(rpc.BlockID{Hash: blockHash})
require.Equal(t, rpc.ErrBlockNotFound, err)
assert.Nil(t, result)
})
t.Run("pending block", func(t *testing.T) {
Expand Down Expand Up @@ -2210,7 +2210,7 @@ func TestTraceBlockTransactions(t *testing.T) {
mockVM.EXPECT().Execute(block.Transactions, []core.Class{declaredClass.Class}, height+1, header.Timestamp, sequencerAddress,
state, network, paidL1Fees, false, header.GasPrice).Return(nil, []json.RawMessage{vmTrace, vmTrace}, nil)

result, err := handler.TraceBlockTransactions(*blockHash)
result, err := handler.TraceBlockTransactions(rpc.BlockID{Hash: blockHash})
require.Nil(t, err)
assert.Equal(t, vmTrace, result[0].TraceRoot)
assert.Equal(t, l1Tx.TransactionHash, result[0].TransactionHash)
Expand Down Expand Up @@ -2259,7 +2259,7 @@ func TestTraceBlockTransactions(t *testing.T) {
TraceRoot: vmTrace,
},
}
result, err := handler.TraceBlockTransactions(*blockHash)
result, err := handler.TraceBlockTransactions(rpc.BlockID{Hash: blockHash})
require.Nil(t, err)
assert.Equal(t, expectedResult, result)
})
Expand Down
101 changes: 61 additions & 40 deletions vm/rust/src/jsonrpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,43 @@ use starknet_api::transaction::{Calldata, EthAddress, EventContent, L2ToL1Payloa
use starknet_api::transaction::{Transaction as StarknetApiTransaction};

#[derive(Serialize)]
#[serde(untagged)]
pub enum TransactionTrace {
// used for INVOKE_TXN_TRACE and DECLARE_TXN_TRACE
Common {
#[serde(skip_serializing_if = "Option::is_none")]
validate_invocation: Option<FunctionInvocation>,
#[serde(skip_serializing_if = "Option::is_none")]
execute_invocation: Option<ExecuteInvocation>,
#[serde(skip_serializing_if = "Option::is_none")]
fee_transfer_invocation: Option<FunctionInvocation>,
},
DeployAccount {
#[serde(skip_serializing_if = "Option::is_none")]
validate_invocation: Option<FunctionInvocation>,
#[serde(skip_serializing_if = "Option::is_none")]
constructor_invocation: Option<FunctionInvocation>,
#[serde(skip_serializing_if = "Option::is_none")]
fee_transfer_invocation: Option<FunctionInvocation>,
},
L1Handler {
#[serde(skip_serializing_if = "Option::is_none")]
function_invocation: Option<FunctionInvocation>
#[serde(rename_all = "UPPERCASE")]
pub enum TransactionType {
// dummy type for implementing Default trait
Unknown,
Invoke,
Declare,
#[serde(rename = "DEPLOY_ACCOUNT")]
DeployAccount,
#[serde(rename = "L1_HANDLER")]
L1Handler,
}

#[derive(Serialize)]
pub struct TransactionTrace {
#[serde(skip_serializing_if = "Option::is_none")]
validate_invocation: Option<FunctionInvocation>,
#[serde(skip_serializing_if = "Option::is_none")]
execute_invocation: Option<ExecuteInvocation>,
#[serde(skip_serializing_if = "Option::is_none")]
fee_transfer_invocation: Option<FunctionInvocation>,
#[serde(skip_serializing_if = "Option::is_none")]
constructor_invocation: Option<FunctionInvocation>,
#[serde(skip_serializing_if = "Option::is_none")]
function_invocation: Option<FunctionInvocation>,
r#type: TransactionType,
}

impl Default for TransactionTrace {
fn default() -> Self {
Self {
validate_invocation: None,
execute_invocation: None,
fee_transfer_invocation: None,
constructor_invocation: None,
function_invocation: None,
r#type: TransactionType::Unknown,
}
}
}

Expand All @@ -44,34 +59,40 @@ pub enum ExecuteInvocation {

type BlockifierTxInfo = blockifier::transaction::objects::TransactionExecutionInfo;
pub fn new_transaction_trace(tx: StarknetApiTransaction, info: BlockifierTxInfo) -> TransactionTrace {
let mut trace = TransactionTrace::default();

match tx {
StarknetApiTransaction::L1Handler(_) => {
TransactionTrace::L1Handler {
function_invocation: info.execute_call_info.map(|v| v.into()),
}
trace.function_invocation = info.execute_call_info.map(|v| v.into());
trace.r#type = TransactionType::L1Handler;
},
StarknetApiTransaction::DeployAccount(_) => {
TransactionTrace::DeployAccount {
validate_invocation: info.validate_call_info.map(|v| v.into()),
constructor_invocation: info.execute_call_info.map(|v| v.into()),
fee_transfer_invocation: info.fee_transfer_call_info.map(|v| v.into()),
}
trace.validate_invocation = info.validate_call_info.map(|v| v.into());
trace.constructor_invocation = info.execute_call_info.map(|v| v.into());
trace.fee_transfer_invocation = info.fee_transfer_call_info.map(|v| v.into());
trace.r#type = TransactionType::DeployAccount;
},
StarknetApiTransaction::Declare(_) | StarknetApiTransaction::Invoke(_) => {
TransactionTrace::Common {
validate_invocation: info.validate_call_info.map(|v| v.into()),
execute_invocation: match info.revert_error {
Some(str) => Some(ExecuteInvocation::Revert{revert_reason: str}),
None => info.execute_call_info.map(|v| ExecuteInvocation::Ok(v.into())),
},
fee_transfer_invocation: info.fee_transfer_call_info.map(|v| v.into()),
}
StarknetApiTransaction::Invoke(_) => {
trace.validate_invocation = info.validate_call_info.map(|v| v.into());
trace.execute_invocation = match info.revert_error {
Some(str) => Some(ExecuteInvocation::Revert{revert_reason: str}),
None => info.execute_call_info.map(|v| ExecuteInvocation::Ok(v.into())),
};
trace.fee_transfer_invocation = info.fee_transfer_call_info.map(|v| v.into());
trace.r#type = TransactionType::Invoke;
},
StarknetApiTransaction::Declare(_) => {
trace.validate_invocation = info.validate_call_info.map(|v| v.into());
trace.fee_transfer_invocation = info.fee_transfer_call_info.map(|v| v.into());
trace.r#type = TransactionType::Declare;
},
StarknetApiTransaction::Deploy(_) => {
// shouldn't happen since we don't support deploy
panic!("Can't create transaction trace for deploy transaction (unsupported)");
}
}
};

trace
}

#[derive(Serialize)]
Expand Down

0 comments on commit d289239

Please sign in to comment.