Search in sources :

Example 6 with Account

use of org.hyperledger.besu.evm.account.Account in project besu by hyperledger.

the class MainnetTransactionValidatorTest method account.

private Account account(final Wei balance, final long nonce) {
    final Account account = mock(Account.class);
    when(account.getBalance()).thenReturn(balance);
    when(account.getNonce()).thenReturn(nonce);
    return account;
}
Also used : Account(org.hyperledger.besu.evm.account.Account)

Example 7 with Account

use of org.hyperledger.besu.evm.account.Account in project besu by hyperledger.

the class MainnetTransactionValidatorTest method shouldAcceptTransactionWhenTransactionNonceAboveAccountNonceAndFutureNonceIsAllowed.

@Test
public void shouldAcceptTransactionWhenTransactionNonceAboveAccountNonceAndFutureNonceIsAllowed() {
    final MainnetTransactionValidator validator = new MainnetTransactionValidator(gasCalculator, false, Optional.of(BigInteger.ONE), defaultGoQuorumCompatibilityMode);
    final Account account = accountWithNonce(basicTransaction.getNonce() - 1);
    assertThat(validator.validateForSender(basicTransaction, account, true)).isEqualTo(ValidationResult.valid());
}
Also used : Account(org.hyperledger.besu.evm.account.Account) Test(org.junit.Test)

Example 8 with Account

use of org.hyperledger.besu.evm.account.Account in project besu by hyperledger.

the class EthGetCode method resultByBlockHash.

@Override
protected String resultByBlockHash(final JsonRpcRequestContext request, final Hash blockHash) {
    final Address address = request.getRequiredParameter(0, Address.class);
    if (privacyParameters.isPresent() && privacyParameters.get().getGoQuorumPrivacyParameters().isPresent()) {
        // get from private state if we can
        final Optional<BlockHeader> blockHeader = blockchainQueries.get().getBlockHeaderByHash(blockHash);
        if (blockHeader.isPresent()) {
            final MutableWorldState privateState = getPrivateWorldStateAtBlock(privacyParameters.get().getGoQuorumPrivacyParameters(), blockHeader.get());
            final Account privAccount = privateState.get(address);
            if (privAccount != null) {
                return privAccount.getCode().toHexString();
            }
        }
    }
    return getBlockchainQueries().getCode(address, blockHash).map(Bytes::toString).orElse(null);
}
Also used : Account(org.hyperledger.besu.evm.account.Account) Address(org.hyperledger.besu.datatypes.Address) MutableWorldState(org.hyperledger.besu.ethereum.core.MutableWorldState) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader)

Example 9 with Account

use of org.hyperledger.besu.evm.account.Account in project besu by hyperledger.

the class FastWorldStateDownloaderTest method downloadAvailableWorldStateFromPeers.

private void downloadAvailableWorldStateFromPeers(final int peerCount, final int accountCount, final int hashesPerRequest, final int maxOutstandingRequests, final NetworkResponder networkResponder) {
    final int trailingPeerCount = 5;
    // Setup "remote" state
    final WorldStateStorage remoteStorage = new WorldStateKeyValueStorage(new InMemoryKeyValueStorage());
    final WorldStateArchive remoteWorldStateArchive = new DefaultWorldStateArchive(remoteStorage, createPreimageStorage());
    final MutableWorldState remoteWorldState = remoteWorldStateArchive.getMutable();
    // Generate accounts and save corresponding state root
    final List<Account> accounts = dataGen.createRandomAccounts(remoteWorldState, accountCount);
    final Hash stateRoot = remoteWorldState.rootHash();
    // Sanity check
    assertThat(stateRoot).isNotEqualTo(EMPTY_TRIE_ROOT);
    final BlockHeader header = dataGen.block(BlockOptions.create().setStateRoot(stateRoot).setBlockNumber(10)).getHeader();
    // Generate more data that should not be downloaded
    final List<Account> otherAccounts = dataGen.createRandomAccounts(remoteWorldState, 5);
    final Hash otherStateRoot = remoteWorldState.rootHash();
    final BlockHeader otherHeader = dataGen.block(BlockOptions.create().setStateRoot(otherStateRoot).setBlockNumber(11)).getHeader();
    // Sanity check
    assertThat(otherStateRoot).isNotEqualTo(stateRoot);
    final InMemoryTasksPriorityQueues<NodeDataRequest> taskCollection = new InMemoryTasksPriorityQueues<>();
    final WorldStateStorage localStorage = new WorldStateKeyValueStorage(new InMemoryKeyValueStorage());
    final WorldStateArchive localWorldStateArchive = new DefaultWorldStateArchive(localStorage, createPreimageStorage());
    final SynchronizerConfiguration syncConfig = SynchronizerConfiguration.builder().worldStateHashCountPerRequest(hashesPerRequest).worldStateRequestParallelism(maxOutstandingRequests).build();
    final WorldStateDownloader downloader = createDownloader(syncConfig, ethProtocolManager.ethContext(), localStorage, taskCollection);
    // Create some peers that can respond
    final List<RespondingEthPeer> usefulPeers = Stream.generate(() -> EthProtocolManagerTestUtil.createPeer(ethProtocolManager, header.getNumber())).limit(peerCount).collect(Collectors.toList());
    // And some irrelevant peers
    final List<RespondingEthPeer> trailingPeers = Stream.generate(() -> EthProtocolManagerTestUtil.createPeer(ethProtocolManager, header.getNumber() - 1L)).limit(trailingPeerCount).collect(Collectors.toList());
    // Start downloader
    final CompletableFuture<?> result = downloader.run(null, new FastSyncState(header));
    // A second run should return an error without impacting the first result
    final CompletableFuture<?> secondResult = downloader.run(null, new FastSyncState(header));
    assertThat(secondResult).isCompletedExceptionally();
    assertThat(result).isNotCompletedExceptionally();
    // Respond to node data requests
    // Send one round of full responses, so that we can get multiple requests queued up
    final RespondingEthPeer.Responder fullResponder = RespondingEthPeer.blockchainResponder(mock(Blockchain.class), remoteWorldStateArchive);
    for (final RespondingEthPeer peer : usefulPeers) {
        peer.respond(fullResponder);
    }
    // Respond to remaining queued requests in custom way
    networkResponder.respond(usefulPeers, remoteWorldStateArchive, result);
    // Check that trailing peers were not queried for data
    for (final RespondingEthPeer trailingPeer : trailingPeers) {
        assertThat(trailingPeer.hasOutstandingRequests()).isFalse();
    }
    // Check that all expected account data was downloaded
    final WorldState localWorldState = localWorldStateArchive.get(stateRoot, null).get();
    assertThat(result).isDone();
    assertAccountsMatch(localWorldState, accounts);
    // We shouldn't have any extra data locally
    assertThat(localStorage.contains(otherHeader.getStateRoot())).isFalse();
    for (final Account otherAccount : otherAccounts) {
        assertThat(localWorldState.get(otherAccount.getAddress())).isNull();
    }
}
Also used : WorldStateStorage(org.hyperledger.besu.ethereum.worldstate.WorldStateStorage) Account(org.hyperledger.besu.evm.account.Account) DefaultWorldStateArchive(org.hyperledger.besu.ethereum.worldstate.DefaultWorldStateArchive) InMemoryKeyValueStorage(org.hyperledger.besu.services.kvstore.InMemoryKeyValueStorage) MutableWorldState(org.hyperledger.besu.ethereum.core.MutableWorldState) RespondingEthPeer(org.hyperledger.besu.ethereum.eth.manager.RespondingEthPeer) Blockchain(org.hyperledger.besu.ethereum.chain.Blockchain) InMemoryTasksPriorityQueues(org.hyperledger.besu.services.tasks.InMemoryTasksPriorityQueues) WorldState(org.hyperledger.besu.evm.worldstate.WorldState) MutableWorldState(org.hyperledger.besu.ethereum.core.MutableWorldState) Hash(org.hyperledger.besu.datatypes.Hash) WorldStateDownloader(org.hyperledger.besu.ethereum.eth.sync.worldstate.WorldStateDownloader) FastSyncState(org.hyperledger.besu.ethereum.eth.sync.fastsync.FastSyncState) WorldStateKeyValueStorage(org.hyperledger.besu.ethereum.storage.keyvalue.WorldStateKeyValueStorage) InMemoryKeyValueStorageProvider.createInMemoryWorldStateArchive(org.hyperledger.besu.ethereum.core.InMemoryKeyValueStorageProvider.createInMemoryWorldStateArchive) DefaultWorldStateArchive(org.hyperledger.besu.ethereum.worldstate.DefaultWorldStateArchive) WorldStateArchive(org.hyperledger.besu.ethereum.worldstate.WorldStateArchive) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) SynchronizerConfiguration(org.hyperledger.besu.ethereum.eth.sync.SynchronizerConfiguration)

Example 10 with Account

use of org.hyperledger.besu.evm.account.Account in project besu by hyperledger.

the class FastWorldStateDownloaderTest method canRecoverFromTimeouts.

@Test
public void canRecoverFromTimeouts() {
    final DeterministicEthScheduler.TimeoutPolicy timeoutPolicy = DeterministicEthScheduler.TimeoutPolicy.timeoutXTimes(2);
    final EthProtocolManager ethProtocolManager = EthProtocolManagerTestUtil.create(timeoutPolicy);
    final MockExecutorService serviceExecutor = ((DeterministicEthScheduler) ethProtocolManager.ethContext().getScheduler()).mockServiceExecutor();
    serviceExecutor.setAutoRun(false);
    // Setup "remote" state
    final WorldStateArchive remoteWorldStateArchive = createInMemoryWorldStateArchive();
    final MutableWorldState remoteWorldState = remoteWorldStateArchive.getMutable();
    // Generate accounts and save corresponding state root
    final List<Account> accounts = dataGen.createRandomAccounts(remoteWorldState, 20);
    final Hash stateRoot = remoteWorldState.rootHash();
    // Sanity check
    assertThat(stateRoot).isNotEqualTo(EMPTY_TRIE_ROOT);
    final BlockHeader header = dataGen.block(BlockOptions.create().setStateRoot(stateRoot).setBlockNumber(10)).getHeader();
    // Create some peers
    final List<RespondingEthPeer> peers = Stream.generate(() -> EthProtocolManagerTestUtil.createPeer(ethProtocolManager, header.getNumber())).limit(5).collect(Collectors.toList());
    final InMemoryTasksPriorityQueues<NodeDataRequest> taskCollection = new InMemoryTasksPriorityQueues<>();
    final WorldStateStorage localStorage = new WorldStateKeyValueStorage(new InMemoryKeyValueStorage());
    final WorldStateDownloader downloader = createDownloader(ethProtocolManager.ethContext(), localStorage, taskCollection);
    final FastSyncState fastSyncState = new FastSyncState(header);
    final CompletableFuture<Void> result = downloader.run(null, fastSyncState);
    serviceExecutor.runPendingFuturesInSeparateThreads(persistenceThread);
    // Respond to node data requests
    final RespondingEthPeer.Responder responder = RespondingEthPeer.blockchainResponder(mock(Blockchain.class), remoteWorldStateArchive);
    respondUntilDone(peers, responder, result);
    // Check that all expected account data was downloaded
    final WorldStateArchive localWorldStateArchive = new DefaultWorldStateArchive(localStorage, createPreimageStorage());
    final WorldState localWorldState = localWorldStateArchive.get(stateRoot, null).get();
    assertThat(result).isDone();
    assertAccountsMatch(localWorldState, accounts);
}
Also used : Account(org.hyperledger.besu.evm.account.Account) WorldStateStorage(org.hyperledger.besu.ethereum.worldstate.WorldStateStorage) MutableWorldState(org.hyperledger.besu.ethereum.core.MutableWorldState) RespondingEthPeer(org.hyperledger.besu.ethereum.eth.manager.RespondingEthPeer) Blockchain(org.hyperledger.besu.ethereum.chain.Blockchain) DeterministicEthScheduler(org.hyperledger.besu.ethereum.eth.manager.DeterministicEthScheduler) WorldState(org.hyperledger.besu.evm.worldstate.WorldState) MutableWorldState(org.hyperledger.besu.ethereum.core.MutableWorldState) Hash(org.hyperledger.besu.datatypes.Hash) WorldStateDownloader(org.hyperledger.besu.ethereum.eth.sync.worldstate.WorldStateDownloader) EthProtocolManager(org.hyperledger.besu.ethereum.eth.manager.EthProtocolManager) WorldStateKeyValueStorage(org.hyperledger.besu.ethereum.storage.keyvalue.WorldStateKeyValueStorage) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) DefaultWorldStateArchive(org.hyperledger.besu.ethereum.worldstate.DefaultWorldStateArchive) InMemoryKeyValueStorage(org.hyperledger.besu.services.kvstore.InMemoryKeyValueStorage) InMemoryTasksPriorityQueues(org.hyperledger.besu.services.tasks.InMemoryTasksPriorityQueues) FastSyncState(org.hyperledger.besu.ethereum.eth.sync.fastsync.FastSyncState) InMemoryKeyValueStorageProvider.createInMemoryWorldStateArchive(org.hyperledger.besu.ethereum.core.InMemoryKeyValueStorageProvider.createInMemoryWorldStateArchive) DefaultWorldStateArchive(org.hyperledger.besu.ethereum.worldstate.DefaultWorldStateArchive) WorldStateArchive(org.hyperledger.besu.ethereum.worldstate.WorldStateArchive) MockExecutorService(org.hyperledger.besu.testutil.MockExecutorService) Test(org.junit.Test)

Aggregations

Account (org.hyperledger.besu.evm.account.Account)56 Address (org.hyperledger.besu.datatypes.Address)24 BlockHeader (org.hyperledger.besu.ethereum.core.BlockHeader)19 Hash (org.hyperledger.besu.datatypes.Hash)18 Bytes (org.apache.tuweni.bytes.Bytes)15 Bytes32 (org.apache.tuweni.bytes.Bytes32)14 MutableWorldState (org.hyperledger.besu.ethereum.core.MutableWorldState)14 Test (org.junit.Test)13 Optional (java.util.Optional)12 WorldState (org.hyperledger.besu.evm.worldstate.WorldState)12 Map (java.util.Map)11 Wei (org.hyperledger.besu.datatypes.Wei)10 Blockchain (org.hyperledger.besu.ethereum.chain.Blockchain)10 WorldUpdater (org.hyperledger.besu.evm.worldstate.WorldUpdater)10 Transaction (org.hyperledger.besu.ethereum.core.Transaction)9 WorldStateArchive (org.hyperledger.besu.ethereum.worldstate.WorldStateArchive)9 ArrayList (java.util.ArrayList)8 List (java.util.List)8 HashSet (java.util.HashSet)7 Set (java.util.Set)7