Skip to content

Commit

Permalink
fix layer issue with bonsai trie (hyperledger#2137)
Browse files Browse the repository at this point in the history
Fix the bonsai layers bug that impacted the tracing. This came during a reorg the link between layers was not valid
This resulted in invalid traces based on bad blocks history
Signed-off-by: Karim TAAM <karim.t2am@gmail.com>
  • Loading branch information
matkt authored Apr 16, 2021
1 parent 92048e2 commit 51283cc
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ public void persist(final BlockHeader blockHeader) {
LOG.debug("Writing Trie Log for {}", worldStateBlockHash);
final TrieLogLayer trieLog = updater.generateTrieLog(worldStateBlockHash);
trieLog.freeze();
archive.addLayeredWorldState(this, blockHeader.getNumber(), worldStateRootHash, trieLog);
archive.addLayeredWorldState(this, blockHeader, worldStateRootHash, trieLog);
final BytesValueRLPOutput rlpLog = new BytesValueRLPOutput();
trieLog.writeTo(rlpLog);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,30 @@ public BonsaiWorldStateArchive(
final StorageProvider provider, final Blockchain blockchain, final long maxLayersToLoad) {
this.blockchain = blockchain;

worldStateStorage = new BonsaiWorldStateKeyValueStorage(provider);
persistedState = new BonsaiPersistedWorldState(this, worldStateStorage);
layeredWorldStatesByHash = new HashMap<>();
this.worldStateStorage = new BonsaiWorldStateKeyValueStorage(provider);
this.persistedState = new BonsaiPersistedWorldState(this, worldStateStorage);
this.layeredWorldStatesByHash = new HashMap<>();
this.maxLayersToLoad = maxLayersToLoad;
blockchain.observeBlockAdded(
event -> {
if (event.isNewCanonicalHead()) {
final BlockHeader eventBlockHeader = event.getBlock().getHeader();
layeredWorldStatesByHash.computeIfPresent(
eventBlockHeader.getParentHash(),
(hash, bonsaiLayeredWorldState) -> {
if (layeredWorldStatesByHash.containsKey(eventBlockHeader.getHash())) {
bonsaiLayeredWorldState.setNextWorldView(
Optional.of(layeredWorldStatesByHash.get(eventBlockHeader.getHash())));
}
return bonsaiLayeredWorldState;
});
}
});
}

@Override
public Optional<WorldState> get(final Hash rootHash, final Hash blockHash) {
if (layeredWorldStatesByHash.containsKey(rootHash)) {
if (layeredWorldStatesByHash.containsKey(blockHash)) {
return Optional.of(layeredWorldStatesByHash.get(blockHash));
} else if (rootHash.equals(persistedState.blockHash())) {
return Optional.of(persistedState);
Expand All @@ -78,27 +93,18 @@ public Optional<WorldState> get(final Hash rootHash, final Hash blockHash) {

public void addLayeredWorldState(
final BonsaiWorldView persistedWorldState,
final long blockNumber,
final BlockHeader blockHeader,
final Hash worldStateRootHash,
final TrieLogLayer trieLog) {
final BonsaiLayeredWorldState bonsaiLayeredWorldState =
new BonsaiLayeredWorldState(
blockchain,
this,
Optional.of(persistedWorldState),
blockNumber,
blockHeader.getNumber(),
worldStateRootHash,
trieLog);
layeredWorldStatesByHash.put(bonsaiLayeredWorldState.blockHash(), bonsaiLayeredWorldState);
if (blockNumber > 0) {
final Optional<Hash> blockHashByNumber = blockchain.getBlockHashByNumber(blockNumber - 1);
if (blockHashByNumber.isPresent()
&& layeredWorldStatesByHash.containsKey(blockHashByNumber.get())) {
layeredWorldStatesByHash
.get(blockHashByNumber.get())
.setNextWorldView(Optional.of(bonsaiLayeredWorldState));
}
}
}

public Optional<TrieLogLayer> getTrieLogLayer(final Hash blockHash) {
Expand Down Expand Up @@ -236,10 +242,6 @@ public Optional<Bytes> getNodeData(final Hash hash) {
return Optional.empty();
}

public BonsaiWorldStateKeyValueStorage getWorldStateStorage() {
return worldStateStorage;
}

@Override
public Optional<WorldStateProof> getAccountProof(
final Hash worldStateRoot,
Expand Down

0 comments on commit 51283cc

Please sign in to comment.