2019-12-20 12:25:40 +00:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-05-22 15:11:00 +00:00
|
|
|
"encoding/binary"
|
2020-04-09 17:23:29 +00:00
|
|
|
"fmt"
|
2020-03-11 10:31:49 +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
|
|
|
|
2019-12-20 12:25:40 +00:00
|
|
|
"github.com/ledgerwatch/turbo-geth/common"
|
2020-04-09 17:23:29 +00:00
|
|
|
"github.com/ledgerwatch/turbo-geth/common/changeset"
|
2019-12-20 12:25:40 +00:00
|
|
|
"github.com/ledgerwatch/turbo-geth/common/dbutils"
|
2020-04-18 20:09:44 +00:00
|
|
|
"github.com/ledgerwatch/turbo-geth/core/rawdb"
|
2019-12-20 12:25:40 +00:00
|
|
|
"github.com/ledgerwatch/turbo-geth/core/types/accounts"
|
2020-04-09 17:23:29 +00:00
|
|
|
"github.com/ledgerwatch/turbo-geth/ethdb"
|
2019-12-20 12:25:40 +00:00
|
|
|
"github.com/ledgerwatch/turbo-geth/trie"
|
|
|
|
)
|
|
|
|
|
2020-05-15 07:52:45 +00:00
|
|
|
var _ WriterWithChangeSets = (*DbStateWriter)(nil)
|
|
|
|
|
2020-05-26 12:27:21 +00:00
|
|
|
func NewDbStateWriter(stateDb, changeDb ethdb.Database, blockNr uint64) *DbStateWriter {
|
2020-04-26 16:02:38 +00:00
|
|
|
return &DbStateWriter{
|
2020-05-21 12:27:52 +00:00
|
|
|
stateDb: stateDb,
|
|
|
|
changeDb: changeDb,
|
2020-05-08 04:52:55 +00:00
|
|
|
blockNr: blockNr,
|
2020-05-21 12:27:52 +00:00
|
|
|
pw: &PreimageWriter{db: stateDb, savePreimages: false},
|
2020-05-08 04:52:55 +00:00
|
|
|
csw: NewChangeSetWriter(),
|
2020-04-26 16:02:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-20 12:25:40 +00:00
|
|
|
type DbStateWriter struct {
|
2020-05-21 12:27:52 +00:00
|
|
|
stateDb ethdb.Database
|
|
|
|
changeDb ethdb.Database
|
2020-05-08 04:52:55 +00:00
|
|
|
pw *PreimageWriter
|
|
|
|
blockNr uint64
|
|
|
|
csw *ChangeSetWriter
|
2020-05-22 15:11:00 +00:00
|
|
|
accountCache *fastcache.Cache
|
|
|
|
storageCache *fastcache.Cache
|
|
|
|
codeCache *fastcache.Cache
|
|
|
|
codeSizeCache *fastcache.Cache
|
2020-05-21 12:27:52 +00:00
|
|
|
}
|
|
|
|
|
2020-05-22 15:11:00 +00:00
|
|
|
func (dsw *DbStateWriter) SetAccountCache(accountCache *fastcache.Cache) {
|
2020-05-21 12:27:52 +00:00
|
|
|
dsw.accountCache = accountCache
|
|
|
|
}
|
|
|
|
|
2020-05-22 15:11:00 +00:00
|
|
|
func (dsw *DbStateWriter) SetStorageCache(storageCache *fastcache.Cache) {
|
2020-05-21 12:27:52 +00:00
|
|
|
dsw.storageCache = storageCache
|
|
|
|
}
|
|
|
|
|
2020-05-22 15:11:00 +00:00
|
|
|
func (dsw *DbStateWriter) SetCodeCache(codeCache *fastcache.Cache) {
|
2020-05-21 12:27:52 +00:00
|
|
|
dsw.codeCache = codeCache
|
|
|
|
}
|
|
|
|
|
2020-05-22 15:11:00 +00:00
|
|
|
func (dsw *DbStateWriter) SetCodeSizeCache(codeSizeCache *fastcache.Cache) {
|
2020-05-21 12:27:52 +00:00
|
|
|
dsw.codeSizeCache = codeSizeCache
|
2019-12-20 12:25:40 +00:00
|
|
|
}
|
|
|
|
|
2020-03-26 21:52:22 +00:00
|
|
|
func originalAccountData(original *accounts.Account, omitHashes bool) []byte {
|
|
|
|
var originalData []byte
|
|
|
|
if !original.Initialised {
|
|
|
|
originalData = []byte{}
|
|
|
|
} else if omitHashes {
|
|
|
|
testAcc := original.SelfCopy()
|
|
|
|
copy(testAcc.CodeHash[:], emptyCodeHash)
|
|
|
|
testAcc.Root = trie.EmptyRoot
|
|
|
|
originalDataLen := testAcc.EncodingLengthForStorage()
|
|
|
|
originalData = make([]byte, originalDataLen)
|
|
|
|
testAcc.EncodeForStorage(originalData)
|
|
|
|
} else {
|
|
|
|
originalDataLen := original.EncodingLengthForStorage()
|
|
|
|
originalData = make([]byte, originalDataLen)
|
|
|
|
original.EncodeForStorage(originalData)
|
|
|
|
}
|
|
|
|
return originalData
|
|
|
|
}
|
|
|
|
|
2019-12-20 12:25:40 +00:00
|
|
|
func (dsw *DbStateWriter) UpdateAccountData(ctx context.Context, address common.Address, original, account *accounts.Account) error {
|
2020-04-09 17:23:29 +00:00
|
|
|
if err := dsw.csw.UpdateAccountData(ctx, address, original, account); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-04-26 16:02:38 +00:00
|
|
|
addrHash, err := dsw.pw.HashAddress(address, true /*save*/)
|
2019-12-20 12:25:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-05-22 15:11:00 +00:00
|
|
|
value := make([]byte, account.EncodingLengthForStorage())
|
|
|
|
account.EncodeForStorage(value)
|
|
|
|
if err := dsw.stateDb.Put(dbutils.CurrentStateBucket, addrHash[:], value); err != nil {
|
2020-04-18 20:09:44 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-05-21 12:27:52 +00:00
|
|
|
if dsw.accountCache != nil {
|
2020-05-22 15:11:00 +00:00
|
|
|
dsw.accountCache.Set(address[:], value)
|
2020-05-21 12:27:52 +00:00
|
|
|
}
|
2020-04-18 20:09:44 +00:00
|
|
|
return nil
|
2019-12-20 12:25:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (dsw *DbStateWriter) DeleteAccount(ctx context.Context, address common.Address, original *accounts.Account) error {
|
2020-04-09 17:23:29 +00:00
|
|
|
if err := dsw.csw.DeleteAccount(ctx, address, original); err != nil {
|
2019-12-20 12:25:40 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-04-26 16:02:38 +00:00
|
|
|
addrHash, err := dsw.pw.HashAddress(address, true /*save*/)
|
2020-04-09 17:23:29 +00:00
|
|
|
if err != nil {
|
2019-12-20 12:25:40 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-05-21 12:27:52 +00:00
|
|
|
if err := rawdb.DeleteAccount(dsw.stateDb, addrHash); err != nil {
|
2020-04-18 20:09:44 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-05-08 04:52:55 +00:00
|
|
|
if original.Incarnation > 0 {
|
2020-05-26 12:27:21 +00:00
|
|
|
var b [8]byte
|
|
|
|
binary.BigEndian.PutUint64(b[:], original.Incarnation)
|
|
|
|
if err := dsw.stateDb.Put(dbutils.IncarnationMapBucket, address[:], b[:]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-05-08 04:52:55 +00:00
|
|
|
}
|
2020-05-21 12:27:52 +00:00
|
|
|
if dsw.accountCache != nil {
|
2020-05-22 15:11:00 +00:00
|
|
|
dsw.accountCache.Set(address[:], nil)
|
|
|
|
}
|
|
|
|
if dsw.codeCache != nil {
|
|
|
|
dsw.codeCache.Set(address[:], nil)
|
|
|
|
}
|
|
|
|
if dsw.codeSizeCache != nil {
|
|
|
|
var b [4]byte
|
|
|
|
binary.BigEndian.PutUint32(b[:], 0)
|
|
|
|
dsw.codeSizeCache.Set(address[:], b[:])
|
2020-05-21 12:27:52 +00:00
|
|
|
}
|
2020-04-18 20:09:44 +00:00
|
|
|
return nil
|
2019-12-20 12:25:40 +00:00
|
|
|
}
|
|
|
|
|
2020-05-15 07:52:45 +00:00
|
|
|
func (dsw *DbStateWriter) UpdateAccountCode(address common.Address, incarnation uint64, codeHash common.Hash, code []byte) error {
|
|
|
|
if err := dsw.csw.UpdateAccountCode(address, incarnation, codeHash, code); err != nil {
|
2020-04-09 17:23:29 +00:00
|
|
|
return err
|
|
|
|
}
|
2019-12-20 12:25:40 +00:00
|
|
|
//save contract code mapping
|
2020-05-21 12:27:52 +00:00
|
|
|
if err := dsw.stateDb.Put(dbutils.CodeBucket, codeHash[:], code); err != nil {
|
2019-12-20 12:25:40 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-05-15 07:52:45 +00:00
|
|
|
addrHash, err := common.HashData(address.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-04-15 09:33:22 +00:00
|
|
|
//save contract to codeHash mapping
|
2020-05-21 12:27:52 +00:00
|
|
|
if err := dsw.stateDb.Put(dbutils.ContractCodeBucket, dbutils.GenerateStoragePrefix(addrHash[:], incarnation), codeHash[:]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-05-22 15:11:00 +00:00
|
|
|
if dsw.codeCache != nil {
|
|
|
|
if len(code) <= 1024 {
|
|
|
|
dsw.codeCache.Set(address[:], code)
|
|
|
|
} else {
|
|
|
|
dsw.codeCache.Del(address[:])
|
|
|
|
}
|
2020-05-21 12:27:52 +00:00
|
|
|
}
|
|
|
|
if dsw.codeSizeCache != nil {
|
2020-05-22 15:11:00 +00:00
|
|
|
var b [4]byte
|
|
|
|
binary.BigEndian.PutUint32(b[:], uint32(len(code)))
|
|
|
|
dsw.codeSizeCache.Set(address[:], b[:])
|
2020-05-21 12:27:52 +00:00
|
|
|
}
|
|
|
|
return nil
|
2019-12-20 12:25:40 +00:00
|
|
|
}
|
|
|
|
|
2020-05-25 11:12:25 +00:00
|
|
|
func (dsw *DbStateWriter) WriteAccountStorage(ctx context.Context, address common.Address, incarnation uint64, key *common.Hash, original, value *uint256.Int) error {
|
2020-04-09 17:23:29 +00:00
|
|
|
// We delegate here first to let the changeSetWrite make its own decision on whether to proceed in case *original == *value
|
|
|
|
if err := dsw.csw.WriteAccountStorage(ctx, address, incarnation, key, original, value); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-12-20 12:25:40 +00:00
|
|
|
if *original == *value {
|
|
|
|
return nil
|
|
|
|
}
|
2020-04-26 16:02:38 +00:00
|
|
|
seckey, err := dsw.pw.HashKey(key, true /*save*/)
|
2019-12-20 12:25:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-04-26 16:02:38 +00:00
|
|
|
addrHash, err := dsw.pw.HashAddress(address, false /*save*/)
|
2019-12-20 12:25:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
compositeKey := dbutils.GenerateCompositeStorageKey(addrHash, incarnation, seckey)
|
2020-05-15 07:52:45 +00:00
|
|
|
|
2020-05-25 11:12:25 +00:00
|
|
|
v := value.Bytes()
|
2020-05-21 12:27:52 +00:00
|
|
|
if dsw.storageCache != nil {
|
2020-05-22 15:11:00 +00:00
|
|
|
dsw.storageCache.Set(compositeKey, v)
|
2020-05-21 12:27:52 +00:00
|
|
|
}
|
2019-12-20 12:25:40 +00:00
|
|
|
if len(v) == 0 {
|
2020-05-21 12:27:52 +00:00
|
|
|
return dsw.stateDb.Delete(dbutils.CurrentStateBucket, compositeKey)
|
2020-04-09 17:23:29 +00:00
|
|
|
}
|
2020-05-25 11:12:25 +00:00
|
|
|
return dsw.stateDb.Put(dbutils.CurrentStateBucket, compositeKey, v)
|
2020-04-09 17:23:29 +00:00
|
|
|
}
|
2020-03-11 10:31:49 +00:00
|
|
|
|
2020-05-02 18:00:42 +00:00
|
|
|
func (dsw *DbStateWriter) CreateContract(address common.Address) error {
|
2020-05-26 12:27:21 +00:00
|
|
|
if err := dsw.csw.CreateContract(address); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := dsw.stateDb.Delete(dbutils.IncarnationMapBucket, address[:]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2020-04-09 17:23:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// WriteChangeSets causes accumulated change sets to be written into
|
|
|
|
// the database (or batch) associated with the `dsw`
|
|
|
|
func (dsw *DbStateWriter) WriteChangeSets() error {
|
|
|
|
accountChanges, err := dsw.csw.GetAccountChanges()
|
2019-12-20 12:25:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-04-09 17:23:29 +00:00
|
|
|
var accountSerialised []byte
|
2020-04-15 09:33:22 +00:00
|
|
|
accountSerialised, err = changeset.EncodeAccounts(accountChanges)
|
2020-04-09 17:23:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-04-26 16:02:38 +00:00
|
|
|
key := dbutils.EncodeTimestamp(dsw.blockNr)
|
2020-05-21 12:27:52 +00:00
|
|
|
if err = dsw.changeDb.Put(dbutils.AccountChangeSetBucket, key, accountSerialised); err != nil {
|
2020-04-09 17:23:29 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
storageChanges, err := dsw.csw.GetStorageChanges()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var storageSerialized []byte
|
|
|
|
if storageChanges.Len() > 0 {
|
2020-04-15 09:33:22 +00:00
|
|
|
storageSerialized, err = changeset.EncodeStorage(storageChanges)
|
2020-04-09 17:23:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-05-21 12:27:52 +00:00
|
|
|
if err = dsw.changeDb.Put(dbutils.StorageChangeSetBucket, key, storageSerialized); err != nil {
|
2020-04-09 17:23:29 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2019-12-20 12:25:40 +00:00
|
|
|
}
|
|
|
|
|
2020-04-09 17:23:29 +00:00
|
|
|
func (dsw *DbStateWriter) WriteHistory() error {
|
|
|
|
accountChanges, err := dsw.csw.GetAccountChanges()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-05-31 06:57:47 +00:00
|
|
|
err = writeIndex(dsw.blockNr, accountChanges, dbutils.AccountsHistoryBucket, dsw.changeDb)
|
2020-04-20 10:35:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-04-09 17:23:29 +00:00
|
|
|
}
|
2020-04-20 10:35:33 +00:00
|
|
|
|
2020-04-09 17:23:29 +00:00
|
|
|
storageChanges, err := dsw.csw.GetStorageChanges()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-05-31 06:57:47 +00:00
|
|
|
err = writeIndex(dsw.blockNr, storageChanges, dbutils.StorageHistoryBucket, dsw.changeDb)
|
2020-04-20 10:35:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-31 06:57:47 +00:00
|
|
|
func writeIndex(blocknum uint64, changes *changeset.ChangeSet, bucket []byte, changeDb ethdb.Database) error {
|
2020-04-20 10:35:33 +00:00
|
|
|
for _, change := range changes.Changes {
|
2020-05-31 06:57:47 +00:00
|
|
|
currentChunkKey := dbutils.CurrentChunkKey(change.Key)
|
|
|
|
indexBytes, err := changeDb.Get(bucket, currentChunkKey)
|
2020-04-20 10:35:33 +00:00
|
|
|
if err != nil && err != ethdb.ErrKeyNotFound {
|
|
|
|
return fmt.Errorf("find chunk failed: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-04-21 17:43:57 +00:00
|
|
|
var index dbutils.HistoryIndexBytes
|
2020-04-20 10:35:33 +00:00
|
|
|
if len(indexBytes) == 0 {
|
|
|
|
index = dbutils.NewHistoryIndex()
|
2020-05-31 06:57:47 +00:00
|
|
|
} else if dbutils.CheckNewIndexChunk(indexBytes, blocknum) {
|
2020-04-21 17:43:57 +00:00
|
|
|
// Chunk overflow, need to write the "old" current chunk under its key derived from the last element
|
|
|
|
index = dbutils.WrapHistoryIndex(indexBytes)
|
|
|
|
indexKey, err := index.Key(change.Key)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Flush the old chunk
|
2020-05-31 06:57:47 +00:00
|
|
|
if err := changeDb.Put(bucket, indexKey, index); err != nil {
|
2020-04-21 17:43:57 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Start a new chunk
|
2020-04-20 10:35:33 +00:00
|
|
|
index = dbutils.NewHistoryIndex()
|
|
|
|
} else {
|
|
|
|
index = dbutils.WrapHistoryIndex(indexBytes)
|
2020-04-09 17:23:29 +00:00
|
|
|
}
|
2020-05-31 06:57:47 +00:00
|
|
|
index = index.Append(blocknum, len(change.Value) == 0)
|
2020-04-20 10:35:33 +00:00
|
|
|
|
2020-05-31 06:57:47 +00:00
|
|
|
if err := changeDb.Put(bucket, currentChunkKey, index); err != nil {
|
2020-04-15 09:33:22 +00:00
|
|
|
return err
|
2020-04-09 17:23:29 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-20 10:35:33 +00:00
|
|
|
|
2019-12-20 12:25:40 +00:00
|
|
|
return nil
|
|
|
|
}
|