From a80d198e88f79c04e0ab168b42e457cec93db6a3 Mon Sep 17 00:00:00 2001 From: Oleksandr Pravdyvyi Date: Tue, 3 Dec 2024 09:32:35 +0200 Subject: [PATCH] feat: node core strucutres added --- Cargo.lock | 291 ++++++++++++++++++++++-- accounts/src/key_management/mod.rs | 2 +- node_core/Cargo.toml | 5 + node_core/src/config.rs | 11 + node_core/src/lib.rs | 14 ++ node_core/src/sequencer_client/json.rs | 1 + node_core/src/sequencer_client/mod.rs | 8 + node_core/src/storage/accounts_store.rs | 28 +++ node_core/src/storage/block_store.rs | 33 +++ node_core/src/storage/mod.rs | 71 ++++++ 10 files changed, 448 insertions(+), 16 deletions(-) create mode 100644 node_core/src/config.rs create mode 100644 node_core/src/sequencer_client/json.rs create mode 100644 node_core/src/sequencer_client/mod.rs create mode 100644 node_core/src/storage/accounts_store.rs create mode 100644 node_core/src/storage/block_store.rs create mode 100644 node_core/src/storage/mod.rs diff --git a/Cargo.lock b/Cargo.lock index c18aa0b..c6bb207 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -89,7 +89,7 @@ dependencies = [ "actix-service", "actix-utils", "ahash 0.8.11", - "base64", + "base64 0.22.1", "bitflags 2.6.0", "bytes", "bytestring", @@ -607,6 +607,12 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4c7f02d4ea65f2c1853089ffd8d2787bdbc63de2f0d29dedbcf8ccdfa0ccd4cf" +[[package]] +name = "base64" +version = "0.21.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9d297deb1925b89f2ccc13d7635fa0714f12c87adce1c75356b39ca9b7178567" + [[package]] name = "base64" version = "0.22.1" @@ -756,7 +762,7 @@ source = "git+https://github.com/risc0/risc0.git?branch=release-1.1#20df1afbcdd2 dependencies = [ "duplicate", "maybe-async", - "reqwest", + "reqwest 0.12.9", "serde", "thiserror", ] @@ -1358,7 +1364,7 @@ dependencies = [ "digest 0.10.7", "futures", "rand 0.8.5", - "reqwest", + "reqwest 0.12.9", "thiserror", "tokio", ] @@ -1538,6 +1544,15 @@ version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" +[[package]] +name = "foreign-types" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6f339eb8adc052cd2ca78910fda869aefa38d22d5cb648e6485e4d3fc06f3b1" +dependencies = [ + "foreign-types-shared 0.1.1", +] + [[package]] name = "foreign-types" version = "0.5.0" @@ -1545,7 +1560,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d737d9aa519fb7b749cbc3b962edcf310a8dd1f4b67c91c4f83975dbdd17d965" dependencies = [ "foreign-types-macros", - "foreign-types-shared", + "foreign-types-shared 0.3.1", ] [[package]] @@ -1559,6 +1574,12 @@ dependencies = [ "syn 2.0.87", ] +[[package]] +name = "foreign-types-shared" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b" + [[package]] name = "foreign-types-shared" version = "0.3.1" @@ -1895,6 +1916,17 @@ dependencies = [ "itoa", ] +[[package]] +name = "http-body" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ceab25649e9960c0311ea418d17bee82c0dcec1bd053b5f9a66e265a693bed2" +dependencies = [ + "bytes", + "http 0.2.12", + "pin-project-lite", +] + [[package]] name = "http-body" version = "1.0.1" @@ -1914,7 +1946,7 @@ dependencies = [ "bytes", "futures-util", "http 1.1.0", - "http-body", + "http-body 1.0.1", "pin-project-lite", ] @@ -1936,6 +1968,30 @@ version = "2.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" +[[package]] +name = "hyper" +version = "0.14.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8c08302e8fa335b151b788c775ff56e7a03ae64ff85c548ee820fecb70356e85" +dependencies = [ + "bytes", + "futures-channel", + "futures-core", + "futures-util", + "h2", + "http 0.2.12", + "http-body 0.4.6", + "httparse", + "httpdate", + "itoa", + "pin-project-lite", + "socket2 0.5.7", + "tokio", + "tower-service", + "tracing", + "want", +] + [[package]] name = "hyper" version = "1.5.0" @@ -1946,7 +2002,7 @@ dependencies = [ "futures-channel", "futures-util", "http 1.1.0", - "http-body", + "http-body 1.0.1", "httparse", "itoa", "pin-project-lite", @@ -1963,7 +2019,7 @@ checksum = "08afdbb5c31130e3034af566421053ab03787c640246a446327f550d11bcb333" dependencies = [ "futures-util", "http 1.1.0", - "hyper", + "hyper 1.5.0", "hyper-util", "rustls", "rustls-pki-types", @@ -1973,6 +2029,19 @@ dependencies = [ "webpki-roots", ] +[[package]] +name = "hyper-tls" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d6183ddfa99b85da61a140bea0efc93fdf56ceaa041b37d553518030827f9905" +dependencies = [ + "bytes", + "hyper 0.14.31", + "native-tls", + "tokio", + "tokio-native-tls", +] + [[package]] name = "hyper-util" version = "0.1.10" @@ -1983,8 +2052,8 @@ dependencies = [ "futures-channel", "futures-util", "http 1.1.0", - "http-body", - "hyper", + "http-body 1.0.1", + "hyper 1.5.0", "pin-project-lite", "socket2 0.5.7", "tokio", @@ -2471,7 +2540,7 @@ dependencies = [ "bitflags 2.6.0", "block", "core-graphics-types", - "foreign-types", + "foreign-types 0.5.0", "log", "objc", "paste 1.0.15", @@ -2536,6 +2605,23 @@ dependencies = [ "sha3", ] +[[package]] +name = "native-tls" +version = "0.2.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a8614eb2c83d59d1c8cc974dd3f920198647674a0a035e1af1fa58707e317466" +dependencies = [ + "libc", + "log", + "openssl", + "openssl-probe", + "openssl-sys", + "schannel", + "security-framework", + "security-framework-sys", + "tempfile", +] + [[package]] name = "ndarray" version = "0.16.1" @@ -2567,13 +2653,16 @@ dependencies = [ name = "node_core" version = "0.1.0" dependencies = [ + "accounts", "anyhow", "bincode", + "elliptic-curve", "env_logger", "k256", "log", "monotree", "rand 0.8.5", + "reqwest 0.11.27", "secp256k1-zkp", "serde", "serde_json", @@ -2815,6 +2904,50 @@ version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c08d65885ee38876c4f86fa503fb49d7b507c2b62552df7c70b2fce627e06381" +[[package]] +name = "openssl" +version = "0.10.68" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6174bc48f102d208783c2c84bf931bb75927a617866870de8a4ea85597f871f5" +dependencies = [ + "bitflags 2.6.0", + "cfg-if 1.0.0", + "foreign-types 0.3.2", + "libc", + "once_cell", + "openssl-macros", + "openssl-sys", +] + +[[package]] +name = "openssl-macros" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.87", +] + +[[package]] +name = "openssl-probe" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf" + +[[package]] +name = "openssl-sys" +version = "0.9.104" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "45abf306cbf99debc8195b66b7346498d7b10c210de50418b5ccd7ceba08c741" +dependencies = [ + "cc", + "libc", + "pkg-config", + "vcpkg", +] + [[package]] name = "option-ext" version = "0.2.0" @@ -3268,21 +3401,61 @@ version = "0.8.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" +[[package]] +name = "reqwest" +version = "0.11.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd67538700a17451e7cba03ac727fb961abb7607553461627b97de0b89cf4a62" +dependencies = [ + "base64 0.21.7", + "bytes", + "encoding_rs", + "futures-core", + "futures-util", + "h2", + "http 0.2.12", + "http-body 0.4.6", + "hyper 0.14.31", + "hyper-tls", + "ipnet", + "js-sys", + "log", + "mime", + "native-tls", + "once_cell", + "percent-encoding", + "pin-project-lite", + "rustls-pemfile 1.0.4", + "serde", + "serde_json", + "serde_urlencoded", + "sync_wrapper 0.1.2", + "system-configuration", + "tokio", + "tokio-native-tls", + "tower-service", + "url", + "wasm-bindgen", + "wasm-bindgen-futures", + "web-sys", + "winreg", +] + [[package]] name = "reqwest" version = "0.12.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a77c62af46e79de0a562e1a9849205ffcb7fc1238876e9bd743357570e04046f" dependencies = [ - "base64", + "base64 0.22.1", "bytes", "futures-channel", "futures-core", "futures-util", "http 1.1.0", - "http-body", + "http-body 1.0.1", "http-body-util", - "hyper", + "hyper 1.5.0", "hyper-rustls", "hyper-util", "ipnet", @@ -3294,12 +3467,12 @@ dependencies = [ "pin-project-lite", "quinn", "rustls", - "rustls-pemfile", + "rustls-pemfile 2.2.0", "rustls-pki-types", "serde", "serde_json", "serde_urlencoded", - "sync_wrapper", + "sync_wrapper 1.0.1", "tokio", "tokio-rustls", "tokio-util", @@ -3681,6 +3854,15 @@ dependencies = [ "zeroize", ] +[[package]] +name = "rustls-pemfile" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1c74cae0a4cf6ccbbf5f359f08efdf8ee7e1dc532573bf0db71968cb56b1448c" +dependencies = [ + "base64 0.21.7", +] + [[package]] name = "rustls-pemfile" version = "2.2.0" @@ -3724,6 +3906,15 @@ version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" +[[package]] +name = "schannel" +version = "0.1.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f29ebaa345f945cec9fbbc532eb307f0fdad8161f281b6369539c8d84876b3d" +dependencies = [ + "windows-sys 0.59.0", +] + [[package]] name = "scopeguard" version = "1.2.0" @@ -3787,6 +3978,29 @@ dependencies = [ "secp256k1-sys", ] +[[package]] +name = "security-framework" +version = "2.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "897b2245f0b511c87893af39b033e5ca9cce68824c4d7e7630b5a1d339658d02" +dependencies = [ + "bitflags 2.6.0", + "core-foundation", + "core-foundation-sys", + "libc", + "security-framework-sys", +] + +[[package]] +name = "security-framework-sys" +version = "2.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa39c7303dc58b5543c94d22c1766b0d31f2ee58306363ea622b10bbc075eaa2" +dependencies = [ + "core-foundation-sys", + "libc", +] + [[package]] name = "semver" version = "1.0.23" @@ -4128,6 +4342,12 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "sync_wrapper" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2047c6ded9c721764247e62cd3b03c09ffc529b2ba5b10ec482ae507a4a70160" + [[package]] name = "sync_wrapper" version = "1.0.1" @@ -4148,6 +4368,27 @@ dependencies = [ "syn 2.0.87", ] +[[package]] +name = "system-configuration" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba3a3adc5c275d719af8cb4272ea1c4a6d668a777f37e115f6d11ddbc1c8e0e7" +dependencies = [ + "bitflags 1.3.2", + "core-foundation", + "system-configuration-sys", +] + +[[package]] +name = "system-configuration-sys" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a75fb188eb626b924683e3b95e3a48e63551fcfb51949de2f06a9d91dbee93c9" +dependencies = [ + "core-foundation-sys", + "libc", +] + [[package]] name = "tap" version = "1.0.1" @@ -4282,6 +4523,16 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "tokio-native-tls" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbae76ab933c85776efabc971569dd6119c580d8f5d448769dec1764bf796ef2" +dependencies = [ + "native-tls", + "tokio", +] + [[package]] name = "tokio-rustls" version = "0.26.0" @@ -4922,6 +5173,16 @@ dependencies = [ "memchr", ] +[[package]] +name = "winreg" +version = "0.50.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "524e57b2c537c0f9b1e69f1965311ec12182b4122e45035b1508cd24d2adadb1" +dependencies = [ + "cfg-if 1.0.0", + "windows-sys 0.48.0", +] + [[package]] name = "winsafe" version = "0.0.19" diff --git a/accounts/src/key_management/mod.rs b/accounts/src/key_management/mod.rs index 7305937..817906f 100644 --- a/accounts/src/key_management/mod.rs +++ b/accounts/src/key_management/mod.rs @@ -12,7 +12,7 @@ pub mod constants_types; pub mod ephemeral_key_holder; pub mod secret_holders; -#[derive(Debug)] +#[derive(Clone)] ///Entrypoint to key management pub struct AddressKeyHolder { //Will be useful in future diff --git a/node_core/Cargo.toml b/node_core/Cargo.toml index e80981f..4ac9e9c 100644 --- a/node_core/Cargo.toml +++ b/node_core/Cargo.toml @@ -14,6 +14,11 @@ k256.workspace = true sha2.workspace = true monotree.workspace = true bincode.workspace = true +elliptic-curve.workspace = true +reqwest.workspace = true + +[dependencies.accounts] +path = "../accounts" [dependencies.storage] path = "../storage" diff --git a/node_core/src/config.rs b/node_core/src/config.rs new file mode 100644 index 0000000..f6cb680 --- /dev/null +++ b/node_core/src/config.rs @@ -0,0 +1,11 @@ +use std::path::PathBuf; + +use serde::{Deserialize, Serialize}; + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct NodeConfig { + ///Home dir of sequencer storage + pub home: PathBuf, + ///Override rust log (env var logging level) + pub override_rust_log: Option, +} diff --git a/node_core/src/lib.rs b/node_core/src/lib.rs index ed9a835..a59c5f9 100644 --- a/node_core/src/lib.rs +++ b/node_core/src/lib.rs @@ -1 +1,15 @@ +use accounts::account_core::AccountAddress; +use config::NodeConfig; +use storage::NodeChainStore; + +pub mod config; pub mod executions; +pub mod sequencer_client; +pub mod storage; + +pub struct NodeCore { + pub storage: NodeChainStore, + pub curr_height: u64, + pub main_acc_addr: AccountAddress, + pub node_config: NodeConfig, +} diff --git a/node_core/src/sequencer_client/json.rs b/node_core/src/sequencer_client/json.rs new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/node_core/src/sequencer_client/json.rs @@ -0,0 +1 @@ + diff --git a/node_core/src/sequencer_client/mod.rs b/node_core/src/sequencer_client/mod.rs new file mode 100644 index 0000000..f265ced --- /dev/null +++ b/node_core/src/sequencer_client/mod.rs @@ -0,0 +1,8 @@ +use crate::config::NodeConfig; + +pub mod json; + +pub struct SequencerClient { + pub client: reqwest::Client, + pub config: NodeConfig, +} diff --git a/node_core/src/storage/accounts_store.rs b/node_core/src/storage/accounts_store.rs new file mode 100644 index 0000000..bcf1bc4 --- /dev/null +++ b/node_core/src/storage/accounts_store.rs @@ -0,0 +1,28 @@ +use accounts::account_core::{Account, AccountAddress}; +use std::collections::HashMap; + +pub struct NodeAccountsStore { + pub accounts: HashMap, +} + +impl NodeAccountsStore { + pub fn new() -> Self { + Self { + accounts: HashMap::new(), + } + } + + pub fn register_account(&mut self, account: Account) { + self.accounts.insert(account.address, account); + } + + pub fn unregister_account(&mut self, account_addr: AccountAddress) { + self.accounts.remove(&account_addr); + } +} + +impl Default for NodeAccountsStore { + fn default() -> Self { + Self::new() + } +} diff --git a/node_core/src/storage/block_store.rs b/node_core/src/storage/block_store.rs new file mode 100644 index 0000000..2766f45 --- /dev/null +++ b/node_core/src/storage/block_store.rs @@ -0,0 +1,33 @@ +use std::path::Path; + +use anyhow::Result; +use storage::{block::Block, RocksDBIO}; + +pub struct NodeBlockStore { + dbio: RocksDBIO, +} + +impl NodeBlockStore { + ///Starting database at the start of new chain. + /// Creates files if necessary. + /// + /// ATTENTION: Will overwrite genesis block. + pub fn open_db_with_genesis(location: &Path, genesis_block: Option) -> Result { + Ok(Self { + dbio: RocksDBIO::new(location, genesis_block)?, + }) + } + + ///Reopening existing database + pub fn open_db_restart(location: &Path) -> Result { + NodeBlockStore::open_db_with_genesis(location, None) + } + + pub fn get_block_at_id(&self, id: u64) -> Result { + Ok(self.dbio.get_block(id)?) + } + + pub fn put_block_at_id(&self, block: Block) -> Result<()> { + Ok(self.dbio.put_block(block)?) + } +} diff --git a/node_core/src/storage/mod.rs b/node_core/src/storage/mod.rs new file mode 100644 index 0000000..0f48c35 --- /dev/null +++ b/node_core/src/storage/mod.rs @@ -0,0 +1,71 @@ +use std::path::Path; + +use accounts::account_core::{Account, AccountAddress}; +use accounts_store::NodeAccountsStore; +use block_store::NodeBlockStore; +use rand::{rngs::OsRng, RngCore}; +use storage::{ + block::{Block, HashableBlockData}, + merkle_tree_public::merkle_tree::{PublicTransactionMerkleTree, UTXOCommitmentsMerkleTree}, + nullifier_sparse_merkle_tree::NullifierSparseMerkleTree, +}; + +pub mod accounts_store; +pub mod block_store; + +pub struct NodeChainStore { + pub acc_store: NodeAccountsStore, + pub block_store: NodeBlockStore, + pub nullifier_store: NullifierSparseMerkleTree, + pub utxo_commitments_store: UTXOCommitmentsMerkleTree, + pub pub_tx_store: PublicTransactionMerkleTree, + ///For simplicity, we will allow only one account per node. + /// ToDo: Change it in future + node_main_account_info: Account, +} + +impl NodeChainStore { + pub fn new_with_genesis(home_dir: &Path, genesis_id: u64, is_genesis_random: bool) -> Self { + let acc_store = NodeAccountsStore::default(); + let nullifier_store = NullifierSparseMerkleTree::default(); + let utxo_commitments_store = UTXOCommitmentsMerkleTree::new(vec![]); + let pub_tx_store = PublicTransactionMerkleTree::new(vec![]); + + let mut data = [0; 32]; + let mut prev_block_hash = [0; 32]; + + if is_genesis_random { + OsRng.fill_bytes(&mut data); + OsRng.fill_bytes(&mut prev_block_hash); + } + + let hashable_data = HashableBlockData { + block_id: genesis_id, + prev_block_id: genesis_id.saturating_sub(1), + transactions: vec![], + data: data.to_vec(), + prev_block_hash, + }; + + let genesis_block = Block::produce_block_from_hashable_data(hashable_data); + + //Sequencer should panic if unable to open db, + //as fixing this issue may require actions non-native to program scope + let block_store = + NodeBlockStore::open_db_with_genesis(&home_dir.join("rocksdb"), Some(genesis_block)) + .unwrap(); + + Self { + acc_store, + block_store, + nullifier_store, + utxo_commitments_store, + pub_tx_store, + node_main_account_info: Account::new(), + } + } + + pub fn get_main_account_addr(&self) -> AccountAddress { + self.node_main_account_info.address + } +}