Merge pull request #30 from vacp2p/Pravdyvy/sequencer-interaction

Node to sequencer interaction
This commit is contained in:
tyshko-rostyslav 2024-12-08 19:51:53 -05:00 committed by GitHub
commit c322aba074
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 524 additions and 90 deletions

244
Cargo.lock generated
View File

@ -71,7 +71,7 @@ checksum = "0346d8c1f762b41b458ed3145eea914966bb9ad20b9be0d6d463b20d45586370"
dependencies = [
"actix-utils",
"actix-web",
"derive_more",
"derive_more 0.99.18",
"futures-util",
"log",
"once_cell",
@ -93,7 +93,7 @@ dependencies = [
"bitflags 2.6.0",
"bytes",
"bytestring",
"derive_more",
"derive_more 0.99.18",
"encoding_rs",
"futures-core",
"h2",
@ -204,7 +204,7 @@ dependencies = [
"bytes",
"bytestring",
"cfg-if 1.0.0",
"derive_more",
"derive_more 0.99.18",
"encoding_rs",
"futures-core",
"futures-util",
@ -580,6 +580,12 @@ dependencies = [
"winapi",
]
[[package]]
name = "auto_ops"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7460f7dd8e100147b82a63afca1a20eb6c231ee36b90ba7272e14951cb58af59"
[[package]]
name = "autocfg"
version = "1.4.0"
@ -757,8 +763,8 @@ dependencies = [
[[package]]
name = "bonsai-sdk"
version = "1.1.3"
source = "git+https://github.com/risc0/risc0.git?branch=release-1.1#20df1afbcdd2ca442ece8c3fecd25a20cc0aafb5"
version = "1.2.0"
source = "git+https://github.com/risc0/risc0.git?branch=release-1.2#baf81cdbab100e706f5f8241fc47701c662111cd"
dependencies = [
"duplicate",
"maybe-async",
@ -1246,17 +1252,6 @@ dependencies = [
"syn 1.0.109",
]
[[package]]
name = "derive-debug"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e53ef7e1cf756fd5a8e74b9a0a9504ec446eddde86c3063a76ff26a13b7773b1"
dependencies = [
"proc-macro2",
"quote",
"syn 1.0.109",
]
[[package]]
name = "derive_arbitrary"
version = "1.4.1"
@ -1281,6 +1276,27 @@ dependencies = [
"syn 2.0.87",
]
[[package]]
name = "derive_more"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4a9b99b9cbbe49445b21764dc0625032a89b145a2642e67603e1c936f5458d05"
dependencies = [
"derive_more-impl",
]
[[package]]
name = "derive_more-impl"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cb7330aeadfbe296029522e6c40f315320aba36fc43a5b3632f3795348f3bd22"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.87",
"unicode-xid",
]
[[package]]
name = "digest"
version = "0.8.1"
@ -2602,7 +2618,7 @@ dependencies = [
"rand 0.7.3",
"scopeguard",
"sha2 0.8.2",
"sha3",
"sha3 0.8.2",
]
[[package]]
@ -2668,6 +2684,8 @@ dependencies = [
"serde_json",
"sha2 0.10.8",
"storage",
"thiserror",
"tokio",
"utxo",
]
@ -2806,6 +2824,17 @@ version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "51d515d32fb182ee37cda2ccdcb92950d6a3c2893aa280e540671c2cd0f3b1d9"
[[package]]
name = "num-derive"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed3955f1a9c7c0c15e092f9c887db08b1fc683305fdf6eb6684f22555355e202"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.87",
]
[[package]]
name = "num-integer"
version = "0.1.46"
@ -3519,13 +3548,27 @@ dependencies = [
"anyhow",
"borsh",
"elf",
"risc0-zkp",
"risc0-zkvm-platform",
"risc0-zkp 1.1.3",
"risc0-zkvm-platform 1.1.3",
"serde",
"syn 2.0.87",
"tracing",
]
[[package]]
name = "risc0-binfmt"
version = "1.2.0"
source = "git+https://github.com/risc0/risc0.git?branch=release-1.2#baf81cdbab100e706f5f8241fc47701c662111cd"
dependencies = [
"anyhow",
"borsh",
"elf",
"risc0-zkp 1.2.0",
"risc0-zkvm-platform 1.2.0",
"serde",
"tracing",
]
[[package]]
name = "risc0-build"
version = "1.1.3"
@ -3536,9 +3579,27 @@ dependencies = [
"dirs",
"docker-generate",
"hex",
"risc0-binfmt",
"risc0-zkp",
"risc0-zkvm-platform",
"risc0-binfmt 1.1.3",
"risc0-zkp 1.1.3",
"risc0-zkvm-platform 1.1.3",
"serde",
"serde_json",
"tempfile",
]
[[package]]
name = "risc0-build"
version = "1.2.0"
source = "git+https://github.com/risc0/risc0.git?branch=release-1.2#baf81cdbab100e706f5f8241fc47701c662111cd"
dependencies = [
"anyhow",
"cargo_metadata",
"dirs",
"docker-generate",
"hex",
"risc0-binfmt 1.2.0",
"risc0-zkp 1.2.0",
"risc0-zkvm-platform 1.2.0",
"serde",
"serde_json",
"tempfile",
@ -3546,8 +3607,8 @@ dependencies = [
[[package]]
name = "risc0-build-kernel"
version = "1.1.3"
source = "git+https://github.com/risc0/risc0.git?branch=release-1.1#20df1afbcdd2ca442ece8c3fecd25a20cc0aafb5"
version = "1.2.0"
source = "git+https://github.com/risc0/risc0.git?branch=release-1.2#baf81cdbab100e706f5f8241fc47701c662111cd"
dependencies = [
"cc",
"directories",
@ -3561,8 +3622,8 @@ dependencies = [
[[package]]
name = "risc0-circuit-recursion"
version = "1.1.3"
source = "git+https://github.com/risc0/risc0.git?branch=release-1.1#20df1afbcdd2ca442ece8c3fecd25a20cc0aafb5"
version = "1.2.0"
source = "git+https://github.com/risc0/risc0.git?branch=release-1.2#baf81cdbab100e706f5f8241fc47701c662111cd"
dependencies = [
"anyhow",
"bytemuck",
@ -3575,9 +3636,9 @@ dependencies = [
"rand 0.8.5",
"rayon",
"risc0-circuit-recursion-sys",
"risc0-core",
"risc0-core 1.2.0",
"risc0-sys",
"risc0-zkp",
"risc0-zkp 1.2.0",
"serde",
"sha2 0.10.8",
"tracing",
@ -3586,38 +3647,43 @@ dependencies = [
[[package]]
name = "risc0-circuit-recursion-sys"
version = "1.1.3"
source = "git+https://github.com/risc0/risc0.git?branch=release-1.1#20df1afbcdd2ca442ece8c3fecd25a20cc0aafb5"
version = "1.2.0"
source = "git+https://github.com/risc0/risc0.git?branch=release-1.2#baf81cdbab100e706f5f8241fc47701c662111cd"
dependencies = [
"glob",
"risc0-build-kernel",
"risc0-core",
"risc0-core 1.2.0",
"risc0-sys",
"sppark",
]
[[package]]
name = "risc0-circuit-rv32im"
version = "1.1.3"
source = "git+https://github.com/risc0/risc0.git?branch=release-1.1#20df1afbcdd2ca442ece8c3fecd25a20cc0aafb5"
version = "1.2.0"
source = "git+https://github.com/risc0/risc0.git?branch=release-1.2#baf81cdbab100e706f5f8241fc47701c662111cd"
dependencies = [
"anyhow",
"auto_ops",
"bytemuck",
"byteorder",
"cfg-if 1.0.0",
"crossbeam",
"crypto-bigint",
"cust",
"derive-debug",
"derive_more 1.0.0",
"lazy-regex",
"metal",
"num-bigint 0.4.6",
"num-derive",
"num-traits",
"rand 0.8.5",
"rayon",
"risc0-binfmt",
"risc0-binfmt 1.2.0",
"risc0-circuit-rv32im-sys",
"risc0-core",
"risc0-core 1.2.0",
"risc0-sys",
"risc0-zkp",
"risc0-zkvm-platform",
"risc0-zkp 1.2.0",
"risc0-zkvm-platform 1.2.0",
"serde",
"sha2 0.10.8",
"tracing",
@ -3625,12 +3691,12 @@ dependencies = [
[[package]]
name = "risc0-circuit-rv32im-sys"
version = "1.1.3"
source = "git+https://github.com/risc0/risc0.git?branch=release-1.1#20df1afbcdd2ca442ece8c3fecd25a20cc0aafb5"
version = "1.2.0"
source = "git+https://github.com/risc0/risc0.git?branch=release-1.2#baf81cdbab100e706f5f8241fc47701c662111cd"
dependencies = [
"glob",
"risc0-build-kernel",
"risc0-core",
"risc0-core 1.2.0",
"risc0-sys",
"sppark",
]
@ -3639,6 +3705,15 @@ dependencies = [
name = "risc0-core"
version = "1.1.3"
source = "git+https://github.com/risc0/risc0.git?branch=release-1.1#20df1afbcdd2ca442ece8c3fecd25a20cc0aafb5"
dependencies = [
"bytemuck",
"rand_core 0.6.4",
]
[[package]]
name = "risc0-core"
version = "1.2.0"
source = "git+https://github.com/risc0/risc0.git?branch=release-1.2#baf81cdbab100e706f5f8241fc47701c662111cd"
dependencies = [
"bytemuck",
"nvtx",
@ -3648,8 +3723,8 @@ dependencies = [
[[package]]
name = "risc0-groth16"
version = "1.1.3"
source = "git+https://github.com/risc0/risc0.git?branch=release-1.1#20df1afbcdd2ca442ece8c3fecd25a20cc0aafb5"
version = "1.2.0"
source = "git+https://github.com/risc0/risc0.git?branch=release-1.2#baf81cdbab100e706f5f8241fc47701c662111cd"
dependencies = [
"anyhow",
"ark-bn254",
@ -3660,19 +3735,20 @@ dependencies = [
"hex",
"num-bigint 0.4.6",
"num-traits",
"risc0-binfmt",
"risc0-core",
"risc0-zkp",
"risc0-binfmt 1.2.0",
"risc0-core 1.2.0",
"risc0-zkp 1.2.0",
"serde",
"serde_json",
"stability",
"tempfile",
"tracing",
]
[[package]]
name = "risc0-sys"
version = "1.1.3"
source = "git+https://github.com/risc0/risc0.git?branch=release-1.1#20df1afbcdd2ca442ece8c3fecd25a20cc0aafb5"
version = "1.2.0"
source = "git+https://github.com/risc0/risc0.git?branch=release-1.2#baf81cdbab100e706f5f8241fc47701c662111cd"
dependencies = [
"anyhow",
"cc",
@ -3685,6 +3761,29 @@ dependencies = [
name = "risc0-zkp"
version = "1.1.3"
source = "git+https://github.com/risc0/risc0.git?branch=release-1.1#20df1afbcdd2ca442ece8c3fecd25a20cc0aafb5"
dependencies = [
"anyhow",
"blake2",
"borsh",
"bytemuck",
"cfg-if 1.0.0",
"digest 0.10.7",
"hex",
"hex-literal",
"metal",
"paste 1.0.15",
"rand_core 0.6.4",
"risc0-core 1.1.3",
"risc0-zkvm-platform 1.1.3",
"serde",
"sha2 0.10.8",
"tracing",
]
[[package]]
name = "risc0-zkp"
version = "1.2.0"
source = "git+https://github.com/risc0/risc0.git?branch=release-1.2#baf81cdbab100e706f5f8241fc47701c662111cd"
dependencies = [
"anyhow",
"blake2",
@ -3703,9 +3802,9 @@ dependencies = [
"rand 0.8.5",
"rand_core 0.6.4",
"rayon",
"risc0-core",
"risc0-core 1.2.0",
"risc0-sys",
"risc0-zkvm-platform",
"risc0-zkvm-platform 1.2.0",
"serde",
"sha2 0.10.8",
"tracing",
@ -3713,8 +3812,8 @@ dependencies = [
[[package]]
name = "risc0-zkvm"
version = "1.1.3"
source = "git+https://github.com/risc0/risc0.git?branch=release-1.1#20df1afbcdd2ca442ece8c3fecd25a20cc0aafb5"
version = "1.2.0"
source = "git+https://github.com/risc0/risc0.git?branch=release-1.2#baf81cdbab100e706f5f8241fc47701c662111cd"
dependencies = [
"addr2line 0.22.0",
"anyhow",
@ -3727,22 +3826,25 @@ dependencies = [
"getrandom 0.2.15",
"hex",
"lazy-regex",
"num-bigint 0.4.6",
"num-traits",
"prost",
"rand 0.8.5",
"rayon",
"risc0-binfmt",
"risc0-build",
"risc0-binfmt 1.2.0",
"risc0-build 1.2.0",
"risc0-circuit-recursion",
"risc0-circuit-rv32im",
"risc0-core",
"risc0-core 1.2.0",
"risc0-groth16",
"risc0-zkp",
"risc0-zkvm-platform",
"risc0-zkp 1.2.0",
"risc0-zkvm-platform 1.2.0",
"rrs-lib",
"rustc-demangle",
"semver",
"serde",
"sha2 0.10.8",
"sha3 0.10.8",
"stability",
"tempfile",
"tracing",
@ -3753,6 +3855,14 @@ dependencies = [
name = "risc0-zkvm-platform"
version = "1.1.3"
source = "git+https://github.com/risc0/risc0.git?branch=release-1.1#20df1afbcdd2ca442ece8c3fecd25a20cc0aafb5"
dependencies = [
"stability",
]
[[package]]
name = "risc0-zkvm-platform"
version = "1.2.0"
source = "git+https://github.com/risc0/risc0.git?branch=release-1.2#baf81cdbab100e706f5f8241fc47701c662111cd"
dependencies = [
"bytemuck",
"getrandom 0.2.15",
@ -3896,7 +4006,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5174a470eeb535a721ae9fdd6e291c2411a906b96592182d05217591d5c5cf7b"
dependencies = [
"byteorder",
"derive_more",
"derive_more 0.99.18",
"twox-hash",
]
@ -4171,6 +4281,16 @@ dependencies = [
"opaque-debug 0.2.3",
]
[[package]]
name = "sha3"
version = "0.10.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "75872d278a8f37ef87fa0ddbda7802605cb18344497949862c0d4dcb291eba60"
dependencies = [
"digest 0.10.7",
"keccak",
]
[[package]]
name = "shlex"
version = "1.3.0"
@ -4421,7 +4541,7 @@ dependencies = [
name = "test-methods"
version = "0.1.0"
dependencies = [
"risc0-build",
"risc0-build 1.1.3",
]
[[package]]
@ -4708,6 +4828,12 @@ version = "1.0.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe"
[[package]]
name = "unicode-xid"
version = "0.2.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ebc1c04c71510c7f702b52b7c350734c9ff1295c464a03335b00bb84fc54f853"
[[package]]
name = "universal-hash"
version = "0.5.1"

View File

@ -16,6 +16,8 @@ monotree.workspace = true
bincode.workspace = true
elliptic-curve.workspace = true
reqwest.workspace = true
thiserror.workspace = true
tokio.workspace = true
[dependencies.accounts]
path = "../accounts"

View File

@ -8,4 +8,8 @@ pub struct NodeConfig {
pub home: PathBuf,
///Override rust log (env var logging level)
pub override_rust_log: Option<String>,
///Sequencer URL
pub sequencer_addr: String,
///Sequencer polling duration for new blocks in seconds
pub seq_poll_timeout_secs: u64,
}

View File

@ -1,6 +1,14 @@
use accounts::account_core::AccountAddress;
use std::sync::{
atomic::{AtomicU64, Ordering},
Arc,
};
use accounts::account_core::Account;
use anyhow::Result;
use config::NodeConfig;
use sequencer_client::SequencerClient;
use storage::NodeChainStore;
use tokio::{sync::Mutex, task::JoinHandle};
pub mod config;
pub mod executions;
@ -8,8 +16,73 @@ pub mod sequencer_client;
pub mod storage;
pub struct NodeCore {
pub storage: NodeChainStore,
pub curr_height: u64,
pub main_acc_addr: AccountAddress,
pub storage: Arc<Mutex<NodeChainStore>>,
pub curr_height: Arc<AtomicU64>,
pub main_acc: Account,
pub node_config: NodeConfig,
pub db_updater_handle: JoinHandle<Result<()>>,
}
impl NodeCore {
pub async fn start_from_config_update_chain(config: NodeConfig) -> Result<Self> {
let client = SequencerClient::new(config.clone())?;
let genesis_id = client.get_genesis_id().await?;
let genesis_block = client.get_block(genesis_id.genesis_id).await?.block;
let mut storage = NodeChainStore::new_with_genesis(&config.home, genesis_block);
let account = Account::new();
let mut chain_height = genesis_id.genesis_id;
//Chain update loop
loop {
let next_block = chain_height + 1;
if let Ok(block) = client.get_block(next_block).await {
storage.dissect_insert_block(block.block)?;
} else {
break;
}
chain_height += 1;
}
let wrapped_storage = Arc::new(Mutex::new(storage));
let chain_height_wrapped = Arc::new(AtomicU64::new(chain_height));
let wrapped_storage_thread = wrapped_storage.clone();
let wrapped_chain_height_thread = chain_height_wrapped.clone();
let client_thread = client.clone();
let updater_handle = tokio::spawn(async move {
loop {
let next_block = wrapped_chain_height_thread.load(Ordering::Relaxed) + 1;
if let Ok(block) = client_thread.get_block(next_block).await {
{
let mut storage_guard = wrapped_storage_thread.lock().await;
storage_guard.dissect_insert_block(block.block)?;
}
wrapped_chain_height_thread.store(next_block, Ordering::Relaxed);
} else {
tokio::time::sleep(std::time::Duration::from_secs(
config.seq_poll_timeout_secs,
))
.await;
}
}
});
Ok(Self {
storage: wrapped_storage,
curr_height: chain_height_wrapped,
main_acc: account,
node_config: config.clone(),
db_updater_handle: updater_handle,
})
}
}

View File

@ -1 +1,68 @@
use serde::{Deserialize, Serialize};
use storage::{block::Block, transaction::Transaction};
//Requests
#[derive(Serialize, Deserialize, Debug)]
pub struct RegisterAccountRequest {
pub nullifier_public_key: Vec<u8>,
pub viewing_public_key: Vec<u8>,
pub address: [u8; 32],
}
#[derive(Serialize, Deserialize, Debug)]
pub struct SendTxRequest {
pub transaction: Transaction,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct GetBlockDataRequest {
pub block_id: u64,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct GetGenesisIdRequest {}
//Responses
#[derive(Serialize, Deserialize, Debug)]
pub struct RegisterAccountResponse {
pub status: String,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct SendTxResponse {
pub status: String,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct GetBlockDataResponse {
pub block: Block,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct GetGenesisIdResponse {
pub genesis_id: u64,
}
//General
#[derive(Debug, Clone, Serialize)]
pub struct SequencerRpcRequest {
jsonrpc: String,
pub method: String,
pub params: serde_json::Value,
pub id: u64,
}
impl SequencerRpcRequest {
pub fn from_payload_version_2_0(method: String, payload: serde_json::Value) -> Self {
Self {
jsonrpc: "2.0".to_string(),
method,
params: payload,
//ToDo: Correct checking of id
id: 1,
}
}
}

View File

@ -1,8 +1,132 @@
use accounts::account_core::Account;
use anyhow::Result;
use json::{
GetBlockDataRequest, GetBlockDataResponse, GetGenesisIdRequest, GetGenesisIdResponse,
RegisterAccountRequest, RegisterAccountResponse, SendTxRequest, SendTxResponse,
SequencerRpcRequest,
};
use k256::elliptic_curve::group::GroupEncoding;
use reqwest::Client;
use serde_json::Value;
use storage::transaction::Transaction;
use crate::config::NodeConfig;
pub mod json;
#[derive(Clone)]
pub struct SequencerClient {
pub client: reqwest::Client,
pub config: NodeConfig,
}
#[derive(thiserror::Error, Debug)]
pub enum SequencerClientError {
#[error("HTTP error")]
HTTPError(reqwest::Error),
#[error("Serde error")]
SerdeError(serde_json::Error),
}
impl From<reqwest::Error> for SequencerClientError {
fn from(value: reqwest::Error) -> Self {
SequencerClientError::HTTPError(value)
}
}
impl From<serde_json::Error> for SequencerClientError {
fn from(value: serde_json::Error) -> Self {
SequencerClientError::SerdeError(value)
}
}
impl SequencerClient {
pub fn new(config: NodeConfig) -> Result<Self> {
Ok(Self {
client: Client::builder()
//Add more fiedls if needed
.timeout(std::time::Duration::from_secs(60))
.build()?,
config,
})
}
pub async fn call_method_with_payload(
&self,
method: &str,
payload: Value,
) -> Result<Value, SequencerClientError> {
let request = SequencerRpcRequest::from_payload_version_2_0(method.to_string(), payload);
let call_builder = self.client.post(&self.config.sequencer_addr);
let call_res = call_builder.json(&request).send().await?;
let response = call_res.json::<Value>().await?;
Ok(response)
}
pub async fn get_block(
&self,
block_id: u64,
) -> Result<GetBlockDataResponse, SequencerClientError> {
let block_req = GetBlockDataRequest { block_id };
let req = serde_json::to_value(block_req)?;
let resp = self.call_method_with_payload("get_block", req).await?;
let resp_deser = serde_json::from_value(resp)?;
Ok(resp_deser)
}
pub async fn send_tx(
&self,
transaction: Transaction,
) -> Result<SendTxResponse, SequencerClientError> {
let tx_req = SendTxRequest { transaction };
let req = serde_json::to_value(tx_req)?;
let resp = self.call_method_with_payload("send_tx", req).await?;
let resp_deser = serde_json::from_value(resp)?;
Ok(resp_deser)
}
pub async fn register_account(
&self,
account: &Account,
) -> Result<RegisterAccountResponse, SequencerClientError> {
let acc_req = RegisterAccountRequest {
nullifier_public_key: account.key_holder.nullifer_public_key.to_bytes().to_vec(),
viewing_public_key: account.key_holder.viewing_public_key.to_bytes().to_vec(),
address: account.address,
};
let req = serde_json::to_value(acc_req)?;
let resp = self
.call_method_with_payload("register_account", req)
.await?;
let resp_deser = serde_json::from_value(resp)?;
Ok(resp_deser)
}
pub async fn get_genesis_id(&self) -> Result<GetGenesisIdResponse, SequencerClientError> {
let genesis_req = GetGenesisIdRequest {};
let req = serde_json::to_value(genesis_req)?;
let resp = self.call_method_with_payload("get_genesis", req).await?;
let resp_deser = serde_json::from_value(resp)?;
Ok(resp_deser)
}
}

View File

@ -2,12 +2,14 @@ use std::path::Path;
use accounts::account_core::{Account, AccountAddress};
use accounts_store::NodeAccountsStore;
use anyhow::Result;
use block_store::NodeBlockStore;
use rand::{rngs::OsRng, RngCore};
use storage::{
block::{Block, HashableBlockData},
block::Block,
merkle_tree_public::merkle_tree::{PublicTransactionMerkleTree, UTXOCommitmentsMerkleTree},
nullifier::UTXONullifier,
nullifier_sparse_merkle_tree::NullifierSparseMerkleTree,
utxo_commitment::UTXOCommitment,
};
pub mod accounts_store;
@ -25,30 +27,12 @@ pub struct NodeChainStore {
}
impl NodeChainStore {
pub fn new_with_genesis(home_dir: &Path, genesis_id: u64, is_genesis_random: bool) -> Self {
pub fn new_with_genesis(home_dir: &Path, genesis_block: Block) -> 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 =
@ -68,4 +52,30 @@ impl NodeChainStore {
pub fn get_main_account_addr(&self) -> AccountAddress {
self.node_main_account_info.address
}
pub fn dissect_insert_block(&mut self, block: Block) -> Result<()> {
for tx in &block.transactions {
self.utxo_commitments_store.add_tx_multiple(
tx.utxo_commitments_created_hashes
.clone()
.into_iter()
.map(|hash| UTXOCommitment { hash })
.collect(),
);
self.nullifier_store.insert_items(
tx.nullifier_created_hashes
.clone()
.into_iter()
.map(|hash| UTXONullifier { utxo_hash: hash })
.collect(),
)?;
self.pub_tx_store.add_tx(tx.clone());
}
self.block_store.put_block_at_id(block)?;
Ok(())
}
}

View File

@ -5,6 +5,7 @@ use storage::{block::Block, RocksDBIO};
pub struct SequecerBlockStore {
dbio: RocksDBIO,
pub genesis_id: u64,
}
impl SequecerBlockStore {
@ -13,9 +14,11 @@ impl SequecerBlockStore {
///
/// ATTENTION: Will overwrite genesis block.
pub fn open_db_with_genesis(location: &Path, genesis_block: Option<Block>) -> Result<Self> {
Ok(Self {
dbio: RocksDBIO::new(location, genesis_block)?,
})
let dbio = RocksDBIO::new(location, genesis_block)?;
let genesis_id = dbio.get_meta_first_block_in_db()?;
Ok(Self { dbio, genesis_id })
}
///Reopening existing database

View File

@ -11,8 +11,9 @@ use rpc_primitives::{
use crate::{
rpc_error_responce_inverter,
types::rpc_structs::{
GetBlockDataRequest, GetBlockDataResponse, HelloRequest, HelloResponse,
RegisterAccountRequest, RegisterAccountResponse, SendTxRequest, SendTxResponse,
GetBlockDataRequest, GetBlockDataResponse, GetGenesisIdRequest, GetGenesisIdResponse,
HelloRequest, HelloResponse, RegisterAccountRequest, RegisterAccountResponse,
SendTxRequest, SendTxResponse,
},
};
@ -100,12 +101,27 @@ impl JsonHandler {
respond(helperstruct)
}
async fn process_get_genesis(&self, request: Request) -> Result<Value, RpcErr> {
let _get_genesis_req = GetGenesisIdRequest::parse(Some(request.params))?;
let genesis_id = {
let state = self.sequencer_state.lock().await;
state.store.block_store.genesis_id
};
let helperstruct = GetGenesisIdResponse { genesis_id };
respond(helperstruct)
}
pub async fn process_request_internal(&self, request: Request) -> Result<Value, RpcErr> {
match request.method.as_ref() {
"hello" => self.process_temp_hello(request).await,
"register_account" => self.process_register_account_request(request).await,
"send_tx" => self.process_send_tx(request).await,
"get_block" => self.process_get_block_data(request).await,
"get_genesis" => self.process_get_genesis(request).await,
_ => Err(RpcErr(RpcError::method_not_found(request.method))),
}
}

View File

@ -27,10 +27,14 @@ pub struct GetBlockDataRequest {
pub block_id: u64,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct GetGenesisIdRequest {}
parse_request!(HelloRequest);
parse_request!(RegisterAccountRequest);
parse_request!(SendTxRequest);
parse_request!(GetBlockDataRequest);
parse_request!(GetGenesisIdRequest);
#[derive(Serialize, Deserialize, Debug)]
pub struct HelloResponse {
@ -51,3 +55,8 @@ pub struct SendTxResponse {
pub struct GetBlockDataResponse {
pub block: Block,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct GetGenesisIdResponse {
pub genesis_id: u64,
}

View File

@ -10,7 +10,7 @@ env_logger.workspace = true
log.workspace = true
serde.workspace = true
risc0-zkvm = { git = "https://github.com/risc0/risc0.git", branch = "release-1.1" }
risc0-zkvm = { git = "https://github.com/risc0/risc0.git", branch = "release-1.2" }
test-methods = { path = "test_methods" }
[features]