Search in sources :

Example 41 with RskAddress

use of co.rsk.core.RskAddress 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)

Example 42 with RskAddress

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

the class RemascProcessMinerFeesTest method processMinersFeesFromTxThatIsNotTheLatestTx.

@Test
public void processMinersFeesFromTxThatIsNotTheLatestTx() 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());
        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 tx to remasc and a fool/accomplice miner includes that tx in a block
    Transaction tx = new Transaction(Coin.valueOf(1).getBytes(), Coin.valueOf(1).getBytes(), Coin.valueOf(minerFee).getBytes(), PrecompiledContracts.REMASC_ADDR.getBytes(), Coin.valueOf(txValue * 2).getBytes(), null, config.getBlockchainConfig().getCommonConstants().getChainId());
    tx.sign(cowKey.getPrivKeyBytes());
    Block newblock = RemascTestRunner.createBlock(this.genesisBlock, blocks.get(blocks.size() - 1), PegTestUtils.createHash3(), TestUtils.randomAddress(), null, null, tx);
    blockExecutor.executeAndFillAll(newblock, blockchain.getBestBlock());
    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(minerFee + txValue + minerFee)), repository.getAccountState(new RskAddress(cowAddress)).getBalance());
    long blockReward = minerFee / remascConfig.getSyntheticSpan();
    long originalBlockReward = blockReward;
    assertEquals(Coin.valueOf(minerFee + minerFee - blockReward), repository.getAccountState(PrecompiledContracts.REMASC_ADDR).getBalance());
    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)

Example 43 with RskAddress

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

the class RemascProcessMinerFeesTest method processMinersFeesWithoutMinimumSyntheticSpan.

@Test
public void processMinersFeesWithoutMinimumSyntheticSpan() throws IOException {
    BlockChainBuilder builder = new BlockChainBuilder();
    Blockchain blockchain = builder.setTesting(true).setGenesis(genesisBlock).build();
    List<Block> blocks = createSimpleBlocks(genesisBlock, 2);
    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());
        blockchain.tryToConnect(b);
    }
    assertEquals(cowInitialBalance.subtract(Coin.valueOf(minerFee + txValue)), blockchain.getRepository().getAccountState(new RskAddress(cowAddress)).getBalance());
    Repository repository = blockchain.getRepository();
    assertEquals(Coin.valueOf(minerFee), repository.getAccountState(PrecompiledContracts.REMASC_ADDR).getBalance());
    assertNull(repository.getAccountState(coinbaseA));
    assertNull(repository.getAccountState(remascConfig.getRskLabsAddress()));
    Block newblock = RemascTestRunner.createBlock(this.genesisBlock, blocks.get(blocks.size() - 1), PegTestUtils.createHash3(), TestUtils.randomAddress(), null, null);
    blockExecutor.executeAndFillAll(newblock, blockchain.getBestBlock());
    blockchain.tryToConnect(newblock);
    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 remascStorageProvider = getRemascStorageProvider(blockchain);
    assertEquals(Coin.valueOf(minerFee), remascStorageProvider.getRewardBalance());
    assertEquals(Coin.ZERO, remascStorageProvider.getBurnedBalance());
    assertEquals(0, remascStorageProvider.getSiblings().size());
}
Also used : BlockExecutor(co.rsk.core.bc.BlockExecutor) RskAddress(co.rsk.core.RskAddress) BlockChainBuilder(co.rsk.test.builders.BlockChainBuilder) Test(org.junit.Test)

Example 44 with RskAddress

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

the class RemascStorageProviderTest method setAndGetBrokenSelectionRule.

@Test
public void setAndGetBrokenSelectionRule() {
    RskAddress accountAddress = randomAddress();
    Repository repository = new RepositoryImpl(config);
    RemascStorageProvider provider = new RemascStorageProvider(repository, accountAddress);
    provider.setBrokenSelectionRule(Boolean.TRUE);
    Assert.assertEquals(Boolean.TRUE, provider.getBrokenSelectionRule());
}
Also used : Repository(org.ethereum.core.Repository) RepositoryImpl(co.rsk.db.RepositoryImpl) RskAddress(co.rsk.core.RskAddress) Test(org.junit.Test)

Example 45 with RskAddress

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

the class RemascStorageProviderTest method setSaveRetrieveAndGetManySiblings.

@Test
public void setSaveRetrieveAndGetManySiblings() throws IOException {
    RskAddress accountAddress = randomAddress();
    Repository repository = new RepositoryImplForTesting();
    RemascStorageProvider provider = new RemascStorageProvider(repository, accountAddress);
    BlockGenerator blockGenerator = new BlockGenerator();
    Block genesis = blockGenerator.getGenesisBlock();
    Block block1 = blockGenerator.createChildBlock(genesis);
    Block block2 = blockGenerator.createChildBlock(block1);
    Block block3 = blockGenerator.createChildBlock(block2);
    Block block4 = blockGenerator.createChildBlock(block3);
    Block block5 = blockGenerator.createChildBlock(block4);
    Sibling sibling1 = new Sibling(genesis.getHeader(), genesis.getCoinbase(), 1);
    Sibling sibling2 = new Sibling(block1.getHeader(), block1.getCoinbase(), 2);
    Sibling sibling3 = new Sibling(block2.getHeader(), block2.getCoinbase(), 3);
    Sibling sibling4 = new Sibling(block3.getHeader(), block3.getCoinbase(), 4);
    Sibling sibling5 = new Sibling(block4.getHeader(), block4.getCoinbase(), 5);
    Sibling sibling6 = new Sibling(block5.getHeader(), block5.getCoinbase(), 6);
    List<Sibling> siblings0 = new ArrayList<>();
    List<Sibling> siblings1 = new ArrayList<>();
    List<Sibling> siblings2 = new ArrayList<>();
    siblings0.add(sibling1);
    siblings0.add(sibling2);
    siblings1.add(sibling3);
    siblings1.add(sibling4);
    siblings2.add(sibling5);
    siblings2.add(sibling6);
    provider.getSiblings().put(Long.valueOf(0), siblings0);
    provider.getSiblings().put(Long.valueOf(1), siblings1);
    provider.getSiblings().put(Long.valueOf(2), siblings2);
    provider.save();
    RemascStorageProvider newProvider = new RemascStorageProvider(repository, accountAddress);
    SortedMap<Long, List<Sibling>> map = newProvider.getSiblings();
    Assert.assertNotNull(map);
    Assert.assertFalse(map.isEmpty());
    Assert.assertTrue(map.containsKey(Long.valueOf(0)));
    Assert.assertTrue(map.containsKey(Long.valueOf(1)));
    Assert.assertTrue(map.containsKey(Long.valueOf(2)));
    Assert.assertEquals(2, map.get(Long.valueOf(0)).size());
    Assert.assertEquals(2, map.get(Long.valueOf(1)).size());
    Assert.assertEquals(2, map.get(Long.valueOf(2)).size());
    List<Sibling> list0 = map.get(Long.valueOf(0));
    List<Sibling> list1 = map.get(Long.valueOf(1));
    List<Sibling> list2 = map.get(Long.valueOf(2));
    Assert.assertEquals(1, list0.get(0).getIncludedHeight());
    Assert.assertArrayEquals(genesis.getHeader().getHash().getBytes(), list0.get(0).getHash());
    Assert.assertEquals(2, list0.get(1).getIncludedHeight());
    Assert.assertArrayEquals(block1.getHeader().getHash().getBytes(), list0.get(1).getHash());
    Assert.assertEquals(3, list1.get(0).getIncludedHeight());
    Assert.assertArrayEquals(block2.getHeader().getHash().getBytes(), list1.get(0).getHash());
    Assert.assertEquals(4, list1.get(1).getIncludedHeight());
    Assert.assertArrayEquals(block3.getHeader().getHash().getBytes(), list1.get(1).getHash());
    Assert.assertEquals(5, list2.get(0).getIncludedHeight());
    Assert.assertArrayEquals(block4.getHeader().getHash().getBytes(), list2.get(0).getHash());
    Assert.assertEquals(6, list2.get(1).getIncludedHeight());
    Assert.assertArrayEquals(block5.getHeader().getHash().getBytes(), list2.get(1).getHash());
}
Also used : Repository(org.ethereum.core.Repository) RskAddress(co.rsk.core.RskAddress) RepositoryImplForTesting(co.rsk.db.RepositoryImplForTesting) ArrayList(java.util.ArrayList) Block(org.ethereum.core.Block) ArrayList(java.util.ArrayList) List(java.util.List) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) Test(org.junit.Test)

Aggregations

RskAddress (co.rsk.core.RskAddress)174 Test (org.junit.Test)102 Repository (org.ethereum.core.Repository)60 BigInteger (java.math.BigInteger)47 Coin (co.rsk.core.Coin)38 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)29 DataWord (org.ethereum.vm.DataWord)27 TrieImplHashTest (co.rsk.trie.TrieImplHashTest)24 RepositoryImpl (co.rsk.db.RepositoryImpl)16 SimpleRskTransaction (co.rsk.peg.simples.SimpleRskTransaction)15 Transaction (org.ethereum.core.Transaction)15 Program (org.ethereum.vm.program.Program)15 InvocationOnMock (org.mockito.invocation.InvocationOnMock)14 AccountState (org.ethereum.core.AccountState)12 HashMapDB (org.ethereum.datasource.HashMapDB)11 ArrayList (java.util.ArrayList)10 ProgramInvokeMockImpl (org.ethereum.vm.program.invoke.ProgramInvokeMockImpl)10 BridgeConstants (co.rsk.config.BridgeConstants)8 RskSystemProperties (co.rsk.config.RskSystemProperties)8 TrieStoreImpl (co.rsk.trie.TrieStoreImpl)8