Skip to content

Commit

Permalink
refactor: 🚧 add starknet_version and eth_l1_gas_price
Browse files Browse the repository at this point in the history
add starknet_version and eth_l1_gas_price in DB, change type of starknet_version from u8 to Felt252Wrapper

#25 , #26
  • Loading branch information
jbcaron committed Jan 16, 2024
1 parent 70cf543 commit 03b4844
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 8 deletions.
27 changes: 25 additions & 2 deletions crates/client/deoxys/src/convert.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Converts types from [`starknet_providers`] to madara's expected types.
use mp_felt::Felt252Wrapper;
use starknet_api::hash::StarkFelt;
use mp_fee::ResourcePrice;
use starknet_ff::FieldElement;
Expand All @@ -11,7 +12,8 @@ pub fn block(block: &p::Block) -> mp_block::Block {
let block_number = block.block_number.expect("no block number provided");
let sequencer_address = block.sequencer_address.map_or(contract_address(FieldElement::ZERO), contract_address);
let (transaction_commitment, event_commitment) = commitments(&transactions, &events, block_number);
let l1_gas_price = ResourcePrice::default();
let l1_gas_price = resource_price(block.eth_l1_gas_price);
let protocol_version = starknet_version(&block.starknet_version);

let header = mp_block::Header {
parent_block_hash: felt(block.parent_block_hash),
Expand All @@ -23,7 +25,7 @@ pub fn block(block: &p::Block) -> mp_block::Block {
transaction_commitment,
event_count: events.len() as u128,
event_commitment,
protocol_version: 0,
protocol_version,
l1_gas_price,
extra_data: block.block_hash.map(|h| sp_core::U256::from_big_endian(&h.to_bytes_be())),
};
Expand Down Expand Up @@ -136,10 +138,31 @@ fn l1_handler_transaction(tx: &p::L1HandlerTransaction) -> mp_transactions::Hand
}
}

/// Converts a starknet version string to a felt value.
/// If the string contains more than 31 bytes, the function panics.
fn starknet_version(version: &Option<String>) -> Felt252Wrapper {
let ret = match version {
Some(version) => Felt252Wrapper::try_from(
version
.as_bytes())
.expect("Failed to convert version to felt: string is too long"),
None => Felt252Wrapper::ZERO,
};
println!("Starknet version: {}", ret.from_utf8().unwrap());
ret
}

fn fee(felt: starknet_ff::FieldElement) -> u128 {
felt.try_into().expect("Value out of range for u128")
}

fn resource_price(eth_l1_gas_price: starknet_ff::FieldElement) -> ResourcePrice {
ResourcePrice {
price_in_strk: None,
price_in_wei: fee(eth_l1_gas_price).try_into().expect("Value out of range for u64"),
}
}

fn events(receipts: &[p::ConfirmedTransactionReceipt]) -> Vec<starknet_api::transaction::Event> {
receipts.iter().flat_map(|r| &r.events).map(event).collect()
}
Expand Down
4 changes: 2 additions & 2 deletions crates/client/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -876,7 +876,7 @@ where
timestamp: starknet_block.header().block_timestamp,
sequencer_address: Felt252Wrapper::from(starknet_block.header().sequencer_address).into(),
l1_gas_price: starknet_block.header().l1_gas_price.into(),
starknet_version: starknet_version.to_string(),
starknet_version: starknet_version.from_utf8().expect("starknet version should be a valid utf8 string"),
};

Ok(MaybePendingBlockWithTxHashes::Block(block_with_tx_hashes))
Expand Down Expand Up @@ -1124,7 +1124,7 @@ where
sequencer_address: Felt252Wrapper::from(starknet_block.header().sequencer_address).into(),
transactions,
l1_gas_price: starknet_block.header().l1_gas_price.into(),
starknet_version: starknet_version.to_string(),
starknet_version: starknet_version.from_utf8().expect("starknet version should be a valid utf8 string"),
};

Ok(MaybePendingBlockWithTxs::Block(block_with_txs))
Expand Down
4 changes: 2 additions & 2 deletions crates/pallets/starknet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ pub mod pallet {
#[pallet::constant]
type ValidateMaxNSteps: Get<u32>;
#[pallet::constant]
type ProtocolVersion: Get<u8>;
type ProtocolVersion: Get<Felt252Wrapper>;
#[pallet::constant]
type ChainId: Get<Felt252Wrapper>;
#[pallet::constant]
Expand Down Expand Up @@ -1049,7 +1049,7 @@ impl<T: Config> Pallet<T> {
transaction_commitment.into(),
events.len() as u128,
event_commitment.into(),
protocol_version,
protocol_version.into(),
l1_gas_price,
extra_data,
),
Expand Down
4 changes: 2 additions & 2 deletions crates/primitives/block/src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ pub struct Header {
/// A commitment to the events produced in this block
pub event_commitment: StarkHash,
/// The version of the Starknet protocol used when creating this block
pub protocol_version: u8,
pub protocol_version: Felt252Wrapper,
/// l1 gas price for this block
pub l1_gas_price: ResourcePrice,
/// Extraneous data that might be useful for running transactions
Expand All @@ -91,7 +91,7 @@ impl Header {
transaction_commitment: StarkHash,
event_count: u128,
event_commitment: StarkHash,
protocol_version: u8,
protocol_version: Felt252Wrapper,
l1_gas_price: ResourcePrice,
extra_data: Option<U256>,
) -> Self {
Expand Down

0 comments on commit 03b4844

Please sign in to comment.