Coinbase balance math

+ miner reward for the block
+ gas charging benefit
This commit is contained in:
romanman 2014-06-26 23:47:27 +01:00
parent 7116c41f4c
commit 7cf755f448
4 changed files with 25 additions and 9 deletions

View File

@ -38,6 +38,7 @@ public class Block {
/* A scalar value equal to the mininum limit of gas expenditure per block */ /* A scalar value equal to the mininum limit of gas expenditure per block */
private static long MIN_GAS_LIMIT = BigInteger.valueOf(10).pow(4).longValue(); private static long MIN_GAS_LIMIT = BigInteger.valueOf(10).pow(4).longValue();
public static BigInteger coinbaseReward = BigInteger.valueOf(1500000000000000000L);
private BlockHeader header; private BlockHeader header;

View File

@ -130,6 +130,10 @@ public class Blockchain {
private void addBlock(Block block) { private void addBlock(Block block) {
if(block.isValid()) { if(block.isValid()) {
if (!block.isGenesis())
WorldManager.instance.applyBlock(block);
this.wallet.processBlock(block); this.wallet.processBlock(block);
// In case of the genesis block we don't want to rely on the min gas price // In case of the genesis block we don't want to rely on the min gas price
this.gasPrice = block.isGenesis() ? INITIAL_MIN_GAS_PRICE : block.getMinGasPrice(); this.gasPrice = block.isGenesis() ? INITIAL_MIN_GAS_PRICE : block.getMinGasPrice();
@ -193,8 +197,6 @@ public class Blockchain {
long blockNr = Genesis.NUMBER; long blockNr = Genesis.NUMBER;
for (iterator.seekToFirst(); iterator.hasNext(); iterator.next()) { for (iterator.seekToFirst(); iterator.hasNext(); iterator.next()) {
this.lastBlock = new Block(db.get(ByteUtil.longToBytes(blockNr))); this.lastBlock = new Block(db.get(ByteUtil.longToBytes(blockNr)));
// in case of cold load play the contracts
WorldManager.instance.applyBlock(lastBlock);
logger.debug("Block #{} -> {}", lastBlock.getNumber(), lastBlock.toFlatString()); logger.debug("Block #{} -> {}", lastBlock.getNumber(), lastBlock.toFlatString());
this.addBlock(lastBlock); this.addBlock(lastBlock);
blockNr = lastBlock.getNumber()+1; blockNr = lastBlock.getNumber()+1;

View File

@ -74,13 +74,14 @@ public class WorldManager {
blockChain.loadChain(); blockChain.loadChain();
} }
public void applyTransaction(Transaction tx) { public void applyTransaction(Transaction tx, byte[] coinbase) {
// TODO: refactor the wallet pending transactions to the world manager // TODO: refactor the wallet pending transactions to the world manager
if (blockChain != null) if (blockChain != null)
blockChain.addWalletTransaction(tx); blockChain.addWalletTransaction(tx);
// TODO: what is going on with simple wallet transfer // TODO: what is going on with simple wallet transfer ?
// 1. VALIDATE THE NONCE // 1. VALIDATE THE NONCE
byte[] senderAddress = tx.getSender(); byte[] senderAddress = tx.getSender();
@ -109,6 +110,9 @@ public class WorldManager {
BigInteger gasDebit = tx.getTotalGasValueDebit(); BigInteger gasDebit = tx.getTotalGasValueDebit();
gasDebit = gasDebit.multiply(new BigInteger(tx.getGasPrice())); gasDebit = gasDebit.multiply(new BigInteger(tx.getGasPrice()));
// The coinbase get the gas cost
repository.addBalance(coinbase, gasDebit);
byte[] contractAddress; byte[] contractAddress;
// Contract creation or existing Contract call // Contract creation or existing Contract call
@ -205,7 +209,7 @@ public class WorldManager {
Program program = new Program(initCode, programInvoke); Program program = new Program(initCode, programInvoke);
vm.play(program); vm.play(program);
ProgramResult result = program.getResult(); ProgramResult result = program.getResult();
applyProgramResult(result, gasDebit, trackRepository, senderAddress, tx.getContractAddress()); applyProgramResult(result, gasDebit, trackRepository, senderAddress, tx.getContractAddress(), coinbase);
} else { } else {
@ -227,7 +231,7 @@ public class WorldManager {
vm.play(program); vm.play(program);
ProgramResult result = program.getResult(); ProgramResult result = program.getResult();
applyProgramResult(result, gasDebit, trackRepository, senderAddress, tx.getReceiveAddress()); applyProgramResult(result, gasDebit, trackRepository, senderAddress, tx.getReceiveAddress(), coinbase);
} }
} }
} catch (RuntimeException e) { } catch (RuntimeException e) {
@ -253,7 +257,8 @@ public class WorldManager {
*/ */
private void applyProgramResult(ProgramResult result, BigInteger gasDebit, private void applyProgramResult(ProgramResult result, BigInteger gasDebit,
Repository repository, Repository repository,
byte[] senderAddress, byte[] contractAddress) { byte[] senderAddress, byte[] contractAddress,
byte[] coinbase) {
if (result.getException() != null && if (result.getException() != null &&
result.getException() instanceof Program.OutOfGasException){ result.getException() instanceof Program.OutOfGasException){
@ -279,7 +284,10 @@ public class WorldManager {
stateLogger.info("After contract execution the sender address refunded with gas leftover , \n sender={} \n contract={} \n gas_refund= {}", stateLogger.info("After contract execution the sender address refunded with gas leftover , \n sender={} \n contract={} \n gas_refund= {}",
Hex.toHexString(senderAddress) ,Hex.toHexString(contractAddress), refund); Hex.toHexString(senderAddress) ,Hex.toHexString(contractAddress), refund);
// gas refund
repository.addBalance(senderAddress, refund); repository.addBalance(senderAddress, refund);
repository.addBalance(coinbase, refund.negate());
} }
if (bodyCode != null){ if (bodyCode != null){
@ -300,10 +308,16 @@ public class WorldManager {
public void applyBlock(Block block) { public void applyBlock(Block block) {
// miner reward
if (repository.getAccountState(block.getCoinbase()) == null )
repository.createAccount(block.getCoinbase());
repository.addBalance(block.getCoinbase(), Block.coinbaseReward);
int i = 0; int i = 0;
List<Transaction> txList = block.getTransactionsList(); List<Transaction> txList = block.getTransactionsList();
for (Transaction tx : txList){ for (Transaction tx : txList){
applyTransaction(tx); applyTransaction(tx, block.getCoinbase());
repository.dumpState(block.getNumber(), i, Hex.toHexString(tx.getHash())); repository.dumpState(block.getNumber(), i, Hex.toHexString(tx.getHash()));
++i; ++i;

View File

@ -255,7 +255,6 @@ public class EthereumProtocolHandler extends ChannelInboundHandlerAdapter {
} }
WorldManager.instance.getBlockChain().addBlocks(blockList); WorldManager.instance.getBlockChain().addBlocks(blockList);
WorldManager.instance.applyBlockList(blockList);
if (peerListener != null) peerListener.console(blocksMessage.toString()); if (peerListener != null) peerListener.console(blocksMessage.toString());
} }