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);
}
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);
}
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());
}
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());
}
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());
}
Aggregations