Search in sources :

Example 1 with Transaction

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

the class PendingTransactionsSenderTest method testSendEth65PeersOnly.

@Test
public void testSendEth65PeersOnly() {
    PeerPendingTransactionTracker peerPendingTransactionTracker = mock(PeerPendingTransactionTracker.class);
    PendingTransactionsMessageSender pendingTransactionsMessageSender = mock(PendingTransactionsMessageSender.class);
    EthContext ethContext = mock(EthContext.class);
    EthScheduler ethScheduler = mock(EthScheduler.class);
    when(ethContext.getScheduler()).thenReturn(ethScheduler);
    PendingTransactionSender sender = new PendingTransactionSender(peerPendingTransactionTracker, pendingTransactionsMessageSender, ethContext);
    EthPeer peer1 = mock(EthPeer.class);
    EthPeer peer2 = mock(EthPeer.class);
    Transaction tx = mock(Transaction.class);
    Hash hash = Hash.wrap(Bytes32.random());
    when(tx.getHash()).thenReturn(hash);
    EthPeers ethPeers = mock(EthPeers.class);
    when(ethContext.getEthPeers()).thenReturn(ethPeers);
    when(ethPeers.streamAvailablePeers()).thenReturn(Arrays.asList(peer1, peer2).stream());
    when(peerPendingTransactionTracker.isPeerSupported(peer1, EthProtocol.ETH65)).thenReturn(true);
    when(peerPendingTransactionTracker.isPeerSupported(peer2, EthProtocol.ETH65)).thenReturn(false);
    sender.onTransactionsAdded(Collections.singleton(tx));
    verify(peerPendingTransactionTracker, times(1)).addToPeerSendQueue(peer1, hash);
    verify(peerPendingTransactionTracker, never()).addToPeerSendQueue(peer2, hash);
}
Also used : EthContext(org.hyperledger.besu.ethereum.eth.manager.EthContext) EthScheduler(org.hyperledger.besu.ethereum.eth.manager.EthScheduler) EthPeer(org.hyperledger.besu.ethereum.eth.manager.EthPeer) Transaction(org.hyperledger.besu.ethereum.core.Transaction) EthPeers(org.hyperledger.besu.ethereum.eth.manager.EthPeers) Hash(org.hyperledger.besu.datatypes.Hash) Test(org.junit.Test)

Example 2 with Transaction

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

the class TransactionPoolTest method shouldRejectRemoteTransactionsWhenNotInSync.

@Test
public void shouldRejectRemoteTransactionsWhenNotInSync() {
    SyncState syncState = mock(SyncState.class);
    when(syncState.isInSync(anyLong())).thenReturn(false);
    TransactionPool transactionPool = new TransactionPool(transactions, protocolSchedule, protocolContext, batchAddedListener, pendingBatchAddedListener, syncState, ethContext, peerTransactionTracker, peerPendingTransactionTracker, new MiningParameters.Builder().minTransactionGasPrice(Wei.ZERO).build(), metricsSystem, TransactionPoolConfiguration.DEFAULT);
    final TransactionTestFixture builder = new TransactionTestFixture();
    final Transaction transaction1 = builder.nonce(1).createTransaction(KEY_PAIR1);
    final Transaction transaction2 = builder.nonce(2).createTransaction(KEY_PAIR1);
    final Transaction transaction3 = builder.nonce(3).createTransaction(KEY_PAIR1);
    transactionPool.addRemoteTransactions(asList(transaction3, transaction1, transaction2));
    assertTransactionNotPending(transaction1);
    assertTransactionNotPending(transaction2);
    assertTransactionNotPending(transaction3);
    verifyNoInteractions(batchAddedListener);
}
Also used : MiningParameters(org.hyperledger.besu.ethereum.core.MiningParameters) TransactionTestFixture(org.hyperledger.besu.ethereum.core.TransactionTestFixture) Transaction(org.hyperledger.besu.ethereum.core.Transaction) SyncState(org.hyperledger.besu.ethereum.eth.sync.state.SyncState) Test(org.junit.Test)

Example 3 with Transaction

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

the class PeerPendingTransactionTrackerTest method setUp.

@Before
public void setUp() {
    tracker = new PeerPendingTransactionTracker(pendingTransactions);
    Transaction tx = mock(Transaction.class);
    when(pendingTransactions.getTransactionByHash(any())).thenReturn(Optional.of(tx));
}
Also used : Transaction(org.hyperledger.besu.ethereum.core.Transaction) Before(org.junit.Before)

Example 4 with Transaction

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

the class MainnetTransactionValidatorTest method shouldRejectTransactionWhenEffectiveGasPriceIsTooLow.

@Test
public void shouldRejectTransactionWhenEffectiveGasPriceIsTooLow() {
    final MainnetTransactionValidator validator = new MainnetTransactionValidator(gasCalculator, FeeMarket.london(0L), false, Optional.of(BigInteger.ONE), Set.of(TransactionType.values()), defaultGoQuorumCompatibilityMode);
    validator.setTransactionFilter(transactionFilter(true));
    final Transaction transaction = Transaction.builder().type(TransactionType.EIP1559).nonce(0).maxPriorityFeePerGas(Wei.ZERO).maxFeePerGas(Wei.of(4)).gasLimit(15).to(Address.ZERO).value(Wei.of(0)).payload(Bytes.EMPTY).chainId(BigInteger.ONE).signAndBuild(new SECP256K1().generateKeyPair());
    final ValidationResult<TransactionInvalidReason> validationResult = validator.validate(transaction, Optional.of(Wei.ONE), transactionValidationParams);
    assertThat(validationResult).isEqualTo(ValidationResult.invalid(INVALID_TRANSACTION_FORMAT));
    assertThat(validationResult.getErrorMessage()).isEqualTo("effective gas price is too low to execute");
}
Also used : Transaction(org.hyperledger.besu.ethereum.core.Transaction) SECP256K1(org.hyperledger.besu.crypto.SECP256K1) TransactionInvalidReason(org.hyperledger.besu.ethereum.transaction.TransactionInvalidReason) Test(org.junit.Test)

Example 5 with Transaction

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

the class BlockchainQueries method transactionByHash.

/**
 * Given a transaction hash, returns the associated transaction.
 *
 * @param transactionHash The hash of the target transaction.
 * @return The transaction associated with the given hash.
 */
public Optional<TransactionWithMetadata> transactionByHash(final Hash transactionHash) {
    final Optional<TransactionLocation> maybeLocation = blockchain.getTransactionLocation(transactionHash);
    if (maybeLocation.isEmpty()) {
        return Optional.empty();
    }
    final TransactionLocation loc = maybeLocation.get();
    final Hash blockHash = loc.getBlockHash();
    // getTransactionLocation should not return if the TX or block doesn't exist, so throwing
    // on a missing optional is appropriate.
    final BlockHeader header = blockchain.getBlockHeader(blockHash).orElseThrow();
    final Transaction transaction = blockchain.getTransactionByHash(transactionHash).orElseThrow();
    return Optional.of(new TransactionWithMetadata(transaction, header.getNumber(), header.getBaseFee(), blockHash, loc.getTransactionIndex()));
}
Also used : Transaction(org.hyperledger.besu.ethereum.core.Transaction) TransactionLocation(org.hyperledger.besu.ethereum.chain.TransactionLocation) Hash(org.hyperledger.besu.datatypes.Hash) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader)

Aggregations

Transaction (org.hyperledger.besu.ethereum.core.Transaction)249 Test (org.junit.Test)149 ArrayList (java.util.ArrayList)57 BlockHeader (org.hyperledger.besu.ethereum.core.BlockHeader)45 TransactionTestFixture (org.hyperledger.besu.ethereum.core.TransactionTestFixture)45 Optional (java.util.Optional)42 Hash (org.hyperledger.besu.datatypes.Hash)38 Wei (org.hyperledger.besu.datatypes.Wei)38 Block (org.hyperledger.besu.ethereum.core.Block)35 List (java.util.List)33 Address (org.hyperledger.besu.datatypes.Address)33 Account (org.hyperledger.besu.evm.account.Account)29 TransactionInvalidReason (org.hyperledger.besu.ethereum.transaction.TransactionInvalidReason)28 TransactionReceipt (org.hyperledger.besu.ethereum.core.TransactionReceipt)26 Bytes (org.apache.tuweni.bytes.Bytes)24 BlockBody (org.hyperledger.besu.ethereum.core.BlockBody)22 MiningParameters (org.hyperledger.besu.ethereum.core.MiningParameters)22 TransactionValidationParams (org.hyperledger.besu.ethereum.mainnet.TransactionValidationParams)22 KeyPair (org.hyperledger.besu.crypto.KeyPair)21 EthContext (org.hyperledger.besu.ethereum.eth.manager.EthContext)20