Skip to content
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

feat(ethexe): Add more context to returned errors #4463

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 17 additions & 26 deletions ethexe/cli/src/commands/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,41 +50,35 @@ impl KeyCommand {
pub fn exec(self) -> Result<()> {
let key_store = self.key_store.expect("must never be empty after merging");

let signer = Signer::new(key_store).with_context(|| "failed to create signer")?;
let signer = Signer::new(key_store).context("failed to create signer")?;

match self.command {
KeySubcommand::Clear => {
let len = signer.list_keys()?.len();

signer
.clear_keys()
.with_context(|| "failed to clear keys")?;
signer.clear_keys().context("failed to clear keys")?;

println!("Removed {len} keys");
}
KeySubcommand::Generate => {
// TODO: remove println from there.
let public = signer
.generate_key()
.with_context(|| "failed to generate new keypair")?;
.context("failed to generate new keypair")?;

println!("Public key: {public}");
println!("Ethereum address: {}", public.to_address());
}
KeySubcommand::Insert { private_key } => {
let private = private_key
.parse()
.with_context(|| "invalid `private-key`")?;
let private = private_key.parse().context("invalid `private-key`")?;

let public = signer
.add_key(private)
.with_context(|| "failed to add key")?;
let public = signer.add_key(private).context("failed to add key")?;

println!("Public key: {public}");
println!("Ethereum address: {}", public.to_address());
}
KeySubcommand::List => {
let publics = signer.list_keys().with_context(|| "failed to list keys")?;
let publics = signer.list_keys().context("failed to list keys")?;

println!("[ No | {:^66} | {:^42} ]", "Public key", "Ethereum address");

Expand All @@ -93,21 +87,19 @@ impl KeyCommand {
}
}
KeySubcommand::Recover { message, signature } => {
let message =
utils::hex_str_to_vec(message).with_context(|| "invalid `message`")?;
let signature =
utils::hex_str_to_vec(signature).with_context(|| "invalid `signature`")?;
let message = utils::hex_str_to_vec(message).context("invalid `message`")?;
let signature = utils::hex_str_to_vec(signature).context("invalid `signature`")?;

let signature_bytes: [u8; 65] = signature
.try_into()
.map_err(|_| anyhow!("signature isn't 65 bytes len"))
.with_context(|| "invalid `signature`")?;
.context("invalid `signature`")?;

let signature = unsafe { Signature::from_bytes(signature_bytes) };

let public = signature
.recover_from_digest(message.to_digest())
.with_context(|| "failed to recover signature from digest")?;
.context("failed to recover signature from digest")?;

println!("Signed by: {public}");
println!("Ethereum address: {}", public.to_address());
Expand All @@ -116,38 +108,37 @@ impl KeyCommand {
let key = key.strip_prefix("0x").unwrap_or(&key);

let public = if key.len() == 66 {
key.parse().with_context(|| "invalid `key`")?
key.parse().context("invalid `key`")?
} else if key.len() == 40 {
let mut address_bytes = [0u8; 20];
hex::decode_to_slice(key, &mut address_bytes)
.map_err(|e| anyhow!("Failed to parse eth address hex: {e}"))
.with_context(|| "invalid `key`")?;
.context("invalid `key`")?;

signer
.get_key_by_addr(address_bytes.into())?
.ok_or_else(|| anyhow!("Unrecognized eth address"))
.with_context(|| "invalid `key`")?
.context("invalid `key`")?
} else {
bail!("Invalid key length: should be 33 bytes public key or 20 bytes eth address ");
};

let private = signer
.get_private_key(public)
.with_context(|| "failed to get private key")?;
.context("failed to get private key")?;

println!("Secret key: {}", hex::encode(private.0));
println!("Public key: {public}");
println!("Ethereum address: {}", public.to_address());
}
KeySubcommand::Sign { key, message } => {
let public = key.parse().with_context(|| "invalid `key`")?;
let public = key.parse().context("invalid `key`")?;

let message =
utils::hex_str_to_vec(message).with_context(|| "invalid `message`")?;
let message = utils::hex_str_to_vec(message).context("invalid `message`")?;

let signature = signer
.sign(public, &message)
.with_context(|| "failed to sign message")?;
.context("failed to sign message")?;

println!("Signature: {signature}");
}
Expand Down
9 changes: 3 additions & 6 deletions ethexe/cli/src/commands/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,15 @@ impl RunCommand {
.filter_module("wasmtime_cranelift", LevelFilter::Off)
.filter_module("cranelift", LevelFilter::Off)
.try_init()
.with_context(|| "failed to initialize logger")?;
.context("failed to initialize logger")?;

let config = self
.params
.into_config()
.with_context(|| "invalid configuration")?;
let config = self.params.into_config().context("invalid configuration")?;

config.log_info();

let service = Service::new(&config)
.await
.with_context(|| "failed to create ethexe primary service")?;
.context("failed to create ethexe primary service")?;

tokio::select! {
res = service.run() => res,
Expand Down
32 changes: 15 additions & 17 deletions ethexe/cli/src/commands/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl TxCommand {
pub async fn exec(self) -> Result<()> {
let key_store = self.key_store.expect("must never be empty after merging");

let signer = Signer::new(key_store).with_context(|| "failed to create signer")?;
let signer = Signer::new(key_store).context("failed to create signer")?;

let rpc = self
.ethereum_rpc
Expand All @@ -88,17 +88,17 @@ impl TxCommand {
.ethereum_router
.ok_or_else(|| anyhow!("missing `ethereum-router`"))?
.parse()
.with_context(|| "invalid `ethereum-router`")?;
.context("invalid `ethereum-router`")?;

let sender = self
.sender
.ok_or_else(|| anyhow!("missing `sender`"))?
.parse()
.with_context(|| "invalid `sender`")?;
.context("invalid `sender`")?;

let ethereum = Ethereum::new(&rpc, router_addr, signer, sender)
.await
.with_context(|| "failed to create Ethereum client")?;
.context("failed to create Ethereum client")?;

let router = ethereum.router();
let router_query = router.query();
Expand All @@ -108,20 +108,20 @@ impl TxCommand {
let code_id = code_id
.parse()
.map_err(|e| anyhow!("{e:?}"))
.with_context(|| "invalid `code-id`")?;
.context("invalid `code-id`")?;

let salt = salt
.map(|s| s.parse())
.transpose()
.with_context(|| "invalid `salt`")?
.context("invalid `salt`")?
.unwrap_or_else(H256::random);

println!("Creating program on Ethereum from code id {code_id}");

let (tx, actor_id) = router
.create_program(code_id, salt)
.await
.with_context(|| "failed to create program")?;
.context("failed to create program")?;

println!("Completed in transaction {tx:?}");
println!(
Expand All @@ -137,15 +137,14 @@ impl TxCommand {
approve,
watch,
} => {
let mirror_addr: Address = mirror.parse().with_context(|| "invalid `mirror`")?;
let mirror_addr: Address = mirror.parse().context("invalid `mirror`")?;

let payload =
utils::hex_str_to_vec(payload).with_context(|| "invalid `payload`")?;
let payload = utils::hex_str_to_vec(payload).context("invalid `payload`")?;

let maybe_code_id = router_query
.program_code_id(mirror_addr.into())
.await
.with_context(|| "failed to check if mirror in known by router")?;
.context("failed to check if mirror in known by router")?;

ensure!(
maybe_code_id.is_some(),
Expand All @@ -160,7 +159,7 @@ impl TxCommand {
.wvara()
.approve(mirror_addr.0.into(), value)
.await
.with_context(|| "failed to approve wvara")?;
.context("failed to approve wvara")?;

println!("Completed in transaction {tx:?}");
}
Expand All @@ -172,7 +171,7 @@ impl TxCommand {
let (tx, message_id) = mirror
.send_message(payload, value)
.await
.with_context(|| "failed to send message to mirror")?;
.context("failed to send message to mirror")?;

println!("Completed in transaction {tx:?}");
println!("Message with id {message_id} successfully sent");
Expand All @@ -182,23 +181,22 @@ impl TxCommand {
}
}
TxSubcommand::Upload { path_to_wasm } => {
let code =
fs::read(&path_to_wasm).with_context(|| "failed to read wasm from file")?;
let code = fs::read(&path_to_wasm).context("failed to read wasm from file")?;

println!("Uploading {} to Ethereum", path_to_wasm.display(),);

let (tx, code_id) = router
.request_code_validation_with_sidecar(&code)
.await
.with_context(|| "failed to request code validation")?;
.context("failed to request code validation")?;

println!("Completed in transaction {tx:?}");
println!("Waiting for approval of code id {code_id}...");

let valid = router
.wait_code_validation(code_id)
.await
.with_context(|| "failed to wait for code validation")?;
.context("failed to wait for code validation")?;

if valid {
println!("Now you can create program from code id {code_id}!");
Expand Down
2 changes: 1 addition & 1 deletion ethexe/cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl Cli {
pub async fn run(self) -> Result<()> {
let params = self
.file_params()
.with_context(|| "failed to read params from file")?
.context("failed to read params from file")?
.unwrap_or_default();

self.command.run(params).await
Expand Down
2 changes: 1 addition & 1 deletion ethexe/cli/src/params/ethereum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl EthereumParams {
.ethereum_router
.ok_or_else(|| anyhow!("missing `ethereum-router`"))?
.parse()
.with_context(|| "invalid `ethereum-router`")?,
.context("invalid `ethereum-router`")?,
block_time: Duration::from_secs(Self::BLOCK_TIME as u64),
})
}
Expand Down
6 changes: 2 additions & 4 deletions ethexe/cli/src/params/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,8 @@ pub struct Params {
impl Params {
/// Load the parameters from a TOML file.
pub fn from_file(path: PathBuf) -> Result<Self> {
let content =
std::fs::read_to_string(path).with_context(|| "failed to read params file")?;
let params =
toml::from_str(&content).with_context(|| "failed to parse toml params file")?;
let content = std::fs::read_to_string(path).context("failed to read params file")?;
let params = toml::from_str(&content).context("failed to parse toml params file")?;

Ok(params)
}
Expand Down
2 changes: 1 addition & 1 deletion ethexe/cli/src/params/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl NetworkParams {
.network_key
.map(|k| k.parse())
.transpose()
.with_context(|| "invalid `network-key`")?;
.context("invalid `network-key`")?;

let external_addresses = self
.network_public_addr
Expand Down
6 changes: 2 additions & 4 deletions ethexe/cli/src/params/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,8 @@ impl NodeParams {
Ok(NodeConfig {
database_path: self.db_dir(),
key_path: self.keys_dir(),
sequencer: ConfigPublicKey::new(&self.sequencer)
.with_context(|| "invalid `sequencer` key")?,
validator: ConfigPublicKey::new(&self.validator)
.with_context(|| "invalid `validator` key")?,
sequencer: ConfigPublicKey::new(&self.sequencer).context("invalid `sequencer` key")?,
validator: ConfigPublicKey::new(&self.validator).context("invalid `validator` key")?,
max_commitment_depth: self.max_depth.unwrap_or(Self::DEFAULT_MAX_DEPTH).get(),
worker_threads_override: self.physical_threads.map(|v| v.get() as usize),
virtual_threads: self
Expand Down
13 changes: 9 additions & 4 deletions ethexe/network/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,9 @@ impl NetworkEventLoop {
}
};

let mut key = signer.get_private_key(key)?;
let mut key = signer
.get_private_key(key)
.context("failed to get a private key for the public one")?;
let key = identity::secp256k1::SecretKey::try_from_bytes(&mut key.0)
.expect("Signer provided invalid key; qed");
let pair = identity::secp256k1::Keypair::from(key);
Expand All @@ -281,7 +283,9 @@ impl NetworkEventLoop {
TransportType::QuicOrTcp => {
let tcp = libp2p::tcp::tokio::Transport::default()
.upgrade(upgrade::Version::V1Lazy)
.authenticate(libp2p::tls::Config::new(&keypair)?)
.authenticate(
libp2p::tls::Config::new(&keypair).context("failed tls config creation")?,
)
.multiplex(yamux::Config::default())
.timeout(Duration::from_secs(20));

Expand All @@ -302,7 +306,8 @@ impl NetworkEventLoop {
.boxed(),
};

let behaviour = Behaviour::new(&keypair, db)?;
let behaviour =
Behaviour::new(&keypair, db).context("failed ethexe network behaviour creation")?;
let local_peer_id = keypair.public().to_peer_id();
let config = SwarmConfig::with_tokio_executor();

Expand Down Expand Up @@ -482,7 +487,7 @@ impl NetworkEventLoop {
.gossipsub
.publish(gpu_commitments_topic(), data)
{
log::debug!("gossipsub publishing failed: {e}")
log::error!("gossipsub publishing failed: {e}")
}
}
NetworkSenderEvent::RequestDbData(request) => {
Expand Down
Loading
Loading