Skip to content

Commit 7fb8969

Browse files
bmperreafjl
authored andcommitted
core/types: add block location fields to receipt (#17662)
Solves #15210 without changing consensus, in a backwards compatible way, by adding tx inclusion information to the Receipt struct.
1 parent 42e2c58 commit 7fb8969

8 files changed

Lines changed: 59 additions & 2 deletions

File tree

‎core/blockchain.go‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -810,6 +810,11 @@ func SetReceiptsData(config *params.ChainConfig, block *types.Block, receipts ty
810810
// The transaction hash can be retrieved from the transaction itself
811811
receipts[j].TxHash = transactions[j].Hash()
812812

813+
// block location fields
814+
receipts[j].BlockHash = block.Hash()
815+
receipts[j].BlockNumber = block.Number()
816+
receipts[j].TransactionIndex = uint(j)
817+
813818
// The contract address can be derived from the transaction itself
814819
if transactions[j].To() == nil {
815820
// Deriving the signer is expensive, only do if it's actually needed

‎core/rawdb/accessors_chain.go‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,9 @@ func ReadReceipts(db ethdb.Reader, hash common.Hash, number uint64) types.Receip
325325
logIndex += 1
326326
}
327327
receipts[i] = (*types.Receipt)(receipt)
328+
receipts[i].BlockHash = hash
329+
receipts[i].BlockNumber = big.NewInt(0).SetUint64(number)
330+
receipts[i].TransactionIndex = uint(i)
328331
}
329332
return receipts
330333
}

‎core/state/statedb.go‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,16 @@ func (self *StateDB) GetNonce(addr common.Address) uint64 {
237237
return 0
238238
}
239239

240+
// TxIndex returns the current transaction index set by Prepare.
241+
func (self *StateDB) TxIndex() int {
242+
return self.txIndex
243+
}
244+
245+
// BlockHash returns the current block hash set by Prepare.
246+
func (self *StateDB) BlockHash() common.Hash {
247+
return self.bhash
248+
}
249+
240250
func (self *StateDB) GetCode(addr common.Address) []byte {
241251
stateObject := self.getStateObject(addr)
242252
if stateObject != nil {

‎core/state_processor.go‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,9 @@ func ApplyTransaction(config *params.ChainConfig, bc ChainContext, author *commo
121121
// Set the receipt logs and create a bloom for filtering
122122
receipt.Logs = statedb.GetLogs(tx.Hash())
123123
receipt.Bloom = types.CreateBloom(types.Receipts{receipt})
124+
receipt.BlockHash = statedb.BlockHash()
125+
receipt.BlockNumber = header.Number
126+
receipt.TransactionIndex = uint(statedb.TxIndex())
124127

125128
return receipt, gas, err
126129
}

‎core/types/gen_receipt_json.go‎

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎core/types/gen_tx_json.go‎

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎core/types/receipt.go‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"bytes"
2121
"fmt"
2222
"io"
23+
"math/big"
2324
"unsafe"
2425

2526
"github.com/ethereum/go-ethereum/common"
@@ -44,24 +45,33 @@ const (
4445

4546
// Receipt represents the results of a transaction.
4647
type Receipt struct {
47-
// Consensus fields
48+
// Consensus fields: These fields are defined by the Yellow Paper
4849
PostState []byte `json:"root"`
4950
Status uint64 `json:"status"`
5051
CumulativeGasUsed uint64 `json:"cumulativeGasUsed" gencodec:"required"`
5152
Bloom Bloom `json:"logsBloom" gencodec:"required"`
5253
Logs []*Log `json:"logs" gencodec:"required"`
5354

54-
// Implementation fields (don't reorder!)
55+
// Implementation fields: These fields are added by geth when processing a transaction.
56+
// They are stored in the chain database.
5557
TxHash common.Hash `json:"transactionHash" gencodec:"required"`
5658
ContractAddress common.Address `json:"contractAddress"`
5759
GasUsed uint64 `json:"gasUsed" gencodec:"required"`
60+
61+
// Inclusion information: These fields provide information about the inclusion of the
62+
// transaction corresponding to this receipt.
63+
BlockHash common.Hash `json:"blockHash,omitempty"`
64+
BlockNumber *big.Int `json:"blockNumber,omitempty"`
65+
TransactionIndex uint `json:"transactionIndex"`
5866
}
5967

6068
type receiptMarshaling struct {
6169
PostState hexutil.Bytes
6270
Status hexutil.Uint64
6371
CumulativeGasUsed hexutil.Uint64
6472
GasUsed hexutil.Uint64
73+
BlockNumber *hexutil.Big
74+
TransactionIndex hexutil.Uint
6575
}
6676

6777
// receiptRLP is the consensus encoding of a receipt.

‎miner/worker.go‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,11 @@ func (w *worker) resultLoop() {
566566
logs []*types.Log
567567
)
568568
for i, receipt := range task.receipts {
569+
// add block location fields
570+
receipt.BlockHash = hash
571+
receipt.BlockNumber = block.Number()
572+
receipt.TransactionIndex = uint(i)
573+
569574
receipts[i] = new(types.Receipt)
570575
*receipts[i] = *receipt
571576
// Update the block hash in all logs since it is now available and not when the

0 commit comments

Comments
 (0)