core: remove superfluous big.Int allocations

With blocks now being immutable, use big.Int values from
accessor functions instead of copying their results.
pull/1357/head
Felix Lange 10 years ago committed by Jeffrey Wilcke
parent d0bb90c69e
commit fccc7d71eb
  1. 31
      core/block_processor.go
  2. 24
      core/chain_manager.go
  3. 38
      core/state_transition.go

@ -81,9 +81,8 @@ func (self *BlockProcessor) ApplyTransaction(coinbase *state.StateObject, stated
// Update the state with pending changes
statedb.Update()
cumulative := new(big.Int).Set(usedGas.Add(usedGas, gas))
receipt := types.NewReceipt(statedb.Root().Bytes(), cumulative)
usedGas.Add(usedGas, gas)
receipt := types.NewReceipt(statedb.Root().Bytes(), usedGas)
logs := statedb.GetLogs(tx.Hash())
receipt.SetLogs(logs)
receipt.Bloom = types.CreateBloom(types.Receipts{receipt})
@ -260,26 +259,28 @@ func (sm *BlockProcessor) processWithParent(block, parent *types.Block) (logs st
return state.Logs(), nil
}
var (
big8 = big.NewInt(8)
big32 = big.NewInt(32)
)
// AccumulateRewards credits the coinbase of the given block with the
// mining reward. The total reward consists of the static block reward
// and rewards for included uncles.
// and rewards for included uncles. The coinbase of each uncle block is
// also rewarded.
func AccumulateRewards(statedb *state.StateDB, header *types.Header, uncles []*types.Header) {
reward := new(big.Int).Set(BlockReward)
for _, uncle := range uncles {
num := new(big.Int).Add(big.NewInt(8), uncle.Number)
num.Sub(num, header.Number)
r := new(big.Int)
r.Mul(BlockReward, num)
r.Div(r, big.NewInt(8))
for _, uncle := range uncles {
r.Add(uncle.Number, big8)
r.Sub(r, header.Number)
r.Mul(r, BlockReward)
r.Div(r, big8)
statedb.AddBalance(uncle.Coinbase, r)
reward.Add(reward, new(big.Int).Div(BlockReward, big.NewInt(32)))
r.Div(BlockReward, big32)
reward.Add(reward, r)
}
// Get the account associated with the coinbase
statedb.AddBalance(header.Coinbase, reward)
}

@ -60,7 +60,9 @@ func CalcTD(block, parent *types.Block) *big.Int {
if parent == nil {
return block.Difficulty()
}
return new(big.Int).Add(parent.Td, block.Header().Difficulty)
d := block.Difficulty()
d.Add(d, parent.Td)
return d
}
// CalcGasLimit computes the gas limit of the next block after parent.
@ -465,26 +467,6 @@ func (bc *ChainManager) setTotalDifficulty(td *big.Int) {
bc.td = new(big.Int).Set(td)
}
func (self *ChainManager) CalcTotalDiff(block *types.Block) (*big.Int, error) {
parent := self.GetBlock(block.Header().ParentHash)
if parent == nil {
return nil, fmt.Errorf("Unable to calculate total diff without known parent %x", block.Header().ParentHash)
}
parentTd := parent.Td
uncleDiff := new(big.Int)
for _, uncle := range block.Uncles() {
uncleDiff = uncleDiff.Add(uncleDiff, uncle.Difficulty)
}
td := new(big.Int)
td = td.Add(parentTd, uncleDiff)
td = td.Add(td, block.Header().Difficulty)
return td, nil
}
func (bc *ChainManager) Stop() {
close(bc.quit)
atomic.StoreInt32(&bc.procInterrupt, 1)

@ -13,10 +13,6 @@ import (
"github.com/ethereum/go-ethereum/params"
)
const tryJit = false
var ()
/*
* The State transitioning model
*
@ -69,10 +65,6 @@ func MessageCreatesContract(msg Message) bool {
return msg.To() == nil
}
func MessageGasValue(msg Message) *big.Int {
return new(big.Int).Mul(msg.Gas(), msg.GasPrice())
}
// IntrinsicGas computes the 'intrisic gas' for a message
// with the given data.
func IntrinsicGas(data []byte) *big.Int {
@ -104,7 +96,7 @@ func NewStateTransition(env vm.Environment, msg Message, coinbase *state.StateOb
env: env,
msg: msg,
gas: new(big.Int),
gasPrice: new(big.Int).Set(msg.GasPrice()),
gasPrice: msg.GasPrice(),
initialGas: new(big.Int),
value: msg.Value(),
data: msg.Data(),
@ -148,26 +140,22 @@ func (self *StateTransition) AddGas(amount *big.Int) {
}
func (self *StateTransition) BuyGas() error {
var err error
mgas := self.msg.Gas()
mgval := new(big.Int).Mul(mgas, self.gasPrice)
sender, err := self.From()
if err != nil {
return err
}
if sender.Balance().Cmp(MessageGasValue(self.msg)) < 0 {
return fmt.Errorf("insufficient ETH for gas (%x). Req %v, has %v", sender.Address().Bytes()[:4], MessageGasValue(self.msg), sender.Balance())
if sender.Balance().Cmp(mgval) < 0 {
return fmt.Errorf("insufficient ETH for gas (%x). Req %v, has %v", sender.Address().Bytes()[:4], mgval, sender.Balance())
}
coinbase := self.Coinbase()
err = coinbase.SubGas(self.msg.Gas(), self.msg.GasPrice())
if err != nil {
if err = self.Coinbase().SubGas(mgas, self.gasPrice); err != nil {
return err
}
self.AddGas(self.msg.Gas())
self.initialGas.Set(self.msg.Gas())
sender.SubBalance(MessageGasValue(self.msg))
self.AddGas(mgas)
self.initialGas.Set(mgas)
sender.SubBalance(mgval)
return nil
}
@ -245,15 +233,15 @@ func (self *StateTransition) refundGas() {
coinbase := self.Coinbase()
sender, _ := self.From() // err already checked
// Return remaining gas
remaining := new(big.Int).Mul(self.gas, self.msg.GasPrice())
remaining := new(big.Int).Mul(self.gas, self.gasPrice)
sender.AddBalance(remaining)
uhalf := new(big.Int).Div(self.gasUsed(), common.Big2)
uhalf := remaining.Div(self.gasUsed(), common.Big2)
refund := common.BigMin(uhalf, self.state.Refunds())
self.gas.Add(self.gas, refund)
self.state.AddBalance(sender.Address(), refund.Mul(refund, self.msg.GasPrice()))
self.state.AddBalance(sender.Address(), refund.Mul(refund, self.gasPrice))
coinbase.AddGas(self.gas, self.msg.GasPrice())
coinbase.AddGas(self.gas, self.gasPrice)
}
func (self *StateTransition) gasUsed() *big.Int {

Loading…
Cancel
Save