Skip to content

Commit

Permalink
Reduce index durability when testing (#2347)
Browse files Browse the repository at this point in the history
  • Loading branch information
casey authored Aug 21, 2023
1 parent 3a3e9f5 commit 4b9a530
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 10 deletions.
35 changes: 28 additions & 7 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,13 @@ impl<T> BitcoinCoreRpcResultExt<T> for Result<T, bitcoincore_rpc::Error> {
pub(crate) struct Index {
client: Client,
database: Database,
path: PathBuf,
durability: redb::Durability,
first_inscription_height: u64,
genesis_block_coinbase_transaction: Transaction,
genesis_block_coinbase_txid: Txid,
height_limit: Option<u64>,
options: Options,
path: PathBuf,
unrecoverably_reorged: AtomicBool,
}

Expand Down Expand Up @@ -188,6 +189,12 @@ impl Index {

log::info!("Setting DB cache size to {} bytes", db_cache_size);

let durability = if cfg!(test) {
redb::Durability::None
} else {
redb::Durability::Immediate
};

let database = match Database::builder()
.set_cache_size(db_cache_size)
.open(&path)
Expand Down Expand Up @@ -224,7 +231,7 @@ impl Index {

let mut tx = database.begin_write()?;

tx.set_durability(redb::Durability::Immediate);
tx.set_durability(durability);

tx.open_table(HEIGHT_TO_BLOCK_HASH)?;
tx.open_table(INSCRIPTION_ID_TO_INSCRIPTION_ENTRY)?;
Expand Down Expand Up @@ -258,15 +265,21 @@ impl Index {
genesis_block_coinbase_txid: genesis_block_coinbase_transaction.txid(),
client,
database,
path,
durability,
first_inscription_height: options.first_inscription_height(),
genesis_block_coinbase_transaction,
height_limit: options.height_limit,
options: options.clone(),
path,
unrecoverably_reorged: AtomicBool::new(false),
})
}

#[cfg(test)]
fn set_durability(&mut self, durability: redb::Durability) {
self.durability = durability;
}

pub(crate) fn get_unspent_outputs(&self, _wallet: Wallet) -> Result<BTreeMap<OutPoint, Amount>> {
let mut utxos = BTreeMap::new();
utxos.extend(
Expand Down Expand Up @@ -500,7 +513,9 @@ impl Index {
}

fn begin_write(&self) -> Result<WriteTransaction> {
Ok(self.database.begin_write()?)
let mut tx = self.database.begin_write()?;
tx.set_durability(self.durability);
Ok(tx)
}

fn increment_statistic(wtx: &WriteTransaction, statistic: Statistic, n: u64) -> Result {
Expand Down Expand Up @@ -3298,7 +3313,9 @@ mod tests {

#[test]
fn recover_from_reorg() {
for context in Context::configurations() {
for mut context in Context::configurations() {
context.index.set_durability(redb::Durability::Immediate);

context.mine_blocks(1);

let txid = context.rpc_server.broadcast_tx(TransactionTemplate {
Expand Down Expand Up @@ -3348,7 +3365,9 @@ mod tests {

#[test]
fn recover_from_3_block_deep_and_consecutive_reorg() {
for context in Context::configurations() {
for mut context in Context::configurations() {
context.index.set_durability(redb::Durability::Immediate);

context.mine_blocks(1);

let txid = context.rpc_server.broadcast_tx(TransactionTemplate {
Expand Down Expand Up @@ -3401,7 +3420,9 @@ mod tests {

#[test]
fn recover_from_very_unlikely_7_block_deep_reorg() {
for context in Context::configurations() {
for mut context in Context::configurations() {
context.index.set_durability(redb::Durability::Immediate);

context.mine_blocks(1);

let txid = context.rpc_server.broadcast_tx(TransactionTemplate {
Expand Down
8 changes: 8 additions & 0 deletions src/index/reorg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ impl Reorg {
pub(crate) fn handle_reorg(index: &Index, height: u64, depth: u64) -> Result {
log::info!("rolling back database after reorg of depth {depth} at height {height}");

if let redb::Durability::None = index.durability {
panic!("set index durability to `Durability::Immediate` to test reorg handling");
}

let mut wtx = index.begin_write()?;

let oldest_savepoint =
Expand All @@ -75,6 +79,10 @@ impl Reorg {
}

pub(crate) fn update_savepoints(index: &Index, height: u64) -> Result {
if let redb::Durability::None = index.durability {
return Ok(());
}

if (height < SAVEPOINT_INTERVAL || height % SAVEPOINT_INTERVAL == 0)
&& index
.client
Expand Down
6 changes: 3 additions & 3 deletions src/subcommand/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2147,15 +2147,15 @@ mod tests {
let server = TestServer::new();

thread::sleep(Duration::from_millis(100));
assert_eq!(server.index.statistic(crate::index::Statistic::Commits), 3);
assert_eq!(server.index.statistic(crate::index::Statistic::Commits), 1);

let info = server.index.info().unwrap();
assert_eq!(info.transactions.len(), 1);
assert_eq!(info.transactions[0].starting_block_count, 0);

server.index.update().unwrap();

assert_eq!(server.index.statistic(crate::index::Statistic::Commits), 3);
assert_eq!(server.index.statistic(crate::index::Statistic::Commits), 1);

let info = server.index.info().unwrap();
assert_eq!(info.transactions.len(), 1);
Expand All @@ -2166,7 +2166,7 @@ mod tests {
thread::sleep(Duration::from_millis(10));
server.index.update().unwrap();

assert_eq!(server.index.statistic(crate::index::Statistic::Commits), 6);
assert_eq!(server.index.statistic(crate::index::Statistic::Commits), 2);

let info = server.index.info().unwrap();
assert_eq!(info.transactions.len(), 2);
Expand Down

0 comments on commit 4b9a530

Please sign in to comment.