Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce durability to speed up tests #2347

Merged
merged 4 commits into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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