Skip to content

Commit af4080b

Browse files
committed
trie: fix concurrent usage of secKeyBuf, ref #20920
1 parent 00064dd commit af4080b

1 file changed

Lines changed: 25 additions & 11 deletions

File tree

‎trie/database.go‎

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,11 @@ var (
5959
// secureKeyPrefix is the database key prefix used to store trie node preimages.
6060
var secureKeyPrefix = []byte("secure-key-")
6161

62+
// secureKeyPrefixLength is the length of the above prefix
63+
const secureKeyPrefixLength = 11
64+
6265
// secureKeyLength is the length of the above prefix + 32byte hash.
63-
const secureKeyLength = 11 + 32
66+
const secureKeyLength = secureKeyPrefixLength + 32
6467

6568
// Database is an intermediate write layer between the trie data structures and
6669
// the disk database. The aim is to accumulate trie writes in-memory and only
@@ -79,7 +82,6 @@ type Database struct {
7982
newest common.Hash // Newest tracked node, flush-list tail
8083

8184
preimages map[common.Hash][]byte // Preimages of nodes from the secure trie
82-
seckeybuf [secureKeyLength]byte // Ephemeral buffer for calculating preimage keys
8385

8486
gctime time.Duration // Time spent on garbage collection since last commit
8587
gcnodes uint64 // Nodes garbage collected since last commit
@@ -445,15 +447,15 @@ func (db *Database) preimage(hash common.Hash) ([]byte, error) {
445447
return preimage, nil
446448
}
447449
// Content unavailable in memory, attempt to retrieve from disk
448-
return db.diskdb.Get(db.secureKey(hash[:]))
450+
return db.diskdb.Get(secureKey(hash))
449451
}
450452

451-
// secureKey returns the database key for the preimage of key, as an ephemeral
452-
// buffer. The caller must not hold onto the return value because it will become
453-
// invalid on the next call.
454-
func (db *Database) secureKey(key []byte) []byte {
455-
buf := append(db.seckeybuf[:0], secureKeyPrefix...)
456-
buf = append(buf, key...)
453+
// secureKey returns the database key for the preimage of key (as a newly
454+
// allocated byte-slice)
455+
func secureKey(hash common.Hash) []byte {
456+
buf := make([]byte, secureKeyLength)
457+
copy(buf, secureKeyPrefix)
458+
copy(buf[secureKeyPrefixLength:], hash[:])
457459
return buf
458460
}
459461

@@ -596,12 +598,18 @@ func (db *Database) Cap(limit common.StorageSize) error {
596598
size := db.dirtiesSize + common.StorageSize((len(db.dirties)-1)*cachedNodeSize)
597599
size += db.childrenSize - common.StorageSize(len(db.dirties[common.Hash{}].children)*(common.HashLength+2))
598600

601+
// We reuse an ephemeral buffer for the keys. The batch Put operation
602+
// copies it internally, so we can reuse it.
603+
var keyBuf [secureKeyLength]byte
604+
copy(keyBuf[:], secureKeyPrefix)
605+
599606
// If the preimage cache got large enough, push to disk. If it's still small
600607
// leave for later to deduplicate writes.
601608
flushPreimages := db.preimagesSize > 4*1024*1024
602609
if flushPreimages {
603610
for hash, preimage := range db.preimages {
604-
if err := batch.Put(db.secureKey(hash[:]), preimage); err != nil {
611+
copy(keyBuf[secureKeyPrefixLength:], hash[:])
612+
if err := batch.Put(keyBuf[:], preimage); err != nil {
605613
log.Error("Failed to commit preimage from trie database", "err", err)
606614
return err
607615
}
@@ -692,9 +700,15 @@ func (db *Database) Commit(node common.Hash, report bool) error {
692700
start := time.Now()
693701
batch := db.diskdb.NewBatch()
694702

703+
// We reuse an ephemeral buffer for the keys. The batch Put operation
704+
// copies it internally, so we can reuse it.
705+
var keyBuf [secureKeyLength]byte
706+
copy(keyBuf[:], secureKeyPrefix)
707+
695708
// Move all of the accumulated preimages into a write batch
696709
for hash, preimage := range db.preimages {
697-
if err := batch.Put(db.secureKey(hash[:]), preimage); err != nil {
710+
copy(keyBuf[secureKeyPrefixLength:], hash[:])
711+
if err := batch.Put(keyBuf[:], preimage); err != nil {
698712
log.Error("Failed to commit preimage from trie database", "err", err)
699713
return err
700714
}

0 commit comments

Comments
 (0)