use of co.rsk.remasc.RemascTransaction in project rskj by rsksmart.
the class Block method parseTxs.
private List<Transaction> parseTxs(RLPList txTransactions) {
List<Transaction> parsedTxs = new ArrayList<>();
for (int i = 0; i < txTransactions.size(); i++) {
RLPElement transactionRaw = txTransactions.get(i);
Transaction tx = new ImmutableTransaction(transactionRaw.getRLPData());
if (isRemascTransaction(tx, i, txTransactions.size())) {
// It is the remasc transaction
tx = new RemascTransaction(transactionRaw.getRLPData());
}
parsedTxs.add(tx);
}
return Collections.unmodifiableList(parsedTxs);
}
use of co.rsk.remasc.RemascTransaction in project rskj by rsksmart.
the class BlockToMineBuilder method getTransactions.
private List<Transaction> getTransactions(List<Transaction> txsToRemove, BlockHeader parentHeader, 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(parentHeader.getNumber() + 1);
txs.add(remascTx);
Map<RskAddress, BigInteger> accountNonces = new HashMap<>();
RepositorySnapshot originalRepo = repositoryLocator.snapshotAt(parentHeader);
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, RepositorySnapshot 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;
}
use of co.rsk.remasc.RemascTransaction in project rskj by rsksmart.
the class MinerServerTest method buildBlockToMineCheckThatLastTransactionIsForREMASC.
@Test
public void buildBlockToMineCheckThatLastTransactionIsForREMASC() {
Transaction tx1 = Tx.create(config, 0, 21000, 100, 0, 0, 0);
byte[] s1 = new byte[32];
s1[0] = 0;
when(tx1.getHash()).thenReturn(new Keccak256(s1));
when(tx1.getEncoded()).thenReturn(new byte[32]);
Repository repository = repositoryLocator.startTrackingAt(blockStore.getBestBlock().getHeader());
Repository track = mock(Repository.class);
BlockTxSignatureCache blockTxSignatureCache = mock(BlockTxSignatureCache.class);
Mockito.doReturn(repository.getRoot()).when(track).getRoot();
Mockito.doReturn(repository.getTrie()).when(track).getTrie();
when(track.getNonce(tx1.getSender())).thenReturn(BigInteger.ZERO);
when(track.getNonce(tx1.getSender(blockTxSignatureCache))).thenReturn(BigInteger.ZERO);
when(track.getNonce(RemascTransaction.REMASC_ADDRESS)).thenReturn(BigInteger.ZERO);
when(track.getBalance(tx1.getSender())).thenReturn(Coin.valueOf(4200000L));
when(track.getBalance(RemascTransaction.REMASC_ADDRESS)).thenReturn(Coin.valueOf(4200000L));
Mockito.doReturn(track).when(repositoryLocator).startTrackingAt(any());
Mockito.doReturn(track).when(track).startTracking();
List<Transaction> txs = new ArrayList<>(Collections.singletonList(tx1));
TransactionPool localTransactionPool = mock(TransactionPool.class);
when(localTransactionPool.getPendingTransactions()).thenReturn(txs);
BlockUnclesValidationRule unclesValidationRule = mock(BlockUnclesValidationRule.class);
when(unclesValidationRule.isValid(any())).thenReturn(true);
MinerClock clock = new MinerClock(true, Clock.systemUTC());
MinerServerImpl minerServer = makeMinerServer(mock(EthereumImpl.class), unclesValidationRule, clock, localTransactionPool);
minerServer.buildBlockToMine(false);
Block blockAtHeightOne = minerServer.getBlocksWaitingForPoW().entrySet().iterator().next().getValue();
List<Transaction> blockTransactions = blockAtHeightOne.getTransactionsList();
assertNotNull(blockTransactions);
assertEquals(2, blockTransactions.size());
Transaction remascTransaction = blockTransactions.get(1);
assertThat(remascTransaction, instanceOf(RemascTransaction.class));
}
use of co.rsk.remasc.RemascTransaction in project rskj by rsksmart.
the class BlockValidatorTest method remascTxNotInLastPosition.
@Test
public void remascTxNotInLastPosition() {
BlockGenerator blockGenerator = new BlockGenerator();
Block genesis = blockGenerator.getGenesisBlock();
List<Transaction> txs = new ArrayList<>();
byte chainId = config.getNetworkConstants().getChainId();
Transaction tx = Transaction.builder().nonce(BigInteger.ZERO).gasPrice(BigInteger.valueOf(12L)).gasLimit(BigInteger.TEN).destination(Hex.decode("0000000000000000000000000000000000000006")).chainId(chainId).value(BigInteger.ZERO).build();
tx.sign(new byte[] {});
txs.add(new RemascTransaction(BigInteger.ONE.longValue()));
txs.add(tx);
Block block = new BlockBuilder(null, null, null).parent(genesis).transactions(txs).build();
BlockValidatorImpl validator = new BlockValidatorBuilder().addRemascValidationRule().build();
Assert.assertFalse(validator.isValid(block));
}
Aggregations