2019-12-20 12:25:40 +00:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
2020-04-09 17:23:29 +00:00
|
|
|
"fmt"
|
2020-03-11 10:31:49 +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-08 04:52:55 +00:00
|
|
|
func NewDbStateWriter(db ethdb.Database, blockNr uint64, incarnationMap map[common.Address]uint64) *DbStateWriter {
|
2020-04-26 16:02:38 +00:00
|
|
|
return &DbStateWriter{
|
2020-05-08 04:52:55 +00:00
|
|
|
db: db,
|
|
|
|
blockNr: blockNr,
|
|
|
|
pw: &PreimageWriter{db: db, savePreimages: false},
|
|
|
|
csw: NewChangeSetWriter(),
|
|
|
|
incarnationMap: incarnationMap,
|
2020-04-26 16:02:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-20 12:25:40 +00:00
|
|
|
type DbStateWriter struct {
|
2020-05-08 04:52:55 +00:00
|
|
|
db ethdb.Database
|
|
|
|
pw *PreimageWriter
|
|
|
|
blockNr uint64
|
|
|
|
csw *ChangeSetWriter
|
|
|
|
incarnationMap map[common.Address]uint64
|
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-04-26 16:02:38 +00:00
|
|
|
if err := rawdb.WriteAccount(dsw.db, addrHash, *account); err != nil {
|
2020-04-18 20:09:44 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
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-04-26 16:02:38 +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 {
|
|
|
|
dsw.incarnationMap[address] = original.Incarnation
|
|
|
|
}
|
2020-04-18 20:09:44 +00:00
|
|
|
return nil
|
2019-12-20 12:25:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (dsw *DbStateWriter) UpdateAccountCode(addrHash common.Hash, incarnation uint64, codeHash common.Hash, code []byte) error {
|
2020-04-09 17:23:29 +00:00
|
|
|
if err := dsw.csw.UpdateAccountCode(addrHash, incarnation, codeHash, code); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-12-20 12:25:40 +00:00
|
|
|
//save contract code mapping
|
2020-04-26 16:02:38 +00:00
|
|
|
if err := dsw.db.Put(dbutils.CodeBucket, codeHash[:], code); err != nil {
|
2019-12-20 12:25:40 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-04-15 09:33:22 +00:00
|
|
|
//save contract to codeHash mapping
|
2020-04-26 16:02:38 +00:00
|
|
|
return dsw.db.Put(dbutils.ContractCodeBucket, dbutils.GenerateStoragePrefix(addrHash[:], incarnation), codeHash[:])
|
2019-12-20 12:25:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (dsw *DbStateWriter) WriteAccountStorage(ctx context.Context, address common.Address, incarnation uint64, key, original, value *common.Hash) 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
|
|
|
|
}
|
|
|
|
v := bytes.TrimLeft(value[:], "\x00")
|
|
|
|
vv := make([]byte, len(v))
|
|
|
|
copy(vv, v)
|
|
|
|
|
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)
|
|
|
|
if len(v) == 0 {
|
2020-04-26 16:02:38 +00:00
|
|
|
return dsw.db.Delete(dbutils.CurrentStateBucket, compositeKey)
|
2019-12-20 12:25:40 +00:00
|
|
|
} else {
|
2020-04-26 16:02:38 +00:00
|
|
|
return dsw.db.Put(dbutils.CurrentStateBucket, compositeKey, vv)
|
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 {
|
|
|
|
return dsw.csw.CreateContract(address)
|
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)
|
|
|
|
if err = dsw.db.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-04-26 16:02:38 +00:00
|
|
|
if err = dsw.db.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-04-20 10:35:33 +00:00
|
|
|
err = dsw.writeIndex(accountChanges, dbutils.AccountsHistoryBucket)
|
|
|
|
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-04-20 10:35:33 +00:00
|
|
|
err = dsw.writeIndex(storageChanges, dbutils.StorageHistoryBucket)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dsw *DbStateWriter) writeIndex(changes *changeset.ChangeSet, bucket []byte) error {
|
|
|
|
for _, change := range changes.Changes {
|
2020-04-21 17:43:57 +00:00
|
|
|
currentChunkKey := dbutils.IndexChunkKey(change.Key, ^uint64(0))
|
2020-04-26 16:02:38 +00:00
|
|
|
indexBytes, err := dsw.db.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-26 16:02:38 +00:00
|
|
|
v := dsw.blockNr
|
2020-04-20 10:35:33 +00:00
|
|
|
|
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-04-25 14:50:32 +00:00
|
|
|
} else if dbutils.CheckNewIndexChunk(indexBytes, v) {
|
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-04-26 16:02:38 +00:00
|
|
|
if err := dsw.db.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-04-25 14:50:32 +00:00
|
|
|
index = index.Append(v, len(change.Value) == 0)
|
2020-04-20 10:35:33 +00:00
|
|
|
|
2020-04-26 16:02:38 +00:00
|
|
|
if err := dsw.db.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
|
|
|
|
}
|