-
Notifications
You must be signed in to change notification settings - Fork 28
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
allow zero as gas prices #416
Merged
Merged
Changes from 8 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
75dd346
allow zero as gas prices
ftheirs 5577de3
improve felt_to_u128 conversion
ftheirs b89c2fd
improve gas price conversion
ftheirs 1a9b0c4
export FeltConversionError
ftheirs 15c5c4c
handle errors properly
ftheirs 3b79bb7
clippy
ftheirs 37f5e75
fix unit test
ftheirs 17e2839
Merge branch 'main' into ft/allow_zero_gas_prices
HermanObst 6d4329f
avoid using unwrap
ftheirs 4ef2cba
use expect instead of unwrap
ftheirs 729ffe1
add test with non-zero gas price values
ftheirs 0a89869
remove unnecesary Ok - clippy
ftheirs 98cc00b
Merge branch 'main' into ft/allow_zero_gas_prices
HermanObst f423331
Merge branch 'main' into ft/allow_zero_gas_prices
HermanObst File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,32 @@ | ||
use std::num::NonZeroU128; | ||
|
||
use blockifier::blockifier::block::{BlockInfo, GasPrices}; | ||
use blockifier::bouncer::BouncerConfig; | ||
use blockifier::context::{BlockContext, ChainInfo, FeeTokenAddresses}; | ||
use blockifier::versioned_constants::VersionedConstants; | ||
use starknet::core::types::{BlockWithTxs, L1DataAvailabilityMode}; | ||
use starknet::core::types::{BlockWithTxs, Felt, L1DataAvailabilityMode}; | ||
use starknet_api::block::{BlockNumber, BlockTimestamp}; | ||
use starknet_api::core::{ChainId, ContractAddress, PatriciaKey}; | ||
use starknet_api::{contract_address, felt, patricia_key}; | ||
|
||
use crate::utils::felt_to_u128; | ||
use crate::utils::{felt_to_u128, FeltConversionError}; | ||
|
||
fn felt_to_gas_price(price: &Felt) -> Result<NonZeroU128, FeltConversionError> { | ||
// Inspiration taken from Papyrus: | ||
// https://github.com/starkware-libs/sequencer/blob/7218aa1f7ca3fe21c0a2bede2570820939ffe069/crates/papyrus_execution/src/lib.rs#L363-L371 | ||
if *price == Felt::ZERO { | ||
return Ok(NonZeroU128::MIN); | ||
} | ||
|
||
// If felt_to_u128 conversion is ok, it won't fail cause we're catching the zero above | ||
Ok(NonZeroU128::new(felt_to_u128(price)?).unwrap()) | ||
} | ||
|
||
pub fn build_block_context( | ||
chain_id: ChainId, | ||
block: &BlockWithTxs, | ||
starknet_version: blockifier::versioned_constants::StarknetVersion, | ||
) -> BlockContext { | ||
) -> Result<BlockContext, FeltConversionError> { | ||
let sequencer_address_hex = block.sequencer_address.to_hex_string(); | ||
let sequencer_address = contract_address!(sequencer_address_hex.as_str()); | ||
let use_kzg_da = match block.l1_da_mode { | ||
|
@@ -26,10 +39,10 @@ pub fn build_block_context( | |
block_timestamp: BlockTimestamp(block.timestamp), | ||
sequencer_address, | ||
gas_prices: GasPrices { | ||
eth_l1_gas_price: felt_to_u128(&block.l1_gas_price.price_in_wei).try_into().unwrap(), | ||
strk_l1_gas_price: felt_to_u128(&block.l1_gas_price.price_in_fri).try_into().unwrap(), | ||
eth_l1_data_gas_price: felt_to_u128(&block.l1_data_gas_price.price_in_wei).try_into().unwrap(), | ||
strk_l1_data_gas_price: felt_to_u128(&block.l1_data_gas_price.price_in_fri).try_into().unwrap(), | ||
eth_l1_gas_price: felt_to_gas_price(&block.l1_gas_price.price_in_wei)?, | ||
strk_l1_gas_price: felt_to_gas_price(&block.l1_gas_price.price_in_fri)?, | ||
eth_l1_data_gas_price: felt_to_gas_price(&block.l1_data_gas_price.price_in_wei)?, | ||
strk_l1_data_gas_price: felt_to_gas_price(&block.l1_data_gas_price.price_in_fri)?, | ||
}, | ||
use_kzg_da, | ||
}; | ||
|
@@ -50,5 +63,46 @@ pub fn build_block_context( | |
let versioned_constants = VersionedConstants::get(starknet_version); | ||
let bouncer_config = BouncerConfig::max(); | ||
|
||
BlockContext::new(block_info, chain_info, versioned_constants.clone(), bouncer_config) | ||
Ok(BlockContext::new(block_info, chain_info, versioned_constants.clone(), bouncer_config)) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
|
||
use starknet::core::types::{Felt, ResourcePrice}; | ||
use starknet_api::core::ChainId; | ||
|
||
use super::*; | ||
|
||
#[test] | ||
fn test_build_block_context_with_zero_gas_prices() { | ||
let chain_id = ChainId::Mainnet; | ||
// We don't really care about most of the fields. | ||
// What's important here is to set to zero different gas prices | ||
let block = BlockWithTxs { | ||
status: starknet::core::types::BlockStatus::AcceptedOnL1, | ||
block_hash: Felt::ZERO, | ||
parent_hash: Felt::ZERO, | ||
block_number: 1, | ||
new_root: Felt::ZERO, | ||
timestamp: 0, | ||
sequencer_address: Felt::ZERO, | ||
l1_gas_price: ResourcePrice { price_in_wei: Felt::ZERO, price_in_fri: Felt::ZERO }, | ||
l1_data_gas_price: ResourcePrice { price_in_wei: Felt::ZERO, price_in_fri: Felt::ZERO }, | ||
l1_da_mode: L1DataAvailabilityMode::Blob, | ||
starknet_version: String::from("0.13.2.1"), | ||
transactions: vec![], | ||
}; | ||
|
||
let starknet_version = blockifier::versioned_constants::StarknetVersion::Latest; | ||
|
||
// Call this function must not fail | ||
let block_context = build_block_context(chain_id, &block, starknet_version).unwrap(); | ||
|
||
// Verify that gas prices were set to NonZeroU128::MIN | ||
assert_eq!(block_context.block_info().gas_prices.eth_l1_gas_price, NonZeroU128::MIN); | ||
assert_eq!(block_context.block_info().gas_prices.strk_l1_gas_price, NonZeroU128::MIN); | ||
assert_eq!(block_context.block_info().gas_prices.eth_l1_data_gas_price, NonZeroU128::MIN); | ||
assert_eq!(block_context.block_info().gas_prices.strk_l1_data_gas_price, NonZeroU128::MIN); | ||
} | ||
Comment on lines
+68
to
+109
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cool! Could we add a test that checks that, if we have correct values, the block context is updated accordingly? |
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
pub mod block_context; | ||
pub mod rpc_state_reader; | ||
pub mod transactions; | ||
pub(crate) mod utils; | ||
pub mod utils; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,7 +28,7 @@ async fn test_replay_block() { | |
let state_reader = AsyncRpcStateReader::new(rpc_client.clone(), previous_block_id); | ||
let mut state = CachedState::from(state_reader); | ||
|
||
let block_context = build_block_context(ChainId::Sepolia, &block_with_txs, StarknetVersion::V0_13_1); | ||
let block_context = build_block_context(ChainId::Sepolia, &block_with_txs, StarknetVersion::V0_13_1).unwrap(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Handle error |
||
|
||
let traces = rpc_client | ||
.starknet_rpc() | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why don't handle the error gracefully?