use of co.rsk.db.RepositoryLocator 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);
}
use of co.rsk.db.RepositoryLocator 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);
}
use of co.rsk.db.RepositoryLocator 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);
}
use of co.rsk.db.RepositoryLocator in project rskj by rsksmart.
the class RemascProcessMinerFeesTest method unclesPublishingFeeMustBeRoundedAndTheRoundedSurplusBurned.
@Test
public void unclesPublishingFeeMustBeRoundedAndTheRoundedSurplusBurned() {
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 < 10; 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 -20
assertEquals(Coin.valueOf(1680 - 20), testRunner.getAccountBalance(blockAtHeightSix.getCoinbase()));
Block blockAtHeightFiveMainchain = blockchain.getBlockByNumber(6);
assertEquals(Coin.valueOf(1374 - 14), testRunner.getAccountBalance(blockAtHeightFiveMainchain.getCoinbase()));
Block blockAtHeightFiveFirstSibling = testRunner.getAddedSiblings().get(0);
assertEquals(Coin.valueOf(1374 - 14), testRunner.getAccountBalance(blockAtHeightFiveFirstSibling.getCoinbase()));
Block blockAtHeightFiveSecondSibling = testRunner.getAddedSiblings().get(1);
assertEquals(Coin.valueOf(1374 - 14), testRunner.getAccountBalance(blockAtHeightFiveSecondSibling.getCoinbase()));
Block blockAtHeightFiveThirdSibling = testRunner.getAddedSiblings().get(2);
assertEquals(Coin.valueOf(1374 - 14), testRunner.getAccountBalance(blockAtHeightFiveThirdSibling.getCoinbase()));
// Review value +6
this.validateRemascsStorageIsCorrect(getRemascStorageProvider(repository), Coin.valueOf(84000), Coin.valueOf(6 + 6), 0);
}
use of co.rsk.db.RepositoryLocator in project rskj by rsksmart.
the class RemascProcessMinerFeesTest method processMinersFeesWithoutRequiredMaturity.
@Test
public void processMinersFeesWithoutRequiredMaturity() {
List<Block> blocks = createSimpleBlocks(genesisBlock, 1);
Block blockWithOneTx = RemascTestRunner.createBlock(this.genesisBlock, blocks.get(blocks.size() - 1), PegTestUtils.createHash3(), coinbaseA, Collections.emptyList(), minerFee, 0, txValue, cowKey);
blocks.add(blockWithOneTx);
blocks.addAll(createSimpleBlocks(blockWithOneTx, 1));
Blockchain blockchain = blockchainBuilder.setBlocks(blocks).build();
RepositoryLocator repositoryLocator = blockchainBuilder.getRepositoryLocator();
RepositorySnapshot repository = repositoryLocator.snapshotAt(blockchain.getBestBlock().getHeader());
assertNull(repository.getAccountState(coinbaseA));
}
Aggregations