Search in sources :

Example 76 with Transaction

use of org.ethereum.core.Transaction in project rskj by rsksmart.

the class BlockTxsValidationRule method isValid.

@Override
public boolean isValid(Block block, Block parent) {
    if (block == null || parent == null) {
        logger.warn("BlockTxsValidationRule - block or parent are null");
        return false;
    }
    List<Transaction> txs = block.getTransactionsList();
    if (txs.isEmpty()) {
        return true;
    }
    Repository parentRepo = repository.getSnapshotTo(parent.getStateRoot());
    Map<RskAddress, BigInteger> curNonce = new HashMap<>();
    for (Transaction tx : txs) {
        try {
            tx.verify();
        } catch (RuntimeException e) {
            logger.warn("Unable to verify transaction", e);
            return false;
        }
        RskAddress sender = tx.getSender();
        BigInteger expectedNonce = curNonce.get(sender);
        if (expectedNonce == null) {
            expectedNonce = parentRepo.getNonce(sender);
        }
        curNonce.put(sender, expectedNonce.add(ONE));
        BigInteger txNonce = new BigInteger(1, tx.getNonce());
        if (!expectedNonce.equals(txNonce)) {
            logger.warn("Invalid transaction: Tx nonce {} != expected nonce {} (parent nonce: {}): {}", txNonce, expectedNonce, parentRepo.getNonce(sender), tx);
            panicProcessor.panic("invalidtransaction", String.format("Invalid transaction: Tx nonce %s != expected nonce %s (parent nonce: %s): %s", txNonce, expectedNonce, parentRepo.getNonce(sender), tx.getHash()));
            return false;
        }
    }
    return true;
}
Also used : Repository(org.ethereum.core.Repository) Transaction(org.ethereum.core.Transaction) HashMap(java.util.HashMap) RskAddress(co.rsk.core.RskAddress) BigInteger(java.math.BigInteger)

Example 77 with Transaction

use of org.ethereum.core.Transaction in project rskj by rsksmart.

the class TxBuilder method createNewTransaction.

public Transaction createNewTransaction(BigInteger gasPrice, BigInteger gasLimit, BigInteger txNonce) {
    String toAddress = Hex.toHexString(new ECKey(Utils.getRandom()).getAddress());
    Transaction tx = Transaction.create(config, toAddress, BigInteger.valueOf(1000), txNonce, gasPrice, gasLimit);
    tx.sign(privateKeyBytes);
    return tx;
}
Also used : Transaction(org.ethereum.core.Transaction) ECKey(org.ethereum.crypto.ECKey)

Example 78 with Transaction

use of org.ethereum.core.Transaction in project rskj by rsksmart.

the class TxBuilder method createNewTx.

public void createNewTx(BigInteger txNonce) throws InterruptedException {
    Transaction tx = this.createNewTransaction(BigInteger.valueOf(1), BigInteger.valueOf(21000), txNonce);
    // Adds created transaction to the local node's memory pool
    ethereum.submitTransaction(tx);
    logger.info("Added pending tx={}", tx.getHash());
    try {
        SecureRandom random = new SecureRandom();
        Thread.sleep(random.nextInt(51000));
    } catch (InterruptedException e) {
        throw e;
    }
}
Also used : Transaction(org.ethereum.core.Transaction) SecureRandom(java.security.SecureRandom)

Example 79 with Transaction

use of org.ethereum.core.Transaction in project rskj by rsksmart.

the class Metrics method prettyTxs.

@Nonnull
private static String prettyTxs(@Nonnull final List<Transaction> txs) {
    StringBuilder res = new StringBuilder();
    res.append('[');
    for (Transaction tx : txs) {
        if (res.length() > 1) {
            res.append("][");
        }
        String nonce = BIUtil.toBI(tx.getNonce()).toString();
        String pretty = String.format("H:%s - N:%s", HashUtil.shortHash(tx.getHash().getBytes()), nonce);
        res.append(pretty);
    }
    res.append(']');
    return res.toString();
}
Also used : Transaction(org.ethereum.core.Transaction) Nonnull(javax.annotation.Nonnull)

Example 80 with Transaction

use of org.ethereum.core.Transaction in project rskj by rsksmart.

the class NodeMessageHandler method processTransactionsMessage.

private void processTransactionsMessage(@Nonnull final MessageChannel sender, @Nonnull final TransactionsMessage message) {
    long start = System.nanoTime();
    loggerMessageProcess.debug("Tx message about to be process: {}", message.getMessageContentInfo());
    List<Transaction> messageTxs = message.getTransactions();
    Metrics.processTxsMessage("start", messageTxs, sender.getPeerNodeID());
    List<Transaction> txs = new LinkedList();
    for (Transaction tx : messageTxs) {
        if (!tx.acceptTransactionSignature(config.getBlockchainConfig().getCommonConstants().getChainId())) {
            recordEvent(sender, EventType.INVALID_TRANSACTION);
        } else {
            txs.add(tx);
            recordEvent(sender, EventType.VALID_TRANSACTION);
        }
    }
    List<Transaction> acceptedTxs = txHandler.retrieveValidTxs(txs);
    Metrics.processTxsMessage("txsValidated", acceptedTxs, sender.getPeerNodeID());
    // TODO(mmarquez): Add all this logic to the TxHandler
    acceptedTxs = transactionPool.addTransactions(acceptedTxs);
    Metrics.processTxsMessage("validTxsAddedToTransactionPool", acceptedTxs, sender.getPeerNodeID());
    /* Relay all transactions to peers that don't have them */
    relayTransactions(sender, acceptedTxs);
    Metrics.processTxsMessage("validTxsRelayed", acceptedTxs, sender.getPeerNodeID());
    Metrics.processTxsMessage("finish", acceptedTxs, sender.getPeerNodeID());
    loggerMessageProcess.debug("Tx message process finished after [{}] nano.", System.nanoTime() - start);
}
Also used : Transaction(org.ethereum.core.Transaction)

Aggregations

Transaction (org.ethereum.core.Transaction)131 Test (org.junit.Test)82 BigInteger (java.math.BigInteger)33 Coin (co.rsk.core.Coin)23 Account (org.ethereum.core.Account)20 ArrayList (java.util.ArrayList)17 Block (org.ethereum.core.Block)17 RskAddress (co.rsk.core.RskAddress)15 JsonNode (com.fasterxml.jackson.databind.JsonNode)15 Repository (org.ethereum.core.Repository)12 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)11 AccountState (org.ethereum.core.AccountState)10 RemascTransaction (co.rsk.remasc.RemascTransaction)9 TransactionBuilder (co.rsk.test.builders.TransactionBuilder)9 ImmutableTransaction (org.ethereum.core.ImmutableTransaction)8 ECKey (org.ethereum.crypto.ECKey)8 Keccak256 (co.rsk.crypto.Keccak256)7 AccountBuilder (co.rsk.test.builders.AccountBuilder)7 DslParser (co.rsk.test.dsl.DslParser)6 WorldDslProcessor (co.rsk.test.dsl.WorldDslProcessor)6