1
0
Fork 0
mirror of https://github.com/romanz/electrs.git synced 2025-02-23 22:56:55 +01:00

Merge pull request #545 from romanz/reindex-blocks

Allow reindexing last blocks, mainly for sync logic testing
This commit is contained in:
Roman Zeyde 2021-10-11 12:01:10 +03:00 committed by GitHub
commit 2d2544f502
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 32 additions and 3 deletions

View file

@ -110,6 +110,12 @@ type = "usize"
doc = "Number of transactions to lookup before returning an error, to prevent 'too popular' addresses from causing the RPC server to get stuck (0 - disable the limit)"
default = "200"
[[param]]
name = "reindex_last_blocks"
type = "usize"
doc = "Number of last blocks to reindex (used for testing)"
default = "0"
[[param]]
name = "server_banner"
type = "String"

View file

@ -54,6 +54,18 @@ impl Chain {
}
}
pub(crate) fn drop_last_headers(&mut self, n: usize) {
if n == 0 {
return;
}
if let Some(new_height) = self.height().checked_sub(n) {
self.update(vec![NewHeader::from((
self.headers[new_height].1,
new_height,
))])
}
}
/// Load the chain from a collecion of headers, up to the given tip
pub(crate) fn load(&mut self, headers: Vec<BlockHeader>, tip: BlockHash) {
let genesis_hash = self.headers[0].0;
@ -91,7 +103,6 @@ impl Chain {
}
/// Update the chain with a list of new headers (possibly a reorg)
/// Note that we cannot shorten a chain (e.g. by dropping )
pub(crate) fn update(&mut self, headers: Vec<NewHeader>) {
if let Some(first_height) = headers.first().map(|h| h.height) {
for (hash, _header) in self.headers.drain(first_height..) {
@ -211,12 +222,11 @@ mod tests {
// test chain shortening
for i in (0..=headers.len()).rev() {
let header = *regtest.get_block_header(i).unwrap();
let hash = regtest.get_block_hash(i).unwrap();
assert_eq!(regtest.get_block_height(&hash), Some(i));
regtest.update(vec![NewHeader::from((header, i))]);
assert_eq!(regtest.height(), i);
assert_eq!(regtest.tip(), hash);
regtest.drop_last_headers(1);
}
assert_eq!(regtest.height(), 0);
assert_eq!(
@ -224,6 +234,13 @@ mod tests {
"0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206"
);
regtest.drop_last_headers(1);
assert_eq!(regtest.height(), 0);
assert_eq!(
regtest.tip().to_hex(),
"0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206"
);
// test reorg
let mut regtest = Chain::new(Regtest);
regtest.load(headers.clone(), headers.last().unwrap().block_hash());

View file

@ -16,6 +16,7 @@ const DEFAULT_SERVER_ADDRESS: [u8; 4] = [127, 0, 0, 1]; // by default, serve on
mod internal {
#![allow(unused)]
#![allow(clippy::identity_conversion)]
#![allow(clippy::cognitive_complexity)]
include!(concat!(env!("OUT_DIR"), "/configure_me_config.rs"));
}
@ -133,6 +134,7 @@ pub struct Config {
pub jsonrpc_timeout: Duration,
pub index_batch_size: usize,
pub index_lookup_limit: Option<usize>,
pub reindex_last_blocks: usize,
pub auto_reindex: bool,
pub ignore_mempool: bool,
pub sync_once: bool,
@ -306,6 +308,7 @@ impl Config {
jsonrpc_timeout: Duration::from_secs(config.jsonrpc_timeout_secs),
index_batch_size: config.index_batch_size,
index_lookup_limit,
reindex_last_blocks: config.reindex_last_blocks,
auto_reindex: config.auto_reindex,
ignore_mempool: config.ignore_mempool,
sync_once: config.sync_once,

View file

@ -79,6 +79,7 @@ impl Index {
metrics: &Metrics,
batch_size: usize,
lookup_limit: Option<usize>,
reindex_last_blocks: usize,
) -> Result<Self> {
if let Some(row) = store.get_tip() {
let tip = deserialize(&row).expect("invalid tip");
@ -88,6 +89,7 @@ impl Index {
.map(|row| HeaderRow::from_db_row(&row).header)
.collect();
chain.load(headers, tip);
chain.drop_last_headers(reindex_last_blocks);
};
let stats = Stats::new(metrics);

View file

@ -34,6 +34,7 @@ impl Tracker {
&metrics,
config.index_batch_size,
config.index_lookup_limit,
config.reindex_last_blocks,
)
.context("failed to open index")?,
mempool: Mempool::new(),