use of org.ethereum.core.Transaction in project rskj by rsksmart.
the class CodeReplaceTest method createTx.
protected Transaction createTx(BlockChainImpl blockchain, ECKey sender, byte[] receiveAddress, byte[] data, long value) throws InterruptedException {
BigInteger nonce = blockchain.getRepository().getNonce(new RskAddress(sender.getAddress()));
Transaction tx = new Transaction(ByteUtil.bigIntegerToBytes(nonce), ByteUtil.longToBytesNoLeadZeroes(1), ByteUtil.longToBytesNoLeadZeroes(3_000_000), receiveAddress, ByteUtil.longToBytesNoLeadZeroes(value), data, config.getBlockchainConfig().getCommonConstants().getChainId());
tx.sign(sender.getPrivKeyBytes());
return tx;
}
use of org.ethereum.core.Transaction in project rskj by rsksmart.
the class NodeMessageHandler method relayTransactions.
private void relayTransactions(@Nonnull MessageChannel sender, List<Transaction> acceptedTxs) {
for (Transaction tx : acceptedTxs) {
Keccak256 txHash = tx.getHash();
transactionNodeInformation.addTransactionToNode(txHash, sender.getPeerNodeID());
final Set<NodeID> nodesToSkip = new HashSet<>(transactionNodeInformation.getNodesByTransaction(txHash));
final Set<NodeID> newNodes = channelManager.broadcastTransaction(tx, nodesToSkip);
newNodes.forEach(nodeID -> transactionNodeInformation.addTransactionToNode(txHash, nodeID));
}
}
use of org.ethereum.core.Transaction in project rskj by rsksmart.
the class ChannelManagerImpl method broadcastTransaction.
/**
* broadcastTransaction Propagates a transaction message across active peers with exclusion of
* the peers with an id belonging to the skip set.
*
* @param transaction new Transaction to be sent
* @param skip the set of peers to avoid sending the message.
* @return a set containing the ids of the peers that received the transaction.
*/
@Nonnull
public Set<NodeID> broadcastTransaction(@Nonnull final Transaction transaction, @Nullable final Set<NodeID> skip) {
Metrics.broadcastTransaction(transaction);
List<Transaction> transactions = new ArrayList<>();
transactions.add(transaction);
final Set<NodeID> res = new HashSet<>();
final EthMessage newTransactions = new RskMessage(config, new TransactionsMessage(transactions));
synchronized (activePeers) {
final Vector<Channel> peers = activePeers.values().stream().filter(p -> skip == null || !skip.contains(p.getNodeId())).collect(Collectors.toCollection(Vector::new));
for (Channel peer : peers) {
res.add(peer.getNodeId());
peer.sendMessage(newTransactions);
}
}
return res;
}
use of org.ethereum.core.Transaction in project account-identity by cryptofiat.
the class EthereumService method sendTransaction.
private String sendTransaction(ECKey signer, byte[] callData, String _contract, int nonceIncrement) throws IOException {
// TODO: maybe a queue of pending transactions, otherwise one goes through at a time
long transactionCount = getTransactionCount(hex(signer.getAddress())) + nonceIncrement;
long gasPriceWei = wsService.getGasPriceWei();
log.info("Current gas price: " + String.valueOf(gasPriceWei));
byte[] nonce = ByteUtil.longToBytesNoLeadZeroes(transactionCount);
byte[] gasPrice = ByteUtil.longToBytesNoLeadZeroes(gasPriceWei);
byte[] gasLimit = ByteUtil.longToBytesNoLeadZeroes(GAS_LIMIT);
byte[] toAddress = Hex.decode(without0x(_contract));
Transaction transaction = new Transaction(nonce, gasPrice, gasLimit, toAddress, null, callData);
// noinspection ConstantConditions
transaction.sign(signer.getPrivKeyBytes());
return send(json("eth_sendRawTransaction", hex(transaction.getEncoded())));
}
use of org.ethereum.core.Transaction in project rskj by rsksmart.
the class MinerUtils method filterTransactions.
public List<org.ethereum.core.Transaction> filterTransactions(List<Transaction> txsToRemove, List<Transaction> txs, Map<RskAddress, BigInteger> accountNonces, RepositorySnapshot originalRepo, Coin minGasPrice) {
List<org.ethereum.core.Transaction> txsResult = new ArrayList<>();
for (org.ethereum.core.Transaction tx : txs) {
try {
Keccak256 hash = tx.getHash();
Coin txValue = tx.getValue();
BigInteger txNonce = new BigInteger(1, tx.getNonce());
RskAddress txSender = tx.getSender();
logger.debug("Examining tx={} sender: {} value: {} nonce: {}", hash, txSender, txValue, txNonce);
BigInteger expectedNonce;
if (accountNonces.containsKey(txSender)) {
expectedNonce = accountNonces.get(txSender).add(BigInteger.ONE);
} else {
expectedNonce = originalRepo.getNonce(txSender);
}
if (!(tx instanceof RemascTransaction) && tx.getGasPrice().compareTo(minGasPrice) < 0) {
logger.warn("Rejected tx={} because of low gas account {}, removing tx from pending state.", hash, txSender);
txsToRemove.add(tx);
continue;
}
if (!expectedNonce.equals(txNonce)) {
logger.warn("Invalid nonce, expected {}, found {}, tx={}", expectedNonce, txNonce, hash);
continue;
}
accountNonces.put(txSender, txNonce);
logger.debug("Accepted tx={} sender: {} value: {} nonce: {}", hash, txSender, txValue, txNonce);
} catch (Exception e) {
// Txs that can't be selected by any reason should be removed from pending state
logger.warn(String.format("Error when processing tx=%s", tx.getHash()), e);
if (txsToRemove != null) {
txsToRemove.add(tx);
} else {
logger.error("Can't remove invalid txs from pending state.");
}
continue;
}
txsResult.add(tx);
}
logger.debug("Ending getTransactions {}", txsResult.size());
return txsResult;
}
Aggregations