Skip to content

Commit

Permalink
Start indexing at correct block height (#3956)
Browse files Browse the repository at this point in the history
  • Loading branch information
partialord committed Sep 20, 2024
1 parent a861cdd commit 51d84da
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 102 deletions.
1 change: 0 additions & 1 deletion ord.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ config: /var/lib/ord/ord.yaml
config_dir: /var/lib/ord
cookie_file: /var/lib/bitcoin/.cookie
data_dir: /var/lib/ord
first_inscription_height: 100
height_limit: 1000
hidden:
- 6fb976ab49dcec017f1e201e84395983204ae1a7c2abf7ced0a85d692e442799i0
Expand Down
79 changes: 20 additions & 59 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,6 @@ pub struct Index {
database: Database,
durability: redb::Durability,
event_sender: Option<tokio::sync::mpsc::Sender<Event>>,
first_inscription_height: u32,
genesis_block_coinbase_transaction: Transaction,
genesis_block_coinbase_txid: Txid,
height_limit: Option<u32>,
Expand All @@ -198,6 +197,7 @@ pub struct Index {
path: PathBuf,
settings: Settings,
started: DateTime<Utc>,
first_index_height: u32,
unrecoverably_reorged: AtomicBool,
}

Expand Down Expand Up @@ -420,13 +420,23 @@ impl Index {
let genesis_block_coinbase_transaction =
settings.chain().genesis_block().coinbase().unwrap().clone();

let first_index_height = if index_sats || index_addresses {
0
} else if index_inscriptions {
settings.first_inscription_height()
} else if index_runes {
settings.first_rune_height()
} else {
u32::MAX
};

Ok(Self {
genesis_block_coinbase_txid: genesis_block_coinbase_transaction.txid(),
client,
database,
durability,
event_sender,
first_inscription_height: settings.first_inscription_height(),
first_index_height,
genesis_block_coinbase_transaction,
height_limit: settings.height_limit(),
index_addresses,
Expand All @@ -441,6 +451,10 @@ impl Index {
})
}

pub fn have_full_utxo_index(&self) -> bool {
self.first_index_height == 0
}

/// Unlike normal outpoints, which are added to index on creation and removed
/// when spent, the UTXO entry for special outpoints may be updated.
///
Expand Down Expand Up @@ -1677,7 +1691,7 @@ impl Index {
Ok(
outpoint != OutPoint::null()
&& outpoint != self.settings.chain().genesis_coinbase_outpoint()
&& if self.index_sats {
&& if self.have_full_utxo_index() {
self
.database
.begin_read()?
Expand Down Expand Up @@ -2420,9 +2434,7 @@ mod tests {
}

{
let context = Context::builder()
.arg("--first-inscription-height=3")
.build();
let context = Context::builder().chain(Chain::Mainnet).build();
context.mine_blocks(1);
let txid = context.core.broadcast_tx(template);
let inscription_id = InscriptionId { txid, index: 0 };
Expand Down Expand Up @@ -3009,53 +3021,6 @@ mod tests {
}
}

#[test]
fn missing_inputs_are_fetched_from_bitcoin_core() {
for args in [
["--first-inscription-height", "2"].as_slice(),
["--first-inscription-height", "2", "--index-sats"].as_slice(),
] {
let context = Context::builder().args(args).build();
context.mine_blocks(1);

let txid = context.core.broadcast_tx(TransactionTemplate {
inputs: &[(1, 0, 0, inscription("text/plain", "hello").to_witness())],
..default()
});
let inscription_id = InscriptionId { txid, index: 0 };

context.mine_blocks(1);

context.index.assert_inscription_location(
inscription_id,
SatPoint {
outpoint: OutPoint { txid, vout: 0 },
offset: 0,
},
Some(50 * COIN_VALUE),
);

let send_txid = context.core.broadcast_tx(TransactionTemplate {
inputs: &[(2, 0, 0, Default::default()), (2, 1, 0, Default::default())],
..default()
});

context.mine_blocks(1);

context.index.assert_inscription_location(
inscription_id,
SatPoint {
outpoint: OutPoint {
txid: send_txid,
vout: 0,
},
offset: 50 * COIN_VALUE,
},
Some(50 * COIN_VALUE),
);
}
}

#[test]
fn one_input_fee_spent_inscriptions_are_tracked_correctly() {
for context in Context::configurations() {
Expand Down Expand Up @@ -3231,9 +3196,7 @@ mod tests {

#[test]
fn lost_sats_are_tracked_correctly() {
let context = Context::builder()
.args(["--index-sats", "--first-inscription-height", "10"])
.build();
let context = Context::builder().args(["--index-sats"]).build();
assert_eq!(context.index.statistic(Statistic::LostSats), 0);

context.mine_blocks(1);
Expand All @@ -3260,9 +3223,7 @@ mod tests {

#[test]
fn lost_sat_ranges_are_tracked_correctly() {
let context = Context::builder()
.args(["--index-sats", "--first-inscription-height", "10"])
.build();
let context = Context::builder().args(["--index-sats"]).build();

let null_ranges = || {
context
Expand Down
22 changes: 10 additions & 12 deletions src/index/updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ impl<'index> Updater<'index> {
Some(progress_bar)
};

let rx = Self::fetch_blocks_from(self.index, self.height, self.index.index_sats)?;
let rx = Self::fetch_blocks_from(self.index, self.height)?;

let (mut output_sender, mut txout_receiver) = Self::spawn_fetcher(self.index)?;

Expand Down Expand Up @@ -153,24 +153,23 @@ impl<'index> Updater<'index> {
fn fetch_blocks_from(
index: &Index,
mut height: u32,
index_sats: bool,
) -> Result<std::sync::mpsc::Receiver<BlockData>> {
let (tx, rx) = std::sync::mpsc::sync_channel(32);

let first_index_height = index.first_index_height;

let height_limit = index.height_limit;

let client = index.settings.bitcoin_rpc_client(None)?;

let first_inscription_height = index.first_inscription_height;

thread::spawn(move || loop {
if let Some(height_limit) = height_limit {
if height >= height_limit {
break;
}
}

match Self::get_block_with_retries(&client, height, index_sats, first_inscription_height) {
match Self::get_block_with_retries(&client, height, first_index_height) {
Ok(Some(block)) => {
if let Err(err) = tx.send(block.into()) {
log::info!("Block receiver disconnected: {err}");
Expand All @@ -192,8 +191,7 @@ impl<'index> Updater<'index> {
fn get_block_with_retries(
client: &Client,
height: u32,
index_sats: bool,
first_inscription_height: u32,
first_index_height: u32,
) -> Result<Option<Block>> {
let mut errors = 0;
loop {
Expand All @@ -203,7 +201,7 @@ impl<'index> Updater<'index> {
.and_then(|option| {
option
.map(|hash| {
if index_sats || height >= first_inscription_height {
if height >= first_index_height {
Ok(client.get_block(&hash)?)
} else {
Ok(Block {
Expand Down Expand Up @@ -425,8 +423,8 @@ impl<'index> Updater<'index> {
wtx.open_table(SEQUENCE_NUMBER_TO_INSCRIPTION_ENTRY)?;
let mut transaction_id_to_transaction = wtx.open_table(TRANSACTION_ID_TO_TRANSACTION)?;

let index_inscriptions =
self.height >= self.index.first_inscription_height && self.index.index_inscriptions;
let index_inscriptions = self.height >= self.index.settings.first_inscription_height()
&& self.index.index_inscriptions;

// If the receiver still has inputs something went wrong in the last
// block and we shouldn't recover from this and commit the last block
Expand All @@ -437,7 +435,7 @@ impl<'index> Updater<'index> {
);
}

if !self.index.index_sats {
if !self.index.have_full_utxo_index() {
// Send all missing input outpoints to be fetched
let txids = block
.txdata
Expand Down Expand Up @@ -562,7 +560,7 @@ impl<'index> Updater<'index> {

entry.value().to_buf()
} else {
assert!(!self.index.index_sats);
assert!(!self.index.have_full_utxo_index());
let txout = txout_receiver.blocking_recv().map_err(|err| {
anyhow!(
"failed to get transaction for {}: {err}",
Expand Down
5 changes: 0 additions & 5 deletions src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,6 @@ pub struct Options {
pub(crate) cookie_file: Option<PathBuf>,
#[arg(long, alias = "datadir", help = "Store index in <DATA_DIR>.")]
pub(crate) data_dir: Option<PathBuf>,
#[arg(
long,
help = "Don't look for inscriptions below <FIRST_INSCRIPTION_HEIGHT>."
)]
pub(crate) first_inscription_height: Option<u32>,
#[arg(long, help = "Limit index to <HEIGHT_LIMIT> blocks.")]
pub(crate) height_limit: Option<u32>,
#[arg(long, help = "Use index at <INDEX>.")]
Expand Down
24 changes: 5 additions & 19 deletions src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ pub struct Settings {
config_dir: Option<PathBuf>,
cookie_file: Option<PathBuf>,
data_dir: Option<PathBuf>,
first_inscription_height: Option<u32>,
height_limit: Option<u32>,
hidden: Option<HashSet<InscriptionId>>,
http_port: Option<u16>,
Expand Down Expand Up @@ -120,9 +119,6 @@ impl Settings {
config_dir: self.config_dir.or(source.config_dir),
cookie_file: self.cookie_file.or(source.cookie_file),
data_dir: self.data_dir.or(source.data_dir),
first_inscription_height: self
.first_inscription_height
.or(source.first_inscription_height),
height_limit: self.height_limit.or(source.height_limit),
hidden: Some(
self
Expand Down Expand Up @@ -166,7 +162,6 @@ impl Settings {
config_dir: options.config_dir,
cookie_file: options.cookie_file,
data_dir: options.data_dir,
first_inscription_height: options.first_inscription_height,
height_limit: options.height_limit,
hidden: None,
http_port: None,
Expand Down Expand Up @@ -255,7 +250,6 @@ impl Settings {
config_dir: get_path("CONFIG_DIR"),
cookie_file: get_path("COOKIE_FILE"),
data_dir: get_path("DATA_DIR"),
first_inscription_height: get_u32("FIRST_INSCRIPTION_HEIGHT")?,
height_limit: get_u32("HEIGHT_LIMIT")?,
hidden: inscriptions("HIDDEN")?,
http_port: get_u16("HTTP_PORT")?,
Expand Down Expand Up @@ -286,7 +280,6 @@ impl Settings {
config_dir: None,
cookie_file: None,
data_dir: Some(dir.into()),
first_inscription_height: None,
height_limit: None,
hidden: None,
http_port: None,
Expand Down Expand Up @@ -354,13 +347,6 @@ impl Settings {
config_dir: None,
cookie_file: Some(cookie_file),
data_dir: Some(data_dir),
first_inscription_height: Some(if self.integration_test {
0
} else {
self
.first_inscription_height
.unwrap_or_else(|| chain.first_inscription_height())
}),
height_limit: self.height_limit,
hidden: self.hidden,
http_port: self.http_port,
Expand Down Expand Up @@ -516,7 +502,11 @@ impl Settings {
}

pub fn first_inscription_height(&self) -> u32 {
self.first_inscription_height.unwrap()
if self.integration_test {
0
} else {
self.chain.unwrap().first_inscription_height()
}
}

pub fn first_rune_height(&self) -> u32 {
Expand Down Expand Up @@ -1021,7 +1011,6 @@ mod tests {
("CONFIG_DIR", "config dir"),
("COOKIE_FILE", "cookie file"),
("DATA_DIR", "/data/dir"),
("FIRST_INSCRIPTION_HEIGHT", "2"),
("HEIGHT_LIMIT", "3"),
("HIDDEN", "6fb976ab49dcec017f1e201e84395983204ae1a7c2abf7ced0a85d692e442799i0 703e5f7c49d82aab99e605af306b9a30e991e57d42f982908a962a81ac439832i0"),
("HTTP_PORT", "8080"),
Expand Down Expand Up @@ -1055,7 +1044,6 @@ mod tests {
config_dir: Some("config dir".into()),
cookie_file: Some("cookie file".into()),
data_dir: Some("/data/dir".into()),
first_inscription_height: Some(2),
height_limit: Some(3),
hidden: Some(
vec![
Expand Down Expand Up @@ -1102,7 +1090,6 @@ mod tests {
"--config-dir=config dir",
"--cookie-file=cookie file",
"--datadir=/data/dir",
"--first-inscription-height=2",
"--height-limit=3",
"--index-addresses",
"--index-cache-size=4",
Expand All @@ -1129,7 +1116,6 @@ mod tests {
config_dir: Some("config dir".into()),
cookie_file: Some("cookie file".into()),
data_dir: Some("/data/dir".into()),
first_inscription_height: Some(2),
height_limit: Some(3),
hidden: None,
http_port: None,
Expand Down
6 changes: 1 addition & 5 deletions tests/json_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,11 +235,7 @@ fn get_inscriptions() {
fn get_inscriptions_in_block() {
let core = mockcore::spawn();

let ord = TestServer::spawn_with_server_args(
&core,
&["--index-sats", "--first-inscription-height", "0"],
&[],
);
let ord = TestServer::spawn_with_server_args(&core, &["--index-sats"], &[]);

create_wallet(&core, &ord);

Expand Down
1 change: 0 additions & 1 deletion tests/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ fn default() {
"config_dir": null,
"cookie_file": ".*\.cookie",
"data_dir": ".*",
"first_inscription_height": 767430,
"height_limit": null,
"hidden": \[\],
"http_port": null,
Expand Down

0 comments on commit 51d84da

Please sign in to comment.