Skip to content

Commit 432bb41

Browse files
committed
Block index option checks. Closes #14
1 parent 9a60f35 commit 432bb41

File tree

1 file changed

+24
-10
lines changed

1 file changed

+24
-10
lines changed

node/src/config.rs

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -159,23 +159,37 @@ impl Args {
159159
bitcoin_rpc_auth,
160160
);
161161

162-
std::fs::create_dir_all(data_dir.clone())?;
162+
fs::create_dir_all(data_dir.clone())?;
163163

164-
let chain_store = Store::open(data_dir.join("protocol.sdb"))?;
164+
let proto_db_path = data_dir.join("protocol.sdb");
165+
let initial_sync = !proto_db_path.exists();
166+
167+
let chain_store = Store::open(proto_db_path)?;
165168
let chain = LiveStore {
166169
state: chain_store.begin(&genesis)?,
167170
store: chain_store,
168171
};
169172

170-
let block_index = match args.block_index {
171-
true => {
172-
let block_store = Store::open(data_dir.join("blocks.sdb"))?;
173-
Some(LiveStore {
174-
state: block_store.begin(&genesis).expect("begin block index"),
175-
store: block_store,
176-
})
173+
let block_index = if args.block_index {
174+
let block_db_path = data_dir.join("block_index.sdb");
175+
if !initial_sync && !block_db_path.exists() {
176+
return Err(anyhow::anyhow!("Block index must be enabled from the initial sync."))
177177
}
178-
false => None,
178+
let block_store = Store::open(block_db_path)?;
179+
let index = LiveStore {
180+
state: block_store.begin(&genesis).expect("begin block index"),
181+
store: block_store,
182+
};
183+
{
184+
let tip_1 = index.state.tip.read().expect("index");
185+
let tip_2 = chain.state.tip.read().expect("tip");
186+
if tip_1.height != tip_2.height || tip_1.hash != tip_2.hash {
187+
return Err(anyhow::anyhow!("Protocol and block index states don't match."))
188+
}
189+
}
190+
Some(index)
191+
} else {
192+
None
179193
};
180194

181195
Ok(Spaced {

0 commit comments

Comments
 (0)