Skip to content

Commit 2feb23c

Browse files
committed
core, eth, miner, xeth: receipt storage fix
* Added GetReceiptsFromBlock, GetReceipt, PutReceipts * Added ContractAddress to receipt. See bnb-chain#1042
1 parent acd85fe commit 2feb23c

8 files changed

Lines changed: 75 additions & 57 deletions

File tree

‎core/block_processor.go‎

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ import (
99
"github.com/ethereum/go-ethereum/common"
1010
"github.com/ethereum/go-ethereum/core/state"
1111
"github.com/ethereum/go-ethereum/core/types"
12+
"github.com/ethereum/go-ethereum/crypto"
1213
"github.com/ethereum/go-ethereum/event"
1314
"github.com/ethereum/go-ethereum/logger"
1415
"github.com/ethereum/go-ethereum/logger/glog"
1516
"github.com/ethereum/go-ethereum/params"
1617
"github.com/ethereum/go-ethereum/pow"
17-
"github.com/ethereum/go-ethereum/rlp"
1818
"gopkg.in/fatih/set.v0"
1919
)
2020

@@ -24,8 +24,6 @@ const (
2424
BlockChainVersion = 3
2525
)
2626

27-
var receiptsPre = []byte("receipts-")
28-
2927
type BlockProcessor struct {
3028
db common.Database
3129
extraDb common.Database
@@ -83,6 +81,12 @@ func (self *BlockProcessor) ApplyTransaction(coinbase *state.StateObject, stated
8381

8482
usedGas.Add(usedGas, gas)
8583
receipt := types.NewReceipt(statedb.Root().Bytes(), usedGas)
84+
receipt.TxHash = tx.Hash()
85+
if MessageCreatesContract(tx) {
86+
from, _ := tx.From()
87+
receipt.ContractAddress = crypto.CreateAddress(from, tx.Nonce())
88+
}
89+
8690
logs := statedb.GetLogs(tx.Hash())
8791
receipt.SetLogs(logs)
8892
receipt.Bloom = types.CreateBloom(types.Receipts{receipt})
@@ -319,16 +323,20 @@ func (sm *BlockProcessor) VerifyUncles(statedb *state.StateDB, block, parent *ty
319323
}
320324

321325
// GetBlockReceipts returns the receipts beloniging to the block hash
322-
func (sm *BlockProcessor) GetBlockReceipts(bhash common.Hash) (receipts types.Receipts, err error) {
323-
return getBlockReceipts(sm.extraDb, bhash)
326+
func (sm *BlockProcessor) GetBlockReceipts(bhash common.Hash) types.Receipts {
327+
if block := sm.ChainManager().GetBlock(bhash); block != nil {
328+
return GetReceiptsFromBlock(sm.extraDb, block)
329+
}
330+
331+
return nil
324332
}
325333

326334
// GetLogs returns the logs of the given block. This method is using a two step approach
327335
// where it tries to get it from the (updated) method which gets them from the receipts or
328336
// the depricated way by re-processing the block.
329337
func (sm *BlockProcessor) GetLogs(block *types.Block) (logs state.Logs, err error) {
330-
receipts, err := sm.GetBlockReceipts(block.Hash())
331-
if err == nil && len(receipts) > 0 {
338+
receipts := GetReceiptsFromBlock(sm.extraDb, block)
339+
if len(receipts) > 0 {
332340
// coalesce logs
333341
for _, receipt := range receipts {
334342
logs = append(logs, receipt.Logs()...)
@@ -391,15 +399,3 @@ func ValidateHeader(pow pow.PoW, block *types.Header, parent *types.Block, check
391399

392400
return nil
393401
}
394-
395-
func getBlockReceipts(db common.Database, bhash common.Hash) (receipts types.Receipts, err error) {
396-
var rdata []byte
397-
rdata, err = db.Get(append(receiptsPre, bhash[:]...))
398-
399-
if err == nil {
400-
err = rlp.DecodeBytes(rdata, &receipts)
401-
} else {
402-
glog.V(logger.Detail).Infof("getBlockReceipts error %v\n", err)
403-
}
404-
return
405-
}

‎core/block_processor_test.go‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,9 @@ func TestPutReceipt(t *testing.T) {
6464
Index: 0,
6565
}})
6666

67-
PutReceipts(db, hash, types.Receipts{receipt})
68-
receipts, err := getBlockReceipts(db, hash)
69-
if err != nil {
70-
t.Error("got err:", err)
71-
}
72-
if len(receipts) != 1 {
73-
t.Error("expected to get 1 receipt, got", len(receipts))
67+
PutReceipts(db, types.Receipts{receipt})
68+
receipt = GetReceipt(db, common.Hash{})
69+
if receipt == nil {
70+
t.Error("expected to get 1 receipt, got none.")
7471
}
7572
}

‎core/chain_manager.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,7 @@ func (self *ChainManager) InsertChain(chain types.Blocks) (int, error) {
632632
// This puts transactions in a extra db for rpc
633633
PutTransactions(self.extraDb, block, block.Transactions())
634634
// store the receipts
635-
PutReceipts(self.extraDb, block.Hash(), receipts)
635+
PutReceipts(self.extraDb, receipts)
636636
case SideStatTy:
637637
if glog.V(logger.Detail) {
638638
glog.Infof("inserted forked block #%d (TD=%v) (%d TXs %d UNCs) (%x...). Took %v\n", block.Number(), block.Difficulty(), len(block.Transactions()), len(block.Uncles()), block.Hash().Bytes()[0:4], time.Since(bstart))

‎core/transaction_util.go‎

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import (
88
"github.com/ethereum/go-ethereum/rlp"
99
)
1010

11+
var receiptsPre = []byte("receipts-")
12+
13+
// PutTransactions stores the transactions in the given database
1114
func PutTransactions(db common.Database, block *types.Block, txs types.Transactions) {
1215
for i, tx := range block.Transactions() {
1316
rlpEnc, err := rlp.EncodeToBytes(tx)
@@ -34,18 +37,49 @@ func PutTransactions(db common.Database, block *types.Block, txs types.Transacti
3437
}
3538
}
3639

37-
func PutReceipts(db common.Database, hash common.Hash, receipts types.Receipts) error {
38-
storageReceipts := make([]*types.ReceiptForStorage, len(receipts))
39-
for i, receipt := range receipts {
40-
storageReceipts[i] = (*types.ReceiptForStorage)(receipt)
40+
// PutReceipts stores the receipts in the current database
41+
func PutReceipts(db common.Database, receipts types.Receipts) error {
42+
for _, receipt := range receipts {
43+
storageReceipt := (*types.ReceiptForStorage)(receipt)
44+
bytes, err := rlp.EncodeToBytes(storageReceipt)
45+
if err != nil {
46+
return err
47+
}
48+
err = db.Put(append(receiptsPre, receipt.TxHash[:]...), bytes)
49+
if err != nil {
50+
return err
51+
}
52+
}
53+
54+
return nil
55+
}
56+
57+
// GetReceipt returns a receipt by hash
58+
func GetReceipt(db common.Database, txHash common.Hash) *types.Receipt {
59+
data, _ := db.Get(append(receiptsPre, txHash[:]...))
60+
if len(data) == 0 {
61+
return nil
4162
}
4263

43-
bytes, err := rlp.EncodeToBytes(storageReceipts)
64+
var receipt types.Receipt
65+
err := rlp.DecodeBytes(data, &receipt)
4466
if err != nil {
45-
return err
67+
glog.V(logger.Error).Infoln("GetReceipt err:", err)
4668
}
69+
return &receipt
70+
}
4771

48-
db.Put(append(receiptsPre, hash[:]...), bytes)
72+
// GetReceiptFromBlock returns all receipts with the given block
73+
func GetReceiptsFromBlock(db common.Database, block *types.Block) types.Receipts {
74+
// at some point we want:
75+
//receipts := make(types.Receipts, len(block.Transactions()))
76+
// but since we need to support legacy, we can't (yet)
77+
var receipts types.Receipts
78+
for _, tx := range block.Transactions() {
79+
if receipt := GetReceipt(db, tx.Hash()); receipt != nil {
80+
receipts = append(receipts, receipt)
81+
}
82+
}
4983

50-
return nil
84+
return receipts
5185
}

‎core/types/receipt.go‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ type Receipt struct {
1515
PostState []byte
1616
CumulativeGasUsed *big.Int
1717
Bloom Bloom
18+
TxHash common.Hash
19+
ContractAddress common.Address
1820
logs state.Logs
1921
}
2022

@@ -39,12 +41,14 @@ func (self *Receipt) DecodeRLP(s *rlp.Stream) error {
3941
PostState []byte
4042
CumulativeGasUsed *big.Int
4143
Bloom Bloom
44+
TxHash common.Hash
45+
ContractAddress common.Address
4246
Logs state.Logs
4347
}
4448
if err := s.Decode(&r); err != nil {
4549
return err
4650
}
47-
self.PostState, self.CumulativeGasUsed, self.Bloom, self.logs = r.PostState, r.CumulativeGasUsed, r.Bloom, r.Logs
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
4852

4953
return nil
5054
}
@@ -56,7 +60,7 @@ func (self *ReceiptForStorage) EncodeRLP(w io.Writer) error {
5660
for i, log := range self.logs {
5761
storageLogs[i] = (*state.LogForStorage)(log)
5862
}
59-
return rlp.Encode(w, []interface{}{self.PostState, self.CumulativeGasUsed, self.Bloom, storageLogs})
63+
return rlp.Encode(w, []interface{}{self.PostState, self.CumulativeGasUsed, self.Bloom, self.TxHash, self.ContractAddress, storageLogs})
6064
}
6165

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

‎eth/gasprice.go‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,10 @@ func (self *GasPriceOracle) processBlock(block *types.Block) {
131131
// returns the lowers possible price with which a tx was or could have been included
132132
func (self *GasPriceOracle) lowestPrice(block *types.Block) *big.Int {
133133
gasUsed := new(big.Int)
134-
recepits, err := self.eth.BlockProcessor().GetBlockReceipts(block.Hash())
135-
if err != nil {
136-
return self.eth.GpoMinGasPrice
137-
}
138134

139-
if len(recepits) > 0 {
140-
gasUsed = recepits[len(recepits)-1].CumulativeGasUsed
135+
receipts := self.eth.BlockProcessor().GetBlockReceipts(block.Hash())
136+
if len(receipts) > 0 {
137+
gasUsed = receipts[len(receipts)-1].CumulativeGasUsed
141138
}
142139

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

‎miner/worker.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ func (self *worker) wait() {
255255
// This puts transactions in a extra db for rpc
256256
core.PutTransactions(self.extraDb, block, block.Transactions())
257257
// store the receipts
258-
core.PutReceipts(self.extraDb, block.Hash(), self.current.receipts)
258+
core.PutReceipts(self.extraDb, self.current.receipts)
259259
}
260260

261261
// check staleness and display confirmation

‎xeth/xeth.go‎

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -364,22 +364,12 @@ func (self *XEth) CurrentBlock() *types.Block {
364364
return self.backend.ChainManager().CurrentBlock()
365365
}
366366

367-
func (self *XEth) GetBlockReceipts(bhash common.Hash) (receipts types.Receipts, err error) {
367+
func (self *XEth) GetBlockReceipts(bhash common.Hash) types.Receipts {
368368
return self.backend.BlockProcessor().GetBlockReceipts(bhash)
369369
}
370370

371-
func (self *XEth) GetTxReceipt(txhash common.Hash) (receipt *types.Receipt, err error) {
372-
_, bhash, _, txi := self.EthTransactionByHash(common.ToHex(txhash[:]))
373-
var receipts types.Receipts
374-
receipts, err = self.backend.BlockProcessor().GetBlockReceipts(bhash)
375-
if err == nil {
376-
if txi < uint64(len(receipts)) {
377-
receipt = receipts[txi]
378-
} else {
379-
err = fmt.Errorf("Invalid tx index")
380-
}
381-
}
382-
return
371+
func (self *XEth) GetTxReceipt(txhash common.Hash) *types.Receipt {
372+
return core.GetReceipt(self.backend.ExtraDb(), txhash)
383373
}
384374

385375
func (self *XEth) GasLimit() *big.Int {

0 commit comments

Comments
 (0)