Search in sources :

Example 11 with Transaction

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;
}
Also used : Transaction(org.ethereum.core.Transaction) BigInteger(java.math.BigInteger)

Example 12 with Transaction

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));
    }
}
Also used : Transaction(org.ethereum.core.Transaction) Keccak256(co.rsk.crypto.Keccak256)

Example 13 with Transaction

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;
}
Also used : NodeFilter(org.ethereum.config.NodeFilter) java.util(java.util) LoggerFactory(org.slf4j.LoggerFactory) LRUMap(org.apache.commons.collections4.map.LRUMap) Autowired(org.springframework.beans.factory.annotation.Autowired) CollectionUtils(org.apache.commons.collections4.CollectionUtils) Block(org.ethereum.core.Block) InetAddress(java.net.InetAddress) EthMessage(org.ethereum.net.eth.message.EthMessage) Status(co.rsk.net.Status) SyncPool(org.ethereum.sync.SyncPool) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Metrics(co.rsk.net.Metrics) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) BlockIdentifier(org.ethereum.core.BlockIdentifier) RskMessage(co.rsk.net.eth.RskMessage) Logger(org.slf4j.Logger) co.rsk.net.messages(co.rsk.net.messages) ReasonCode(org.ethereum.net.message.ReasonCode) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) TimeUnit(java.util.concurrent.TimeUnit) Component(org.springframework.stereotype.Component) NodeID(co.rsk.net.NodeID) VisibleForTesting(com.google.common.annotations.VisibleForTesting) RskSystemProperties(co.rsk.config.RskSystemProperties) Transaction(org.ethereum.core.Transaction) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Transaction(org.ethereum.core.Transaction) EthMessage(org.ethereum.net.eth.message.EthMessage) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) NodeID(co.rsk.net.NodeID) RskMessage(co.rsk.net.eth.RskMessage) Nonnull(javax.annotation.Nonnull)

Example 14 with Transaction

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())));
}
Also used : Transaction(org.ethereum.core.Transaction)

Example 15 with Transaction

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;
}
Also used : RemascTransaction(co.rsk.remasc.RemascTransaction) ArrayList(java.util.ArrayList) Keccak256(co.rsk.crypto.Keccak256) IOException(java.io.IOException) Coin(co.rsk.core.Coin) RemascTransaction(co.rsk.remasc.RemascTransaction) BtcTransaction(co.rsk.bitcoinj.core.BtcTransaction) Transaction(org.ethereum.core.Transaction) RskAddress(co.rsk.core.RskAddress) BigInteger(java.math.BigInteger) Transaction(org.ethereum.core.Transaction)

Aggregations

Transaction (org.ethereum.core.Transaction)257 Test (org.junit.Test)166 Block (org.ethereum.core.Block)73 RskAddress (co.rsk.core.RskAddress)69 ActivationConfigsForTest (org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)57 InternalTransaction (org.ethereum.vm.program.InternalTransaction)57 SimpleRskTransaction (co.rsk.peg.simples.SimpleRskTransaction)39 ActivationConfig (org.ethereum.config.blockchain.upgrades.ActivationConfig)39 BigInteger (java.math.BigInteger)38 ArrayList (java.util.ArrayList)38 Repository (org.ethereum.core.Repository)35 JsonNode (com.fasterxml.jackson.databind.JsonNode)32 ECKey (org.ethereum.crypto.ECKey)30 co.rsk.bitcoinj.core (co.rsk.bitcoinj.core)29 Keccak256 (co.rsk.crypto.Keccak256)28 Account (org.ethereum.core.Account)26 World (co.rsk.test.World)23 DslParser (co.rsk.test.dsl.DslParser)23 WorldDslProcessor (co.rsk.test.dsl.WorldDslProcessor)23 MutableRepository (org.ethereum.db.MutableRepository)22