use of org.hyperledger.besu.evm.account.AccountStorageEntry in project besu by hyperledger.
the class DefaultMutableWorldStateTest method shouldCombineUnchangedAndChangedValuesWhenRetrievingStorageEntries.
@Test
public void shouldCombineUnchangedAndChangedValuesWhenRetrievingStorageEntries() {
final MutableWorldState worldState = createEmpty();
WorldUpdater updater = worldState.updater();
MutableAccount account = updater.createAccount(ADDRESS).getMutable();
account.setBalance(Wei.of(100000));
account.setStorageValue(UInt256.ONE, UInt256.valueOf(2));
account.setStorageValue(UInt256.valueOf(2), UInt256.valueOf(5));
updater.commit();
final List<AccountStorageEntry> initialSetOfEntries = new ArrayList<>();
initialSetOfEntries.add(AccountStorageEntry.forKeyAndValue(UInt256.ONE, UInt256.valueOf(2)));
initialSetOfEntries.add(AccountStorageEntry.forKeyAndValue(UInt256.valueOf(2), UInt256.valueOf(5)));
final Map<Bytes32, AccountStorageEntry> initialEntries = new TreeMap<>();
initialSetOfEntries.forEach(entry -> initialEntries.put(entry.getKeyHash(), entry));
updater = worldState.updater();
account = updater.getAccount(ADDRESS).getMutable();
account.setStorageValue(UInt256.ONE, UInt256.valueOf(3));
account.setStorageValue(UInt256.valueOf(3), UInt256.valueOf(6));
final List<AccountStorageEntry> finalSetOfEntries = new ArrayList<>();
finalSetOfEntries.add(AccountStorageEntry.forKeyAndValue(UInt256.ONE, UInt256.valueOf(3)));
finalSetOfEntries.add(AccountStorageEntry.forKeyAndValue(UInt256.valueOf(2), UInt256.valueOf(5)));
finalSetOfEntries.add(AccountStorageEntry.forKeyAndValue(UInt256.valueOf(3), UInt256.valueOf(6)));
final Map<Bytes32, AccountStorageEntry> finalEntries = new TreeMap<>();
finalSetOfEntries.forEach(entry -> finalEntries.put(entry.getKeyHash(), entry));
assertThat(account.storageEntriesFrom(Hash.ZERO, 10)).isEqualTo(finalEntries);
assertThat(updater.get(ADDRESS).storageEntriesFrom(Hash.ZERO, 10)).isEqualTo(finalEntries);
assertThat(worldState.get(ADDRESS).storageEntriesFrom(Hash.ZERO, 10)).isEqualTo(initialEntries);
worldState.persist(null);
assertThat(updater.get(ADDRESS).storageEntriesFrom(Hash.ZERO, 10)).isEqualTo(finalEntries);
assertThat(worldState.get(ADDRESS).storageEntriesFrom(Hash.ZERO, 10)).isEqualTo(initialEntries);
updater.commit();
assertThat(worldState.get(ADDRESS).storageEntriesFrom(Hash.ZERO, 10)).isEqualTo(finalEntries);
worldState.persist(null);
assertThat(worldState.get(ADDRESS).storageEntriesFrom(Hash.ZERO, 10)).isEqualTo(finalEntries);
}
use of org.hyperledger.besu.evm.account.AccountStorageEntry in project besu by hyperledger.
the class DebugStorageRangeAt method extractStorageAt.
private JsonRpcSuccessResponse extractStorageAt(final JsonRpcRequestContext requestContext, final Address accountAddress, final Hash startKey, final int limit, final WorldState worldState) {
final Account account = worldState.get(accountAddress);
final NavigableMap<Bytes32, AccountStorageEntry> entries = account.storageEntriesFrom(startKey, limit + 1);
Bytes32 nextKey = null;
if (entries.size() == limit + 1) {
nextKey = entries.lastKey();
entries.remove(nextKey);
}
return new JsonRpcSuccessResponse(requestContext.getRequest().getId(), new DebugStorageRangeAtResult(entries, nextKey, shortValues));
}
use of org.hyperledger.besu.evm.account.AccountStorageEntry in project besu by hyperledger.
the class EntriesFromIntegrationTest method shouldCollectStateEntries.
@Test
@SuppressWarnings("MathAbsoluteRandom")
public void shouldCollectStateEntries() {
final MutableWorldState worldState = createInMemoryWorldStateArchive().getMutable();
final WorldUpdater updater = worldState.updater();
MutableAccount account = updater.getOrCreate(Address.fromHexString("0x56")).getMutable();
final Map<Bytes32, AccountStorageEntry> expectedValues = new TreeMap<>();
final int nodeCount = 100_000;
final Random random = new Random(42989428249L);
// Create some storage entries in the committed, underlying account.
for (int i = 0; i <= nodeCount; i++) {
addExpectedValue(account, expectedValues, UInt256.valueOf(Math.abs(random.nextLong())), UInt256.valueOf(i * 10 + 1));
}
updater.commit();
// Add some changes on top that AbstractWorldUpdater.UpdateTrackingAccount will have to merge.
account = worldState.updater().getOrCreate(Address.fromHexString("0x56")).getMutable();
for (int i = 0; i <= nodeCount; i++) {
addExpectedValue(account, expectedValues, UInt256.valueOf(Math.abs(random.nextLong())), UInt256.valueOf(i * 10 + 1));
}
final Map<Bytes32, AccountStorageEntry> values = account.storageEntriesFrom(Bytes32.ZERO, Integer.MAX_VALUE);
assertThat(values).isEqualTo(expectedValues);
}
use of org.hyperledger.besu.evm.account.AccountStorageEntry in project besu by hyperledger.
the class DebugStorageRangeAtTest method shouldRetrieveStorageRange_fullValues.
@Test
public void shouldRetrieveStorageRange_fullValues() {
final TransactionWithMetadata transactionWithMetadata = new TransactionWithMetadata(transaction, 12L, Optional.empty(), blockHash, TRANSACTION_INDEX);
final BlockWithMetadata<TransactionWithMetadata, Hash> blockWithMetadata = new BlockWithMetadata<>(blockHeader, Collections.singletonList(transactionWithMetadata), Collections.emptyList(), Difficulty.ONE, 1);
final JsonRpcRequestContext request = new JsonRpcRequestContext(new JsonRpcRequest("2.0", "debug_storageRangeAt", new Object[] { blockHash.toString(), TRANSACTION_INDEX, accountAddress, START_KEY_HASH.toString(), 10 }));
when(blockchainQueries.blockByHash(blockHash)).thenReturn(Optional.of(blockWithMetadata));
when(blockchainQueries.transactionByBlockHashAndIndex(blockHash, TRANSACTION_INDEX)).thenReturn(Optional.of(transactionWithMetadata));
when(worldState.get(accountAddress)).thenReturn(account);
when(blockReplay.afterTransactionInBlock(eq(blockHash), eq(transactionHash), any())).thenAnswer(this::callAction);
final List<AccountStorageEntry> entries = new ArrayList<>();
entries.add(AccountStorageEntry.forKeyAndValue(UInt256.fromHexString("0x33"), UInt256.valueOf(6)));
entries.add(AccountStorageEntry.forKeyAndValue(UInt256.fromHexString("0x44"), UInt256.valueOf(7)));
entries.add(AccountStorageEntry.create(UInt256.valueOf(7), Hash.hash(Bytes32.fromHexString("0x45")), Optional.empty()));
final NavigableMap<Bytes32, AccountStorageEntry> rawEntries = new TreeMap<>();
entries.forEach(e -> rawEntries.put(e.getKeyHash(), e));
when(account.storageEntriesFrom(START_KEY_HASH, 11)).thenReturn(rawEntries);
final JsonRpcSuccessResponse response = (JsonRpcSuccessResponse) debugStorageRangeAt.response(request);
final DebugStorageRangeAtResult result = (DebugStorageRangeAtResult) response.getResult();
assertThat(result).isNotNull();
assertThat(result.getNextKey()).isNull();
entries.sort(Comparator.comparing(AccountStorageEntry::getKeyHash));
assertThat(result.getStorage()).containsExactly(entry(entries.get(0).getKeyHash().toString(), new DebugStorageRangeAtResult.StorageEntry(entries.get(0), false)), entry(entries.get(1).getKeyHash().toString(), new DebugStorageRangeAtResult.StorageEntry(entries.get(1), false)), entry(entries.get(2).getKeyHash().toString(), new DebugStorageRangeAtResult.StorageEntry(entries.get(2), false)));
}
use of org.hyperledger.besu.evm.account.AccountStorageEntry in project besu by hyperledger.
the class FastWorldStateDownloaderTest method assertAccountsMatch.
private void assertAccountsMatch(final WorldState worldState, final List<Account> expectedAccounts) {
for (final Account expectedAccount : expectedAccounts) {
final Account actualAccount = worldState.get(expectedAccount.getAddress());
assertThat(actualAccount).isNotNull();
// Check each field
assertThat(actualAccount.getNonce()).isEqualTo(expectedAccount.getNonce());
assertThat(actualAccount.getCode()).isEqualTo(expectedAccount.getCode());
assertThat(actualAccount.getBalance()).isEqualTo(expectedAccount.getBalance());
final Map<Bytes32, AccountStorageEntry> actualStorage = actualAccount.storageEntriesFrom(Bytes32.ZERO, 500);
final Map<Bytes32, AccountStorageEntry> expectedStorage = expectedAccount.storageEntriesFrom(Bytes32.ZERO, 500);
assertThat(actualStorage).isEqualTo(expectedStorage);
}
}
Aggregations