Skip to content

Commit

Permalink
feat: add l2_gas_price to BlockHeader
Browse files Browse the repository at this point in the history
  • Loading branch information
t00ts committed Oct 18, 2024
1 parent ce452c1 commit c92dd6c
Show file tree
Hide file tree
Showing 17 changed files with 43 additions and 2 deletions.
6 changes: 6 additions & 0 deletions crates/common/src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub struct BlockHeader {
pub strk_l1_gas_price: GasPrice,
pub eth_l1_data_gas_price: GasPrice,
pub strk_l1_data_gas_price: GasPrice,
pub l2_gas_price: GasPrice,
pub sequencer_address: SequencerAddress,
pub starknet_version: StarknetVersion,
pub class_commitment: ClassCommitment,
Expand Down Expand Up @@ -109,6 +110,11 @@ impl BlockHeaderBuilder {
self
}

pub fn l2_gas_price(mut self, l2_gas_price: GasPrice) -> Self {
self.0.l2_gas_price = l2_gas_price;
self
}

pub fn eth_l1_data_gas_price(mut self, eth_l1_data_gas_price: GasPrice) -> Self {
self.0.eth_l1_data_gas_price = eth_l1_data_gas_price;
self
Expand Down
1 change: 1 addition & 0 deletions crates/p2p/src/client/conv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ impl ToDto<p2p_proto::header::SignedBlockHeader> for SignedBlockHeader {
gas_price_wei: self.header.eth_l1_gas_price.0,
data_gas_price_fri: self.header.strk_l1_data_gas_price.0,
data_gas_price_wei: self.header.eth_l1_data_gas_price.0,
l2_gas_price: self.header.l2_gas_price.0,
l1_data_availability_mode: self.header.l1_da_mode.to_dto(),
signatures: vec![p2p_proto::common::ConsensusSignature {
r: self.signature.r.0,
Expand Down
1 change: 1 addition & 0 deletions crates/p2p/src/client/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ impl TryFromDto<p2p_proto::header::SignedBlockHeader> for SignedBlockHeader {
strk_l1_gas_price: GasPrice(dto.gas_price_fri),
eth_l1_data_gas_price: GasPrice(dto.data_gas_price_wei),
strk_l1_data_gas_price: GasPrice(dto.data_gas_price_fri),
l2_gas_price: GasPrice(dto.l2_gas_price),
sequencer_address: SequencerAddress(dto.sequencer_address.0),
starknet_version: dto.protocol_version.parse()?,
event_commitment: EventCommitment(dto.events.root.0),
Expand Down
1 change: 1 addition & 0 deletions crates/p2p_proto/proto/header.proto
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ message SignedBlockHeader {
starknet.common.Uint128 gas_price_wei = 13;
starknet.common.Uint128 data_gas_price_fri = 14;
starknet.common.Uint128 data_gas_price_wei = 15;
starknet.common.Uint128 l2_gas_price = 18;
starknet.common.L1DataAvailabilityMode l1_data_availability_mode = 16;
// for now, we assume a small consensus, so this fits in 1M. Else, these will be repeated and extracted from this message.
repeated starknet.common.ConsensusSignature signatures = 17;
Expand Down
2 changes: 2 additions & 0 deletions crates/p2p_proto/src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub struct SignedBlockHeader {
pub gas_price_wei: u128,
pub data_gas_price_fri: u128,
pub data_gas_price_wei: u128,
pub l2_gas_price: u128,
pub l1_data_availability_mode: L1DataAvailabilityMode,
pub signatures: Vec<ConsensusSignature>,
}
Expand Down Expand Up @@ -78,6 +79,7 @@ impl<T> Dummy<T> for SignedBlockHeader {
gas_price_wei: Faker.fake_with_rng(rng),
data_gas_price_fri: Faker.fake_with_rng(rng),
data_gas_price_wei: Faker.fake_with_rng(rng),
l2_gas_price: Faker.fake_with_rng(rng),
l1_data_availability_mode: Faker.fake_with_rng(rng),
signatures: Faker.fake_with_rng(rng),
}
Expand Down
1 change: 1 addition & 0 deletions crates/pathfinder/src/state/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -916,6 +916,7 @@ async fn l2_update(
eth_l1_data_gas_price: block.l1_data_gas_price.price_in_wei,
// Default value for Starknet <0.13.1 is zero
strk_l1_data_gas_price: block.l1_data_gas_price.price_in_fri,
l2_gas_price: GasPrice(0), // TODO: Fix when we get l2_gas_price in the gateway
sequencer_address: block
.sequencer_address
.unwrap_or(SequencerAddress(Felt::ZERO)),
Expand Down
1 change: 1 addition & 0 deletions crates/pathfinder/src/sync/checkpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,7 @@ mod tests {
eth_l1_data_gas_price: GasPrice(1),
strk_l1_gas_price: GasPrice(0),
strk_l1_data_gas_price: GasPrice(1),
l2_gas_price: GasPrice(0),
l1_da_mode: L1DataAvailabilityMode::Calldata,
class_commitment: ClassCommitment::ZERO,
storage_commitment: StorageCommitment::ZERO,
Expand Down
2 changes: 2 additions & 0 deletions crates/pathfinder/src/sync/checkpoint/fixture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ pub fn blocks() -> [Block; 2] {
strk_l1_gas_price: GasPrice(0),
eth_l1_data_gas_price: GasPrice(1),
strk_l1_data_gas_price: GasPrice(1),
l2_gas_price: GasPrice(3),
sequencer_address: Default::default(),
starknet_version: StarknetVersion::new(0, 0, 0, 0),
class_commitment: class_commitment!("0x0"),
Expand Down Expand Up @@ -1202,6 +1203,7 @@ pub fn blocks() -> [Block; 2] {
receipt_commitment: receipt_commitment!("0x00FB6833B56FCA428975B0DF7875F35B7EADBD26B517DAF1B9702E1D85665065"),
state_diff_commitment: state_diff_commitment!("0x05D83BBEEDF35B7D310A43B11F3623DD5D705FF09A7FBC8B634222E083433CAE"),
state_diff_length: 12,
l2_gas_price: todo!(),
},
signature: BlockCommitmentSignature {
r: block_commitment_signature_elem!("0x05C328D673C07E530A45D6F12E569DF0D059D97BF920D978E44DAA54FB3DB655"),
Expand Down
1 change: 1 addition & 0 deletions crates/pathfinder/src/sync/track.rs
Original file line number Diff line number Diff line change
Expand Up @@ -796,6 +796,7 @@ impl ProcessStage for StoreBlock {
receipt_commitment: header.receipt_commitment,
state_diff_commitment: header.state_diff_commitment,
state_diff_length: header.state_diff_length,
l2_gas_price: header.l2_gas_price,
};

db.insert_block_header(&header)
Expand Down
Binary file modified crates/rpc/fixtures/mainnet.sqlite
Binary file not shown.
2 changes: 2 additions & 0 deletions crates/rpc/src/jsonrpc/websocket/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ impl serde::Serialize for BlockHeader {
strk_l1_gas_price,
eth_l1_data_gas_price,
strk_l1_data_gas_price,
l2_gas_price,
sequencer_address,
starknet_version,
class_commitment,
Expand All @@ -261,6 +262,7 @@ impl serde::Serialize for BlockHeader {
map.serialize_entry("strk_l1_gas_price", &strk_l1_gas_price)?;
map.serialize_entry("eth_l1_data_gas_price", &eth_l1_data_gas_price)?;
map.serialize_entry("strk_l1_data_gas_price", &strk_l1_data_gas_price)?;
map.serialize_entry("l2_gas_price", &l2_gas_price)?;
map.serialize_entry("sequencer_address", &sequencer_address)?;
map.serialize_entry("starknet_version", &starknet_version.to_string())?;
map.serialize_entry("class_commitment", &class_commitment)?;
Expand Down
1 change: 1 addition & 0 deletions crates/rpc/src/pending.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ impl PendingData {
strk_l1_gas_price: self.block.l1_gas_price.price_in_fri,
eth_l1_data_gas_price: self.block.l1_data_gas_price.price_in_wei,
strk_l1_data_gas_price: self.block.l1_data_gas_price.price_in_fri,
l2_gas_price: 0.into(), // TODO: Fix when we get l2_gas_price in the gateway
sequencer_address: self.block.sequencer_address,
starknet_version: self.block.starknet_version,
// Pending block does not know what these are yet.
Expand Down
1 change: 1 addition & 0 deletions crates/rpc/src/v06/method/trace_block_transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,7 @@ pub(crate) mod tests {
strk_l1_gas_price: block.l1_gas_price.price_in_fri,
eth_l1_data_gas_price: block.l1_data_gas_price.price_in_wei,
strk_l1_data_gas_price: block.l1_data_gas_price.price_in_fri,
l2_gas_price: GasPrice(0), // TODO: Fix when we get l2_gas_price in the gateway
sequencer_address: block
.sequencer_address
.unwrap_or(SequencerAddress(Felt::ZERO)),
Expand Down
1 change: 1 addition & 0 deletions crates/rpc/src/v06/method/trace_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ pub mod tests {
strk_l1_gas_price: block.l1_gas_price.price_in_fri,
eth_l1_data_gas_price: block.l1_data_gas_price.price_in_wei,
strk_l1_data_gas_price: block.l1_data_gas_price.price_in_fri,
l2_gas_price: 0.into(), // TODO: Fix when we get l2_gas_price in the gateway
sequencer_address: block
.sequencer_address
.unwrap_or(SequencerAddress(Felt::ZERO)),
Expand Down
12 changes: 10 additions & 2 deletions crates/storage/src/connection/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ impl Transaction<'_> {
// Insert the header
self.inner().execute(
r"INSERT INTO block_headers
( number, hash, parent_hash, storage_commitment, timestamp, eth_l1_gas_price, strk_l1_gas_price, eth_l1_data_gas_price, strk_l1_data_gas_price, sequencer_address, version, transaction_commitment, event_commitment, state_commitment, class_commitment, transaction_count, event_count, l1_da_mode, receipt_commitment, state_diff_commitment, state_diff_length)
VALUES (:number, :hash, :parent_hash, :storage_commitment, :timestamp, :eth_l1_gas_price, :strk_l1_gas_price, :eth_l1_data_gas_price, :strk_l1_data_gas_price, :sequencer_address, :version, :transaction_commitment, :event_commitment, :state_commitment, :class_commitment, :transaction_count, :event_count, :l1_da_mode, :receipt_commitment, :state_diff_commitment, :state_diff_length)",
( number, hash, parent_hash, storage_commitment, timestamp, eth_l1_gas_price, strk_l1_gas_price, eth_l1_data_gas_price, strk_l1_data_gas_price, l2_gas_price, sequencer_address, version, transaction_commitment, event_commitment, state_commitment, class_commitment, transaction_count, event_count, l1_da_mode, receipt_commitment, state_diff_commitment, state_diff_length)
VALUES (:number, :hash, :parent_hash, :storage_commitment, :timestamp, :eth_l1_gas_price, :strk_l1_gas_price, :eth_l1_data_gas_price, :strk_l1_data_gas_price, :l2_gas_price, :sequencer_address, :version, :transaction_commitment, :event_commitment, :state_commitment, :class_commitment, :transaction_count, :event_count, :l1_da_mode, :receipt_commitment, :state_diff_commitment, :state_diff_length)",
named_params! {
":number": &header.number,
":hash": &header.hash,
Expand All @@ -35,6 +35,7 @@ impl Transaction<'_> {
":strk_l1_gas_price": &header.strk_l1_gas_price.to_be_bytes().as_slice(),
":eth_l1_data_gas_price": &header.eth_l1_data_gas_price.to_be_bytes().as_slice(),
":strk_l1_data_gas_price": &header.strk_l1_data_gas_price.to_be_bytes().as_slice(),
":l2_gas_price": &header.l2_gas_price.to_be_bytes().as_slice(),
":sequencer_address": &header.sequencer_address,
":version": &header.starknet_version.as_u32(),
":transaction_commitment": &header.transaction_commitment,
Expand Down Expand Up @@ -613,6 +614,9 @@ fn parse_row_as_header(row: &rusqlite::Row<'_>) -> rusqlite::Result<BlockHeader>
let strk_l1_data_gas_price = row
.get_optional_gas_price("strk_l1_data_gas_price")?
.unwrap_or(GasPrice::ZERO);
let l2_gas_price = row
.get_optional_gas_price("l2_gas_price")?
.unwrap_or(GasPrice::ZERO);
let sequencer_address = row.get_sequencer_address("sequencer_address")?;
let transaction_commitment = row.get_transaction_commitment("transaction_commitment")?;
let event_commitment = row.get_event_commitment("event_commitment")?;
Expand All @@ -637,6 +641,7 @@ fn parse_row_as_header(row: &rusqlite::Row<'_>) -> rusqlite::Result<BlockHeader>
strk_l1_gas_price,
eth_l1_data_gas_price,
strk_l1_data_gas_price,
l2_gas_price,
sequencer_address,
class_commitment,
event_commitment,
Expand Down Expand Up @@ -688,6 +693,7 @@ mod tests {
strk_l1_gas_price: GasPrice(33),
eth_l1_data_gas_price: GasPrice(34),
strk_l1_data_gas_price: GasPrice(35),
l2_gas_price: GasPrice(36),
sequencer_address: sequencer_address_bytes!(b"sequencer address genesis"),
starknet_version: StarknetVersion::default(),
class_commitment,
Expand All @@ -707,6 +713,7 @@ mod tests {
.timestamp(BlockTimestamp::new_or_panic(12))
.eth_l1_gas_price(GasPrice(34))
.strk_l1_gas_price(GasPrice(35))
.l2_gas_price(GasPrice(36))
.sequencer_address(sequencer_address_bytes!(b"sequencer address 1"))
.event_commitment(event_commitment_bytes!(b"event commitment 1"))
.class_commitment(class_commitment_bytes!(b"class commitment 1"))
Expand All @@ -721,6 +728,7 @@ mod tests {
.child_builder()
.eth_l1_gas_price(GasPrice(38))
.strk_l1_gas_price(GasPrice(39))
.l2_gas_price(GasPrice(40))
.timestamp(BlockTimestamp::new_or_panic(15))
.sequencer_address(sequencer_address_bytes!(b"sequencer address 2"))
.event_commitment(event_commitment_bytes!(b"event commitment 2"))
Expand Down
2 changes: 2 additions & 0 deletions crates/storage/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ mod revision_0061;
mod revision_0062;
mod revision_0063;
mod revision_0064;
mod revision_0065;

pub(crate) use base::base_schema;

Expand Down Expand Up @@ -56,6 +57,7 @@ pub fn migrations() -> &'static [MigrationFn] {
revision_0062::migrate,
revision_0063::migrate,
revision_0064::migrate,
revision_0065::migrate,
]
}

Expand Down
10 changes: 10 additions & 0 deletions crates/storage/src/schema/revision_0065.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use anyhow::Context;

pub(crate) fn migrate(tx: &rusqlite::Transaction<'_>) -> anyhow::Result<()> {
tracing::info!("Adding l2_gas_price column to block_headers");

tx.execute_batch("ALTER TABLE block_headers ADD COLUMN l2_gas_price BLOB DEFAULT NULL;")
.context("Adding l2_gas_price column to block_headers")?;

Ok(())
}

0 comments on commit c92dd6c

Please sign in to comment.