2020-12-08 09:44:29 +00:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/holiman/uint256"
|
2023-06-14 03:01:00 +00:00
|
|
|
"github.com/ledgerwatch/erigon-lib/common"
|
2023-01-13 18:12:18 +00:00
|
|
|
|
2021-05-20 18:25:53 +00:00
|
|
|
"github.com/ledgerwatch/erigon/core/types/accounts"
|
|
|
|
"github.com/ledgerwatch/erigon/turbo/shards"
|
2020-12-08 09:44:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// CachedWriter is a wrapper for an instance of type StateWriter
|
|
|
|
type CachedWriter struct {
|
|
|
|
w WriterWithChangeSets
|
|
|
|
cache *shards.StateCache
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewCachedWriter wraps a given state writer into a cached writer
|
|
|
|
func NewCachedWriter(w WriterWithChangeSets, cache *shards.StateCache) *CachedWriter {
|
|
|
|
return &CachedWriter{w: w, cache: cache}
|
|
|
|
}
|
|
|
|
|
2023-06-14 03:01:00 +00:00
|
|
|
func (cw *CachedWriter) UpdateAccountData(address common.Address, original, account *accounts.Account) error {
|
2021-07-05 08:42:44 +00:00
|
|
|
if err := cw.w.UpdateAccountData(address, original, account); err != nil {
|
2020-12-08 09:44:29 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
cw.cache.SetAccountWrite(address.Bytes(), account)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-06-14 03:01:00 +00:00
|
|
|
func (cw *CachedWriter) UpdateAccountCode(address common.Address, incarnation uint64, codeHash common.Hash, code []byte) error {
|
2020-12-08 09:44:29 +00:00
|
|
|
if err := cw.w.UpdateAccountCode(address, incarnation, codeHash, code); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cw.cache.SetCodeWrite(address.Bytes(), incarnation, code)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-06-14 03:01:00 +00:00
|
|
|
func (cw *CachedWriter) DeleteAccount(address common.Address, original *accounts.Account) error {
|
2021-07-05 08:42:44 +00:00
|
|
|
if err := cw.w.DeleteAccount(address, original); err != nil {
|
2020-12-08 09:44:29 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
cw.cache.SetAccountDelete(address.Bytes())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-06-14 03:01:00 +00:00
|
|
|
func (cw *CachedWriter) WriteAccountStorage(address common.Address, incarnation uint64, key *common.Hash, original, value *uint256.Int) error {
|
2021-07-05 08:42:44 +00:00
|
|
|
if err := cw.w.WriteAccountStorage(address, incarnation, key, original, value); err != nil {
|
2020-12-08 09:44:29 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if *original == *value {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if value.IsZero() {
|
|
|
|
cw.cache.SetStorageDelete(address.Bytes(), incarnation, key.Bytes())
|
|
|
|
} else {
|
|
|
|
cw.cache.SetStorageWrite(address.Bytes(), incarnation, key.Bytes(), value.Bytes())
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-06-14 03:01:00 +00:00
|
|
|
func (cw *CachedWriter) CreateContract(address common.Address) error {
|
2020-12-08 09:44:29 +00:00
|
|
|
return cw.w.CreateContract(address)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cw *CachedWriter) WriteChangeSets() error {
|
|
|
|
return cw.w.WriteChangeSets()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cw *CachedWriter) WriteHistory() error {
|
|
|
|
return cw.w.WriteHistory()
|
|
|
|
}
|