Skip to content

Commit

Permalink
node: commit on db once every inserted block batch (#1364)
Browse files Browse the repository at this point in the history
  • Loading branch information
canepat authored Jul 24, 2023
1 parent a176f2e commit 1c57c51
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion silkworm/node/stagedsync/forks/main_chain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@

namespace silkworm::stagedsync {

//! The number of inserted blocks between two successive commits on db
constexpr uint64_t kInsertedBlockBatch{1'000};

MainChain::MainChain(asio::io_context& ctx, NodeSettings& ns, const db::RWAccess dba)
: io_context_{ctx},
node_settings_{ns},
Expand Down Expand Up @@ -120,8 +123,19 @@ void MainChain::insert_block(const Block& block) {
Hash header_hash = insert_header(block.header);
insert_body(block, header_hash);

auto parent = get_header(block.header.number, header_hash); // remove in production?
// Check chain integrity also on execution side (remove in production?)
const auto parent = get_header(block.header.number - 1, block.header.parent_hash);
ensure_invariant(parent.has_value(), "inserting block must have parent");

// Commit inserted blocks once in a while not to lose downloading progress on restart
static uint64_t block_count{0};
if (++block_count == kInsertedBlockBatch) {
block_count = 0;
StopWatch timing{StopWatch::kStart};
tx_.commit_and_renew();
SILK_INFO << "MainChain: commit " << kInsertedBlockBatch << " blocks up to " << block.header.number
<< " took " << StopWatch::format(timing.since_start());
}
}

auto MainChain::verify_chain(Hash head_block_hash) -> VerificationResult {
Expand Down

0 comments on commit 1c57c51

Please sign in to comment.