From 666a7dda369e9a30715f560c8f72b81735a347fc Mon Sep 17 00:00:00 2001 From: Jeffrey Wilcke Date: Mon, 6 Jul 2015 20:59:12 +0200 Subject: [PATCH] core, eth, rpc: proper gas used. Closes #1417 Added some additional backward compatibility code for old receipts --- core/block_processor.go | 1 + core/transaction_util.go | 2 +- core/types/receipt.go | 6 ++++-- eth/gasprice.go | 4 +++- rpc/api/eth.go | 1 - rpc/api/parsing.go | 8 ++++---- 6 files changed, 13 insertions(+), 9 deletions(-) diff --git a/core/block_processor.go b/core/block_processor.go index 9a7478381..362036445 100644 --- a/core/block_processor.go +++ b/core/block_processor.go @@ -82,6 +82,7 @@ func (self *BlockProcessor) ApplyTransaction(coinbase *state.StateObject, stated usedGas.Add(usedGas, gas) receipt := types.NewReceipt(statedb.Root().Bytes(), usedGas) receipt.TxHash = tx.Hash() + receipt.GasUsed = new(big.Int).Set(gas) if MessageCreatesContract(tx) { from, _ := tx.From() receipt.ContractAddress = crypto.CreateAddress(from, tx.Nonce()) diff --git a/core/transaction_util.go b/core/transaction_util.go index cb5d6c7f7..7d432848a 100644 --- a/core/transaction_util.go +++ b/core/transaction_util.go @@ -64,7 +64,7 @@ func GetReceipt(db common.Database, txHash common.Hash) *types.Receipt { var receipt types.Receipt err := rlp.DecodeBytes(data, &receipt) if err != nil { - glog.V(logger.Error).Infoln("GetReceipt err:", err) + glog.V(logger.Core).Infoln("GetReceipt err:", err) } return &receipt } diff --git a/core/types/receipt.go b/core/types/receipt.go index ab52c6e60..aff29f565 100644 --- a/core/types/receipt.go +++ b/core/types/receipt.go @@ -18,6 +18,7 @@ type Receipt struct { TxHash common.Hash ContractAddress common.Address logs state.Logs + GasUsed *big.Int } func NewReceipt(root []byte, cumalativeGasUsed *big.Int) *Receipt { @@ -44,11 +45,12 @@ func (self *Receipt) DecodeRLP(s *rlp.Stream) error { TxHash common.Hash ContractAddress common.Address Logs state.Logs + GasUsed *big.Int } if err := s.Decode(&r); err != nil { return err } - self.PostState, self.CumulativeGasUsed, self.Bloom, self.TxHash, self.ContractAddress, self.logs = r.PostState, r.CumulativeGasUsed, r.Bloom, r.TxHash, r.ContractAddress, r.Logs + self.PostState, self.CumulativeGasUsed, self.Bloom, self.TxHash, self.ContractAddress, self.logs, self.GasUsed = r.PostState, r.CumulativeGasUsed, r.Bloom, r.TxHash, r.ContractAddress, r.Logs, r.GasUsed return nil } @@ -60,7 +62,7 @@ func (self *ReceiptForStorage) EncodeRLP(w io.Writer) error { for i, log := range self.logs { storageLogs[i] = (*state.LogForStorage)(log) } - return rlp.Encode(w, []interface{}{self.PostState, self.CumulativeGasUsed, self.Bloom, self.TxHash, self.ContractAddress, storageLogs}) + return rlp.Encode(w, []interface{}{self.PostState, self.CumulativeGasUsed, self.Bloom, self.TxHash, self.ContractAddress, storageLogs, self.GasUsed}) } func (self *Receipt) RlpEncode() []byte { diff --git a/eth/gasprice.go b/eth/gasprice.go index 09ef8cded..4aa2ad295 100644 --- a/eth/gasprice.go +++ b/eth/gasprice.go @@ -134,7 +134,9 @@ func (self *GasPriceOracle) lowestPrice(block *types.Block) *big.Int { receipts := self.eth.BlockProcessor().GetBlockReceipts(block.Hash()) if len(receipts) > 0 { - gasUsed = receipts[len(receipts)-1].CumulativeGasUsed + if cgu := receipts[len(receipts)-1].CumulativeGasUsed; cgu != nil { + gasUsed = receipts[len(receipts)-1].CumulativeGasUsed + } } if new(big.Int).Mul(gasUsed, big.NewInt(100)).Cmp(new(big.Int).Mul(block.GasLimit(), diff --git a/rpc/api/eth.go b/rpc/api/eth.go index 6d759a087..944e96070 100644 --- a/rpc/api/eth.go +++ b/rpc/api/eth.go @@ -615,7 +615,6 @@ func (self *ethApi) GetTransactionReceipt(req *shared.Request) (interface{}, err v := NewReceiptRes(rec) v.BlockHash = newHexData(bhash) v.BlockNumber = newHexNum(bnum) - v.GasUsed = newHexNum(tx.Gas().Bytes()) v.TransactionIndex = newHexNum(txi) return v, nil } diff --git a/rpc/api/parsing.go b/rpc/api/parsing.go index 8e25ffffb..493d196e0 100644 --- a/rpc/api/parsing.go +++ b/rpc/api/parsing.go @@ -421,11 +421,11 @@ func NewReceiptRes(rec *types.Receipt) *ReceiptRes { var v = new(ReceiptRes) v.TransactionHash = newHexData(rec.TxHash) - // v.TransactionIndex = newHexNum(input) - // v.BlockNumber = newHexNum(input) - // v.BlockHash = newHexData(input) + if rec.GasUsed != nil { + v.GasUsed = newHexNum(rec.GasUsed.Bytes()) + } v.CumulativeGasUsed = newHexNum(rec.CumulativeGasUsed) - // v.GasUsed = newHexNum(input) + // If the ContractAddress is 20 0x0 bytes, assume it is not a contract creation if bytes.Compare(rec.ContractAddress.Bytes(), bytes.Repeat([]byte{0}, 20)) != 0 { v.ContractAddress = newHexData(rec.ContractAddress)