Search in sources :

Example 46 with Coin

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

the class BlockGenerator method generatePreMine.

private Map<RskAddress, InitialAddressState> generatePreMine(Map<byte[], BigInteger> alloc) {
    Map<RskAddress, InitialAddressState> premine = new HashMap<>();
    for (byte[] key : alloc.keySet()) {
        AccountState acctState = new AccountState(BigInteger.valueOf(0), new Coin(alloc.get(key)));
        premine.put(new RskAddress(key), new InitialAddressState(acctState, null));
    }
    return premine;
}
Also used : Coin(co.rsk.core.Coin) HashMap(java.util.HashMap) RskAddress(co.rsk.core.RskAddress) InitialAddressState(org.ethereum.core.genesis.InitialAddressState)

Example 47 with Coin

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

the class MinerServerImpl method getNotify.

/**
 * getNotifies determines whether miners should be notified or not. (Used for mining pools).
 *
 * @param block      the block to mine.
 * @param parentHash block's parent hash.
 * @return true if miners should be notified about this new block to mine.
 */
@GuardedBy("lock")
private boolean getNotify(Block block, Keccak256 parentHash) {
    if (!parentHash.equals(latestParentHash)) {
        return true;
    }
    // note: integer divisions might truncate values
    BigInteger percentage = BigInteger.valueOf(100L + RskMiningConstants.NOTIFY_FEES_PERCENTAGE_INCREASE);
    Coin minFeesNotify = latestPaidFeesWithNotify.multiply(percentage).divide(BigInteger.valueOf(100L));
    Coin feesPaidToMiner = block.getFeesPaidToMiner();
    BigDecimal feesPaidToMinerInDollars = new BigDecimal(feesPaidToMiner.asBigInteger()).multiply(gasUnitInDollars);
    return feesPaidToMiner.compareTo(minFeesNotify) > 0 && feesPaidToMinerInDollars.compareTo(minFeesNotifyInDollars) >= 0;
}
Also used : Coin(co.rsk.core.Coin) BigInteger(java.math.BigInteger) BigDecimal(java.math.BigDecimal) GuardedBy(javax.annotation.concurrent.GuardedBy)

Example 48 with Coin

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

the class BlockExecutor method validate.

/**
 * Validate the final state of a block.
 *
 * @param block        A block to validate
 * @param result       A block result (state root, receipts root, etc...)
 * @return true if the block final state is equalBytes to the calculated final state.
 */
public boolean validate(Block block, BlockResult result) {
    if (result == BlockResult.INTERRUPTED_EXECUTION_BLOCK_RESULT) {
        logger.error("Block's execution was interrupted because of an invalid transaction: {} {}.", block.getNumber(), block.getShortHash());
        return false;
    }
    if (!Arrays.equals(result.getStateRoot(), block.getStateRoot())) {
        logger.error("Block's given State Root doesn't match: {} {} {} != {}", block.getNumber(), block.getShortHash(), Hex.toHexString(block.getStateRoot()), Hex.toHexString(result.getStateRoot()));
        return false;
    }
    if (!Arrays.equals(result.getReceiptsRoot(), block.getReceiptsRoot())) {
        logger.error("Block's given Receipt Hash doesn't match: {} {} != {}", block.getNumber(), block.getShortHash(), Hex.toHexString(result.getReceiptsRoot()));
        return false;
    }
    byte[] resultLogsBloom = result.getLogsBloom();
    byte[] blockLogsBloom = block.getLogBloom();
    if (!Arrays.equals(resultLogsBloom, blockLogsBloom)) {
        String resultLogsBloomString = Hex.toHexString(resultLogsBloom);
        String blockLogsBloomString = Hex.toHexString(blockLogsBloom);
        logger.error("Block's given logBloom Hash doesn't match: {} != {} Block {} {}", resultLogsBloomString, blockLogsBloomString, block.getNumber(), block.getShortHash());
        return false;
    }
    if (result.getGasUsed() != block.getGasUsed()) {
        logger.error("Block's given gasUsed doesn't match: {} != {} Block {} {}", block.getGasUsed(), result.getGasUsed(), block.getNumber(), block.getShortHash());
        return false;
    }
    Coin paidFees = result.getPaidFees();
    Coin feesPaidToMiner = block.getFeesPaidToMiner();
    if (!paidFees.equals(feesPaidToMiner)) {
        logger.error("Block's given paidFees doesn't match: {} != {} Block {} {}", feesPaidToMiner, paidFees, block.getNumber(), block.getShortHash());
        return false;
    }
    List<Transaction> executedTransactions = result.getExecutedTransactions();
    List<Transaction> transactionsList = block.getTransactionsList();
    if (!executedTransactions.equals(transactionsList)) {
        logger.error("Block's given txs doesn't match: {} != {} Block {} {}", transactionsList, executedTransactions, block.getNumber(), block.getShortHash());
        return false;
    }
    return true;
}
Also used : Coin(co.rsk.core.Coin)

Example 49 with Coin

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

the class BlockExecutor method execute.

private BlockResult execute(Block block, byte[] stateRoot, boolean discardInvalidTxs, boolean ignoreReadyToExecute) {
    logger.trace("applyBlock: block: [{}] tx.list: [{}]", block.getNumber(), block.getTransactionsList().size());
    Repository initialRepository = repository.getSnapshotTo(stateRoot);
    byte[] lastStateRootHash = initialRepository.getRoot();
    Repository track = initialRepository.startTracking();
    int i = 1;
    long totalGasUsed = 0;
    Coin totalPaidFees = Coin.ZERO;
    List<TransactionReceipt> receipts = new ArrayList<>();
    List<Transaction> executedTransactions = new ArrayList<>();
    int txindex = 0;
    for (Transaction tx : block.getTransactionsList()) {
        logger.trace("apply block: [{}] tx: [{}] ", block.getNumber(), i);
        TransactionExecutor txExecutor = new TransactionExecutor(config, tx, txindex++, block.getCoinbase(), track, blockStore, receiptStore, programInvokeFactory, block, listener, totalGasUsed);
        boolean readyToExecute = txExecutor.init();
        if (!ignoreReadyToExecute && !readyToExecute) {
            if (discardInvalidTxs) {
                logger.warn("block: [{}] discarded tx: [{}]", block.getNumber(), tx.getHash());
                continue;
            } else {
                logger.warn("block: [{}] execution interrupted because of invalid tx: [{}]", block.getNumber(), tx.getHash());
                return BlockResult.INTERRUPTED_EXECUTION_BLOCK_RESULT;
            }
        }
        executedTransactions.add(tx);
        txExecutor.execute();
        txExecutor.go();
        txExecutor.finalization();
        logger.trace("tx executed");
        track.commit();
        logger.trace("track commit");
        long gasUsed = txExecutor.getGasUsed();
        totalGasUsed += gasUsed;
        Coin paidFees = txExecutor.getPaidFees();
        if (paidFees != null) {
            totalPaidFees = totalPaidFees.add(paidFees);
        }
        TransactionReceipt receipt = new TransactionReceipt();
        receipt.setGasUsed(gasUsed);
        receipt.setCumulativeGas(totalGasUsed);
        lastStateRootHash = initialRepository.getRoot();
        receipt.setTxStatus(txExecutor.getReceipt().isSuccessful());
        receipt.setTransaction(tx);
        receipt.setLogInfoList(txExecutor.getVMLogs());
        receipt.setStatus(txExecutor.getReceipt().getStatus());
        logger.trace("block: [{}] executed tx: [{}] state: [{}]", block.getNumber(), tx.getHash(), Hex.toHexString(lastStateRootHash));
        logger.trace("tx[{}].receipt", i);
        i++;
        receipts.add(receipt);
        logger.trace("tx done");
    }
    return new BlockResult(executedTransactions, receipts, lastStateRootHash, totalGasUsed, totalPaidFees);
}
Also used : ArrayList(java.util.ArrayList) Coin(co.rsk.core.Coin)

Example 50 with Coin

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

the class SelectionRule method shouldWeAddThisBlock.

public static boolean shouldWeAddThisBlock(BlockDifficulty blockDifficulty, BlockDifficulty currentDifficulty, Block block, Block currentBlock) {
    int compareDifficulties = blockDifficulty.compareTo(currentDifficulty);
    if (compareDifficulties > 0) {
        return true;
    }
    if (compareDifficulties < 0) {
        return false;
    }
    Coin pfm = currentBlock.getHeader().getPaidFees().multiply(PAID_FEES_MULTIPLIER_CRITERIA);
    // fees over PAID_FEES_MULTIPLIER_CRITERIA times higher
    if (block.getHeader().getPaidFees().compareTo(pfm) > 0) {
        return true;
    }
    Coin blockFeesCriteria = block.getHeader().getPaidFees().multiply(PAID_FEES_MULTIPLIER_CRITERIA);
    // the fees are at least bigger than the half of current block.
    return currentBlock.getHeader().getPaidFees().compareTo(blockFeesCriteria) < 0 && isThisBlockHashSmaller(block.getHash().getBytes(), currentBlock.getHash().getBytes());
}
Also used : Coin(co.rsk.core.Coin)

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