Skip to content

Commit bf4f915

Browse files
authored
Merge pull request #520 from gzliudan/receipt-fields
types, core, miner: add block location fields to receipt
2 parents 3281766 + 50847c9 commit bf4f915

File tree

8 files changed

+75
-9
lines changed

8 files changed

+75
-9
lines changed

core/blockchain.go

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

1052+
// block location fields
1053+
receipts[j].BlockHash = block.Hash()
1054+
receipts[j].BlockNumber = block.Number()
1055+
receipts[j].TransactionIndex = uint(j)
1056+
10521057
// The contract address can be derived from the transaction itself
10531058
if transactions[j].To() == nil {
10541059
// Deriving the signer is expensive, only do if it's actually needed

core/database_util.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,9 @@ func GetBlockReceipts(db DatabaseReader, hash common.Hash, number uint64) types.
259259
receipts := make(types.Receipts, len(storageReceipts))
260260
for i, receipt := range storageReceipts {
261261
receipts[i] = (*types.Receipt)(receipt)
262+
receipts[i].BlockHash = hash
263+
receipts[i].BlockNumber = big.NewInt(0).SetUint64(number)
264+
receipts[i].TransactionIndex = uint(i)
262265
for _, log := range receipts[i].Logs {
263266
// update BlockHash to fix #208
264267
log.BlockHash = hash

core/state/statedb.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,16 @@ func (self *StateDB) GetStorageRoot(addr common.Address) common.Hash {
239239
return common.Hash{}
240240
}
241241

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

core/state_processor.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,9 @@ func ApplyTransaction(config *params.ChainConfig, tokensFee map[common.Address]*
429429
// Set the receipt logs and create a bloom for filtering
430430
receipt.Logs = statedb.GetLogs(tx.Hash())
431431
receipt.Bloom = types.CreateBloom(types.Receipts{receipt})
432+
receipt.BlockHash = statedb.BlockHash()
433+
receipt.BlockNumber = header.Number
434+
receipt.TransactionIndex = uint(statedb.TxIndex())
432435
if balanceFee != nil && failed {
433436
state.PayFeeWithTRC21TxFail(statedb, msg.From(), *tx.To())
434437
}
@@ -467,6 +470,9 @@ func ApplySignTransaction(config *params.ChainConfig, statedb *state.StateDB, he
467470
statedb.AddLog(log)
468471
receipt.Logs = statedb.GetLogs(tx.Hash())
469472
receipt.Bloom = types.CreateBloom(types.Receipts{receipt})
473+
receipt.BlockHash = statedb.BlockHash()
474+
receipt.BlockNumber = header.Number
475+
receipt.TransactionIndex = uint(statedb.TxIndex())
470476
return receipt, 0, nil, false
471477
}
472478

@@ -491,6 +497,9 @@ func ApplyEmptyTransaction(config *params.ChainConfig, statedb *state.StateDB, h
491497
statedb.AddLog(log)
492498
receipt.Logs = statedb.GetLogs(tx.Hash())
493499
receipt.Bloom = types.CreateBloom(types.Receipts{receipt})
500+
receipt.BlockHash = statedb.BlockHash()
501+
receipt.BlockNumber = header.Number
502+
receipt.TransactionIndex = uint(statedb.TxIndex())
494503
return receipt, 0, nil, false
495504
}
496505

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/XinFinOrg/XDPoSChain/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 uint `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.Uint
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: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -364,18 +364,26 @@ func (self *worker) wait() {
364364
}
365365
work := result.Work
366366

367+
// Different block could share same sealhash, deep copy here to prevent write-write conflict.
368+
hash := block.Hash()
369+
receipts := make([]*types.Receipt, len(work.receipts))
370+
for i, receipt := range work.receipts {
371+
// add block location fields
372+
receipt.BlockHash = hash
373+
receipt.BlockNumber = block.Number()
374+
receipt.TransactionIndex = uint(i)
375+
376+
receipts[i] = new(types.Receipt)
377+
*receipts[i] = *receipt
378+
}
367379
// Update the block hash in all logs since it is now available and not when the
368380
// receipt/log of individual transactions were created.
369-
for _, r := range work.receipts {
370-
for _, l := range r.Logs {
371-
l.BlockHash = block.Hash()
372-
}
373-
}
374381
for _, log := range work.state.Logs() {
375-
log.BlockHash = block.Hash()
382+
log.BlockHash = hash
376383
}
384+
// Commit block and state to database.
377385
self.currentMu.Lock()
378-
stat, err := self.chain.WriteBlockWithState(block, work.receipts, work.state, work.tradingState, work.lendingState)
386+
stat, err := self.chain.WriteBlockWithState(block, receipts, work.state, work.tradingState, work.lendingState)
379387
self.currentMu.Unlock()
380388
if err != nil {
381389
log.Error("Failed writing block to chain", "err", err)

0 commit comments

Comments
 (0)