mirror of
https://github.com/logos-blockchain/logos-execution-zone.git
synced 2026-05-17 13:39:39 +00:00
use anyhow for block settlement client error handling
This commit is contained in:
parent
e397d6c576
commit
56295d12d8
@ -1,6 +1,6 @@
|
|||||||
use std::{fs, path::Path};
|
use std::{fs, path::Path};
|
||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::{Result, anyhow};
|
||||||
use bedrock_client::BedrockClient;
|
use bedrock_client::BedrockClient;
|
||||||
use common::block::HashableBlockData;
|
use common::block::HashableBlockData;
|
||||||
use key_management_system_service::keys::{ED25519_SECRET_KEY_SIZE, Ed25519Key, Ed25519PublicKey};
|
use key_management_system_service::keys::{ED25519_SECRET_KEY_SIZE, Ed25519Key, Ed25519PublicKey};
|
||||||
@ -25,8 +25,7 @@ impl BlockSettlementClient {
|
|||||||
pub fn new(home: &Path, config: &BedrockConfig) -> Self {
|
pub fn new(home: &Path, config: &BedrockConfig) -> Self {
|
||||||
let bedrock_signing_key = load_or_create_signing_key(&home.join("bedrock_signing_key"))
|
let bedrock_signing_key = load_or_create_signing_key(&home.join("bedrock_signing_key"))
|
||||||
.expect("Signing key should load or be created successfully");
|
.expect("Signing key should load or be created successfully");
|
||||||
let bedrock_node_url =
|
let bedrock_node_url = config.node_url.clone();
|
||||||
Url::parse(&config.node_url).expect("Bedrock URL should be a valid URL");
|
|
||||||
let bedrock_channel_id = ChannelId::from(config.channel_id);
|
let bedrock_channel_id = ChannelId::from(config.channel_id);
|
||||||
let bedrock_client =
|
let bedrock_client =
|
||||||
BedrockClient::new(None).expect("Bedrock client should be able to initialize");
|
BedrockClient::new(None).expect("Bedrock client should be able to initialize");
|
||||||
@ -96,20 +95,17 @@ impl BlockSettlementClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Load signing key from file or generate a new one if it doesn't exist
|
/// Load signing key from file or generate a new one if it doesn't exist
|
||||||
fn load_or_create_signing_key(path: &Path) -> Result<Ed25519Key, ()> {
|
fn load_or_create_signing_key(path: &Path) -> Result<Ed25519Key> {
|
||||||
if path.exists() {
|
if path.exists() {
|
||||||
let key_bytes = fs::read(path).map_err(|_| ())?;
|
let key_bytes = fs::read(path)?;
|
||||||
if key_bytes.len() != ED25519_SECRET_KEY_SIZE {
|
let key_array: [u8; ED25519_SECRET_KEY_SIZE] = key_bytes
|
||||||
// TODO: proper error
|
.try_into()
|
||||||
return Err(());
|
.map_err(|_| anyhow!("Found key with incorrect length"))?;
|
||||||
}
|
|
||||||
let key_array: [u8; ED25519_SECRET_KEY_SIZE] =
|
|
||||||
key_bytes.try_into().expect("length already checked");
|
|
||||||
Ok(Ed25519Key::from_bytes(&key_array))
|
Ok(Ed25519Key::from_bytes(&key_array))
|
||||||
} else {
|
} else {
|
||||||
let mut key_bytes = [0u8; ED25519_SECRET_KEY_SIZE];
|
let mut key_bytes = [0u8; ED25519_SECRET_KEY_SIZE];
|
||||||
rand::RngCore::fill_bytes(&mut rand::thread_rng(), &mut key_bytes);
|
rand::RngCore::fill_bytes(&mut rand::thread_rng(), &mut key_bytes);
|
||||||
fs::write(path, key_bytes).map_err(|_| ())?;
|
fs::write(path, key_bytes)?;
|
||||||
Ok(Ed25519Key::from_bytes(&key_bytes))
|
Ok(Ed25519Key::from_bytes(&key_bytes))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -5,6 +5,7 @@ use std::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
|
use reqwest::Url;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||||
@ -56,7 +57,7 @@ pub struct BedrockConfig {
|
|||||||
/// Bedrock channel ID
|
/// Bedrock channel ID
|
||||||
pub channel_id: [u8; 32],
|
pub channel_id: [u8; 32],
|
||||||
/// Bedrock Url
|
/// Bedrock Url
|
||||||
pub node_url: String,
|
pub node_url: Url,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SequencerConfig {
|
impl SequencerConfig {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user