Search in sources :

Example 86 with Coin

use of co.rsk.core.Coin in project rskj by rsksmart.

the class RemascProcessMinerFeesTest method noPublisherFeeIsPaidWhenThePublisherHasNoSiblings.

@Test
public void noPublisherFeeIsPaidWhenThePublisherHasNoSiblings() 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(genesisBlock, 4);
    Block blockWithOneTxD = RemascTestRunner.createBlock(this.genesisBlock, blocks.get(blocks.size() - 1), PegTestUtils.createHash3(), coinbaseD, null, minerFee, 0, txValue, cowKey);
    blocks.add(blockWithOneTxD);
    Block blockWithOneTxA = RemascTestRunner.createBlock(this.genesisBlock, blockWithOneTxD, PegTestUtils.createHash3(), coinbaseA, null, minerFee, 1, txValue, cowKey);
    Block blockWithOneTxB = RemascTestRunner.createBlock(this.genesisBlock, blockWithOneTxD, PegTestUtils.createHash3(), coinbaseB, null, minerFee * 3, 1, txValue, cowKey);
    blocks.add(blockWithOneTxA);
    Block blockThatIncludesUncleC = RemascTestRunner.createBlock(this.genesisBlock, blockWithOneTxA, PegTestUtils.createHash3(), coinbaseC, Lists.newArrayList(blockWithOneTxB.getHeader()), minerFee, 2, txValue, cowKey);
    blocks.add(blockThatIncludesUncleC);
    blocks.addAll(createSimpleBlocks(blockThatIncludesUncleC, 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 4
    Block blockToPayFeesOnHeightFour = RemascTestRunner.createBlock(this.genesisBlock, blocks.get(blocks.size() - 1), PegTestUtils.createHash3(), TestUtils.randomAddress(), null, minerFee, 0, txValue, cowKey);
    blockExecutor.executeAndFillAll(blockToPayFeesOnHeightFour, blockchain.getBestBlock());
    blockchain.tryToConnect(blockToPayFeesOnHeightFour);
    Repository repository = blockchain.getRepository();
    // -- 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(this.getRemascStorageProvider(blockchain), expectedRewardBalance, Coin.ZERO, 1L);
    this.validateFederatorsBalanceIsCorrect(blockchain.getRepository(), federationReward);
}
Also used : Coin(co.rsk.core.Coin) BlockExecutor(co.rsk.core.bc.BlockExecutor) BlockChainBuilder(co.rsk.test.builders.BlockChainBuilder) Test(org.junit.Test)

Example 87 with Coin

use of co.rsk.core.Coin in project rskj by rsksmart.

the class RemascTestRunner method createBlock.

public static Block createBlock(Block genesis, Block parentBlock, Keccak256 blockHash, RskAddress coinbase, List<BlockHeader> uncles, Long difficulty, Transaction... txsToInlcude) {
    List<Transaction> txs = new ArrayList<>();
    if (txsToInlcude != null) {
        for (Transaction tx : txsToInlcude) {
            txs.add(tx);
        }
    }
    Transaction remascTx = new RemascTransaction(parentBlock.getNumber() + 1);
    txs.add(remascTx);
    long difficultyAsLong = difficulty == null ? parentBlock.getDifficulty().asBigInteger().longValue() : difficulty;
    if (difficultyAsLong == 0)
        difficultyAsLong = 1;
    byte[] diffBytes = BigInteger.valueOf(difficultyAsLong).toByteArray();
    Coin paidFees = Coin.ZERO;
    for (Transaction tx : txs) {
        BigInteger gasLimit = new BigInteger(1, tx.getGasLimit());
        Coin gasPrice = tx.getGasPrice();
        paidFees = paidFees.add(gasPrice.multiply(gasLimit));
    }
    Block block = new Block(// parent hash
    parentBlock.getHash().getBytes(), // uncle hash
    EMPTY_LIST_HASH, // coinbase
    coinbase.getBytes(), // logs bloom
    new Bloom().getData(), // difficulty
    diffBytes, parentBlock.getNumber() + 1, parentBlock.getGasLimit(), parentBlock.getGasUsed(), parentBlock.getTimestamp(), // extraData
    new byte[0], // mixHash
    new byte[0], // provisory nonce
    BigInteger.ZERO.toByteArray(), // receipts root
    HashUtil.EMPTY_TRIE_HASH, // transaction root
    BlockChainImpl.calcTxTrie(txs), // EMPTY_TRIE_HASH,   // state root
    genesis.getStateRoot(), // transaction list
    txs, // uncle list
    uncles, BigInteger.TEN.toByteArray(), paidFees) {

        private BlockHeader harcodedHashHeader;

        @Override
        public BlockHeader getHeader() {
            if (harcodedHashHeader == null) {
                harcodedHashHeader = new BlockHeader(super.getHeader().getEncoded(), false) {

                    @Override
                    public Keccak256 getHash() {
                        return blockHash;
                    }
                };
            }
            return harcodedHashHeader;
        }

        @Override
        public Keccak256 getHash() {
            return blockHash;
        }

        @Override
        public void flushRLP() {
            if (harcodedHashHeader != null)
                super.getHeader().setPaidFees(harcodedHashHeader.getPaidFees());
            super.flushRLP();
            harcodedHashHeader = null;
        }
    };
    return block;
}
Also used : ArrayList(java.util.ArrayList) Keccak256(co.rsk.crypto.Keccak256) Coin(co.rsk.core.Coin) BigInteger(java.math.BigInteger)

Example 88 with Coin

use of co.rsk.core.Coin in project rskj by rsksmart.

the class BlockChainBuilder method ofSize.

public static Blockchain ofSize(int size, boolean mining, List<Account> accounts, List<Coin> balances, boolean withoutCleaner) {
    BlockChainBuilder builder = new BlockChainBuilder();
    BlockChainImpl blockChain = builder.build(withoutCleaner);
    BlockGenerator blockGenerator = new BlockGenerator();
    Block genesis = blockGenerator.getGenesisBlock();
    if (accounts != null)
        for (int k = 0; k < accounts.size(); k++) {
            Account account = accounts.get(k);
            Coin balance = balances.get(k);
            blockChain.getRepository().createAccount(account.getAddress());
            blockChain.getRepository().addBalance(account.getAddress(), balance);
        }
    genesis.setStateRoot(blockChain.getRepository().getRoot());
    genesis.flushRLP();
    Assert.assertEquals(ImportResult.IMPORTED_BEST, blockChain.tryToConnect(genesis));
    if (size > 0) {
        List<Block> blocks = mining ? blockGenerator.getMinedBlockChain(genesis, size) : blockGenerator.getBlockChain(genesis, size);
        for (Block block : blocks) Assert.assertEquals(ImportResult.IMPORTED_BEST, blockChain.tryToConnect(block));
    }
    return blockChain;
}
Also used : Coin(co.rsk.core.Coin) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator)

Example 89 with Coin

use of co.rsk.core.Coin in project rskj by rsksmart.

the class WorldDslProcessor method processAccountNewCommand.

private void processAccountNewCommand(DslCommand cmd) {
    AccountBuilder builder = new AccountBuilder(world);
    String name = cmd.getArgument(0);
    builder.name(name);
    if (cmd.getArity() > 1)
        builder.balance(new Coin(new BigInteger(cmd.getArgument(1))));
    Account account = builder.build();
    world.saveAccount(name, account);
}
Also used : Coin(co.rsk.core.Coin) Account(org.ethereum.core.Account) BigInteger(java.math.BigInteger) AccountBuilder(co.rsk.test.builders.AccountBuilder)

Example 90 with Coin

use of co.rsk.core.Coin in project rskj by rsksmart.

the class WorldDslProcessor method processAssertBalanceCommand.

private void processAssertBalanceCommand(DslCommand cmd) throws DslProcessorException {
    String accountName = cmd.getArgument(0);
    Coin expected = new Coin(new BigInteger(cmd.getArgument(1)));
    RskAddress accountAddress;
    Account account = world.getAccountByName(accountName);
    if (account != null)
        accountAddress = account.getAddress();
    else {
        Transaction tx = world.getTransactionByName(accountName);
        if (tx != null)
            accountAddress = tx.getContractAddress();
        else
            accountAddress = new RskAddress(accountName);
    }
    Coin accountBalance = world.getRepository().getBalance(accountAddress);
    if (expected.equals(accountBalance))
        return;
    throw new DslProcessorException(String.format("Expected account '%s' with balance '%s', but got '%s'", accountName, expected, accountBalance));
}
Also used : Coin(co.rsk.core.Coin) Account(org.ethereum.core.Account) Transaction(org.ethereum.core.Transaction) RskAddress(co.rsk.core.RskAddress) BigInteger(java.math.BigInteger)

Aggregations

Coin (co.rsk.core.Coin)95 Test (org.junit.Test)46 RskAddress (co.rsk.core.RskAddress)37 BigInteger (java.math.BigInteger)32 Repository (org.ethereum.core.Repository)23 Transaction (org.ethereum.core.Transaction)23 Program (org.ethereum.vm.program.Program)12 AccountState (org.ethereum.core.AccountState)10 ArrayList (java.util.ArrayList)9 Ignore (org.junit.Ignore)9 ProgramInvokeMockImpl (org.ethereum.vm.program.invoke.ProgramInvokeMockImpl)8 Account (org.ethereum.core.Account)7 BlockExecutor (co.rsk.core.bc.BlockExecutor)6 BlockChainBuilder (co.rsk.test.builders.BlockChainBuilder)6 Keccak256 (co.rsk.crypto.Keccak256)5 Block (org.ethereum.core.Block)5 DataWord (org.ethereum.vm.DataWord)5 ProgramInvoke (org.ethereum.vm.program.invoke.ProgramInvoke)5 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)4 RskSystemProperties (co.rsk.config.RskSystemProperties)4