Skip to content

Commit

Permalink
fix(rpc): failed fee estimation due to absence of l1_data_gas
Browse files Browse the repository at this point in the history
add dummy test for code coverage
  • Loading branch information
weiihann committed Mar 1, 2025
1 parent c905aaf commit cf05857
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
15 changes: 15 additions & 0 deletions adapters/sn2core/sn2core.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,21 @@ func adaptResourceBounds(rb *map[starknet.Resource]starknet.ResourceBounds) map[
MaxPricePerUnit: bounds.MaxPricePerUnit,
}
}

// Ensures that the L1DataGas resource is always present if L2Gas is non-zero.
// In RPC v8, L1Gas, L2Gas and L1DataGas are part of the spec and required.
// In RPC v6 and v7, only L1Gas and L2Gas are part of the spec and required.
// Blockifier will throw an error if L1DataGas is absent and L2Gas is non-zero.
// To avoid that, if L2Gas is non-zero, we set the L1DataGas resource to zero.
if l2Gas, ok := coreBounds[core.ResourceL2Gas]; ok && !l2Gas.IsZero() {
if _, ok := coreBounds[core.ResourceL1DataGas]; !ok {
coreBounds[core.ResourceL1DataGas] = core.ResourceBounds{
MaxAmount: 0,
MaxPricePerUnit: &felt.Zero,
}
}
}

return coreBounds
}

Expand Down
4 changes: 4 additions & 0 deletions core/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ func (rb ResourceBounds) Bytes(resource Resource) []byte {
)
}

func (rb ResourceBounds) IsZero() bool {
return rb.MaxAmount == 0 && (rb.MaxPricePerUnit == nil || rb.MaxPricePerUnit.IsZero())
}

type Event struct {
Data []*felt.Felt
From *felt.Felt
Expand Down
28 changes: 28 additions & 0 deletions rpc/v7/simulation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,34 @@ func TestSimulateTransactions(t *testing.T) {
assert.Equal(t, httpHeader.Get(rpcv7.ExecutionStepsHeader), "123")
})

t.Run("ok with l2gas present", func(t *testing.T) {
stepsUsed := uint64(123)
mockVM.EXPECT().Execute([]core.Transaction{}, nil, []*felt.Felt{}, &vm.BlockInfo{
Header: headsHeader,
}, mockState, n, false, true, false, false).
Return(vm.ExecutionResults{
OverallFees: []*felt.Felt{},
DataAvailability: []core.DataAvailability{},
Traces: []vm.TransactionTrace{},
NumSteps: stepsUsed,
}, nil)

txn := rpcv7.BroadcastedTransaction{
Transaction: rpcv7.Transaction{
ResourceBounds: &map[rpcv7.Resource]rpcv7.ResourceBounds{
rpcv7.Resource(core.ResourceL2Gas): {
MaxAmount: new(felt.Felt).SetUint64(100),
MaxPricePerUnit: new(felt.Felt).SetUint64(100),
},
},
},
}

_, httpHeader, err := handler.SimulateTransactions(rpcv7.BlockID{Latest: true}, []rpcv7.BroadcastedTransaction{txn}, []rpcv7.SimulationFlag{rpcv7.SkipValidateFlag})
require.Nil(t, err)
assert.Equal(t, httpHeader.Get(rpcv7.ExecutionStepsHeader), "123")
})

t.Run("transaction execution error", func(t *testing.T) {
t.Run("v0_7, v0_8", func(t *testing.T) { //nolint:dupl
mockVM.EXPECT().Execute([]core.Transaction{}, nil, []*felt.Felt{}, &vm.BlockInfo{
Expand Down

0 comments on commit cf05857

Please sign in to comment.