Skip to content

Commit

Permalink
Update Trace API for 0.6
Browse files Browse the repository at this point in the history
  • Loading branch information
joshklop authored and omerfirmak committed Nov 27, 2023
1 parent 3b96b8d commit 2dce06f
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 90 deletions.
17 changes: 9 additions & 8 deletions core/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,15 @@ type ExecutionResources struct {
}

type BuiltinInstanceCounter struct {
Pedersen uint64
RangeCheck uint64
Bitwise uint64
Output uint64
Ecsda uint64
EcOp uint64
Keccak uint64
Poseidon uint64
Pedersen uint64
RangeCheck uint64
Bitwise uint64
Output uint64
Ecsda uint64
EcOp uint64
Keccak uint64
Poseidon uint64
SegmentArena uint64
}

type TransactionReceipt struct {
Expand Down
14 changes: 9 additions & 5 deletions rpc/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -1256,6 +1256,10 @@ func (h *Handler) TraceTransaction(ctx context.Context, hash felt.Felt) (json.Ra
return h.traceTransaction(ctx, &hash, false)
}

// LegacyTraceTransaction returns the trace for a given executed transaction, including internal calls
//
// It follows the specification defined here:
// https://github.com/starkware-libs/starknet-specs/blob/1ae810e0137cc5d175ace4554892a4f43052be56/api/starknet_trace_api_openrpc.json#L11
func (h *Handler) LegacyTraceTransaction(ctx context.Context, hash felt.Felt) (json.RawMessage, *jsonrpc.Error) {
return h.traceTransaction(ctx, &hash, true)
}
Expand Down Expand Up @@ -1382,10 +1386,10 @@ func (h *Handler) TraceBlockTransactions(ctx context.Context, id BlockID) ([]Tra
return h.traceBlockTransactions(ctx, block, false)
}

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

return h.traceBlockTransactions(ctx, block, true)
Expand Down Expand Up @@ -1902,7 +1906,7 @@ func (h *Handler) LegacyMethods() ([]jsonrpc.Method, string) { //nolint: funlen
},
{
Name: "starknet_traceBlockTransactions",
Params: []jsonrpc.Parameter{{Name: "block_hash"}},
Params: []jsonrpc.Parameter{{Name: "block_id"}},
Handler: h.LegacyTraceBlockTransactions,
},
}, "/v0_4"
Expand Down
48 changes: 27 additions & 21 deletions rpc/handlers_test.go

Large diffs are not rendered by default.

48 changes: 28 additions & 20 deletions rpc/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type FunctionInvocation struct {
Calls []FunctionInvocation `json:"calls"`
Events []OrderedEvent `json:"events"`
Messages []OrderedL2toL1Message `json:"messages"`
ExecutionResources *ExecutionResources `json:"execution_resources,omitempty"`
}

type ExecuteInvocation struct {
Expand All @@ -40,16 +41,16 @@ type ExecuteInvocation struct {
}

type OrderedEvent struct {
Order *uint64 `json:"order,omitempty"`
Order uint64 `json:"order"`
Event
}

type OrderedL2toL1Message struct {
Order *uint64 `json:"order,omitempty"`
Order uint64 `json:"order"`
MsgToL1
}

func adaptBlockTrace(block *BlockWithTxs, blockTrace *starknet.BlockTrace, legacyTrace bool) ([]TracedBlockTransaction, error) {
func adaptBlockTrace(block *BlockWithTxs, blockTrace *starknet.BlockTrace, _ bool) ([]TracedBlockTransaction, error) {
if blockTrace == nil {
return nil, nil
}
Expand All @@ -60,14 +61,12 @@ func adaptBlockTrace(block *BlockWithTxs, blockTrace *starknet.BlockTrace, legac
for index := range blockTrace.Traces {
feederTrace := &blockTrace.Traces[index]
trace := TransactionTrace{}
if !legacyTrace {
trace.Type = block.Transactions[index].Type
}
trace.Type = block.Transactions[index].Type

trace.FeeTransferInvocation = adaptFunctionInvocation(feederTrace.FeeTransferInvocation, legacyTrace)
trace.ValidateInvocation = adaptFunctionInvocation(feederTrace.ValidateInvocation, legacyTrace)
trace.FeeTransferInvocation = adaptFunctionInvocation(feederTrace.FeeTransferInvocation)
trace.ValidateInvocation = adaptFunctionInvocation(feederTrace.ValidateInvocation)

fnInvocation := adaptFunctionInvocation(feederTrace.FunctionInvocation, legacyTrace)
fnInvocation := adaptFunctionInvocation(feederTrace.FunctionInvocation)
switch block.Transactions[index].Type {
case TxnDeploy:
trace.ConstructorInvocation = fnInvocation
Expand Down Expand Up @@ -97,18 +96,11 @@ func adaptBlockTrace(block *BlockWithTxs, blockTrace *starknet.BlockTrace, legac
return traces, nil
}

func adaptFunctionInvocation(snFnInvocation *starknet.FunctionInvocation, legacyTrace bool) *FunctionInvocation {
func adaptFunctionInvocation(snFnInvocation *starknet.FunctionInvocation) *FunctionInvocation {
if snFnInvocation == nil {
return nil
}

orderPtr := func(o uint64) *uint64 {
if legacyTrace {
return nil
}
return &o
}

fnInvocation := FunctionInvocation{
ContractAddress: snFnInvocation.ContractAddress,
EntryPointSelector: snFnInvocation.Selector,
Expand All @@ -121,14 +113,15 @@ func adaptFunctionInvocation(snFnInvocation *starknet.FunctionInvocation, legacy
Calls: make([]FunctionInvocation, 0, len(snFnInvocation.InternalCalls)),
Events: make([]OrderedEvent, 0, len(snFnInvocation.Events)),
Messages: make([]OrderedL2toL1Message, 0, len(snFnInvocation.Messages)),
ExecutionResources: adaptFeederExecutionResources(&snFnInvocation.ExecutionResources),
}
for index := range snFnInvocation.InternalCalls {
fnInvocation.Calls = append(fnInvocation.Calls, *adaptFunctionInvocation(&snFnInvocation.InternalCalls[index], legacyTrace))
fnInvocation.Calls = append(fnInvocation.Calls, *adaptFunctionInvocation(&snFnInvocation.InternalCalls[index]))
}
for index := range snFnInvocation.Events {
snEvent := &snFnInvocation.Events[index]
fnInvocation.Events = append(fnInvocation.Events, OrderedEvent{
Order: orderPtr(snEvent.Order),
Order: snEvent.Order,
Event: Event{
Keys: utils.Map(snEvent.Keys, utils.Ptr[felt.Felt]),
Data: utils.Map(snEvent.Data, utils.Ptr[felt.Felt]),
Expand All @@ -138,7 +131,7 @@ func adaptFunctionInvocation(snFnInvocation *starknet.FunctionInvocation, legacy
for index := range snFnInvocation.Messages {
snMessage := &snFnInvocation.Messages[index]
fnInvocation.Messages = append(fnInvocation.Messages, OrderedL2toL1Message{
Order: orderPtr(snMessage.Order),
Order: snMessage.Order,
MsgToL1: MsgToL1{
Payload: utils.Map(snMessage.Payload, utils.Ptr[felt.Felt]),
To: common.HexToAddress(snMessage.ToAddr),
Expand All @@ -148,3 +141,18 @@ func adaptFunctionInvocation(snFnInvocation *starknet.FunctionInvocation, legacy

return &fnInvocation
}

func adaptFeederExecutionResources(resources *starknet.ExecutionResources) *ExecutionResources {
builtins := resources.BuiltinInstanceCounter
return &ExecutionResources{
Steps: NumAsHex(resources.Steps),
MemoryHoles: NumAsHex(resources.MemoryHoles),
Pedersen: NumAsHex(builtins.Pedersen),
RangeCheck: NumAsHex(builtins.RangeCheck),
Bitwise: NumAsHex(builtins.Bitwise),
Ecsda: NumAsHex(builtins.Ecsda),
EcOp: NumAsHex(builtins.EcOp),
Keccak: NumAsHex(builtins.Keccak),
Poseidon: NumAsHex(builtins.Poseidon),
}
}
17 changes: 9 additions & 8 deletions starknet/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,14 +202,15 @@ type ExecutionResources struct {
}

type BuiltinInstanceCounter struct {
Pedersen uint64 `json:"pedersen_builtin"`
RangeCheck uint64 `json:"range_check_builtin"`
Bitwise uint64 `json:"bitwise_builtin"`
Output uint64 `json:"output_builtin"`
Ecsda uint64 `json:"ecdsa_builtin"`
EcOp uint64 `json:"ec_op_builtin"`
Keccak uint64 `json:"keccak_builtin"`
Poseidon uint64 `json:"poseidon_builtin"`
Pedersen uint64 `json:"pedersen_builtin"`
RangeCheck uint64 `json:"range_check_builtin"`
Bitwise uint64 `json:"bitwise_builtin"`
Output uint64 `json:"output_builtin"`
Ecsda uint64 `json:"ecdsa_builtin"`
EcOp uint64 `json:"ec_op_builtin"`
Keccak uint64 `json:"keccak_builtin"`
Poseidon uint64 `json:"poseidon_builtin"`
SegmentArena uint64 `json:"segment_arena_builtin"`
}

type TransactionReceipt struct {
Expand Down
Loading

0 comments on commit 2dce06f

Please sign in to comment.