Search in sources :

Example 26 with RepositoryLocator

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

the class TransactionPoolImplTest method setUp.

@Before
public void setUp() {
    rskTestContext = new RskTestContext(new String[] { "--regtest" }) {

        @Override
        protected GenesisLoader buildGenesisLoader() {
            return new TestGenesisLoader(getTrieStore(), "rsk-unittests.json", BigInteger.ZERO, true, true, true);
        }

        @Override
        protected RepositoryLocator buildRepositoryLocator() {
            return spy(super.buildRepositoryLocator());
        }
    };
    blockChain = rskTestContext.getBlockchain();
    RepositoryLocator repositoryLocator = rskTestContext.getRepositoryLocator();
    repository = repositoryLocator.startTrackingAt(blockChain.getBestBlock().getHeader());
    signatureCache = spy(rskTestContext.getReceivedTxSignatureCache());
    transactionPool = new TransactionPoolImpl(rskTestContext.getRskSystemProperties(), repositoryLocator, rskTestContext.getBlockStore(), rskTestContext.getBlockFactory(), rskTestContext.getCompositeEthereumListener(), rskTestContext.getTransactionExecutorFactory(), signatureCache, 10, 100);
    // don't call start to avoid creating threads
    transactionPool.processBest(blockChain.getBestBlock());
    // this is to workaround the current test structure, which abuses the Repository by
    // modifying it in place
    doReturn(repository).when(repositoryLocator).snapshotAt(any());
}
Also used : GenesisLoader(org.ethereum.core.genesis.GenesisLoader) TestGenesisLoader(co.rsk.core.genesis.TestGenesisLoader) RepositoryLocator(co.rsk.db.RepositoryLocator) RskTestContext(org.ethereum.util.RskTestContext) TestGenesisLoader(co.rsk.core.genesis.TestGenesisLoader) Before(org.junit.Before)

Example 27 with RepositoryLocator

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

the class RemascProcessMinerFeesTest method processMinersFeesWithNoSiblings.

@Test
public void processMinersFeesWithNoSiblings() {
    Blockchain blockchain = blockchainBuilder.build();
    BlockStore blockStore = blockchainBuilder.getBlockStore();
    RepositoryLocator repositoryLocator = blockchainBuilder.getRepositoryLocator();
    List<Block> blocks = createSimpleBlocks(genesisBlock, 4);
    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, 9));
    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(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();
    assertEquals(Coin.valueOf(minerFee - blockReward), 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);
    assertEquals(Coin.valueOf(blockReward - rskReward - federationReward), repository.getAccountState(coinbaseA).getBalance());
    remascStorageProvider = getRemascStorageProvider(repository);
    assertEquals(Coin.valueOf(minerFee - blockReward), remascStorageProvider.getRewardBalance());
    assertEquals(Coin.ZERO, 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)

Example 28 with RepositoryLocator

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

the class RemascProcessMinerFeesTest method noPublisherFeeIsPaidWhenThePublisherHasNoSiblings.

@Test
public void noPublisherFeeIsPaidWhenThePublisherHasNoSiblings() {
    Blockchain blockchain = blockchainBuilder.build();
    BlockStore blockStore = blockchainBuilder.getBlockStore();
    RepositoryLocator repositoryLocator = blockchainBuilder.getRepositoryLocator();
    final long NUMBER_OF_TXS_WITH_FEES = 3;
    List<Block> blocks = createSimpleBlocks(genesisBlock, 4);
    Block blockWithOneTxD = RemascTestRunner.createBlock(this.genesisBlock, blocks.get(blocks.size() - 1), PegTestUtils.createHash3(), coinbaseD, Collections.emptyList(), minerFee, 0, txValue, cowKey);
    blocks.add(blockWithOneTxD);
    Block blockWithOneTxA = RemascTestRunner.createBlock(this.genesisBlock, blockWithOneTxD, PegTestUtils.createHash3(), coinbaseA, Collections.emptyList(), minerFee, 1, txValue, cowKey);
    Block blockWithOneTxB = RemascTestRunner.createBlock(this.genesisBlock, blockWithOneTxD, PegTestUtils.createHash3(), coinbaseB, Collections.emptyList(), minerFee * 3, 1, txValue, cowKey);
    blocks.add(blockWithOneTxA);
    Block blockThatIncludesUncleC = RemascTestRunner.createBlock(this.genesisBlock, blockWithOneTxA, PegTestUtils.createHash3(), coinbaseC, Collections.singletonList(blockWithOneTxB.getHeader()), minerFee, 2, txValue, cowKey);
    blocks.add(blockThatIncludesUncleC);
    blocks.addAll(createSimpleBlocks(blockThatIncludesUncleC, 7));
    BlockExecutor blockExecutor = buildBlockExecutor(repositoryLocator, blockStore);
    executeBlocks(blockchain, blocks, blockExecutor);
    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 4
    Block blockToPayFeesOnHeightFour = RemascTestRunner.createBlock(this.genesisBlock, blocks.get(blocks.size() - 1), PegTestUtils.createHash3(), TestUtils.randomAddress(), Collections.emptyList(), minerFee, 0, txValue, cowKey);
    blockExecutor.executeAndFillAll(blockToPayFeesOnHeightFour, blockchain.getBestBlock().getHeader());
    blockchain.tryToConnect(blockToPayFeesOnHeightFour);
    repository = repositoryLocator.snapshotAt(blockchain.getBestBlock().getHeader());
    // -- After executing REMASC's contract for paying height 4 block
    // validate that account's balances are correct
    long blockRewardOnHeightFour = minerFee / remascConfig.getSyntheticSpan();
    long remascCurrentBalance = minerFee * 3 - blockRewardOnHeightFour;
    long rskCurrentBalance = blockRewardOnHeightFour / remascConfig.getRskLabsDivisor();
    blockRewardOnHeightFour -= rskCurrentBalance;
    long federationReward = blockRewardOnHeightFour / remascConfig.getFederationDivisor();
    assertEquals(33, federationReward);
    blockRewardOnHeightFour -= federationReward;
    List<Long> otherAccountsBalanceOnHeightFour = new ArrayList<>(Arrays.asList(null, null, null, blockRewardOnHeightFour));
    this.validateAccountsCurrentBalanceIsCorrect(repository, cowRemainingBalance, remascCurrentBalance, rskCurrentBalance, this.getAccountsWithExpectedBalance(otherAccountsBalanceOnHeightFour));
    // validate that REMASC's state is correct
    blockRewardOnHeightFour = minerFee / remascConfig.getSyntheticSpan();
    Coin expectedRewardBalance = Coin.valueOf(minerFee - blockRewardOnHeightFour);
    this.validateRemascsStorageIsCorrect(getRemascStorageProvider(repository), expectedRewardBalance, Coin.ZERO, 1L);
    this.validateFederatorsBalanceIsCorrect(repository, federationReward, blockToPayFeesOnHeightFour);
}
Also used : RepositoryLocator(co.rsk.db.RepositoryLocator) Coin(co.rsk.core.Coin) RepositorySnapshot(co.rsk.db.RepositorySnapshot) BlockStore(org.ethereum.db.BlockStore) BlockExecutor(co.rsk.core.bc.BlockExecutor) Test(org.junit.Test) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)

Example 29 with RepositoryLocator

use of co.rsk.db.RepositoryLocator 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 30 with RepositoryLocator

use of co.rsk.db.RepositoryLocator 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)

Aggregations

RepositoryLocator (co.rsk.db.RepositoryLocator)38 Test (org.junit.Test)25 BlockStore (org.ethereum.db.BlockStore)21 RepositorySnapshot (co.rsk.db.RepositorySnapshot)18 BlockExecutor (co.rsk.core.bc.BlockExecutor)15 ActivationConfigsForTest (org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)14 HashMapDB (org.ethereum.datasource.HashMapDB)10 TestSystemProperties (co.rsk.config.TestSystemProperties)9 Coin (co.rsk.core.Coin)7 World (co.rsk.test.World)7 RskAddress (co.rsk.core.RskAddress)6 StateRootHandler (co.rsk.db.StateRootHandler)6 TransactionGateway (co.rsk.net.TransactionGateway)6 BlockChainBuilder (co.rsk.test.builders.BlockChainBuilder)6 TrieStore (co.rsk.trie.TrieStore)6 SimpleChannelManager (org.ethereum.rpc.Simples.SimpleChannelManager)6 ProgramInvokeFactoryImpl (org.ethereum.vm.program.invoke.ProgramInvokeFactoryImpl)6 RskSystemProperties (co.rsk.config.RskSystemProperties)5 TransactionExecutorFactory (co.rsk.core.TransactionExecutorFactory)5 StateRootsStoreImpl (co.rsk.db.StateRootsStoreImpl)5