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;
}
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;
}
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;
}
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);
}
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());
}
Aggregations