Skip to content

Commit

Permalink
Merge branch feature/ocean-archive into ocean-poolswap-result
Browse files Browse the repository at this point in the history
  • Loading branch information
Jouzo committed Jan 12, 2024
2 parents 752ec92 + 24e2997 commit 78218ae
Show file tree
Hide file tree
Showing 9 changed files with 141 additions and 77 deletions.
1 change: 1 addition & 0 deletions lib/ain-grpc/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
77 changes: 26 additions & 51 deletions lib/ain-ocean/src/api/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,45 @@ use axum::{
routing::get,
Json, Router,
};
use serde::{Deserialize, Serialize};
use bitcoin::BlockHash;

use crate::{api_paged_response::ApiPagedResponse, api_query::PaginationQuery, Result};

#[derive(Deserialize)]
struct BlockId {
id: String,
}

#[derive(Deserialize)]
struct BlockHash {
hash: String,
}
use crate::{
api_paged_response::ApiPagedResponse, api_query::PaginationQuery, model::Block,
repository::RepositoryOps, Result, SERVICES,
};

async fn list_blocks(
Query(query): Query<PaginationQuery>,
) -> Result<Json<ApiPagedResponse<Block>>> {
// 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(b)
})
.collect::<Result<Vec<_>>>()?;

Ok(Json(ApiPagedResponse::of(blocks, query.size, |block| {
block.clone().id
})))
}

async fn get_block(Path(BlockId { id }): Path<BlockId>) -> Result<Json<Block>> {
Ok(Json(Block { id }))
async fn get_block(Path(id): Path<BlockHash>) -> Result<Json<Option<Block>>> {
let block = SERVICES.block.by_id.get(&id)?;

Ok(Json(block))
}

async fn get_transactions(Path(BlockHash { hash }): Path<BlockHash>) -> String {
async fn get_transactions(Path(hash): Path<BlockHash>) -> String {
format!("Transactions for block with hash {}", hash)
}

Expand All @@ -46,33 +51,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,
}
54 changes: 48 additions & 6 deletions lib/ain-ocean/src/indexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,68 @@ pub mod tx_result;
use dftx_rs::{deserialize, Block, DfTx, Transaction};
use log::debug;

use crate::{model::BlockContext, Result};
use crate::{
model::{Block as BlockMapper, BlockContext},
repository::RepositoryOps,
Result, SERVICES,
};

pub(crate) trait Index {
fn index(&self, ctx: &BlockContext, tx: Transaction, idx: usize) -> Result<()>;
fn invalidate(&self, context: &BlockContext, tx: Transaction, idx: usize) -> Result<()>;
}

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 stake_modifier: String,
pub minter: String,
pub masternode: 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::<Block>(&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: info.stake_modifier.to_owned(),
merkleroot: block.header.merkle_root.to_string(),
size: info.size,
size_stripped: info.size_stripped,
weight: info.weight,
};

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();
Expand Down Expand Up @@ -60,6 +102,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(())
}
2 changes: 1 addition & 1 deletion lib/ain-ocean/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::{path::PathBuf, sync::Arc};

pub use api::ocean_router;
use error::OceanError;
pub use indexer::{index_block, invalidate_block, tx_result};
pub use indexer::{index_block, invalidate_block, tx_result, BlockV2Info};
use repository::{
AuctionHistoryByHeightRepository, AuctionHistoryRepository, BlockByHeightRepository,
BlockRepository, MasternodeByHeightRepository, MasternodeRepository, MasternodeStatsRepository,
Expand Down
19 changes: 9 additions & 10 deletions lib/ain-ocean/src/model/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,20 @@ 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,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
Expand Down
3 changes: 2 additions & 1 deletion lib/ain-ocean/src/storage/columns/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ pub use transaction_vout::*;
pub use tx_result::*;
pub use vault_auction_history::*;

pub const COLUMN_NAMES: [&'static str; 23] = [
pub const COLUMN_NAMES: [&'static str; 24] = [
block::Block::NAME,
block::BlockByHeight::NAME,
masternode_stats::MasternodeStats::NAME,
masternode::Masternode::NAME,
masternode::MasternodeByHeight::NAME,
Expand Down
19 changes: 17 additions & 2 deletions lib/ain-rs-exports/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 stake_modifier: String,
pub minter: String,
pub masternode: String,
}

// ========== Block ==========
#[derive(Default)]
pub struct EVMBlockHeader {
Expand Down Expand Up @@ -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,
);

fn ocean_try_set_tx_result(
Expand Down
28 changes: 23 additions & 5 deletions lib/ain-rs-exports/src/ocean.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,37 @@
use ain_macros::ffi_fallible;
use ain_ocean::Result;
use ain_ocean::{BlockV2Info, Result};

use crate::{
ffi,
ffi::BlockV2Info as BlockV2InfoFFI,
prelude::{cross_boundary_error_return, cross_boundary_success_return},
};

// 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,
stake_modifier: b.stake_modifier.to_owned(),
minter: b.minter.to_owned(),
masternode: b.masternode.to_owned(),
}
}

#[ffi_fallible]
pub fn ocean_index_block(block: String, block_height: u32) -> Result<()> {
ain_ocean::index_block(block, block_height)
pub fn ocean_index_block(block: String, b: &BlockV2InfoFFI) -> Result<()> {
ain_ocean::index_block(block, &convert(b))
}

#[ffi_fallible]
pub fn ocean_invalidate_block(block: String, block_height: u32) -> Result<()> {
ain_ocean::invalidate_block(block, block_height)
pub fn ocean_invalidate_block(block: String, b: &BlockV2InfoFFI) -> Result<()> {
ain_ocean::invalidate_block(block, &convert(b))
}

#[ffi_fallible]
Expand Down
15 changes: 14 additions & 1 deletion src/dfi/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <ffi/ffiexports.h>
#include <ffi/ffihelpers.h>
#include <validation.h>
#include <consensus/validation.h>

#include <boost/asio.hpp>

Expand Down Expand Up @@ -2799,7 +2800,19 @@ 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 = "", // mn operator address
info.masternode = "", // mn owner address
ocean_index_block(result, serializedData, info);
// if (!result.ok) {
// return Res::Err(result.reason.c_str());
// }
Expand Down

0 comments on commit 78218ae

Please sign in to comment.