-
Notifications
You must be signed in to change notification settings - Fork 15
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
small changes #89
Merged
Merged
small changes #89
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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 |
---|---|---|
|
@@ -13,11 +13,9 @@ use alloy::{ | |
signers::local::PrivateKeySigner, | ||
}; | ||
|
||
// use eyre::Result; | ||
use alloy::eips::eip2930::AccessList; | ||
use alloy::eips::eip4844::BYTES_PER_BLOB; | ||
use alloy::hex; | ||
// use alloy::node_bindings::Anvil; | ||
use alloy::rpc::types::TransactionRequest; | ||
use alloy_primitives::Bytes; | ||
use async_trait::async_trait; | ||
|
@@ -29,6 +27,8 @@ use mockall::{automock, lazy_static, predicate::*}; | |
use alloy::providers::ProviderBuilder; | ||
use conversion::{get_input_data_for_eip_4844, prepare_sidecar}; | ||
use settlement_client_interface::{SettlementClient, SettlementVerificationStatus, SETTLEMENT_SETTINGS_NAME}; | ||
#[cfg(test)] | ||
use url::Url; | ||
use utils::{env_utils::get_env_var_or_panic, settings::SettingsProvider}; | ||
|
||
use crate::clients::interfaces::validity_interface::StarknetValidityContractTrait; | ||
|
@@ -39,23 +39,12 @@ pub mod clients; | |
pub mod config; | ||
pub mod conversion; | ||
|
||
// IMPORTANT to understand #[cfg(test)], #[cfg(not(test))] and SHOULD_IMPERSONATE_ACCOUNT | ||
// Two tests : `update_state_blob_with_dummy_contract_works` & `update_state_blob_with_impersonation_works` use a env var `SHOULD_IMPERSONATE_ACCOUNT` to inform the function `update_state_with_blobs` about the kind of testing, | ||
// `SHOULD_IMPERSONATE_ACCOUNT` can have any of "0" or "1" value : | ||
// - if "0" then : Testing via default Anvil address. | ||
// - if "1" then : Testing via impersonating `Starknet Operator Address`. | ||
// Note : changing between "0" and "1" is handled automatically by each test function, `no` manual change in `env.test` is needed. | ||
|
||
#[cfg(test)] | ||
mod tests; | ||
pub mod types; | ||
|
||
#[cfg(test)] | ||
use {alloy::providers::RootProvider, alloy::transports::http::Http, reqwest::Client}; | ||
|
||
#[cfg(not(test))] | ||
use types::EthHttpProvider; | ||
|
||
pub const ENV_PRIVATE_KEY: &str = "ETHEREUM_PRIVATE_KEY"; | ||
|
||
lazy_static! { | ||
|
@@ -66,19 +55,16 @@ lazy_static! { | |
.expect("Error loading trusted setup file"); | ||
} | ||
|
||
#[allow(dead_code)] | ||
pub struct EthereumSettlementClient { | ||
core_contract_client: StarknetValidityContractClient, | ||
wallet: EthereumWallet, | ||
wallet_address: Address, | ||
#[cfg(not(test))] | ||
provider: Arc<EthHttpProvider>, | ||
provider: Arc<RootProvider<Http<Client>>>, | ||
#[cfg(test)] | ||
provider: RootProvider<Http<Client>>, | ||
impersonate_account: Option<Address>, | ||
} | ||
|
||
impl EthereumSettlementClient { | ||
#[cfg(not(test))] | ||
pub fn with_settings(settings: &impl SettingsProvider) -> Self { | ||
let settlement_cfg: EthereumSettlementConfig = settings.get_settings(SETTLEMENT_SETTINGS_NAME).unwrap(); | ||
|
||
|
@@ -87,46 +73,63 @@ impl EthereumSettlementClient { | |
let wallet_address = signer.address(); | ||
let wallet = EthereumWallet::from(signer); | ||
|
||
let provider = Arc::new( | ||
// provider without wallet | ||
let provider = Arc::new(ProviderBuilder::new().on_http(settlement_cfg.rpc_url.clone())); | ||
|
||
// provider with wallet | ||
let filler_provider = Arc::new( | ||
ProviderBuilder::new().with_recommended_fillers().wallet(wallet.clone()).on_http(settlement_cfg.rpc_url), | ||
); | ||
|
||
let core_contract_client = StarknetValidityContractClient::new( | ||
Address::from_str(&settlement_cfg.core_contract_address) | ||
.expect("Failed to convert the validity contract address.") | ||
.0 | ||
.into(), | ||
provider.clone(), | ||
filler_provider, | ||
); | ||
|
||
EthereumSettlementClient { provider, core_contract_client, wallet, wallet_address } | ||
EthereumSettlementClient { | ||
provider, | ||
core_contract_client, | ||
wallet, | ||
wallet_address, | ||
#[cfg(test)] | ||
impersonate_account: None, | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
pub fn with_test_settings(settings: &impl SettingsProvider, provider: RootProvider<Http<Client>>) -> Self { | ||
use tests::{SHOULD_IMPERSONATE_ACCOUNT, TEST_DUMMY_CONTRACT_ADDRESS}; | ||
let settlement_cfg: EthereumSettlementConfig = settings.get_settings(SETTLEMENT_SETTINGS_NAME).unwrap(); | ||
|
||
pub fn with_test_settings( | ||
provider: RootProvider<Http<Client>>, | ||
core_contract_address: Option<Address>, | ||
rpc_url: Url, | ||
impersonate_account: Option<Address>, | ||
) -> Self { | ||
let private_key = get_env_var_or_panic(ENV_PRIVATE_KEY); | ||
let signer: PrivateKeySigner = private_key.parse().expect("Failed to parse private key"); | ||
let wallet_address = signer.address(); | ||
let wallet = EthereumWallet::from(signer); | ||
|
||
let fill_provider = Arc::new( | ||
ProviderBuilder::new().with_recommended_fillers().wallet(wallet.clone()).on_http(settlement_cfg.rpc_url), | ||
); | ||
let fill_provider = | ||
Arc::new(ProviderBuilder::new().with_recommended_fillers().wallet(wallet.clone()).on_http(rpc_url)); | ||
|
||
let core_contract_address = if *SHOULD_IMPERSONATE_ACCOUNT { | ||
&settlement_cfg.core_contract_address | ||
let core_contract_address = if let Some(core_contract_address) = core_contract_address { | ||
core_contract_address.clone() | ||
} else { | ||
&*TEST_DUMMY_CONTRACT_ADDRESS | ||
// dummy address | ||
Address::from_str("0x0").unwrap() | ||
}; | ||
|
||
let core_contract_client = StarknetValidityContractClient::new( | ||
Address::from_str(core_contract_address).unwrap().0.into(), | ||
fill_provider, | ||
); | ||
let core_contract_client = StarknetValidityContractClient::new(core_contract_address, fill_provider); | ||
|
||
EthereumSettlementClient { provider, core_contract_client, wallet, wallet_address } | ||
EthereumSettlementClient { | ||
provider: Arc::new(provider), | ||
core_contract_client, | ||
wallet, | ||
wallet_address, | ||
impersonate_account, | ||
} | ||
} | ||
|
||
/// Build kzg proof for the x_0 point evaluation | ||
|
@@ -184,12 +187,6 @@ impl SettlementClient for EthereumSettlementClient { | |
} | ||
|
||
/// Should be used to update state on core contract when DA is in blobs/alt DA | ||
async fn update_state_blobs(&self, program_output: Vec<[u8; 32]>, kzg_proof: [u8; 48]) -> Result<String> { | ||
let program_output: Vec<U256> = vec_u8_32_to_vec_u256(&program_output)?; | ||
let tx_receipt = self.core_contract_client.update_state_kzg(program_output, kzg_proof).await?; | ||
Ok(format!("0x{:x}", tx_receipt.transaction_hash)) | ||
} | ||
|
||
async fn update_state_with_blobs( | ||
&self, | ||
program_output: Vec<[u8; 32]>, | ||
|
@@ -246,13 +243,14 @@ impl SettlementClient for EthereumSettlementClient { | |
let signature = self.wallet.default_signer().sign_transaction(&mut variant).await?; | ||
let tx_signed = variant.into_signed(signature); | ||
let tx_envelope: TxEnvelope = tx_signed.into(); | ||
// IMP: this conversion strips signature from the transaction | ||
|
||
// IMP: this conversion strips signature from the transaction | ||
#[cfg(not(test))] | ||
let txn_request: TransactionRequest = tx_envelope.into(); | ||
|
||
#[cfg(test)] | ||
let txn_request = test_config::configure_transaction(tx_envelope); | ||
let txn_request = | ||
test_config::configure_transaction(self.provider.clone(), tx_envelope, self.impersonate_account).await; | ||
|
||
let pending_transaction = self.provider.send_transaction(txn_request).await?; | ||
return Ok(pending_transaction.tx_hash().to_string()); | ||
|
@@ -297,14 +295,25 @@ impl SettlementClient for EthereumSettlementClient { | |
mod test_config { | ||
use super::*; | ||
use alloy::network::TransactionBuilder; | ||
use tests::{ADDRESS_TO_IMPERSONATE, SHOULD_IMPERSONATE_ACCOUNT, TEST_NONCE}; | ||
|
||
pub fn configure_transaction(tx_envelope: TxEnvelope) -> TransactionRequest { | ||
pub async fn configure_transaction( | ||
provider: Arc<RootProvider<Http<Client>>>, | ||
tx_envelope: TxEnvelope, | ||
impersonate_account: Option<Address>, | ||
) -> TransactionRequest { | ||
let mut txn_request: TransactionRequest = tx_envelope.into(); | ||
|
||
if *SHOULD_IMPERSONATE_ACCOUNT { | ||
txn_request.set_nonce(*TEST_NONCE); | ||
txn_request = txn_request.with_from(*ADDRESS_TO_IMPERSONATE); | ||
// IMPORTANT to understand #[cfg(test)], #[cfg(not(test))] and SHOULD_IMPERSONATE_ACCOUNT | ||
// Two tests : `update_state_blob_with_dummy_contract_works` & `update_state_blob_with_impersonation_works` use a env var `SHOULD_IMPERSONATE_ACCOUNT` to inform the function `update_state_with_blobs` about the kind of testing, | ||
// `SHOULD_IMPERSONATE_ACCOUNT` can have any of "0" or "1" value : | ||
// - if "0" then : Testing via default Anvil address. | ||
// - if "1" then : Testing via impersonating `Starknet Operator Address`. | ||
// Note : changing between "0" and "1" is handled automatically by each test function, `no` manual change in `env.test` is needed. | ||
if let Some(impersonate_account) = impersonate_account { | ||
let nonce = | ||
provider.get_transaction_count(impersonate_account).await.unwrap().to_string().parse::<u64>().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. We already send the |
||
txn_request.set_nonce(nonce); | ||
txn_request = txn_request.with_from(impersonate_account); | ||
} | ||
|
||
txn_request | ||
|
Oops, something went wrong.
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.
This is needed in test cases outside of Settlement Client, hence keeping