Skip to content

Commit

Permalink
refactor: apply review suggestion
Browse files Browse the repository at this point in the history
  • Loading branch information
jjyr committed Jan 15, 2023
1 parent f1025bf commit c10322b
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 30 deletions.
6 changes: 3 additions & 3 deletions crates/block-producer/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ impl BaseInitComponents {
let rollup_context = RollupContext {
rollup_config: rollup_config.clone(),
rollup_script_hash: consensus.genesis.rollup_type_hash.clone().into(),
fork_config: consensus.clone().into_owned(),
fork_config: consensus.clone(),
};
let rollup_type_script: Script = consensus.chain.rollup_type_script.clone().into();
let rpc_client = {
Expand Down Expand Up @@ -319,8 +319,8 @@ impl BaseInitComponents {
if NodeMode::ReadOnly != config.node_mode {
let block_producer_config =
opt_block_producer_config.ok_or_else(|| anyhow!("not set block producer"))?;
check_rollup_config_cell(consensus.as_ref(), &rollup_config, &rpc_client).await?;
check_locks(block_producer_config, consensus.as_ref(), &rollup_config)?;
check_rollup_config_cell(consensus, &rollup_config, &rpc_client).await?;
check_locks(block_producer_config, consensus, &rollup_config)?;
}
}

Expand Down
18 changes: 8 additions & 10 deletions crates/config/src/consensus/builtins.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
use crate::ForkConfig;
use lazy_static::lazy_static;

pub fn testnet() -> ForkConfig {
pub fn testnet() -> &'static ForkConfig {
lazy_static! {
pub static ref CONFIG: ForkConfig = {
let content = include_str!("builtins/testnet.toml");
toml::from_str(content).expect("builtin testnet config")
};
}
CONFIG.clone()
&CONFIG
}

pub fn mainnet() -> ForkConfig {
pub fn mainnet() -> &'static ForkConfig {
lazy_static! {
pub static ref CONFIG: ForkConfig = {
let content = include_str!("builtins/mainnet.toml");
toml::from_str(content).expect("builtin mainnet config")
};
}
CONFIG.clone()
&CONFIG
}

#[cfg(not(feature = "no-builtin"))]
Expand All @@ -30,15 +30,14 @@ mod tests {
#[test]
fn test_builtin_testnet_config() {
let config = super::testnet();
for f in config.backend_forks {
for b in f.backends {
for f in &config.backend_forks {
for b in &f.backends {
let checksum: H256 = content_checksum(
b.generator
.get()
.unwrap_or_else(|_| panic!("can't find: {}", b.generator))
.as_ref(),
)
.unwrap()
.into();
if checksum != b.generator_checksum {
panic!(
Expand All @@ -53,15 +52,14 @@ mod tests {
#[test]
fn test_builtin_mainnet_config() {
let config = super::mainnet();
for f in config.backend_forks {
for b in f.backends {
for f in &config.backend_forks {
for b in &f.backends {
let checksum: H256 = content_checksum(
b.generator
.get()
.unwrap_or_else(|_| panic!("can't find: {}", b.generator))
.as_ref(),
)
.unwrap()
.into();
if checksum != b.generator_checksum {
panic!(
Expand Down
10 changes: 4 additions & 6 deletions crates/config/src/consensus/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::borrow::Cow;

use serde::{Deserialize, Serialize};

use crate::ForkConfig;
Expand Down Expand Up @@ -30,13 +28,13 @@ pub enum Consensus {
}

impl Consensus {
pub fn get_config(&self) -> Cow<ForkConfig> {
pub fn get_config(&self) -> &ForkConfig {
match self {
Consensus::Builtin { builtin } => match builtin {
BuiltinConsensus::Mainnet => Cow::Owned(builtins::mainnet()),
BuiltinConsensus::Testnet => Cow::Owned(builtins::testnet()),
BuiltinConsensus::Mainnet => builtins::mainnet(),
BuiltinConsensus::Testnet => builtins::testnet(),
},
Consensus::Config { config } => Cow::Borrowed(config),
Consensus::Config { config } => config,
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions crates/generator/src/backend_manage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl Backend {
generator: Bytes,
generator_checksum: H256,
) -> Result<Self> {
let checksum: H256 = content_checksum(&generator)?;
let checksum: H256 = content_checksum(&generator);

if generator_checksum != checksum {
bail!(
Expand Down Expand Up @@ -258,15 +258,15 @@ mod tests {
generator: Resource::file_system(
format!("{}/sudt_v0", dir.to_string_lossy()).into(),
),
generator_checksum: content_checksum(b"sudt_v0").unwrap().into(),
generator_checksum: content_checksum(b"sudt_v0").into(),
},
BackendConfig {
validator_script_type_hash: [43u8; 32].into(),
backend_type: BackendType::EthAddrReg,
generator: Resource::file_system(
format!("{}/addr_v0", dir.to_string_lossy()).into(),
),
generator_checksum: content_checksum(b"addr_v0").unwrap().into(),
generator_checksum: content_checksum(b"addr_v0").into(),
},
],
};
Expand Down Expand Up @@ -319,15 +319,15 @@ mod tests {
generator: Resource::file_system(
format!("{}/meta_v0", dir.to_string_lossy()).into(),
),
generator_checksum: content_checksum(b"meta_v0").unwrap().into(),
generator_checksum: content_checksum(b"meta_v0").into(),
},
BackendConfig {
validator_script_type_hash: [42u8; 32].into(),
backend_type: BackendType::Sudt,
generator: Resource::file_system(
format!("{}/sudt_v1", dir.to_string_lossy()).into(),
),
generator_checksum: content_checksum(b"sudt_v1").unwrap().into(),
generator_checksum: content_checksum(b"sudt_v1").into(),
},
],
};
Expand Down
2 changes: 1 addition & 1 deletion crates/replay-chain/src/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub async fn setup(args: SetupArgs) -> Result<Context> {
let rollup_context = RollupContext {
rollup_config: rollup_config.clone(),
rollup_script_hash: consensus.genesis.rollup_type_hash.clone().into(),
fork_config: consensus.clone().into_owned(),
fork_config: consensus.clone(),
};
let secp_data: Bytes = {
let rpc_client = {
Expand Down
8 changes: 3 additions & 5 deletions crates/utils/src/checksum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@ use gw_types::h256::H256;
use sha2::{Digest, Sha256};
use std::path::Path;

pub fn content_checksum(content: &[u8]) -> Result<H256> {
let mut hasher = Sha256::new();
hasher.update(&content);
Ok(hasher.finalize().into())
pub fn content_checksum(content: &[u8]) -> H256 {
Sha256::digest(content).into()
}

pub fn file_checksum<P: AsRef<Path>>(path: P) -> Result<H256> {
let content = std::fs::read(path)?;
content_checksum(&content)
Ok(content_checksum(&content))
}

0 comments on commit c10322b

Please sign in to comment.