core: remove unnecessary fields in log

This commit is contained in:
rjl493456442 2018-06-30 15:02:31 +08:00
parent 15b9b39e6c
commit 1ba971e597
5 changed files with 61 additions and 20 deletions

View file

@ -294,7 +294,17 @@ func ReadReceipts(db DatabaseReader, hash common.Hash, number uint64) types.Rece
return nil return nil
} }
receipts := make(types.Receipts, len(storageReceipts)) receipts := make(types.Receipts, len(storageReceipts))
logIndex := uint(0)
for i, receipt := range storageReceipts { 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) receipts[i] = (*types.Receipt)(receipt)
} }
return receipts return receipts

View file

@ -279,6 +279,7 @@ func TestBlockReceiptStorage(t *testing.T) {
ContractAddress: common.BytesToAddress([]byte{0x01, 0x11, 0x11}), ContractAddress: common.BytesToAddress([]byte{0x01, 0x11, 0x11}),
GasUsed: 111111, GasUsed: 111111,
} }
receipt1.Bloom = types.CreateBloom(types.Receipts{receipt1})
receipt2 := &types.Receipt{ receipt2 := &types.Receipt{
PostState: common.Hash{2}.Bytes(), PostState: common.Hash{2}.Bytes(),
CumulativeGasUsed: 2, CumulativeGasUsed: 2,
@ -290,6 +291,7 @@ func TestBlockReceiptStorage(t *testing.T) {
ContractAddress: common.BytesToAddress([]byte{0x02, 0x22, 0x22}), ContractAddress: common.BytesToAddress([]byte{0x02, 0x22, 0x22}),
GasUsed: 222222, GasUsed: 222222,
} }
receipt2.Bloom = types.CreateBloom(types.Receipts{receipt2})
receipts := []*types.Receipt{receipt1, receipt2} receipts := []*types.Receipt{receipt1, receipt2}
// Check that no receipt entries are in a pristine database // Check that no receipt entries are in a pristine database

View file

@ -12,6 +12,7 @@ import (
var _ = (*logMarshaling)(nil) var _ = (*logMarshaling)(nil)
// MarshalJSON marshals as JSON.
func (l Log) MarshalJSON() ([]byte, error) { func (l Log) MarshalJSON() ([]byte, error) {
type Log struct { type Log struct {
Address common.Address `json:"address" gencodec:"required"` Address common.Address `json:"address" gencodec:"required"`
@ -37,6 +38,7 @@ func (l Log) MarshalJSON() ([]byte, error) {
return json.Marshal(&enc) return json.Marshal(&enc)
} }
// UnmarshalJSON unmarshals from JSON.
func (l *Log) UnmarshalJSON(input []byte) error { func (l *Log) UnmarshalJSON(input []byte) error {
type Log struct { type Log struct {
Address *common.Address `json:"address" gencodec:"required"` Address *common.Address `json:"address" gencodec:"required"`

View file

@ -68,7 +68,11 @@ type rlpLog struct {
Data []byte 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 Address common.Address
Topics []common.Hash Topics []common.Hash
Data []byte Data []byte
@ -101,31 +105,34 @@ type LogForStorage Log
// EncodeRLP implements rlp.Encoder. // EncodeRLP implements rlp.Encoder.
func (l *LogForStorage) EncodeRLP(w io.Writer) error { func (l *LogForStorage) EncodeRLP(w io.Writer) error {
return rlp.Encode(w, rlpStorageLog{ return rlp.Encode(w, rlpStorageLog{
Address: l.Address, Address: l.Address,
Topics: l.Topics, Topics: l.Topics,
Data: l.Data, Data: l.Data,
BlockNumber: l.BlockNumber,
TxHash: l.TxHash,
TxIndex: l.TxIndex,
BlockHash: l.BlockHash,
Index: l.Index,
}) })
} }
// DecodeRLP implements rlp.Decoder. // 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 { func (l *LogForStorage) DecodeRLP(s *rlp.Stream) error {
var dec rlpStorageLog var dec rlpStorageLog
err := s.Decode(&dec) err := s.Decode(&dec)
if err == nil { if err == nil {
*l = LogForStorage{ *l = LogForStorage{
Address: dec.Address, Address: dec.Address,
Topics: dec.Topics, Topics: dec.Topics,
Data: dec.Data, Data: dec.Data,
BlockNumber: dec.BlockNumber, }
TxHash: dec.TxHash, } else {
TxIndex: dec.TxIndex, // Try to decode log with previous definition.
BlockHash: dec.BlockHash, var dec rlpSwollenStorageLog
Index: dec.Index, err = s.Decode(&dec)
if err == nil {
*l = LogForStorage{
Address: dec.Address,
Topics: dec.Topics,
Data: dec.Data,
}
} }
} }
return err return err

View file

@ -72,7 +72,18 @@ type receiptRLP struct {
Logs []*Log Logs []*Log
} }
// receiptStorageRLP is the storage encoding of a receipt.
type receiptStorageRLP struct { 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 PostStateOrStatus []byte
CumulativeGasUsed uint64 CumulativeGasUsed uint64
Bloom Bloom Bloom Bloom
@ -159,7 +170,6 @@ func (r *ReceiptForStorage) EncodeRLP(w io.Writer) error {
enc := &receiptStorageRLP{ enc := &receiptStorageRLP{
PostStateOrStatus: (*Receipt)(r).statusEncoding(), PostStateOrStatus: (*Receipt)(r).statusEncoding(),
CumulativeGasUsed: r.CumulativeGasUsed, CumulativeGasUsed: r.CumulativeGasUsed,
Bloom: r.Bloom,
TxHash: r.TxHash, TxHash: r.TxHash,
ContractAddress: r.ContractAddress, ContractAddress: r.ContractAddress,
Logs: make([]*LogForStorage, len(r.Logs)), 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 { func (r *ReceiptForStorage) DecodeRLP(s *rlp.Stream) error {
var dec receiptStorageRLP var dec receiptStorageRLP
if err := s.Decode(&dec); err != nil { 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 { if err := (*Receipt)(r).setStatus(dec.PostStateOrStatus); err != nil {
return err return err
} }
// Assign the consensus fields // Assign the consensus fields
r.CumulativeGasUsed, r.Bloom = dec.CumulativeGasUsed, dec.Bloom r.CumulativeGasUsed = dec.CumulativeGasUsed
r.Logs = make([]*Log, len(dec.Logs)) r.Logs = make([]*Log, len(dec.Logs))
for i, log := range dec.Logs { for i, log := range dec.Logs {
r.Logs[i] = (*Log)(log) r.Logs[i] = (*Log)(log)
} }
r.Bloom = CreateBloom(Receipts{(*Receipt)(r)})
// Assign the implementation fields // Assign the implementation fields
r.TxHash, r.ContractAddress, r.GasUsed = dec.TxHash, dec.ContractAddress, dec.GasUsed r.TxHash, r.ContractAddress, r.GasUsed = dec.TxHash, dec.ContractAddress, dec.GasUsed
return nil return nil