Skip to content

Commit

Permalink
additional testing for override
Browse files Browse the repository at this point in the history
Signed-off-by: garyschulte <[email protected]>
  • Loading branch information
garyschulte committed Jan 22, 2025
1 parent 294f98b commit 8a3ad52
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.util.function.BiConsumer;
import java.util.function.Function;

import graphql.VisibleForTesting;
import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.bytes.Bytes32;
import org.apache.tuweni.units.bigints.UInt256;
Expand Down Expand Up @@ -102,12 +103,12 @@ public void run() {
System.exit(-1);
}

final BlockHeader headHeader =
controller.getProtocolContext().getBlockchain().getChainHeadHeader();

BlockHashAndStateRoot blockHashAndStateRoot = BlockHashAndStateRoot.create(overrideHashes);

if (blockHashAndStateRoot == null) {
final BlockHeader headHeader =
controller.getProtocolContext().getBlockchain().getChainHeadHeader();

worldStateStorage
.getWorldStateRootHash()
// we want state root hash to either be empty or same the same as chain head
Expand All @@ -123,27 +124,33 @@ public void run() {
blockHashAndStateRoot =
new BlockHashAndStateRoot(headHeader.getBlockHash(), headHeader.getStateRoot());
}
verifyAndRebuild(blockHashAndStateRoot, worldStateStorage);
}
}

// rebuild trie:
var newHash = rebuildTrie(worldStateStorage);

// write state root and block hash from the header:
if (!blockHashAndStateRoot.stateRoot().equals(newHash)) {
@VisibleForTesting
protected void verifyAndRebuild(
final BlockHashAndStateRoot blockHashAndStateRoot,
final BonsaiWorldStateKeyValueStorage worldStateStorage) {
// rebuild trie:
var newHash = rebuildTrie(worldStateStorage);

// write state root and block hash from the header:
if (!blockHashAndStateRoot.stateRoot().equals(newHash)) {
LOG.error(
"Catastrophic: calculated state root {} after state rebuild, was expecting {}.",
newHash,
blockHashAndStateRoot.stateRoot());
if (overrideHashes == null) {
LOG.error(
"Catastrophic: calculated state root {} after state rebuild, was expecting {}.",
newHash,
blockHashAndStateRoot.stateRoot());
if (overrideHashes == null) {
LOG.error(
"Refusing to write mismatched block hash and state root. Node needs manual intervention.");
System.exit(-1);
} else {
LOG.error(
"Writing the override block hash and state root, but node likely needs manual intervention.");
}
"Refusing to write mismatched block hash and state root. Node needs manual intervention.");
System.exit(-1);
} else {
LOG.error(
"Writing the override block hash and state root, but node likely needs manual intervention.");
}
writeStateRootAndBlockHash(blockHashAndStateRoot, worldStateStorage);
}
writeStateRootAndBlockHash(blockHashAndStateRoot, worldStateStorage);
}

Hash rebuildTrie(final BonsaiWorldStateKeyValueStorage worldStateStorage) {
Expand Down Expand Up @@ -330,8 +337,8 @@ static BlockHashAndStateRoot create(final String comboString) {
if (comboString != null) {
var hashArray = comboString.split(":", 2);
try {
return new BlockHashAndStateRoot(Hash.fromHexString(hashArray[0]),
Hash.fromHexString(hashArray[1]));
return new BlockHashAndStateRoot(
Hash.fromHexString(hashArray[0]), Hash.fromHexString(hashArray[1]));
} catch (Exception ex) {
System.err.println("failed parsing supplied block hash and stateroot " + ex.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,34 @@ public void assertStateRootMatchesAfterRebuild() {
assertThat(newHash).isEqualTo(headRootHash);
}

@Test
public void assertOverrideBlockHashAndStateRoot() {
var worldstate = (BonsaiWorldState) archive.getMutable();
var headRootHash = worldstate.rootHash();
var override = new BlockHashAndStateRoot(Hash.ZERO, headRootHash);

// rebuild the trie, should not complain about block hash mismatch:
command.verifyAndRebuild(override, worldstate.getWorldStateStorage());

// assert new storage:
var newStorage = worldstate.getWorldStateStorage();
var newRoot = newStorage.getWorldStateRootHash();
var newBlockHash = newStorage.getWorldStateBlockHash();

assertThat(newRoot).isPresent();
assertThat(newRoot.get()).isEqualTo(override.stateRoot());
assertThat(newBlockHash).isPresent();
assertThat(newBlockHash.get()).isEqualTo(override.blockHash());
}

@Test
public void assertBlockHashAndStateRootParsing() {

assertThat(BlockHashAndStateRoot.create("0xdeadbeef:0xdeadbeef")).isNull();
assertThat(BlockHashAndStateRoot.create(Hash.EMPTY + "::" + Hash.EMPTY_TRIE_HASH)).isNull();
assertThat(BlockHashAndStateRoot.create("" + Hash.EMPTY + Hash.EMPTY_TRIE_HASH)).isNull();
assertThat(BlockHashAndStateRoot.create(Hash.EMPTY + ":" + Hash.EMPTY_TRIE_HASH + ":"))
.isNull();

var mockVal = BlockHashAndStateRoot.create(Hash.EMPTY + ":" + Hash.EMPTY_TRIE_HASH);
assertThat(mockVal).isNotNull();
Expand Down

0 comments on commit 8a3ad52

Please sign in to comment.