Search in sources :

Example 6 with BlockExecutor

use of co.rsk.core.bc.BlockExecutor in project rskj by rsksmart.

the class RemascTestRunner method start.

public void start() {
    this.blockchain = this.builder.build();
    ((BlockChainImpl) this.blockchain).setNoValidation(true);
    this.addedSiblings = new ArrayList<>();
    List<Block> mainChainBlocks = new ArrayList<>();
    this.blockchain.tryToConnect(this.genesis);
    BlockExecutor blockExecutor = new BlockExecutor(new RskSystemProperties(), blockchain.getRepository(), null, blockchain.getBlockStore(), null);
    for (int i = 0; i <= this.initialHeight; i++) {
        int finalI = i;
        List<SiblingElement> siblingsForCurrentHeight = this.siblingElements.stream().filter(siblingElement -> siblingElement.getHeightToBeIncluded() == finalI).collect(Collectors.toList());
        List<BlockHeader> blockSiblings = new ArrayList<>();
        // Going to add siblings
        BlockDifficulty cummDifficulty = BlockDifficulty.ZERO;
        if (siblingsForCurrentHeight.size() > 0) {
            cummDifficulty = blockchain.getTotalDifficulty();
        }
        for (SiblingElement sibling : siblingsForCurrentHeight) {
            RskAddress siblingCoinbase = TestUtils.randomAddress();
            Block mainchainSiblingParent = mainChainBlocks.get(sibling.getHeight() - 1);
            Block siblingBlock = createBlock(this.genesis, mainchainSiblingParent, PegTestUtils.createHash3(), siblingCoinbase, null, minerFee, Long.valueOf(i), this.txValue, this.txSigningKey, null);
            blockSiblings.add(siblingBlock.getHeader());
            blockchain.getBlockStore().saveBlock(siblingBlock, cummDifficulty.add(siblingBlock.getCumulativeDifficulty()), false);
            this.addedSiblings.add(siblingBlock);
        }
        long txNonce = i;
        RskAddress coinbase = TestUtils.randomAddress();
        Block block = createBlock(this.genesis, this.blockchain.getBestBlock(), PegTestUtils.createHash3(), coinbase, blockSiblings, minerFee, txNonce, this.txValue, this.txSigningKey, null);
        mainChainBlocks.add(block);
        blockExecutor.executeAndFillAll(block, this.blockchain.getBestBlock());
        block.seal();
        ImportResult result = this.blockchain.tryToConnect(block);
        System.out.println(result);
    }
}
Also used : BlockDifficulty(co.rsk.core.BlockDifficulty) BlockChainBuilder(co.rsk.test.builders.BlockChainBuilder) TestUtils(org.ethereum.TestUtils) HashUtil(org.ethereum.crypto.HashUtil) RLP(org.ethereum.util.RLP) RskAddress(co.rsk.core.RskAddress) BlockExecutor(co.rsk.core.bc.BlockExecutor) Coin(co.rsk.core.Coin) PegTestUtils(co.rsk.peg.PegTestUtils) Keccak256(co.rsk.crypto.Keccak256) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) List(java.util.List) BigInteger(java.math.BigInteger) RskSystemProperties(co.rsk.config.RskSystemProperties) BlockChainImpl(co.rsk.core.bc.BlockChainImpl) org.ethereum.core(org.ethereum.core) ECKey(org.ethereum.crypto.ECKey) BlockExecutor(co.rsk.core.bc.BlockExecutor) BlockChainImpl(co.rsk.core.bc.BlockChainImpl) ArrayList(java.util.ArrayList) BlockDifficulty(co.rsk.core.BlockDifficulty) RskAddress(co.rsk.core.RskAddress) RskSystemProperties(co.rsk.config.RskSystemProperties)

Example 7 with BlockExecutor

use of co.rsk.core.bc.BlockExecutor in project rskj by rsksmart.

the class BlockBuilder method build.

public Block build() {
    Block block = blockGenerator.createChildBlock(parent, txs, uncles, difficulty, this.minGasPrice, gasLimit);
    if (blockChain != null) {
        BlockExecutor executor = new BlockExecutor(new RskSystemProperties(), blockChain.getRepository(), null, blockChain.getBlockStore(), blockChain.getListener());
        executor.executeAndFill(block, parent);
    }
    return block;
}
Also used : BlockExecutor(co.rsk.core.bc.BlockExecutor) Block(org.ethereum.core.Block) RskSystemProperties(co.rsk.config.RskSystemProperties)

Example 8 with BlockExecutor

use of co.rsk.core.bc.BlockExecutor in project rskj by rsksmart.

the class RemascProcessMinerFeesTest method processMinersFeesWithNoSiblings.

@Test
public void processMinersFeesWithNoSiblings() 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 remascStorageProvider = getRemascStorageProvider(blockchain);
    assertEquals(Coin.ZERO, remascStorageProvider.getRewardBalance());
    assertEquals(Coin.ZERO, remascStorageProvider.getBurnedBalance());
    assertEquals(0, remascStorageProvider.getSiblings().size());
    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());
    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(blockchain);
    assertEquals(Coin.valueOf(minerFee - blockReward), remascStorageProvider.getRewardBalance());
    assertEquals(Coin.ZERO, remascStorageProvider.getBurnedBalance());
    assertEquals(0, remascStorageProvider.getSiblings().size());
    this.validateFederatorsBalanceIsCorrect(blockchain.getRepository(), federationReward);
}
Also used : BlockExecutor(co.rsk.core.bc.BlockExecutor) RskAddress(co.rsk.core.RskAddress) BlockChainBuilder(co.rsk.test.builders.BlockChainBuilder) Test(org.junit.Test)

Example 9 with BlockExecutor

use of co.rsk.core.bc.BlockExecutor in project rskj by rsksmart.

the class RemascProcessMinerFeesTest method processMinersFeesWithOneSibling.

@Test
public void processMinersFeesWithOneSibling() throws IOException, BlockStoreException {
    BlockChainBuilder builder = new BlockChainBuilder();
    Blockchain blockchain = builder.setTesting(true).setGenesis(genesisBlock).build();
    List<Block> blocks = createSimpleBlocks(genesisBlock, 4);
    Block blockWithOneTxA = RemascTestRunner.createBlock(this.genesisBlock, blocks.get(blocks.size() - 1), PegTestUtils.createHash3(), coinbaseA, null, minerFee, 0, txValue, cowKey);
    Block blockWithOneTxB = RemascTestRunner.createBlock(this.genesisBlock, blocks.get(blocks.size() - 1), PegTestUtils.createHash3(), coinbaseB, null, (long) (minerFee * 1.5), 0, txValue, cowKey);
    blocks.add(blockWithOneTxA);
    Block blockThatIncludesUncle = RemascTestRunner.createBlock(this.genesisBlock, blockWithOneTxA, PegTestUtils.createHash3(), coinbaseC, Lists.newArrayList(blockWithOneTxB.getHeader()), null);
    blocks.add(blockThatIncludesUncle);
    blocks.addAll(createSimpleBlocks(blockThatIncludesUncle, 8));
    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(coinbaseB));
    assertNull(repository.getAccountState(coinbaseC));
    assertNull(repository.getAccountState(remascConfig.getRskLabsAddress()));
    RemascStorageProvider remascStorageProvider = getRemascStorageProvider(blockchain);
    assertEquals(Coin.ZERO, remascStorageProvider.getRewardBalance());
    assertEquals(Coin.ZERO, remascStorageProvider.getBurnedBalance());
    assertEquals(1, remascStorageProvider.getSiblings().size());
    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());
    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(blockchain);
    assertEquals(Coin.valueOf(minerFee - blockReward), remascStorageProvider.getRewardBalance());
    assertEquals(Coin.valueOf(1), remascStorageProvider.getBurnedBalance());
    assertEquals(0, remascStorageProvider.getSiblings().size());
    this.validateFederatorsBalanceIsCorrect(blockchain.getRepository(), federationReward);
}
Also used : BlockExecutor(co.rsk.core.bc.BlockExecutor) RskAddress(co.rsk.core.RskAddress) BlockChainBuilder(co.rsk.test.builders.BlockChainBuilder) Test(org.junit.Test)

Example 10 with BlockExecutor

use of co.rsk.core.bc.BlockExecutor in project rskj by rsksmart.

the class RemascProcessMinerFeesTest method processMinersFeesWithOneSiblingBrokenSelectionRule.

private void processMinersFeesWithOneSiblingBrokenSelectionRule(String reasonForBrokenSelectionRule) throws IOException, BlockStoreException {
    BlockChainBuilder builder = new BlockChainBuilder();
    Blockchain blockchain = builder.setTesting(true).setGenesis(genesisBlock).build();
    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(), coinbaseA, null, minerFee, 0, txValue, cowKey);
        blockWithOneTxB = RemascTestRunner.createBlock(this.genesisBlock, blocks.get(blocks.size() - 1), PegTestUtils.createHash3(), coinbaseB, null, minerFee * 3, 0, txValue, cowKey);
    } else {
        Keccak256 blockWithOneTxBHash = PegTestUtils.createHash3();
        Keccak256 blockWithOneTxAHash = PegTestUtils.createHash3();
        blockWithOneTxA = RemascTestRunner.createBlock(this.genesisBlock, blocks.get(blocks.size() - 1), blockWithOneTxAHash, coinbaseA, null, minerFee, 0, txValue, cowKey);
        blockWithOneTxB = RemascTestRunner.createBlock(this.genesisBlock, blocks.get(blocks.size() - 1), blockWithOneTxBHash, coinbaseB, null, (long) (minerFee * 1.5), 0, txValue, cowKey);
    }
    blocks.add(blockWithOneTxA);
    Block blockThatIncludesUncleC = RemascTestRunner.createBlock(this.genesisBlock, blockWithOneTxA, PegTestUtils.createHash3(), coinbaseC, Lists.newArrayList(blockWithOneTxB.getHeader()), minerFee, 1, txValue, cowKey);
    blocks.add(blockThatIncludesUncleC);
    Block blockWithOneTxD = RemascTestRunner.createBlock(this.genesisBlock, blockThatIncludesUncleC, PegTestUtils.createHash3(), coinbaseD, null, minerFee, 2, txValue, cowKey);
    blocks.add(blockWithOneTxD);
    blocks.addAll(createSimpleBlocks(blockWithOneTxD, 7));
    BlockExecutor blockExecutor = new BlockExecutor(config, blockchain.getRepository(), null, blockchain.getBlockStore(), null);
    for (Block b : blocks) {
        blockExecutor.executeAndFillAll(b, blockchain.getBestBlock());
        blockchain.tryToConnect(b);
    }
    // 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(blockchain.getRepository(), cowRemainingBalance, minerFee * NUMBER_OF_TXS_WITH_FEES, null, this.getAccountsWithExpectedBalance(otherAccountsBalance));
    this.validateRemascsStorageIsCorrect(this.getRemascStorageProvider(blockchain), 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(), TestUtils.randomAddress(), null, null);
    blockExecutor.executeAndFillAll(blockToPayFeesOnHeightFive, blockchain.getBestBlock());
    blockchain.tryToConnect(blockToPayFeesOnHeightFive);
    // -- 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(blockchain.getRepository(), federationReward);
    // TODO review value + 1
    this.validateAccountsCurrentBalanceIsCorrect(blockchain.getRepository(), 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(this.getRemascStorageProvider(blockchain), 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(), TestUtils.randomAddress(), null, null);
    blockExecutor.executeAndFillAll(blockToPayFeesOnHeightSix, blockchain.getBestBlock());
    blockchain.tryToConnect(blockToPayFeesOnHeightSix);
    // -- 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(blockchain.getRepository(), cowRemainingBalance, remascCurrentBalance + 1, rskCurrentBalance, this.getAccountsWithExpectedBalance(otherAccountsBalanceOnHeightSix));
    this.validateFederatorsBalanceIsCorrect(blockchain.getRepository(), federationReward + federationReward2);
    // TODO review + 1
    this.validateRemascsStorageIsCorrect(this.getRemascStorageProvider(blockchain), Coin.valueOf(rewardBalance), Coin.valueOf(burnedBalance + 1), 0L);
}
Also used : Coin(co.rsk.core.Coin) BlockExecutor(co.rsk.core.bc.BlockExecutor) Keccak256(co.rsk.crypto.Keccak256) BlockChainBuilder(co.rsk.test.builders.BlockChainBuilder)

Aggregations

BlockExecutor (co.rsk.core.bc.BlockExecutor)13 BlockChainBuilder (co.rsk.test.builders.BlockChainBuilder)9 Test (org.junit.Test)8 Coin (co.rsk.core.Coin)7 RskAddress (co.rsk.core.RskAddress)6 RskSystemProperties (co.rsk.config.RskSystemProperties)4 Block (org.ethereum.core.Block)3 BlockChainImpl (co.rsk.core.bc.BlockChainImpl)2 Keccak256 (co.rsk.crypto.Keccak256)2 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)1 BlockDifficulty (co.rsk.core.BlockDifficulty)1 SimpleMessageChannel (co.rsk.net.simples.SimpleMessageChannel)1 DownloadingBodiesSyncState (co.rsk.net.sync.DownloadingBodiesSyncState)1 PegTestUtils (co.rsk.peg.PegTestUtils)1 ProofOfWorkRule (co.rsk.validators.ProofOfWorkRule)1 BigInteger (java.math.BigInteger)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 StringTokenizer (java.util.StringTokenizer)1 Collectors (java.util.stream.Collectors)1