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"
|
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
|
|
|
type DbStateWriter struct {
|
|
|
|
tds *TrieDbState
|
2020-04-09 17:23:29 +00:00
|
|
|
csw *ChangeSetWriter
|
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
|
|
|
|
}
|
2019-12-20 12:25:40 +00:00
|
|
|
dataLen := account.EncodingLengthForStorage()
|
|
|
|
data := make([]byte, dataLen)
|
|
|
|
account.EncodeForStorage(data)
|
|
|
|
|
|
|
|
addrHash, err := dsw.tds.HashAddress(address, true /*save*/)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-04-09 17:23:29 +00:00
|
|
|
return dsw.tds.db.Put(dbutils.AccountsBucket, addrHash[:], data)
|
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-09 17:23:29 +00:00
|
|
|
addrHash, err := dsw.tds.HashAddress(address, true /*save*/)
|
|
|
|
if err != nil {
|
2019-12-20 12:25:40 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-04-09 17:23:29 +00:00
|
|
|
return dsw.tds.db.Delete(dbutils.AccountsBucket, addrHash[:])
|
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
|
|
|
|
if err := dsw.tds.db.Put(dbutils.CodeBucket, codeHash[:], code); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-04-15 09:33:22 +00:00
|
|
|
//save contract to codeHash mapping
|
|
|
|
return dsw.tds.db.Put(dbutils.ContractCodeBucket, dbutils.GenerateStoragePrefix(addrHash, incarnation), codeHash.Bytes())
|
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
|
|
|
|
}
|
|
|
|
seckey, err := dsw.tds.HashKey(key, true /*save*/)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
v := bytes.TrimLeft(value[:], "\x00")
|
|
|
|
vv := make([]byte, len(v))
|
|
|
|
copy(vv, v)
|
|
|
|
|
|
|
|
addrHash, err := dsw.tds.HashAddress(address, false /*save*/)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
compositeKey := dbutils.GenerateCompositeStorageKey(addrHash, incarnation, seckey)
|
|
|
|
if len(v) == 0 {
|
2020-04-09 17:23:29 +00:00
|
|
|
return dsw.tds.db.Delete(dbutils.StorageBucket, compositeKey)
|
2019-12-20 12:25:40 +00:00
|
|
|
} else {
|
2020-04-09 17:23:29 +00:00
|
|
|
return dsw.tds.db.Put(dbutils.StorageBucket, compositeKey, vv)
|
|
|
|
}
|
|
|
|
}
|
2020-03-11 10:31:49 +00:00
|
|
|
|
2020-04-09 17:23:29 +00:00
|
|
|
func (dsw *DbStateWriter) CreateContract(address common.Address) error {
|
|
|
|
if err := dsw.csw.CreateContract(address); err != nil {
|
|
|
|
return err
|
2019-12-20 12:25:40 +00:00
|
|
|
}
|
2020-04-09 17:23:29 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
key := dbutils.EncodeTimestamp(dsw.tds.blockNr)
|
|
|
|
if err = dsw.tds.db.Put(dbutils.AccountChangeSetBucket, key, accountSerialised); err != nil {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
if err = dsw.tds.db.Put(dbutils.StorageChangeSetBucket, key, storageSerialized); err != nil {
|
|
|
|
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-15 09:33:22 +00:00
|
|
|
for _, change := range accountChanges.Changes {
|
|
|
|
value, err1 := dsw.tds.db.Get(dbutils.AccountsHistoryBucket, change.Key)
|
|
|
|
if err1 != nil && err1 != ethdb.ErrKeyNotFound {
|
|
|
|
return fmt.Errorf("db.Get failed: %w", err1)
|
2020-04-09 17:23:29 +00:00
|
|
|
}
|
2020-04-15 09:33:22 +00:00
|
|
|
index := dbutils.WrapHistoryIndex(value)
|
|
|
|
index.Append(dsw.tds.blockNr)
|
|
|
|
if err2 := dsw.tds.db.Put(dbutils.AccountsHistoryBucket, change.Key, *index); err2 != nil {
|
|
|
|
return err2
|
2020-04-09 17:23:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
storageChanges, err := dsw.csw.GetStorageChanges()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-04-15 09:33:22 +00:00
|
|
|
for _, change := range storageChanges.Changes {
|
|
|
|
keyNoInc := make([]byte, len(change.Key)-common.IncarnationLength)
|
|
|
|
copy(keyNoInc, change.Key[:common.HashLength])
|
|
|
|
copy(keyNoInc[common.HashLength:], change.Key[common.HashLength+common.IncarnationLength:])
|
|
|
|
value, err1 := dsw.tds.db.Get(dbutils.StorageHistoryBucket, keyNoInc)
|
|
|
|
if err1 != nil && err1 != ethdb.ErrKeyNotFound {
|
|
|
|
return fmt.Errorf("db.Get failed: %w", err1)
|
2020-04-09 17:23:29 +00:00
|
|
|
}
|
2020-04-15 09:33:22 +00:00
|
|
|
index := dbutils.WrapHistoryIndex(value)
|
|
|
|
index.Append(dsw.tds.blockNr)
|
|
|
|
if err := dsw.tds.db.Put(dbutils.StorageHistoryBucket, keyNoInc, *index); err != nil {
|
|
|
|
return err
|
2020-04-09 17:23:29 +00:00
|
|
|
}
|
|
|
|
}
|
2019-12-20 12:25:40 +00:00
|
|
|
return nil
|
|
|
|
}
|