use of co.rsk.remasc.RemascTransaction in project rskj by rsksmart.
the class BlockTest method testParseRemascTransaction.
@Test
public void testParseRemascTransaction() {
List<Transaction> txs = new ArrayList<>();
Transaction txNotToRemasc = new Transaction(BigInteger.ZERO.toByteArray(), BigInteger.ONE.toByteArray(), BigInteger.valueOf(21000).toByteArray(), new ECKey().getAddress(), BigInteger.valueOf(1000).toByteArray(), null);
txNotToRemasc.sign(new ECKey().getPrivKeyBytes());
txs.add(txNotToRemasc);
Transaction txToRemascThatIsNotTheLatestTx = new Transaction(BigInteger.ZERO.toByteArray(), BigInteger.ONE.toByteArray(), BigInteger.valueOf(21000).toByteArray(), PrecompiledContracts.REMASC_ADDR.getBytes(), BigInteger.valueOf(1000).toByteArray(), null);
txToRemascThatIsNotTheLatestTx.sign(new ECKey().getPrivKeyBytes());
txs.add(txToRemascThatIsNotTheLatestTx);
Transaction remascTx = new RemascTransaction(1);
txs.add(remascTx);
Block block = new Block(// parent hash
PegTestUtils.createHash3().getBytes(), // uncle hash
EMPTY_LIST_HASH, // coinbase
TestUtils.randomAddress().getBytes(), // logs bloom
new Bloom().getData(), // difficulty
BigInteger.ONE.toByteArray(), 1, // gasLimit
BigInteger.valueOf(4000000).toByteArray(), // gasUsed
3000000, // timestamp
100, // extraData
new byte[0], // mixHash
new byte[0], // provisory nonce
new byte[] { 0 }, // receipts root
HashUtil.EMPTY_TRIE_HASH, // transaction root
BlockChainImpl.calcTxTrie(txs), // EMPTY_TRIE_HASH, // state root
HashUtil.EMPTY_TRIE_HASH, // transaction list
txs, // uncle list
null, BigInteger.TEN.toByteArray(), Coin.ZERO);
Block parsedBlock = new Block(block.getEncoded());
Assert.assertEquals(ImmutableTransaction.class, parsedBlock.getTransactionsList().get(0).getClass());
Assert.assertEquals(ImmutableTransaction.class, parsedBlock.getTransactionsList().get(1).getClass());
Assert.assertEquals(RemascTransaction.class, parsedBlock.getTransactionsList().get(2).getClass());
}
use of co.rsk.remasc.RemascTransaction in project rskj by rsksmart.
the class BlockValidatorTest method remascTx.
@Test
public void remascTx() {
BlockGenerator blockGenerator = new BlockGenerator();
Block genesis = blockGenerator.getGenesisBlock();
List<Transaction> txs = new ArrayList<>();
Transaction tx = Transaction.create(config, "0000000000000000000000000000000000000006", BigInteger.ZERO, BigInteger.ZERO, BigInteger.valueOf(12L), BigInteger.TEN);
tx.sign(new byte[] {});
txs.add(tx);
txs.add(new RemascTransaction(BigInteger.ONE.longValue()));
Block block = new BlockBuilder().parent(genesis).transactions(txs).build();
BlockValidatorImpl validator = new BlockValidatorBuilder().addRemascValidationRule().build();
Assert.assertTrue(validator.isValid(block));
}
use of co.rsk.remasc.RemascTransaction in project rskj by rsksmart.
the class RemascValidationRuleTest method remascTxIsNotTheLastOne.
@Test
public void remascTxIsNotTheLastOne() {
Block b = Mockito.mock(Block.class);
List<Transaction> tx = new ArrayList<>();
tx.add(new RemascTransaction(1L));
tx.add(Transaction.create(config, "0000000000000000000000000000000000000001", BigInteger.ZERO, BigInteger.ZERO, BigInteger.ONE, BigInteger.TEN));
Mockito.when(b.getTransactionsList()).thenReturn(tx);
RemascValidationRule rule = new RemascValidationRule();
Assert.assertFalse(rule.isValid(b));
}
use of co.rsk.remasc.RemascTransaction in project rskj by rsksmart.
the class BlockToMineBuilder method getTransactions.
private List<Transaction> getTransactions(List<Transaction> txsToRemove, Block parent, Coin minGasPrice) {
logger.debug("getting transactions from pending state");
List<Transaction> txs = minerUtils.getAllTransactions(transactionPool);
logger.debug("{} transaction(s) collected from pending state", txs.size());
Transaction remascTx = new RemascTransaction(parent.getNumber() + 1);
txs.add(remascTx);
Map<RskAddress, BigInteger> accountNonces = new HashMap<>();
Repository originalRepo = repository.getSnapshotTo(parent.getStateRoot());
return minerUtils.filterTransactions(txsToRemove, txs, accountNonces, originalRepo, minGasPrice);
}
use of co.rsk.remasc.RemascTransaction 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, Repository 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;
}
Aggregations