2019-12-20 12:25:40 +00:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
2020-11-28 14:24:47 +00:00
|
|
|
"bytes"
|
2019-12-20 12:25:40 +00:00
|
|
|
"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-12-04 21:16:51 +00:00
|
|
|
"github.com/RoaringBitmap/roaring/roaring64"
|
2020-05-22 15:11:00 +00:00
|
|
|
"github.com/VictoriaMetrics/fastcache"
|
2020-05-25 11:12:25 +00:00
|
|
|
"github.com/holiman/uint256"
|
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-12-04 21:16:51 +00:00
|
|
|
"github.com/ledgerwatch/turbo-geth/common/math"
|
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"
|
2020-12-04 21:16:51 +00:00
|
|
|
"github.com/ledgerwatch/turbo-geth/ethdb/bitmapdb"
|
2020-09-14 10:33:39 +00:00
|
|
|
"github.com/ledgerwatch/turbo-geth/turbo/trie"
|
2019-12-20 12:25:40 +00:00
|
|
|
)
|
|
|
|
|
2020-07-15 12:26:59 +00:00
|
|
|
// This type is now used in GenerateChain to generate blockchains for the tests (core/chain_makers.go)
|
|
|
|
// Main mode of operation uses PlainDbStateWriter
|
|
|
|
|
2020-05-15 07:52:45 +00:00
|
|
|
var _ WriterWithChangeSets = (*DbStateWriter)(nil)
|
|
|
|
|
2020-06-09 13:11:09 +00:00
|
|
|
func NewDbStateWriter(db ethdb.Database, blockNr uint64) *DbStateWriter {
|
2020-04-26 16:02:38 +00:00
|
|
|
return &DbStateWriter{
|
2020-06-09 13:11:09 +00:00
|
|
|
db: db,
|
|
|
|
blockNr: blockNr,
|
|
|
|
pw: &PreimageWriter{db: db, savePreimages: false},
|
|
|
|
csw: NewChangeSetWriter(),
|
2020-04-26 16:02:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-20 12:25:40 +00:00
|
|
|
type DbStateWriter struct {
|
2020-06-09 13:11:09 +00:00
|
|
|
db ethdb.Database
|
|
|
|
pw *PreimageWriter
|
|
|
|
blockNr uint64
|
|
|
|
csw *ChangeSetWriter
|
|
|
|
accountCache *fastcache.Cache
|
|
|
|
storageCache *fastcache.Cache
|
|
|
|
codeCache *fastcache.Cache
|
|
|
|
codeSizeCache *fastcache.Cache
|
2020-05-21 12:27:52 +00:00
|
|
|
}
|
|
|
|
|
2020-07-15 12:26:59 +00:00
|
|
|
func (dsw *DbStateWriter) ChangeSetWriter() *ChangeSetWriter {
|
|
|
|
return dsw.csw
|
|
|
|
}
|
|
|
|
|
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)
|
2020-06-09 13:11:09 +00:00
|
|
|
if err := dsw.db.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-06-09 13:11:09 +00:00
|
|
|
if err := rawdb.DeleteAccount(dsw.db, 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)
|
2020-06-09 13:11:09 +00:00
|
|
|
if err := dsw.db.Put(dbutils.IncarnationMapBucket, address[:], b[:]); err != nil {
|
2020-05-26 12:27:21 +00:00
|
|
|
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-06-09 13:11:09 +00:00
|
|
|
if err := dsw.db.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-06-09 13:11:09 +00:00
|
|
|
if err := dsw.db.Put(dbutils.ContractCodeBucket, dbutils.GenerateStoragePrefix(addrHash[:], incarnation), codeHash[:]); err != nil {
|
2020-05-21 12:27:52 +00:00
|
|
|
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-10-29 13:19:31 +00:00
|
|
|
return dsw.db.Delete(dbutils.CurrentStateBucket, compositeKey, nil)
|
2020-04-09 17:23:29 +00:00
|
|
|
}
|
2020-06-09 13:11:09 +00:00
|
|
|
return dsw.db.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
|
|
|
|
}
|
2020-10-29 13:19:31 +00:00
|
|
|
if err := dsw.db.Delete(dbutils.IncarnationMapBucket, address[:], nil); err != nil {
|
2020-05-26 12:27:21 +00:00
|
|
|
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-11-28 14:24:47 +00:00
|
|
|
var prevK []byte
|
2020-11-16 12:08:28 +00:00
|
|
|
if err = changeset.Mapper[dbutils.AccountChangeSetBucket].Encode(dsw.blockNr, accountChanges, func(k, v []byte) error {
|
2020-11-28 14:24:47 +00:00
|
|
|
if bytes.Equal(k, prevK) {
|
|
|
|
if err = dsw.db.AppendDup(dbutils.AccountChangeSetBucket, k, v); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err = dsw.db.Append(dbutils.AccountChangeSetBucket, k, v); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
prevK = k
|
|
|
|
return nil
|
2020-11-16 12:08:28 +00:00
|
|
|
}); err != nil {
|
2020-04-09 17:23:29 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-11-28 14:24:47 +00:00
|
|
|
prevK = nil
|
2020-11-16 12:08:28 +00:00
|
|
|
|
2020-04-09 17:23:29 +00:00
|
|
|
storageChanges, err := dsw.csw.GetStorageChanges()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-11-16 12:08:28 +00:00
|
|
|
if storageChanges.Len() == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if err = changeset.Mapper[dbutils.StorageChangeSetBucket].Encode(dsw.blockNr, storageChanges, func(k, v []byte) error {
|
2020-11-28 14:24:47 +00:00
|
|
|
if bytes.Equal(k, prevK) {
|
|
|
|
if err = dsw.db.AppendDup(dbutils.StorageChangeSetBucket, k, v); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err = dsw.db.Append(dbutils.StorageChangeSetBucket, k, v); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
prevK = k
|
|
|
|
return nil
|
2020-11-16 12:08:28 +00:00
|
|
|
}); err != nil {
|
|
|
|
return err
|
2020-04-09 17:23:29 +00:00
|
|
|
}
|
|
|
|
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-06-09 13:11:09 +00:00
|
|
|
err = writeIndex(dsw.blockNr, accountChanges, dbutils.AccountsHistoryBucket, dsw.db)
|
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-06-09 13:11:09 +00:00
|
|
|
err = writeIndex(dsw.blockNr, storageChanges, dbutils.StorageHistoryBucket, dsw.db)
|
2020-04-20 10:35:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-17 06:45:52 +00:00
|
|
|
func writeIndex(blocknum uint64, changes *changeset.ChangeSet, bucket string, changeDb ethdb.GetterPutter) error {
|
2020-12-04 21:16:51 +00:00
|
|
|
buf := bytes.NewBuffer(nil)
|
2020-04-20 10:35:33 +00:00
|
|
|
for _, change := range changes.Changes {
|
2020-12-04 21:16:51 +00:00
|
|
|
k := dbutils.CompositeKeyWithoutIncarnation(change.Key)
|
|
|
|
|
|
|
|
index, err := bitmapdb.Get64(changeDb, bucket, k, 0, math.MaxUint32)
|
|
|
|
if err != nil {
|
2020-04-20 10:35:33 +00:00
|
|
|
return fmt.Errorf("find chunk failed: %w", err)
|
|
|
|
}
|
2020-12-04 21:16:51 +00:00
|
|
|
index.Add(blocknum)
|
|
|
|
if err = bitmapdb.WalkChunkWithKeys64(k, index, bitmapdb.ChunkLimit, func(chunkKey []byte, chunk *roaring64.Bitmap) error {
|
|
|
|
buf.Reset()
|
|
|
|
if _, err = chunk.WriteTo(buf); err != nil {
|
2020-04-21 17:43:57 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-12-04 21:16:51 +00:00
|
|
|
return changeDb.Put(bucket, chunkKey, common.CopyBytes(buf.Bytes()))
|
|
|
|
}); 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
|
|
|
|
}
|