From fd136b23f60bcd7cb4534a53268e93456fba7729 Mon Sep 17 00:00:00 2001
From: "hinto.janai" <hinto.janai@protonmail.com>
Date: Fri, 11 Oct 2024 18:07:11 -0400
Subject: [PATCH] add `BlockChainContextService`, `on_get_block_hash`

---
 binaries/cuprated/src/rpc/handler.rs |  6 ++++++
 binaries/cuprated/src/rpc/json.rs    | 26 ++++++++++++--------------
 rpc/types/src/json.rs                |  4 ++--
 3 files changed, 20 insertions(+), 16 deletions(-)

diff --git a/binaries/cuprated/src/rpc/handler.rs b/binaries/cuprated/src/rpc/handler.rs
index af2e3f2c..2019beb4 100644
--- a/binaries/cuprated/src/rpc/handler.rs
+++ b/binaries/cuprated/src/rpc/handler.rs
@@ -3,6 +3,7 @@
 use std::task::{Context, Poll};
 
 use anyhow::Error;
+use cuprate_consensus::BlockChainContextService;
 use futures::future::BoxFuture;
 use monero_serai::block::Block;
 use tower::Service;
@@ -102,6 +103,9 @@ pub struct CupratedRpcHandler {
     /// Read handle to the blockchain database.
     pub blockchain_read: BlockchainReadHandle,
 
+    /// Handle to the blockchain context service.
+    pub blockchain_context: BlockChainContextService,
+
     /// Handle to the blockchain manager.
     pub blockchain_manager: BlockchainManagerHandle,
 
@@ -117,6 +121,7 @@ impl CupratedRpcHandler {
     pub const fn new(
         restricted: bool,
         blockchain_read: BlockchainReadHandle,
+        blockchain_context: BlockChainContextService,
         blockchain_manager: BlockchainManagerHandle,
         txpool_read: TxpoolReadHandle,
         txpool_manager: std::convert::Infallible,
@@ -124,6 +129,7 @@ impl CupratedRpcHandler {
         Self {
             restricted,
             blockchain_read,
+            blockchain_context,
             blockchain_manager,
             txpool_read,
             txpool_manager,
diff --git a/binaries/cuprated/src/rpc/json.rs b/binaries/cuprated/src/rpc/json.rs
index 604ed91e..ffbac282 100644
--- a/binaries/cuprated/src/rpc/json.rs
+++ b/binaries/cuprated/src/rpc/json.rs
@@ -38,7 +38,11 @@ use cuprate_rpc_types::{
     CORE_RPC_VERSION,
 };
 
-use crate::rpc::{helper, request::blockchain, CupratedRpcHandler};
+use crate::rpc::{
+    helper,
+    request::{blockchain, blockchain_context, blockchain_manager},
+    CupratedRpcHandler,
+};
 
 /// Map a [`JsonRpcRequest`] to the function that will lead to a [`JsonRpcResponse`].
 pub(super) async fn map_request(
@@ -115,7 +119,8 @@ async fn on_get_block_hash(
     request: OnGetBlockHashRequest,
 ) -> Result<OnGetBlockHashResponse, Error> {
     let [height] = request.block_height;
-    let hash = blockchain::block_hash(&mut state.blockchain_read, height, todo!()).await?;
+    let hash = blockchain::block_hash(&mut state.blockchain_read, height, todo!("access to chain"))
+        .await?;
     let block_hash = hex::encode(hash);
 
     Ok(OnGetBlockHashResponse { block_hash })
@@ -126,21 +131,14 @@ async fn submit_block(
     mut state: CupratedRpcHandler,
     request: SubmitBlockRequest,
 ) -> Result<SubmitBlockResponse, Error> {
+    // Parse hex into block.
     let [blob] = request.block_blob;
-
-    let limit = todo!(); //blockchain::cumulative_block_weight_limit(&mut state.blockchain_read).await?;
-
-    // if blob.len() > limit + BLOCK_SIZE_SANITY_LEEWAY {
-    //     return Err(anyhow!("Block size is too big, rejecting block"));
-    // }
-
     let bytes = hex::decode(blob)?;
     let block = Block::read(&mut bytes.as_slice())?;
+    let block_id = hex::encode(block.hash());
 
-    // <https://github.com/monero-project/monero/blob/master/src/cryptonote_core/cryptonote_core.cpp#L1540>
-    let block_id = todo!("submit block to DB");
-    todo!("relay to P2P");
-    todo!("send to txpool");
+    // Attempt to relay the block.
+    blockchain_manager::relay_block(&mut state.blockchain_manager, block).await?;
 
     Ok(SubmitBlockResponse {
         base: ResponseBase::ok(),
@@ -383,7 +381,7 @@ async fn hard_fork_info(
     Ok(HardForkInfoResponse {
         base: AccessResponseBase::ok(),
         earliest_height: todo!(),
-        enabled: hard_fork.is_current(),
+        enabled: todo!("hard_fork.is_latest() is not always correct"),
         state: todo!(),
         threshold: todo!(),
         version: hard_fork.as_u8(),
diff --git a/rpc/types/src/json.rs b/rpc/types/src/json.rs
index 7e9700b3..fd9ffa32 100644
--- a/rpc/types/src/json.rs
+++ b/rpc/types/src/json.rs
@@ -836,9 +836,9 @@ define_request_and_response! {
             enabled: true,
             state: 0,
             threshold: 0,
-            version: 3,
+            version: 16,
             votes: 10080,
-            voting: 3,
+            voting: 16,
             window: 10080
         }
     )]