Skip to content

Commit ac1a591

Browse files
alxiongtwittner
andcommitted
address comments
Co-authored-by: Toralf Wittner <[email protected]>
1 parent 45136c3 commit ac1a591

File tree

8 files changed

+93
-81
lines changed

8 files changed

+93
-81
lines changed

Cargo.lock

Lines changed: 0 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

justfile

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -136,31 +136,4 @@ test-individually:
136136
done
137137

138138
test-contract-deploy:
139-
#!/bin/bash
140-
set -exo pipefail
141-
142-
# Kill any existing anvil processes to avoid port conflicts
143-
pkill anvil || true
144-
sleep 1
145-
146-
# Start anvil in background
147-
anvil --port 8545 > anvil.log 2>&1 &
148-
ANVIL_PID=$!
149-
echo $ANVIL_PID > .anvil.pid
150-
151-
# Set up cleanup function
152-
cleanup() {
153-
if [ -n "$ANVIL_PID" ]; then
154-
kill $ANVIL_PID 2>/dev/null || true
155-
fi
156-
rm -f .anvil.pid anvil.log
157-
}
158-
159-
# Ensure cleanup happens on exit
160-
trap cleanup EXIT
161-
162-
# Wait for anvil to start
163-
sleep 1
164-
165-
# Run the deploy command
166-
RUST_LOG=info cargo run --bin deploy --config ./test-configs/keymanager.toml
139+
./scripts/test-contract-deploy

scripts/test-contract-deploy

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/env bash
2+
3+
set -emuo pipefail
4+
5+
cleanup() {
6+
trap - EXIT TERM
7+
if [ -n "$ANVIL_PID" ]; then
8+
kill "$ANVIL_PID" 2>/dev/null || true
9+
fi
10+
rm -f .anvil.pid anvil.log
11+
}
12+
13+
trap cleanup EXIT TERM INT
14+
15+
# Kill any existing anvil processes to avoid port conflicts
16+
pkill anvil || true
17+
sleep 1
18+
19+
# Start anvil in background
20+
anvil --port 8545 >anvil.log 2>&1 &
21+
ANVIL_PID=$!
22+
echo $ANVIL_PID >.anvil.pid
23+
24+
# Wait for anvil to start
25+
sleep 1
26+
27+
# Run the deploy command
28+
MANAGER_MNEMONIC="test test test test test test test test test test test junk"
29+
MANAGER_ACCOUNT_INDEX=0
30+
URL="http://localhost:8545"
31+
RUST_LOG=info cargo run --bin deploy -- -m "$MANAGER_MNEMONIC" -i "$MANAGER_ACCOUNT_INDEX" -u "$URL"

test-configs/keymanager.toml

Lines changed: 0 additions & 7 deletions
This file was deleted.

timeboost-contract/Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ serde = { workspace = true }
1515
timeboost-utils = { path = "../timeboost-utils" }
1616
tokio = { workspace = true }
1717
toml = { workspace = true }
18-
toml_edit = { workspace = true }
19-
tracing-subscriber = { workspace = true }
2018
url = { workspace = true }
2119

2220
[build-dependencies]
Lines changed: 55 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,57 @@
11
//! CLI for contract deployment
22
//!
3+
//! # Usage
4+
//!
5+
//! ```
6+
//! # Write config to stdout
7+
//! cargo run --bin deploy -- -m "your mnemonic here" -i 0 -u http://localhost:8545
8+
//!
9+
//! # Write config to a file
10+
//! cargo run --bin deploy -- -m "your mnemonic here" -i 0 -u http://localhost:8545 -o output.toml
11+
//! ```
12+
//!
313
//! # Local test
414
//! Run `just test-contract-deploy`
515
use alloy::{primitives::Address, providers::WalletProvider};
616
use anyhow::{Context, Result};
717
use clap::Parser;
8-
use serde::Deserialize;
18+
use serde::{Deserialize, Serialize};
919
use std::{fs, path::PathBuf};
1020
use timeboost_contract::provider::build_provider;
1121
use timeboost_utils::types::logging;
12-
use toml_edit::{DocumentMut, value};
22+
use tracing::info;
1323
use url::Url;
1424

1525
#[derive(Clone, Debug, Parser)]
1626
struct Args {
17-
/// Config file storing `KeyManagerConfig`
18-
#[clap(short, long, default_value = "./test-configs/keymanager.toml")]
19-
config: PathBuf,
27+
#[clap(short, long)]
28+
mnemonic: String,
29+
30+
#[clap(short, long)]
31+
index: u32,
32+
33+
#[clap(short, long)]
34+
url: Url,
35+
36+
#[clap(short, long)]
37+
output: Option<PathBuf>,
2038
}
2139

2240
/// Config type for the key manager who has the permission to update the KeyManager contract
2341
/// See `test-configs/keymanager.toml` for an example
24-
#[derive(Debug, Deserialize)]
42+
#[derive(Debug, Deserialize, Serialize)]
2543
struct KeyManagerConfig {
2644
wallet: LocalWalletConfig,
2745
deployments: Deployments,
2846
}
2947

30-
#[derive(Debug, Deserialize)]
48+
#[derive(Debug, Deserialize, Serialize)]
3149
struct LocalWalletConfig {
3250
mnemonic: String,
3351
account_index: u32,
3452
}
3553

36-
#[derive(Debug, Deserialize)]
54+
#[derive(Debug, Deserialize, Serialize)]
3755
#[allow(dead_code)]
3856
struct Deployments {
3957
/// RPC endpoint of the target chain
@@ -47,49 +65,48 @@ async fn main() -> Result<()> {
4765
logging::init_logging();
4866

4967
let args = Args::parse();
50-
let config_path = args.config;
5168

52-
tracing::info!(
53-
"Starting contract deployment with config: {:?}",
54-
config_path
55-
);
69+
info!("Starting contract deployment");
5670

57-
// Read and parse config file
58-
let config_content = fs::read_to_string(&config_path)
59-
.with_context(|| format!("Failed to read config file: {config_path:?}"))?;
60-
let config: KeyManagerConfig = toml::from_str(&config_content)
61-
.with_context(|| format!("Failed to parse config file: {config_path:?}"))?;
62-
tracing::info!("Config loaded successfully");
71+
// Construct the config from command-line arguments
72+
let mut cfg = KeyManagerConfig {
73+
wallet: LocalWalletConfig {
74+
mnemonic: args.mnemonic,
75+
account_index: args.index,
76+
},
77+
deployments: Deployments {
78+
chain_url: args.url,
79+
key_manager: None,
80+
},
81+
};
6382

6483
// Build provider
6584
let provider = build_provider(
66-
config.wallet.mnemonic,
67-
config.wallet.account_index,
68-
config.deployments.chain_url,
85+
cfg.wallet.mnemonic.clone(),
86+
cfg.wallet.account_index,
87+
cfg.deployments.chain_url.clone(),
6988
);
7089

7190
let manager = provider.default_signer_address();
72-
tracing::info!("Deploying with maanger address: {manager:#x}");
91+
info!("Deploying with manager address: {manager:#x}");
7392

7493
// Deploy the KeyManager contract
7594
let km_addr = timeboost_contract::deployer::deploy_key_manager_contract(&provider, manager)
7695
.await
7796
.context("Failed to deploy KeyManager contract")?;
78-
tracing::info!("KeyManager deployed successfully at: {km_addr:#x}");
79-
80-
// Update the config file with the deployed address
81-
let mut doc = config_content
82-
.parse::<DocumentMut>()
83-
.with_context(|| format!("Failed to parse TOML in config file: {config_path:?}"))?;
84-
85-
// Set the key_manager address in the deployments section
86-
doc["deployments"]["key_manager"] = value(format!("{km_addr:#x}"));
87-
88-
// Write back to file
89-
fs::write(&config_path, doc.to_string())
90-
.with_context(|| format!("Failed to write updated config file: {config_path:?}"))?;
91-
92-
tracing::info!("Config file updated with KeyManager address: {km_addr:#x}");
97+
info!("KeyManager deployed successfully at: {km_addr:#x}");
98+
99+
// Update the address and deliver the final config
100+
cfg.deployments.key_manager = Some(km_addr);
101+
let toml = toml::to_string_pretty(&cfg)?;
102+
103+
if let Some(out) = &args.output {
104+
fs::write(out, &toml)?;
105+
info!(file=?out, "Config written to file");
106+
} else {
107+
println!("===== OUTPUT ======");
108+
println!("{toml}");
109+
}
93110

94111
Ok(())
95112
}

timeboost-contract/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ mod sol_types;
1818
use provider::TestProviderWithWallet;
1919
pub use sol_types::*;
2020

21-
/// Similar to [`init_chain()`] but spawn a local test chain and deploy contracts there instead.
21+
/// Spawn a local test blockchain and deploy KeyManager contract.
22+
/// Returns a WalletProvider to the chain and the deployed contract address.
2223
pub async fn init_test_chain() -> Result<(TestProviderWithWallet, Address)> {
2324
// this provider wraps both the test chain instance (exit on drop), and the wallet provider
2425
let provider = ProviderBuilder::new().connect_anvil_with_wallet();

timeboost-contract/src/sol_types.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
//! Solidity types for contract interaction
22
3-
use alloy::primitives::Bytes;
4-
use rand::prelude::*;
5-
63
// We manually re-export the type here carefully due to alloy's lack of shared type:
74
// tracking issue: https://github.com/foundry-rs/foundry/issues/10153
85
pub use crate::bindings::{
@@ -12,7 +9,11 @@ pub use crate::bindings::{
129
};
1310

1411
impl CommitteeMemberSol {
12+
#[cfg(test)]
1513
pub fn random() -> Self {
14+
use alloy::primitives::Bytes;
15+
use rand::prelude::*;
16+
1617
let mut rng = rand::rng();
1718
CommitteeMemberSol {
1819
sigKey: Bytes::from(rng.random::<[u8; 32]>()),

0 commit comments

Comments
 (0)