Search in sources :

Example 26 with RepositorySnapshot

use of co.rsk.db.RepositorySnapshot in project rskj by rsksmart.

the class RemascProcessMinerFeesTest method siblingIncludedOneBlockLater.

@Test
public void siblingIncludedOneBlockLater() {
    RskSystemProperties config = spy(new TestSystemProperties());
    when(config.getActivationConfig()).thenReturn(ActivationConfigsForTest.allBut(ConsensusRule.RSKIP85));
    BlockChainBuilder builder = new BlockChainBuilder().setTesting(true).setGenesis(genesisBlock).setConfig(config);
    List<SiblingElement> siblings = Collections.singletonList(new SiblingElement(5, 7, this.minerFee));
    RemascTestRunner testRunner = new RemascTestRunner(builder, this.genesisBlock).txValue(txValue).minerFee(this.minerFee).initialHeight(15).siblingElements(siblings).txSigningKey(this.cowKey).gasPrice(1L);
    testRunner.start();
    Blockchain blockchain = testRunner.getBlockChain();
    RepositoryLocator repositoryLocator = builder.getRepositoryLocator();
    RepositorySnapshot repository = repositoryLocator.snapshotAt(blockchain.getBestBlock().getHeader());
    Block blockAtHeightSeven = blockchain.getBlockByNumber(8);
    assertEquals(Coin.valueOf(1680L - 17L), testRunner.getAccountBalance(blockAtHeightSeven.getCoinbase()));
    Block blockAtHeightFiveMainchain = blockchain.getBlockByNumber(6);
    assertEquals(Coin.valueOf(7560L - 76L), testRunner.getAccountBalance(blockAtHeightFiveMainchain.getCoinbase()));
    Block blockAtHeightFiveSibling = testRunner.getAddedSiblings().get(0);
    assertEquals(Coin.valueOf(7182L - 72L), testRunner.getAccountBalance(blockAtHeightFiveSibling.getCoinbase()));
    this.validateRemascsStorageIsCorrect(getRemascStorageProvider(repository), Coin.valueOf(84000L), Coin.valueOf(378L - 3L), 0L);
}
Also used : RepositoryLocator(co.rsk.db.RepositoryLocator) RepositorySnapshot(co.rsk.db.RepositorySnapshot) BlockChainBuilder(co.rsk.test.builders.BlockChainBuilder) RskSystemProperties(co.rsk.config.RskSystemProperties) TestSystemProperties(co.rsk.config.TestSystemProperties) Test(org.junit.Test) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)

Example 27 with RepositorySnapshot

use of co.rsk.db.RepositorySnapshot in project rskj by rsksmart.

the class RemascProcessMinerFeesTest method siblingsFeeForMiningBlockMustBeRoundedAndTheRoundedSurplusBurned.

@Test
public void siblingsFeeForMiningBlockMustBeRoundedAndTheRoundedSurplusBurned() {
    RskSystemProperties config = spy(new TestSystemProperties());
    when(config.getActivationConfig()).thenReturn(ActivationConfigsForTest.allBut(ConsensusRule.RSKIP85));
    BlockChainBuilder builder = new BlockChainBuilder().setTesting(true).setGenesis(genesisBlock).setConfig(config);
    long minerFee = 21000;
    List<SiblingElement> siblings = new ArrayList<>();
    for (int i = 0; i < 9; i++) {
        siblings.add(new SiblingElement(5, 6, minerFee));
    }
    RemascTestRunner testRunner = new RemascTestRunner(builder, this.genesisBlock).txValue(txValue).minerFee(minerFee).initialHeight(15).siblingElements(siblings).txSigningKey(this.cowKey).gasPrice(1L);
    testRunner.start();
    Blockchain blockchain = testRunner.getBlockChain();
    RepositoryLocator repositoryLocator = builder.getRepositoryLocator();
    RepositorySnapshot repository = repositoryLocator.snapshotAt(blockchain.getBestBlock().getHeader());
    Block blockAtHeightSix = blockchain.getBlockByNumber(7);
    // TODO Review value 18
    assertEquals(Coin.valueOf(1674 - 18), testRunner.getAccountBalance(blockAtHeightSix.getCoinbase()));
    Block blockAtHeightSeven = blockchain.getBlockByNumber(8);
    assertNull(testRunner.getAccountBalance(blockAtHeightSeven.getCoinbase()));
    Block blockAtHeightFiveMainchain = blockchain.getBlockByNumber(6);
    assertEquals(Coin.valueOf(1512 - 16), testRunner.getAccountBalance(blockAtHeightFiveMainchain.getCoinbase()));
    Block blockAtHeightFiveFirstSibling = testRunner.getAddedSiblings().get(0);
    assertEquals(Coin.valueOf(1512 - 16), testRunner.getAccountBalance(blockAtHeightFiveFirstSibling.getCoinbase()));
    Block blockAtHeightFiveSecondSibling = testRunner.getAddedSiblings().get(1);
    assertEquals(Coin.valueOf(1512 - 16), testRunner.getAccountBalance(blockAtHeightFiveSecondSibling.getCoinbase()));
    // TODO Review value 16 (original value 6)
    this.validateRemascsStorageIsCorrect(getRemascStorageProvider(repository), Coin.valueOf(84000), Coin.valueOf(16), 0L);
}
Also used : BlockChainBuilder(co.rsk.test.builders.BlockChainBuilder) RepositoryLocator(co.rsk.db.RepositoryLocator) RepositorySnapshot(co.rsk.db.RepositorySnapshot) RskSystemProperties(co.rsk.config.RskSystemProperties) TestSystemProperties(co.rsk.config.TestSystemProperties) Test(org.junit.Test) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)

Example 28 with RepositorySnapshot

use of co.rsk.db.RepositorySnapshot in project rskj by rsksmart.

the class RemascProcessMinerFeesTest method siblingIncludedSevenBlocksLater.

@Test
public void siblingIncludedSevenBlocksLater() {
    RskSystemProperties config = spy(new TestSystemProperties());
    when(config.getActivationConfig()).thenReturn(ActivationConfigsForTest.allBut(ConsensusRule.RSKIP85));
    BlockChainBuilder builder = new BlockChainBuilder().setTesting(true).setGenesis(genesisBlock).setConfig(config);
    List<SiblingElement> siblings = Collections.singletonList(new SiblingElement(5, 12, this.minerFee));
    RemascTestRunner testRunner = new RemascTestRunner(builder, this.genesisBlock).txValue(txValue).minerFee(this.minerFee).initialHeight(15).siblingElements(siblings).txSigningKey(this.cowKey).gasPrice(1L);
    testRunner.start();
    Blockchain blockchain = testRunner.getBlockChain();
    RepositoryLocator repositoryLocator = builder.getRepositoryLocator();
    RepositorySnapshot repository = repositoryLocator.snapshotAt(blockchain.getBestBlock().getHeader());
    Block blockAtHeightTwelve = blockchain.getBlockByNumber(13);
    assertEquals(Coin.valueOf(1680L - 17), testRunner.getAccountBalance(blockAtHeightTwelve.getCoinbase()));
    Block blockAtHeightFiveMainchain = blockchain.getBlockByNumber(6);
    assertEquals(Coin.valueOf(7560L - 76), testRunner.getAccountBalance(blockAtHeightFiveMainchain.getCoinbase()));
    Block blockAtHeightFiveSibling = testRunner.getAddedSiblings().get(0);
    assertEquals(Coin.valueOf(5292L - 53), testRunner.getAccountBalance(blockAtHeightFiveSibling.getCoinbase()));
    Coin remascCurrentBalance = testRunner.getAccountBalance(PrecompiledContracts.REMASC_ADDR);
    // TODO review value -22
    assertEquals(Coin.valueOf(296268L - 22), remascCurrentBalance);
    // TODO review value -22
    this.validateRemascsStorageIsCorrect(getRemascStorageProvider(repository), Coin.valueOf(84000L), Coin.valueOf(2268L - 22), 0L);
}
Also used : RepositoryLocator(co.rsk.db.RepositoryLocator) Coin(co.rsk.core.Coin) RepositorySnapshot(co.rsk.db.RepositorySnapshot) BlockChainBuilder(co.rsk.test.builders.BlockChainBuilder) RskSystemProperties(co.rsk.config.RskSystemProperties) TestSystemProperties(co.rsk.config.TestSystemProperties) Test(org.junit.Test) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)

Example 29 with RepositorySnapshot

use of co.rsk.db.RepositorySnapshot in project rskj by rsksmart.

the class RemascProcessMinerFeesTest method processMinersFeesWithOneSiblingBrokenSelectionRule.

private void processMinersFeesWithOneSiblingBrokenSelectionRule(String reasonForBrokenSelectionRule) {
    Blockchain blockchain = blockchainBuilder.build();
    BlockStore blockStore = blockchainBuilder.getBlockStore();
    RepositoryLocator repositoryLocator = blockchainBuilder.getRepositoryLocator();
    int NHASH = 200;
    final long NUMBER_OF_TXS_WITH_FEES = 3;
    List<Block> blocks = createSimpleBlocks(this.genesisBlock, 4);
    Block blockWithOneTxA;
    Block blockWithOneTxB;
    if ("higherFees".equals(reasonForBrokenSelectionRule)) {
        blockWithOneTxA = RemascTestRunner.createBlock(this.genesisBlock, blocks.get(blocks.size() - 1), PegTestUtils.createHash3(NHASH++), coinbaseA, Collections.emptyList(), minerFee, 0, txValue, cowKey);
        blockWithOneTxB = RemascTestRunner.createBlock(this.genesisBlock, blocks.get(blocks.size() - 1), PegTestUtils.createHash3(NHASH++), coinbaseB, Collections.emptyList(), minerFee * 3, 0, txValue, cowKey);
    } else {
        Keccak256 blockWithOneTxBHash = PegTestUtils.createHash3(NHASH++);
        Keccak256 blockWithOneTxAHash = PegTestUtils.createHash3(NHASH++);
        blockWithOneTxA = RemascTestRunner.createBlock(this.genesisBlock, blocks.get(blocks.size() - 1), blockWithOneTxAHash, coinbaseA, Collections.emptyList(), minerFee, 0, txValue, cowKey);
        blockWithOneTxB = RemascTestRunner.createBlock(this.genesisBlock, blocks.get(blocks.size() - 1), blockWithOneTxBHash, coinbaseB, Collections.emptyList(), (long) (minerFee * 1.5), 0, txValue, cowKey);
    }
    blocks.add(blockWithOneTxA);
    Block blockThatIncludesUncleC = RemascTestRunner.createBlock(this.genesisBlock, blockWithOneTxA, PegTestUtils.createHash3(NHASH++), coinbaseC, Collections.singletonList(blockWithOneTxB.getHeader()), minerFee, 1, txValue, cowKey);
    blocks.add(blockThatIncludesUncleC);
    Block blockWithOneTxD = RemascTestRunner.createBlock(this.genesisBlock, blockThatIncludesUncleC, PegTestUtils.createHash3(NHASH++), coinbaseD, Collections.emptyList(), minerFee, 2, txValue, cowKey);
    blocks.add(blockWithOneTxD);
    blocks.addAll(createSimpleBlocks(blockWithOneTxD, 7));
    BlockExecutor blockExecutor = buildBlockExecutor(repositoryLocator, blockStore);
    boolean isSuccessful = executeBlocks(blockchain, blocks, blockExecutor);
    assertTrue(isSuccessful);
    RepositorySnapshot repository = repositoryLocator.snapshotAt(blockchain.getBestBlock().getHeader());
    // validate that the blockchain's and REMASC's initial states are correct
    Coin cowRemainingBalance = cowInitialBalance.subtract(Coin.valueOf(minerFee * NUMBER_OF_TXS_WITH_FEES + txValue * NUMBER_OF_TXS_WITH_FEES));
    List<Long> otherAccountsBalance = new ArrayList<>(Arrays.asList(null, null, null, null));
    this.validateAccountsCurrentBalanceIsCorrect(repository, cowRemainingBalance, minerFee * NUMBER_OF_TXS_WITH_FEES, null, this.getAccountsWithExpectedBalance(otherAccountsBalance));
    this.validateRemascsStorageIsCorrect(getRemascStorageProvider(repository), Coin.ZERO, Coin.ZERO, 1L);
    // add block to pay fees of blocks on blockchain's height 5
    Block blockToPayFeesOnHeightFive = RemascTestRunner.createBlock(this.genesisBlock, blocks.get(blocks.size() - 1), PegTestUtils.createHash3(NHASH++), TestUtils.randomAddress(), Collections.emptyList(), null);
    blockExecutor.executeAndFillAll(blockToPayFeesOnHeightFive, blockchain.getBestBlock().getHeader());
    ImportResult importResult = blockchain.tryToConnect(blockToPayFeesOnHeightFive);
    assertTrue(importResult.isSuccessful());
    repository = repositoryLocator.snapshotAt(blockchain.getBestBlock().getHeader());
    // -- After executing REMASC's contract for paying height 5 blocks
    // validate that account's balances are correct
    long blockRewardOnHeightFive = minerFee / remascConfig.getSyntheticSpan();
    long remascCurrentBalance = minerFee * 3 - blockRewardOnHeightFive;
    long rskReward = blockRewardOnHeightFive / remascConfig.getRskLabsDivisor();
    long rskCurrentBalance = rskReward;
    blockRewardOnHeightFive -= rskReward;
    long federationReward = blockRewardOnHeightFive / remascConfig.getFederationDivisor();
    blockRewardOnHeightFive -= federationReward;
    long publisherReward = blockRewardOnHeightFive / remascConfig.getPublishersDivisor();
    blockRewardOnHeightFive -= publisherReward;
    long minerRewardOnHeightFive = blockRewardOnHeightFive / 2;
    List<Long> otherAccountsBalanceOnHeightFive = new ArrayList<>(Arrays.asList(minerRewardOnHeightFive, minerRewardOnHeightFive, publisherReward, null));
    this.validateFederatorsBalanceIsCorrect(repository, federationReward, blockToPayFeesOnHeightFive);
    // TODO review value + 1
    this.validateAccountsCurrentBalanceIsCorrect(repository, cowRemainingBalance, remascCurrentBalance + 1, rskCurrentBalance, this.getAccountsWithExpectedBalance(otherAccountsBalanceOnHeightFive));
    // validate that REMASC's state is correct
    blockRewardOnHeightFive = minerFee / remascConfig.getSyntheticSpan();
    Coin expectedRewardBalance = Coin.valueOf(minerFee - blockRewardOnHeightFive);
    // TODO review burned value 1
    this.validateRemascsStorageIsCorrect(getRemascStorageProvider(repository), expectedRewardBalance, Coin.valueOf(1), 0L);
    // add block to pay fees of blocks on blockchain's height 6
    Block blockToPayFeesOnHeightSix = RemascTestRunner.createBlock(this.genesisBlock, blockToPayFeesOnHeightFive, PegTestUtils.createHash3(NHASH++), TestUtils.randomAddress(), Collections.emptyList(), null);
    blockExecutor.executeAndFillAll(blockToPayFeesOnHeightSix, blockchain.getBestBlock().getHeader());
    importResult = blockchain.tryToConnect(blockToPayFeesOnHeightSix);
    assertTrue(importResult.isSuccessful());
    repository = repositoryLocator.snapshotAt(blockchain.getBestBlock().getHeader());
    // -- After executing REMASC's contract for paying height 6 blocks
    // validate that account's balances are correct
    long rewardBalance = minerFee - blockRewardOnHeightFive + minerFee;
    long blockRewardOnHeightSix = rewardBalance / remascConfig.getSyntheticSpan();
    rewardBalance -= blockRewardOnHeightSix;
    rskReward = blockRewardOnHeightSix / remascConfig.getRskLabsDivisor();
    long blockRewardWithoutRskFee = blockRewardOnHeightSix - rskReward;
    long federationReward2 = blockRewardWithoutRskFee / remascConfig.getFederationDivisor();
    long blockRewardWithoutRskAndFederationFee = blockRewardWithoutRskFee - federationReward2;
    long burnedBalance = blockRewardWithoutRskAndFederationFee / remascConfig.getPunishmentDivisor();
    remascCurrentBalance = minerFee * NUMBER_OF_TXS_WITH_FEES - blockRewardOnHeightFive - blockRewardOnHeightSix + burnedBalance;
    rskCurrentBalance += blockRewardOnHeightSix / remascConfig.getRskLabsDivisor();
    blockRewardOnHeightSix -= blockRewardOnHeightSix / remascConfig.getRskLabsDivisor();
    blockRewardOnHeightSix -= blockRewardOnHeightSix / remascConfig.getFederationDivisor();
    blockRewardOnHeightSix -= blockRewardOnHeightSix / remascConfig.getPunishmentDivisor();
    List<Long> otherAccountsBalanceOnHeightSix = new ArrayList<>(Arrays.asList(minerRewardOnHeightFive, minerRewardOnHeightFive, publisherReward + blockRewardOnHeightSix, null));
    // TODO review + 1
    this.validateAccountsCurrentBalanceIsCorrect(repository, cowRemainingBalance, remascCurrentBalance + 1, rskCurrentBalance, this.getAccountsWithExpectedBalance(otherAccountsBalanceOnHeightSix));
    this.validateFederatorsBalanceIsCorrect(repository, federationReward + federationReward2, blockToPayFeesOnHeightSix);
    // TODO review + 1
    this.validateRemascsStorageIsCorrect(getRemascStorageProvider(repository), Coin.valueOf(rewardBalance), Coin.valueOf(burnedBalance + 1), 0L);
}
Also used : BlockStore(org.ethereum.db.BlockStore) BlockExecutor(co.rsk.core.bc.BlockExecutor) Keccak256(co.rsk.crypto.Keccak256) RepositoryLocator(co.rsk.db.RepositoryLocator) Coin(co.rsk.core.Coin) RepositorySnapshot(co.rsk.db.RepositorySnapshot)

Example 30 with RepositorySnapshot

use of co.rsk.db.RepositorySnapshot in project rskj by rsksmart.

the class RemascProcessMinerFeesTest method processMinersFeesWithOneSibling.

@Test
public void processMinersFeesWithOneSibling() {
    Blockchain blockchain = blockchainBuilder.build();
    BlockStore blockStore = blockchainBuilder.getBlockStore();
    RepositoryLocator repositoryLocator = blockchainBuilder.getRepositoryLocator();
    List<Block> blocks = createSimpleBlocks(genesisBlock, 4);
    Block blockWithOneTxA = RemascTestRunner.createBlock(this.genesisBlock, blocks.get(blocks.size() - 1), PegTestUtils.createHash3(), coinbaseA, Collections.emptyList(), minerFee, 0, txValue, cowKey);
    Block blockWithOneTxB = RemascTestRunner.createBlock(this.genesisBlock, blocks.get(blocks.size() - 1), PegTestUtils.createHash3(), coinbaseB, Collections.emptyList(), (long) (minerFee * 1.5), 0, txValue, cowKey);
    blocks.add(blockWithOneTxA);
    Block blockThatIncludesUncle = RemascTestRunner.createBlock(this.genesisBlock, blockWithOneTxA, PegTestUtils.createHash3(), coinbaseC, Collections.singletonList(blockWithOneTxB.getHeader()), null);
    blocks.add(blockThatIncludesUncle);
    blocks.addAll(createSimpleBlocks(blockThatIncludesUncle, 8));
    BlockExecutor blockExecutor = buildBlockExecutor(repositoryLocator, blockStore);
    executeBlocks(blockchain, blocks, blockExecutor);
    RepositorySnapshot repository = repositoryLocator.snapshotAt(blockchain.getBestBlock().getHeader());
    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(coinbaseB));
    assertNull(repository.getAccountState(coinbaseC));
    assertNull(repository.getAccountState(remascConfig.getRskLabsAddress()));
    RemascStorageProvider remascStorageProvider = getRemascStorageProvider(repository);
    assertEquals(Coin.ZERO, remascStorageProvider.getRewardBalance());
    assertEquals(Coin.ZERO, remascStorageProvider.getBurnedBalance());
    Block newblock = RemascTestRunner.createBlock(this.genesisBlock, blocks.get(blocks.size() - 1), PegTestUtils.createHash3(), TestUtils.randomAddress(), Collections.emptyList(), null);
    blockExecutor.executeAndFillAll(newblock, blockchain.getBestBlock().getHeader());
    blockchain.tryToConnect(newblock);
    repository = repositoryLocator.snapshotAt(blockchain.getBestBlock().getHeader());
    assertEquals(cowInitialBalance.subtract(Coin.valueOf(minerFee + txValue)), repository.getAccountState(new RskAddress(cowAddress)).getBalance());
    long blockReward = minerFee / remascConfig.getSyntheticSpan();
    // There is one unit burned
    assertEquals(Coin.valueOf(minerFee - blockReward + 1), repository.getAccountState(PrecompiledContracts.REMASC_ADDR).getBalance());
    long rskReward = blockReward / remascConfig.getRskLabsDivisor();
    assertEquals(Coin.valueOf(rskReward), repository.getAccountState(remascConfig.getRskLabsAddress()).getBalance());
    long federationReward = (blockReward - rskReward) / remascConfig.getFederationDivisor();
    assertEquals(33, federationReward);
    blockReward = blockReward - rskReward - federationReward;
    assertEquals(Coin.valueOf(blockReward / remascConfig.getPublishersDivisor()), repository.getAccountState(coinbaseC).getBalance());
    blockReward = blockReward - blockReward / remascConfig.getPublishersDivisor();
    assertEquals(Coin.valueOf(blockReward / 2), repository.getAccountState(coinbaseA).getBalance());
    assertEquals(Coin.valueOf(blockReward / 2), repository.getAccountState(coinbaseB).getBalance());
    blockReward = minerFee / remascConfig.getSyntheticSpan();
    remascStorageProvider = getRemascStorageProvider(repository);
    assertEquals(Coin.valueOf(minerFee - blockReward), remascStorageProvider.getRewardBalance());
    assertEquals(Coin.valueOf(1), remascStorageProvider.getBurnedBalance());
    this.validateFederatorsBalanceIsCorrect(repository, federationReward, newblock);
}
Also used : RepositoryLocator(co.rsk.db.RepositoryLocator) RepositorySnapshot(co.rsk.db.RepositorySnapshot) BlockStore(org.ethereum.db.BlockStore) BlockExecutor(co.rsk.core.bc.BlockExecutor) RskAddress(co.rsk.core.RskAddress) Test(org.junit.Test) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)

Aggregations

RepositorySnapshot (co.rsk.db.RepositorySnapshot)37 Test (org.junit.Test)25 RepositoryLocator (co.rsk.db.RepositoryLocator)18 ActivationConfigsForTest (org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)13 RskAddress (co.rsk.core.RskAddress)12 BlockStore (org.ethereum.db.BlockStore)11 BigInteger (java.math.BigInteger)10 Coin (co.rsk.core.Coin)9 BlockExecutor (co.rsk.core.bc.BlockExecutor)8 World (co.rsk.test.World)7 TestSystemProperties (co.rsk.config.TestSystemProperties)6 RskSystemProperties (co.rsk.config.RskSystemProperties)5 BlockChainBuilder (co.rsk.test.builders.BlockChainBuilder)5 Account (org.ethereum.core.Account)5 AccountInformationProvider (co.rsk.core.bc.AccountInformationProvider)3 TransactionGateway (co.rsk.net.TransactionGateway)3 DslParser (co.rsk.test.dsl.DslParser)3 WorldDslProcessor (co.rsk.test.dsl.WorldDslProcessor)3 TrieStore (co.rsk.trie.TrieStore)3 Block (org.ethereum.core.Block)3