Skip to content

Commit 64faa51

Browse files
committed
add checks in StackTrie to ensure lastKey() is safe to use
Signed-off-by: garyschulte <garyschulte@gmail.com>
1 parent 389bfb3 commit 64faa51

File tree

4 files changed

+7
-6
lines changed

4 files changed

+7
-6
lines changed

datatypes/src/main/java/org/hyperledger/besu/datatypes/Hash.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ public class Hash extends DelegatingBytes32 {
2929
/** The constant ZERO. */
3030
public static final Hash ZERO = new Hash(Bytes32.ZERO);
3131

32+
/** Last hash */
33+
public static final Hash LAST = new Hash(Bytes32.fromHexString("F".repeat(64)));
34+
3235
/**
3336
* Hash of an RLP encoded trie hash with no content, or
3437
* "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421"

ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/sync/snapsync/StackTrie.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public void commit(final FlatDatabaseUpdater flatDatabaseUpdater, final NodeUpda
125125
Function.identity(),
126126
Function.identity(),
127127
startKeyHash,
128-
keys.lastKey(),
128+
keys.size() > 0 ? keys.lastKey() : Hash.LAST,
129129
true);
130130

131131
final MerkleTrie<Bytes, Bytes> trie =

ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/snapsync/request/StorageRangeDataRequestTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
@ExtendWith(MockitoExtension.class)
2020
public class StorageRangeDataRequestTest {
21-
private static final Hash HASH_LAST = Hash.fromHexString("F".repeat(64));
2221

2322
@Mock WorldStateStorage storage;
2423
WorldStateProofProvider worldstateProofProvider = new WorldStateProofProvider(storage);
@@ -28,7 +27,7 @@ public void assertEmptySlotsWithProofOfExclusionCompletes() {
2827

2928
var storageRangeRequest =
3029
new StorageRangeDataRequest(
31-
Hash.EMPTY_TRIE_HASH, Bytes32.ZERO, Hash.EMPTY_TRIE_HASH, Bytes32.ZERO, HASH_LAST);
30+
Hash.EMPTY_TRIE_HASH, Bytes32.ZERO, Hash.EMPTY_TRIE_HASH, Bytes32.ZERO, Hash.LAST);
3231

3332
var proofs = new ArrayDeque<Bytes>();
3433
proofs.add(0, Hash.EMPTY_TRIE_HASH);
@@ -44,7 +43,7 @@ public void assertEmptySlotsWithProofOfExclusionCompletes() {
4443
public void assertEmptySlotsWithInvalidProofCompletes() {
4544
var storageRangeRequest =
4645
new StorageRangeDataRequest(
47-
Hash.EMPTY_TRIE_HASH, Bytes32.ZERO, Hash.EMPTY_TRIE_HASH, Bytes32.ZERO, HASH_LAST);
46+
Hash.EMPTY_TRIE_HASH, Bytes32.ZERO, Hash.EMPTY_TRIE_HASH, Bytes32.ZERO, Hash.LAST);
4847

4948
var proofs = new ArrayDeque<Bytes>();
5049
proofs.add(0, Hash.ZERO);

evm/src/test/java/org/hyperledger/besu/evm/operations/BlockHashOperationTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ class BlockHashOperationTest {
3838

3939
@Test
4040
void shouldReturnZeroWhenArgIsBiggerThanALong() {
41-
assertBlockHash(
42-
Bytes32.fromHexString("F".repeat(64)), Bytes32.ZERO, 100, n -> Hash.EMPTY_LIST_HASH);
41+
assertBlockHash(Hash.LAST, Bytes32.ZERO, 100, n -> Hash.EMPTY_LIST_HASH);
4342
}
4443

4544
@Test

0 commit comments

Comments
 (0)