Skip to content

Commit 666a7dd

Browse files
committed
core, eth, rpc: proper gas used. Closes #1417
Added some additional backward compatibility code for old receipts
1 parent 4c30f0f commit 666a7dd

File tree

6 files changed

+13
-9
lines changed

6 files changed

+13
-9
lines changed

core/block_processor.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ func (self *BlockProcessor) ApplyTransaction(coinbase *state.StateObject, stated
8282
usedGas.Add(usedGas, gas)
8383
receipt := types.NewReceipt(statedb.Root().Bytes(), usedGas)
8484
receipt.TxHash = tx.Hash()
85+
receipt.GasUsed = new(big.Int).Set(gas)
8586
if MessageCreatesContract(tx) {
8687
from, _ := tx.From()
8788
receipt.ContractAddress = crypto.CreateAddress(from, tx.Nonce())

core/transaction_util.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func GetReceipt(db common.Database, txHash common.Hash) *types.Receipt {
6464
var receipt types.Receipt
6565
err := rlp.DecodeBytes(data, &receipt)
6666
if err != nil {
67-
glog.V(logger.Error).Infoln("GetReceipt err:", err)
67+
glog.V(logger.Core).Infoln("GetReceipt err:", err)
6868
}
6969
return &receipt
7070
}

core/types/receipt.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ type Receipt struct {
1818
TxHash common.Hash
1919
ContractAddress common.Address
2020
logs state.Logs
21+
GasUsed *big.Int
2122
}
2223

2324
func NewReceipt(root []byte, cumalativeGasUsed *big.Int) *Receipt {
@@ -44,11 +45,12 @@ func (self *Receipt) DecodeRLP(s *rlp.Stream) error {
4445
TxHash common.Hash
4546
ContractAddress common.Address
4647
Logs state.Logs
48+
GasUsed *big.Int
4749
}
4850
if err := s.Decode(&r); err != nil {
4951
return err
5052
}
51-
self.PostState, self.CumulativeGasUsed, self.Bloom, self.TxHash, self.ContractAddress, self.logs = r.PostState, r.CumulativeGasUsed, r.Bloom, r.TxHash, r.ContractAddress, r.Logs
53+
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
5254

5355
return nil
5456
}
@@ -60,7 +62,7 @@ func (self *ReceiptForStorage) EncodeRLP(w io.Writer) error {
6062
for i, log := range self.logs {
6163
storageLogs[i] = (*state.LogForStorage)(log)
6264
}
63-
return rlp.Encode(w, []interface{}{self.PostState, self.CumulativeGasUsed, self.Bloom, self.TxHash, self.ContractAddress, storageLogs})
65+
return rlp.Encode(w, []interface{}{self.PostState, self.CumulativeGasUsed, self.Bloom, self.TxHash, self.ContractAddress, storageLogs, self.GasUsed})
6466
}
6567

6668
func (self *Receipt) RlpEncode() []byte {

eth/gasprice.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,9 @@ func (self *GasPriceOracle) lowestPrice(block *types.Block) *big.Int {
134134

135135
receipts := self.eth.BlockProcessor().GetBlockReceipts(block.Hash())
136136
if len(receipts) > 0 {
137-
gasUsed = receipts[len(receipts)-1].CumulativeGasUsed
137+
if cgu := receipts[len(receipts)-1].CumulativeGasUsed; cgu != nil {
138+
gasUsed = receipts[len(receipts)-1].CumulativeGasUsed
139+
}
138140
}
139141

140142
if new(big.Int).Mul(gasUsed, big.NewInt(100)).Cmp(new(big.Int).Mul(block.GasLimit(),

rpc/api/eth.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,6 @@ func (self *ethApi) GetTransactionReceipt(req *shared.Request) (interface{}, err
615615
v := NewReceiptRes(rec)
616616
v.BlockHash = newHexData(bhash)
617617
v.BlockNumber = newHexNum(bnum)
618-
v.GasUsed = newHexNum(tx.Gas().Bytes())
619618
v.TransactionIndex = newHexNum(txi)
620619
return v, nil
621620
}

rpc/api/parsing.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -421,11 +421,11 @@ func NewReceiptRes(rec *types.Receipt) *ReceiptRes {
421421

422422
var v = new(ReceiptRes)
423423
v.TransactionHash = newHexData(rec.TxHash)
424-
// v.TransactionIndex = newHexNum(input)
425-
// v.BlockNumber = newHexNum(input)
426-
// v.BlockHash = newHexData(input)
424+
if rec.GasUsed != nil {
425+
v.GasUsed = newHexNum(rec.GasUsed.Bytes())
426+
}
427427
v.CumulativeGasUsed = newHexNum(rec.CumulativeGasUsed)
428-
// v.GasUsed = newHexNum(input)
428+
429429
// If the ContractAddress is 20 0x0 bytes, assume it is not a contract creation
430430
if bytes.Compare(rec.ContractAddress.Bytes(), bytes.Repeat([]byte{0}, 20)) != 0 {
431431
v.ContractAddress = newHexData(rec.ContractAddress)

0 commit comments

Comments
 (0)