Skip to content

Commit

Permalink
Fix traces (fees, state diff, events) (#2118)
Browse files Browse the repository at this point in the history
  • Loading branch information
rianhughes authored Oct 15, 2024
1 parent 384fe56 commit f83a70e
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 81 deletions.
4 changes: 2 additions & 2 deletions mocks/mock_vm.go

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

8 changes: 4 additions & 4 deletions node/throttled_vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ func (tvm *ThrottledVM) Call(callInfo *vm.CallInfo, blockInfo *vm.BlockInfo, sta
func (tvm *ThrottledVM) Execute(txns []core.Transaction, declaredClasses []core.Class, paidFeesOnL1 []*felt.Felt,
blockInfo *vm.BlockInfo, state core.StateReader, network *utils.Network, skipChargeFee, skipValidate, errOnRevert,
useBlobData bool,
) ([]*felt.Felt, []*felt.Felt, []vm.TransactionTrace, uint64, error) {
) ([]*felt.Felt, []core.GasConsumed, []vm.TransactionTrace, uint64, error) {
var ret []*felt.Felt
var traces []vm.TransactionTrace
var dataGasConsumed []*felt.Felt
var daGas []core.GasConsumed
var numSteps uint64
return ret, dataGasConsumed, traces, numSteps, tvm.Do(func(vm *vm.VM) error {
return ret, daGas, traces, numSteps, tvm.Do(func(vm *vm.VM) error {
var err error
ret, dataGasConsumed, traces, numSteps, err = (*vm).Execute(txns, declaredClasses, paidFeesOnL1, blockInfo, state, network,
ret, daGas, traces, numSteps, err = (*vm).Execute(txns, declaredClasses, paidFeesOnL1, blockInfo, state, network,
skipChargeFee, skipValidate, errOnRevert, useBlobData)
return err
})
Expand Down
9 changes: 5 additions & 4 deletions rpc/estimate_fee_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,16 @@ func TestEstimateMessageFeeV0_6(t *testing.T) {
}, gomock.Any(), &utils.Mainnet, gomock.Any(), false, true, false).DoAndReturn(
func(txns []core.Transaction, declaredClasses []core.Class, paidFeesOnL1 []*felt.Felt, blockInfo *vm.BlockInfo,
state core.StateReader, network *utils.Network, skipChargeFee, skipValidate, errOnRevert, useBlobData bool,
) ([]*felt.Felt, []*felt.Felt, []vm.TransactionTrace, uint64, error) {
) ([]*felt.Felt, []core.GasConsumed, []vm.TransactionTrace, uint64, error) {
require.Len(t, txns, 1)
assert.NotNil(t, txns[0].(*core.L1HandlerTransaction))

assert.Empty(t, declaredClasses)
assert.Len(t, paidFeesOnL1, 1)

actualFee := new(felt.Felt).Mul(expectedGasConsumed, blockInfo.Header.GasPrice)
return []*felt.Felt{actualFee}, []*felt.Felt{&felt.Zero}, []vm.TransactionTrace{{
daGas := []core.GasConsumed{{L1Gas: 0, L1DataGas: 0}}
return []*felt.Felt{actualFee}, daGas, []vm.TransactionTrace{{
StateDiff: &vm.StateDiff{
StorageDiffs: []vm.StorageDiff{},
Nonces: []vm.Nonce{},
Expand Down Expand Up @@ -116,7 +117,7 @@ func TestEstimateFee(t *testing.T) {
blockInfo := vm.BlockInfo{Header: &core.Header{}}
t.Run("ok with zero values", func(t *testing.T) {
mockVM.EXPECT().Execute([]core.Transaction{}, nil, []*felt.Felt{}, &blockInfo, mockState, n, true, false, true, true).
Return([]*felt.Felt{}, []*felt.Felt{}, []vm.TransactionTrace{}, uint64(123), nil)
Return([]*felt.Felt{}, []core.GasConsumed{}, []vm.TransactionTrace{}, uint64(123), nil)

_, httpHeader, err := handler.EstimateFee([]rpc.BroadcastedTransaction{}, []rpc.SimulationFlag{}, rpc.BlockID{Latest: true})
require.Nil(t, err)
Expand All @@ -125,7 +126,7 @@ func TestEstimateFee(t *testing.T) {

t.Run("ok with zero values, skip validate", func(t *testing.T) {
mockVM.EXPECT().Execute([]core.Transaction{}, nil, []*felt.Felt{}, &blockInfo, mockState, n, true, true, true, true).
Return([]*felt.Felt{}, []*felt.Felt{}, []vm.TransactionTrace{}, uint64(123), nil)
Return([]*felt.Felt{}, []core.GasConsumed{}, []vm.TransactionTrace{}, uint64(123), nil)

_, httpHeader, err := handler.EstimateFee([]rpc.BroadcastedTransaction{}, []rpc.SimulationFlag{rpc.SkipValidateFlag}, rpc.BlockID{Latest: true})
require.Nil(t, err)
Expand Down
12 changes: 8 additions & 4 deletions rpc/simulation.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func (h *Handler) simulateTransactions(id BlockID, transactions []BroadcastedTra
BlockHashToBeRevealed: blockHashToBeRevealed,
}
useBlobData := !v0_6Response
overallFees, dataGasConsumed, traces, numSteps, err := h.vm.Execute(txns, classes, paidFeesOnL1, &blockInfo,
overallFees, daGas, traces, numSteps, err := h.vm.Execute(txns, classes, paidFeesOnL1, &blockInfo,
state, h.bcReader.Network(), skipFeeCharge, skipValidate, errOnRevert, useBlobData)

httpHeader.Set(ExecutionStepsHeader, strconv.FormatUint(numSteps, 10))
Expand Down Expand Up @@ -152,8 +152,9 @@ func (h *Handler) simulateTransactions(id BlockID, transactions []BroadcastedTra
}

var gasConsumed *felt.Felt
daGasL1DataGas := new(felt.Felt).SetUint64(daGas[i].L1DataGas)
if !v0_6Response {
dataGasFee := new(felt.Felt).Mul(dataGasConsumed[i], dataGasPrice)
dataGasFee := new(felt.Felt).Mul(daGasL1DataGas, dataGasPrice)
gasConsumed = new(felt.Felt).Sub(overallFee, dataGasFee)
} else {
gasConsumed = overallFee.Clone()
Expand All @@ -163,7 +164,7 @@ func (h *Handler) simulateTransactions(id BlockID, transactions []BroadcastedTra
estimate := FeeEstimate{
GasConsumed: gasConsumed,
GasPrice: gasPrice,
DataGasConsumed: dataGasConsumed[i],
DataGasConsumed: daGasL1DataGas,
DataGasPrice: dataGasPrice,
OverallFee: overallFee,
Unit: utils.Ptr(feeUnit),
Expand All @@ -173,7 +174,10 @@ func (h *Handler) simulateTransactions(id BlockID, transactions []BroadcastedTra
if !v0_6Response {
trace := traces[i]
executionResources := trace.TotalExecutionResources()
executionResources.DataAvailability = vm.NewDataAvailability(gasConsumed, dataGasConsumed[i], header.L1DAMode)
executionResources.DataAvailability = &vm.DataAvailability{
L1Gas: daGas[i].L1Gas,
L1DataGas: daGas[i].L1DataGas,
}
traces[i].ExecutionResources = executionResources
}

Expand Down
4 changes: 2 additions & 2 deletions rpc/simulation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestSimulateTransactionsV0_6(t *testing.T) {
mockVM.EXPECT().Execute([]core.Transaction{}, nil, []*felt.Felt{}, &vm.BlockInfo{
Header: headsHeader,
}, mockState, n, true, false, false, false).
Return([]*felt.Felt{}, []*felt.Felt{}, []vm.TransactionTrace{}, stepsUsed, nil)
Return([]*felt.Felt{}, []core.GasConsumed{}, []vm.TransactionTrace{}, stepsUsed, nil)

_, httpHeader, err := handler.SimulateTransactionsV0_6(rpc.BlockID{Latest: true}, []rpc.BroadcastedTransaction{}, []rpc.SimulationFlag{rpc.SkipFeeChargeFlag})
require.Nil(t, err)
Expand All @@ -51,7 +51,7 @@ func TestSimulateTransactionsV0_6(t *testing.T) {
mockVM.EXPECT().Execute([]core.Transaction{}, nil, []*felt.Felt{}, &vm.BlockInfo{
Header: headsHeader,
}, mockState, n, false, true, false, false).
Return([]*felt.Felt{}, []*felt.Felt{}, []vm.TransactionTrace{}, stepsUsed, nil)
Return([]*felt.Felt{}, []core.GasConsumed{}, []vm.TransactionTrace{}, stepsUsed, nil)

_, httpHeader, err := handler.SimulateTransactionsV0_6(rpc.BlockID{Latest: true}, []rpc.BroadcastedTransaction{}, []rpc.SimulationFlag{rpc.SkipValidateFlag})
require.Nil(t, err)
Expand Down
31 changes: 5 additions & 26 deletions rpc/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ func (h *Handler) traceBlockTransactions(ctx context.Context, block *core.Block,
}

useBlobData := !v0_6Response
overallFees, dataGasConsumed, traces, numSteps, err := h.vm.Execute(block.Transactions, classes, paidFeesOnL1,
_, daGas, traces, numSteps, err := h.vm.Execute(block.Transactions, classes, paidFeesOnL1,
&blockInfo, state, network, false, false, false, useBlobData)

httpHeader.Set(ExecutionStepsHeader, strconv.FormatUint(numSteps, 10))
Expand All @@ -292,32 +292,11 @@ func (h *Handler) traceBlockTransactions(ctx context.Context, block *core.Block,
result := make([]TracedBlockTransaction, 0, len(traces))
for index, trace := range traces {
if !v0_6Response {
feeUnit := feeUnit(block.Transactions[index])

gasPrice := header.GasPrice
if feeUnit == FRI {
if gasPrice = header.GasPriceSTRK; gasPrice == nil {
gasPrice = &felt.Zero
}
}

dataGasPrice := &felt.Zero
if header.L1DataGasPrice != nil {
switch feeUnit {
case FRI:
dataGasPrice = header.L1DataGasPrice.PriceInFri
case WEI:
dataGasPrice = header.L1DataGasPrice.PriceInWei
}
}

dataGasFee := new(felt.Felt).Mul(dataGasConsumed[index], dataGasPrice)
gasConsumed := new(felt.Felt).Sub(overallFees[index], dataGasFee)
gasConsumed = gasConsumed.Div(gasConsumed, gasPrice) // division by zero felt is zero felt

executionResources := trace.TotalExecutionResources()
executionResources.DataAvailability = vm.NewDataAvailability(gasConsumed, dataGasConsumed[index],
header.L1DAMode)
executionResources.DataAvailability = &vm.DataAvailability{
L1Gas: daGas[index].L1Gas,
L1DataGas: daGas[index].L1DataGas,
}
traces[index].ExecutionResources = executionResources
}
result = append(result, TracedBlockTransaction{
Expand Down
4 changes: 2 additions & 2 deletions rpc/trace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func TestTraceTransaction(t *testing.T) {
}`, executionResources)
vmTrace := new(vm.TransactionTrace)
require.NoError(t, json.Unmarshal(json.RawMessage(vmTraceJSON), vmTrace))
consumedGas := []*felt.Felt{new(felt.Felt).SetUint64(1)}
consumedGas := []core.GasConsumed{{L1Gas: 1, L1DataGas: 0}}
overallFee := []*felt.Felt{new(felt.Felt).SetUint64(1)}
stepsUsed := uint64(123)
stepsUsedStr := "123"
Expand Down Expand Up @@ -249,7 +249,7 @@ func TestTraceTransaction(t *testing.T) {
}`, executionResources)
vmTrace := new(vm.TransactionTrace)
require.NoError(t, json.Unmarshal(json.RawMessage(vmTraceJSON), vmTrace))
consumedGas := []*felt.Felt{new(felt.Felt).SetUint64(1)}
consumedGas := []core.GasConsumed{{L1Gas: 1, L1DataGas: 0}}
overallFee := []*felt.Felt{new(felt.Felt).SetUint64(1)}
stepsUsed := uint64(123)
stepsUsedStr := "123"
Expand Down
12 changes: 5 additions & 7 deletions vm/rust/src/juno_state_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,19 +208,17 @@ pub fn class_info_from_json_str(raw_json: &str) -> Result<BlockifierClassInfo, S
let class_info: ClassInfo = serde_json::from_str(raw_json)
.map_err(|err| format!("failed parsing class info: {:?}", err))?;
let class_def = class_info.contract_class.to_string();

let mut sierra_len = class_info.sierra_program_length;
let mut abi_len = class_info.abi_length;
let class: ContractClass =
if let Ok(class) = ContractClassV0::try_from_json_string(class_def.as_str()) {
sierra_len = 0;
abi_len = 0;
class.into()
} else if let Ok(class) = ContractClassV1::try_from_json_string(class_def.as_str()) {
class.into()
} else {
return Err("not a valid contract class".to_string());
};
Ok(BlockifierClassInfo::new(
&class,
class_info.sierra_program_length,
class_info.abi_length,
)
.unwrap())
Ok(BlockifierClassInfo::new(&class, sierra_len, abi_len).unwrap())
}
12 changes: 7 additions & 5 deletions vm/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ extern "C" {
fn JunoAppendTrace(reader_handle: usize, json_trace: *const c_void, len: usize);
fn JunoAppendResponse(reader_handle: usize, ptr: *const c_uchar);
fn JunoAppendActualFee(reader_handle: usize, ptr: *const c_uchar);
fn JunoAppendDataGasConsumed(reader_handle: usize, ptr: *const c_uchar);
fn JunoAppendGasConsumed(reader_handle: usize, ptr: *const c_uchar, ptr: *const c_uchar);
fn JunoAddExecutionSteps(reader_handle: usize, execSteps: c_ulonglong);
}

Expand Down Expand Up @@ -349,8 +349,9 @@ pub extern "C" fn cairoVMExecute(
)
}

let actual_fee = t.transaction_receipt.fee.0.into();
let data_gas_consumed = t.transaction_receipt.da_gas.l1_data_gas.into();
let actual_fee: Felt = t.transaction_receipt.fee.0.into();
let da_gas_l1_gas = t.transaction_receipt.da_gas.l1_gas.into();
let da_gas_l1_data_gas = t.transaction_receipt.da_gas.l1_data_gas.into();
let execution_steps = t
.transaction_receipt
.resources
Expand All @@ -372,9 +373,10 @@ pub extern "C" fn cairoVMExecute(

unsafe {
JunoAppendActualFee(reader_handle, felt_to_byte_array(&actual_fee).as_ptr());
JunoAppendDataGasConsumed(
JunoAppendGasConsumed(
reader_handle,
felt_to_byte_array(&data_gas_consumed).as_ptr(),
felt_to_byte_array(&da_gas_l1_gas).as_ptr(),
felt_to_byte_array(&da_gas_l1_data_gas).as_ptr(),
);
JunoAddExecutionSteps(reader_handle, execution_steps)
}
Expand Down
14 changes: 0 additions & 14 deletions vm/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"errors"
"slices"

"github.com/NethermindEth/juno/core"
"github.com/NethermindEth/juno/core/felt"
)

Expand Down Expand Up @@ -247,16 +246,3 @@ type ExecutionResources struct {
ComputationResources
DataAvailability *DataAvailability `json:"data_availability,omitempty"`
}

func NewDataAvailability(gasConsumed, dataGasConsumed *felt.Felt, mode core.L1DAMode) *DataAvailability {
da := &DataAvailability{}

switch mode {
case core.Calldata:
da.L1Gas = gasConsumed.Uint64()
case core.Blob:
da.L1DataGas = dataGasConsumed.Uint64()
}

return da
}
24 changes: 13 additions & 11 deletions vm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ type VM interface {
maxSteps uint64, useBlobData bool) ([]*felt.Felt, error)
Execute(txns []core.Transaction, declaredClasses []core.Class, paidFeesOnL1 []*felt.Felt, blockInfo *BlockInfo,
state core.StateReader, network *utils.Network, skipChargeFee, skipValidate, errOnRevert, useBlobData bool,
) ([]*felt.Felt, []*felt.Felt, []TransactionTrace, uint64, error)
) ([]*felt.Felt, []core.GasConsumed, []TransactionTrace, uint64, error)
}

type vm struct {
Expand All @@ -92,10 +92,10 @@ type callContext struct {
// response from the executed Cairo function
response []*felt.Felt
// fee amount taken per transaction during VM execution
actualFees []*felt.Felt
traces []json.RawMessage
dataGasConsumed []*felt.Felt
executionSteps uint64
actualFees []*felt.Felt
traces []json.RawMessage
daGas []core.GasConsumed
executionSteps uint64
}

func unwrapContext(readerHandle C.uintptr_t) *callContext {
Expand Down Expand Up @@ -133,10 +133,13 @@ func JunoAppendActualFee(readerHandle C.uintptr_t, ptr unsafe.Pointer) {
context.actualFees = append(context.actualFees, makeFeltFromPtr(ptr))
}

//export JunoAppendDataGasConsumed
func JunoAppendDataGasConsumed(readerHandle C.uintptr_t, ptr unsafe.Pointer) {
//export JunoAppendGasConsumed
func JunoAppendGasConsumed(readerHandle C.uintptr_t, ptr, ptr2 unsafe.Pointer) {
context := unwrapContext(readerHandle)
context.dataGasConsumed = append(context.dataGasConsumed, makeFeltFromPtr(ptr))
context.daGas = append(context.daGas, core.GasConsumed{
L1Gas: makeFeltFromPtr(ptr).Uint64(),
L1DataGas: makeFeltFromPtr(ptr2).Uint64(),
})
}

//export JunoAddExecutionSteps
Expand Down Expand Up @@ -261,7 +264,7 @@ func (v *vm) Call(callInfo *CallInfo, blockInfo *BlockInfo, state core.StateRead
func (v *vm) Execute(txns []core.Transaction, declaredClasses []core.Class, paidFeesOnL1 []*felt.Felt,
blockInfo *BlockInfo, state core.StateReader, network *utils.Network,
skipChargeFee, skipValidate, errOnRevert, useBlobData bool,
) ([]*felt.Felt, []*felt.Felt, []TransactionTrace, uint64, error) {
) ([]*felt.Felt, []core.GasConsumed, []TransactionTrace, uint64, error) {
context := &callContext{
state: state,
log: v.log,
Expand Down Expand Up @@ -339,8 +342,7 @@ func (v *vm) Execute(txns []core.Transaction, declaredClasses []core.Class, paid
return nil, nil, nil, 0, fmt.Errorf("unmarshal trace: %v", err)
}
}

return context.actualFees, context.dataGasConsumed, traces, context.executionSteps, nil
return context.actualFees, context.daGas, traces, context.executionSteps, nil
}

func marshalTxnsAndDeclaredClasses(txns []core.Transaction, declaredClasses []core.Class) (json.RawMessage, json.RawMessage, error) { //nolint:lll
Expand Down

0 comments on commit f83a70e

Please sign in to comment.