Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update blockifier #2397

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion adapters/core2p2p/receipt.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ func AdaptExecutionResources(er *core.ExecutionResources) *gen.Receipt_Execution
var l1Gas, l1DataGas, l2Gas, totalL1Gas, totalL1DataGas *felt.Felt
if da := er.DataAvailability; da != nil { // todo(kirill) check that it might be null
l1Gas = new(felt.Felt).SetUint64(da.L1Gas)
l2Gas = new(felt.Felt).SetUint64(da.L2Gas)
l1DataGas = new(felt.Felt).SetUint64(da.L1DataGas)
}
if tgs := er.TotalGasConsumed; tgs != nil {
Expand Down
1 change: 0 additions & 1 deletion adapters/p2p2core/receipt.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ func adaptExecutionResources(er *gen.Receipt_ExecutionResources) *core.Execution
},
DataAvailability: &core.DataAvailability{
L1Gas: feltToUint64(er.L1Gas),
L2Gas: feltToUint64(er.L2Gas),
L1DataGas: feltToUint64(er.L1DataGas),
},
MemoryHoles: uint64(er.MemoryHoles),
Expand Down
1 change: 0 additions & 1 deletion core/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ type ExecutionResources struct {
type DataAvailability struct {
L1Gas uint64
L1DataGas uint64
L2Gas uint64
AnkushinDaniil marked this conversation as resolved.
Show resolved Hide resolved
}

type BuiltinInstanceCounter struct {
Expand Down
28 changes: 15 additions & 13 deletions mocks/mock_vm.go

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

13 changes: 7 additions & 6 deletions node/throttled_vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

func NewThrottledVM(res vm.VM, concurrenyBudget uint, maxQueueLen int32) *ThrottledVM {
return &ThrottledVM{
Throttler: utils.NewThrottler[vm.VM](concurrenyBudget, &res).WithMaxQueueLen(maxQueueLen),
Throttler: utils.NewThrottler(concurrenyBudget, &res).WithMaxQueueLen(maxQueueLen),
}
}

Expand All @@ -30,16 +30,17 @@
})
}

func (tvm *ThrottledVM) Execute(txns []core.Transaction, declaredClasses []core.Class, paidFeesOnL1 []*felt.Felt,
func (tvm *ThrottledVM) Execute(txns []core.Transaction, declaredClasses []core.Class, paidFeesOnL1 []*felt.Felt, //nolint:gocritic
blockInfo *vm.BlockInfo, state core.StateReader, network *utils.Network, skipChargeFee, skipValidate, errOnRevert bool,
) ([]*felt.Felt, []core.GasConsumed, []vm.TransactionTrace, uint64, error) {
) ([]*felt.Felt, []core.DataAvailability, []core.GasConsumed, []vm.TransactionTrace, uint64, error) {
var ret []*felt.Felt
var traces []vm.TransactionTrace
var daGas []core.GasConsumed
var daGas []core.DataAvailability
var gasConsumed []core.GasConsumed
var numSteps uint64
return ret, daGas, traces, numSteps, tvm.Do(func(vm *vm.VM) error {
return ret, daGas, gasConsumed, traces, numSteps, tvm.Do(func(vm *vm.VM) error {
var err error
ret, daGas, traces, numSteps, err = (*vm).Execute(txns, declaredClasses, paidFeesOnL1, blockInfo, state, network,
ret, daGas, gasConsumed, traces, numSteps, err = (*vm).Execute(txns, declaredClasses, paidFeesOnL1, blockInfo, state, network,

Check warning on line 43 in node/throttled_vm.go

View check run for this annotation

Codecov / codecov/patch

node/throttled_vm.go#L43

Added line #L43 was not covered by tests
skipChargeFee, skipValidate, errOnRevert)
return err
})
Expand Down
6 changes: 3 additions & 3 deletions rpc/estimate_fee_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,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).
Return([]*felt.Felt{}, []core.GasConsumed{}, []vm.TransactionTrace{}, uint64(123), nil).Times(2)
Return([]*felt.Felt{}, []core.DataAvailability{}, []core.GasConsumed{}, []vm.TransactionTrace{}, uint64(123), nil).Times(2)

_, httpHeader, err := handler.EstimateFee([]rpc.BroadcastedTransaction{}, []rpc.SimulationFlag{}, rpc.BlockID{Latest: true})
require.Nil(t, err)
Expand All @@ -50,7 +50,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).
Return([]*felt.Felt{}, []core.GasConsumed{}, []vm.TransactionTrace{}, uint64(123), nil).Times(2)
Return([]*felt.Felt{}, []core.DataAvailability{}, []core.GasConsumed{}, []vm.TransactionTrace{}, uint64(123), nil).Times(2)

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

t.Run("transaction execution error", func(t *testing.T) {
mockVM.EXPECT().Execute([]core.Transaction{}, nil, []*felt.Felt{}, &blockInfo, mockState, n, true, true, true).
Return(nil, nil, nil, uint64(0), vm.TransactionExecutionError{
Return(nil, nil, nil, nil, uint64(0), vm.TransactionExecutionError{
Index: 44,
Cause: errors.New("oops"),
}).Times(2)
Expand Down
1 change: 0 additions & 1 deletion rpc/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ func adaptExecutionResources(resources *core.ExecutionResources) *ExecutionResou
if da := resources.DataAvailability; da != nil {
res.DataAvailability = &DataAvailability{
L1Gas: da.L1Gas,
L2Gas: da.L2Gas,
L1DataGas: da.L1DataGas,
}
}
Expand Down
171 changes: 105 additions & 66 deletions rpc/simulation.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,48 @@
return nil, httpHeader, rpcErr
}

txns, classes, paidFeesOnL1, rpcErr := h.prepareTransactions(transactions)
if rpcErr != nil {
return nil, httpHeader, rpcErr
}

blockHashToBeRevealed, err := h.getRevealedBlockHash(header.Number)
if err != nil {
return nil, httpHeader, ErrInternal.CloneWithData(err)
}

Check warning on line 87 in rpc/simulation.go

View check run for this annotation

Codecov / codecov/patch

rpc/simulation.go#L86-L87

Added lines #L86 - L87 were not covered by tests
blockInfo := vm.BlockInfo{
Header: header,
BlockHashToBeRevealed: blockHashToBeRevealed,
}

overallFees, daGas, gasConsumed, traces, numSteps, err := h.vm.Execute(txns, classes, paidFeesOnL1, &blockInfo,
state, h.bcReader.Network(), skipFeeCharge, skipValidate, errOnRevert)

httpHeader.Set(ExecutionStepsHeader, strconv.FormatUint(numSteps, 10))

if err != nil {
return nil, httpHeader, h.handleExecutionError(err)
}

simulatedTransactions, err := h.createSimulatedTransactions(overallFees, traces, gasConsumed, daGas, txns, header)
if err != nil {
return nil, httpHeader, ErrInternal.CloneWithData(err)
}

Check warning on line 105 in rpc/simulation.go

View check run for this annotation

Codecov / codecov/patch

rpc/simulation.go#L104-L105

Added lines #L104 - L105 were not covered by tests

return simulatedTransactions, httpHeader, nil
}

func (h *Handler) prepareTransactions(transactions []BroadcastedTransaction) (
[]core.Transaction, []core.Class, []*felt.Felt, *jsonrpc.Error,
) {
txns := make([]core.Transaction, 0, len(transactions))
var classes []core.Class

paidFeesOnL1 := make([]*felt.Felt, 0)

for idx := range transactions {
txn, declaredClass, paidFeeOnL1, aErr := adaptBroadcastedTransaction(&transactions[idx], h.bcReader.Network())
if aErr != nil {
return nil, httpHeader, jsonrpc.Err(jsonrpc.InvalidParams, aErr.Error())
return nil, nil, nil, jsonrpc.Err(jsonrpc.InvalidParams, aErr.Error())
}

if paidFeeOnL1 != nil {
Expand All @@ -96,82 +130,87 @@
}
}

blockHashToBeRevealed, err := h.getRevealedBlockHash(header.Number)
if err != nil {
return nil, httpHeader, ErrInternal.CloneWithData(err)
return txns, classes, paidFeesOnL1, nil
}

func (h *Handler) handleExecutionError(err error) *jsonrpc.Error {
if errors.Is(err, utils.ErrResourceBusy) {
return ErrInternal.CloneWithData(throttledVMErr)
}
blockInfo := vm.BlockInfo{
Header: header,
BlockHashToBeRevealed: blockHashToBeRevealed,
var txnExecutionError vm.TransactionExecutionError
if errors.As(err, &txnExecutionError) {
return makeTransactionExecutionError(&txnExecutionError)
}
overallFees, daGas, traces, numSteps, err := h.vm.Execute(txns, classes, paidFeesOnL1, &blockInfo,
state, h.bcReader.Network(), skipFeeCharge, skipValidate, errOnRevert)

httpHeader.Set(ExecutionStepsHeader, strconv.FormatUint(numSteps, 10))
return ErrUnexpectedError.CloneWithData(err.Error())

Check warning on line 144 in rpc/simulation.go

View check run for this annotation

Codecov / codecov/patch

rpc/simulation.go#L144

Added line #L144 was not covered by tests
}

if err != nil {
if errors.Is(err, utils.ErrResourceBusy) {
return nil, httpHeader, ErrInternal.CloneWithData(throttledVMErr)
}
var txnExecutionError vm.TransactionExecutionError
if errors.As(err, &txnExecutionError) {
return nil, httpHeader, makeTransactionExecutionError(&txnExecutionError)
}
return nil, httpHeader, ErrUnexpectedError.CloneWithData(err.Error())
func (h *Handler) createSimulatedTransactions(
overallFees []*felt.Felt, traces []vm.TransactionTrace, gasConsumed []core.GasConsumed,
daGas []core.DataAvailability, txns []core.Transaction, header *core.Header,
) ([]SimulatedTransaction, error) {
if len(overallFees) != len(traces) || len(overallFees) != len(gasConsumed) || len(overallFees) != len(daGas) {
return nil, fmt.Errorf("inconsistent lengths: %d overall fees, %d traces, %d gas consumed, %d data availability",
len(overallFees), len(traces), len(gasConsumed), len(daGas))

Check warning on line 153 in rpc/simulation.go

View check run for this annotation

Codecov / codecov/patch

rpc/simulation.go#L152-L153

Added lines #L152 - L153 were not covered by tests
}

result := make([]SimulatedTransaction, 0, len(overallFees))
for i, overallFee := range overallFees {
feeUnit := feeUnit(txns[i])
l1GasPriceWei := header.L1GasPriceETH
l1GasPriceStrk := header.L1GasPriceSTRK
l2GasPriceWei := &felt.Zero
l2GasPriceStrk := &felt.Zero
l1DataGasPriceWei := &felt.Zero
l1DataGasPriceStrk := &felt.Zero

estimate := calculateFeeEstimate(overallFee, daGas[i].L1DataGas, feeUnit, header)
if gasPrice := header.L2GasPrice; gasPrice != nil {
l2GasPriceWei = gasPrice.PriceInWei
l2GasPriceStrk = gasPrice.PriceInFri
}

Check warning on line 166 in rpc/simulation.go

View check run for this annotation

Codecov / codecov/patch

rpc/simulation.go#L164-L166

Added lines #L164 - L166 were not covered by tests
if gasPrice := header.L1DataGasPrice; gasPrice != nil {
l1DataGasPriceWei = gasPrice.PriceInWei
l1DataGasPriceStrk = gasPrice.PriceInFri
}

Check warning on line 170 in rpc/simulation.go

View check run for this annotation

Codecov / codecov/patch

rpc/simulation.go#L168-L170

Added lines #L168 - L170 were not covered by tests

simulatedTransactions := make([]SimulatedTransaction, len(overallFees))
for i, overallFee := range overallFees {
trace := traces[i]
executionResources := trace.TotalExecutionResources()
executionResources.DataAvailability = &vm.DataAvailability{
L1Gas: daGas[i].L1Gas,
L1DataGas: daGas[i].L1DataGas,
traces[i].ExecutionResources = &vm.ExecutionResources{
L1Gas: gasConsumed[i].L1Gas,
L1DataGas: gasConsumed[i].L1DataGas,
L2Gas: gasConsumed[i].L2Gas,
ComputationResources: trace.TotalComputationResources(),
DataAvailability: &vm.DataAvailability{
L1Gas: daGas[i].L1Gas,
L1DataGas: daGas[i].L1DataGas,
},

Check warning on line 183 in rpc/simulation.go

View check run for this annotation

Codecov / codecov/patch

rpc/simulation.go#L175-L183

Added lines #L175 - L183 were not covered by tests
}
traces[i].ExecutionResources = executionResources

result = append(result, SimulatedTransaction{
TransactionTrace: &traces[i],
FeeEstimation: estimate,
})
}

return result, httpHeader, nil
}

func calculateFeeEstimate(overallFee *felt.Felt, l1DataGas uint64, feeUnit FeeUnit, header *core.Header) FeeEstimate {
var l1GasPrice, l2GasPrice, l1DataGasPrice *felt.Felt

switch feeUnit {
case FRI:
l1GasPrice = header.L1GasPriceSTRK
l2GasPrice = header.L2GasPrice.PriceInFri
l1DataGasPrice = header.L1DataGasPrice.PriceInFri
case WEI:
l1GasPrice = header.L1GasPriceETH
l2GasPrice = header.L2GasPrice.PriceInWei
l1DataGasPrice = header.L1DataGasPrice.PriceInWei
}
var l1GasPrice, l2GasPrice, l1DataGasPrice *felt.Felt
feeUnit := feeUnit(txns[i])
switch feeUnit {
case WEI:
l1GasPrice = l1GasPriceWei
l2GasPrice = l2GasPriceWei
l1DataGasPrice = l1DataGasPriceWei
case FRI:
l1GasPrice = l1GasPriceStrk
l2GasPrice = l2GasPriceStrk
l1DataGasPrice = l1DataGasPriceStrk

Check warning on line 196 in rpc/simulation.go

View check run for this annotation

Codecov / codecov/patch

rpc/simulation.go#L186-L196

Added lines #L186 - L196 were not covered by tests
}

l1DataGasConsumed := new(felt.Felt).SetUint64(l1DataGas)
dataGasFee := new(felt.Felt).Mul(l1DataGasConsumed, l1DataGasPrice)
l1GasConsumed := new(felt.Felt).Sub(overallFee, dataGasFee)
l1GasConsumed = l1GasConsumed.Div(l1GasConsumed, l1GasPrice)

return FeeEstimate{
L1GasConsumed: l1GasConsumed,
L2GasConsumed: &felt.Zero, // TODO: Fix when we have l2 gas price
L1GasPrice: l1GasPrice,
L2GasPrice: l2GasPrice,
L1DataGasConsumed: l1DataGasConsumed,
L1DataGasPrice: l1DataGasPrice,
OverallFee: overallFee,
Unit: utils.Ptr(feeUnit),
simulatedTransactions[i] = SimulatedTransaction{
TransactionTrace: &traces[i],
FeeEstimation: FeeEstimate{
L1GasConsumed: new(felt.Felt).SetUint64(gasConsumed[i].L1Gas),
L1GasPrice: l1GasPrice,
L2GasConsumed: new(felt.Felt).SetUint64(gasConsumed[i].L2Gas),
L2GasPrice: l2GasPrice,
L1DataGasConsumed: new(felt.Felt).SetUint64(gasConsumed[i].L1DataGas),
L1DataGasPrice: l1DataGasPrice,
OverallFee: overallFee,
Unit: utils.Ptr(feeUnit),
},
}

Check warning on line 211 in rpc/simulation.go

View check run for this annotation

Codecov / codecov/patch

rpc/simulation.go#L199-L211

Added lines #L199 - L211 were not covered by tests
}
return simulatedTransactions, nil
}

type TransactionExecutionErrorData struct {
Expand Down
40 changes: 0 additions & 40 deletions rpc/simulation_pkg_test.go

This file was deleted.

Loading