Search in sources :

Example 26 with AccountState

use of org.ethereum.core.AccountState 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 27 with AccountState

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

the class GenesisLoader method generatePreMine.

private static Map<RskAddress, InitialAddressState> generatePreMine(RskSystemProperties config, BigInteger initialNonce, Map<String, AllocatedAccount> alloc) {
    Map<RskAddress, InitialAddressState> premine = new HashMap<>();
    ContractDetailsMapper detailsMapper = new ContractDetailsMapper(config);
    for (Map.Entry<String, AllocatedAccount> accountEntry : alloc.entrySet()) {
        if (!StringUtils.equals("00", accountEntry.getKey())) {
            Coin balance = new Coin(new BigInteger(accountEntry.getValue().getBalance()));
            BigInteger nonce;
            if (accountEntry.getValue().getNonce() != null) {
                nonce = new BigInteger(accountEntry.getValue().getNonce());
            } else {
                nonce = initialNonce;
            }
            AccountState acctState = new AccountState(nonce, balance);
            ContractDetails contractDetails = null;
            Contract contract = accountEntry.getValue().getContract();
            if (contract != null) {
                contractDetails = detailsMapper.mapFromContract(contract);
                if (contractDetails.getCode() != null) {
                    acctState.setCodeHash(Keccak256Helper.keccak256(contractDetails.getCode()));
                }
                acctState.setStateRoot(contractDetails.getStorageHash());
            }
            premine.put(new RskAddress(accountEntry.getKey()), new InitialAddressState(acctState, contractDetails));
        }
    }
    return premine;
}
Also used : HashMap(java.util.HashMap) AccountState(org.ethereum.core.AccountState) ContractDetails(org.ethereum.db.ContractDetails) Coin(co.rsk.core.Coin) RskAddress(co.rsk.core.RskAddress) BigInteger(java.math.BigInteger) HashMap(java.util.HashMap) Map(java.util.Map)

Example 28 with AccountState

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

the class RepositoryTrack method loadAccount.

@Override
public void loadAccount(RskAddress addr, Map<RskAddress, AccountState> cacheAccounts, Map<RskAddress, ContractDetails> cacheDetails) {
    synchronized (repository) {
        AccountState accountState = this.cacheAccounts.get(addr);
        ContractDetails contractDetails = this.cacheDetails.get(addr);
        if (accountState == null) {
            repository.loadAccount(addr, this.cacheAccounts, this.cacheDetails);
            accountState = this.cacheAccounts.get(addr);
            contractDetails = this.cacheDetails.get(addr);
        }
        cacheAccounts.put(addr, accountState.clone());
        ContractDetails contractDetailsLvl2 = new ContractDetailsCacheImpl(contractDetails);
        cacheDetails.put(addr, contractDetailsLvl2);
    }
}
Also used : AccountState(org.ethereum.core.AccountState)

Example 29 with AccountState

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

the class TxFilterAccumCostFilterTest method twoTxsValidAccumGasPrice.

@Test
public void twoTxsValidAccumGasPrice() {
    Transaction tx1 = Mockito.mock(Transaction.class);
    Transaction tx2 = Mockito.mock(Transaction.class);
    AccountState as1 = Mockito.mock(AccountState.class);
    AccountState as2 = Mockito.mock(AccountState.class);
    AccountState as3 = Mockito.mock(AccountState.class);
    TxsPerAccount tpa1 = new TxsPerAccount();
    TxsPerAccount tpa2 = new TxsPerAccount();
    Mockito.when(tx1.getGasLimit()).thenReturn(BigInteger.valueOf(1).toByteArray());
    Mockito.when(tx2.getGasLimit()).thenReturn(BigInteger.valueOf(1).toByteArray());
    Mockito.when(tx1.getGasPrice()).thenReturn(Coin.valueOf(1));
    Mockito.when(tx2.getGasPrice()).thenReturn(Coin.valueOf(1));
    Mockito.when(tx1.getValue()).thenReturn(Coin.valueOf(1));
    Mockito.when(tx2.getValue()).thenReturn(Coin.valueOf(1));
    Mockito.when(tx1.getNonce()).thenReturn(BigInteger.valueOf(0).toByteArray());
    Mockito.when(tx2.getNonce()).thenReturn(BigInteger.valueOf(1).toByteArray());
    Mockito.when(as1.getBalance()).thenReturn(Coin.valueOf(1000));
    Mockito.when(as2.getBalance()).thenReturn(Coin.valueOf(4));
    Mockito.when(as3.getBalance()).thenReturn(Coin.valueOf(3));
    Mockito.when(as1.getNonce()).thenReturn(BigInteger.valueOf(0));
    Mockito.when(as2.getNonce()).thenReturn(BigInteger.valueOf(0));
    Mockito.when(as3.getNonce()).thenReturn(BigInteger.valueOf(0));
    TxFilterAccumCostFilter tfacf = new TxFilterAccumCostFilter(config);
    tpa1.setTransactions(new LinkedList<>());
    tpa1.getTransactions().add(tx1);
    tpa1.getTransactions().add(tx2);
    Assert.assertEquals(2, tfacf.filter(as1, tpa1, null).size());
    tpa1.setTransactions(new LinkedList<>());
    tpa1.getTransactions().add(tx1);
    tpa1.getTransactions().add(tx2);
    Assert.assertEquals(2, tfacf.filter(as2, tpa1, null).size());
    tpa1.setTransactions(new LinkedList<>());
    tpa1.getTransactions().add(tx1);
    tpa1.getTransactions().add(tx2);
    Assert.assertEquals(2, tfacf.filter(as3, tpa1, null).size());
    tpa2.setTransactions(new LinkedList<>());
    tpa2.getTransactions().add(tx1);
    tpa2.getTransactions().add(tx2);
    Assert.assertEquals(2, tfacf.filter(as1, tpa2, null).size());
    tpa2.setTransactions(new LinkedList<>());
    tpa2.getTransactions().add(tx1);
    tpa2.getTransactions().add(tx2);
    Assert.assertEquals(2, tfacf.filter(as2, tpa2, null).size());
    tpa2.setTransactions(new LinkedList<>());
    tpa2.getTransactions().add(tx1);
    tpa2.getTransactions().add(tx2);
    Assert.assertEquals(2, tfacf.filter(as3, tpa2, null).size());
}
Also used : Transaction(org.ethereum.core.Transaction) TxsPerAccount(co.rsk.net.handler.TxsPerAccount) AccountState(org.ethereum.core.AccountState) Test(org.junit.Test)

Example 30 with AccountState

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

the class TxValidatorAccountBalanceValidatorTest method balanceIsNotValidatedIfFreeTx.

@Test
public void balanceIsNotValidatedIfFreeTx() {
    Transaction tx = new Transaction(BigInteger.ZERO.toByteArray(), BigInteger.ONE.toByteArray(), BigInteger.valueOf(21071).toByteArray(), new ECKey().getAddress(), BigInteger.ZERO.toByteArray(), Hex.decode("0001"), new RskSystemProperties().getBlockchainConfig().getCommonConstants().getChainId());
    tx.sign(new ECKey().getPrivKeyBytes());
    TxValidatorAccountBalanceValidator tv = new TxValidatorAccountBalanceValidator();
    Assert.assertTrue(tv.validate(tx, new AccountState(), BigInteger.ONE, Coin.valueOf(1L), Long.MAX_VALUE, true));
}
Also used : Transaction(org.ethereum.core.Transaction) ECKey(org.ethereum.crypto.ECKey) AccountState(org.ethereum.core.AccountState) RskSystemProperties(co.rsk.config.RskSystemProperties) Test(org.junit.Test)

Aggregations

AccountState (org.ethereum.core.AccountState)46 Test (org.junit.Test)20 RskAddress (co.rsk.core.RskAddress)12 BigInteger (java.math.BigInteger)11 Coin (co.rsk.core.Coin)10 Transaction (org.ethereum.core.Transaction)10 Repository (org.ethereum.core.Repository)7 ContractDetails (org.ethereum.db.ContractDetails)7 Program (org.ethereum.vm.program.Program)5 ProgramInvokeMockImpl (org.ethereum.vm.program.invoke.ProgramInvokeMockImpl)5 Ignore (org.junit.Ignore)5 RskSystemProperties (co.rsk.config.RskSystemProperties)3 TrieImplHashTest (co.rsk.trie.TrieImplHashTest)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 ECKey (org.ethereum.crypto.ECKey)3 RepositoryImpl (co.rsk.db.RepositoryImpl)2 TxsPerAccount (co.rsk.net.handler.TxsPerAccount)2 TrieStoreImpl (co.rsk.trie.TrieStoreImpl)2 HashMapDB (org.ethereum.datasource.HashMapDB)2