Skip to content

Commit 7fd0cca

Browse files
rjl493456442karalabe
authored andcommitted
core: remove unnecessary fields in logs, receipts and tx lookups (#17106)
* core: remove unnecessary fields in log * core: bump blockchain database version * core, les: remove unnecessary fields in txlookup * eth: print db version explicitly * core/rawdb: drop txlookup entry struct wrapper
1 parent 8577b5b commit 7fd0cca

13 files changed

Lines changed: 147 additions & 61 deletions

‎core/blockchain.go‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,13 @@ const (
6565
triesInMemory = 128
6666

6767
// BlockChainVersion ensures that an incompatible database forces a resync from scratch.
68-
BlockChainVersion uint64 = 3
68+
//
69+
// During the process of upgrading the database version from 3 to 4,
70+
// the following incompatible database changes were added.
71+
// * the `BlockNumber`, `TxHash`, `TxIndex`, `BlockHash` and `Index` fields of log are deleted
72+
// * the `Bloom` field of receipt is deleted
73+
// * the `BlockIndex` and `TxIndex` fields of txlookup are deleted
74+
BlockChainVersion uint64 = 4
6975
)
7076

7177
// CacheConfig contains the configuration values for the trie caching/pruning

‎core/rawdb/accessors_chain.go‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,17 @@ func ReadReceipts(db DatabaseReader, hash common.Hash, number uint64) types.Rece
294294
return nil
295295
}
296296
receipts := make(types.Receipts, len(storageReceipts))
297+
logIndex := uint(0)
297298
for i, receipt := range storageReceipts {
299+
// Assemble deriving fields for log.
300+
for _, log := range receipt.Logs {
301+
log.TxHash = receipt.TxHash
302+
log.BlockHash = hash
303+
log.BlockNumber = number
304+
log.TxIndex = uint(i)
305+
log.Index = logIndex
306+
logIndex += 1
307+
}
298308
receipts[i] = (*types.Receipt)(receipt)
299309
}
300310
return receipts

‎core/rawdb/accessors_chain_test.go‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ func TestBlockReceiptStorage(t *testing.T) {
279279
ContractAddress: common.BytesToAddress([]byte{0x01, 0x11, 0x11}),
280280
GasUsed: 111111,
281281
}
282+
receipt1.Bloom = types.CreateBloom(types.Receipts{receipt1})
282283
receipt2 := &types.Receipt{
283284
PostState: common.Hash{2}.Bytes(),
284285
CumulativeGasUsed: 2,
@@ -290,6 +291,7 @@ func TestBlockReceiptStorage(t *testing.T) {
290291
ContractAddress: common.BytesToAddress([]byte{0x02, 0x22, 0x22}),
291292
GasUsed: 222222,
292293
}
294+
receipt2.Bloom = types.CreateBloom(types.Receipts{receipt2})
293295
receipts := []*types.Receipt{receipt1, receipt2}
294296

295297
// Check that no receipt entries are in a pristine database

‎core/rawdb/accessors_indexes.go‎

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -25,33 +25,28 @@ import (
2525

2626
// ReadTxLookupEntry retrieves the positional metadata associated with a transaction
2727
// hash to allow retrieving the transaction or receipt by hash.
28-
func ReadTxLookupEntry(db DatabaseReader, hash common.Hash) (common.Hash, uint64, uint64) {
28+
func ReadTxLookupEntry(db DatabaseReader, hash common.Hash) common.Hash {
2929
data, _ := db.Get(txLookupKey(hash))
3030
if len(data) == 0 {
31-
return common.Hash{}, 0, 0
31+
return common.Hash{}
3232
}
33-
var entry TxLookupEntry
33+
if len(data) == common.HashLength {
34+
return common.BytesToHash(data)
35+
}
36+
// Probably it's legacy txlookup entry data, try to decode it.
37+
var entry LegacyTxLookupEntry
3438
if err := rlp.DecodeBytes(data, &entry); err != nil {
35-
log.Error("Invalid transaction lookup entry RLP", "hash", hash, "err", err)
36-
return common.Hash{}, 0, 0
39+
log.Error("Invalid transaction lookup entry RLP", "hash", hash, "blob", data, "err", err)
40+
return common.Hash{}
3741
}
38-
return entry.BlockHash, entry.BlockIndex, entry.Index
42+
return entry.BlockHash
3943
}
4044

4145
// WriteTxLookupEntries stores a positional metadata for every transaction from
4246
// a block, enabling hash based transaction and receipt lookups.
4347
func WriteTxLookupEntries(db DatabaseWriter, block *types.Block) {
44-
for i, tx := range block.Transactions() {
45-
entry := TxLookupEntry{
46-
BlockHash: block.Hash(),
47-
BlockIndex: block.NumberU64(),
48-
Index: uint64(i),
49-
}
50-
data, err := rlp.EncodeToBytes(entry)
51-
if err != nil {
52-
log.Crit("Failed to encode transaction lookup entry", "err", err)
53-
}
54-
if err := db.Put(txLookupKey(tx.Hash()), data); err != nil {
48+
for _, tx := range block.Transactions() {
49+
if err := db.Put(txLookupKey(tx.Hash()), block.Hash().Bytes()); err != nil {
5550
log.Crit("Failed to store transaction lookup entry", "err", err)
5651
}
5752
}
@@ -65,31 +60,47 @@ func DeleteTxLookupEntry(db DatabaseDeleter, hash common.Hash) {
6560
// ReadTransaction retrieves a specific transaction from the database, along with
6661
// its added positional metadata.
6762
func ReadTransaction(db DatabaseReader, hash common.Hash) (*types.Transaction, common.Hash, uint64, uint64) {
68-
blockHash, blockNumber, txIndex := ReadTxLookupEntry(db, hash)
63+
blockHash := ReadTxLookupEntry(db, hash)
6964
if blockHash == (common.Hash{}) {
7065
return nil, common.Hash{}, 0, 0
7166
}
72-
body := ReadBody(db, blockHash, blockNumber)
73-
if body == nil || len(body.Transactions) <= int(txIndex) {
74-
log.Error("Transaction referenced missing", "number", blockNumber, "hash", blockHash, "index", txIndex)
67+
blockNumber := ReadHeaderNumber(db, blockHash)
68+
if blockNumber == nil {
69+
return nil, common.Hash{}, 0, 0
70+
}
71+
body := ReadBody(db, blockHash, *blockNumber)
72+
if body == nil {
73+
log.Error("Transaction referenced missing", "number", blockNumber, "hash", blockHash)
7574
return nil, common.Hash{}, 0, 0
7675
}
77-
return body.Transactions[txIndex], blockHash, blockNumber, txIndex
76+
for txIndex, tx := range body.Transactions {
77+
if tx.Hash() == hash {
78+
return tx, blockHash, *blockNumber, uint64(txIndex)
79+
}
80+
}
81+
log.Error("Transaction not found", "number", blockNumber, "hash", blockHash, "txhash", hash)
82+
return nil, common.Hash{}, 0, 0
7883
}
7984

8085
// ReadReceipt retrieves a specific transaction receipt from the database, along with
8186
// its added positional metadata.
8287
func ReadReceipt(db DatabaseReader, hash common.Hash) (*types.Receipt, common.Hash, uint64, uint64) {
83-
blockHash, blockNumber, receiptIndex := ReadTxLookupEntry(db, hash)
88+
blockHash := ReadTxLookupEntry(db, hash)
8489
if blockHash == (common.Hash{}) {
8590
return nil, common.Hash{}, 0, 0
8691
}
87-
receipts := ReadReceipts(db, blockHash, blockNumber)
88-
if len(receipts) <= int(receiptIndex) {
89-
log.Error("Receipt refereced missing", "number", blockNumber, "hash", blockHash, "index", receiptIndex)
92+
blockNumber := ReadHeaderNumber(db, blockHash)
93+
if blockNumber == nil {
9094
return nil, common.Hash{}, 0, 0
9195
}
92-
return receipts[receiptIndex], blockHash, blockNumber, receiptIndex
96+
receipts := ReadReceipts(db, blockHash, *blockNumber)
97+
for receiptIndex, receipt := range receipts {
98+
if receipt.TxHash == hash {
99+
return receipt, blockHash, *blockNumber, uint64(receiptIndex)
100+
}
101+
}
102+
log.Error("Receipt not found", "number", blockNumber, "hash", blockHash, "txhash", hash)
103+
return nil, common.Hash{}, 0, 0
93104
}
94105

95106
// ReadBloomBits retrieves the compressed bloom bit vector belonging to the given

‎core/rawdb/accessors_indexes_test.go‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/ethereum/go-ethereum/common"
2424
"github.com/ethereum/go-ethereum/core/types"
2525
"github.com/ethereum/go-ethereum/ethdb"
26+
"github.com/ethereum/go-ethereum/rlp"
2627
)
2728

2829
// Tests that positional lookup metadata can be stored and retrieved.
@@ -65,4 +66,26 @@ func TestLookupStorage(t *testing.T) {
6566
t.Fatalf("tx #%d [%x]: deleted transaction returned: %v", i, tx.Hash(), txn)
6667
}
6768
}
69+
// Insert legacy txlookup and verify the data retrieval
70+
for index, tx := range block.Transactions() {
71+
entry := LegacyTxLookupEntry{
72+
BlockHash: block.Hash(),
73+
BlockIndex: block.NumberU64(),
74+
Index: uint64(index),
75+
}
76+
data, _ := rlp.EncodeToBytes(entry)
77+
db.Put(txLookupKey(tx.Hash()), data)
78+
}
79+
for i, tx := range txs {
80+
if txn, hash, number, index := ReadTransaction(db, tx.Hash()); txn == nil {
81+
t.Fatalf("tx #%d [%x]: transaction not found", i, tx.Hash())
82+
} else {
83+
if hash != block.Hash() || number != block.NumberU64() || index != uint64(i) {
84+
t.Fatalf("tx #%d [%x]: positional metadata mismatch: have %x/%d/%d, want %x/%v/%v", i, tx.Hash(), hash, number, index, block.Hash(), block.NumberU64(), i)
85+
}
86+
if tx.Hash() != txn.Hash() {
87+
t.Fatalf("tx #%d [%x]: transaction mismatch: have %v, want %v", i, tx.Hash(), txn, tx)
88+
}
89+
}
90+
}
6891
}

‎core/rawdb/schema.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ var (
6363
preimageHitCounter = metrics.NewRegisteredCounter("db/preimage/hits", nil)
6464
)
6565

66-
// TxLookupEntry is a positional metadata to help looking up the data content of
67-
// a transaction or receipt given only its hash.
68-
type TxLookupEntry struct {
66+
// LegacyTxLookupEntry is the legacy TxLookupEntry definition with some unnecessary
67+
// fields.
68+
type LegacyTxLookupEntry struct {
6969
BlockHash common.Hash
7070
BlockIndex uint64
7171
Index uint64

‎core/types/gen_log_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/log.go‎

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,11 @@ type rlpLog struct {
6868
Data []byte
6969
}
7070

71-
type rlpStorageLog struct {
71+
// rlpStorageLog is the storage encoding of a log.
72+
type rlpStorageLog rlpLog
73+
74+
// LegacyRlpStorageLog is the previous storage encoding of a log including some redundant fields.
75+
type LegacyRlpStorageLog struct {
7276
Address common.Address
7377
Topics []common.Hash
7478
Data []byte
@@ -101,31 +105,34 @@ type LogForStorage Log
101105
// EncodeRLP implements rlp.Encoder.
102106
func (l *LogForStorage) EncodeRLP(w io.Writer) error {
103107
return rlp.Encode(w, rlpStorageLog{
104-
Address: l.Address,
105-
Topics: l.Topics,
106-
Data: l.Data,
107-
BlockNumber: l.BlockNumber,
108-
TxHash: l.TxHash,
109-
TxIndex: l.TxIndex,
110-
BlockHash: l.BlockHash,
111-
Index: l.Index,
108+
Address: l.Address,
109+
Topics: l.Topics,
110+
Data: l.Data,
112111
})
113112
}
114113

115114
// DecodeRLP implements rlp.Decoder.
115+
//
116+
// Note some redundant fields(e.g. block number, tx hash etc) will be assembled later.
116117
func (l *LogForStorage) DecodeRLP(s *rlp.Stream) error {
117118
var dec rlpStorageLog
118119
err := s.Decode(&dec)
119120
if err == nil {
120121
*l = LogForStorage{
121-
Address: dec.Address,
122-
Topics: dec.Topics,
123-
Data: dec.Data,
124-
BlockNumber: dec.BlockNumber,
125-
TxHash: dec.TxHash,
126-
TxIndex: dec.TxIndex,
127-
BlockHash: dec.BlockHash,
128-
Index: dec.Index,
122+
Address: dec.Address,
123+
Topics: dec.Topics,
124+
Data: dec.Data,
125+
}
126+
} else {
127+
// Try to decode log with previous definition.
128+
var dec LegacyRlpStorageLog
129+
err = s.Decode(&dec)
130+
if err == nil {
131+
*l = LogForStorage{
132+
Address: dec.Address,
133+
Topics: dec.Topics,
134+
Data: dec.Data,
135+
}
129136
}
130137
}
131138
return err

‎core/types/receipt.go‎

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,18 @@ type receiptRLP struct {
7272
Logs []*Log
7373
}
7474

75+
// receiptStorageRLP is the storage encoding of a receipt.
7576
type receiptStorageRLP struct {
77+
PostStateOrStatus []byte
78+
CumulativeGasUsed uint64
79+
TxHash common.Hash
80+
ContractAddress common.Address
81+
Logs []*LogForStorage
82+
GasUsed uint64
83+
}
84+
85+
// LegacyReceiptStorageRLP is the previous storage encoding of a receipt including some unnecessary fields.
86+
type LegacyReceiptStorageRLP struct {
7687
PostStateOrStatus []byte
7788
CumulativeGasUsed uint64
7889
Bloom Bloom
@@ -159,7 +170,6 @@ func (r *ReceiptForStorage) EncodeRLP(w io.Writer) error {
159170
enc := &receiptStorageRLP{
160171
PostStateOrStatus: (*Receipt)(r).statusEncoding(),
161172
CumulativeGasUsed: r.CumulativeGasUsed,
162-
Bloom: r.Bloom,
163173
TxHash: r.TxHash,
164174
ContractAddress: r.ContractAddress,
165175
Logs: make([]*LogForStorage, len(r.Logs)),
@@ -176,17 +186,28 @@ func (r *ReceiptForStorage) EncodeRLP(w io.Writer) error {
176186
func (r *ReceiptForStorage) DecodeRLP(s *rlp.Stream) error {
177187
var dec receiptStorageRLP
178188
if err := s.Decode(&dec); err != nil {
179-
return err
189+
var sdec LegacyReceiptStorageRLP
190+
if err := s.Decode(&sdec); err != nil {
191+
return err
192+
}
193+
dec.PostStateOrStatus = common.CopyBytes(sdec.PostStateOrStatus)
194+
dec.CumulativeGasUsed = sdec.CumulativeGasUsed
195+
dec.TxHash = sdec.TxHash
196+
dec.ContractAddress = sdec.ContractAddress
197+
dec.Logs = sdec.Logs
198+
dec.GasUsed = sdec.GasUsed
180199
}
181200
if err := (*Receipt)(r).setStatus(dec.PostStateOrStatus); err != nil {
182201
return err
183202
}
184203
// Assign the consensus fields
185-
r.CumulativeGasUsed, r.Bloom = dec.CumulativeGasUsed, dec.Bloom
204+
r.CumulativeGasUsed = dec.CumulativeGasUsed
186205
r.Logs = make([]*Log, len(dec.Logs))
187206
for i, log := range dec.Logs {
188207
r.Logs[i] = (*Log)(log)
189208
}
209+
210+
r.Bloom = CreateBloom(Receipts{(*Receipt)(r)})
190211
// Assign the implementation fields
191212
r.TxHash, r.ContractAddress, r.GasUsed = dec.TxHash, dec.ContractAddress, dec.GasUsed
192213
return nil

‎eth/backend.go‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,16 +145,20 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) {
145145
bloomIndexer: NewBloomIndexer(chainDb, params.BloomBitsBlocks, params.BloomConfirms),
146146
}
147147

148-
log.Info("Initialising Ethereum protocol", "versions", ProtocolVersions, "network", config.NetworkId)
148+
bcVersion := rawdb.ReadDatabaseVersion(chainDb)
149+
var dbVer = "<nil>"
150+
if bcVersion != nil {
151+
dbVer = fmt.Sprintf("%d", *bcVersion)
152+
}
153+
log.Info("Initialising Ethereum protocol", "versions", ProtocolVersions, "network", config.NetworkId, "dbversion", dbVer)
149154

150155
if !config.SkipBcVersionCheck {
151-
bcVersion := rawdb.ReadDatabaseVersion(chainDb)
152156
if bcVersion != nil && *bcVersion > core.BlockChainVersion {
153157
return nil, fmt.Errorf("database version is v%d, Geth %s only supports v%d", *bcVersion, params.VersionWithMeta, core.BlockChainVersion)
154-
} else if bcVersion != nil && *bcVersion < core.BlockChainVersion {
155-
log.Warn("Upgrade blockchain database version", "from", *bcVersion, "to", core.BlockChainVersion)
158+
} else if bcVersion == nil || *bcVersion < core.BlockChainVersion {
159+
log.Warn("Upgrade blockchain database version", "from", dbVer, "to", core.BlockChainVersion)
160+
rawdb.WriteDatabaseVersion(chainDb, core.BlockChainVersion)
156161
}
157-
rawdb.WriteDatabaseVersion(chainDb, core.BlockChainVersion)
158162
}
159163
var (
160164
vmConfig = vm.Config{

0 commit comments

Comments
 (0)