current_hard_fork

This commit is contained in:
hinto.janai 2024-09-13 17:26:24 -04:00
parent 35065b7866
commit 82c56b99c0
No known key found for this signature in database
GPG key ID: D47CE05FA175A499
8 changed files with 100 additions and 16 deletions

View file

@ -18,7 +18,7 @@ use cuprate_helper::{
}; };
use cuprate_types::{ use cuprate_types::{
blockchain::{BlockchainReadRequest, BlockchainWriteRequest}, blockchain::{BlockchainReadRequest, BlockchainWriteRequest},
Chain, ExtendedBlockHeader, OutputOnChain, VerifiedBlockInformation, Chain, ExtendedBlockHeader, HardFork, OutputOnChain, VerifiedBlockInformation,
}; };
use crate::rpc::{CupratedRpcHandlerState, RESTRICTED_BLOCK_COUNT, RESTRICTED_BLOCK_HEADER_RANGE}; use crate::rpc::{CupratedRpcHandlerState, RESTRICTED_BLOCK_COUNT, RESTRICTED_BLOCK_HEADER_RANGE};
@ -131,6 +131,23 @@ pub(super) async fn top_block_full(
Ok((block, header)) Ok((block, header))
} }
/// [`BlockchainResponse::CurrentHardFork`]
pub(super) async fn current_hard_fork(
state: &mut CupratedRpcHandlerState,
) -> Result<HardFork, Error> {
let BlockchainResponse::CurrentHardFork(hard_fork) = state
.blockchain_read
.ready()
.await?
.call(BlockchainReadRequest::CurrentHardFork)
.await?
else {
unreachable!();
};
Ok(hard_fork)
}
/// [`BlockchainResponse::BlockHash`] with [`Chain::Main`]. /// [`BlockchainResponse::BlockHash`] with [`Chain::Main`].
pub(super) async fn block_hash( pub(super) async fn block_hash(
state: &mut CupratedRpcHandlerState, state: &mut CupratedRpcHandlerState,

View file

@ -35,7 +35,7 @@ use cuprate_rpc_types::{
misc::{BlockHeader, Status}, misc::{BlockHeader, Status},
CORE_RPC_VERSION, CORE_RPC_VERSION,
}; };
use cuprate_types::{blockchain::BlockchainReadRequest, Chain}; use cuprate_types::{blockchain::BlockchainReadRequest, Chain, HardFork};
use crate::{ use crate::{
rpc::{ rpc::{
@ -377,16 +377,22 @@ async fn get_info(
/// <https://github.com/monero-project/monero/blob/cc73fe71162d564ffda8e549b79a350bca53c454/src/rpc/core_rpc_server.cpp#L2751-L2766> /// <https://github.com/monero-project/monero/blob/cc73fe71162d564ffda8e549b79a350bca53c454/src/rpc/core_rpc_server.cpp#L2751-L2766>
async fn hard_fork_info( async fn hard_fork_info(
state: CupratedRpcHandlerState, mut state: CupratedRpcHandlerState,
request: HardForkInfoRequest, request: HardForkInfoRequest,
) -> Result<HardForkInfoResponse, Error> { ) -> Result<HardForkInfoResponse, Error> {
let hard_fork = if request.version > 0 {
HardFork::from_version(request.version)?
} else {
blockchain::current_hard_fork(&mut state).await?
};
Ok(HardForkInfoResponse { Ok(HardForkInfoResponse {
base: AccessResponseBase::ok(), base: AccessResponseBase::ok(),
earliest_height: todo!(), earliest_height: todo!(),
enabled: todo!(), enabled: hard_fork.is_current(),
state: todo!(), state: todo!(),
threshold: todo!(), threshold: todo!(),
version: todo!(), version: hard_fork.as_u8(),
votes: todo!(), votes: todo!(),
voting: todo!(), voting: todo!(),
window: todo!(), window: todo!(),

View file

@ -815,8 +815,17 @@ define_request_and_response! {
hard_fork_info, hard_fork_info,
cc73fe71162d564ffda8e549b79a350bca53c454 => cc73fe71162d564ffda8e549b79a350bca53c454 =>
core_rpc_server_commands_defs.h => 1958..=1995, core_rpc_server_commands_defs.h => 1958..=1995,
HardForkInfo (empty), HardForkInfo,
Request {},
#[doc = serde_doc_test!(
HARD_FORK_INFO => HardForkInfo {
version: 16,
}
)]
#[derive(Copy)]
Request {
version: u8,
},
#[doc = serde_doc_test!( #[doc = serde_doc_test!(
HARD_FORK_INFO_RESPONSE => HardForkInfoResponse { HARD_FORK_INFO_RESPONSE => HardForkInfoResponse {
@ -825,9 +834,9 @@ define_request_and_response! {
enabled: true, enabled: true,
state: 0, state: 0,
threshold: 0, threshold: 0,
version: 16, version: 3,
votes: 10080, votes: 10080,
voting: 16, voting: 3,
window: 10080 window: 10080
} }
)] )]

View file

@ -2,10 +2,11 @@
//---------------------------------------------------------------------------------------------------- Import //---------------------------------------------------------------------------------------------------- Import
use cuprate_database::{DatabaseRo, RuntimeError}; use cuprate_database::{DatabaseRo, RuntimeError};
use cuprate_types::HardFork;
use crate::{ use crate::{
ops::macros::doc_error, ops::{block, macros::doc_error},
tables::{BlockHeights, BlockInfos}, tables::{BlockHeights, BlockInfos, Tables},
types::BlockHeight, types::BlockHeight,
}; };
@ -78,6 +79,15 @@ pub fn cumulative_generated_coins(
} }
} }
/// TODO
///
#[doc = doc_error!()]
#[inline]
pub fn current_hard_fork(tables: &impl Tables) -> Result<HardFork, RuntimeError> {
let (header, _) = block::get_block_extended_header_top(tables)?;
Ok(header.version)
}
//---------------------------------------------------------------------------------------------------- Tests //---------------------------------------------------------------------------------------------------- Tests
#[cfg(test)] #[cfg(test)]
mod test { mod test {

View file

@ -95,6 +95,7 @@ fn map_request(
R::BlockExtendedHeaderByHash(hash) => block_extended_header_by_hash(env, hash), R::BlockExtendedHeaderByHash(hash) => block_extended_header_by_hash(env, hash),
R::TopBlockExtendedHeader => top_block_extended_header_by_hash(env), R::TopBlockExtendedHeader => top_block_extended_header_by_hash(env),
R::TopBlockFull => top_block_full(env), R::TopBlockFull => top_block_full(env),
R::CurrentHardFork => current_hard_fork(env),
R::BlockHash(height, chain) => block_hash(env, height, chain), R::BlockHash(height, chain) => block_hash(env, height, chain),
R::FindBlock(_) => todo!("Add alt blocks to DB"), R::FindBlock(_) => todo!("Add alt blocks to DB"),
R::FilterUnknownHashes(hashes) => filter_unknown_hashes(env, hashes), R::FilterUnknownHashes(hashes) => filter_unknown_hashes(env, hashes),
@ -283,6 +284,19 @@ fn top_block_full(env: &ConcreteEnv) -> ResponseResult {
Ok(BlockchainResponse::TopBlockFull(block, header)) Ok(BlockchainResponse::TopBlockFull(block, header))
} }
/// [`BlockchainReadRequest::CurrentHardFork`].
#[inline]
fn current_hard_fork(env: &ConcreteEnv) -> ResponseResult {
// Single-threaded, no `ThreadLocal` required.
let env_inner = env.env_inner();
let tx_ro = env_inner.tx_ro()?;
let tables = env_inner.open_tables(&tx_ro)?;
Ok(BlockchainResponse::CurrentHardFork(
blockchain::current_hard_fork(&tables)?,
))
}
/// [`BlockchainReadRequest::BlockHash`]. /// [`BlockchainReadRequest::BlockHash`].
#[inline] #[inline]
fn block_hash(env: &ConcreteEnv, block_height: BlockHeight, chain: Chain) -> ResponseResult { fn block_hash(env: &ConcreteEnv, block_height: BlockHeight, chain: Chain) -> ResponseResult {
@ -364,7 +378,7 @@ fn chain_height(env: &ConcreteEnv) -> ResponseResult {
let table_block_heights = env_inner.open_db_ro::<BlockHeights>(&tx_ro)?; let table_block_heights = env_inner.open_db_ro::<BlockHeights>(&tx_ro)?;
let table_block_infos = env_inner.open_db_ro::<BlockInfos>(&tx_ro)?; let table_block_infos = env_inner.open_db_ro::<BlockInfos>(&tx_ro)?;
let chain_height = crate::ops::blockchain::chain_height(&table_block_heights)?; let chain_height = blockchain::chain_height(&table_block_heights)?;
let block_hash = let block_hash =
get_block_info(&chain_height.saturating_sub(1), &table_block_infos)?.block_hash; get_block_info(&chain_height.saturating_sub(1), &table_block_infos)?.block_hash;

View file

@ -608,7 +608,10 @@ define_request_and_response! {
r#"{ r#"{
"jsonrpc": "2.0", "jsonrpc": "2.0",
"id": "0", "id": "0",
"method": "hard_fork_info" "method": "hard_fork_info",
"params": {
"version": 16
}
}"#; }"#;
Response = Response =
r#"{ r#"{

View file

@ -11,7 +11,10 @@ use std::{
use monero_serai::block::Block; use monero_serai::block::Block;
use crate::types::{Chain, ExtendedBlockHeader, OutputOnChain, VerifiedBlockInformation}; use crate::{
types::{Chain, ExtendedBlockHeader, OutputOnChain, VerifiedBlockInformation},
HardFork,
};
//---------------------------------------------------------------------------------------------------- ReadRequest //---------------------------------------------------------------------------------------------------- ReadRequest
/// A read request to the blockchain database. /// A read request to the blockchain database.
@ -53,6 +56,9 @@ pub enum BlockchainReadRequest {
/// TODO /// TODO
TopBlockFull, TopBlockFull,
/// TODO
CurrentHardFork,
/// Request a block's hash. /// Request a block's hash.
/// ///
/// The input is the block's height and the chain it is on. /// The input is the block's height and the chain it is on.
@ -163,14 +169,25 @@ pub enum BlockchainWriteRequest {
pub enum BlockchainResponse { pub enum BlockchainResponse {
//------------------------------------------------------ Reads //------------------------------------------------------ Reads
/// Response to [`BlockchainReadRequest::Block`]. /// Response to [`BlockchainReadRequest::Block`].
///
/// Inner value is TODO.
Block(Block), Block(Block),
/// Response to [`BlockchainReadRequest::BlockByHash`]. /// Response to [`BlockchainReadRequest::BlockByHash`].
///
/// Inner value is TODO.
BlockByHash(Block), BlockByHash(Block),
/// Response to [`BlockchainReadRequest::TopBlock`]. /// Response to [`BlockchainReadRequest::TopBlock`].
///
/// Inner value is TODO.
TopBlock(Block), TopBlock(Block),
/// Response to [`BlockchainReadRequest::CurrentHardFork`].
///
/// Inner value is TODO.
CurrentHardFork(HardFork),
/// Response to [`BlockchainReadRequest::BlockExtendedHeader`]. /// Response to [`BlockchainReadRequest::BlockExtendedHeader`].
/// ///
/// Inner value is the extended headed of the requested block. /// Inner value is the extended headed of the requested block.

View file

@ -48,11 +48,14 @@ pub enum HardFork {
V13, V13,
V14, V14,
V15, V15,
// remember to update from_vote! // remember to update [`Self::CURRENT`]!
V16, V16,
} }
impl HardFork { impl HardFork {
/// TODO
pub const CURRENT: Self = Self::V16;
/// Returns the hard-fork for a blocks [`BlockHeader::hardfork_version`] field. /// Returns the hard-fork for a blocks [`BlockHeader::hardfork_version`] field.
/// ///
/// ref: <https://monero-book.cuprate.org/consensus_rules/hardforks.html#blocks-version-and-vote> /// ref: <https://monero-book.cuprate.org/consensus_rules/hardforks.html#blocks-version-and-vote>
@ -93,7 +96,7 @@ impl HardFork {
return Self::V1; return Self::V1;
} }
// This must default to the latest hard-fork! // This must default to the latest hard-fork!
Self::from_version(vote).unwrap_or(Self::V16) Self::from_version(vote).unwrap_or(Self::CURRENT)
} }
/// Returns the [`HardFork`] version and vote from this block header. /// Returns the [`HardFork`] version and vote from this block header.
@ -128,4 +131,9 @@ impl HardFork {
_ => BLOCK_TIME_V2, _ => BLOCK_TIME_V2,
} }
} }
/// TODO
pub const fn is_current(self) -> bool {
matches!(self, Self::CURRENT)
}
} }