From 4eba26572b591bb677046695bbc1c15777c3accb Mon Sep 17 00:00:00 2001 From: Michael Sproul Date: Thu, 12 Dec 2019 12:48:18 +1100 Subject: [PATCH] Store states in the DB before their blocks (#712) Partial fix for #692 --- beacon_node/beacon_chain/src/beacon_chain.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/beacon_node/beacon_chain/src/beacon_chain.rs b/beacon_node/beacon_chain/src/beacon_chain.rs index 20dd3f049..b5ed74e19 100644 --- a/beacon_node/beacon_chain/src/beacon_chain.rs +++ b/beacon_node/beacon_chain/src/beacon_chain.rs @@ -1322,8 +1322,13 @@ impl BeaconChain { } // Store the block and state. - self.store.put(&block_root, &block)?; + // NOTE: we store the block *after* the state to guard against inconsistency in the event of + // a crash, as states are usually looked up from blocks, not the other way around. A better + // solution would be to use a database transaction (once our choice of database and API + // settles down). + // See: https://github.com/sigp/lighthouse/issues/692 self.store.put_state(&state_root, &state)?; + self.store.put(&block_root, &block)?; metrics::stop_timer(db_write_timer);