Skip to content

Commit

Permalink
core/types: add block location fields to receipt (#17662)
Browse files Browse the repository at this point in the history
Solves #15210 without changing consensus, in a backwards compatible way,
by adding tx inclusion information to the Receipt struct.
  • Loading branch information
bmperrea authored and fjl committed Mar 27, 2019
1 parent 42e2c58 commit 7fb8969
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 2 deletions.
5 changes: 5 additions & 0 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -810,6 +810,11 @@ func SetReceiptsData(config *params.ChainConfig, block *types.Block, receipts ty
// The transaction hash can be retrieved from the transaction itself
receipts[j].TxHash = transactions[j].Hash()

// block location fields
receipts[j].BlockHash = block.Hash()
receipts[j].BlockNumber = block.Number()
receipts[j].TransactionIndex = uint(j)

// The contract address can be derived from the transaction itself
if transactions[j].To() == nil {
// Deriving the signer is expensive, only do if it's actually needed
Expand Down
3 changes: 3 additions & 0 deletions core/rawdb/accessors_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,9 @@ func ReadReceipts(db ethdb.Reader, hash common.Hash, number uint64) types.Receip
logIndex += 1
}
receipts[i] = (*types.Receipt)(receipt)
receipts[i].BlockHash = hash
receipts[i].BlockNumber = big.NewInt(0).SetUint64(number)
receipts[i].TransactionIndex = uint(i)
}
return receipts
}
Expand Down
10 changes: 10 additions & 0 deletions core/state/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,16 @@ func (self *StateDB) GetNonce(addr common.Address) uint64 {
return 0
}

// TxIndex returns the current transaction index set by Prepare.
func (self *StateDB) TxIndex() int {
return self.txIndex
}

// BlockHash returns the current block hash set by Prepare.
func (self *StateDB) BlockHash() common.Hash {
return self.bhash
}

func (self *StateDB) GetCode(addr common.Address) []byte {
stateObject := self.getStateObject(addr)
if stateObject != nil {
Expand Down
3 changes: 3 additions & 0 deletions core/state_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ func ApplyTransaction(config *params.ChainConfig, bc ChainContext, author *commo
// Set the receipt logs and create a bloom for filtering
receipt.Logs = statedb.GetLogs(tx.Hash())
receipt.Bloom = types.CreateBloom(types.Receipts{receipt})
receipt.BlockHash = statedb.BlockHash()
receipt.BlockNumber = header.Number
receipt.TransactionIndex = uint(statedb.TxIndex())

return receipt, gas, err
}
19 changes: 19 additions & 0 deletions core/types/gen_receipt_json.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions core/types/gen_tx_json.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 12 additions & 2 deletions core/types/receipt.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"bytes"
"fmt"
"io"
"math/big"
"unsafe"

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

// Receipt represents the results of a transaction.
type Receipt struct {
// Consensus fields
// Consensus fields: These fields are defined by the Yellow Paper
PostState []byte `json:"root"`
Status uint64 `json:"status"`
CumulativeGasUsed uint64 `json:"cumulativeGasUsed" gencodec:"required"`
Bloom Bloom `json:"logsBloom" gencodec:"required"`
Logs []*Log `json:"logs" gencodec:"required"`

// Implementation fields (don't reorder!)
// Implementation fields: These fields are added by geth when processing a transaction.
// They are stored in the chain database.
TxHash common.Hash `json:"transactionHash" gencodec:"required"`
ContractAddress common.Address `json:"contractAddress"`
GasUsed uint64 `json:"gasUsed" gencodec:"required"`

// Inclusion information: These fields provide information about the inclusion of the
// transaction corresponding to this receipt.
BlockHash common.Hash `json:"blockHash,omitempty"`
BlockNumber *big.Int `json:"blockNumber,omitempty"`
TransactionIndex uint `json:"transactionIndex"`
}

type receiptMarshaling struct {
PostState hexutil.Bytes
Status hexutil.Uint64
CumulativeGasUsed hexutil.Uint64
GasUsed hexutil.Uint64
BlockNumber *hexutil.Big
TransactionIndex hexutil.Uint
}

// receiptRLP is the consensus encoding of a receipt.
Expand Down
5 changes: 5 additions & 0 deletions miner/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,11 @@ func (w *worker) resultLoop() {
logs []*types.Log
)
for i, receipt := range task.receipts {
// add block location fields
receipt.BlockHash = hash
receipt.BlockNumber = block.Number()
receipt.TransactionIndex = uint(i)

receipts[i] = new(types.Receipt)
*receipts[i] = *receipt
// Update the block hash in all logs since it is now available and not when the
Expand Down

0 comments on commit 7fb8969

Please sign in to comment.