From 2599e67899ed98c23dcc727b62961d51bf167809 Mon Sep 17 00:00:00 2001 From: canonbrother Date: Thu, 4 Jan 2024 16:01:25 +0800 Subject: [PATCH 1/7] experimental_allow_proto3_optional --- lib/ain-grpc/build.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/ain-grpc/build.rs b/lib/ain-grpc/build.rs index 2594bf2e39..2f7ed5f644 100644 --- a/lib/ain-grpc/build.rs +++ b/lib/ain-grpc/build.rs @@ -266,6 +266,7 @@ fn compile_proto_and_generate_services( // There's no way to compile protos using custom generator in tonic, // so we're left with creating a prost config and using that for codegen. let mut config = Config::new(); + config.protoc_arg("--experimental_allow_proto3_optional"); config.out_dir(out_dir); config.service_generator(Box::new(gen)); config From fb88cafd1047c3b8955926d3010a14ff05d27528 Mon Sep 17 00:00:00 2001 From: canonbrother Date: Thu, 11 Jan 2024 01:04:21 +0800 Subject: [PATCH 2/7] ffi blockv2info --- lib/ain-ocean/src/indexer/mod.rs | 57 ++++++++++++++++++++++++++++---- lib/ain-ocean/src/lib.rs | 2 +- lib/ain-ocean/src/model/block.rs | 20 +++++------ lib/ain-rs-exports/src/lib.rs | 19 +++++++++-- lib/ain-rs-exports/src/ocean.rs | 28 +++++++++++++--- src/dfi/validation.cpp | 16 ++++++++- 6 files changed, 117 insertions(+), 25 deletions(-) diff --git a/lib/ain-ocean/src/indexer/mod.rs b/lib/ain-ocean/src/indexer/mod.rs index 8f31056fa6..3ac3520bef 100644 --- a/lib/ain-ocean/src/indexer/mod.rs +++ b/lib/ain-ocean/src/indexer/mod.rs @@ -13,21 +13,66 @@ pub(crate) trait Index { use dftx_rs::{deserialize, Block, DfTx}; use log::debug; -use crate::{model::BlockContext, Result}; +use crate::{ + model::{BlockContext, Block as BlockMapper}, + repository::RepositoryOps, + Result, + SERVICES, +}; -pub fn index_block(block: String, block_height: u32) -> Result<()> { +pub struct BlockV2Info { + pub height: u32, + pub difficulty: u32, + pub version: i32, + pub median_time: i64, + pub minter_block_count: u64, + pub size: usize, + pub size_stripped: usize, + pub weight: i64, + pub minter: String, + pub masternode: String, + pub reward: String, +} + +pub fn index_block(encoded_block: String, info: &BlockV2Info) -> Result<()> { debug!("[index_block] Indexing block..."); - let hex = hex::decode(block)?; + let hex = hex::decode(&encoded_block)?; debug!("got hex"); let block = deserialize::(&hex)?; debug!("got block"); + let block_hash = block.block_hash(); let ctx = BlockContext { - height: block_height, - hash: block.block_hash(), + height: info.height, + hash: block_hash, time: 0, // TODO median_time: 0, // TODO }; + let block_mapper = BlockMapper { + id: block_hash.to_string(), + hash: block_hash.to_string(), + previous_hash: block.header.prev_blockhash.to_string(), + height: info.height, + version: info.version, + time: block.header.time, + median_time: info.median_time, + transaction_count: block.txdata.len(), + difficulty: info.difficulty, + masternode: info.masternode.to_owned(), + minter: info.minter.to_owned(), + minter_block_count: info.minter_block_count, + // stake_modifier: String::from_utf8(block.header.stake_modifier.to_vec()).unwrap(), // TODO + stake_modifier: "".to_string(), + merkleroot: block.header.merkle_root.to_string(), + size: info.size, + size_stripped: info.size_stripped, + weight: info.weight, + reward: info.reward.to_owned(), + }; + + SERVICES.block.raw.put(&ctx.hash, &encoded_block)?; + SERVICES.block.by_id.put(&ctx.hash, &block_mapper)?; + SERVICES.block.by_height.put(&ctx.height, &block_hash)?; for (idx, tx) in block.txdata.into_iter().enumerate() { let bytes = tx.output[0].script_pubkey.as_bytes(); @@ -60,6 +105,6 @@ pub fn index_block(block: String, block_height: u32) -> Result<()> { Ok(()) } -pub fn invalidate_block(block: String, block_height: u32) -> Result<()> { +pub fn invalidate_block(block: String, info: &BlockV2Info) -> Result<()> { Ok(()) } diff --git a/lib/ain-ocean/src/lib.rs b/lib/ain-ocean/src/lib.rs index 2b567c7d47..2f170c8d95 100644 --- a/lib/ain-ocean/src/lib.rs +++ b/lib/ain-ocean/src/lib.rs @@ -6,7 +6,7 @@ mod indexer; use std::{path::PathBuf, sync::Arc}; pub use api::ocean_router; -pub use indexer::{index_block, invalidate_block}; +pub use indexer::{BlockV2Info, index_block, invalidate_block}; use repository::{ AuctionHistoryByHeightRepository, AuctionHistoryRepository, BlockByHeightRepository, BlockRepository, MasternodeByHeightRepository, MasternodeRepository, MasternodeStatsRepository, diff --git a/lib/ain-ocean/src/model/block.rs b/lib/ain-ocean/src/model/block.rs index f302d91201..559030f00f 100644 --- a/lib/ain-ocean/src/model/block.rs +++ b/lib/ain-ocean/src/model/block.rs @@ -7,21 +7,21 @@ pub struct Block { pub id: String, pub hash: String, pub previous_hash: String, - pub height: i32, + pub height: u32, pub version: i32, - pub time: i32, - pub median_time: i32, - pub transaction_count: i32, - pub difficulty: i32, + pub time: u32, + pub median_time: i64, + pub transaction_count: usize, + pub difficulty: u32, pub masternode: String, pub minter: String, - pub minter_block_count: i32, - pub reward: String, + pub minter_block_count: u64, pub stake_modifier: String, pub merkleroot: String, - pub size: i32, - pub size_stripped: i32, - pub weight: i32, + pub size: usize, + pub size_stripped: usize, + pub weight: i64, + pub reward: String, } #[derive(Serialize, Deserialize, Debug, Clone)] diff --git a/lib/ain-rs-exports/src/lib.rs b/lib/ain-rs-exports/src/lib.rs index 22ec6a5b7e..39b9017f53 100644 --- a/lib/ain-rs-exports/src/lib.rs +++ b/lib/ain-rs-exports/src/lib.rs @@ -52,6 +52,21 @@ pub mod ffi { fn ain_rs_stop_network_services(result: &mut CrossBoundaryResult); } + #[derive(Default)] + pub struct BlockV2Info { + pub height: u32, + pub difficulty: u32, + pub version: i32, + pub median_time: i64, + pub minter_block_count: u64, + pub size: usize, + pub size_stripped: usize, + pub weight: i64, + pub minter: String, + pub masternode: String, + pub reward: String, + } + // ========== Block ========== #[derive(Default)] pub struct EVMBlockHeader { @@ -336,11 +351,11 @@ pub mod ffi { raw_tx: &str, ); - fn ocean_index_block(result: &mut CrossBoundaryResult, block: String, block_height: u32); + fn ocean_index_block(result: &mut CrossBoundaryResult, block: String, info: &BlockV2Info); fn ocean_invalidate_block( result: &mut CrossBoundaryResult, block: String, - block_height: u32, + info: &BlockV2Info, ); } diff --git a/lib/ain-rs-exports/src/ocean.rs b/lib/ain-rs-exports/src/ocean.rs index eb2b4a0c02..369d23ef26 100644 --- a/lib/ain-rs-exports/src/ocean.rs +++ b/lib/ain-rs-exports/src/ocean.rs @@ -1,7 +1,25 @@ -use crate::ffi::CrossBoundaryResult; +use crate::ffi::{BlockV2Info as BlockV2InfoFFI, CrossBoundaryResult}; +use ain_ocean::BlockV2Info; -pub fn ocean_index_block(result: &mut CrossBoundaryResult, block: String, block_height: u32) { - match ain_ocean::index_block(block, block_height) { +// manually convert since BlockV2InfoFFI is belongs to CPP which can't impl From -> Into +pub fn convert(b: &BlockV2InfoFFI) -> BlockV2Info { + BlockV2Info { + height: b.height, + difficulty: b.difficulty, + version: b.version, + median_time: b.median_time, + minter_block_count: b.minter_block_count, + size: b.size, + size_stripped: b.size_stripped, + weight: b.weight, + minter: b.minter.to_owned(), + masternode: b.masternode.to_owned(), + reward: b.reward.to_owned(), + } +} + +pub fn ocean_index_block(result: &mut CrossBoundaryResult, block: String, b: &BlockV2InfoFFI) { + match ain_ocean::index_block(block, &convert(b)) { Ok(()) => result.ok = true, Err(e) => { result.ok = false; @@ -10,8 +28,8 @@ pub fn ocean_index_block(result: &mut CrossBoundaryResult, block: String, block_ } } -pub fn ocean_invalidate_block(result: &mut CrossBoundaryResult, block: String, block_height: u32) { - match ain_ocean::invalidate_block(block, block_height) { +pub fn ocean_invalidate_block(result: &mut CrossBoundaryResult, block: String, b: &BlockV2InfoFFI) { + match ain_ocean::invalidate_block(block, &convert(b)) { Ok(()) => result.ok = true, Err(e) => { result.ok = false; diff --git a/src/dfi/validation.cpp b/src/dfi/validation.cpp index 54b477eba9..b9dffa7fe0 100644 --- a/src/dfi/validation.cpp +++ b/src/dfi/validation.cpp @@ -21,6 +21,7 @@ #include #include #include +#include #include @@ -2799,7 +2800,20 @@ Res ProcessDeFiEventFallible(const CBlock &block, std::string serializedData = HexStr(ss.begin(), ss.end()); CrossBoundaryResult result; - ocean_index_block(result, serializedData, pindex->nHeight); + BlockV2Info info; + info.height = pindex->nHeight; + info.difficulty = pindex->nBits; + info.version = pindex->nVersion; + info.median_time = (int64_t)pindex->GetMedianTimePast(); + info.minter_block_count = pindex->mintedBlocks; + info.size = GetSerializeSize(block, PROTOCOL_VERSION); + info.size_stripped = GetSerializeSize(block, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS); + info.weight = GetBlockWeight(block); + info.stake_modifier = pindex->stakeModifier.ToString(); + info.minter = "", // TODO; + info.masternode = "", // TODO; + info.reward = "", // TODO; + ocean_index_block(result, serializedData, info); // if (!result.ok) { // return Res::Err(result.reason.c_str()); // } From 0c0d93bb7b882ec2e60ed1766af9b62f763996e9 Mon Sep 17 00:00:00 2001 From: canonbrother Date: Thu, 11 Jan 2024 06:25:45 +0800 Subject: [PATCH 3/7] update ocean block apis --- lib/ain-ocean/src/api/block.rs | 87 +++++++++++++++------------------- 1 file changed, 39 insertions(+), 48 deletions(-) diff --git a/lib/ain-ocean/src/api/block.rs b/lib/ain-ocean/src/api/block.rs index 3113f8003d..d1bab75a0e 100644 --- a/lib/ain-ocean/src/api/block.rs +++ b/lib/ain-ocean/src/api/block.rs @@ -3,40 +3,61 @@ use axum::{ routing::get, Json, Router, }; +use bitcoin::BlockHash; +use log::debug; use serde::{Deserialize, Serialize}; -use crate::{api_paged_response::ApiPagedResponse, api_query::PaginationQuery, error::OceanResult}; +use crate::{ + api_paged_response::ApiPagedResponse, + api_query::PaginationQuery, + error::OceanResult, + SERVICES, + repository::RepositoryOps, + model::Block, +}; #[derive(Deserialize)] struct BlockId { id: String, } -#[derive(Deserialize)] -struct BlockHash { - hash: String, -} - async fn list_blocks( Query(query): Query, ) -> OceanResult>> { - // TODO(): query from lvldb.. or maybe pull from index - let blocks = vec![ - Block { id: "0".into() }, - Block { id: "1".into() }, - Block { id: "2".into() }, - ]; + let blocks = SERVICES + .block + .by_height + .list(None)? + .take(query.size) + .map(|item| { + let (_, id) = item?; + let b = SERVICES + .block + .by_id + .get(&id)? + .ok_or("Missing block index")?; - Ok(Json(ApiPagedResponse::of(blocks, query.size, |block| { - block.clone().id - }))) + Ok(b) + }) + .collect::>>()?; + + Ok(Json(ApiPagedResponse::of( + blocks, + query.size, + |block| block.clone().id, + ))) } -async fn get_block(Path(BlockId { id }): Path) -> OceanResult> { - Ok(Json(Block { id })) +async fn get_block(Path(id): Path) -> OceanResult>> { + let block = SERVICES + .block + .by_id + .get(&id)?; + + Ok(Json(block)) } -async fn get_transactions(Path(BlockHash { hash }): Path) -> String { +async fn get_transactions(Path(hash): Path) -> String { format!("Transactions for block with hash {}", hash) } @@ -46,33 +67,3 @@ pub fn router() -> Router { .route("/:id", get(get_block)) .route("/:hash/transactions", get(get_transactions)) } - -#[derive(Clone, Debug, Serialize)] -#[serde(default)] -pub struct Block { - id: String, - // TODO(): type mapping - // hash: H256, - // previous_hash: H256, - - // height: u64, - // version: u64, - // time: u64, // ---------------| block time in seconds since epoch - // median_time: u64, // --------| meidan time of the past 11 block timestamps - - // transaction_count: u64, - - // difficulty: u64, - - // masternode: H256, - // minter: H256, - // minter_block_count: u64, - // reward: f64 - - // state_modifier: H256, - // merkle_root: H256, - - // size: u64, - // size_stripped: u64, - // weight: u64, -} From 963cba78aa2966733e18aaa170f92eb27a9b839b Mon Sep 17 00:00:00 2001 From: canonbrother Date: Thu, 11 Jan 2024 06:53:02 +0800 Subject: [PATCH 4/7] pindex->stakeModifier --- lib/ain-ocean/src/indexer/mod.rs | 4 ++-- lib/ain-rs-exports/src/lib.rs | 1 + lib/ain-rs-exports/src/ocean.rs | 1 + 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/ain-ocean/src/indexer/mod.rs b/lib/ain-ocean/src/indexer/mod.rs index 3ac3520bef..ca7a19fdeb 100644 --- a/lib/ain-ocean/src/indexer/mod.rs +++ b/lib/ain-ocean/src/indexer/mod.rs @@ -29,6 +29,7 @@ pub struct BlockV2Info { pub size: usize, pub size_stripped: usize, pub weight: i64, + pub stake_modifier: String, pub minter: String, pub masternode: String, pub reward: String, @@ -61,8 +62,7 @@ pub fn index_block(encoded_block: String, info: &BlockV2Info) -> Result<()> { masternode: info.masternode.to_owned(), minter: info.minter.to_owned(), minter_block_count: info.minter_block_count, - // stake_modifier: String::from_utf8(block.header.stake_modifier.to_vec()).unwrap(), // TODO - stake_modifier: "".to_string(), + stake_modifier: info.stake_modifier.to_owned(), merkleroot: block.header.merkle_root.to_string(), size: info.size, size_stripped: info.size_stripped, diff --git a/lib/ain-rs-exports/src/lib.rs b/lib/ain-rs-exports/src/lib.rs index 39b9017f53..051a65e67e 100644 --- a/lib/ain-rs-exports/src/lib.rs +++ b/lib/ain-rs-exports/src/lib.rs @@ -62,6 +62,7 @@ pub mod ffi { pub size: usize, pub size_stripped: usize, pub weight: i64, + pub stake_modifier: String, pub minter: String, pub masternode: String, pub reward: String, diff --git a/lib/ain-rs-exports/src/ocean.rs b/lib/ain-rs-exports/src/ocean.rs index 369d23ef26..9535ca5024 100644 --- a/lib/ain-rs-exports/src/ocean.rs +++ b/lib/ain-rs-exports/src/ocean.rs @@ -12,6 +12,7 @@ pub fn convert(b: &BlockV2InfoFFI) -> BlockV2Info { size: b.size, size_stripped: b.size_stripped, weight: b.weight, + stake_modifier: b.stake_modifier.to_owned(), minter: b.minter.to_owned(), masternode: b.masternode.to_owned(), reward: b.reward.to_owned(), From e51f2ab363490e397a089ee93bf36ba8d3dddc12 Mon Sep 17 00:00:00 2001 From: canonbrother Date: Thu, 11 Jan 2024 07:23:09 +0800 Subject: [PATCH 5/7] rm block.reward --- lib/ain-ocean/src/indexer/mod.rs | 2 -- lib/ain-ocean/src/model/block.rs | 1 - lib/ain-rs-exports/src/lib.rs | 1 - lib/ain-rs-exports/src/ocean.rs | 1 - src/dfi/validation.cpp | 5 ++--- 5 files changed, 2 insertions(+), 8 deletions(-) diff --git a/lib/ain-ocean/src/indexer/mod.rs b/lib/ain-ocean/src/indexer/mod.rs index ca7a19fdeb..0a81fda0c7 100644 --- a/lib/ain-ocean/src/indexer/mod.rs +++ b/lib/ain-ocean/src/indexer/mod.rs @@ -32,7 +32,6 @@ pub struct BlockV2Info { pub stake_modifier: String, pub minter: String, pub masternode: String, - pub reward: String, } pub fn index_block(encoded_block: String, info: &BlockV2Info) -> Result<()> { @@ -67,7 +66,6 @@ pub fn index_block(encoded_block: String, info: &BlockV2Info) -> Result<()> { size: info.size, size_stripped: info.size_stripped, weight: info.weight, - reward: info.reward.to_owned(), }; SERVICES.block.raw.put(&ctx.hash, &encoded_block)?; diff --git a/lib/ain-ocean/src/model/block.rs b/lib/ain-ocean/src/model/block.rs index 559030f00f..273c89d9cc 100644 --- a/lib/ain-ocean/src/model/block.rs +++ b/lib/ain-ocean/src/model/block.rs @@ -21,7 +21,6 @@ pub struct Block { pub size: usize, pub size_stripped: usize, pub weight: i64, - pub reward: String, } #[derive(Serialize, Deserialize, Debug, Clone)] diff --git a/lib/ain-rs-exports/src/lib.rs b/lib/ain-rs-exports/src/lib.rs index 051a65e67e..2e404ec010 100644 --- a/lib/ain-rs-exports/src/lib.rs +++ b/lib/ain-rs-exports/src/lib.rs @@ -65,7 +65,6 @@ pub mod ffi { pub stake_modifier: String, pub minter: String, pub masternode: String, - pub reward: String, } // ========== Block ========== diff --git a/lib/ain-rs-exports/src/ocean.rs b/lib/ain-rs-exports/src/ocean.rs index 9535ca5024..1f58753c8c 100644 --- a/lib/ain-rs-exports/src/ocean.rs +++ b/lib/ain-rs-exports/src/ocean.rs @@ -15,7 +15,6 @@ pub fn convert(b: &BlockV2InfoFFI) -> BlockV2Info { stake_modifier: b.stake_modifier.to_owned(), minter: b.minter.to_owned(), masternode: b.masternode.to_owned(), - reward: b.reward.to_owned(), } } diff --git a/src/dfi/validation.cpp b/src/dfi/validation.cpp index b9dffa7fe0..d7fb8adf97 100644 --- a/src/dfi/validation.cpp +++ b/src/dfi/validation.cpp @@ -2810,9 +2810,8 @@ Res ProcessDeFiEventFallible(const CBlock &block, info.size_stripped = GetSerializeSize(block, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS); info.weight = GetBlockWeight(block); info.stake_modifier = pindex->stakeModifier.ToString(); - info.minter = "", // TODO; - info.masternode = "", // TODO; - info.reward = "", // TODO; + info.minter = "", // mn operator address + info.masternode = "", // mn owner address ocean_index_block(result, serializedData, info); // if (!result.ok) { // return Res::Err(result.reason.c_str()); From f802a7c446c27f72e799ec321f941d667b20e881 Mon Sep 17 00:00:00 2001 From: canonbrother Date: Thu, 11 Jan 2024 08:36:26 +0800 Subject: [PATCH 6/7] add missing column block_by_height --- lib/ain-ocean/src/storage/columns/mod.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/ain-ocean/src/storage/columns/mod.rs b/lib/ain-ocean/src/storage/columns/mod.rs index 1da163fbad..2283a873d1 100644 --- a/lib/ain-ocean/src/storage/columns/mod.rs +++ b/lib/ain-ocean/src/storage/columns/mod.rs @@ -41,8 +41,9 @@ pub use transaction_vin::*; pub use transaction_vout::*; pub use vault_auction_history::*; -pub const COLUMN_NAMES: [&'static str; 22] = [ +pub const COLUMN_NAMES: [&'static str; 23] = [ block::Block::NAME, + block::BlockByHeight::NAME, masternode_stats::MasternodeStats::NAME, masternode::Masternode::NAME, masternode::MasternodeByHeight::NAME, From a93c137dd4ceba71226d64859687a318730916e5 Mon Sep 17 00:00:00 2001 From: canonbrother Date: Thu, 11 Jan 2024 09:31:55 +0800 Subject: [PATCH 7/7] rm unuse --- lib/ain-ocean/src/api/block.rs | 7 ------- 1 file changed, 7 deletions(-) diff --git a/lib/ain-ocean/src/api/block.rs b/lib/ain-ocean/src/api/block.rs index d1bab75a0e..f141604ce1 100644 --- a/lib/ain-ocean/src/api/block.rs +++ b/lib/ain-ocean/src/api/block.rs @@ -4,8 +4,6 @@ use axum::{ Json, Router, }; use bitcoin::BlockHash; -use log::debug; -use serde::{Deserialize, Serialize}; use crate::{ api_paged_response::ApiPagedResponse, @@ -16,11 +14,6 @@ use crate::{ model::Block, }; -#[derive(Deserialize)] -struct BlockId { - id: String, -} - async fn list_blocks( Query(query): Query, ) -> OceanResult>> {