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

Reorg resistance #2320

Merged
merged 18 commits into from
Aug 10, 2023
298 changes: 266 additions & 32 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use {
BlockHashValue, Entry, InscriptionEntry, InscriptionEntryValue, InscriptionIdValue,
OutPointValue, SatPointValue, SatRange,
},
updater::Updater,
updater::{Updater, UpdaterError},
},
super::*,
crate::wallet::Wallet,
Expand All @@ -19,7 +19,6 @@ use {
},
std::collections::HashMap,
std::io::{BufWriter, Read, Write},
std::sync::atomic::{self, AtomicBool},
};

mod entry;
Expand Down Expand Up @@ -55,18 +54,6 @@ define_table! { SAT_TO_SATPOINT, u64, &SatPointValue }
define_table! { STATISTIC_TO_COUNT, u64, u64 }
define_table! { WRITE_TRANSACTION_STARTING_BLOCK_COUNT_TO_TIMESTAMP, u64, u128 }

pub(crate) struct Index {
client: Client,
database: Database,
path: PathBuf,
first_inscription_height: u64,
genesis_block_coinbase_transaction: Transaction,
genesis_block_coinbase_txid: Txid,
height_limit: Option<u64>,
options: Options,
reorged: AtomicBool,
}

#[derive(Debug, PartialEq)]
pub(crate) enum List {
Spent,
Expand Down Expand Up @@ -143,6 +130,17 @@ impl<T> BitcoinCoreRpcResultExt<T> for Result<T, bitcoincore_rpc::Error> {
}
}

pub(crate) struct Index {
client: Client,
database: Database,
path: PathBuf,
first_inscription_height: u64,
genesis_block_coinbase_transaction: Transaction,
genesis_block_coinbase_txid: Txid,
height_limit: Option<u64>,
options: Options,
}

impl Index {
pub(crate) fn open(options: &Options) -> Result<Self> {
let client = options.bitcoin_rpc_client()?;
Expand Down Expand Up @@ -221,11 +219,7 @@ impl Index {

let mut tx = database.begin_write()?;

if cfg!(test) {
tx.set_durability(redb::Durability::None);
} else {
tx.set_durability(redb::Durability::Immediate);
};
tx.set_durability(redb::Durability::Immediate);

tx.open_table(HEIGHT_TO_BLOCK_HASH)?;
tx.open_table(INSCRIPTION_ID_TO_INSCRIPTION_ENTRY)?;
Expand Down Expand Up @@ -263,7 +257,6 @@ impl Index {
first_inscription_height: options.first_inscription_height(),
genesis_block_coinbase_transaction,
height_limit: options.height_limit,
reorged: AtomicBool::new(false),
options: options.clone(),
})
}
Expand Down Expand Up @@ -396,7 +389,40 @@ impl Index {
}

pub(crate) fn update(&self) -> Result {
Updater::update(self)
let mut updater = Updater::new(self)?;

loop {
raphjaph marked this conversation as resolved.
Show resolved Hide resolved
match updater.update_index() {
Ok(ok) => return Ok(ok),
Err(err) => {
if let Some(&UpdaterError::Reorged(height)) = err.downcast_ref::<UpdaterError>() {
log::info!("{}", err.to_string());
self.handle_reorg(height)?;
updater = Updater::new(self)?;
} else {
return Err(err);
}
}
}
}
}

pub(crate) fn handle_reorg(&self, height: u64) -> Result {
log::info!("rolling back database after reorg at height {}", height);
let mut wtx = self.begin_write()?;

let oldest_savepoint =
wtx.get_persistent_savepoint(wtx.list_persistent_savepoints()?.min().unwrap())?;

wtx.restore_savepoint(&oldest_savepoint)?;

wtx.commit()?;
log::info!(
"successfully rolled back database to height {}",
self.block_count()?
);

Ok(())
}

pub(crate) fn export(&self, filename: &String, include_addresses: bool) -> Result {
Expand Down Expand Up @@ -464,22 +490,12 @@ impl Index {
Ok(())
}

pub(crate) fn is_reorged(&self) -> bool {
self.reorged.load(atomic::Ordering::Relaxed)
}

fn begin_read(&self) -> Result<rtx::Rtx> {
Ok(rtx::Rtx(self.database.begin_read()?))
}

fn begin_write(&self) -> Result<WriteTransaction> {
if cfg!(test) {
let mut tx = self.database.begin_write()?;
tx.set_durability(redb::Durability::None);
Ok(tx)
} else {
Ok(self.database.begin_write()?)
}
Ok(self.database.begin_write()?)
}

fn increment_statistic(wtx: &WriteTransaction, statistic: Statistic, n: u64) -> Result {
Expand Down Expand Up @@ -1009,6 +1025,65 @@ impl Index {
}
}

#[cfg(test)]
fn assert_non_existence_of_inscription(&self, inscription_id: InscriptionId) {
let rtx = self.database.begin_read().unwrap();

let inscription_id_to_satpoint = rtx.open_table(INSCRIPTION_ID_TO_SATPOINT).unwrap();
assert!(inscription_id_to_satpoint
.get(&inscription_id.store())
.unwrap()
.is_none());

let inscription_id_to_entry = rtx.open_table(INSCRIPTION_ID_TO_INSCRIPTION_ENTRY).unwrap();
assert!(inscription_id_to_entry
.get(&inscription_id.store())
.unwrap()
.is_none());

for range in rtx
.open_table(INSCRIPTION_NUMBER_TO_INSCRIPTION_ID)
.unwrap()
.iter()
.into_iter()
{
for entry in range.into_iter() {
let (_number, id) = entry.unwrap();
assert!(InscriptionId::load(*id.value()) != inscription_id);
}
}

for range in rtx
.open_multimap_table(SATPOINT_TO_INSCRIPTION_ID)
.unwrap()
.iter()
.into_iter()
{
for entry in range.into_iter() {
let (_satpoint, ids) = entry.unwrap();
assert!(!ids
.into_iter()
.any(|id| InscriptionId::load(*id.unwrap().value()) == inscription_id))
}
}

if self.has_sat_index().unwrap() {
for range in rtx
.open_multimap_table(SAT_TO_INSCRIPTION_ID)
.unwrap()
.iter()
.into_iter()
{
for entry in range.into_iter() {
let (_sat, ids) = entry.unwrap();
assert!(!ids
.into_iter()
.any(|id| InscriptionId::load(*id.unwrap().value()) == inscription_id))
}
}
}
}

fn inscriptions_on_output_unordered<'a: 'tx, 'tx>(
satpoint_to_id: &'a impl ReadableMultimapTable<&'static SatPointValue, &'static InscriptionIdValue>,
outpoint: OutPoint,
Expand Down Expand Up @@ -3210,4 +3285,163 @@ mod tests {
)
}
}

#[test]
fn recover_from_reorg() {
for context in Context::configurations() {
context.mine_blocks(1);

let txid = context.rpc_server.broadcast_tx(TransactionTemplate {
inputs: &[(1, 0, 0)],
witness: inscription("text/plain;charset=utf-8", "hello").to_witness(),
..Default::default()
});

context.mine_blocks(6);

let first_id = InscriptionId { txid, index: 0 };
let first_location = SatPoint {
outpoint: OutPoint { txid, vout: 0 },
offset: 0,
};

context
.index
.assert_inscription_location(first_id, first_location, Some(50 * COIN_VALUE));

let txid = context.rpc_server.broadcast_tx(TransactionTemplate {
inputs: &[(7, 0, 0)],
witness: inscription("text/plain;charset=utf-8", "hello").to_witness(),
..Default::default()
});

context.mine_blocks(1);

let second_id = InscriptionId { txid, index: 0 };
let second_location = SatPoint {
outpoint: OutPoint { txid, vout: 0 },
offset: 0,
};

context
.index
.assert_inscription_location(second_id, second_location, Some(350 * COIN_VALUE));

context.rpc_server.invalidate_tip();

context.mine_blocks(2);

context
.index
.assert_inscription_location(first_id, first_location, Some(50 * COIN_VALUE));

context.index.assert_non_existence_of_inscription(second_id);
}
}

#[test]
fn recover_from_3_block_deep_and_consecutive_reorg() {
for context in Context::configurations() {
context.mine_blocks(1);

let txid = context.rpc_server.broadcast_tx(TransactionTemplate {
inputs: &[(1, 0, 0)],
witness: inscription("text/plain;charset=utf-8", "hello").to_witness(),
..Default::default()
});

context.mine_blocks(6);

let first_id = InscriptionId { txid, index: 0 };
let first_location = SatPoint {
outpoint: OutPoint { txid, vout: 0 },
offset: 0,
};

let txid = context.rpc_server.broadcast_tx(TransactionTemplate {
inputs: &[(7, 0, 0)],
witness: inscription("text/plain;charset=utf-8", "hello").to_witness(),
..Default::default()
});

let second_id = InscriptionId { txid, index: 0 };
let second_location = SatPoint {
outpoint: OutPoint { txid, vout: 0 },
offset: 0,
};

context.mine_blocks(3);

context
.index
.assert_inscription_location(second_id, second_location, Some(350 * COIN_VALUE));

context.rpc_server.invalidate_tip();
context.rpc_server.invalidate_tip();
context.rpc_server.invalidate_tip();

context.mine_blocks(4);

context.index.assert_non_existence_of_inscription(second_id);

context.rpc_server.invalidate_tip();

context.mine_blocks(2);

context
.index
.assert_inscription_location(first_id, first_location, Some(50 * COIN_VALUE));
}
}

#[test]
fn recover_from_very_unlikely_7_block_deep_reorg() {
for context in Context::configurations() {
context.mine_blocks(1);

let txid = context.rpc_server.broadcast_tx(TransactionTemplate {
inputs: &[(1, 0, 0)],
witness: inscription("text/plain;charset=utf-8", "hello").to_witness(),
..Default::default()
});

context.mine_blocks(6);

let first_id = InscriptionId { txid, index: 0 };
let first_location = SatPoint {
outpoint: OutPoint { txid, vout: 0 },
offset: 0,
};

let txid = context.rpc_server.broadcast_tx(TransactionTemplate {
inputs: &[(7, 0, 0)],
witness: inscription("text/plain;charset=utf-8", "hello").to_witness(),
..Default::default()
});

let second_id = InscriptionId { txid, index: 0 };
let second_location = SatPoint {
outpoint: OutPoint { txid, vout: 0 },
offset: 0,
};

context.mine_blocks(7);

context
.index
.assert_inscription_location(second_id, second_location, Some(350 * COIN_VALUE));

for _ in 0..7 {
context.rpc_server.invalidate_tip();
}

context.mine_blocks(9);

context.index.assert_non_existence_of_inscription(second_id);

context
.index
.assert_inscription_location(first_id, first_location, Some(50 * COIN_VALUE));
}
}
}
Loading