mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-05-17 13:36:37 +00:00
trie: delete secKeyCacheOwner (#31785)
The optimization tried to defer allocating the cache map until it was used for the first time. It's a relic from earlier times, when tries were copied often. This seems unnecessary now, so we can just create the map when the trie is created. --------- Co-authored-by: Felix Lange <fjl@twurst.com>
This commit is contained in:
parent
d4462828e9
commit
09289fd154
1 changed files with 19 additions and 27 deletions
|
|
@ -64,11 +64,10 @@ func NewSecure(stateRoot common.Hash, owner common.Hash, root common.Hash, db da
|
||||||
//
|
//
|
||||||
// StateTrie is not safe for concurrent use.
|
// StateTrie is not safe for concurrent use.
|
||||||
type StateTrie struct {
|
type StateTrie struct {
|
||||||
trie Trie
|
trie Trie
|
||||||
db database.NodeDatabase
|
db database.NodeDatabase
|
||||||
preimages preimageStore
|
preimages preimageStore
|
||||||
secKeyCache map[common.Hash][]byte
|
secKeyCache map[common.Hash][]byte
|
||||||
secKeyCacheOwner *StateTrie // Pointer to self, replace the key cache on mismatch
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewStateTrie creates a trie with an existing root node from a backing database.
|
// NewStateTrie creates a trie with an existing root node from a backing database.
|
||||||
|
|
@ -84,7 +83,11 @@ func NewStateTrie(id *ID, db database.NodeDatabase) (*StateTrie, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
tr := &StateTrie{trie: *trie, db: db}
|
tr := &StateTrie{
|
||||||
|
trie: *trie,
|
||||||
|
db: db,
|
||||||
|
secKeyCache: make(map[common.Hash][]byte),
|
||||||
|
}
|
||||||
|
|
||||||
// link the preimage store if it's supported
|
// link the preimage store if it's supported
|
||||||
if preimages, ok := db.(preimageStore); ok && preimages.PreimageEnabled() {
|
if preimages, ok := db.(preimageStore); ok && preimages.PreimageEnabled() {
|
||||||
|
|
@ -162,7 +165,7 @@ func (t *StateTrie) MustUpdate(key, value []byte) {
|
||||||
hk := crypto.Keccak256(key)
|
hk := crypto.Keccak256(key)
|
||||||
t.trie.MustUpdate(hk, value)
|
t.trie.MustUpdate(hk, value)
|
||||||
if t.preimages != nil {
|
if t.preimages != nil {
|
||||||
t.getSecKeyCache()[common.Hash(hk)] = common.CopyBytes(key)
|
t.secKeyCache[common.Hash(hk)] = common.CopyBytes(key)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -182,7 +185,7 @@ func (t *StateTrie) UpdateStorage(_ common.Address, key, value []byte) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if t.preimages != nil {
|
if t.preimages != nil {
|
||||||
t.getSecKeyCache()[common.Hash(hk)] = common.CopyBytes(key)
|
t.secKeyCache[common.Hash(hk)] = common.CopyBytes(key)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
@ -198,7 +201,7 @@ func (t *StateTrie) UpdateAccount(address common.Address, acc *types.StateAccoun
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if t.preimages != nil {
|
if t.preimages != nil {
|
||||||
t.getSecKeyCache()[common.Hash(hk)] = address.Bytes()
|
t.secKeyCache[common.Hash(hk)] = address.Bytes()
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
@ -212,7 +215,7 @@ func (t *StateTrie) UpdateContractCode(_ common.Address, _ common.Hash, _ []byte
|
||||||
func (t *StateTrie) MustDelete(key []byte) {
|
func (t *StateTrie) MustDelete(key []byte) {
|
||||||
hk := crypto.Keccak256(key)
|
hk := crypto.Keccak256(key)
|
||||||
if t.preimages != nil {
|
if t.preimages != nil {
|
||||||
delete(t.getSecKeyCache(), common.Hash(hk))
|
delete(t.secKeyCache, common.Hash(hk))
|
||||||
}
|
}
|
||||||
t.trie.MustDelete(hk)
|
t.trie.MustDelete(hk)
|
||||||
}
|
}
|
||||||
|
|
@ -223,7 +226,7 @@ func (t *StateTrie) MustDelete(key []byte) {
|
||||||
func (t *StateTrie) DeleteStorage(_ common.Address, key []byte) error {
|
func (t *StateTrie) DeleteStorage(_ common.Address, key []byte) error {
|
||||||
hk := crypto.Keccak256(key)
|
hk := crypto.Keccak256(key)
|
||||||
if t.preimages != nil {
|
if t.preimages != nil {
|
||||||
delete(t.getSecKeyCache(), common.Hash(hk))
|
delete(t.secKeyCache, common.Hash(hk))
|
||||||
}
|
}
|
||||||
return t.trie.Delete(hk)
|
return t.trie.Delete(hk)
|
||||||
}
|
}
|
||||||
|
|
@ -232,7 +235,7 @@ func (t *StateTrie) DeleteStorage(_ common.Address, key []byte) error {
|
||||||
func (t *StateTrie) DeleteAccount(address common.Address) error {
|
func (t *StateTrie) DeleteAccount(address common.Address) error {
|
||||||
hk := crypto.Keccak256(address.Bytes())
|
hk := crypto.Keccak256(address.Bytes())
|
||||||
if t.preimages != nil {
|
if t.preimages != nil {
|
||||||
delete(t.getSecKeyCache(), common.Hash(hk))
|
delete(t.secKeyCache, common.Hash(hk))
|
||||||
}
|
}
|
||||||
return t.trie.Delete(hk)
|
return t.trie.Delete(hk)
|
||||||
}
|
}
|
||||||
|
|
@ -243,7 +246,7 @@ func (t *StateTrie) GetKey(shaKey []byte) []byte {
|
||||||
if t.preimages == nil {
|
if t.preimages == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if key, ok := t.getSecKeyCache()[common.BytesToHash(shaKey)]; ok {
|
if key, ok := t.secKeyCache[common.BytesToHash(shaKey)]; ok {
|
||||||
return key
|
return key
|
||||||
}
|
}
|
||||||
return t.preimages.Preimage(common.BytesToHash(shaKey))
|
return t.preimages.Preimage(common.BytesToHash(shaKey))
|
||||||
|
|
@ -263,11 +266,11 @@ func (t *StateTrie) Witness() map[string]struct{} {
|
||||||
// be created with new root and updated trie database for following usage
|
// be created with new root and updated trie database for following usage
|
||||||
func (t *StateTrie) Commit(collectLeaf bool) (common.Hash, *trienode.NodeSet) {
|
func (t *StateTrie) Commit(collectLeaf bool) (common.Hash, *trienode.NodeSet) {
|
||||||
// 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.secKeyCache) > 0 {
|
||||||
if t.preimages != nil {
|
if t.preimages != nil {
|
||||||
t.preimages.InsertPreimage(t.secKeyCache)
|
t.preimages.InsertPreimage(t.secKeyCache)
|
||||||
}
|
}
|
||||||
t.secKeyCache = make(map[common.Hash][]byte)
|
clear(t.secKeyCache)
|
||||||
}
|
}
|
||||||
// Commit the trie and return its modified nodeset.
|
// Commit the trie and return its modified nodeset.
|
||||||
return t.trie.Commit(collectLeaf)
|
return t.trie.Commit(collectLeaf)
|
||||||
|
|
@ -284,7 +287,7 @@ func (t *StateTrie) Copy() *StateTrie {
|
||||||
return &StateTrie{
|
return &StateTrie{
|
||||||
trie: *t.trie.Copy(),
|
trie: *t.trie.Copy(),
|
||||||
db: t.db,
|
db: t.db,
|
||||||
secKeyCache: t.secKeyCache,
|
secKeyCache: make(map[common.Hash][]byte),
|
||||||
preimages: t.preimages,
|
preimages: t.preimages,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -301,17 +304,6 @@ func (t *StateTrie) MustNodeIterator(start []byte) NodeIterator {
|
||||||
return t.trie.MustNodeIterator(start)
|
return t.trie.MustNodeIterator(start)
|
||||||
}
|
}
|
||||||
|
|
||||||
// getSecKeyCache returns the current secure key cache, creating a new one if
|
|
||||||
// ownership changed (i.e. the current secure trie is a copy of another owning
|
|
||||||
// the actual cache).
|
|
||||||
func (t *StateTrie) getSecKeyCache() map[common.Hash][]byte {
|
|
||||||
if t != t.secKeyCacheOwner {
|
|
||||||
t.secKeyCacheOwner = t
|
|
||||||
t.secKeyCache = make(map[common.Hash][]byte)
|
|
||||||
}
|
|
||||||
return t.secKeyCache
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *StateTrie) IsVerkle() bool {
|
func (t *StateTrie) IsVerkle() bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue