Skip to content

Commit

Permalink
test: full block sequencing
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelcr committed Sep 11, 2024
1 parent 044bec4 commit 9f32b30
Show file tree
Hide file tree
Showing 6 changed files with 387 additions and 270 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -344,49 +344,81 @@ pub fn process_block(
mod test {
use std::sync::Arc;

use chainhook_sdk::{types::OrdinalOperation, utils::Context};
use chainhook_sdk::{
types::{bitcoin::TxOut, OrdinalOperation},
utils::Context,
};

use crate::{
config::Config,
core::{
meta_protocols::brc20::cache::brc20_new_cache, new_traversals_lazy_cache,
protocol::inscription_sequencing::SequenceCursor,
},
core::{new_traversals_lazy_cache, protocol::inscription_sequencing::SequenceCursor},
db::{
blocks::open_blocks_db_with_retry, drop_sqlite_dbs, initialize_sqlite_dbs,
blocks::{insert_standardized_block, open_blocks_db_with_retry},
drop_all_dbs, initialize_sqlite_dbs,
ordinals::open_ordinals_db_rw,
},
utils::{
monitoring::PrometheusMonitoring,
test_helpers::{new_test_block, new_test_reveal_tx},
test_helpers::{TestBlockBuilder, TestTransactionBuilder, TestTxInBuilder},
},
};

use super::process_blocks;

#[test]
fn process_block_with_inscription() {
fn process_block_with_inscription_reveal() {
let ctx = Context::empty();
let config = Config::test_default();

// Create DBs
drop_sqlite_dbs(&config);
let db_conns = initialize_sqlite_dbs(&config, &ctx);
let _ = open_blocks_db_with_retry(true, &config, &ctx);

// Insert block into rocksdb

let mut next_blocks = vec![new_test_block(vec![new_test_reveal_tx()])];
let mut sequence_cursor = SequenceCursor::new(&db_conns.ordinals);
drop_all_dbs(&config);
let sqlite_dbs = initialize_sqlite_dbs(&config, &ctx);
let blocks_db = open_blocks_db_with_retry(true, &config, &ctx);

// Block 0: A coinbase tx generating the inscription sat.
let coinbase0 = TestTransactionBuilder::new()
.hash("0xa321c61c83563a377f82ef59301f2527079f6bda7c2d04f9f5954c873f42e8ac".to_string())
.build();
let block0 = TestBlockBuilder::new()
.hash("0x00000000000000000001b228f9faca9e7d11fcecff9d463bd05546ff0aa4651a".to_string())
.height(849999)
.add_transaction(coinbase0)
.build();
insert_standardized_block(&block0, &blocks_db, &ctx);

// Block 1: The actual inscription.
let coinbase1 = TestTransactionBuilder::new().build();
let tx1 = TestTransactionBuilder::new()
.hash("0xc62d436323e14cdcb91dd21cb7814fd1ac5b9ecb6e3cc6953b54c02a343f7ec9".to_string())
.add_input(
TestTxInBuilder::new()
.prev_out_block_height(849999)
.prev_out_tx_hash(
"0xa321c61c83563a377f82ef59301f2527079f6bda7c2d04f9f5954c873f42e8ac"
.to_string(),
)
.build(),
)
.add_output(TxOut {
value: 10000,
script_pubkey: "0x00145e5f0d045e441bf001584eaeca6cd84da04b1084".to_string(),
})
.build();
let block1 = TestBlockBuilder::new()
.hash("0xb61b0172d95e266c18aea0c624db987e971a5d6d4ebc2aaed85da4642d635735".to_string())
.height(850000)
.add_transaction(coinbase1)
.add_transaction(tx1)
.build();
insert_standardized_block(&block1, &blocks_db, &ctx);

let mut next_blocks = vec![block1];
let mut sequence_cursor = SequenceCursor::new(&sqlite_dbs.ordinals);
let cache_l2 = Arc::new(new_traversals_lazy_cache(2048));
let mut inscriptions_db_conn_rw =
open_ordinals_db_rw(&config.expected_cache_path(), &ctx).expect("");

let results = process_blocks(
&mut next_blocks,
&mut sequence_cursor,
&cache_l2,
&mut inscriptions_db_conn_rw,
&mut open_ordinals_db_rw(&config.expected_cache_path(), &ctx).expect(""),
&mut None,
&mut None,
&None,
Expand All @@ -395,53 +427,62 @@ mod test {
&ctx,
);

assert_eq!(results.len(), 1);
let transactions = &results[0].transactions;
assert_eq!(transactions.len(), 1);
let parsed_tx = &transactions[0];
assert_eq!(parsed_tx.metadata.ordinal_operations.len(), 1);
let result_tx = &results[0].transactions[1];
assert_eq!(result_tx.metadata.ordinal_operations.len(), 1);
let OrdinalOperation::InscriptionRevealed(reveal) =
&parsed_tx.metadata.ordinal_operations[0]
&result_tx.metadata.ordinal_operations[0]
else {
panic!();
};
assert_eq!(
reveal.inscription_id,
"b61b0172d95e266c18aea0c624db987e971a5d6d4ebc2aaed85da4642d635735i0".to_string()
"c62d436323e14cdcb91dd21cb7814fd1ac5b9ecb6e3cc6953b54c02a343f7ec9i0".to_string()
);
assert_eq!(reveal.inscription_number.jubilee, 0);
assert_eq!(reveal.content_bytes, "0x7b200a20202270223a20226272632d3230222c0a2020226f70223a20226465706c6f79222c0a2020227469636b223a20226f726469222c0a2020226d6178223a20223231303030303030222c0a2020226c696d223a202231303030220a7d".to_string());
assert_eq!(reveal.content_length, 94);
}

#[test]
fn process_block_with_brc20_inscription() {
let ctx = Context::empty();
let mut config = Config::mainnet_default();
config.storage.working_dir = "tmp".to_string();
config.meta_protocols.brc20 = true;
drop_sqlite_dbs(&config);
let mut db_conns = initialize_sqlite_dbs(&config, &ctx);
let mut next_blocks = vec![new_test_block(vec![new_test_reveal_tx()])];
let mut sequence_cursor = SequenceCursor::new(&db_conns.ordinals);
let cache_l2 = Arc::new(new_traversals_lazy_cache(2048));
let mut inscriptions_db_conn_rw =
open_ordinals_db_rw(&config.expected_cache_path(), &ctx).expect("");
let mut brc20_cache = brc20_new_cache(&config);

let _ = process_blocks(
&mut next_blocks,
&mut sequence_cursor,
&cache_l2,
&mut inscriptions_db_conn_rw,
&mut brc20_cache,
&mut db_conns.brc20,
&None,
&PrometheusMonitoring::new(),
&config,
&ctx,
assert_eq!(reveal.content_type, "text/plain;charset=utf-8".to_string());
assert_eq!(
reveal.inscriber_address,
Some("bc1qte0s6pz7gsdlqq2cf6hv5mxcfksykyyyjkdfd5".to_string())
);
assert_eq!(reveal.ordinal_number, 1971874687500000);
assert_eq!(reveal.ordinal_block_height, 849999);
assert_eq!(
reveal.satpoint_post_inscription,
"c62d436323e14cdcb91dd21cb7814fd1ac5b9ecb6e3cc6953b54c02a343f7ec9:0:0".to_string()
);

// let op = get_brc20_operations_on_block(838964, &db_conns.brc20.unwrap(), &ctx);
// assert_eq!(op.len(), 1);
}

// #[test]
// fn process_block_with_brc20_inscription() {
// let ctx = Context::empty();
// let mut config = Config::mainnet_default();
// config.storage.working_dir = "tmp".to_string();
// config.meta_protocols.brc20 = true;
// drop_sqlite_dbs(&config);
// let mut db_conns = initialize_sqlite_dbs(&config, &ctx);
// let mut next_blocks = vec![new_test_block(vec![new_test_reveal_tx()])];
// let mut sequence_cursor = SequenceCursor::new(&db_conns.ordinals);
// let cache_l2 = Arc::new(new_traversals_lazy_cache(2048));
// let mut inscriptions_db_conn_rw =
// open_ordinals_db_rw(&config.expected_cache_path(), &ctx).expect("");
// let mut brc20_cache = brc20_new_cache(&config);

// let _ = process_blocks(
// &mut next_blocks,
// &mut sequence_cursor,
// &cache_l2,
// &mut inscriptions_db_conn_rw,
// &mut brc20_cache,
// &mut db_conns.brc20,
// &None,
// &PrometheusMonitoring::new(),
// &config,
// &ctx,
// );

// // let op = get_brc20_operations_on_block(838964, &db_conns.brc20.unwrap(), &ctx);
// // assert_eq!(op.len(), 1);
// }
}
99 changes: 85 additions & 14 deletions components/ordhook-core/src/core/protocol/inscription_parsing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,45 +247,116 @@ mod test {
use std::collections::HashMap;

use chainhook_sdk::{
types::{BitcoinBlockData, BitcoinNetwork, OrdinalOperation},
bitcoin::Amount,
indexer::bitcoin::{
BitcoinBlockFullBreakdown, BitcoinTransactionFullBreakdown,
BitcoinTransactionInputFullBreakdown, BitcoinTransactionInputPrevoutFullBreakdown,
GetRawTransactionResultVinScriptSig,
},
types::{
BitcoinBlockData, BitcoinNetwork, BitcoinTransactionData,
OrdinalInscriptionTransferData, OrdinalInscriptionTransferDestination,
OrdinalOperation,
},
utils::Context,
};

use test_case::test_case;

use crate::{
config::Config,
utils::test_helpers::{
new_test_block, new_test_raw_block, new_test_reveal_raw_tx, new_test_reveal_tx,
new_test_reveal_tx_with_operation, new_test_transfer_tx_with_operation,
},
utils::test_helpers::{TestBlockBuilder, TestTransactionBuilder, TestTxInBuilder},
};

use super::{
get_inscriptions_revealed_in_block, get_inscriptions_transferred_in_block,
parse_inscriptions_and_standardize_block, parse_inscriptions_in_standardized_block,
};

#[test_case(&new_test_block(vec![]) => 0; "with empty block")]
#[test_case(&new_test_block(vec![new_test_reveal_tx_with_operation()]) => 1; "with reveal transaction")]
#[test_case(&new_test_block(vec![new_test_transfer_tx_with_operation()]) => 0; "with transfer transaction")]
pub fn new_test_transfer_tx_with_operation() -> BitcoinTransactionData {
TestTransactionBuilder::new()
.ordinal_operations(vec![OrdinalOperation::InscriptionTransferred(
OrdinalInscriptionTransferData {
ordinal_number: 300144140535834,
destination: OrdinalInscriptionTransferDestination::Transferred(
"bc1pcwway0ne322s0lrc5e905f3chuclvnyy3z6wn86azkgmgcprf3tqvyy7ws"
.to_string(),
),
satpoint_pre_transfer:
"ab2683db34e335c89a5c1d634e6c5bd8d8bca8ded281be84f71f921c9e8783b2:0:0"
.to_string(),
satpoint_post_transfer:
"42fa098abab8d5cca1c303a97bd0404cf8e9b8faaab6dd228a309e66daff8fae:1:0"
.to_string(),
post_transfer_output_value: Some(546),
tx_index: 54,
},
)])
.build()
}

pub fn new_test_raw_block(
transactions: Vec<BitcoinTransactionFullBreakdown>,
) -> BitcoinBlockFullBreakdown {
BitcoinBlockFullBreakdown {
hash: "000000000000000000018ddf8a6484db391fb85c9f9ddc384f03a92729423aaf".to_string(),
height: 838964,
tx: transactions,
time: 1712982301,
nonce: 100,
previousblockhash: Some(
"000000000000000000021f8b96d34c0f223281d7d825dd3588c2858c96e689d4".to_string(),
),
confirmations: 200,
}
}

pub fn new_test_reveal_raw_tx() -> BitcoinTransactionFullBreakdown {
BitcoinTransactionFullBreakdown {
txid: "b61b0172d95e266c18aea0c624db987e971a5d6d4ebc2aaed85da4642d635735".to_string(),
vin: vec![BitcoinTransactionInputFullBreakdown {
sequence: 4294967293,
txid: Some("a321c61c83563a377f82ef59301f2527079f6bda7c2d04f9f5954c873f42e8ac".to_string()),
vout: Some(0),
script_sig: Some(GetRawTransactionResultVinScriptSig { hex: "".to_string()}),
txinwitness: Some(vec![
"6c00eb3c4d35fedd257051333b4ca81d1a25a37a9af4891f1fec2869edd56b14180eafbda8851d63138a724c9b15384bc5f0536de658bd294d426a36212e6f08".to_string(),
"209e2849b90a2353691fccedd467215c88eec89a5d0dcf468e6cf37abed344d746ac0063036f7264010118746578742f706c61696e3b636861727365743d7574662d38004c5e7b200a20202270223a20226272632d3230222c0a2020226f70223a20226465706c6f79222c0a2020227469636b223a20226f726469222c0a2020226d6178223a20223231303030303030222c0a2020226c696d223a202231303030220a7d68".to_string(),
"c19e2849b90a2353691fccedd467215c88eec89a5d0dcf468e6cf37abed344d746".to_string(),
]),
prevout: Some(
BitcoinTransactionInputPrevoutFullBreakdown { height: 779878, value: Amount::from_sat(14830) }
),
}],
vout: vec![],
}
}

#[test_case(&TestBlockBuilder::new().build() => 0; "with empty block")]
#[test_case(&TestBlockBuilder::new().transactions(vec![TestTransactionBuilder::new_with_operation().build()]).build() => 1; "with reveal transaction")]
#[test_case(&TestBlockBuilder::new().transactions(vec![new_test_transfer_tx_with_operation()]).build() => 0; "with transfer transaction")]
fn gets_reveals_in_block(block: &BitcoinBlockData) -> usize {
get_inscriptions_revealed_in_block(block).len()
}

#[test_case(&new_test_block(vec![]) => 0; "with empty block")]
#[test_case(&new_test_block(vec![new_test_reveal_tx_with_operation()]) => 0; "with reveal transaction")]
#[test_case(&new_test_block(vec![new_test_transfer_tx_with_operation()]) => 1; "with transfer transaction")]
#[test_case(&TestBlockBuilder::new().build() => 0; "with empty block")]
#[test_case(&TestBlockBuilder::new().transactions(vec![TestTransactionBuilder::new_with_operation().build()]).build() => 0; "with reveal transaction")]
#[test_case(&TestBlockBuilder::new().transactions(vec![new_test_transfer_tx_with_operation()]).build() => 1; "with transfer transaction")]
fn gets_transfers_in_block(block: &BitcoinBlockData) -> usize {
get_inscriptions_transferred_in_block(block).len()
}

#[test]
fn parses_inscriptions_in_block() {
let mut block = new_test_block(vec![new_test_reveal_tx()]);
let ctx = Context::empty();
let mut config = Config::mainnet_default();
config.storage.working_dir = "tmp".to_string();
let config = Config::test_default();
let mut block = TestBlockBuilder::new()
.add_transaction(
TestTransactionBuilder::new()
.add_input(TestTxInBuilder::new().build())
.build(),
)
.build();
parse_inscriptions_in_standardized_block(&mut block, &mut HashMap::new(), &config, &ctx);
let OrdinalOperation::InscriptionRevealed(reveal) =
&block.transactions[0].metadata.ordinal_operations[0]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -981,10 +981,9 @@ mod test {
config::Config,
core::protocol::inscription_sequencing::SequenceCursor,
db::{
drop_sqlite_dbs, initialize_sqlite_dbs,
ordinals::update_sequence_metadata_with_block,
drop_all_dbs, initialize_sqlite_dbs, ordinals::update_sequence_metadata_with_block,
},
utils::test_helpers::{new_test_block, new_test_reveal_tx_with_operation},
utils::test_helpers::{TestBlockBuilder, TestTransactionBuilder},
};

#[test_case((780000, false) => (2, 2); "with blessed pre jubilee")]
Expand All @@ -994,9 +993,11 @@ mod test {
fn picks_next((block_height, cursed): (u64, bool)) -> (i64, i64) {
let ctx = Context::empty();
let config = Config::test_default();
drop_sqlite_dbs(&config);
drop_all_dbs(&config);
let db_conns = initialize_sqlite_dbs(&config, &ctx);
let mut block = new_test_block(vec![new_test_reveal_tx_with_operation()]);
let mut block = TestBlockBuilder::new()
.transactions(vec![TestTransactionBuilder::new_with_operation().build()])
.build();
block.block_identifier.index = block_height;

// Pick next twice so we can test all cases.
Expand Down Expand Up @@ -1026,9 +1027,11 @@ mod test {
fn resets_on_previous_block() {
let ctx = Context::empty();
let config = Config::test_default();
drop_sqlite_dbs(&config);
drop_all_dbs(&config);
let db_conns = initialize_sqlite_dbs(&config, &ctx);
let block = new_test_block(vec![new_test_reveal_tx_with_operation()]);
let block = TestBlockBuilder::new()
.transactions(vec![TestTransactionBuilder::new_with_operation().build()])
.build();
update_sequence_metadata_with_block(&block, &db_conns.ordinals, &ctx);
let mut cursor = SequenceCursor::new(&db_conns.ordinals);
let _ = cursor.pick_next(
Expand Down
Loading

0 comments on commit 9f32b30

Please sign in to comment.