Skip to content

Commit

Permalink
a few fixes for preImageProxy usage, account and storage streaming
Browse files Browse the repository at this point in the history
Signed-off-by: garyschulte <garyschulte@gmail.com>
  • Loading branch information
garyschulte committed Aug 24, 2023
1 parent 4596e43 commit b4c60e6
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@

import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.bytes.Bytes32;
import org.apache.tuweni.rlp.RLP;
import org.apache.tuweni.units.bigints.UInt256;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -285,7 +286,7 @@ public NavigableMap<Bytes32, AccountStorageEntry> storageEntriesFrom(
e -> e.getKey(),
e ->
AccountStorageEntry.create(
UInt256.fromBytes(e.getValue()),
UInt256.fromBytes(RLP.decodeValue(e.getValue())),
Hash.wrap(e.getKey()),
preImageProxy.getStorageTrieKeyPreimage(e.getKey())),
(a, b) -> a,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public Map<Bytes32, Bytes> streamStorageFlatDatabase(
.streamFromKey(
ACCOUNT_STORAGE_STORAGE,
Bytes.concatenate(accountHash, startKeyHash).toArrayUnsafe())
.takeWhile(pair -> Bytes.wrap(pair.getKey()).slice(0, Hash.SIZE).equals(accountHash))
.filter(pair -> Bytes.wrap(pair.getKey()).slice(0, Hash.SIZE).equals(accountHash))
.limit(max)
.map(
pair ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public class BonsaiWorldState

private static final Logger LOG = LoggerFactory.getLogger(BonsaiWorldState.class);

private BonsaiWorldStateKeyValueStorage worldStateStorage;
protected BonsaiWorldStateKeyValueStorage worldStateStorage;

protected final CachedMerkleTrieLoader cachedMerkleTrieLoader;
protected final TrieLogManager trieLogManager;
Expand Down Expand Up @@ -218,7 +218,7 @@ private void updateTheAccounts(
final BonsaiAccount updatedAccount = bonsaiValue.getUpdated();
try {
if (updatedAccount == null) {
final Hash addressHash = Hash.hash(accountKey);
final Hash addressHash = preImageProxy.hashAndSavePreImage(accountKey);
accountTrie.remove(addressHash);
maybeStateUpdater.ifPresent(
bonsaiUpdater -> bonsaiUpdater.removeAccountInfoState(addressHash));
Expand All @@ -227,7 +227,8 @@ private void updateTheAccounts(
final Bytes accountValue = updatedAccount.serializeAccount();
maybeStateUpdater.ifPresent(
bonsaiUpdater ->
bonsaiUpdater.putAccountInfoState(Hash.hash(accountKey), accountValue));
bonsaiUpdater.putAccountInfoState(
preImageProxy.hashAndSavePreImage(accountKey), accountValue));
accountTrie.put(addressHash, accountValue);
}
} catch (MerkleTrieException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,8 @@ public void commit() {
pendingCode.setUpdated(updatedAccount.getCode());
}

// This is especially to avoid unnecessary computation for withdrawals
// This is especially to avoid unnecessary computation for withdrawals and
// self-destruct beneficiaries
if (updatedAccount.getUpdatedStorage().isEmpty()) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ private void traceTestSpecs(final String test, final List<GeneralStateTestCaseEi
}
}
worldStateUpdater.commit();
worldState.freeze();

summaryLine.put("output", result.getOutput().toUnprefixedHexString());
final long gasUsed = transaction.getGasLimit() - result.getGasRemaining();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,6 @@ static void insertAccount(
static ReferenceTestWorldState create(final Map<String, AccountMock> accounts) {
// delegate to a Bonsai reference test world state:
return BonsaiReferenceTestWorldState.create(accounts);
// return DefaultReferenceTestWorldState.create(accounts);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ public Stream<Pair<byte[], byte[]>> stream(final SegmentIdentifier segmentId) {

@Override
public Stream<Pair<byte[], byte[]>> streamFromKey(
final SegmentIdentifier segmentId, final byte[] startKey) {
final SegmentIdentifier segmentId, final byte[] startKeyHash) {
return stream(segmentId)
.filter(e -> Bytes.wrap(startKey).compareTo(Bytes.wrap(e.getKey())) <= 0);
.filter(e -> Bytes.wrap(startKeyHash).compareTo(Bytes.wrap(e.getKey())) <= 0);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,9 @@ public Stream<Pair<byte[], byte[]>> stream() {
}

@Override
public Stream<Pair<byte[], byte[]>> streamFromKey(final byte[] startKey) throws StorageException {
return storage.streamFromKey(segmentIdentifier, startKey);
public Stream<Pair<byte[], byte[]>> streamFromKey(final byte[] startKeyHash)
throws StorageException {
return storage.streamFromKey(segmentIdentifier, startKeyHash);
}

@Override
Expand Down

0 comments on commit b4c60e6

Please sign in to comment.