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: use network from db #841

Merged
merged 3 commits into from
May 10, 2024
Merged
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
11 changes: 7 additions & 4 deletions rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@ anyhow = "1.0.81"
bincode = "2.0.0-rc.3"
derive_more = "0.99"
serde = { version = "1.0.197", features = ["derive"] }
serde_json = { version = "1.0.114", features = ["raw_value", "unbounded_depth"] }
serde_json = { version = "1.0.114", features = [
"raw_value",
"unbounded_depth",
] }
smart-default = "0.7.1"
clap = { version = "4.5.2", features = ["derive"] }
clap = { version = "4.5.2", features = ["derive", "string"] }
thiserror = "1.0.58"
glob = "0.3.1"
mina_serialization_proc_macros = { path = "mina_serialization_macros/proc_macros" }
Expand Down Expand Up @@ -56,10 +59,10 @@ hex-literal = "0.4.1"
chrono = "0.4.35"
csv = "1.3.0"
notify = "6.1.1"

[dev-dependencies]
quickcheck = "1.0.3"
quickcheck_macros = "1.0.0"

[dev-dependencies]
pretty_assertions = "1.4.0"
wasm-bindgen-test = "0.3.42"
lazy_static = "1.4.0"
Expand Down
10 changes: 4 additions & 6 deletions rust/src/block/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use self::vrf_output::VrfOutput;
use crate::{
block::precomputed::PrecomputedBlock,
canonicity::Canonicity,
chain::Network,
protocol::serialization_types::{
common::{Base58EncodableVersionedType, HashV1},
version_bytes,
Expand All @@ -19,9 +20,6 @@ use anyhow::bail;
use serde::{Deserialize, Serialize};
use std::{ffi::OsStr, path::Path};

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Network(pub String);

#[derive(Hash, PartialEq, Eq, Clone, Serialize, Deserialize)]
pub struct Block {
pub parent_hash: BlockHash,
Expand Down Expand Up @@ -356,11 +354,11 @@ pub fn extract_state_hash(path: &Path) -> String {
state_hash.to_owned()
}

pub fn extract_network(path: &Path) -> String {
pub fn extract_network(path: &Path) -> Network {
let name = path.file_stem().and_then(|x| x.to_str()).unwrap();
let dash_pos = name.find('-').unwrap();
let state_hash = &name[..dash_pos];
state_hash.to_owned()
let network = &name[..dash_pos];
Network::from(network)
}

#[cfg(test)]
Expand Down
42 changes: 6 additions & 36 deletions rust/src/block/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,9 +307,12 @@ impl From<ParsedBlock> for PrecomputedBlock {

#[cfg(test)]
mod tests {
use crate::block::{
blockchain_length::BlockchainLength, get_blockchain_length, is_valid_block_file,
length_from_path, BlockHash,
use crate::{
block::{
blockchain_length::BlockchainLength, get_blockchain_length, is_valid_block_file,
length_from_path, BlockHash,
},
chain::Network,
};
use quickcheck::{Arbitrary, Gen};
use quickcheck_macros::quickcheck;
Expand Down Expand Up @@ -443,39 +446,6 @@ mod tests {
#[derive(Debug, Clone)]
struct BlockFileName(PathBuf);

#[derive(Debug, Clone)]
enum Network {
Mainnet,
Devnet,
Testworld,
Berkeley,
}

impl Arbitrary for Network {
fn arbitrary(g: &mut Gen) -> Self {
let idx = usize::arbitrary(g) % 4;
match idx {
0 => Network::Mainnet,
1 => Network::Devnet,
2 => Network::Testworld,
3 => Network::Berkeley,
_ => panic!("should never happen"),
}
}
}

impl std::fmt::Display for Network {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let network = match self {
Network::Mainnet => "mainnet",
Network::Devnet => "devnet",
Network::Testworld => "testworld",
Network::Berkeley => "berkeley",
};
write!(f, "{}", network)
}
}

impl Arbitrary for BlockHash {
fn arbitrary(g: &mut Gen) -> Self {
let mut hash = "3N".to_string();
Expand Down
18 changes: 9 additions & 9 deletions rust/src/block/precomputed.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
//! Indexer internal precomputed block representation

use super::Network;
use crate::{
block::{
extract_block_height, extract_network, extract_state_hash, Block, BlockHash, VrfOutput,
},
canonicity::Canonicity,
chain::Network,
command::{signed::SignedCommand, UserCommandWithStatus, UserCommandWithStatusT},
constants::{berkeley::*, *},
ledger::{coinbase::Coinbase, public_key::PublicKey, LedgerHash},
Expand Down Expand Up @@ -71,7 +71,7 @@ pub enum PrecomputedBlock {

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct PrecomputedBlockV1 {
pub network: String,
pub network: Network,
pub state_hash: BlockHash,
pub scheduled_time: u64,
pub blockchain_length: u32,
Expand All @@ -81,7 +81,7 @@ pub struct PrecomputedBlockV1 {

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct PrecomputedBlockV2 {
pub network: String,
pub network: Network,
pub state_hash: BlockHash,
pub scheduled_time: u64,
pub blockchain_length: u32,
Expand All @@ -99,7 +99,7 @@ pub enum PrecomputedBlockWithCanonicity {
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct PrecomputedBlockWithCanonicityV1 {
pub canonicity: Option<Canonicity>,
pub network: String,
pub network: Network,
pub state_hash: BlockHash,
pub scheduled_time: u64,
pub blockchain_length: u32,
Expand All @@ -110,7 +110,7 @@ pub struct PrecomputedBlockWithCanonicityV1 {
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct PrecomputedBlockWithCanonicityV2 {
pub canonicity: Option<Canonicity>,
pub network: String,
pub network: Network,
pub state_hash: BlockHash,
pub scheduled_time: u64,
pub blockchain_length: u32,
Expand All @@ -137,7 +137,7 @@ impl PrecomputedBlock {
state_hash,
scheduled_time,
blockchain_length,
network: block_file_contents.network.0,
network: block_file_contents.network,
protocol_state: protocol_state.to_owned().into(),
staged_ledger_diff: staged_ledger_diff.to_owned().into(),
})))
Expand All @@ -152,7 +152,7 @@ impl PrecomputedBlock {
state_hash,
scheduled_time,
blockchain_length,
network: block_file_contents.network.0,
network: block_file_contents.network,
protocol_state: protocol_state.to_owned(),
staged_ledger_diff: staged_ledger_diff.to_owned(),
}))
Expand All @@ -170,7 +170,7 @@ impl PrecomputedBlock {
BlockFileContents {
contents,
blockchain_length,
network: Network(network),
network,
state_hash: state_hash.into(),
},
version,
Expand Down Expand Up @@ -585,7 +585,7 @@ impl PrecomputedBlock {
}
}

pub fn network(&self) -> String {
pub fn network(&self) -> Network {
match self {
PrecomputedBlock::V1(v1) => v1.network.to_owned(),
PrecomputedBlock::V2(v2) => v2.network.to_owned(),
Expand Down
146 changes: 146 additions & 0 deletions rust/src/chain/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
pub mod store;

use crate::constants::*;
use bincode::{Decode, Encode};
use clap::builder::OsStr;
use hex::ToHex;
use quickcheck::{Arbitrary, Gen};
use serde::{Deserialize, Serialize};
use std::fmt::{Debug, Display, Formatter, Result};

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct ChainId(pub String);

impl ChainId {
pub fn new(chain_id: &str) -> Self {
Self(chain_id.to_string())
}
}

/// Chain id used by mina node p2p network
pub fn chain_id(
genesis_state_hash: &str,
genesis_constants: &[u32],
constraint_system_digests: &[&str],
) -> ChainId {
use blake2::{digest::VariableOutput, Blake2bVar};
use std::io::Write;

let genesis_constants_hash: String = {
let mut gcs = genesis_constants
.iter()
.map(u32::to_string)
.collect::<Vec<String>>();
gcs.push(
from_timestamp_millis(MAINNET_GENESIS_TIMESTAMP as i64)
.format("%Y-%m-%d %H:%M:%S%.6fZ")
.to_string(),
);

let mut hasher = Blake2bVar::new(32).unwrap();
hasher.write_all(gcs.concat().as_bytes()).unwrap();
hasher.finalize_boxed().encode_hex()
};
let all_snark_keys = constraint_system_digests.concat();
let digest_str = [genesis_state_hash, &all_snark_keys, &genesis_constants_hash].concat();

let mut hasher = Blake2bVar::new(32).unwrap();
hasher.write_all(digest_str.as_bytes()).unwrap();
ChainId(hasher.finalize_boxed().to_vec().encode_hex())
}

#[derive(Clone, PartialEq, Eq, Encode, Decode, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Network {
Mainnet,
Devnet,
Testworld,
Berkeley,
}

impl Arbitrary for Network {
fn arbitrary(g: &mut Gen) -> Self {
let idx = usize::arbitrary(g) % 4;
match idx {
0 => Network::Mainnet,
1 => Network::Devnet,
2 => Network::Testworld,
3 => Network::Berkeley,
_ => panic!("unknown network {idx}"),
}
}
}

impl Network {
const MAINNET: &'static str = "mainnet";
const DEVNET: &'static str = "devnet";
const TESTWORLD: &'static str = "testworld";
const BERKELEY: &'static str = "berkeley";

fn format(&self, f: &mut Formatter<'_>) -> Result {
write!(
f,
"{}",
match self {
Network::Mainnet => Network::MAINNET,
Network::Devnet => Network::DEVNET,
Network::Testworld => Network::TESTWORLD,
Network::Berkeley => Network::BERKELEY,
}
)
}
}

impl Display for Network {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
self.format(f)
}
}

impl Debug for Network {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
self.format(f)
}
}

impl From<Vec<u8>> for Network {
fn from(value: Vec<u8>) -> Self {
Network::from(String::from_utf8(value).unwrap().as_str())
}
}

impl From<&str> for Network {
fn from(value: &str) -> Self {
match value {
Network::MAINNET => Network::Mainnet,
Network::DEVNET => Network::Devnet,
Network::TESTWORLD => Network::Testworld,
Network::BERKELEY => Network::Berkeley,
_ => panic!("{value} is not a valid network"),
}
}
}

impl From<Network> for OsStr {
fn from(value: Network) -> Self {
value.to_string().into()
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn chain_id_test() {
assert_eq!(
"5f704cc0c82e0ed70e873f0893d7e06f148524e3f0bdae2afb02e7819a0c24d1",
chain_id(
MAINNET_GENESIS_HASH,
MAINNET_GENESIS_CONSTANTS,
MAINNET_CONSTRAINT_SYSTEM_DIGESTS
)
.0
)
}
}
3 changes: 1 addition & 2 deletions rust/src/chain_id/store.rs → rust/src/chain/store.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use super::ChainId;
use crate::block::Network;
use super::{ChainId, Network};

pub trait ChainIdStore {
/// Persists a (chain id, network) pair
Expand Down
Loading