trie, XDCx, XDCxlending: not expose db in trie (#1763)

This commit is contained in:
Daniel Liu 2025-11-15 19:19:47 +08:00 committed by GitHub
parent 8f0ad36af6
commit 0c96e76992
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 38 additions and 18 deletions

View file

@ -19,11 +19,10 @@ package tradingstate
import ( import (
"fmt" "fmt"
"github.com/XinFinOrg/XDPoSChain/ethdb"
"github.com/XinFinOrg/XDPoSChain/trie"
"github.com/XinFinOrg/XDPoSChain/common" "github.com/XinFinOrg/XDPoSChain/common"
"github.com/XinFinOrg/XDPoSChain/ethdb"
"github.com/XinFinOrg/XDPoSChain/log" "github.com/XinFinOrg/XDPoSChain/log"
"github.com/XinFinOrg/XDPoSChain/trie"
) )
// XDCXTrie wraps a trie with key hashing. In a secure trie, all // XDCXTrie wraps a trie with key hashing. In a secure trie, all
@ -146,7 +145,7 @@ func (t *XDCXTrie) GetKey(shaKey []byte) []byte {
if key, ok := t.getSecKeyCache()[string(shaKey)]; ok { if key, ok := t.getSecKeyCache()[string(shaKey)]; ok {
return key return key
} }
return t.trie.Db().Preimage(common.BytesToHash(shaKey)) return t.trie.Preimage(common.BytesToHash(shaKey))
} }
// Commit writes all nodes and the secure hash pre-images to the trie's database. // Commit writes all nodes and the secure hash pre-images to the trie's database.
@ -157,7 +156,7 @@ func (t *XDCXTrie) GetKey(shaKey []byte) []byte {
func (t *XDCXTrie) Commit(onleaf trie.LeafCallback) (common.Hash, error) { func (t *XDCXTrie) Commit(onleaf trie.LeafCallback) (common.Hash, error) {
// Write all the pre-images to the actual disk database // Write all the pre-images to the actual disk database
if len(t.getSecKeyCache()) > 0 { if len(t.getSecKeyCache()) > 0 {
t.trie.Db().InsertPreimage(t.secKeyCache) t.trie.InsertPreimage(t.secKeyCache)
t.secKeyCache = make(map[string][]byte) t.secKeyCache = make(map[string][]byte)
} }
// Commit the trie to its intermediate node database // Commit the trie to its intermediate node database
@ -169,7 +168,7 @@ func (t *XDCXTrie) Commit(onleaf trie.LeafCallback) (common.Hash, error) {
return common.Hash{}, err return common.Hash{}, err
} }
if nodes != nil { if nodes != nil {
if err := t.trie.Db().Update(trie.NewWithNodeSet(nodes)); err != nil { if err := t.trie.UpdateDb(trie.NewWithNodeSet(nodes)); err != nil {
return common.Hash{}, err return common.Hash{}, err
} }
} }

View file

@ -19,11 +19,10 @@ package lendingstate
import ( import (
"fmt" "fmt"
"github.com/XinFinOrg/XDPoSChain/ethdb"
"github.com/XinFinOrg/XDPoSChain/trie"
"github.com/XinFinOrg/XDPoSChain/common" "github.com/XinFinOrg/XDPoSChain/common"
"github.com/XinFinOrg/XDPoSChain/ethdb"
"github.com/XinFinOrg/XDPoSChain/log" "github.com/XinFinOrg/XDPoSChain/log"
"github.com/XinFinOrg/XDPoSChain/trie"
) )
// XDCXTrie wraps a trie with key hashing. In a secure trie, all // XDCXTrie wraps a trie with key hashing. In a secure trie, all
@ -142,7 +141,7 @@ func (t *XDCXTrie) GetKey(shaKey []byte) []byte {
if key, ok := t.getSecKeyCache()[string(shaKey)]; ok { if key, ok := t.getSecKeyCache()[string(shaKey)]; ok {
return key return key
} }
return t.trie.Db().Preimage(common.BytesToHash(shaKey)) return t.trie.Preimage(common.BytesToHash(shaKey))
} }
// Commit writes all nodes and the secure hash pre-images to the trie's database. // Commit writes all nodes and the secure hash pre-images to the trie's database.
@ -153,7 +152,7 @@ func (t *XDCXTrie) GetKey(shaKey []byte) []byte {
func (t *XDCXTrie) Commit(onleaf trie.LeafCallback) (common.Hash, error) { func (t *XDCXTrie) Commit(onleaf trie.LeafCallback) (common.Hash, error) {
// Write all the pre-images to the actual disk database // Write all the pre-images to the actual disk database
if len(t.getSecKeyCache()) > 0 { if len(t.getSecKeyCache()) > 0 {
t.trie.Db().InsertPreimage(t.secKeyCache) t.trie.InsertPreimage(t.secKeyCache)
t.secKeyCache = make(map[string][]byte) t.secKeyCache = make(map[string][]byte)
} }
// Commit the trie to its intermediate node database // Commit the trie to its intermediate node database
@ -165,7 +164,7 @@ func (t *XDCXTrie) Commit(onleaf trie.LeafCallback) (common.Hash, error) {
return common.Hash{}, err return common.Hash{}, err
} }
if nodes != nil { if nodes != nil {
if err := t.trie.Db().Update(trie.NewWithNodeSet(nodes)); err != nil { if err := t.trie.UpdateDb(trie.NewWithNodeSet(nodes)); err != nil {
return common.Hash{}, err return common.Hash{}, err
} }
} }

View file

@ -298,21 +298,23 @@ func NewDatabaseWithConfig(diskdb ethdb.KeyValueStore, config *Config) *Database
return db return db
} }
// Preimage retrieves a cached trie Node pre-image from memory. If it cannot be // preimage retrieves a cached trie Node pre-image from memory. If it cannot be
// found cached, the method queries the persistent database for the content. // found cached, the method queries the persistent database for the content.
func (db *Database) Preimage(hash common.Hash) []byte { // NOTE: preimage is only used by XDCx and XDCxlending
func (db *Database) preimage(hash common.Hash) []byte {
if db.preimages == nil { if db.preimages == nil {
return nil return nil
} }
return db.preimages.preimage(hash) return db.preimages.preimage(hash)
} }
func (db *Database) InsertPreimage(secKeyCache map[string][]byte) { // NOTE: insertPreimage is only used by XDCx and XDCxlending
func (db *Database) insertPreimage(secKeyCache map[string][]byte) {
if db.preimages == nil { if db.preimages == nil {
return return
} }
preimages := make(map[common.Hash][]byte) preimages := make(map[common.Hash][]byte, len(secKeyCache))
for hk, key := range secKeyCache { for hk, key := range secKeyCache {
preimages[common.BytesToHash([]byte(hk))] = key preimages[common.BytesToHash([]byte(hk))] = key
} }

View file

@ -76,8 +76,28 @@ func (t *Trie) newFlag() nodeFlag {
return nodeFlag{dirty: true} return nodeFlag{dirty: true}
} }
func (t *Trie) Db() *Database { // NOTE: Preimage is only used by XDCx and XDCxlending
return t.db func (t *Trie) Preimage(hash common.Hash) []byte {
if t.db == nil {
return nil
}
return t.db.preimage(hash)
}
// NOTE: InsertPreimage is only used by XDCx and XDCxlending
func (t *Trie) InsertPreimage(secKeyCache map[string][]byte) {
if t.db == nil {
return
}
t.db.insertPreimage(secKeyCache)
}
// NOTE: UpdateDb is only used by XDCx and XDCxlending
func (t *Trie) UpdateDb(nodes *MergedNodeSet) error {
if t.db == nil {
return errors.New("database is nil in trie")
}
return t.db.Update(nodes)
} }
// Copy returns a copy of Trie. // Copy returns a copy of Trie.