Skip to content

Commit 42c8afd

Browse files
committed
core: differentiate receipt concensus and storage decoding
1 parent b99fe27 commit 42c8afd

9 files changed

Lines changed: 110 additions & 77 deletions

File tree

‎core/block_processor.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ func (self *BlockProcessor) ApplyTransaction(gp GasPool, statedb *state.StateDB,
111111
}
112112

113113
logs := statedb.GetLogs(tx.Hash())
114-
receipt.SetLogs(logs)
114+
receipt.Logs = logs
115115
receipt.Bloom = types.CreateBloom(types.Receipts{receipt})
116116

117117
glog.V(logger.Debug).Infoln(receipt)
@@ -364,7 +364,7 @@ func (sm *BlockProcessor) GetLogs(block *types.Block) (logs vm.Logs, err error)
364364
receipts := GetBlockReceipts(sm.chainDb, block.Hash())
365365
// coalesce logs
366366
for _, receipt := range receipts {
367-
logs = append(logs, receipt.Logs()...)
367+
logs = append(logs, receipt.Logs...)
368368
}
369369
return logs, nil
370370
}

‎core/block_processor_test.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func TestPutReceipt(t *testing.T) {
7070
hash[0] = 2
7171

7272
receipt := new(types.Receipt)
73-
receipt.SetLogs(vm.Logs{&vm.Log{
73+
receipt.Logs = vm.Logs{&vm.Log{
7474
Address: addr,
7575
Topics: []common.Hash{hash},
7676
Data: []byte("hi"),
@@ -79,7 +79,7 @@ func TestPutReceipt(t *testing.T) {
7979
TxIndex: 0,
8080
BlockHash: hash,
8181
Index: 0,
82-
}})
82+
}}
8383

8484
PutReceipts(db, types.Receipts{receipt})
8585
receipt = GetReceipt(db, common.Hash{})

‎core/chain_makers.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func (b *BlockGen) AddTx(tx *types.Transaction) {
9999
b.header.GasUsed.Add(b.header.GasUsed, gas)
100100
receipt := types.NewReceipt(root.Bytes(), b.header.GasUsed)
101101
logs := b.statedb.GetLogs(tx.Hash())
102-
receipt.SetLogs(logs)
102+
receipt.Logs = logs
103103
receipt.Bloom = types.CreateBloom(types.Receipts{receipt})
104104
b.txs = append(b.txs, tx)
105105
b.receipts = append(b.receipts, receipt)

‎core/transaction_util.go‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,16 @@ func GetBlockReceipts(db ethdb.Database, hash common.Hash) types.Receipts {
140140
if len(data) == 0 {
141141
return nil
142142
}
143-
receipts := new(types.Receipts)
144-
if err := rlp.DecodeBytes(data, receipts); err != nil {
143+
rs := []*types.ReceiptForStorage{}
144+
if err := rlp.DecodeBytes(data, &rs); err != nil {
145145
glog.V(logger.Error).Infof("invalid receipt array RLP for hash %x: %v", hash, err)
146146
return nil
147147
}
148-
return *receipts
148+
receipts := make(types.Receipts, len(rs))
149+
for i, receipt := range rs {
150+
receipts[i] = (*types.Receipt)(receipt)
151+
}
152+
return receipts
149153
}
150154

151155
// PutBlockReceipts stores the block's transactions associated receipts

‎core/types/bloom9.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func (b Bloom) TestBytes(test []byte) bool {
7272
func CreateBloom(receipts Receipts) Bloom {
7373
bin := new(big.Int)
7474
for _, receipt := range receipts {
75-
bin.Or(bin, LogsBloom(receipt.logs))
75+
bin.Or(bin, LogsBloom(receipt.Logs))
7676
}
7777

7878
return BytesToBloom(bin.Bytes())

‎core/types/receipt.go‎

Lines changed: 74 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package types
1818

1919
import (
20-
"bytes"
2120
"fmt"
2221
"io"
2322
"math/big"
@@ -27,89 +26,116 @@ import (
2726
"github.com/ethereum/go-ethereum/rlp"
2827
)
2928

29+
// Receipt represents the results of a transaction.
3030
type Receipt struct {
31+
// Consensus fields
3132
PostState []byte
3233
CumulativeGasUsed *big.Int
3334
Bloom Bloom
34-
TxHash common.Hash
35-
ContractAddress common.Address
36-
logs vm.Logs
37-
GasUsed *big.Int
35+
Logs vm.Logs
36+
37+
// Implementation fields
38+
TxHash common.Hash
39+
ContractAddress common.Address
40+
GasUsed *big.Int
3841
}
3942

43+
// NewReceipt creates a barebone transaction receipt, copying the init fields.
4044
func NewReceipt(root []byte, cumalativeGasUsed *big.Int) *Receipt {
4145
return &Receipt{PostState: common.CopyBytes(root), CumulativeGasUsed: new(big.Int).Set(cumalativeGasUsed)}
4246
}
4347

44-
func (self *Receipt) SetLogs(logs vm.Logs) {
45-
self.logs = logs
46-
}
47-
48-
func (self *Receipt) Logs() vm.Logs {
49-
return self.logs
50-
}
51-
52-
func (self *Receipt) EncodeRLP(w io.Writer) error {
53-
return rlp.Encode(w, []interface{}{self.PostState, self.CumulativeGasUsed, self.Bloom, self.logs})
48+
// EncodeRLP implements rlp.Encoder, and flattens the consensus fields of a receipt
49+
// into an RLP stream.
50+
func (r *Receipt) EncodeRLP(w io.Writer) error {
51+
return rlp.Encode(w, []interface{}{r.PostState, r.CumulativeGasUsed, r.Bloom, r.Logs})
5452
}
5553

56-
func (self *Receipt) DecodeRLP(s *rlp.Stream) error {
57-
var r struct {
54+
// DecodeRLP implements rlp.Decoder, and loads the consensus fields of a receipt
55+
// from an RLP stream.
56+
func (r *Receipt) DecodeRLP(s *rlp.Stream) error {
57+
var receipt struct {
5858
PostState []byte
5959
CumulativeGasUsed *big.Int
6060
Bloom Bloom
61-
TxHash common.Hash
62-
ContractAddress common.Address
6361
Logs vm.Logs
64-
GasUsed *big.Int
6562
}
66-
if err := s.Decode(&r); err != nil {
63+
if err := s.Decode(&receipt); err != nil {
6764
return err
6865
}
69-
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
70-
66+
r.PostState, r.CumulativeGasUsed, r.Bloom, r.Logs = receipt.PostState, receipt.CumulativeGasUsed, receipt.Bloom, receipt.Logs
7167
return nil
7268
}
7369

74-
type ReceiptForStorage Receipt
75-
76-
func (self *ReceiptForStorage) EncodeRLP(w io.Writer) error {
77-
storageLogs := make([]*vm.LogForStorage, len(self.logs))
78-
for i, log := range self.logs {
79-
storageLogs[i] = (*vm.LogForStorage)(log)
80-
}
81-
return rlp.Encode(w, []interface{}{self.PostState, self.CumulativeGasUsed, self.Bloom, self.TxHash, self.ContractAddress, storageLogs, self.GasUsed})
82-
}
83-
84-
func (self *Receipt) RlpEncode() []byte {
85-
bytes, err := rlp.EncodeToBytes(self)
70+
// RlpEncode implements common.RlpEncode required for SHA derivation.
71+
func (r *Receipt) RlpEncode() []byte {
72+
bytes, err := rlp.EncodeToBytes(r)
8673
if err != nil {
87-
fmt.Println("TMP -- RECEIPT ENCODE ERROR", err)
74+
panic(err)
8875
}
8976
return bytes
9077
}
9178

92-
func (self *Receipt) Cmp(other *Receipt) bool {
93-
if bytes.Compare(self.PostState, other.PostState) != 0 {
94-
return false
95-
}
79+
// String implements the Stringer interface.
80+
func (r *Receipt) String() string {
81+
return fmt.Sprintf("receipt{med=%x cgas=%v bloom=%x logs=%v}", r.PostState, r.CumulativeGasUsed, r.Bloom, r.Logs)
82+
}
9683

97-
return true
84+
// ReceiptForStorage is a wrapper around a Receipt that flattens and parses the
85+
// entire content of a receipt, opposed to only the consensus fields originally.
86+
type ReceiptForStorage Receipt
87+
88+
// EncodeRLP implements rlp.Encoder, and flattens all content fields of a receipt
89+
// into an RLP stream.
90+
func (r *ReceiptForStorage) EncodeRLP(w io.Writer) error {
91+
logs := make([]*vm.LogForStorage, len(r.Logs))
92+
for i, log := range r.Logs {
93+
logs[i] = (*vm.LogForStorage)(log)
94+
}
95+
return rlp.Encode(w, []interface{}{r.PostState, r.CumulativeGasUsed, r.Bloom, r.TxHash, r.ContractAddress, logs, r.GasUsed})
9896
}
9997

100-
func (self *Receipt) String() string {
101-
return fmt.Sprintf("receipt{med=%x cgas=%v bloom=%x logs=%v}", self.PostState, self.CumulativeGasUsed, self.Bloom, self.logs)
98+
// DecodeRLP implements rlp.Decoder, and loads the consensus fields of a receipt
99+
// from an RLP stream.
100+
func (r *ReceiptForStorage) DecodeRLP(s *rlp.Stream) error {
101+
var receipt struct {
102+
PostState []byte
103+
CumulativeGasUsed *big.Int
104+
Bloom Bloom
105+
TxHash common.Hash
106+
ContractAddress common.Address
107+
Logs []*vm.LogForStorage
108+
GasUsed *big.Int
109+
}
110+
if err := s.Decode(&receipt); err != nil {
111+
return err
112+
}
113+
// Assign the consensus fields
114+
r.PostState, r.CumulativeGasUsed, r.Bloom = receipt.PostState, receipt.CumulativeGasUsed, receipt.Bloom
115+
r.Logs = make(vm.Logs, len(receipt.Logs))
116+
for i, log := range receipt.Logs {
117+
r.Logs[i] = (*vm.Log)(log)
118+
}
119+
// Assign the implementation fields
120+
r.TxHash, r.ContractAddress, r.GasUsed = receipt.TxHash, receipt.ContractAddress, receipt.GasUsed
121+
122+
return nil
102123
}
103124

125+
// Receipts is a wrapper around a Receipt array to implement types.DerivableList.
104126
type Receipts []*Receipt
105127

106-
func (self Receipts) RlpEncode() []byte {
107-
bytes, err := rlp.EncodeToBytes(self)
128+
// RlpEncode implements common.RlpEncode required for SHA derivation.
129+
func (r Receipts) RlpEncode() []byte {
130+
bytes, err := rlp.EncodeToBytes(r)
108131
if err != nil {
109-
fmt.Println("TMP -- RECEIPTS ENCODE ERROR", err)
132+
panic(err)
110133
}
111134
return bytes
112135
}
113136

114-
func (self Receipts) Len() int { return len(self) }
115-
func (self Receipts) GetRlp(i int) []byte { return common.Rlp(self[i]) }
137+
// Len returns the number of receipts in this list.
138+
func (r Receipts) Len() int { return len(r) }
139+
140+
// GetRlp returns the RLP encoding of one receipt from the list.
141+
func (r Receipts) GetRlp(i int) []byte { return common.Rlp(r[i]) }

‎core/vm/log.go‎

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -40,27 +40,30 @@ func NewLog(address common.Address, topics []common.Hash, data []byte, number ui
4040
return &Log{Address: address, Topics: topics, Data: data, Number: number}
4141
}
4242

43-
func (self *Log) EncodeRLP(w io.Writer) error {
44-
return rlp.Encode(w, []interface{}{self.Address, self.Topics, self.Data})
43+
func (l *Log) EncodeRLP(w io.Writer) error {
44+
return rlp.Encode(w, []interface{}{l.Address, l.Topics, l.Data})
4545
}
4646

47-
func (self *Log) String() string {
48-
return fmt.Sprintf(`log: %x %x %x %x %d %x %d`, self.Address, self.Topics, self.Data, self.TxHash, self.TxIndex, self.BlockHash, self.Index)
47+
func (l *Log) DecodeRLP(s *rlp.Stream) error {
48+
var log struct {
49+
Address common.Address
50+
Topics []common.Hash
51+
Data []byte
52+
}
53+
if err := s.Decode(&log); err != nil {
54+
return err
55+
}
56+
l.Address, l.Topics, l.Data = log.Address, log.Topics, log.Data
57+
return nil
58+
}
59+
60+
func (l *Log) String() string {
61+
return fmt.Sprintf(`log: %x %x %x %x %d %x %d`, l.Address, l.Topics, l.Data, l.TxHash, l.TxIndex, l.BlockHash, l.Index)
4962
}
5063

5164
type Logs []*Log
5265

66+
// LogForStorage is a wrapper around a Log that flattens and parses the entire
67+
// content of a log, opposed to only the consensus fields originally (by hiding
68+
// the rlp interface methods).
5369
type LogForStorage Log
54-
55-
func (self *LogForStorage) EncodeRLP(w io.Writer) error {
56-
return rlp.Encode(w, []interface{}{
57-
self.Address,
58-
self.Topics,
59-
self.Data,
60-
self.Number,
61-
self.TxHash,
62-
self.TxIndex,
63-
self.BlockHash,
64-
self.Index,
65-
})
66-
}

‎eth/filters/filter.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ func (self *Filter) getLogs(start, end uint64) (logs vm.Logs) {
138138
unfiltered vm.Logs
139139
)
140140
for _, receipt := range receipts {
141-
unfiltered = append(unfiltered, receipt.Logs()...)
141+
unfiltered = append(unfiltered, receipt.Logs...)
142142
}
143143
logs = append(logs, self.FilterLogs(unfiltered)...)
144144
}

‎rpc/api/parsing.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -453,8 +453,8 @@ func NewReceiptRes(rec *types.Receipt) *ReceiptRes {
453453
v.ContractAddress = newHexData(rec.ContractAddress)
454454
}
455455

456-
logs := make([]interface{}, len(rec.Logs()))
457-
for i, log := range rec.Logs() {
456+
logs := make([]interface{}, len(rec.Logs))
457+
for i, log := range rec.Logs {
458458
logs[i] = NewLogRes(log)
459459
}
460460
v.Logs = &logs

0 commit comments

Comments
 (0)