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;
}
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;
}
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;
}
}
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();
}
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);
}
Aggregations