Search in sources :

Example 56 with Coin

use of co.rsk.core.Coin in project rskj by rsksmart.

the class RemascStorageProvider method getRewardBalance.

public Coin getRewardBalance() {
    if (rewardBalance != null) {
        return rewardBalance;
    }
    DataWord address = new DataWord(REWARD_BALANCE_KEY.getBytes(StandardCharsets.UTF_8));
    DataWord value = this.repository.getStorageValue(this.contractAddress, address);
    if (value == null) {
        return Coin.ZERO;
    }
    return new Coin(value.getData());
}
Also used : Coin(co.rsk.core.Coin) DataWord(org.ethereum.vm.DataWord)

Example 57 with Coin

use of co.rsk.core.Coin 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 58 with Coin

use of co.rsk.core.Coin 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 59 with Coin

use of co.rsk.core.Coin in project rskj by rsksmart.

the class TxValidatorAccountBalanceValidator method validate.

@Override
public boolean validate(Transaction tx, AccountState state, BigInteger gasLimit, Coin minimumGasPrice, long bestBlockNumber, boolean isFreeTx) {
    BigInteger txGasLimit = tx.getGasLimitAsInteger();
    Coin maximumPrice = tx.getGasPrice().multiply(txGasLimit);
    return isFreeTx || state.getBalance().compareTo(maximumPrice) >= 0;
}
Also used : Coin(co.rsk.core.Coin) BigInteger(java.math.BigInteger)

Example 60 with Coin

use of co.rsk.core.Coin 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

Coin (co.rsk.core.Coin)95 Test (org.junit.Test)46 RskAddress (co.rsk.core.RskAddress)37 BigInteger (java.math.BigInteger)32 Repository (org.ethereum.core.Repository)23 Transaction (org.ethereum.core.Transaction)23 Program (org.ethereum.vm.program.Program)12 AccountState (org.ethereum.core.AccountState)10 ArrayList (java.util.ArrayList)9 Ignore (org.junit.Ignore)9 ProgramInvokeMockImpl (org.ethereum.vm.program.invoke.ProgramInvokeMockImpl)8 Account (org.ethereum.core.Account)7 BlockExecutor (co.rsk.core.bc.BlockExecutor)6 BlockChainBuilder (co.rsk.test.builders.BlockChainBuilder)6 Keccak256 (co.rsk.crypto.Keccak256)5 Block (org.ethereum.core.Block)5 DataWord (org.ethereum.vm.DataWord)5 ProgramInvoke (org.ethereum.vm.program.invoke.ProgramInvoke)5 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)4 RskSystemProperties (co.rsk.config.RskSystemProperties)4