Skip to content

Commit

Permalink
add oz account fleet addresses and init test (#1377)
Browse files Browse the repository at this point in the history
* add account fleet addresses and init test

* fix clippy

* trunk fmt
  • Loading branch information
tcoratger committed Sep 12, 2024
1 parent 0a175c2 commit bf32a7c
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 21 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ hex = { version = "0.4", default-features = false }
proptest = { version = "1.5", default-features = false }
reqwest = { version = "0.12", default-features = false }
toml = { version = "0.8", default-features = false }
tempfile = "3.8"

[features]
testing = [
Expand Down
93 changes: 90 additions & 3 deletions src/pool/accounts.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,92 @@
[
"0x00686735619287df0f11ec4cda22675f780886b52bf59cf899dd57fd5d5f4cad",
"0x0332825a42ccbec3e2ceb6c242f4dff4682e7d16b8559104b5df8fd925ddda09",
"0x003f5628053c2d6bdfc9e45ea8aeb14405b8917226d455a94b3225a9a7520559"
{
"address": 2883640181176136234335915321173609316686035330597712783682502353343947167672
},
{
"address": 163189206500119404227396165700254790683726361202744501915894563246389642629
},
{
"address": 99811111405698367639528220086574377972317373523910065236962345649200639292
},
{
"address": 2897092793511649051419086551617932615733568314252678278132566380801240597692
},
{
"address": 3557618131434419594032056470174132498562617124886954494051347809858912455674
},
{
"address": 1217339404557647669586992910030803945817900518679235602197474203370008659931
},
{
"address": 1464989680417864978593244890085291655623537972528277142413062631659350590777
},
{
"address": 3500721539480872355911032760932736747238156289671001523378953906588583720447
},
{
"address": 1189807619050407034899032453787483131133983484974792461173832274025084191774
},
{
"address": 2985546498260957401453332630595421074332534262451681090083934350878055070090
},
{
"address": 2580096488720121038258346221537379350097672926793615943695292983325940933873
},
{
"address": 706586203185748046365025732046053556805324249384167918098765550171718485921
},
{
"address": 421352004629673117208877412201401279649821578920324265912223355570932536940
},
{
"address": 2603356782109509424930544374694920931981019619056919975558520835269149427754
},
{
"address": 731033513512582389832362211515114005979257885275159768513182805678819013813
},
{
"address": 398281199416797166894380656032915600018115167507583891417737921174186928892
},
{
"address": 2774917776797702469508748354373730940526329628211713188213584292456328299705
},
{
"address": 1694157804671749028259579467788788754526050718013798913033975622792629758778
},
{
"address": 1296348027856980725603557273608267665711233357934795200103762754011200899651
},
{
"address": 174699578338218540309219917032974618221356133891943608204320521113172701168
},
{
"address": 3329414828134176321922292135530875317266323263350298936045126776946408659638
},
{
"address": 404387488974038694333026067659627045970703132572673602290137191805941292350
},
{
"address": 3514189287805716489413613662390603552056579910539979406321636000461641750195
},
{
"address": 2685648146184946138079197716161208410943314695135298447068882451708251814403
},
{
"address": 351432195372800705139748767484403593176983146171895792478992735683159545982
},
{
"address": 649343696316947362661130019698399215509076913806056259510851614341093582720
},
{
"address": 3419974615650193381701278879036176812715335592876510554127336537442872429736
},
{
"address": 380681588646550140026679325470389027549802801092031324722960996196573866569
},
{
"address": 1892212033858068894675761061916192924097964867361432342750729538530979196470
},
{
"address": 1931767032496410938755750916548087456375215926597715702491737739690723504395
}
]
90 changes: 72 additions & 18 deletions src/pool/mempool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use reth_transaction_pool::{
};
use serde_json::Value;
use starknet::core::types::Felt;
use std::{collections::HashMap, fs::File, io::Read, sync::Arc, time::Duration};
use std::{collections::HashMap, fs::File, io::Read, str::FromStr, sync::Arc, time::Duration};
use tokio::{
runtime::Handle,
sync::{Mutex, MutexGuard},
Expand Down Expand Up @@ -50,25 +50,28 @@ impl<SP: starknet::providers::Provider + Send + Sync + Clone + 'static> AccountM
// Parse the file contents as JSON
let json: Value = serde_json::from_str(&contents)?;

// Extract the account addresses from the JSON array
// Extract the account addresses from the JSON array of objects
if let Some(array) = json.as_array() {
for item in array {
if let Some(account_address) = item.as_str() {
let felt_address = Felt::from_hex_unchecked(account_address);

let starknet_block_id = eth_client
.eth_provider()
.to_starknet_block_id(Some(BlockId::default()))
.await
.map_err(|e| eyre::eyre!("Error converting block ID: {:?}", e))?;

// Query the initial account_nonce for the account from the provider
let nonce = eth_client
.starknet_provider()
.get_nonce(starknet_block_id, felt_address)
.await
.unwrap_or_default();
accounts.insert(felt_address, Arc::new(Mutex::new(nonce)));
if let Some(address_value) = item.get("address") {
if let Some(account_address) = address_value.as_str() {
let felt_address = Felt::from_str(account_address)
.map_err(|e| eyre::eyre!("Error converting account address to Felt: {:?}", e))?;

let starknet_block_id = eth_client
.eth_provider()
.to_starknet_block_id(Some(BlockId::default()))
.await
.map_err(|e| eyre::eyre!("Error converting block ID: {:?}", e))?;

// Query the initial account_nonce for the account from the provider
let nonce = eth_client
.starknet_provider()
.get_nonce(starknet_block_id, felt_address)
.await
.unwrap_or_default();
accounts.insert(felt_address, Arc::new(Mutex::new(nonce)));
}
}
}
}
Expand Down Expand Up @@ -157,3 +160,54 @@ impl<SP: starknet::providers::Provider + Send + Sync + Clone + 'static> AccountM
self.eth_client.starknet_provider().balance_at(account_address, starknet_block_id).await.map_err(Into::into)
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::test_utils::{fixtures::katana, katana::Katana};
use rstest::rstest;
use serde_json::json;
use std::io::Write;
use tempfile::NamedTempFile;

#[rstest]
#[awt]
#[tokio::test(flavor = "multi_thread")]
async fn test_account_manager_setup(#[future] katana: Katana) {
let eth_client = katana.eth_client();

// Create a temporary file to simulate the account JSON file
let mut temp_file = NamedTempFile::new().unwrap();
let json_data = json!([
{"address": "2883640181176136234335915321173609316686035330597712783682502353343947167672"},
{"address": "163189206500119404227396165700254790683726361202744501915894563246389642629"}
]);
write!(temp_file, "{json_data}").unwrap();

// Create an AccountManager instance with the temporary file
let account_manager =
AccountManager::new(temp_file.path().to_str().unwrap(), Arc::new(eth_client)).await.unwrap();

// Verify that the accounts are loaded correctly
let accounts = account_manager.accounts;
assert_eq!(accounts.len(), 2, "Expected 2 accounts in the manager");

// Expected account addresses.
//
// These are the addresses from the temporary JSON file converted to hex.
//
// We want to test a different init method from hex to be sure that the account manager handle the initialization of Felts correctly.
let expected_addresses = [
Felt::from_hex("0x660151ef6c0c8a4eda708478c8b909a8f784fd5b25c6d0f08fa9ea9957b57b8").unwrap(),
Felt::from_hex("0x5c5ca015b2dbfa8a25113a9e89fe996211f25a32887d43b5e9afefa3b8c585").unwrap(),
];

// Validate if the accounts are initialized with the correct nonce values
for (account, nonce) in &accounts {
// Assert that the account address is in the expected list
assert!(expected_addresses.contains(account), "Account address should be in the expected list");
// Assert that the account nonce is initialized to 0
assert_eq!(*(nonce.lock().await), Felt::ZERO, "Account nonce should be initialized to 0");
}
}
}

0 comments on commit bf32a7c

Please sign in to comment.