Skip to content

Commit 7a6a7e0

Browse files
committed
Merge branch feature/ocean-archive into ocean-poolswap-result
2 parents 752ec92 + 24e2997 commit 7a6a7e0

9 files changed

Lines changed: 142 additions & 77 deletions

File tree

lib/ain-grpc/build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ fn compile_proto_and_generate_services(
266266
// There's no way to compile protos using custom generator in tonic,
267267
// so we're left with creating a prost config and using that for codegen.
268268
let mut config = Config::new();
269+
config.protoc_arg("--experimental_allow_proto3_optional");
269270
config.out_dir(out_dir);
270271
config.service_generator(Box::new(gen));
271272
config

lib/ain-ocean/src/api/block.rs

Lines changed: 26 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,45 @@ use axum::{
33
routing::get,
44
Json, Router,
55
};
6-
use serde::{Deserialize, Serialize};
6+
use bitcoin::BlockHash;
77

8-
use crate::{api_paged_response::ApiPagedResponse, api_query::PaginationQuery, Result};
9-
10-
#[derive(Deserialize)]
11-
struct BlockId {
12-
id: String,
13-
}
14-
15-
#[derive(Deserialize)]
16-
struct BlockHash {
17-
hash: String,
18-
}
8+
use crate::{
9+
api_paged_response::ApiPagedResponse, api_query::PaginationQuery, model::Block,
10+
repository::RepositoryOps, Result, SERVICES,
11+
};
1912

2013
async fn list_blocks(
2114
Query(query): Query<PaginationQuery>,
2215
) -> Result<Json<ApiPagedResponse<Block>>> {
23-
// TODO(): query from lvldb.. or maybe pull from index
24-
let blocks = vec![
25-
Block { id: "0".into() },
26-
Block { id: "1".into() },
27-
Block { id: "2".into() },
28-
];
16+
let blocks = SERVICES
17+
.block
18+
.by_height
19+
.list(None)?
20+
.take(query.size)
21+
.map(|item| {
22+
let (_, id) = item?;
23+
let b = SERVICES
24+
.block
25+
.by_id
26+
.get(&id)?
27+
.ok_or("Missing block index")?;
28+
29+
Ok(b)
30+
})
31+
.collect::<Result<Vec<_>>>()?;
2932

3033
Ok(Json(ApiPagedResponse::of(blocks, query.size, |block| {
3134
block.clone().id
3235
})))
3336
}
3437

35-
async fn get_block(Path(BlockId { id }): Path<BlockId>) -> Result<Json<Block>> {
36-
Ok(Json(Block { id }))
38+
async fn get_block(Path(id): Path<BlockHash>) -> Result<Json<Option<Block>>> {
39+
let block = SERVICES.block.by_id.get(&id)?;
40+
41+
Ok(Json(block))
3742
}
3843

39-
async fn get_transactions(Path(BlockHash { hash }): Path<BlockHash>) -> String {
44+
async fn get_transactions(Path(hash): Path<BlockHash>) -> String {
4045
format!("Transactions for block with hash {}", hash)
4146
}
4247

@@ -46,33 +51,3 @@ pub fn router() -> Router {
4651
.route("/:id", get(get_block))
4752
.route("/:hash/transactions", get(get_transactions))
4853
}
49-
50-
#[derive(Clone, Debug, Serialize)]
51-
#[serde(default)]
52-
pub struct Block {
53-
id: String,
54-
// TODO(): type mapping
55-
// hash: H256,
56-
// previous_hash: H256,
57-
58-
// height: u64,
59-
// version: u64,
60-
// time: u64, // ---------------| block time in seconds since epoch
61-
// median_time: u64, // --------| meidan time of the past 11 block timestamps
62-
63-
// transaction_count: u64,
64-
65-
// difficulty: u64,
66-
67-
// masternode: H256,
68-
// minter: H256,
69-
// minter_block_count: u64,
70-
// reward: f64
71-
72-
// state_modifier: H256,
73-
// merkle_root: H256,
74-
75-
// size: u64,
76-
// size_stripped: u64,
77-
// weight: u64,
78-
}

lib/ain-ocean/src/indexer/mod.rs

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,68 @@ pub mod tx_result;
77
use dftx_rs::{deserialize, Block, DfTx, Transaction};
88
use log::debug;
99

10-
use crate::{model::BlockContext, Result};
10+
use crate::{
11+
model::{Block as BlockMapper, BlockContext},
12+
repository::RepositoryOps,
13+
Result, SERVICES,
14+
};
1115

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

17-
pub fn index_block(block: String, block_height: u32) -> Result<()> {
21+
pub struct BlockV2Info {
22+
pub height: u32,
23+
pub difficulty: u32,
24+
pub version: i32,
25+
pub median_time: i64,
26+
pub minter_block_count: u64,
27+
pub size: usize,
28+
pub size_stripped: usize,
29+
pub weight: i64,
30+
pub stake_modifier: String,
31+
pub minter: String,
32+
pub masternode: String,
33+
}
34+
35+
pub fn index_block(encoded_block: String, info: &BlockV2Info) -> Result<()> {
1836
debug!("[index_block] Indexing block...");
1937

20-
let hex = hex::decode(block)?;
38+
let hex = hex::decode(&encoded_block)?;
2139
debug!("got hex");
2240
let block = deserialize::<Block>(&hex)?;
2341
debug!("got block");
42+
let block_hash = block.block_hash();
2443
let ctx = BlockContext {
25-
height: block_height,
26-
hash: block.block_hash(),
44+
height: info.height,
45+
hash: block_hash,
2746
time: 0, // TODO
2847
median_time: 0, // TODO
2948
};
49+
let block_mapper = BlockMapper {
50+
id: block_hash.to_string(),
51+
hash: block_hash.to_string(),
52+
previous_hash: block.header.prev_blockhash.to_string(),
53+
height: info.height,
54+
version: info.version,
55+
time: block.header.time,
56+
median_time: info.median_time,
57+
transaction_count: block.txdata.len(),
58+
difficulty: info.difficulty,
59+
masternode: info.masternode.to_owned(),
60+
minter: info.minter.to_owned(),
61+
minter_block_count: info.minter_block_count,
62+
stake_modifier: info.stake_modifier.to_owned(),
63+
merkleroot: block.header.merkle_root.to_string(),
64+
size: info.size,
65+
size_stripped: info.size_stripped,
66+
weight: info.weight,
67+
};
68+
69+
SERVICES.block.raw.put(&ctx.hash, &encoded_block)?;
70+
SERVICES.block.by_id.put(&ctx.hash, &block_mapper)?;
71+
SERVICES.block.by_height.put(&ctx.height, &block_hash)?;
3072

3173
for (idx, tx) in block.txdata.into_iter().enumerate() {
3274
let bytes = tx.output[0].script_pubkey.as_bytes();
@@ -60,6 +102,6 @@ pub fn index_block(block: String, block_height: u32) -> Result<()> {
60102
Ok(())
61103
}
62104

63-
pub fn invalidate_block(block: String, block_height: u32) -> Result<()> {
105+
pub fn invalidate_block(block: String, info: &BlockV2Info) -> Result<()> {
64106
Ok(())
65107
}

lib/ain-ocean/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::{path::PathBuf, sync::Arc};
77

88
pub use api::ocean_router;
99
use error::OceanError;
10-
pub use indexer::{index_block, invalidate_block, tx_result};
10+
pub use indexer::{index_block, invalidate_block, tx_result, BlockV2Info};
1111
use repository::{
1212
AuctionHistoryByHeightRepository, AuctionHistoryRepository, BlockByHeightRepository,
1313
BlockRepository, MasternodeByHeightRepository, MasternodeRepository, MasternodeStatsRepository,

lib/ain-ocean/src/model/block.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,20 @@ pub struct Block {
77
pub id: String,
88
pub hash: String,
99
pub previous_hash: String,
10-
pub height: i32,
10+
pub height: u32,
1111
pub version: i32,
12-
pub time: i32,
13-
pub median_time: i32,
14-
pub transaction_count: i32,
15-
pub difficulty: i32,
12+
pub time: u32,
13+
pub median_time: i64,
14+
pub transaction_count: usize,
15+
pub difficulty: u32,
1616
pub masternode: String,
1717
pub minter: String,
18-
pub minter_block_count: i32,
19-
pub reward: String,
18+
pub minter_block_count: u64,
2019
pub stake_modifier: String,
2120
pub merkleroot: String,
22-
pub size: i32,
23-
pub size_stripped: i32,
24-
pub weight: i32,
21+
pub size: usize,
22+
pub size_stripped: usize,
23+
pub weight: i64,
2524
}
2625

2726
#[derive(Serialize, Deserialize, Debug, Clone)]

lib/ain-ocean/src/storage/columns/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,9 @@ pub use transaction_vout::*;
4343
pub use tx_result::*;
4444
pub use vault_auction_history::*;
4545

46-
pub const COLUMN_NAMES: [&'static str; 23] = [
46+
pub const COLUMN_NAMES: [&'static str; 24] = [
4747
block::Block::NAME,
48+
block::BlockByHeight::NAME,
4849
masternode_stats::MasternodeStats::NAME,
4950
masternode::Masternode::NAME,
5051
masternode::MasternodeByHeight::NAME,

lib/ain-rs-exports/src/lib.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,21 @@ pub mod ffi {
5252
fn ain_rs_stop_network_services(result: &mut CrossBoundaryResult);
5353
}
5454

55+
#[derive(Default)]
56+
pub struct BlockV2Info {
57+
pub height: u32,
58+
pub difficulty: u32,
59+
pub version: i32,
60+
pub median_time: i64,
61+
pub minter_block_count: u64,
62+
pub size: usize,
63+
pub size_stripped: usize,
64+
pub weight: i64,
65+
pub stake_modifier: String,
66+
pub minter: String,
67+
pub masternode: String,
68+
}
69+
5570
// ========== Block ==========
5671
#[derive(Default)]
5772
pub struct EVMBlockHeader {
@@ -336,11 +351,11 @@ pub mod ffi {
336351
raw_tx: &str,
337352
);
338353

339-
fn ocean_index_block(result: &mut CrossBoundaryResult, block: String, block_height: u32);
354+
fn ocean_index_block(result: &mut CrossBoundaryResult, block: String, info: &BlockV2Info);
340355
fn ocean_invalidate_block(
341356
result: &mut CrossBoundaryResult,
342357
block: String,
343-
block_height: u32,
358+
info: &BlockV2Info,
344359
);
345360

346361
fn ocean_try_set_tx_result(

lib/ain-rs-exports/src/ocean.rs

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,41 @@
11
use ain_macros::ffi_fallible;
2-
use ain_ocean::Result;
2+
use ain_ocean::{BlockV2Info, Result};
33

44
use crate::{
55
ffi,
6+
ffi::BlockV2Info as BlockV2InfoFFI,
67
prelude::{cross_boundary_error_return, cross_boundary_success_return},
78
};
89

10+
// manually convert since BlockV2InfoFFI is belongs to CPP which can't impl From -> Into
11+
pub fn convert(b: &BlockV2InfoFFI) -> BlockV2Info {
12+
BlockV2Info {
13+
height: b.height,
14+
difficulty: b.difficulty,
15+
version: b.version,
16+
median_time: b.median_time,
17+
minter_block_count: b.minter_block_count,
18+
size: b.size,
19+
size_stripped: b.size_stripped,
20+
weight: b.weight,
21+
stake_modifier: b.stake_modifier.to_owned(),
22+
minter: b.minter.to_owned(),
23+
masternode: b.masternode.to_owned(),
24+
}
25+
}
26+
927
#[ffi_fallible]
10-
pub fn ocean_index_block(block: String, block_height: u32) -> Result<()> {
11-
ain_ocean::index_block(block, block_height)
28+
pub fn ocean_index_block(block: String, b: &BlockV2InfoFFI) -> Result<()> {
29+
ain_ocean::index_block(block, &convert(b))
1230
}
1331

1432
#[ffi_fallible]
15-
pub fn ocean_invalidate_block(block: String, block_height: u32) -> Result<()> {
16-
ain_ocean::invalidate_block(block, block_height)
33+
pub fn ocean_invalidate_block(block: String, b: &BlockV2InfoFFI) -> Result<()> {
34+
ain_ocean::invalidate_block(block, &convert(b))
1735
}
1836

1937
#[ffi_fallible]
2038
fn ocean_try_set_tx_result(tx_type: u8, tx_hash: [u8; 32], result_ptr: usize) -> Result<()> {
2139
ain_ocean::tx_result::index(tx_type, tx_hash, result_ptr)
2240
}
41+
t

src/dfi/validation.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <ffi/ffiexports.h>
2222
#include <ffi/ffihelpers.h>
2323
#include <validation.h>
24+
#include <consensus/validation.h>
2425

2526
#include <boost/asio.hpp>
2627

@@ -2799,7 +2800,19 @@ Res ProcessDeFiEventFallible(const CBlock &block,
27992800
std::string serializedData = HexStr(ss.begin(), ss.end());
28002801

28012802
CrossBoundaryResult result;
2802-
ocean_index_block(result, serializedData, pindex->nHeight);
2803+
BlockV2Info info;
2804+
info.height = pindex->nHeight;
2805+
info.difficulty = pindex->nBits;
2806+
info.version = pindex->nVersion;
2807+
info.median_time = (int64_t)pindex->GetMedianTimePast();
2808+
info.minter_block_count = pindex->mintedBlocks;
2809+
info.size = GetSerializeSize(block, PROTOCOL_VERSION);
2810+
info.size_stripped = GetSerializeSize(block, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS);
2811+
info.weight = GetBlockWeight(block);
2812+
info.stake_modifier = pindex->stakeModifier.ToString();
2813+
info.minter = "", // mn operator address
2814+
info.masternode = "", // mn owner address
2815+
ocean_index_block(result, serializedData, info);
28032816
// if (!result.ok) {
28042817
// return Res::Err(result.reason.c_str());
28052818
// }

0 commit comments

Comments
 (0)