Skip to content

Commit

Permalink
RPC update - add l2 gas (#2335)
Browse files Browse the repository at this point in the history
feat: enhance gas price handling with L2 support and field renaming

- Added support for L2 gas prices and renamed gas price fields for clarity.
- Updated core components to distinguish between L1 and L2 gas prices:
  - adapters/core2p2p/block.go: Handle L2 gas prices, renamed fields.
  - adapters/p2p2core/block.go: AdaptBlockHeader updated for L2.
  - adapters/p2p2core/receipt.go: Included L2 gas prices in adaptExecutionResources.
  - adapters/sn2core/sn2core.go: Updated AdaptBlock and adaptGasConsumed.
  - core/block.go: Renamed fields, updated hash functions.

- Updated core data structures:
  - core/block.go: Introduced L1GasPriceETH, L2GasPriceETH, L1GasPriceSTRK, L2GasPriceSTRK.
  - core/receipt.go: Added L2GasPrice field to GasConsumed struct.
  - core/transaction.go: Added L2GasPrice field to DataAvailability struct.

- Updated RPC components:
  - rpc/block.go: AdaptBlockHeader updated with L2 gas prices.
  - rpc/estimate_fee.go: New FeeEstimate struct, EstimateFeeV0_7 function for compatibility.

- Updated tests:
  - adapters/sn2core/sn2core_test.go: Added assertions for L2 gas prices.
  - clients/feeder/feeder_test.go: Reflected gas price field changes.
  - rpc/block_test.go, rpc/estimate_fee_test.go: Added L2 gas-related test cases.
  - rpc/events_test.go: Updated expected JSON responses.
  • Loading branch information
AnkushinDaniil authored Jan 22, 2025
1 parent e3acc28 commit b45cae5
Show file tree
Hide file tree
Showing 43 changed files with 1,056 additions and 718 deletions.
25 changes: 21 additions & 4 deletions adapters/core2p2p/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,21 @@ func adaptSignature(sig []*felt.Felt) *gen.ConsensusSignature {
func AdaptHeader(header *core.Header, commitments *core.BlockCommitments,
stateDiffCommitment *felt.Felt, stateDiffLength uint64,
) *gen.SignedBlockHeader {
var l1DataGasPriceFri, l1DataGasPriceWei, l2GasPriceFri, l2GasPriceWei *felt.Felt
if l1DataGasPrice := header.L1DataGasPrice; l1DataGasPrice != nil {
l1DataGasPriceFri = l1DataGasPrice.PriceInFri
l1DataGasPriceWei = l1DataGasPrice.PriceInWei
} else {
l1DataGasPriceFri = &felt.Zero
l1DataGasPriceWei = &felt.Zero
}
if l2GasPrice := header.L2GasPrice; l2GasPrice != nil {
l2GasPriceFri = l2GasPrice.PriceInFri
l2GasPriceWei = l2GasPrice.PriceInWei
} else {
l2GasPriceFri = &felt.Zero
l2GasPriceWei = &felt.Zero
}
return &gen.SignedBlockHeader{
BlockHash: AdaptHash(header.Hash),
ParentHash: AdaptHash(header.ParentHash),
Expand All @@ -47,16 +62,18 @@ func AdaptHeader(header *core.Header, commitments *core.BlockCommitments,
},
Receipts: AdaptHash(commitments.ReceiptCommitment),
ProtocolVersion: header.ProtocolVersion,
GasPriceFri: AdaptUint128(header.GasPriceSTRK),
L1GasPriceFri: AdaptUint128(header.L1GasPriceSTRK),
Signatures: utils.Map(header.Signatures, adaptSignature),
StateDiffCommitment: &gen.StateDiffCommitment{
StateDiffLength: stateDiffLength,
Root: AdaptHash(stateDiffCommitment),
},
GasPriceWei: AdaptUint128(header.GasPrice),
DataGasPriceFri: AdaptUint128(header.L1DataGasPrice.PriceInFri),
DataGasPriceWei: AdaptUint128(header.L1DataGasPrice.PriceInWei),
L1GasPriceWei: AdaptUint128(header.L1GasPriceETH),
L1DataGasPriceFri: AdaptUint128(l1DataGasPriceFri),
L1DataGasPriceWei: AdaptUint128(l1DataGasPriceWei),
L1DataAvailabilityMode: adaptL1DA(header.L1DAMode),
L2GasPriceFri: AdaptUint128(l2GasPriceFri),
L2GasPriceWei: AdaptUint128(l2GasPriceWei),
}
}

Expand Down
16 changes: 10 additions & 6 deletions adapters/core2p2p/receipt.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,15 @@ func AdaptExecutionResources(er *core.ExecutionResources) *gen.Receipt_Execution
return nil
}

var l1Gas, l1DataGas, totalL1Gas *felt.Felt
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 {
totalL1Gas = new(felt.Felt).SetUint64(tgs.L1Gas)
totalL1DataGas = new(felt.Felt).SetUint64(tgs.L1DataGas)
}

return &gen.Receipt_ExecutionResources{
Expand All @@ -127,10 +129,12 @@ func AdaptExecutionResources(er *core.ExecutionResources) *gen.Receipt_Execution
MulMod: uint32(er.BuiltinInstanceCounter.MulMod),
RangeCheck96: uint32(er.BuiltinInstanceCounter.RangeCheck96),
},
Steps: uint32(er.Steps),
MemoryHoles: uint32(er.MemoryHoles),
L1Gas: AdaptFelt(l1Gas),
L1DataGas: AdaptFelt(l1DataGas),
TotalL1Gas: AdaptFelt(totalL1Gas),
Steps: uint32(er.Steps),
MemoryHoles: uint32(er.MemoryHoles),
L1Gas: AdaptFelt(l1Gas),
L1DataGas: AdaptFelt(l1DataGas),
TotalL1Gas: AdaptFelt(totalL1Gas),
TotalL1DataGas: AdaptFelt(totalL1DataGas),
L2Gas: AdaptFelt(l2Gas),
}
}
5 changes: 3 additions & 2 deletions adapters/core2p2p/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,9 @@ func adaptResourceLimits(bounds core.ResourceBounds) *gen.ResourceLimits {

func adaptResourceBounds(rb map[core.Resource]core.ResourceBounds) *gen.ResourceBounds {
return &gen.ResourceBounds{
L1Gas: adaptResourceLimits(rb[core.ResourceL1Gas]),
L2Gas: adaptResourceLimits(rb[core.ResourceL2Gas]),
L1Gas: adaptResourceLimits(rb[core.ResourceL1Gas]),
L1DataGas: adaptResourceLimits(rb[core.ResourceL1DataGas]),
L2Gas: adaptResourceLimits(rb[core.ResourceL2Gas]),
}
}

Expand Down
13 changes: 8 additions & 5 deletions adapters/p2p2core/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,18 @@ func AdaptBlockHeader(h *gen.SignedBlockHeader, eventsBloom *bloom.BloomFilter)
Timestamp: h.Time,
ProtocolVersion: h.ProtocolVersion,
EventsBloom: eventsBloom,
L1GasPriceETH: AdaptUint128(h.L1GasPriceWei),
Signatures: utils.Map(h.Signatures, adaptSignature),
L1GasPriceSTRK: AdaptUint128(h.L1GasPriceFri),
L1DAMode: adaptDA(h.L1DataAvailabilityMode),
L1DataGasPrice: &core.GasPrice{
PriceInWei: AdaptUint128(h.DataGasPriceWei),
PriceInFri: AdaptUint128(h.DataGasPriceFri),
PriceInWei: AdaptUint128(h.L1DataGasPriceWei),
PriceInFri: AdaptUint128(h.L1DataGasPriceFri),
},
L2GasPrice: &core.GasPrice{
PriceInWei: AdaptUint128(h.L2GasPriceWei),
PriceInFri: AdaptUint128(h.L2GasPriceFri),
},
GasPrice: AdaptUint128(h.GasPriceWei),
GasPriceSTRK: AdaptUint128(h.GasPriceFri),
L2GasPrice: nil, // todo pass correct value once it's in the p2p spec
}
}

Expand Down
4 changes: 3 additions & 1 deletion adapters/p2p2core/receipt.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,16 @@ 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),
Steps: uint64(er.Steps), // todo SPEC 32 -> 64 bytes
TotalGasConsumed: &core.GasConsumed{
L1Gas: feltToUint64(er.TotalL1Gas),
L2Gas: feltToUint64(er.L2Gas),
// total_l1_data_gas = l1_data_gas, because there's only one place that can generate l1_data_gas costs
L1DataGas: feltToUint64(er.L1DataGas),
L1DataGas: feltToUint64(er.TotalL1DataGas),
},
}
}
Expand Down
15 changes: 9 additions & 6 deletions adapters/p2p2core/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,9 @@ func AdaptTransaction(t *gen.Transaction, network *utils.Network) core.Transacti
CompiledClassHash: AdaptHash(tx.CompiledClassHash),
Tip: tx.Tip,
ResourceBounds: map[core.Resource]core.ResourceBounds{
core.ResourceL1Gas: adaptResourceLimits(tx.ResourceBounds.L1Gas),
core.ResourceL2Gas: adaptResourceLimits(tx.ResourceBounds.L2Gas),
core.ResourceL1Gas: adaptResourceLimits(tx.ResourceBounds.L1Gas),
core.ResourceL2Gas: adaptResourceLimits(tx.ResourceBounds.L2Gas),
core.ResourceL1DataGas: adaptResourceLimits(tx.ResourceBounds.L1DataGas),
},
PaymasterData: utils.Map(tx.PaymasterData, AdaptFelt),
AccountDeploymentData: utils.Map(tx.AccountDeploymentData, AdaptFelt),
Expand Down Expand Up @@ -189,8 +190,9 @@ func AdaptTransaction(t *gen.Transaction, network *utils.Network) core.Transacti
Nonce: AdaptFelt(tx.Nonce),
Tip: tx.Tip,
ResourceBounds: map[core.Resource]core.ResourceBounds{
core.ResourceL1Gas: adaptResourceLimits(tx.ResourceBounds.L1Gas),
core.ResourceL2Gas: adaptResourceLimits(tx.ResourceBounds.L2Gas),
core.ResourceL1Gas: adaptResourceLimits(tx.ResourceBounds.L1Gas),
core.ResourceL2Gas: adaptResourceLimits(tx.ResourceBounds.L2Gas),
core.ResourceL1DataGas: adaptResourceLimits(tx.ResourceBounds.L1DataGas),
},
PaymasterData: utils.Map(tx.PaymasterData, AdaptFelt),
NonceDAMode: nDAMode,
Expand Down Expand Up @@ -268,8 +270,9 @@ func AdaptTransaction(t *gen.Transaction, network *utils.Network) core.Transacti
EntryPointSelector: nil,
Tip: tx.Tip,
ResourceBounds: map[core.Resource]core.ResourceBounds{
core.ResourceL1Gas: adaptResourceLimits(tx.ResourceBounds.L1Gas),
core.ResourceL2Gas: adaptResourceLimits(tx.ResourceBounds.L2Gas),
core.ResourceL1Gas: adaptResourceLimits(tx.ResourceBounds.L1Gas),
core.ResourceL2Gas: adaptResourceLimits(tx.ResourceBounds.L2Gas),
core.ResourceL1DataGas: adaptResourceLimits(tx.ResourceBounds.L1DataGas),
},
PaymasterData: utils.Map(tx.PaymasterData, AdaptFelt),
NonceDAMode: nDAMode,
Expand Down
5 changes: 3 additions & 2 deletions adapters/sn2core/sn2core.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ func AdaptBlock(response *starknet.Block, sig *starknet.Signature) (*core.Block,
TransactionCount: uint64(len(response.Transactions)),
EventCount: eventCount,
EventsBloom: core.EventsBloom(receipts),
GasPrice: response.GasPriceETH(),
GasPriceSTRK: response.GasPriceSTRK(),
L1GasPriceETH: response.L1GasPriceETH(),
L1GasPriceSTRK: response.L1GasPriceSTRK(),
L1DAMode: core.L1DAMode(response.L1DAMode),
L1DataGasPrice: (*core.GasPrice)(response.L1DataGasPrice),
L2GasPrice: (*core.GasPrice)(response.L2GasPrice),
Expand Down Expand Up @@ -88,6 +88,7 @@ func adaptGasConsumed(response *starknet.GasConsumed) *core.GasConsumed {
return &core.GasConsumed{
L1Gas: response.L1Gas,
L1DataGas: response.L1DataGas,
L2Gas: response.L2Gas,
}
}

Expand Down
51 changes: 38 additions & 13 deletions adapters/sn2core/sn2core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,23 @@ func TestAdaptBlock(t *testing.T) {
protocolVersion string
network utils.Network
sig *starknet.Signature
gasPriceWEI *felt.Felt
gasPriceSTRK *felt.Felt
l1GasPriceWEI *felt.Felt
l1GasPriceSTRK *felt.Felt
l1DAGasPriceWEI *felt.Felt
l1DAGasPriceFRI *felt.Felt
l2GasPriceWEI *felt.Felt
l2GasPriceFRI *felt.Felt
}{
{
number: 147,
network: utils.Mainnet,
gasPriceWEI: &felt.Zero,
number: 147,
network: utils.Mainnet,
l1GasPriceWEI: &felt.Zero,
},
{
number: 11817,
protocolVersion: "0.10.1",
network: utils.Mainnet,
gasPriceWEI: utils.HexToFelt(t, "0x27ad16775"),
l1GasPriceWEI: utils.HexToFelt(t, "0x27ad16775"),
},
{
number: 304740,
Expand All @@ -44,7 +46,7 @@ func TestAdaptBlock(t *testing.T) {
sig: &starknet.Signature{
Signature: []*felt.Felt{utils.HexToFelt(t, "0x44"), utils.HexToFelt(t, "0x37")},
},
gasPriceWEI: utils.HexToFelt(t, "0x3bb2acbc"),
l1GasPriceWEI: utils.HexToFelt(t, "0x3bb2acbc"),
},
{
number: 319132,
Expand All @@ -56,8 +58,8 @@ func TestAdaptBlock(t *testing.T) {
utils.HexToFelt(t, "0x6bef4745194c9447fdc8dd3aec4fc738ab0a560b0d2c7bf62fbf58aef3abfc5"),
},
},
gasPriceWEI: utils.HexToFelt(t, "0x3b9aca08"),
gasPriceSTRK: utils.HexToFelt(t, "0x2540be400"),
l1GasPriceWEI: utils.HexToFelt(t, "0x3b9aca08"),
l1GasPriceSTRK: utils.HexToFelt(t, "0x2540be400"),
},
{
number: 330363,
Expand All @@ -69,11 +71,28 @@ func TestAdaptBlock(t *testing.T) {
utils.HexToFelt(t, "0x343e605de3957e664746ba8ef82f2b0f9d53cda3d75dcb078290b8edd010165"),
},
},
gasPriceWEI: utils.HexToFelt(t, "0x3b9aca0a"),
gasPriceSTRK: utils.HexToFelt(t, "0x2b6fdb70"),
l1GasPriceWEI: utils.HexToFelt(t, "0x3b9aca0a"),
l1GasPriceSTRK: utils.HexToFelt(t, "0x2b6fdb70"),
l1DAGasPriceWEI: utils.HexToFelt(t, "0x5265a14ef"),
l1DAGasPriceFRI: utils.HexToFelt(t, "0x3c0c00c87"),
},
{
number: 64164,
network: utils.SepoliaIntegration,
protocolVersion: "0.13.4",
sig: &starknet.Signature{
Signature: []*felt.Felt{
utils.HexToFelt(t, "0x2a1658d74c85266cec309b15fb623ae7b18854a8e08e84834067fd68f07d15a"),
utils.HexToFelt(t, "0x5304bd3e7151d87fabe5977a1d19c2cc9025cce27a2ce0b26a46633386add94"),
},
},
l1GasPriceWEI: utils.HexToFelt(t, "0xcd62576b"),
l1GasPriceSTRK: utils.HexToFelt(t, "0x17842b1d0815"),
l1DAGasPriceWEI: utils.HexToFelt(t, "0x31ea"),
l1DAGasPriceFRI: utils.HexToFelt(t, "0x5b70ba9"),
l2GasPriceWEI: utils.HexToFelt(t, "0x15081"),
l2GasPriceFRI: utils.HexToFelt(t, "0x268771a6"),
},
}

ctx := context.Background()
Expand Down Expand Up @@ -120,14 +139,20 @@ func TestAdaptBlock(t *testing.T) {
assert.Empty(t, block.Signatures)
}

assert.Equal(t, test.gasPriceSTRK, block.GasPriceSTRK)
assert.Equal(t, test.gasPriceWEI, block.GasPrice)
assert.Equal(t, test.l1GasPriceSTRK, block.L1GasPriceSTRK)
assert.Equal(t, test.l1GasPriceWEI, block.L1GasPriceETH)
if test.l1DAGasPriceFRI != nil {
assert.Equal(t, test.l1DAGasPriceFRI, block.L1DataGasPrice.PriceInFri)
}
if test.l1DAGasPriceFRI != nil {
assert.Equal(t, test.l1DAGasPriceWEI, block.L1DataGasPrice.PriceInWei)
}
if test.l2GasPriceFRI != nil {
assert.Equal(t, test.l2GasPriceFRI, block.L2GasPrice.PriceInFri)
}
if test.l2GasPriceWEI != nil {
assert.Equal(t, test.l2GasPriceWEI, block.L2GasPrice.PriceInWei)
}
})
}
}
Expand Down
12 changes: 6 additions & 6 deletions clients/feeder/feeder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ func TestBlockWithoutSequencerAddressUnmarshal(t *testing.T) {
assert.Equal(t, uint64(11817), block.Number)
assert.Equal(t, "0x3df24be7b5fed6b41de08d38686b6142944119ca2a345c38793590d6804bba4", block.StateRoot.String())
assert.Equal(t, "ACCEPTED_ON_L2", block.Status)
assert.Equal(t, "0x27ad16775", block.GasPriceETH().String())
assert.Equal(t, "0x27ad16775", block.L1GasPriceETH().String())
assert.Equal(t, 52, len(block.Transactions))
assert.Equal(t, 52, len(block.Receipts))
assert.Equal(t, uint64(1669465009), block.Timestamp)
Expand All @@ -291,7 +291,7 @@ func TestBlockWithSequencerAddressUnmarshal(t *testing.T) {
assert.Equal(t, uint64(19199), block.Number)
assert.Equal(t, "0x541b796ea02703d02ff31459815f65f410ceefe80a4e3499f7ef9ccc36d26ee", block.StateRoot.String())
assert.Equal(t, "ACCEPTED_ON_L2", block.Status)
assert.Equal(t, "0x31c4e2d75", block.GasPriceETH().String())
assert.Equal(t, "0x31c4e2d75", block.L1GasPriceETH().String())
assert.Equal(t, 324, len(block.Transactions))
assert.Equal(t, 324, len(block.Receipts))
assert.Equal(t, uint64(1674728186), block.Timestamp)
Expand All @@ -309,8 +309,8 @@ func TestBlockHeaderV013Unmarshal(t *testing.T) {
require.Equal(t, uint64(319132), block.Number)
require.Equal(t, utils.HexToFelt(t, "0x2a6b9a8b60e1de80dc50e6b704b415a38e8fd03d82244cec92cbff0821a8975"), block.StateRoot)
require.Equal(t, "ACCEPTED_ON_L2", block.Status)
require.Equal(t, utils.HexToFelt(t, "0x3b9aca08"), block.GasPriceETH())
require.Equal(t, utils.HexToFelt(t, "0x2540be400"), block.GasPriceSTRK())
require.Equal(t, utils.HexToFelt(t, "0x3b9aca08"), block.L1GasPriceETH())
require.Equal(t, utils.HexToFelt(t, "0x2540be400"), block.L1GasPriceSTRK())
require.Equal(t, uint64(1700075354), block.Timestamp)
require.Equal(t, utils.HexToFelt(t, "0x1176a1bd84444c89232ec27754698e5d2e7e1a7f1539f12027f28b23ec9f3d8"), block.SequencerAddress)
require.Equal(t, "0.13.0", block.Version)
Expand All @@ -324,8 +324,8 @@ func TestBlockHeaderV0131Unmarshal(t *testing.T) {
require.Equal(t, utils.HexToFelt(t, "0x8ab8117e952f95efd96de0bc66dc6f13fe68dfda14b95fe1972759dee283a8"), block.Hash)
require.Equal(t, utils.HexToFelt(t, "0x13367121d0b7e34a9b10c8a5a1c269811cd9afc3ce680c88888f1a22d2f017a"), block.TransactionCommitment)
require.Equal(t, utils.HexToFelt(t, "0x1090dd2ab2aa22bd5fc5a59d3b1394d54461bb2a80156c4b2c2622d2c474ca2"), block.EventCommitment)
require.Equal(t, utils.HexToFelt(t, "0x3b9aca0a"), block.GasPriceETH())
require.Equal(t, utils.HexToFelt(t, "0x2b6fdb70"), block.GasPriceSTRK())
require.Equal(t, utils.HexToFelt(t, "0x3b9aca0a"), block.L1GasPriceETH())
require.Equal(t, utils.HexToFelt(t, "0x2b6fdb70"), block.L1GasPriceSTRK())
require.Equal(t, utils.HexToFelt(t, "0x5265a14ef"), block.L1DataGasPrice.PriceInWei)
require.Equal(t, utils.HexToFelt(t, "0x3c0c00c87"), block.L1DataGasPrice.PriceInFri)
require.Equal(t, starknet.Blob, block.L1DAMode)
Expand Down
Loading

0 comments on commit b45cae5

Please sign in to comment.