Search in sources :

Example 1 with BlockchainSetupUtil

use of org.hyperledger.besu.ethereum.core.BlockchainSetupUtil in project besu by hyperledger.

the class CheckpointHeaderFetcherTest method setUpClass.

@BeforeClass
public static void setUpClass() {
    final BlockchainSetupUtil blockchainSetupUtil = BlockchainSetupUtil.forTesting(DataStorageFormat.FOREST);
    blockchainSetupUtil.importAllBlocks();
    blockchain = blockchainSetupUtil.getBlockchain();
    transactionPool = blockchainSetupUtil.getTransactionPool();
    protocolSchedule = blockchainSetupUtil.getProtocolSchedule();
    protocolContext = blockchainSetupUtil.getProtocolContext();
}
Also used : BlockchainSetupUtil(org.hyperledger.besu.ethereum.core.BlockchainSetupUtil) BeforeClass(org.junit.BeforeClass)

Example 2 with BlockchainSetupUtil

use of org.hyperledger.besu.ethereum.core.BlockchainSetupUtil in project besu by hyperledger.

the class AbstractJsonRpcHttpServiceTest method getRpcMethods.

protected Map<String, JsonRpcMethod> getRpcMethods(final JsonRpcConfiguration config, final BlockchainSetupUtil blockchainSetupUtil) {
    final ProtocolContext protocolContext = mock(ProtocolContext.class);
    final Synchronizer synchronizerMock = mock(Synchronizer.class);
    final P2PNetwork peerDiscoveryMock = mock(P2PNetwork.class);
    final TransactionPool transactionPoolMock = mock(TransactionPool.class);
    final PoWMiningCoordinator miningCoordinatorMock = mock(PoWMiningCoordinator.class);
    when(transactionPoolMock.addLocalTransaction(any(Transaction.class))).thenReturn(ValidationResult.valid());
    // nonce too low tests uses a tx with nonce=16
    when(transactionPoolMock.addLocalTransaction(argThat(tx -> tx.getNonce() == 16))).thenReturn(ValidationResult.invalid(TransactionInvalidReason.NONCE_TOO_LOW));
    final GasPricePendingTransactionsSorter pendingTransactionsMock = mock(GasPricePendingTransactionsSorter.class);
    when(transactionPoolMock.getPendingTransactions()).thenReturn(pendingTransactionsMock);
    final PrivacyParameters privacyParameters = mock(PrivacyParameters.class);
    final BlockchainQueries blockchainQueries = new BlockchainQueries(blockchainSetupUtil.getBlockchain(), blockchainSetupUtil.getWorldArchive());
    final FilterIdGenerator filterIdGenerator = mock(FilterIdGenerator.class);
    final FilterRepository filterRepository = new FilterRepository();
    when(filterIdGenerator.nextId()).thenReturn("0x1");
    filterManager = new FilterManagerBuilder().blockchainQueries(blockchainQueries).transactionPool(transactionPoolMock).filterIdGenerator(filterIdGenerator).filterRepository(filterRepository).build();
    final Set<Capability> supportedCapabilities = new HashSet<>();
    supportedCapabilities.add(EthProtocol.ETH62);
    supportedCapabilities.add(EthProtocol.ETH63);
    final NatService natService = new NatService(Optional.empty());
    return new JsonRpcMethodsFactory().methods(CLIENT_VERSION, NETWORK_ID, new StubGenesisConfigOptions(), peerDiscoveryMock, blockchainQueries, synchronizerMock, blockchainSetupUtil.getProtocolSchedule(), protocolContext, filterManager, transactionPoolMock, miningCoordinatorMock, new NoOpMetricsSystem(), supportedCapabilities, Optional.empty(), Optional.empty(), JSON_RPC_APIS, privacyParameters, config, mock(WebSocketConfiguration.class), mock(MetricsConfiguration.class), natService, new HashMap<>(), folder.getRoot().toPath(), mock(EthPeers.class));
}
Also used : ValidationResult(org.hyperledger.besu.ethereum.mainnet.ValidationResult) Arrays(java.util.Arrays) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) URL(java.net.URL) FilterRepository(org.hyperledger.besu.ethereum.api.jsonrpc.internal.filter.FilterRepository) WebSocketConfiguration(org.hyperledger.besu.ethereum.api.jsonrpc.websocket.WebSocketConfiguration) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) BlockchainSetupUtil(org.hyperledger.besu.ethereum.core.BlockchainSetupUtil) DataStorageFormat(org.hyperledger.besu.ethereum.worldstate.DataStorageFormat) Synchronizer(org.hyperledger.besu.ethereum.core.Synchronizer) FilterManagerBuilder(org.hyperledger.besu.ethereum.api.jsonrpc.internal.filter.FilterManagerBuilder) Map(java.util.Map) After(org.junit.After) BigInteger(java.math.BigInteger) PoWMiningCoordinator(org.hyperledger.besu.ethereum.blockcreation.PoWMiningCoordinator) ClassRule(org.junit.ClassRule) GasPricePendingTransactionsSorter(org.hyperledger.besu.ethereum.eth.transactions.sorter.GasPricePendingTransactionsSorter) MediaType(okhttp3.MediaType) BlockchainQueries(org.hyperledger.besu.ethereum.api.query.BlockchainQueries) Collection(java.util.Collection) Set(java.util.Set) P2PNetwork(org.hyperledger.besu.ethereum.p2p.network.P2PNetwork) EthPeers(org.hyperledger.besu.ethereum.eth.manager.EthPeers) Optional(java.util.Optional) Capability(org.hyperledger.besu.ethereum.p2p.rlpx.wire.Capability) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ChainResources(org.hyperledger.besu.testutil.BlockTestUtil.ChainResources) HashMap(java.util.HashMap) NoOpMetricsSystem(org.hyperledger.besu.metrics.noop.NoOpMetricsSystem) EthProtocol(org.hyperledger.besu.ethereum.eth.EthProtocol) HashSet(java.util.HashSet) FilterManager(org.hyperledger.besu.ethereum.api.jsonrpc.internal.filter.FilterManager) HealthService(org.hyperledger.besu.ethereum.api.jsonrpc.health.HealthService) JsonRpcMethodsFactory(org.hyperledger.besu.ethereum.api.jsonrpc.methods.JsonRpcMethodsFactory) TransactionInvalidReason(org.hyperledger.besu.ethereum.transaction.TransactionInvalidReason) Before(org.junit.Before) TransactionPool(org.hyperledger.besu.ethereum.eth.transactions.TransactionPool) Vertx(io.vertx.core.Vertx) StubGenesisConfigOptions(org.hyperledger.besu.config.StubGenesisConfigOptions) Mockito.when(org.mockito.Mockito.when) MetricsConfiguration(org.hyperledger.besu.metrics.prometheus.MetricsConfiguration) OkHttpClient(okhttp3.OkHttpClient) ProtocolContext(org.hyperledger.besu.ethereum.ProtocolContext) FilterIdGenerator(org.hyperledger.besu.ethereum.api.jsonrpc.internal.filter.FilterIdGenerator) Transaction(org.hyperledger.besu.ethereum.core.Transaction) JsonRpcMethod(org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod) PrivacyParameters(org.hyperledger.besu.ethereum.core.PrivacyParameters) NatService(org.hyperledger.besu.nat.NatService) TemporaryFolder(org.junit.rules.TemporaryFolder) Synchronizer(org.hyperledger.besu.ethereum.core.Synchronizer) TransactionPool(org.hyperledger.besu.ethereum.eth.transactions.TransactionPool) Capability(org.hyperledger.besu.ethereum.p2p.rlpx.wire.Capability) WebSocketConfiguration(org.hyperledger.besu.ethereum.api.jsonrpc.websocket.WebSocketConfiguration) NatService(org.hyperledger.besu.nat.NatService) P2PNetwork(org.hyperledger.besu.ethereum.p2p.network.P2PNetwork) FilterManagerBuilder(org.hyperledger.besu.ethereum.api.jsonrpc.internal.filter.FilterManagerBuilder) EthPeers(org.hyperledger.besu.ethereum.eth.manager.EthPeers) PrivacyParameters(org.hyperledger.besu.ethereum.core.PrivacyParameters) JsonRpcMethodsFactory(org.hyperledger.besu.ethereum.api.jsonrpc.methods.JsonRpcMethodsFactory) Transaction(org.hyperledger.besu.ethereum.core.Transaction) MetricsConfiguration(org.hyperledger.besu.metrics.prometheus.MetricsConfiguration) BlockchainQueries(org.hyperledger.besu.ethereum.api.query.BlockchainQueries) FilterRepository(org.hyperledger.besu.ethereum.api.jsonrpc.internal.filter.FilterRepository) FilterIdGenerator(org.hyperledger.besu.ethereum.api.jsonrpc.internal.filter.FilterIdGenerator) NoOpMetricsSystem(org.hyperledger.besu.metrics.noop.NoOpMetricsSystem) ProtocolContext(org.hyperledger.besu.ethereum.ProtocolContext) PoWMiningCoordinator(org.hyperledger.besu.ethereum.blockcreation.PoWMiningCoordinator) GasPricePendingTransactionsSorter(org.hyperledger.besu.ethereum.eth.transactions.sorter.GasPricePendingTransactionsSorter) StubGenesisConfigOptions(org.hyperledger.besu.config.StubGenesisConfigOptions) HashSet(java.util.HashSet)

Example 3 with BlockchainSetupUtil

use of org.hyperledger.besu.ethereum.core.BlockchainSetupUtil in project besu by hyperledger.

the class Istanbul99ProtocolManagerTest method setup.

@BeforeClass
public static void setup() {
    final BlockchainSetupUtil blockchainSetupUtil = BlockchainSetupUtil.forTesting(DataStorageFormat.FOREST);
    blockchainSetupUtil.importAllBlocks();
    blockchain = blockchainSetupUtil.getBlockchain();
    transactionPool = blockchainSetupUtil.getTransactionPool();
    protocolSchedule = blockchainSetupUtil.getProtocolSchedule();
    protocolContext = blockchainSetupUtil.getProtocolContext();
    assertThat(blockchainSetupUtil.getMaxBlockNumber()).isGreaterThanOrEqualTo(20L);
}
Also used : BlockchainSetupUtil(org.hyperledger.besu.ethereum.core.BlockchainSetupUtil) BeforeClass(org.junit.BeforeClass)

Example 4 with BlockchainSetupUtil

use of org.hyperledger.besu.ethereum.core.BlockchainSetupUtil in project besu by hyperledger.

the class EthJsonRpcHttpServiceTest method getFilterChanges_oneBlock.

@Test
public void getFilterChanges_oneBlock() throws Exception {
    BlockchainSetupUtil blockchainSetupUtil = startServiceWithEmptyChain(DataStorageFormat.FOREST);
    final String expectedRespBody = String.format("{%n  \"jsonrpc\" : \"2.0\",%n  \"id\" : 2,%n  \"result\" : [ \"0x10aaf14a53caf27552325374429d3558398a36d3682ede6603c2c6511896e9f9\" ]%n}");
    final ResponseBody body = ethNewBlockFilter(1).body();
    final String result = getResult(body);
    body.close();
    // Import genesis + first block
    blockchainSetupUtil.importFirstBlocks(2);
    final Response resp = ethGetFilterChanges(2, result);
    assertThat(resp.code()).isEqualTo(200);
    assertThat(resp.body().string()).isEqualTo(expectedRespBody);
}
Also used : Response(okhttp3.Response) BlockchainSetupUtil(org.hyperledger.besu.ethereum.core.BlockchainSetupUtil) ResponseBody(okhttp3.ResponseBody) Test(org.junit.Test) AbstractJsonRpcHttpServiceTest(org.hyperledger.besu.ethereum.api.jsonrpc.AbstractJsonRpcHttpServiceTest)

Example 5 with BlockchainSetupUtil

use of org.hyperledger.besu.ethereum.core.BlockchainSetupUtil in project besu by hyperledger.

the class FastSyncChainDownloaderTest method recoversFromSyncTargetDisconnect.

@Test
public void recoversFromSyncTargetDisconnect() {
    final BlockchainSetupUtil shorterChainUtil = BlockchainSetupUtil.forTesting(storageFormat);
    final MutableBlockchain shorterChain = shorterChainUtil.getBlockchain();
    otherBlockchainSetup.importFirstBlocks(30);
    shorterChainUtil.importFirstBlocks(28);
    final RespondingEthPeer bestPeer = EthProtocolManagerTestUtil.createPeer(ethProtocolManager, otherBlockchain);
    final RespondingEthPeer secondBestPeer = EthProtocolManagerTestUtil.createPeer(ethProtocolManager, shorterChain);
    final RespondingEthPeer.Responder shorterResponder = RespondingEthPeer.blockchainResponder(shorterChain);
    // Doesn't respond to requests for checkpoints unless it's starting from genesis
    // So the import can only make it as far as block 15 (3 checkpoints 5 blocks apart)
    final RespondingEthPeer.Responder shorterLimitedRangeResponder = RespondingEthPeer.targetedResponder((cap, msg) -> {
        if (msg.getCode() == EthPV62.GET_BLOCK_HEADERS) {
            final GetBlockHeadersMessage request = GetBlockHeadersMessage.readFrom(msg);
            return request.skip() == 0 || (request.hash().equals(localBlockchain.getBlockHashByNumber(0)));
        } else {
            return true;
        }
    }, (cap, msg) -> shorterResponder.respond(cap, msg).get());
    final SynchronizerConfiguration syncConfig = SynchronizerConfiguration.builder().downloaderChainSegmentSize(5).downloaderHeadersRequestSize(3).downloaderParallelism(1).build();
    final long pivotBlockNumber = 25;
    final ChainDownloader downloader = downloader(syncConfig, pivotBlockNumber);
    final CompletableFuture<Void> result = downloader.start();
    while (localBlockchain.getChainHeadBlockNumber() < 15) {
        bestPeer.respond(shorterLimitedRangeResponder);
        secondBestPeer.respond(shorterLimitedRangeResponder);
        LockSupport.parkNanos(200);
    }
    assertThat(localBlockchain.getChainHeadBlockNumber()).isEqualTo(15);
    assertThat(result).isNotCompleted();
    ethProtocolManager.handleDisconnect(bestPeer.getPeerConnection(), TOO_MANY_PEERS, true);
    secondBestPeer.respondWhileOtherThreadsWork(shorterResponder, () -> !result.isDone());
    assertThat(result).isCompleted();
    assertThat(localBlockchain.getChainHeadBlockNumber()).isEqualTo(pivotBlockNumber);
    assertThat(localBlockchain.getChainHeadHeader()).isEqualTo(otherBlockchain.getBlockHeader(pivotBlockNumber).get());
}
Also used : GetBlockHeadersMessage(org.hyperledger.besu.ethereum.eth.messages.GetBlockHeadersMessage) BlockchainSetupUtil(org.hyperledger.besu.ethereum.core.BlockchainSetupUtil) RespondingEthPeer(org.hyperledger.besu.ethereum.eth.manager.RespondingEthPeer) MutableBlockchain(org.hyperledger.besu.ethereum.chain.MutableBlockchain) SynchronizerConfiguration(org.hyperledger.besu.ethereum.eth.sync.SynchronizerConfiguration) ChainDownloader(org.hyperledger.besu.ethereum.eth.sync.ChainDownloader) Test(org.junit.Test)

Aggregations

BlockchainSetupUtil (org.hyperledger.besu.ethereum.core.BlockchainSetupUtil)16 BeforeClass (org.junit.BeforeClass)7 Before (org.junit.Before)6 NoOpMetricsSystem (org.hyperledger.besu.metrics.noop.NoOpMetricsSystem)4 EthScheduler (org.hyperledger.besu.ethereum.eth.manager.EthScheduler)3 ProtocolContext (org.hyperledger.besu.ethereum.ProtocolContext)2 MutableBlockchain (org.hyperledger.besu.ethereum.chain.MutableBlockchain)2 Test (org.junit.Test)2 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 Vertx (io.vertx.core.Vertx)1 BigInteger (java.math.BigInteger)1 URL (java.net.URL)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Set (java.util.Set)1 MediaType (okhttp3.MediaType)1