Skip to content

Commit

Permalink
move prepare to fix race condition
Browse files Browse the repository at this point in the history
When importing blocks quickly, there's a chance that some state from the
block after the mark block makes it into the database before the node
added listener is fully attached. This means that state after the mark
block could be removed. This creates the node added listener once and
for all before the block added event observer so the race never happens.

Signed-off-by: Ratan Rai Sur <[email protected]>
  • Loading branch information
RatanRSur committed Sep 30, 2019
1 parent ee2ba7a commit b99f2ed
Showing 1 changed file with 1 addition and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public Pruner(

public void start() {
LOG.info("Starting Pruner.");
pruningStrategy.prepare();
blockchain.observeBlockAdded((event, blockchain) -> handleNewBlock(event));
}

Expand All @@ -73,7 +74,6 @@ private void handleNewBlock(final BlockAddedEvent event) {

final long blockNumber = event.getBlock().getHeader().getNumber();
if (state.compareAndSet(State.IDLE, State.MARK_BLOCK_CONFIRMATIONS_AWAITING)) {
pruningStrategy.prepare();
markBlockNumber = blockNumber;
} else if (blockNumber >= markBlockNumber + blockConfirmations
&& state.compareAndSet(State.MARK_BLOCK_CONFIRMATIONS_AWAITING, State.MARKING)) {
Expand Down Expand Up @@ -107,7 +107,6 @@ private void sweep() {
execute(
() -> {
pruningStrategy.sweepBefore(markBlockNumber);
pruningStrategy.cleanup();
state.compareAndSet(State.SWEEPING, State.IDLE);
});
}
Expand Down

0 comments on commit b99f2ed

Please sign in to comment.