Skip to content

Commit

Permalink
Load genesis config from file
Browse files Browse the repository at this point in the history
  • Loading branch information
mertwole committed Aug 20, 2024
1 parent d3149e9 commit 3353f1b
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 26 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

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

1 change: 0 additions & 1 deletion relayer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ futures.workspace = true
gear-core.workspace = true
gclient.workspace = true
hex.workspace = true
hex-literal.workspace = true
keccak-hash.workspace = true
lazy_static.workspace = true
libc.workspace = true
Expand Down
12 changes: 4 additions & 8 deletions relayer/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
extern crate pretty_env_logger;

use clap::{Args, Parser, Subcommand};
use hex_literal::hex;
use pretty_env_logger::env_logger::fmt::TimestampPrecision;

use ethereum_client::EthApi;
use gear_rpc_client::GearApi;
use message_relayer::MessageRelayer;
use proof_storage::{FileSystemProofStorage, GearProofStorage, ProofStorage};
use prover::proving::GenesisConfig;
use relay_merkle_roots::MerkleRootRelayer;
use utils_prometheus::MetricsBuilder;

Expand All @@ -23,11 +21,6 @@ const DEFAULT_VARA_RPC: &str = "ws://localhost:8989";
const DEFAULT_ETH_RPC: &str = "http://localhost:8545";
const DEFAULT_PROMETHEUS_ENDPOINT: &str = "0.0.0.0:9090";

const GENESIS_CONFIG: GenesisConfig = GenesisConfig {
authority_set_id: 1,
authority_set_hash: hex!("b9853ab2fb585702dfd9040ee8bc9f94dc5b0abd8b0f809ec23fdc0265b21e24"),
};

#[derive(Parser)]
#[command(author, version, about, long_about = None)]
#[command(propagate_version = true)]
Expand Down Expand Up @@ -211,7 +204,10 @@ async fn main() {
Box::from(FileSystemProofStorage::new("./proof_storage".into()))
};

let relayer = MerkleRootRelayer::new(gear_api, eth_api, proof_storage).await;
let genesis_config = genesis_config::load_from_file();

let relayer =
MerkleRootRelayer::new(gear_api, eth_api, genesis_config, proof_storage).await;

metrics
.register_service(&relayer)
Expand Down
20 changes: 12 additions & 8 deletions relayer/src/prover_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ use std::{str::FromStr, time::Instant};

use utils_prometheus::MeteredService;

use super::GENESIS_CONFIG;
use gear_rpc_client::{dto, GearApi};
use num::BigUint;
use primitive_types::H256;
use prometheus::{core::Collector, HistogramOpts, HistogramVec};
use prover::proving::{
self, BlockFinality, BranchNodeData, PreCommit, ProofWithCircuitData, StorageInclusion,
self, BlockFinality, BranchNodeData, GenesisConfig, PreCommit, ProofWithCircuitData,
StorageInclusion,
};

pub struct Metrics;
Expand All @@ -27,15 +27,18 @@ lazy_static::lazy_static!(
).unwrap();
);

pub async fn prove_genesis(gear_api: &GearApi) -> anyhow::Result<ProofWithCircuitData> {
pub async fn prove_genesis(
gear_api: &GearApi,
genesis_config: GenesisConfig,
) -> anyhow::Result<ProofWithCircuitData> {
log::info!(
"Proving genesis authority set change {} -> {}",
GENESIS_CONFIG.authority_set_id,
GENESIS_CONFIG.authority_set_id + 1
genesis_config.authority_set_id,
genesis_config.authority_set_id + 1
);

let (block, current_epoch_block_finality) = gear_api
.fetch_finality_proof_for_session(GENESIS_CONFIG.authority_set_id)
.fetch_finality_proof_for_session(genesis_config.authority_set_id)
.await?;

let next_validator_set_inclusion_proof = gear_api
Expand All @@ -51,7 +54,7 @@ pub async fn prove_genesis(gear_api: &GearApi) -> anyhow::Result<ProofWithCircui

let proof = prover::proving::prove_genesis(
parse_rpc_block_finality_proof(current_epoch_block_finality),
GENESIS_CONFIG,
genesis_config,
next_validator_set_inclusion_proof,
next_validator_set_storage_data,
);
Expand Down Expand Up @@ -154,6 +157,7 @@ impl FinalProof {
pub async fn prove_final(
gear_api: &GearApi,
previous_proof: ProofWithCircuitData,
genesis_config: GenesisConfig,
at_block: H256,
) -> anyhow::Result<FinalProof> {
let (block, block_finality) = gear_api.fetch_finality_proof(at_block).await?;
Expand All @@ -168,7 +172,7 @@ pub async fn prove_final(
let proof = proving::prove_message_sent(
previous_proof,
parse_rpc_block_finality_proof(block_finality),
GENESIS_CONFIG,
genesis_config,
sent_message_inclusion_proof,
message_contents,
);
Expand Down
34 changes: 26 additions & 8 deletions relayer/src/relay_merkle_roots.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use prometheus::{Gauge, IntGauge};
use prover::proving::GenesisConfig;
use std::{
thread,
time::{Duration, Instant},
Expand All @@ -7,7 +8,6 @@ use std::{
use crate::{
proof_storage::ProofStorage,
prover_interface::{self, FinalProof},
GENESIS_CONFIG,
};

use ethereum_client::{EthApi, TxHash, TxStatus};
Expand All @@ -20,9 +20,12 @@ const MIN_MAIN_LOOP_DURATION: Duration = Duration::from_secs(5);
pub struct MerkleRootRelayer {
gear_api: GearApi,
eth_api: EthApi,

proof_storage: Box<dyn ProofStorage>,
eras: Eras,

genesis_config: GenesisConfig,

metrics: Metrics,
}

Expand Down Expand Up @@ -73,9 +76,10 @@ impl MerkleRootRelayer {
pub async fn new(
gear_api: GearApi,
eth_api: EthApi,
genesis_config: GenesisConfig,
proof_storage: Box<dyn ProofStorage>,
) -> MerkleRootRelayer {
let eras = Eras::new(None, gear_api.clone(), eth_api.clone())
let eras = Eras::new(None, gear_api.clone(), eth_api.clone(), genesis_config)
.await
.unwrap_or_else(|err| panic!("Error while creating era storage: {}", err));

Expand All @@ -84,6 +88,7 @@ impl MerkleRootRelayer {
MerkleRootRelayer {
gear_api,
eth_api,
genesis_config,
proof_storage,
eras,
metrics,
Expand Down Expand Up @@ -162,18 +167,18 @@ impl MerkleRootRelayer {

match latest_proven_authority_set_id {
None => {
if latest_authority_set_id <= GENESIS_CONFIG.authority_set_id {
if latest_authority_set_id <= self.genesis_config.authority_set_id {
log::warn!(
"Network haven't reached genesis authority set id yet. Current authority set id: {}, expected genesis: {}",
latest_authority_set_id,
GENESIS_CONFIG.authority_set_id,
self.genesis_config.authority_set_id,
);
return Ok(0);
}

let proof = prover_interface::prove_genesis(&self.gear_api).await?;
let proof = prover_interface::prove_genesis(&self.gear_api, self.genesis_config).await?;
self.proof_storage
.init(proof, GENESIS_CONFIG.authority_set_id)
.init(proof, self.genesis_config.authority_set_id)
.unwrap();

Ok(1)
Expand Down Expand Up @@ -211,7 +216,13 @@ impl MerkleRootRelayer {
.proof_storage
.get_proof_for_authority_set_id(authority_set_id)?;

prover_interface::prove_final(&self.gear_api, inner_proof, finalized_head).await
prover_interface::prove_final(
&self.gear_api,
inner_proof,
self.genesis_config,
finalized_head,
)
.await
}
}

Expand All @@ -222,6 +233,8 @@ struct Eras {
gear_api: GearApi,
eth_api: EthApi,

genesis_config: GenesisConfig,

metrics: EraMetrics,
}

Expand Down Expand Up @@ -266,6 +279,7 @@ impl Eras {
last_sealed: Option<u64>,
gear_api: GearApi,
eth_api: EthApi,
genesis_config: GenesisConfig,
) -> anyhow::Result<Self> {
let last_sealed = if let Some(l) = last_sealed {
l
Expand All @@ -285,6 +299,8 @@ impl Eras {
gear_api,
eth_api,

genesis_config,

metrics,
})
}
Expand Down Expand Up @@ -316,7 +332,9 @@ impl Eras {
.find_era_first_block(authority_set_id + 1)
.await?;
let inner_proof = proof_storage.get_proof_for_authority_set_id(authority_set_id)?;
let proof = prover_interface::prove_final(&self.gear_api, inner_proof, block).await?;
let proof =
prover_interface::prove_final(&self.gear_api, inner_proof, self.genesis_config, block)
.await?;

let block_number = self.gear_api.block_hash_to_number(block).await?;

Expand Down

0 comments on commit 3353f1b

Please sign in to comment.