Search in sources :

Example 81 with Transaction

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

the class TxValidator method filterTxs.

/**
 * Where the magic occurs, will filter out invalid txs
 */
List<Transaction> filterTxs(List<Transaction> txs) {
    List<Transaction> acceptedTxs = new LinkedList<>();
    for (Transaction tx : txs) {
        String hash = tx.getHash().toJsonString();
        AccountState state = repository.getAccountState(tx.getSender());
        if (state == null) {
            state = new AccountState();
        }
        BigInteger blockGasLimit = BigIntegers.fromUnsignedByteArray(blockchain.getBestBlock().getGasLimit());
        Coin minimumGasPrice = blockchain.getBestBlock().getMinimumGasPrice();
        long bestBlockNumber = blockchain.getBestBlock().getNumber();
        long basicTxCost = tx.transactionCost(config, blockchain.getBestBlock());
        boolean valid = true;
        for (TxValidatorStep step : validatorSteps) {
            if (!step.validate(tx, state, blockGasLimit, minimumGasPrice, bestBlockNumber, basicTxCost == 0)) {
                logger.info("Tx validation failed: validator {} tx={}", step.getClass().getName(), tx.getHash());
                valid = false;
                break;
            }
        }
        if (!valid) {
            continue;
        }
        acceptedTxs.add(tx);
    }
    return acceptedTxs;
}
Also used : Coin(co.rsk.core.Coin) Transaction(org.ethereum.core.Transaction) BigInteger(java.math.BigInteger) AccountState(org.ethereum.core.AccountState) LinkedList(java.util.LinkedList)

Example 82 with Transaction

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

the class TxsPerAccount method removeNonce.

void removeNonce(BigInteger nonce) {
    List<Transaction> newlist = new LinkedList<>();
    for (Transaction tx : this.txs) {
        if (new BigInteger(1, tx.getNonce()).compareTo(nonce) == 0) {
            continue;
        }
        newlist.add(tx);
    }
    this.txs = newlist;
    if (newlist.isEmpty()) {
        this.nextNonce = null;
    }
}
Also used : Transaction(org.ethereum.core.Transaction) BigInteger(java.math.BigInteger) LinkedList(java.util.LinkedList)

Example 83 with Transaction

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

the class TxFilterAccumCostFilter method filter.

@Override
public List<Transaction> filter(AccountState state, TxsPerAccount tpa, Block block) {
    Coin accumTxCost = Coin.ZERO;
    tpa.getTransactions().sort((t1, t2) -> {
        BigInteger n1 = new BigInteger(1, t1.getNonce());
        BigInteger n2 = new BigInteger(1, t2.getNonce());
        return n1.compareTo(n2);
    });
    List<Transaction> newTxs = new LinkedList<>();
    for (Transaction t : tpa.getTransactions()) {
        Coin gasCost = Coin.ZERO;
        if (block == null || t.transactionCost(config, block) > 0) {
            BigInteger gasLimit = new BigInteger(1, t.getGasLimit());
            gasCost = t.getGasPrice().multiply(gasLimit);
        }
        if (accumTxCost.add(gasCost).compareTo(state.getBalance()) > 0) {
            break;
        }
        accumTxCost = accumTxCost.add(gasCost);
        accumTxCost = accumTxCost.add(t.getValue());
        newTxs.add(t);
    }
    return newTxs;
}
Also used : Coin(co.rsk.core.Coin) Transaction(org.ethereum.core.Transaction) BigInteger(java.math.BigInteger) LinkedList(java.util.LinkedList)

Example 84 with Transaction

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

the class TransactionsMessage method getEncodedMessage.

@Override
public byte[] getEncodedMessage() {
    List<byte[]> encodedElements = new ArrayList<>();
    for (Transaction tx : transactions) {
        encodedElements.add(tx.getEncoded());
    }
    byte[][] encodedElementArray = encodedElements.toArray(new byte[encodedElements.size()][]);
    return RLP.encodeList(encodedElementArray);
}
Also used : Transaction(org.ethereum.core.Transaction) ArrayList(java.util.ArrayList)

Example 85 with Transaction

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

the class MinerUtilsTest method validTransactionRepositoryNonceTest.

@Test
public void validTransactionRepositoryNonceTest() {
    Transaction tx = Tx.create(config, 0, 50000, 5, 0, 0, 0);
    // Mockito.when(tx.checkGasPrice(Mockito.any(BigInteger.class))).thenReturn(true);
    List<Transaction> txs = new LinkedList<>();
    txs.add(tx);
    Map<RskAddress, BigInteger> accountNounces = new HashMap();
    Repository repository = Mockito.mock(Repository.class);
    Mockito.when(repository.getNonce(tx.getSender())).thenReturn(BigInteger.valueOf(0));
    Coin minGasPrice = Coin.valueOf(1L);
    List<Transaction> res = new MinerUtils().filterTransactions(new LinkedList<>(), txs, accountNounces, repository, minGasPrice);
    Assert.assertEquals(1, res.size());
}
Also used : Coin(co.rsk.core.Coin) Repository(org.ethereum.core.Repository) Transaction(org.ethereum.core.Transaction) RskAddress(co.rsk.core.RskAddress) BigInteger(java.math.BigInteger) Test(org.junit.Test)

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