Skip to content

Commit

Permalink
trie: trie: fix concurrent usage of secKeyBuf
Browse files Browse the repository at this point in the history
  • Loading branch information
rameight committed Dec 27, 2022
1 parent 10db710 commit c70b519
Showing 1 changed file with 25 additions and 11 deletions.
36 changes: 25 additions & 11 deletions trie/trie_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,11 @@ var (
// secureKeyPrefix is the database key prefix used to store trie node preimages.
var secureKeyPrefix = []byte("secure-key-")

// secureKeyPrefixLength is the length of the above prefix
const secureKeyPrefixLength = 11

// secureKeyLength is the length of the above prefix + 32byte hash.
const secureKeyLength = 11 + 32
const secureKeyLength = secureKeyPrefixLength + 32

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

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

gctime time.Duration // Time spent on garbage collection since last commit
gcnodes uint64 // Nodes garbage collected since last commit
Expand Down Expand Up @@ -445,15 +447,15 @@ func (db *TrieDatabase) preimage(hash common.Hash) ([]byte, error) {
return preimage, nil
}
// Content unavailable in memory, attempt to retrieve from disk
return db.diskdb.Get(db.secureKey(hash[:]))
return db.diskdb.Get(secureKey(hash))
}

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

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

// We reuse an ephemeral buffer for the keys. The batch Put operation
// copies it internally, so we can reuse it.
var keyBuf [secureKeyLength]byte
copy(keyBuf[:], secureKeyPrefix)

// If the preimage cache got large enough, push to disk. If it's still small
// leave for later to deduplicate writes.
flushPreimages := db.preimagesSize > 4*1024*1024
if flushPreimages {
for hash, preimage := range db.preimages {
if err := batch.Put(db.secureKey(hash[:]), preimage); err != nil {
copy(keyBuf[secureKeyPrefixLength:], hash[:])
if err := batch.Put(keyBuf[:], preimage); err != nil {
log.Error("Failed to commit preimage from trie database", "err", err)
return err
}
Expand Down Expand Up @@ -692,9 +700,15 @@ func (db *TrieDatabase) Commit(node common.Hash, report bool) error {
start := time.Now()
batch := db.diskdb.NewBatch()

// We reuse an ephemeral buffer for the keys. The batch Put operation
// copies it internally, so we can reuse it.
var keyBuf [secureKeyLength]byte
copy(keyBuf[:], secureKeyPrefix)

// Move all of the accumulated preimages into a write batch
for hash, preimage := range db.preimages {
if err := batch.Put(db.secureKey(hash[:]), preimage); err != nil {
copy(keyBuf[secureKeyPrefixLength:], hash[:])
if err := batch.Put(keyBuf[:], preimage); err != nil {
log.Error("Failed to commit preimage from trie database", "err", err)
return err
}
Expand Down

0 comments on commit c70b519

Please sign in to comment.