Search in sources :

Example 1 with RemascTransaction

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);
}
Also used : RemascTransaction(co.rsk.remasc.RemascTransaction) RemascTransaction(co.rsk.remasc.RemascTransaction) RLPElement(org.ethereum.util.RLPElement) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Example 2 with RemascTransaction

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);
}
Also used : RepositorySnapshot(co.rsk.db.RepositorySnapshot) RemascTransaction(co.rsk.remasc.RemascTransaction) RemascTransaction(co.rsk.remasc.RemascTransaction) RskAddress(co.rsk.core.RskAddress) BigInteger(java.math.BigInteger)

Example 3 with RemascTransaction

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;
}
Also used : RemascTransaction(co.rsk.remasc.RemascTransaction) ArrayList(java.util.ArrayList) Keccak256(co.rsk.crypto.Keccak256) IOException(java.io.IOException) Coin(co.rsk.core.Coin) RemascTransaction(co.rsk.remasc.RemascTransaction) BtcTransaction(co.rsk.bitcoinj.core.BtcTransaction) Transaction(org.ethereum.core.Transaction) RskAddress(co.rsk.core.RskAddress) BigInteger(java.math.BigInteger) Transaction(org.ethereum.core.Transaction)

Example 4 with RemascTransaction

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));
}
Also used : RemascTransaction(co.rsk.remasc.RemascTransaction) ArrayList(java.util.ArrayList) EthereumImpl(org.ethereum.facade.EthereumImpl) Keccak256(co.rsk.crypto.Keccak256) BlockUnclesValidationRule(co.rsk.validators.BlockUnclesValidationRule) RemascTransaction(co.rsk.remasc.RemascTransaction) Test(org.junit.Test)

Example 5 with RemascTransaction

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));
}
Also used : RemascTransaction(co.rsk.remasc.RemascTransaction) RemascTransaction(co.rsk.remasc.RemascTransaction) BtcBlock(co.rsk.bitcoinj.core.BtcBlock) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) BlockBuilder(co.rsk.test.builders.BlockBuilder) Test(org.junit.Test)

Aggregations

RemascTransaction (co.rsk.remasc.RemascTransaction)18 Test (org.junit.Test)9 ArrayList (java.util.ArrayList)8 RskAddress (co.rsk.core.RskAddress)6 BigInteger (java.math.BigInteger)5 Transaction (org.ethereum.core.Transaction)5 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)3 Coin (co.rsk.core.Coin)3 Keccak256 (co.rsk.crypto.Keccak256)3 Block (org.ethereum.core.Block)3 BtcBlock (co.rsk.bitcoinj.core.BtcBlock)2 BtcTransaction (co.rsk.bitcoinj.core.BtcTransaction)2 RepositorySnapshot (co.rsk.db.RepositorySnapshot)2 BlockBuilder (co.rsk.test.builders.BlockBuilder)2 IOException (java.io.IOException)2 RLPElement (org.ethereum.util.RLPElement)2 BlockUnclesValidationRule (co.rsk.validators.BlockUnclesValidationRule)1 Random (java.util.Random)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 ActivationConfigsForTest (org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)1