Search in sources :

Example 1 with RespondingEthPeer

use of org.hyperledger.besu.ethereum.eth.manager.RespondingEthPeer in project besu by hyperledger.

the class TransactionPoolTest method shouldSendFullTransactionPoolToNewlyConnectedPeer.

@Test
public void shouldSendFullTransactionPoolToNewlyConnectedPeer() {
    EthProtocolManager ethProtocolManager = EthProtocolManagerTestUtil.create();
    EthContext ethContext = ethProtocolManager.ethContext();
    TransactionPool transactionPool = new TransactionPool(transactions, protocolSchedule, protocolContext, transactionBroadcaster, ethContext, new MiningParameters.Builder().minTransactionGasPrice(Wei.ZERO).build(), metricsSystem, TransactionPoolConfiguration.DEFAULT);
    final TransactionTestFixture builder = new TransactionTestFixture();
    final Transaction transactionLocal = builder.nonce(1).createTransaction(KEY_PAIR1);
    final Transaction transactionRemote = builder.nonce(2).createTransaction(KEY_PAIR1);
    when(transactionValidator.validate(any(Transaction.class), any(Optional.class), any())).thenReturn(valid());
    when(transactionValidator.validateForSender(any(Transaction.class), nullable(Account.class), any(TransactionValidationParams.class))).thenReturn(valid());
    transactionPool.addLocalTransaction(transactionLocal);
    transactionPool.addRemoteTransactions(Collections.singletonList(transactionRemote));
    RespondingEthPeer peer = EthProtocolManagerTestUtil.createPeer(ethProtocolManager);
    Set<Transaction> transactionsToSendToPeer = peerTransactionTracker.claimTransactionsToSendToPeer(peer.getEthPeer());
    assertThat(transactionsToSendToPeer).contains(transactionLocal, transactionRemote);
}
Also used : EthProtocolManager(org.hyperledger.besu.ethereum.eth.manager.EthProtocolManager) EthContext(org.hyperledger.besu.ethereum.eth.manager.EthContext) MiningParameters(org.hyperledger.besu.ethereum.core.MiningParameters) Account(org.hyperledger.besu.evm.account.Account) TransactionTestFixture(org.hyperledger.besu.ethereum.core.TransactionTestFixture) Transaction(org.hyperledger.besu.ethereum.core.Transaction) Optional(java.util.Optional) RespondingEthPeer(org.hyperledger.besu.ethereum.eth.manager.RespondingEthPeer) TransactionValidationParams(org.hyperledger.besu.ethereum.mainnet.TransactionValidationParams) Test(org.junit.Test)

Example 2 with RespondingEthPeer

use of org.hyperledger.besu.ethereum.eth.manager.RespondingEthPeer in project besu by hyperledger.

the class PivotBlockRetrieverTest method shouldIgnorePeersThatDoNotHaveThePivotBlock.

@Test
public void shouldIgnorePeersThatDoNotHaveThePivotBlock() {
    pivotBlockRetriever = createPivotBlockRetriever(3, 1, 1);
    EthProtocolManagerTestUtil.disableEthSchedulerAutoRun(ethProtocolManager);
    final Responder responder = RespondingEthPeer.blockchainResponder(blockchain, protocolContext.getWorldStateArchive(), transactionPool);
    final RespondingEthPeer respondingPeerA = EthProtocolManagerTestUtil.createPeer(ethProtocolManager, 1000);
    final RespondingEthPeer badPeerA = EthProtocolManagerTestUtil.createPeer(ethProtocolManager, 1);
    final RespondingEthPeer badPeerB = EthProtocolManagerTestUtil.createPeer(ethProtocolManager, 1);
    final CompletableFuture<FastSyncState> future = pivotBlockRetriever.downloadPivotBlockHeader();
    respondingPeerA.respond(responder);
    // With only one peer with sufficient height, we should not be done yet
    assertThat(future).isNotCompleted();
    // Check that invalid peers were not queried
    assertThat(badPeerA.hasOutstandingRequests()).isFalse();
    assertThat(badPeerB.hasOutstandingRequests()).isFalse();
    // Add new peer that we can query
    final RespondingEthPeer respondingPeerB = EthProtocolManagerTestUtil.createPeer(ethProtocolManager, 1000);
    respondingPeerB.respond(responder);
    // We need one more responsive peer before we're done
    assertThat(future).isNotCompleted();
    assertThat(badPeerA.hasOutstandingRequests()).isFalse();
    assertThat(badPeerB.hasOutstandingRequests()).isFalse();
    // Add new peer that we can query
    final RespondingEthPeer respondingPeerC = EthProtocolManagerTestUtil.createPeer(ethProtocolManager, 1000);
    respondingPeerC.respond(responder);
    assertThat(badPeerA.hasOutstandingRequests()).isFalse();
    assertThat(badPeerB.hasOutstandingRequests()).isFalse();
    assertThat(future).isCompletedWithValue(new FastSyncState(blockchain.getBlockHeader(PIVOT_BLOCK_NUMBER).get()));
}
Also used : RespondingEthPeer(org.hyperledger.besu.ethereum.eth.manager.RespondingEthPeer) Responder(org.hyperledger.besu.ethereum.eth.manager.RespondingEthPeer.Responder) Test(org.junit.Test)

Example 3 with RespondingEthPeer

use of org.hyperledger.besu.ethereum.eth.manager.RespondingEthPeer in project besu by hyperledger.

the class PivotBlockRetrieverTest method shouldRetryWhenPeersDisagreeOnPivot_exceedMaxRetries.

@Test
public void shouldRetryWhenPeersDisagreeOnPivot_exceedMaxRetries() {
    final long pivotBlockDelta = 1;
    pivotBlockRetriever = createPivotBlockRetriever(2, pivotBlockDelta, 1);
    final Responder responderA = RespondingEthPeer.blockchainResponder(blockchain, protocolContext.getWorldStateArchive(), transactionPool);
    final RespondingEthPeer respondingPeerA = EthProtocolManagerTestUtil.createPeer(ethProtocolManager, 1000);
    final Responder responderB = responderForFakeBlocks(PIVOT_BLOCK_NUMBER, PIVOT_BLOCK_NUMBER - pivotBlockDelta);
    final RespondingEthPeer respondingPeerB = EthProtocolManagerTestUtil.createPeer(ethProtocolManager, 1000);
    // Execute task and wait for response
    final CompletableFuture<FastSyncState> future = pivotBlockRetriever.downloadPivotBlockHeader();
    respondingPeerA.respond(responderA);
    respondingPeerB.respond(responderB);
    // When disagreement is detected, we should push the pivot block back and retry
    final long newPivotBlock = PIVOT_BLOCK_NUMBER - 1;
    assertThat(future).isNotCompleted();
    assertThat(pivotBlockRetriever.pivotBlockNumber).hasValue(newPivotBlock);
    // Another round of invalid responses should lead to failure
    respondingPeerA.respond(responderA);
    respondingPeerB.respond(responderB);
    assertThat(future).isCompletedExceptionally();
    assertThatThrownBy(future::get).hasRootCauseInstanceOf(FastSyncException.class).extracting(e -> ((FastSyncException) ExceptionUtils.rootCause(e)).getError()).isEqualTo(FastSyncError.PIVOT_BLOCK_HEADER_MISMATCH);
}
Also used : Arrays(java.util.Arrays) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) RunWith(org.junit.runner.RunWith) Parameters(org.junit.runners.Parameterized.Parameters) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) NoOpMetricsSystem(org.hyperledger.besu.metrics.noop.NoOpMetricsSystem) Bytes(org.apache.tuweni.bytes.Bytes) Mockito.spy(org.mockito.Mockito.spy) ExceptionUtils(org.hyperledger.besu.util.ExceptionUtils) EthProtocolManagerTestUtil(org.hyperledger.besu.ethereum.eth.manager.EthProtocolManagerTestUtil) ProtocolSchedule(org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule) Responder(org.hyperledger.besu.ethereum.eth.manager.RespondingEthPeer.Responder) BlockchainSetupUtil(org.hyperledger.besu.ethereum.core.BlockchainSetupUtil) DataStorageFormat(org.hyperledger.besu.ethereum.worldstate.DataStorageFormat) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) Parameterized(org.junit.runners.Parameterized) Before(org.junit.Before) Difficulty(org.hyperledger.besu.ethereum.core.Difficulty) TransactionPool(org.hyperledger.besu.ethereum.eth.transactions.TransactionPool) EthProtocolConfiguration(org.hyperledger.besu.ethereum.eth.EthProtocolConfiguration) Collection(java.util.Collection) Blockchain(org.hyperledger.besu.ethereum.chain.Blockchain) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) BlockHeaderTestFixture(org.hyperledger.besu.ethereum.core.BlockHeaderTestFixture) RespondingEthPeer(org.hyperledger.besu.ethereum.eth.manager.RespondingEthPeer) EthProtocolManager(org.hyperledger.besu.ethereum.eth.manager.EthProtocolManager) PeerValidator(org.hyperledger.besu.ethereum.eth.peervalidation.PeerValidator) ProtocolContext(org.hyperledger.besu.ethereum.ProtocolContext) Optional(java.util.Optional) MetricsSystem(org.hyperledger.besu.plugin.services.MetricsSystem) MutableBlockchain(org.hyperledger.besu.ethereum.chain.MutableBlockchain) Mockito.mock(org.mockito.Mockito.mock) RespondingEthPeer(org.hyperledger.besu.ethereum.eth.manager.RespondingEthPeer) Responder(org.hyperledger.besu.ethereum.eth.manager.RespondingEthPeer.Responder) Test(org.junit.Test)

Example 4 with RespondingEthPeer

use of org.hyperledger.besu.ethereum.eth.manager.RespondingEthPeer in project besu by hyperledger.

the class PivotBlockRetrieverTest method shouldSucceedWhenAllPeersAgree.

@Test
public void shouldSucceedWhenAllPeersAgree() {
    final RespondingEthPeer.Responder responder = RespondingEthPeer.blockchainResponder(blockchain, protocolContext.getWorldStateArchive(), transactionPool);
    final RespondingEthPeer respondingPeerA = EthProtocolManagerTestUtil.createPeer(ethProtocolManager, 1000);
    final RespondingEthPeer respondingPeerB = EthProtocolManagerTestUtil.createPeer(ethProtocolManager, 1000);
    final RespondingEthPeer respondingPeerC = EthProtocolManagerTestUtil.createPeer(ethProtocolManager, 1000);
    final CompletableFuture<FastSyncState> future = pivotBlockRetriever.downloadPivotBlockHeader();
    respondingPeerA.respond(responder);
    respondingPeerB.respond(responder);
    respondingPeerC.respond(responder);
    assertThat(future).isCompletedWithValue(new FastSyncState(blockchain.getBlockHeader(PIVOT_BLOCK_NUMBER).get()));
}
Also used : RespondingEthPeer(org.hyperledger.besu.ethereum.eth.manager.RespondingEthPeer) Responder(org.hyperledger.besu.ethereum.eth.manager.RespondingEthPeer.Responder) Test(org.junit.Test)

Example 5 with RespondingEthPeer

use of org.hyperledger.besu.ethereum.eth.manager.RespondingEthPeer in project besu by hyperledger.

the class PivotBlockRetrieverTest method shouldRecoverFromUnresponsivePeer.

@Test
public void shouldRecoverFromUnresponsivePeer() {
    pivotBlockRetriever = createPivotBlockRetriever(2, 1, 1);
    EthProtocolManagerTestUtil.disableEthSchedulerAutoRun(ethProtocolManager);
    final Responder responder = RespondingEthPeer.blockchainResponder(blockchain, protocolContext.getWorldStateArchive(), transactionPool);
    final Responder emptyResponder = RespondingEthPeer.emptyResponder();
    final RespondingEthPeer peerA = EthProtocolManagerTestUtil.createPeer(ethProtocolManager, Difficulty.of(1000), 1000);
    final RespondingEthPeer peerB = EthProtocolManagerTestUtil.createPeer(ethProtocolManager, Difficulty.of(1000), 1000);
    final RespondingEthPeer peerC = EthProtocolManagerTestUtil.createPeer(ethProtocolManager, Difficulty.of(500), 500);
    final CompletableFuture<FastSyncState> future = pivotBlockRetriever.downloadPivotBlockHeader();
    peerA.respond(responder);
    peerB.respondTimes(emptyResponder, 2);
    // PeerA should have responded, while peerB is being retried, peerC shouldn't have been queried
    // yet
    assertThat(future).isNotCompleted();
    assertThat(peerC.hasOutstandingRequests()).isFalse();
    // After exhausting retries for peerB, we should try peerC
    peerB.respondTimes(emptyResponder, 2);
    peerC.respond(responder);
    assertThat(future).isCompletedWithValue(new FastSyncState(blockchain.getBlockHeader(PIVOT_BLOCK_NUMBER).get()));
}
Also used : RespondingEthPeer(org.hyperledger.besu.ethereum.eth.manager.RespondingEthPeer) Responder(org.hyperledger.besu.ethereum.eth.manager.RespondingEthPeer.Responder) Test(org.junit.Test)

Aggregations

RespondingEthPeer (org.hyperledger.besu.ethereum.eth.manager.RespondingEthPeer)106 Test (org.junit.Test)99 Responder (org.hyperledger.besu.ethereum.eth.manager.RespondingEthPeer.Responder)30 Block (org.hyperledger.besu.ethereum.core.Block)29 BlockHeader (org.hyperledger.besu.ethereum.core.BlockHeader)25 EthProtocolManager (org.hyperledger.besu.ethereum.eth.manager.EthProtocolManager)20 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)19 SynchronizerConfiguration (org.hyperledger.besu.ethereum.eth.sync.SynchronizerConfiguration)19 ChainDownloader (org.hyperledger.besu.ethereum.eth.sync.ChainDownloader)17 Blockchain (org.hyperledger.besu.ethereum.chain.Blockchain)16 BlockDataGenerator (org.hyperledger.besu.ethereum.core.BlockDataGenerator)15 NoOpMetricsSystem (org.hyperledger.besu.metrics.noop.NoOpMetricsSystem)13 ArrayList (java.util.ArrayList)12 Optional (java.util.Optional)11 Difficulty (org.hyperledger.besu.ethereum.core.Difficulty)11 NewBlockMessage (org.hyperledger.besu.ethereum.eth.messages.NewBlockMessage)11 AtomicReference (java.util.concurrent.atomic.AtomicReference)9 Bytes (org.apache.tuweni.bytes.Bytes)9 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)9 Hash (org.hyperledger.besu.datatypes.Hash)9