Skip to content

Commit

Permalink
fix: copy hive binary + automatic deployment (#781)
Browse files Browse the repository at this point in the history
* copy hive binary

* add dev allocation to genesis

* fix automatic deploy
  • Loading branch information
greged93 committed Feb 22, 2024
1 parent 606aefa commit 56379e7
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 42 deletions.
2 changes: 1 addition & 1 deletion .trunk/trunk.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ lint:
- [email protected]
- [email protected]
- [email protected]
- [email protected].6
- [email protected].7
- [email protected]
ignore:
- linters: [ALL]
Expand Down
1 change: 1 addition & 0 deletions docker/rpc/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ WORKDIR /usr/src/app

# Copy the built binary from the previous container
COPY --from=builder /usr/src/rpc/target/release/kakarot-rpc /usr/local/bin
COPY --from=builder /usr/src/rpc/target/release/kakarot-rpc-hive /usr/local/bin
COPY --from=builder /usr/src/rpc/target/release/hive_genesis /usr/local/bin

# Expose the port that the RPC server will run on
Expand Down
30 changes: 5 additions & 25 deletions src/bin/hive_genesis.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use dotenv::dotenv;
use kakarot_rpc::test_utils::{hive::HiveGenesisConfig, katana::genesis::KatanaGenesisBuilder};
use katana_primitives::genesis::{
allocation::DevAllocationsGenerator, constant::DEFAULT_PREFUNDED_ACCOUNT_BALANCE, json::GenesisAccountJson,
};
use starknet_crypto::FieldElement;
use std::{env::var, path::Path};

Expand All @@ -16,37 +13,20 @@ fn main() {
Path::new(&var("HIVE_GENESIS_PATH").expect("Failed to load HIVE_GENESIS_PATH var")).to_path_buf();

// Read all the classes.
let builder = KatanaGenesisBuilder::default().load_classes(kakarot_contracts_path);
let mut builder = KatanaGenesisBuilder::default().load_classes(kakarot_contracts_path);

// Add dev allocations.
builder = builder.with_dev_allocation(10);

// Read the hive genesis.
let hive_genesis_content = std::fs::read_to_string(hive_genesis_path).expect("Failed to read hive genesis file");
let hive_genesis: HiveGenesisConfig =
serde_json::from_str(&hive_genesis_content).expect("Failed to parse hive genesis json");

// Convert the hive genesis to a katana genesis.
let mut genesis_json =
let genesis_json =
hive_genesis.try_into_genesis_json(builder.clone()).expect("Failed to convert hive genesis to katana genesis");

// Add dev allocations.
let dev_allocations = DevAllocationsGenerator::new(10)
.with_balance(DEFAULT_PREFUNDED_ACCOUNT_BALANCE)
.generate()
.into_iter()
.map(|(address, account)| {
(
address,
GenesisAccountJson {
public_key: account.public_key,
balance: Some(account.balance),
nonce: account.nonce,
class: None,
storage: account.storage.clone(),
},
)
})
.collect::<Vec<_>>();
genesis_json.accounts.extend(dev_allocations);

let builder = builder.with_kakarot(FieldElement::ZERO).expect("Failed to set up Kakarot");
let manifest = builder.manifest();

Expand Down
1 change: 1 addition & 0 deletions src/bin/katana_genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ fn main() {
.with_kakarot(*COINBASE_ADDRESS)
.expect("Failed to set up Kakarot");
builder = builder.with_eoa(pk).expect("Failed to set up EOA").fund(pk, U256::from(u128::MAX)).unwrap();
builder = builder.with_dev_allocation(10);

let manifest = builder.manifest();

Expand Down
3 changes: 3 additions & 0 deletions src/eth_provider/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ use {
signers::{LocalWallet, SigningKey},
},
starknet_crypto::FieldElement,
std::sync::Arc,
std::{env::var, str::FromStr, sync::OnceLock},
tokio::sync::Mutex,
};

#[cfg(feature = "hive")]
Expand All @@ -34,4 +36,5 @@ lazy_static! {
*CHAIN_ID.get().expect("Missing chain ID"),
ExecutionEncoding::New
);
pub static ref DEPLOY_WALLET_NONCE: Arc<Mutex<FieldElement>> = Arc::new(Mutex::new(FieldElement::ZERO));
}
16 changes: 9 additions & 7 deletions src/eth_provider/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -456,8 +456,7 @@ where
// If the contract is not found, we need to deploy it.
#[cfg(feature = "hive")]
{
use crate::eth_provider::constant::{CHAIN_ID, DEPLOY_WALLET};
use starknet::accounts::Account;
use crate::eth_provider::constant::{CHAIN_ID, DEPLOY_WALLET, DEPLOY_WALLET_NONCE};
use starknet::accounts::Call;
use starknet::accounts::Execution;
use starknet::core::types::BlockTag;
Expand All @@ -478,19 +477,22 @@ where
}],
&*DEPLOY_WALLET,
);
let nonce = self
.starknet_provider
.get_nonce(StarknetBlockId::Tag(BlockTag::Latest), DEPLOY_WALLET.address())
.await?;

let mut nonce = DEPLOY_WALLET_NONCE.lock().await;
let current_nonce = *nonce;

let tx = execution
.nonce(nonce)
.nonce(current_nonce)
.max_fee(FieldElement::from(u64::MAX))
.prepared()
.map_err(|err| eyre::eyre!(err.to_string()))?
.get_invoke_request(false)
.await
.map_err(|err| eyre::eyre!(err.to_string()))?;
self.starknet_provider.add_invoke_transaction(tx).await?;

*nonce += 1u8.into();
drop(nonce);
};
}

Expand Down
10 changes: 10 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ async fn main() -> Result<()> {
DatabaseOptions::builder().read_concern(ReadConcern::MAJORITY).write_concern(WriteConcern::MAJORITY).build(),
));

// Get the deployer nonce and set the value in the DEPLOY_WALLET_NONCE
#[cfg(feature = "hive")]
{
use kakarot_rpc::eth_provider::constant::{DEPLOY_WALLET, DEPLOY_WALLET_NONCE};
use starknet::accounts::ConnectedAccount;
let deployer_nonce = DEPLOY_WALLET.get_nonce().await?;
let mut nonce = DEPLOY_WALLET_NONCE.lock().await;
*nonce = deployer_nonce;
}

let kakarot_rpc_module = match starknet_provider {
StarknetProvider::JsonRpcClient(starknet_provider) => {
let starknet_provider = Arc::new(starknet_provider);
Expand Down
1 change: 1 addition & 0 deletions src/test_utils/eoa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ pub trait Eoa<P: Provider + Send + Sync> {
}
}

#[derive(Clone)]
pub struct KakarotEOA<P: Provider> {
pub private_key: B256,
pub eth_provider: Arc<EthDataProvider<P>>,
Expand Down
30 changes: 29 additions & 1 deletion src/test_utils/katana/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ use ethers::types::U256;
use eyre::{eyre, Result};
use katana_primitives::block::GasPrices;
use katana_primitives::contract::{StorageKey, StorageValue};
use katana_primitives::genesis::allocation::DevAllocationsGenerator;
use katana_primitives::genesis::constant::DEFAULT_FEE_TOKEN_ADDRESS;
use katana_primitives::genesis::constant::DEFAULT_PREFUNDED_ACCOUNT_BALANCE;
use katana_primitives::genesis::json::GenesisAccountJson;
use katana_primitives::genesis::json::{FeeTokenConfigJson, GenesisJson};
use katana_primitives::{
contract::ContractAddress,
Expand Down Expand Up @@ -56,6 +59,7 @@ pub struct KatanaGenesisBuilder<T> {
classes: Vec<GenesisClassJson>,
class_hashes: HashMap<String, FieldElement>,
contracts: HashMap<ContractAddress, GenesisContractJson>,
accounts: HashMap<ContractAddress, GenesisAccountJson>,
fee_token_storage: HashMap<StorageKey, StorageValue>,
cache: HashMap<String, FieldElement>,
status: PhantomData<T>,
Expand All @@ -68,12 +72,35 @@ impl<T> KatanaGenesisBuilder<T> {
classes: self.classes,
class_hashes: self.class_hashes,
contracts: self.contracts,
accounts: self.accounts,
fee_token_storage: self.fee_token_storage,
cache: self.cache,
status: PhantomData::<State>,
}
}

pub fn with_dev_allocation(mut self, amount: u16) -> Self {
let dev_allocations = DevAllocationsGenerator::new(amount)
.with_balance(DEFAULT_PREFUNDED_ACCOUNT_BALANCE)
.generate()
.into_iter()
.map(|(address, account)| {
(
address,
GenesisAccountJson {
public_key: account.public_key,
balance: Some(account.balance),
nonce: account.nonce,
class: None,
storage: account.storage.clone(),
},
)
});
self.accounts.extend(dev_allocations);

self
}

fn kakarot_class_hash(&self) -> Result<FieldElement> {
self.class_hashes.get("kakarot").cloned().ok_or(eyre!("Missing Kakarot class hash"))
}
Expand Down Expand Up @@ -102,6 +129,7 @@ impl Default for KatanaGenesisBuilder<Uninitialized> {
classes: vec![],
class_hashes: HashMap::new(),
contracts: HashMap::new(),
accounts: HashMap::new(),
fee_token_storage: HashMap::new(),
cache: HashMap::new(),
status: PhantomData::<Uninitialized>,
Expand Down Expand Up @@ -284,7 +312,7 @@ impl KatanaGenesisBuilder<Initialized> {
class: None,
},
universal_deployer: None,
accounts: HashMap::new(),
accounts: self.accounts,
contracts: self.contracts,
})
}
Expand Down
4 changes: 2 additions & 2 deletions src/test_utils/katana/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ impl Katana {
self.eoa.eth_provider.clone()
}

pub const fn eoa(&self) -> &KakarotEOA<Arc<JsonRpcClient<HttpTransport>>> {
&self.eoa
pub fn eoa(&self) -> KakarotEOA<Arc<JsonRpcClient<HttpTransport>>> {
self.eoa.clone()
}

/// allow(dead_code) is used because this function is used in tests,
Expand Down
34 changes: 28 additions & 6 deletions tests/eth_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,19 +264,41 @@ async fn test_fee_history(#[future] katana: Katana, _setup: ()) {
#[tokio::test(flavor = "multi_thread")]
#[cfg(feature = "hive")]
async fn test_predeploy_eoa(#[future] katana: Katana, _setup: ()) {
use futures::future::join_all;
use kakarot_rpc::test_utils::eoa::KakarotEOA;
use reth_primitives::B256;
use starknet::providers::Provider;

// Given
let eoa = katana.eoa();
let eth_provider = katana.eth_provider();
let other_eoa = KakarotEOA::new(B256::from_str(&format!("0x{:0>64}", "0abde1")).unwrap(), eth_provider.clone());
let starknet_provider = eth_provider.starknet_provider();
let other_eoa_1 = KakarotEOA::new(B256::from_str(&format!("0x{:0>64}", "0abde1")).unwrap(), eth_provider.clone());
let other_eoa_2 = KakarotEOA::new(B256::from_str(&format!("0x{:0>64}", "0abde2")).unwrap(), eth_provider.clone());

// When
let evm_address = eoa.evm_address().unwrap();
let balance_before = eth_provider.balance(eoa.evm_address().unwrap(), None).await.unwrap();
eoa.transfer(other_eoa.evm_address().unwrap(), 1).await.expect("Failed to transfer funds to other eoa");
other_eoa.transfer(eoa.evm_address().unwrap(), 1).await.expect("Failed to transfer funds back to eoa");
let balance_after = eth_provider.balance(eoa.evm_address().unwrap(), None).await.unwrap();
eoa.transfer(other_eoa_1.evm_address().unwrap(), 1).await.expect("Failed to transfer funds to other eoa 1");
// Sleep for 2 seconds to let the transaction pass
tokio::time::sleep(tokio::time::Duration::from_secs(2)).await;
eoa.transfer(other_eoa_2.evm_address().unwrap(), 2).await.expect("Failed to transfer funds to other eoa 2");
// Sleep for 2 seconds to let the transaction pass
tokio::time::sleep(tokio::time::Duration::from_secs(2)).await;

// When
let t1 = tokio::task::spawn(async move {
other_eoa_1.transfer(evm_address, 1).await.expect("Failed to transfer funds back to eoa");
});
let t2 = tokio::task::spawn(async move {
other_eoa_2.transfer(evm_address, 1).await.expect("Failed to transfer funds back to eoa");
});
join_all([t1, t2]).await;

// Then
assert_eq!(balance_after, balance_before);
// Await all transactions to pass
while starknet_provider.block_number().await.unwrap() < 6 {
tokio::time::sleep(tokio::time::Duration::from_secs(1)).await;
}
let balance_after = eth_provider.balance(evm_address, None).await.unwrap();
assert_eq!(balance_after, balance_before - U256::from(1));
}

0 comments on commit 56379e7

Please sign in to comment.