Search in sources :

Example 21 with Coin

use of co.rsk.core.Coin in project rskj by rsksmart.

the class TransactionPoolImplTest method updateTransactionPool.

@Test
public void updateTransactionPool() {
    BlockChainImpl blockchain = createBlockchain();
    Coin balance = Coin.valueOf(1000000);
    TransactionPoolImpl transactionPool = createSampleNewTransactionPoolWithAccounts(2, balance, blockchain);
    transactionPool.processBest(blockchain.getBestBlock());
    Transaction tx1 = createSampleTransaction(1, 2, 1000, 0);
    Transaction tx2 = createSampleTransaction(1, 2, 3000, 1);
    Account receiver = createAccount(2);
    transactionPool.addTransaction(tx1);
    transactionPool.addTransaction(tx2);
    transactionPool.updateState();
    Repository repository = transactionPool.getRepository();
    Assert.assertEquals(BigInteger.valueOf(1004000), repository.getBalance(receiver.getAddress()).asBigInteger());
}
Also used : Coin(co.rsk.core.Coin) Account(org.ethereum.core.Account) Repository(org.ethereum.core.Repository) Transaction(org.ethereum.core.Transaction) Test(org.junit.Test)

Example 22 with Coin

use of co.rsk.core.Coin in project rskj by rsksmart.

the class TransactionPoolImplTest method retractBlockAddsTransactionsAsPending.

@Test
public void retractBlockAddsTransactionsAsPending() {
    BlockChainImpl blockchain = createBlockchain();
    Coin balance = Coin.valueOf(1000000);
    TransactionPoolImpl transactionPool = createSampleNewTransactionPoolWithAccounts(3, balance, blockchain);
    transactionPool.processBest(blockchain.getBestBlock());
    Transaction tx1 = createSampleTransaction(1, 2, 1000, 0);
    Transaction tx2 = createSampleTransaction(1, 2, 3000, 1);
    Transaction tx3 = createSampleTransaction(2, 3, 1000, 0);
    Transaction tx4 = createSampleTransaction(2, 3, 3000, 1);
    transactionPool.addTransaction(tx1);
    transactionPool.addTransaction(tx2);
    List<Transaction> txs = new ArrayList<>();
    txs.add(tx3);
    txs.add(tx4);
    Block block = new BlockBuilder().parent(new BlockGenerator().getGenesisBlock()).transactions(txs).build();
    transactionPool.retractBlock(block);
    List<Transaction> alltxs = transactionPool.getPendingTransactions();
    Assert.assertNotNull(alltxs);
    Assert.assertFalse(alltxs.isEmpty());
    Assert.assertEquals(4, alltxs.size());
    Assert.assertTrue(alltxs.contains(tx1));
    Assert.assertTrue(alltxs.contains(tx2));
    Assert.assertTrue(alltxs.contains(tx3));
    Assert.assertTrue(alltxs.contains(tx4));
    List<Transaction> ptxs = transactionPool.getPendingTransactions();
    Assert.assertNotNull(ptxs);
    Assert.assertFalse(ptxs.isEmpty());
    Assert.assertEquals(4, ptxs.size());
    Assert.assertTrue(ptxs.contains(tx1));
    Assert.assertTrue(ptxs.contains(tx2));
    Assert.assertTrue(ptxs.contains(tx3));
    Assert.assertTrue(ptxs.contains(tx4));
}
Also used : Coin(co.rsk.core.Coin) Transaction(org.ethereum.core.Transaction) ArrayList(java.util.ArrayList) Block(org.ethereum.core.Block) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) BlockBuilder(co.rsk.test.builders.BlockBuilder) Test(org.junit.Test)

Example 23 with Coin

use of co.rsk.core.Coin in project rskj by rsksmart.

the class TransactionPoolImplTest method addAndExecutePendingTransaction.

@Test
public void addAndExecutePendingTransaction() {
    BlockChainImpl blockchain = createBlockchain();
    Coin balance = Coin.valueOf(1000000);
    TransactionPoolImpl transactionPool = createSampleNewTransactionPoolWithAccounts(2, balance, blockchain);
    transactionPool.processBest(blockchain.getBestBlock());
    Transaction tx = createSampleTransaction(1, 2, 1000, 0);
    Account receiver = createAccount(2);
    transactionPool.addTransaction(tx);
    Repository repository = transactionPool.getRepository();
    Assert.assertEquals(BigInteger.valueOf(1001000), repository.getBalance(receiver.getAddress()).asBigInteger());
}
Also used : Coin(co.rsk.core.Coin) Account(org.ethereum.core.Account) Repository(org.ethereum.core.Repository) Transaction(org.ethereum.core.Transaction) Test(org.junit.Test)

Example 24 with Coin

use of co.rsk.core.Coin in project rskj by rsksmart.

the class RemascProcessMinerFeesTest method validateFederatorsBalanceIsCorrect.

private void validateFederatorsBalanceIsCorrect(Repository repository, long federationReward) throws IOException, BlockStoreException {
    BridgeSupport bridgeSupport = new BridgeSupport(config, repository, null, PrecompiledContracts.BRIDGE_ADDR, null);
    RemascFederationProvider provider = new RemascFederationProvider(bridgeSupport);
    int nfederators = provider.getFederationSize();
    Coin federatorBalance = Coin.valueOf(federationReward / nfederators);
    for (int k = 0; k < nfederators; k++) {
        assertEquals(federatorBalance, repository.getBalance(provider.getFederatorAddress(k)));
    }
}
Also used : Coin(co.rsk.core.Coin) BridgeSupport(co.rsk.peg.BridgeSupport)

Example 25 with Coin

use of co.rsk.core.Coin in project rskj by rsksmart.

the class RemascProcessMinerFeesTest method processMinersFeesFromTxInvokedByAnotherContract.

@Test
public void processMinersFeesFromTxInvokedByAnotherContract() throws IOException, BlockStoreException {
    BlockChainBuilder builder = new BlockChainBuilder();
    Blockchain blockchain = builder.setTesting(true).setGenesis(genesisBlock).build();
    List<Block> blocks = createSimpleBlocks(genesisBlock, 4);
    Block blockWithOneTx = RemascTestRunner.createBlock(this.genesisBlock, blocks.get(blocks.size() - 1), PegTestUtils.createHash3(), coinbaseA, null, minerFee, 0, txValue, cowKey);
    blocks.add(blockWithOneTx);
    blocks.addAll(createSimpleBlocks(blockWithOneTx, 9));
    BlockExecutor blockExecutor = new BlockExecutor(config, blockchain.getRepository(), null, blockchain.getBlockStore(), null);
    for (Block b : blocks) {
        blockExecutor.executeAndFillAll(b, blockchain.getBestBlock());
        b.seal();
        blockchain.tryToConnect(b);
    }
    Repository repository = blockchain.getRepository();
    assertEquals(cowInitialBalance.subtract(Coin.valueOf(minerFee + txValue)), repository.getAccountState(new RskAddress(cowAddress)).getBalance());
    assertEquals(Coin.valueOf(minerFee), repository.getAccountState(PrecompiledContracts.REMASC_ADDR).getBalance());
    assertNull(repository.getAccountState(coinbaseA));
    assertNull(repository.getAccountState(remascConfig.getRskLabsAddress()));
    RemascStorageProvider remasceStorageProvider = getRemascStorageProvider(blockchain);
    assertEquals(Coin.ZERO, remasceStorageProvider.getRewardBalance());
    assertEquals(Coin.ZERO, remasceStorageProvider.getBurnedBalance());
    assertEquals(0, remasceStorageProvider.getSiblings().size());
    // A hacker trying to screw the system creates a contracts that calls remasc and a fool/accomplice miner includes that tx in a block
    // Contract code
    // pragma solidity ^0.4.3;
    // contract RemascHacker {
    // 
    // function()
    // {
    // address remasc = 0x0000000000000000000000000000000001000008;
    // remasc.call();
    // }
    // }
    long txCreateContractGasLimit = 53755 + 32000;
    Transaction txCreateContract = new Transaction(Coin.valueOf(1).getBytes(), Coin.valueOf(1).getBytes(), Coin.valueOf(txCreateContractGasLimit).getBytes(), null, Coin.ZERO.getBytes(), Hex.decode("6060604052346000575b6077806100176000396000f30060606040525b3460005760495b6000600890508073ffffffffffffffffffffffffffffffffffffffff166040518090506000604051808303816000866161da5a03f1915050505b50565b0000a165627a7a7230582036692fbb1395da1688af0189be5b0ac18df3d93a2402f4fc8f927b31c1baa2460029"), config.getBlockchainConfig().getCommonConstants().getChainId());
    txCreateContract.sign(cowKey.getPrivKeyBytes());
    long txCallRemascGasLimit = 21828;
    Transaction txCallRemasc = new Transaction(Coin.valueOf(2).getBytes(), Coin.valueOf(1).getBytes(), Coin.valueOf(txCallRemascGasLimit).getBytes(), Hex.decode("da7ce79725418f4f6e13bf5f520c89cec5f6a974"), Coin.ZERO.getBytes(), null, config.getBlockchainConfig().getCommonConstants().getChainId());
    txCallRemasc.sign(cowKey.getPrivKeyBytes());
    Block newblock = RemascTestRunner.createBlock(this.genesisBlock, blocks.get(blocks.size() - 1), PegTestUtils.createHash3(), TestUtils.randomAddress(), null, null, txCreateContract, txCallRemasc);
    blockExecutor.executeAndFillAll(newblock, blockchain.getBestBlock());
    newblock.seal();
    blockchain.tryToConnect(newblock);
    repository = blockchain.getRepository();
    // Check "hack" tx makes no changes to the remasc state, sender pays fees, and value is added to remasc account balance
    assertEquals(cowInitialBalance.subtract(Coin.valueOf(txCreateContractGasLimit + txCallRemascGasLimit + txValue + minerFee)), repository.getAccountState(new RskAddress(cowAddress)).getBalance());
    long blockReward = minerFee / remascConfig.getSyntheticSpan();
    long originalBlockReward = blockReward;
    long rskReward = blockReward / remascConfig.getRskLabsDivisor();
    assertEquals(Coin.valueOf(rskReward), repository.getAccountState(remascConfig.getRskLabsAddress()).getBalance());
    blockReward -= rskReward;
    long federationReward = blockReward / remascConfig.getFederationDivisor();
    assertEquals(33, federationReward);
    blockReward -= federationReward;
    assertEquals(Coin.valueOf(blockReward), repository.getAccountState(coinbaseA).getBalance());
    Coin expectedRewardBalance = Coin.valueOf(minerFee - originalBlockReward);
    this.validateRemascsStorageIsCorrect(this.getRemascStorageProvider(blockchain), expectedRewardBalance, Coin.ZERO, 0L);
    this.validateFederatorsBalanceIsCorrect(blockchain.getRepository(), federationReward);
}
Also used : Coin(co.rsk.core.Coin) BlockExecutor(co.rsk.core.bc.BlockExecutor) RskAddress(co.rsk.core.RskAddress) BlockChainBuilder(co.rsk.test.builders.BlockChainBuilder) Test(org.junit.Test)

Aggregations

Coin (co.rsk.core.Coin)95 Test (org.junit.Test)46 RskAddress (co.rsk.core.RskAddress)37 BigInteger (java.math.BigInteger)32 Repository (org.ethereum.core.Repository)23 Transaction (org.ethereum.core.Transaction)23 Program (org.ethereum.vm.program.Program)12 AccountState (org.ethereum.core.AccountState)10 ArrayList (java.util.ArrayList)9 Ignore (org.junit.Ignore)9 ProgramInvokeMockImpl (org.ethereum.vm.program.invoke.ProgramInvokeMockImpl)8 Account (org.ethereum.core.Account)7 BlockExecutor (co.rsk.core.bc.BlockExecutor)6 BlockChainBuilder (co.rsk.test.builders.BlockChainBuilder)6 Keccak256 (co.rsk.crypto.Keccak256)5 Block (org.ethereum.core.Block)5 DataWord (org.ethereum.vm.DataWord)5 ProgramInvoke (org.ethereum.vm.program.invoke.ProgramInvoke)5 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)4 RskSystemProperties (co.rsk.config.RskSystemProperties)4