Skip to content

Commit

Permalink
Fix while exit condition (#2953) (#2967)
Browse files Browse the repository at this point in the history
Signed-off-by: Diego López León <[email protected]>
Co-authored-by: garyschulte <[email protected]>
  • Loading branch information
diega and garyschulte committed Oct 28, 2021
1 parent 8292dfe commit 4612926
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
### Additions and Improvements

### Bug Fixes
- Fixes the exit condition for loading a BonsaiPersistedWorldState for a sibling block of the last one persisted [#2967](https://github.com/hyperledger/besu/pull/2967)
- Fixes bonsai getMutable regression affecting fast-sync [#2934](https://github.com/hyperledger/besu/pull/2934)

### Early Access Features

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@
import java.util.Map;
import java.util.Optional;

import org.apache.tuweni.bytes.Bytes32;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

Expand Down Expand Up @@ -160,4 +162,42 @@ public void testGetMutableWithStorageConsistencyNotRollbackTheState() {
verify(layeredWorldStatesByHash).entrySet();
verifyNoMoreInteractions(layeredWorldStatesByHash);
}

@SuppressWarnings({"unchecked"})
@Test
public void testGetMutableWithStorageConsistencyToRollbackAndRollForwardTheState() {
when(keyValueStorage.startTransaction()).thenReturn(mock(KeyValueStorageTransaction.class));
final BlockHeader genesis = blockBuilder.number(0).buildHeader();
final BlockHeader blockHeaderChainA =
blockBuilder.number(1).timestamp(1).parentHash(genesis.getHash()).buildHeader();
final BlockHeader blockHeaderChainB =
blockBuilder.number(1).timestamp(2).parentHash(genesis.getHash()).buildHeader();

final Map<Bytes32, BonsaiLayeredWorldState> layeredWorldStatesByHash = mock(HashMap.class);
when(layeredWorldStatesByHash.containsKey(any(Bytes32.class))).thenReturn(true);
when(layeredWorldStatesByHash.get(eq(blockHeaderChainA.getHash())))
.thenReturn(mock(BonsaiLayeredWorldState.class, Answers.RETURNS_MOCKS));
when(layeredWorldStatesByHash.get(eq(blockHeaderChainB.getHash())))
.thenReturn(mock(BonsaiLayeredWorldState.class, Answers.RETURNS_MOCKS));

bonsaiWorldStateArchive =
new BonsaiWorldStateArchive(storageProvider, blockchain, 12, layeredWorldStatesByHash);

// initial persisted state hash key
when(blockchain.getBlockHeader(eq(Hash.ZERO))).thenReturn(Optional.of(blockHeaderChainA));
when(blockchain.getBlockHeader(eq(blockHeaderChainB.getHash())))
.thenReturn(Optional.of(blockHeaderChainB));
when(blockchain.getBlockHeader(eq(genesis.getHash()))).thenReturn(Optional.of(genesis));

assertThat(bonsaiWorldStateArchive.getMutable(null, blockHeaderChainB.getHash()))
.containsInstanceOf(BonsaiPersistedWorldState.class);

// verify is trying to get the trie log layers to rollback and roll forward
verify(layeredWorldStatesByHash).containsKey(eq(blockHeaderChainA.getHash()));
verify(layeredWorldStatesByHash).get(eq(blockHeaderChainA.getHash()));
verify(layeredWorldStatesByHash).containsKey(eq(blockHeaderChainB.getHash()));
verify(layeredWorldStatesByHash).get(eq(blockHeaderChainB.getHash()));
verify(layeredWorldStatesByHash).entrySet();
verifyNoMoreInteractions(layeredWorldStatesByHash);
}
}

0 comments on commit 4612926

Please sign in to comment.