From 1ba971e59772786ebbcddeeee099a03e3cb5be1e Mon Sep 17 00:00:00 2001 From: rjl493456442 Date: Sat, 30 Jun 2018 15:02:31 +0800 Subject: [PATCH] core: remove unnecessary fields in log --- core/rawdb/accessors_chain.go | 10 ++++++++ core/rawdb/accessors_chain_test.go | 2 ++ core/types/gen_log_json.go | 2 ++ core/types/log.go | 41 +++++++++++++++++------------- core/types/receipt.go | 26 ++++++++++++++++--- 5 files changed, 61 insertions(+), 20 deletions(-) diff --git a/core/rawdb/accessors_chain.go b/core/rawdb/accessors_chain.go index 491a125c65..8a95dafe95 100644 --- a/core/rawdb/accessors_chain.go +++ b/core/rawdb/accessors_chain.go @@ -294,7 +294,17 @@ func ReadReceipts(db DatabaseReader, hash common.Hash, number uint64) types.Rece return nil } receipts := make(types.Receipts, len(storageReceipts)) + logIndex := uint(0) for i, receipt := range storageReceipts { + // Assemble deriving fields for log. + for _, log := range receipt.Logs { + log.TxHash = receipt.TxHash + log.BlockHash = hash + log.BlockNumber = number + log.TxIndex = uint(i) + log.Index = logIndex + logIndex += 1 + } receipts[i] = (*types.Receipt)(receipt) } return receipts diff --git a/core/rawdb/accessors_chain_test.go b/core/rawdb/accessors_chain_test.go index fcc36dc2b4..37e0d4fda1 100644 --- a/core/rawdb/accessors_chain_test.go +++ b/core/rawdb/accessors_chain_test.go @@ -279,6 +279,7 @@ func TestBlockReceiptStorage(t *testing.T) { ContractAddress: common.BytesToAddress([]byte{0x01, 0x11, 0x11}), GasUsed: 111111, } + receipt1.Bloom = types.CreateBloom(types.Receipts{receipt1}) receipt2 := &types.Receipt{ PostState: common.Hash{2}.Bytes(), CumulativeGasUsed: 2, @@ -290,6 +291,7 @@ func TestBlockReceiptStorage(t *testing.T) { ContractAddress: common.BytesToAddress([]byte{0x02, 0x22, 0x22}), GasUsed: 222222, } + receipt2.Bloom = types.CreateBloom(types.Receipts{receipt2}) receipts := []*types.Receipt{receipt1, receipt2} // Check that no receipt entries are in a pristine database diff --git a/core/types/gen_log_json.go b/core/types/gen_log_json.go index 1b5ae3c653..6e94339478 100644 --- a/core/types/gen_log_json.go +++ b/core/types/gen_log_json.go @@ -12,6 +12,7 @@ import ( var _ = (*logMarshaling)(nil) +// MarshalJSON marshals as JSON. func (l Log) MarshalJSON() ([]byte, error) { type Log struct { Address common.Address `json:"address" gencodec:"required"` @@ -37,6 +38,7 @@ func (l Log) MarshalJSON() ([]byte, error) { return json.Marshal(&enc) } +// UnmarshalJSON unmarshals from JSON. func (l *Log) UnmarshalJSON(input []byte) error { type Log struct { Address *common.Address `json:"address" gencodec:"required"` diff --git a/core/types/log.go b/core/types/log.go index 717cd2e5a0..755c11814f 100644 --- a/core/types/log.go +++ b/core/types/log.go @@ -68,7 +68,11 @@ type rlpLog struct { Data []byte } -type rlpStorageLog struct { +// rlpStorageLog is the storage encoding of a log. +type rlpStorageLog rlpLog + +// rlpSwollenStorageLog is the previous storage encoding of a log including some redundant fields. +type rlpSwollenStorageLog struct { Address common.Address Topics []common.Hash Data []byte @@ -101,31 +105,34 @@ type LogForStorage Log // EncodeRLP implements rlp.Encoder. func (l *LogForStorage) EncodeRLP(w io.Writer) error { return rlp.Encode(w, rlpStorageLog{ - Address: l.Address, - Topics: l.Topics, - Data: l.Data, - BlockNumber: l.BlockNumber, - TxHash: l.TxHash, - TxIndex: l.TxIndex, - BlockHash: l.BlockHash, - Index: l.Index, + Address: l.Address, + Topics: l.Topics, + Data: l.Data, }) } // DecodeRLP implements rlp.Decoder. +// +// Note some redundant fields(e.g. block number, tx hash etc) will be assembled later. func (l *LogForStorage) DecodeRLP(s *rlp.Stream) error { var dec rlpStorageLog err := s.Decode(&dec) if err == nil { *l = LogForStorage{ - Address: dec.Address, - Topics: dec.Topics, - Data: dec.Data, - BlockNumber: dec.BlockNumber, - TxHash: dec.TxHash, - TxIndex: dec.TxIndex, - BlockHash: dec.BlockHash, - Index: dec.Index, + Address: dec.Address, + Topics: dec.Topics, + Data: dec.Data, + } + } else { + // Try to decode log with previous definition. + var dec rlpSwollenStorageLog + err = s.Decode(&dec) + if err == nil { + *l = LogForStorage{ + Address: dec.Address, + Topics: dec.Topics, + Data: dec.Data, + } } } return err diff --git a/core/types/receipt.go b/core/types/receipt.go index 3d1fc95aab..298a928697 100644 --- a/core/types/receipt.go +++ b/core/types/receipt.go @@ -72,7 +72,18 @@ type receiptRLP struct { Logs []*Log } +// receiptStorageRLP is the storage encoding of a receipt. type receiptStorageRLP struct { + PostStateOrStatus []byte + CumulativeGasUsed uint64 + TxHash common.Hash + ContractAddress common.Address + Logs []*LogForStorage + GasUsed uint64 +} + +// receiptSwollenStorageRLP is the previous storage encoding of a receipt including some unnecessary fields. +type receiptSwollenStorageRLP struct { PostStateOrStatus []byte CumulativeGasUsed uint64 Bloom Bloom @@ -159,7 +170,6 @@ func (r *ReceiptForStorage) EncodeRLP(w io.Writer) error { enc := &receiptStorageRLP{ PostStateOrStatus: (*Receipt)(r).statusEncoding(), CumulativeGasUsed: r.CumulativeGasUsed, - Bloom: r.Bloom, TxHash: r.TxHash, ContractAddress: r.ContractAddress, Logs: make([]*LogForStorage, len(r.Logs)), @@ -176,17 +186,27 @@ func (r *ReceiptForStorage) EncodeRLP(w io.Writer) error { func (r *ReceiptForStorage) DecodeRLP(s *rlp.Stream) error { var dec receiptStorageRLP if err := s.Decode(&dec); err != nil { - return err + var sdec receiptSwollenStorageRLP + if err := s.Decode(&sdec); err != nil { + return err + } + dec.PostStateOrStatus = common.CopyBytes(sdec.PostStateOrStatus) + dec.CumulativeGasUsed = sdec.CumulativeGasUsed + dec.TxHash = sdec.TxHash + dec.ContractAddress = sdec.ContractAddress + dec.Logs = sdec.Logs + dec.GasUsed = sdec.GasUsed } if err := (*Receipt)(r).setStatus(dec.PostStateOrStatus); err != nil { return err } // Assign the consensus fields - r.CumulativeGasUsed, r.Bloom = dec.CumulativeGasUsed, dec.Bloom + r.CumulativeGasUsed = dec.CumulativeGasUsed r.Logs = make([]*Log, len(dec.Logs)) for i, log := range dec.Logs { r.Logs[i] = (*Log)(log) } + r.Bloom = CreateBloom(Receipts{(*Receipt)(r)}) // Assign the implementation fields r.TxHash, r.ContractAddress, r.GasUsed = dec.TxHash, dec.ContractAddress, dec.GasUsed return nil