2020-05-15 07:52:45 +00:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-05-22 15:11:00 +00:00
|
|
|
"encoding/binary"
|
2020-05-15 07:52:45 +00:00
|
|
|
|
2020-05-22 15:11:00 +00:00
|
|
|
"github.com/VictoriaMetrics/fastcache"
|
2020-05-25 11:12:25 +00:00
|
|
|
"github.com/holiman/uint256"
|
2020-05-21 12:27:52 +00:00
|
|
|
|
2020-05-15 07:52:45 +00:00
|
|
|
"github.com/ledgerwatch/turbo-geth/common"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/common/changeset"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/common/dbutils"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/core/types/accounts"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/ethdb"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ WriterWithChangeSets = (*PlainStateWriter)(nil)
|
|
|
|
|
|
|
|
type PlainStateWriter struct {
|
2020-06-09 13:11:09 +00:00
|
|
|
db ethdb.Database
|
|
|
|
csw *ChangeSetWriter
|
|
|
|
blockNumber uint64
|
|
|
|
accountCache *fastcache.Cache
|
|
|
|
storageCache *fastcache.Cache
|
|
|
|
codeCache *fastcache.Cache
|
|
|
|
codeSizeCache *fastcache.Cache
|
2020-05-15 07:52:45 +00:00
|
|
|
}
|
|
|
|
|
2020-06-09 13:11:09 +00:00
|
|
|
func NewPlainStateWriter(db ethdb.Database, blockNumber uint64) *PlainStateWriter {
|
2020-05-15 07:52:45 +00:00
|
|
|
return &PlainStateWriter{
|
2020-06-09 13:11:09 +00:00
|
|
|
db: db,
|
|
|
|
csw: NewChangeSetWriterPlain(),
|
|
|
|
blockNumber: blockNumber,
|
2020-05-15 07:52:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-22 15:11:00 +00:00
|
|
|
func (w *PlainStateWriter) SetAccountCache(accountCache *fastcache.Cache) {
|
2020-05-21 12:27:52 +00:00
|
|
|
w.accountCache = accountCache
|
|
|
|
}
|
|
|
|
|
2020-05-22 15:11:00 +00:00
|
|
|
func (w *PlainStateWriter) SetStorageCache(storageCache *fastcache.Cache) {
|
2020-05-21 12:27:52 +00:00
|
|
|
w.storageCache = storageCache
|
|
|
|
}
|
|
|
|
|
2020-05-22 15:11:00 +00:00
|
|
|
func (w *PlainStateWriter) SetCodeCache(codeCache *fastcache.Cache) {
|
2020-05-21 12:27:52 +00:00
|
|
|
w.codeCache = codeCache
|
|
|
|
}
|
|
|
|
|
2020-05-22 15:11:00 +00:00
|
|
|
func (w *PlainStateWriter) SetCodeSizeCache(codeSizeCache *fastcache.Cache) {
|
2020-05-21 12:27:52 +00:00
|
|
|
w.codeSizeCache = codeSizeCache
|
|
|
|
}
|
|
|
|
|
2020-05-15 07:52:45 +00:00
|
|
|
func (w *PlainStateWriter) UpdateAccountData(ctx context.Context, address common.Address, original, account *accounts.Account) error {
|
|
|
|
if err := w.csw.UpdateAccountData(ctx, address, original, account); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
value := make([]byte, account.EncodingLengthForStorage())
|
|
|
|
account.EncodeForStorage(value)
|
2020-05-22 15:11:00 +00:00
|
|
|
if w.accountCache != nil {
|
|
|
|
w.accountCache.Set(address[:], value)
|
|
|
|
}
|
2020-06-09 13:11:09 +00:00
|
|
|
return w.db.Put(dbutils.PlainStateBucket, address[:], value)
|
2020-05-15 07:52:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *PlainStateWriter) UpdateAccountCode(address common.Address, incarnation uint64, codeHash common.Hash, code []byte) error {
|
|
|
|
if err := w.csw.UpdateAccountCode(address, incarnation, codeHash, code); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-05-21 12:27:52 +00:00
|
|
|
if w.codeCache != nil {
|
2020-05-22 15:11:00 +00:00
|
|
|
if len(code) <= 1024 {
|
|
|
|
w.codeCache.Set(address[:], code)
|
|
|
|
} else {
|
|
|
|
w.codeCache.Del(address[:])
|
|
|
|
}
|
2020-05-21 12:27:52 +00:00
|
|
|
}
|
|
|
|
if w.codeSizeCache != nil {
|
2020-05-22 15:11:00 +00:00
|
|
|
var b [4]byte
|
|
|
|
binary.BigEndian.PutUint32(b[:], uint32(len(code)))
|
|
|
|
w.codeSizeCache.Set(address[:], b[:])
|
2020-05-21 12:27:52 +00:00
|
|
|
}
|
2020-06-09 13:11:09 +00:00
|
|
|
if err := w.db.Put(dbutils.CodeBucket, codeHash[:], code); err != nil {
|
2020-05-15 07:52:45 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-06-09 13:11:09 +00:00
|
|
|
return w.db.Put(dbutils.PlainContractCodeBucket, dbutils.PlainGenerateStoragePrefix(address[:], incarnation), codeHash[:])
|
2020-05-15 07:52:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *PlainStateWriter) DeleteAccount(ctx context.Context, address common.Address, original *accounts.Account) error {
|
|
|
|
if err := w.csw.DeleteAccount(ctx, address, original); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-05-21 12:27:52 +00:00
|
|
|
if w.accountCache != nil {
|
2020-05-22 15:11:00 +00:00
|
|
|
w.accountCache.Set(address[:], nil)
|
2020-05-21 12:27:52 +00:00
|
|
|
}
|
2020-05-22 15:11:00 +00:00
|
|
|
if w.codeCache != nil {
|
|
|
|
w.codeCache.Set(address[:], nil)
|
2020-05-15 07:52:45 +00:00
|
|
|
}
|
2020-05-22 15:11:00 +00:00
|
|
|
if w.codeSizeCache != nil {
|
|
|
|
var b [4]byte
|
|
|
|
binary.BigEndian.PutUint32(b[:], 0)
|
|
|
|
w.codeSizeCache.Set(address[:], b[:])
|
|
|
|
}
|
2020-06-09 13:11:09 +00:00
|
|
|
if err := w.db.Delete(dbutils.PlainStateBucket, address[:]); err != nil {
|
2020-05-26 12:27:21 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if original.Incarnation > 0 {
|
|
|
|
var b [8]byte
|
|
|
|
binary.BigEndian.PutUint64(b[:], original.Incarnation)
|
2020-06-09 13:11:09 +00:00
|
|
|
if err := w.db.Put(dbutils.IncarnationMapBucket, address[:], b[:]); err != nil {
|
2020-05-26 12:27:21 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2020-05-15 07:52:45 +00:00
|
|
|
}
|
|
|
|
|
2020-05-25 11:12:25 +00:00
|
|
|
func (w *PlainStateWriter) WriteAccountStorage(ctx context.Context, address common.Address, incarnation uint64, key *common.Hash, original, value *uint256.Int) error {
|
2020-05-15 07:52:45 +00:00
|
|
|
if err := w.csw.WriteAccountStorage(ctx, address, incarnation, key, original, value); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if *original == *value {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
compositeKey := dbutils.PlainGenerateCompositeStorageKey(address, incarnation, *key)
|
|
|
|
|
2020-05-25 11:12:25 +00:00
|
|
|
v := value.Bytes()
|
2020-05-21 12:27:52 +00:00
|
|
|
if w.storageCache != nil {
|
2020-05-22 15:11:00 +00:00
|
|
|
w.storageCache.Set(compositeKey, v)
|
2020-05-21 12:27:52 +00:00
|
|
|
}
|
2020-05-15 07:52:45 +00:00
|
|
|
if len(v) == 0 {
|
2020-06-09 13:11:09 +00:00
|
|
|
return w.db.Delete(dbutils.PlainStateBucket, compositeKey)
|
2020-05-15 07:52:45 +00:00
|
|
|
}
|
2020-06-09 13:11:09 +00:00
|
|
|
return w.db.Put(dbutils.PlainStateBucket, compositeKey, v)
|
2020-05-15 07:52:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *PlainStateWriter) CreateContract(address common.Address) error {
|
2020-05-26 12:27:21 +00:00
|
|
|
if err := w.csw.CreateContract(address); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-06-09 13:11:09 +00:00
|
|
|
if err := w.db.Delete(dbutils.IncarnationMapBucket, address[:]); err != nil {
|
2020-05-26 12:27:21 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2020-05-15 07:52:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *PlainStateWriter) WriteChangeSets() error {
|
|
|
|
accountChanges, err := w.csw.GetAccountChanges()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var accountSerialised []byte
|
|
|
|
accountSerialised, err = changeset.EncodeAccountsPlain(accountChanges)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
key := dbutils.EncodeTimestamp(w.blockNumber)
|
2020-06-09 13:11:09 +00:00
|
|
|
if err = w.db.Put(dbutils.PlainAccountChangeSetBucket, key, accountSerialised); err != nil {
|
2020-05-15 07:52:45 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
storageChanges, err := w.csw.GetStorageChanges()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var storageSerialized []byte
|
|
|
|
if storageChanges.Len() > 0 {
|
|
|
|
storageSerialized, err = changeset.EncodeStoragePlain(storageChanges)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-06-09 13:11:09 +00:00
|
|
|
if err = w.db.Put(dbutils.PlainStorageChangeSetBucket, key, storageSerialized); err != nil {
|
2020-05-15 07:52:45 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2020-05-31 06:57:47 +00:00
|
|
|
|
|
|
|
func (w *PlainStateWriter) WriteHistory() error {
|
|
|
|
accountChanges, err := w.csw.GetAccountChanges()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-06-09 13:11:09 +00:00
|
|
|
err = writeIndex(w.blockNumber, accountChanges, dbutils.AccountsHistoryBucket, w.db)
|
2020-05-31 06:57:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
storageChanges, err := w.csw.GetStorageChanges()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-06-09 13:11:09 +00:00
|
|
|
err = writeIndex(w.blockNumber, storageChanges, dbutils.StorageHistoryBucket, w.db)
|
2020-05-31 06:57:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|