فهرست منبع

core: remove superfluous big.Int allocations

With blocks now being immutable, use big.Int values from
accessor functions instead of copying their results.
Felix Lange 10 سال پیش
والد
کامیت
fccc7d71eb
3فایلهای تغییر یافته به همراه32 افزوده شده و 61 حذف شده
  1. 16 15
      core/block_processor.go
  2. 3 21
      core/chain_manager.go
  3. 13 25
      core/state_transition.go

+ 16 - 15
core/block_processor.go

@@ -81,9 +81,8 @@ func (self *BlockProcessor) ApplyTransaction(coinbase *state.StateObject, stated
 	// Update the state with pending changes
 	// Update the state with pending changes
 	statedb.Update()
 	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())
 	logs := statedb.GetLogs(tx.Hash())
 	receipt.SetLogs(logs)
 	receipt.SetLogs(logs)
 	receipt.Bloom = types.CreateBloom(types.Receipts{receipt})
 	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
 	return state.Logs(), nil
 }
 }
 
 
+var (
+	big8  = big.NewInt(8)
+	big32 = big.NewInt(32)
+)
+
 // AccumulateRewards credits the coinbase of the given block with the
 // AccumulateRewards credits the coinbase of the given block with the
 // mining reward. The total reward consists of the static block reward
 // 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) {
 func AccumulateRewards(statedb *state.StateDB, header *types.Header, uncles []*types.Header) {
 	reward := new(big.Int).Set(BlockReward)
 	reward := new(big.Int).Set(BlockReward)
-
+	r := new(big.Int)
 	for _, uncle := range uncles {
 	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))
-
+		r.Add(uncle.Number, big8)
+		r.Sub(r, header.Number)
+		r.Mul(r, BlockReward)
+		r.Div(r, big8)
 		statedb.AddBalance(uncle.Coinbase, r)
 		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)
 	statedb.AddBalance(header.Coinbase, reward)
 }
 }
 
 

+ 3 - 21
core/chain_manager.go

@@ -60,7 +60,9 @@ func CalcTD(block, parent *types.Block) *big.Int {
 	if parent == nil {
 	if parent == nil {
 		return block.Difficulty()
 		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.
 // 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)
 	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() {
 func (bc *ChainManager) Stop() {
 	close(bc.quit)
 	close(bc.quit)
 	atomic.StoreInt32(&bc.procInterrupt, 1)
 	atomic.StoreInt32(&bc.procInterrupt, 1)

+ 13 - 25
core/state_transition.go

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