Search in sources :

Example 1 with DebugStorageRangeAtResult

use of org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.DebugStorageRangeAtResult 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 2 with DebugStorageRangeAtResult

use of org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.DebugStorageRangeAtResult 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)

Aggregations

Bytes32 (org.apache.tuweni.bytes.Bytes32)2 JsonRpcSuccessResponse (org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse)2 DebugStorageRangeAtResult (org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.DebugStorageRangeAtResult)2 AccountStorageEntry (org.hyperledger.besu.evm.account.AccountStorageEntry)2 ArrayList (java.util.ArrayList)1 TreeMap (java.util.TreeMap)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 Account (org.hyperledger.besu.evm.account.Account)1 Test (org.junit.Test)1