Skip to content

Commit 3e11241

Browse files
committed
Ocean indexing scaffold
1 parent 22acb5a commit 3e11241

34 files changed

Lines changed: 736 additions & 475 deletions

lib/Cargo.lock

Lines changed: 397 additions & 230 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ lto = true
1818
vsdb = { git = "https://github.com/defich/vsdb.git" }
1919
vsdbsled = { git = "https://github.com/defich/vsdbsled.git" }
2020
ethereum = { git = "https://github.com/defich/ethereum.git" }
21+
bitcoin = { git = "https://github.com/Jouzo/rust-bitcoin.git" }
22+
bitcoin-io = { git = "https://github.com/Jouzo/rust-bitcoin.git" }
2123

2224
[workspace.dependencies]
2325

@@ -115,3 +117,7 @@ lru = "0.12"
115117
#### Precompile dependencies
116118
sp-io = "24.0"
117119
substrate-bn = "0.6"
120+
121+
#### Ocean dependencies
122+
dftx-rs = { git = "https://github.com/Jouzo/dftx-rs.git" }
123+
bitcoin = "0.31"

lib/ain-ocean/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,6 @@ keccak-hash.workspace = true
1212
log.workspace = true
1313
serde.workspace = true
1414
thiserror.workspace = true
15+
hex.workspace = true
16+
dftx-rs.workspace = true
17+
bitcoin.workspace = true
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
use dftx_rs::{vault::PlaceAuctionBid, Transaction};
2+
3+
use super::BlockContext;
4+
use crate::indexer::{Index, Result};
5+
6+
impl Index for PlaceAuctionBid {
7+
fn index(&self, context: &BlockContext, tx: Transaction) -> Result<()> {
8+
todo!()
9+
}
10+
11+
fn invalidate(&self) {
12+
todo!()
13+
}
14+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
use std::io::Write;
2+
3+
use dftx_rs::{masternode::*, Transaction};
4+
use log::debug;
5+
6+
use super::BlockContext;
7+
use crate::{
8+
indexer::{Index, Result},
9+
model::masternode::{Masternode, MasternodeBlock},
10+
};
11+
12+
impl Index for CreateMasternode {
13+
fn index(&self, context: &BlockContext, tx: Transaction) -> Result<()> {
14+
debug!("[CreateMasternode] Indexing...");
15+
16+
let masternode = Masternode {
17+
id: tx.txid().to_string(),
18+
owner_address: tx.output[1].script_pubkey.to_hex_string(),
19+
operator_address: tx.output[1].script_pubkey.to_hex_string(),
20+
creation_height: context.height,
21+
resign_height: -1,
22+
resign_tx: None,
23+
minted_blocks: 0,
24+
timelock: self.timelock.0.unwrap_or_default(),
25+
block: MasternodeBlock {
26+
hash: context.hash.to_string(),
27+
height: context.height,
28+
time: context.time,
29+
median_time: context.median_time,
30+
},
31+
collateral: tx.output[1].value.to_string(),
32+
sort: None,
33+
history: None,
34+
};
35+
36+
let mut file = std::fs::OpenOptions::new()
37+
.write(true)
38+
.create(true)
39+
.append(true)
40+
.open("/tmp/masternode")
41+
.expect("Unable to open file");
42+
43+
writeln!(file, "{:?}", masternode).expect("Unable to write to file");
44+
45+
Ok(())
46+
}
47+
48+
fn invalidate(&self) {
49+
todo!()
50+
}
51+
}
52+
53+
impl Index for UpdateMasternode {
54+
fn index(&self, context: &BlockContext, tx: Transaction) -> Result<()> {
55+
debug!("[UpdateMasternode] Indexing...");
56+
// TODO
57+
// Get mn
58+
// Update fields
59+
Ok(())
60+
}
61+
62+
fn invalidate(&self) {
63+
// TODO
64+
// Get mn
65+
// Restore from history
66+
}
67+
}
68+
69+
impl Index for ResignMasternode {
70+
fn index(&self, context: &BlockContext, tx: Transaction) -> Result<()> {
71+
debug!("[ResignMasternode] Indexing...");
72+
// TODO
73+
// Get mn
74+
// Set resign tx and resign height
75+
Ok(())
76+
}
77+
78+
fn invalidate(&self) {
79+
// TODO
80+
// Get mn
81+
// Set resign height to -1
82+
}
83+
}

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

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
mod auction;
2+
mod masternode;
3+
mod oracle;
4+
mod pool;
5+
6+
use dftx_rs::Transaction;
7+
8+
pub(crate) type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
9+
10+
pub(crate) trait Index {
11+
fn index(&self, context: &BlockContext, tx: Transaction) -> Result<()>;
12+
fn invalidate(&self);
13+
}
14+
15+
use bitcoin::BlockHash;
16+
use dftx_rs::{deserialize, Block, DfTx};
17+
18+
pub(crate) struct BlockContext {
19+
height: u32,
20+
hash: BlockHash,
21+
time: u64,
22+
median_time: u64,
23+
}
24+
25+
pub fn index_block(block: String, block_height: u32) -> Result<()> {
26+
let hex = hex::decode(block)?;
27+
let block = deserialize::<Block>(&hex)?;
28+
29+
let context = BlockContext {
30+
height: block_height,
31+
hash: block.block_hash(),
32+
time: 0, // TODO
33+
median_time: 0, // TODO
34+
};
35+
36+
for tx in block.txdata {
37+
let bytes = tx.output[0].script_pubkey.as_bytes();
38+
if bytes.len() > 2 && bytes[0] == 0x6a && bytes[1] <= 0x4e {
39+
let offset = 1 + match bytes[1] {
40+
0x4c => 2,
41+
0x4d => 3,
42+
0x4e => 4,
43+
_ => 1,
44+
};
45+
46+
let raw_tx = &bytes[offset..];
47+
48+
let dftx = deserialize::<DfTx>(raw_tx)?;
49+
50+
match dftx {
51+
DfTx::CreateMasternode(data) => data.index(&context, tx)?,
52+
DfTx::UpdateMasternode(data) => data.index(&context, tx)?,
53+
DfTx::ResignMasternode(data) => data.index(&context, tx)?,
54+
DfTx::AppointOracle(data) => data.index(&context, tx)?,
55+
DfTx::RemoveOracle(data) => data.index(&context, tx)?,
56+
DfTx::UpdateOracle(data) => data.index(&context, tx)?,
57+
DfTx::SetOracleData(data) => data.index(&context, tx)?,
58+
DfTx::PoolSwap(data) => data.index(&context, tx)?,
59+
DfTx::CompositeSwap(data) => data.index(&context, tx)?,
60+
DfTx::PlaceAuctionBid(data) => data.index(&context, tx)?,
61+
_ => (),
62+
}
63+
}
64+
}
65+
Ok(())
66+
}
67+
68+
pub fn invalidate_block() {
69+
todo!()
70+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
use dftx_rs::{oracles::*, Transaction};
2+
3+
use super::BlockContext;
4+
use crate::indexer::{Index, Result};
5+
6+
impl Index for AppointOracle {
7+
fn index(&self, context: &BlockContext, tx: Transaction) -> Result<()> {
8+
todo!()
9+
}
10+
11+
fn invalidate(&self) {
12+
todo!()
13+
}
14+
}
15+
16+
impl Index for RemoveOracle {
17+
fn index(&self, context: &BlockContext, tx: Transaction) -> Result<()> {
18+
todo!()
19+
}
20+
21+
fn invalidate(&self) {
22+
todo!()
23+
}
24+
}
25+
26+
impl Index for UpdateOracle {
27+
fn index(&self, context: &BlockContext, tx: Transaction) -> Result<()> {
28+
todo!()
29+
}
30+
31+
fn invalidate(&self) {
32+
todo!()
33+
}
34+
}
35+
36+
impl Index for SetOracleData {
37+
fn index(&self, context: &BlockContext, tx: Transaction) -> Result<()> {
38+
todo!()
39+
}
40+
41+
fn invalidate(&self) {
42+
todo!()
43+
}
44+
}

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use dftx_rs::{pool::*, Transaction};
2+
3+
use super::BlockContext;
4+
use crate::indexer::{Index, Result};
5+
6+
impl Index for PoolSwap {
7+
fn index(&self, context: &BlockContext, tx: Transaction) -> Result<()> {
8+
todo!()
9+
}
10+
11+
fn invalidate(&self) {
12+
todo!()
13+
}
14+
}
15+
16+
impl Index for CompositeSwap {
17+
fn index(&self, context: &BlockContext, tx: Transaction) -> Result<()> {
18+
todo!()
19+
}
20+
21+
fn invalidate(&self) {
22+
todo!()
23+
}
24+
}

lib/ain-ocean/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
mod api;
22
pub mod api_paged_response;
33
pub mod error;
4+
mod indexer;
45
mod model;
56

67
pub use api::ocean_router;
8+
pub use indexer::{index_block, invalidate_block};

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
use serde::{Serialize, Deserialize};
2-
3-
#[derive(Serialize, Deserialize, Debug, Default)]
4-
#[serde(rename_all = "camelCase")]
1+
#[derive(Debug, Default)]
52
pub struct Block {
63
pub id: String,
74
pub hash: String,
@@ -22,6 +19,3 @@ pub struct Block {
2219
pub size_stripped: i32,
2320
pub weight: i32,
2421
}
25-
26-
27-

0 commit comments

Comments
 (0)