Search in sources :

Example 1 with AccountStorageEntry

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);
}
Also used : MutableWorldState(org.hyperledger.besu.ethereum.core.MutableWorldState) WorldUpdater(org.hyperledger.besu.evm.worldstate.WorldUpdater) ArrayList(java.util.ArrayList) MutableAccount(org.hyperledger.besu.evm.account.MutableAccount) AccountStorageEntry(org.hyperledger.besu.evm.account.AccountStorageEntry) TreeMap(java.util.TreeMap) Bytes32(org.apache.tuweni.bytes.Bytes32) Test(org.junit.Test)

Example 2 with AccountStorageEntry

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));
}
Also used : Account(org.hyperledger.besu.evm.account.Account) DebugStorageRangeAtResult(org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.DebugStorageRangeAtResult) AccountStorageEntry(org.hyperledger.besu.evm.account.AccountStorageEntry) JsonRpcSuccessResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse) Bytes32(org.apache.tuweni.bytes.Bytes32)

Example 3 with AccountStorageEntry

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);
}
Also used : MutableWorldState(org.hyperledger.besu.ethereum.core.MutableWorldState) Random(java.util.Random) WorldUpdater(org.hyperledger.besu.evm.worldstate.WorldUpdater) MutableAccount(org.hyperledger.besu.evm.account.MutableAccount) AccountStorageEntry(org.hyperledger.besu.evm.account.AccountStorageEntry) TreeMap(java.util.TreeMap) Bytes32(org.apache.tuweni.bytes.Bytes32) Test(org.junit.jupiter.api.Test)

Example 4 with AccountStorageEntry

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)));
}
Also used : JsonRpcRequest(org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequest) DebugStorageRangeAtResult(org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.DebugStorageRangeAtResult) ArrayList(java.util.ArrayList) BlockWithMetadata(org.hyperledger.besu.ethereum.api.query.BlockWithMetadata) Hash(org.hyperledger.besu.datatypes.Hash) AccountStorageEntry(org.hyperledger.besu.evm.account.AccountStorageEntry) TreeMap(java.util.TreeMap) Bytes32(org.apache.tuweni.bytes.Bytes32) AccountStorageEntry(org.hyperledger.besu.evm.account.AccountStorageEntry) JsonRpcRequestContext(org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext) TransactionWithMetadata(org.hyperledger.besu.ethereum.api.query.TransactionWithMetadata) JsonRpcSuccessResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse) Test(org.junit.Test)

Example 5 with AccountStorageEntry

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);
    }
}
Also used : Account(org.hyperledger.besu.evm.account.Account) AccountStorageEntry(org.hyperledger.besu.evm.account.AccountStorageEntry) Bytes32(org.apache.tuweni.bytes.Bytes32)

Aggregations

Bytes32 (org.apache.tuweni.bytes.Bytes32)5 AccountStorageEntry (org.hyperledger.besu.evm.account.AccountStorageEntry)5 TreeMap (java.util.TreeMap)3 ArrayList (java.util.ArrayList)2 JsonRpcSuccessResponse (org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse)2 DebugStorageRangeAtResult (org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.DebugStorageRangeAtResult)2 MutableWorldState (org.hyperledger.besu.ethereum.core.MutableWorldState)2 Account (org.hyperledger.besu.evm.account.Account)2 MutableAccount (org.hyperledger.besu.evm.account.MutableAccount)2 WorldUpdater (org.hyperledger.besu.evm.worldstate.WorldUpdater)2 Test (org.junit.Test)2 Random (java.util.Random)1 Hash (org.hyperledger.besu.datatypes.Hash)1 JsonRpcRequest (org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequest)1 JsonRpcRequestContext (org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext)1 BlockWithMetadata (org.hyperledger.besu.ethereum.api.query.BlockWithMetadata)1 TransactionWithMetadata (org.hyperledger.besu.ethereum.api.query.TransactionWithMetadata)1 Test (org.junit.jupiter.api.Test)1