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;
}
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;
}
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);
}
}
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());
}
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));
}
Aggregations