Skip to content

Commit e9ba0b3

Browse files
author
Delweng Zheng
committed
core/rawdb: rewrite the wrapper, pass common.Hash
1 parent 863d755 commit e9ba0b3

File tree

4 files changed

+44
-44
lines changed

4 files changed

+44
-44
lines changed

core/rawdb/accessors_chain.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func DeleteCanonicalHash(db DatabaseDeleter, number uint64) {
5252

5353
// ReadHeaderNumber returns the header number assigned to a hash.
5454
func ReadHeaderNumber(db DatabaseReader, hash common.Hash) *uint64 {
55-
data, _ := db.Get(headerNumberKey(hash.Bytes()))
55+
data, _ := db.Get(headerNumberKey(hash))
5656
if len(data) != 8 {
5757
return nil
5858
}
@@ -128,13 +128,13 @@ func WriteFastTrieProgress(db DatabaseWriter, count uint64) {
128128

129129
// ReadHeaderRLP retrieves a block header in its raw RLP database encoding.
130130
func ReadHeaderRLP(db DatabaseReader, hash common.Hash, number uint64) rlp.RawValue {
131-
data, _ := db.Get(headerKey(number, hash.Bytes()))
131+
data, _ := db.Get(headerKey(number, hash))
132132
return data
133133
}
134134

135135
// HasHeader verifies the existence of a block header corresponding to the hash.
136136
func HasHeader(db DatabaseReader, hash common.Hash, number uint64) bool {
137-
if has, err := db.Has(headerKey(number, hash.Bytes())); !has || err != nil {
137+
if has, err := db.Has(headerKey(number, hash)); !has || err != nil {
138138
return false
139139
}
140140
return true
@@ -159,7 +159,7 @@ func ReadHeader(db DatabaseReader, hash common.Hash, number uint64) *types.Heade
159159
func WriteHeader(db DatabaseWriter, header *types.Header) {
160160
// Write the hash -> number mapping
161161
var (
162-
hash = header.Hash().Bytes()
162+
hash = header.Hash()
163163
number = header.Number.Uint64()
164164
encoded = encodeBlockNumber(number)
165165
)
@@ -180,30 +180,30 @@ func WriteHeader(db DatabaseWriter, header *types.Header) {
180180

181181
// DeleteHeader removes all block header data associated with a hash.
182182
func DeleteHeader(db DatabaseDeleter, hash common.Hash, number uint64) {
183-
if err := db.Delete(headerKey(number, hash.Bytes())); err != nil {
183+
if err := db.Delete(headerKey(number, hash)); err != nil {
184184
log.Crit("Failed to delete header", "err", err)
185185
}
186-
if err := db.Delete(headerNumberKey(hash.Bytes())); err != nil {
186+
if err := db.Delete(headerNumberKey(hash)); err != nil {
187187
log.Crit("Failed to delete hash to number mapping", "err", err)
188188
}
189189
}
190190

191191
// ReadBodyRLP retrieves the block body (transactions and uncles) in RLP encoding.
192192
func ReadBodyRLP(db DatabaseReader, hash common.Hash, number uint64) rlp.RawValue {
193-
data, _ := db.Get(blockBodyKey(number, hash.Bytes()))
193+
data, _ := db.Get(blockBodyKey(number, hash))
194194
return data
195195
}
196196

197197
// WriteBodyRLP stores an RLP encoded block body into the database.
198198
func WriteBodyRLP(db DatabaseWriter, hash common.Hash, number uint64, rlp rlp.RawValue) {
199-
if err := db.Put(blockBodyKey(number, hash.Bytes()), rlp); err != nil {
199+
if err := db.Put(blockBodyKey(number, hash), rlp); err != nil {
200200
log.Crit("Failed to store block body", "err", err)
201201
}
202202
}
203203

204204
// HasBody verifies the existence of a block body corresponding to the hash.
205205
func HasBody(db DatabaseReader, hash common.Hash, number uint64) bool {
206-
if has, err := db.Has(blockBodyKey(number, hash.Bytes())); !has || err != nil {
206+
if has, err := db.Has(blockBodyKey(number, hash)); !has || err != nil {
207207
return false
208208
}
209209
return true
@@ -234,14 +234,14 @@ func WriteBody(db DatabaseWriter, hash common.Hash, number uint64, body *types.B
234234

235235
// DeleteBody removes all block body data associated with a hash.
236236
func DeleteBody(db DatabaseDeleter, hash common.Hash, number uint64) {
237-
if err := db.Delete(blockBodyKey(number, hash.Bytes())); err != nil {
237+
if err := db.Delete(blockBodyKey(number, hash)); err != nil {
238238
log.Crit("Failed to delete block body", "err", err)
239239
}
240240
}
241241

242242
// ReadTd retrieves a block's total difficulty corresponding to the hash.
243243
func ReadTd(db DatabaseReader, hash common.Hash, number uint64) *big.Int {
244-
data, _ := db.Get(headerTDKey(number, hash.Bytes()))
244+
data, _ := db.Get(headerTDKey(number, hash))
245245
if len(data) == 0 {
246246
return nil
247247
}
@@ -259,22 +259,22 @@ func WriteTd(db DatabaseWriter, hash common.Hash, number uint64, td *big.Int) {
259259
if err != nil {
260260
log.Crit("Failed to RLP encode block total difficulty", "err", err)
261261
}
262-
if err := db.Put(headerTDKey(number, hash.Bytes()), data); err != nil {
262+
if err := db.Put(headerTDKey(number, hash), data); err != nil {
263263
log.Crit("Failed to store block total difficulty", "err", err)
264264
}
265265
}
266266

267267
// DeleteTd removes all block total difficulty data associated with a hash.
268268
func DeleteTd(db DatabaseDeleter, hash common.Hash, number uint64) {
269-
if err := db.Delete(headerTDKey(number, hash.Bytes())); err != nil {
269+
if err := db.Delete(headerTDKey(number, hash)); err != nil {
270270
log.Crit("Failed to delete block total difficulty", "err", err)
271271
}
272272
}
273273

274274
// ReadReceipts retrieves all the transaction receipts belonging to a block.
275275
func ReadReceipts(db DatabaseReader, hash common.Hash, number uint64) types.Receipts {
276276
// Retrieve the flattened receipt slice
277-
data, _ := db.Get(blockReceiptsKey(number, hash.Bytes()))
277+
data, _ := db.Get(blockReceiptsKey(number, hash))
278278
if len(data) == 0 {
279279
return nil
280280
}
@@ -303,14 +303,14 @@ func WriteReceipts(db DatabaseWriter, hash common.Hash, number uint64, receipts
303303
log.Crit("Failed to encode block receipts", "err", err)
304304
}
305305
// Store the flattened receipt slice
306-
if err := db.Put(blockReceiptsKey(number, hash.Bytes()), bytes); err != nil {
306+
if err := db.Put(blockReceiptsKey(number, hash), bytes); err != nil {
307307
log.Crit("Failed to store block receipts", "err", err)
308308
}
309309
}
310310

311311
// DeleteReceipts removes all receipt data associated with a block hash.
312312
func DeleteReceipts(db DatabaseDeleter, hash common.Hash, number uint64) {
313-
if err := db.Delete(blockReceiptsKey(number, hash.Bytes())); err != nil {
313+
if err := db.Delete(blockReceiptsKey(number, hash)); err != nil {
314314
log.Crit("Failed to delete block receipts", "err", err)
315315
}
316316
}

core/rawdb/accessors_indexes.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626
// ReadTxLookupEntry retrieves the positional metadata associated with a transaction
2727
// hash to allow retrieving the transaction or receipt by hash.
2828
func ReadTxLookupEntry(db DatabaseReader, hash common.Hash) (common.Hash, uint64, uint64) {
29-
data, _ := db.Get(txLookupKey(hash.Bytes()))
29+
data, _ := db.Get(txLookupKey(hash))
3030
if len(data) == 0 {
3131
return common.Hash{}, 0, 0
3232
}
@@ -51,15 +51,15 @@ func WriteTxLookupEntries(db DatabaseWriter, block *types.Block) {
5151
if err != nil {
5252
log.Crit("Failed to encode transaction lookup entry", "err", err)
5353
}
54-
if err := db.Put(txLookupKey(tx.Hash().Bytes()), data); err != nil {
54+
if err := db.Put(txLookupKey(tx.Hash()), data); err != nil {
5555
log.Crit("Failed to store transaction lookup entry", "err", err)
5656
}
5757
}
5858
}
5959

6060
// DeleteTxLookupEntry removes all transaction data associated with a hash.
6161
func DeleteTxLookupEntry(db DatabaseDeleter, hash common.Hash) {
62-
db.Delete(txLookupKey(hash.Bytes()))
62+
db.Delete(txLookupKey(hash))
6363
}
6464

6565
// ReadTransaction retrieves a specific transaction from the database, along with
@@ -95,13 +95,13 @@ func ReadReceipt(db DatabaseReader, hash common.Hash) (*types.Receipt, common.Ha
9595
// ReadBloomBits retrieves the compressed bloom bit vector belonging to the given
9696
// section and bit index from the.
9797
func ReadBloomBits(db DatabaseReader, bit uint, section uint64, head common.Hash) ([]byte, error) {
98-
return db.Get(bloomBitsKey(bit, section, head.Bytes()))
98+
return db.Get(bloomBitsKey(bit, section, head))
9999
}
100100

101101
// WriteBloomBits stores the compressed bloom bits vector belonging to the given
102102
// section and bit index.
103103
func WriteBloomBits(db DatabaseWriter, bit uint, section uint64, head common.Hash, bits []byte) {
104-
if err := db.Put(bloomBitsKey(bit, section, head.Bytes()), bits); err != nil {
104+
if err := db.Put(bloomBitsKey(bit, section, head), bits); err != nil {
105105
log.Crit("Failed to store bloom bits", "err", err)
106106
}
107107
}

core/rawdb/accessors_metadata.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func WriteDatabaseVersion(db DatabaseWriter, version int) {
4545

4646
// ReadChainConfig retrieves the consensus settings based on the given genesis hash.
4747
func ReadChainConfig(db DatabaseReader, hash common.Hash) *params.ChainConfig {
48-
data, _ := db.Get(configKey(hash.Bytes()))
48+
data, _ := db.Get(configKey(hash))
4949
if len(data) == 0 {
5050
return nil
5151
}
@@ -66,22 +66,22 @@ func WriteChainConfig(db DatabaseWriter, hash common.Hash, cfg *params.ChainConf
6666
if err != nil {
6767
log.Crit("Failed to JSON encode chain config", "err", err)
6868
}
69-
if err := db.Put(configKey(hash.Bytes()), data); err != nil {
69+
if err := db.Put(configKey(hash), data); err != nil {
7070
log.Crit("Failed to store chain config", "err", err)
7171
}
7272
}
7373

7474
// ReadPreimage retrieves a single preimage of the provided hash.
7575
func ReadPreimage(db DatabaseReader, hash common.Hash) []byte {
76-
data, _ := db.Get(preimageKey(hash.Bytes()))
76+
data, _ := db.Get(preimageKey(hash))
7777
return data
7878
}
7979

8080
// WritePreimages writes the provided set of preimages to the database. `number` is the
8181
// current block number, and is used for debug messages only.
8282
func WritePreimages(db DatabaseWriter, number uint64, preimages map[common.Hash][]byte) {
8383
for hash, preimage := range preimages {
84-
if err := db.Put(preimageKey(hash.Bytes()), preimage); err != nil {
84+
if err := db.Put(preimageKey(hash), preimage); err != nil {
8585
log.Crit("Failed to store trie preimage", "err", err)
8686
}
8787
}

core/rawdb/schema.go

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -79,43 +79,43 @@ func encodeBlockNumber(number uint64) []byte {
7979
}
8080

8181
// headerKey = headerPrefix + num (uint64 big endian) + hash
82-
func headerKey(number uint64, suffix []byte) []byte {
83-
return append(append(headerPrefix, encodeBlockNumber(number)...), suffix...)
82+
func headerKey(number uint64, hash common.Hash) []byte {
83+
return append(append(headerPrefix, encodeBlockNumber(number)...), hash.Bytes()...)
8484
}
8585

8686
// headerTDKey = headerPrefix + num (uint64 big endian) + hash + headerTDSuffix
87-
func headerTDKey(number uint64, hashBytes []byte) []byte {
88-
return append(headerKey(number, hashBytes), headerTDSuffix...)
87+
func headerTDKey(number uint64, hash common.Hash) []byte {
88+
return append(headerKey(number, hash), headerTDSuffix...)
8989
}
9090

9191
// headerHashKey = headerPrefix + num (uint64 big endian) + headerHashSuffix
9292
func headerHashKey(number uint64) []byte {
93-
return headerKey(number, headerHashSuffix)
93+
return append(append(headerPrefix, encodeBlockNumber(number)...), headerHashSuffix...)
9494
}
9595

9696
// headerNumberKey = headerNumberPrefix + hash
97-
func headerNumberKey(hashBytes []byte) []byte {
98-
return append(headerNumberPrefix, hashBytes...)
97+
func headerNumberKey(hash common.Hash) []byte {
98+
return append(headerNumberPrefix, hash.Bytes()...)
9999
}
100100

101101
// blockBodyKey = blockBodyPrefix + num (uint64 big endian) + hash
102-
func blockBodyKey(number uint64, hashBytes []byte) []byte {
103-
return append(append(blockBodyPrefix, encodeBlockNumber(number)...), hashBytes...)
102+
func blockBodyKey(number uint64, hash common.Hash) []byte {
103+
return append(append(blockBodyPrefix, encodeBlockNumber(number)...), hash.Bytes()...)
104104
}
105105

106106
// blockReceiptsKey = blockReceiptsPrefix + num (uint64 big endian) + hash
107-
func blockReceiptsKey(number uint64, hashBytes []byte) []byte {
108-
return append(append(blockReceiptsPrefix, encodeBlockNumber(number)...), hashBytes...)
107+
func blockReceiptsKey(number uint64, hash common.Hash) []byte {
108+
return append(append(blockReceiptsPrefix, encodeBlockNumber(number)...), hash.Bytes()...)
109109
}
110110

111111
// txLookupKey = txLookupPrefix + hash
112-
func txLookupKey(hashBytes []byte) []byte {
113-
return append(txLookupPrefix, hashBytes...)
112+
func txLookupKey(hash common.Hash) []byte {
113+
return append(txLookupPrefix, hash.Bytes()...)
114114
}
115115

116116
// bloomBitsKey = bloomBitsPrefix + bit (uint16 big endian) + section (uint64 big endian) + hash
117-
func bloomBitsKey(bit uint, section uint64, hashBytes []byte) []byte {
118-
key := append(append(bloomBitsPrefix, make([]byte, 10)...), hashBytes...)
117+
func bloomBitsKey(bit uint, section uint64, hash common.Hash) []byte {
118+
key := append(append(bloomBitsPrefix, make([]byte, 10)...), hash.Bytes()...)
119119

120120
binary.BigEndian.PutUint16(key[1:], uint16(bit))
121121
binary.BigEndian.PutUint64(key[3:], section)
@@ -124,11 +124,11 @@ func bloomBitsKey(bit uint, section uint64, hashBytes []byte) []byte {
124124
}
125125

126126
// preimageKey = preimagePrefix + hash
127-
func preimageKey(hashBytes []byte) []byte {
128-
return append(preimagePrefix, hashBytes...)
127+
func preimageKey(hash common.Hash) []byte {
128+
return append(preimagePrefix, hash.Bytes()...)
129129
}
130130

131131
// configKey = configPrefix + hash
132-
func configKey(hashBytes []byte) []byte {
133-
return append(configPrefix, hashBytes...)
132+
func configKey(hash common.Hash) []byte {
133+
return append(configPrefix, hash.Bytes()...)
134134
}

0 commit comments

Comments
 (0)