diff --git a/Cargo.lock b/Cargo.lock index 5a80120..39c842a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3775,6 +3775,7 @@ dependencies = [ "sequencer_core", "serde", "serde_json", + "storage", "tokio", ] diff --git a/sequencer_core/src/lib.rs b/sequencer_core/src/lib.rs index fd54ecd..53aec6f 100644 --- a/sequencer_core/src/lib.rs +++ b/sequencer_core/src/lib.rs @@ -68,6 +68,7 @@ impl SequencerCore { ref execution_output, ref utxo_commitments_created_hashes, ref nullifier_created_hashes, + .. } = tx.tx; //Sanity check @@ -176,10 +177,18 @@ impl SequencerCore { self.execute_check_transaction_on_state(tx)?; } + let prev_block_hash = self + .store + .block_store + .get_block_at_id(self.chain_height)? + .prev_block_hash; + let hashable_data = HashableBlockData { block_id: self.chain_height + 1, + prev_block_id: self.chain_height, transactions: transactions.into_iter().map(|tx_mem| tx_mem.tx).collect(), data: vec![], + prev_block_hash, }; let block = Block::produce_block_from_hashable_data(hashable_data); diff --git a/sequencer_core/src/sequecer_store/mod.rs b/sequencer_core/src/sequecer_store/mod.rs index 9e34b9a..a057192 100644 --- a/sequencer_core/src/sequecer_store/mod.rs +++ b/sequencer_core/src/sequecer_store/mod.rs @@ -28,15 +28,19 @@ impl SequecerChainStore { 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); diff --git a/sequencer_rpc/Cargo.toml b/sequencer_rpc/Cargo.toml index fedb230..57e319b 100644 --- a/sequencer_rpc/Cargo.toml +++ b/sequencer_rpc/Cargo.toml @@ -28,5 +28,8 @@ path = "../networking" [dependencies.sequencer_core] path = "../sequencer_core" +[dependencies.storage] +path = "../storage" + [dependencies.rpc_primitives] path = "../rpc_primitives" diff --git a/sequencer_rpc/src/process.rs b/sequencer_rpc/src/process.rs index c36557b..5314f62 100644 --- a/sequencer_rpc/src/process.rs +++ b/sequencer_rpc/src/process.rs @@ -11,8 +11,8 @@ use rpc_primitives::{ use crate::{ rpc_error_responce_inverter, types::rpc_structs::{ - HelloRequest, HelloResponse, RegisterAccountRequest, RegisterAccountResponse, - SendTxRequest, SendTxResponse, + GetBlockDataRequest, GetBlockDataResponse, HelloRequest, HelloResponse, + RegisterAccountRequest, RegisterAccountResponse, SendTxRequest, SendTxResponse, }, }; @@ -83,11 +83,29 @@ impl JsonHandler { respond(helperstruct) } + async fn process_get_block_data(&self, request: Request) -> Result { + let get_block_req = GetBlockDataRequest::parse(Some(request.params))?; + + let block = { + let state = self.sequencer_state.lock().await; + + state + .store + .block_store + .get_block_at_id(get_block_req.block_id)? + }; + + let helperstruct = GetBlockDataResponse { block }; + + respond(helperstruct) + } + pub async fn process_request_internal(&self, request: Request) -> Result { 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, _ => Err(RpcErr(RpcError::method_not_found(request.method))), } } diff --git a/sequencer_rpc/src/types/rpc_structs.rs b/sequencer_rpc/src/types/rpc_structs.rs index cbd17c6..c8dc5d3 100644 --- a/sequencer_rpc/src/types/rpc_structs.rs +++ b/sequencer_rpc/src/types/rpc_structs.rs @@ -5,6 +5,7 @@ use rpc_primitives::parser::RpcRequest; use sequencer_core::transaction_mempool::TransactionMempool; use serde::{Deserialize, Serialize}; use serde_json::Value; +use storage::block::Block; #[derive(Serialize, Deserialize, Debug)] pub struct HelloRequest {} @@ -21,9 +22,15 @@ pub struct SendTxRequest { pub transaction: TransactionMempool, } +#[derive(Serialize, Deserialize, Debug)] +pub struct GetBlockDataRequest { + pub block_id: u64, +} + parse_request!(HelloRequest); parse_request!(RegisterAccountRequest); parse_request!(SendTxRequest); +parse_request!(GetBlockDataRequest); #[derive(Serialize, Deserialize, Debug)] pub struct HelloResponse { @@ -39,3 +46,8 @@ pub struct RegisterAccountResponse { pub struct SendTxResponse { pub status: String, } + +#[derive(Serialize, Deserialize, Debug)] +pub struct GetBlockDataResponse { + pub block: Block, +} diff --git a/storage/src/block.rs b/storage/src/block.rs index 3a974d2..8de616a 100644 --- a/storage/src/block.rs +++ b/storage/src/block.rs @@ -7,10 +7,11 @@ pub type BlockHash = [u8; 32]; pub type Data = Vec; pub type BlockId = u64; -//ToDo: Add fields to block when model is clear #[derive(Debug, Serialize, Deserialize)] pub struct Block { pub block_id: BlockId, + pub prev_block_id: BlockId, + pub prev_block_hash: BlockHash, pub hash: BlockHash, pub transactions: Vec, pub data: Data, @@ -19,6 +20,8 @@ pub struct Block { #[derive(Debug, Serialize, Deserialize)] pub struct HashableBlockData { pub block_id: BlockId, + pub prev_block_id: BlockId, + pub prev_block_hash: BlockHash, pub transactions: Vec, pub data: Data, } @@ -31,9 +34,11 @@ impl Block { Self { block_id: hashable_data.block_id, + prev_block_id: hashable_data.prev_block_id, hash, transactions: hashable_data.transactions, data: hashable_data.data, + prev_block_hash: hashable_data.prev_block_hash, } } } diff --git a/storage/src/transaction.rs b/storage/src/transaction.rs index 2acb005..3c1cf37 100644 --- a/storage/src/transaction.rs +++ b/storage/src/transaction.rs @@ -23,4 +23,6 @@ pub struct Transaction { pub utxo_commitments_created_hashes: Vec, ///Tx output nullifiers pub nullifier_created_hashes: Vec, + ///Execution proof (private part) + pub execution_proof_private: String, }