erigon-pulse/core/state/plain_state_writer.go
ledgerwatch 5ea590c18e
State cache switching writes to reads during commit (#1368)
* State cache init

* More code

* Fix lint

* More tests

* More tests

* More tests

* Fix test

* Transformations

* remove writeQueue, before fixing the tests

* Fix tests

* Add more tests, incarnation to the code items

* Fix lint

* Fix lint

* Remove shards prototype, add incarnation to the state reader code

* Clean up and replace cache in call_traces stage

* fix flaky test

* Save changes

* Readers to use addrHash, writes - addresses

* Fix lint

* Fix lint

* More accurate tracking of size

* Optimise for smaller write batches

* Attempt to integrate state cache into Execution stage

* cacheSize to default flags

* Print correct cache sizes and batch sizes

* cacheSize in the integration

* Fix tests

* Fix lint

* Remove print

* Fix exec stage

* Fix test

* Refresh sequence on write

* No double increment

* heap.Remove

* Try to fix alignment

* Refactoring, adding hashItems

* More changes

* Fix compile errors

* Fix lint

* Wrapping cached reader

* Wrap writer into cached writer

* Turn state cache off by default

* Fix plain state writer

* Fix for code/storage mixup

* Fix tests

* Fix clique test

* Better fix for the tests

* Add test and fix some more

* Fix compile error|

* More functions

* Fixes

* Fix for the tests

* sepatate DeletedFlag and AbsentFlag

* Minor fixes

* Test refactoring

* More changes

* Fix some tests

* More test fixes

* More test fixes

* Fix lint

* Move blockchain_test to be able to use stagedsync

* More fixes

* Fixes and cleanup

* Fix tests in turbo/stages

* Fix lint

* Fix lint

* Intemediate

* Fix tests

* Intemediate

* More fixes

* Compilation fixes

* More fixes

* Fix compile errors

* More test fixes

* More fixes

* More test fixes

* Fix compile error

* Fixes

* Fix

* Fix

* More fixes

* Fixes

* More fixes and cleanup

* Further fix

* Check gas used and bloom with header

Co-authored-by: Alexey Sharp <alexeysharp@Alexeys-iMac.local>
2020-12-08 09:44:29 +00:00

178 lines
4.8 KiB
Go

package state
import (
"bytes"
"context"
"encoding/binary"
"github.com/holiman/uint256"
"github.com/ledgerwatch/turbo-geth/common/changeset"
"github.com/ledgerwatch/turbo-geth/common"
"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 {
db ethdb.Database
changeSetsDB ethdb.Database
csw *ChangeSetWriter
blockNumber uint64
}
func NewPlainStateWriter(db ethdb.Database, changeSetsDB ethdb.Database, blockNumber uint64) *PlainStateWriter {
return &PlainStateWriter{
db: db,
changeSetsDB: changeSetsDB,
csw: NewChangeSetWriterPlain(changeSetsDB, blockNumber),
blockNumber: blockNumber,
}
}
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)
return w.db.Put(dbutils.PlainStateBucket, address[:], value)
}
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
}
if err := w.db.Put(dbutils.CodeBucket, codeHash[:], code); err != nil {
return err
}
return w.db.Put(dbutils.PlainContractCodeBucket, dbutils.PlainGenerateStoragePrefix(address[:], incarnation), codeHash[:])
}
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
}
if err := w.db.Delete(dbutils.PlainStateBucket, address[:], nil); err != nil {
return err
}
if original.Incarnation > 0 {
var b [8]byte
binary.BigEndian.PutUint64(b[:], original.Incarnation)
if err := w.db.Put(dbutils.IncarnationMapBucket, address[:], b[:]); err != nil {
return err
}
}
return nil
}
func (w *PlainStateWriter) WriteAccountStorage(ctx context.Context, address common.Address, incarnation uint64, key *common.Hash, original, value *uint256.Int) error {
if err := w.csw.WriteAccountStorage(ctx, address, incarnation, key, original, value); err != nil {
return err
}
if *original == *value {
return nil
}
compositeKey := dbutils.PlainGenerateCompositeStorageKey(address.Bytes(), incarnation, key.Bytes())
v := value.Bytes()
if len(v) == 0 {
return w.db.Delete(dbutils.PlainStateBucket, compositeKey, nil)
}
return w.db.Put(dbutils.PlainStateBucket, compositeKey, v)
}
func (w *PlainStateWriter) CreateContract(address common.Address) error {
if err := w.csw.CreateContract(address); err != nil {
return err
}
if err := w.db.Delete(dbutils.IncarnationMapBucket, address[:], nil); err != nil {
return err
}
return nil
}
func (w *PlainStateWriter) WriteChangeSets() error {
db := w.db
if w.changeSetsDB != nil {
db = w.changeSetsDB
}
accountChanges, err := w.csw.GetAccountChanges()
if err != nil {
return err
}
var prevK []byte
if err = changeset.Mapper[dbutils.PlainAccountChangeSetBucket].Encode(w.blockNumber, accountChanges, func(k, v []byte) error {
if bytes.Equal(k, prevK) {
if err = db.AppendDup(dbutils.PlainAccountChangeSetBucket, k, v); err != nil {
return err
}
} else {
if err = db.Append(dbutils.PlainAccountChangeSetBucket, k, v); err != nil {
return err
}
}
prevK = k
return nil
}); err != nil {
return err
}
prevK = nil
storageChanges, err := w.csw.GetStorageChanges()
if err != nil {
return err
}
if storageChanges.Len() == 0 {
return nil
}
if err = changeset.Mapper[dbutils.PlainStorageChangeSetBucket].Encode(w.blockNumber, storageChanges, func(k, v []byte) error {
if bytes.Equal(k, prevK) {
if err = db.AppendDup(dbutils.PlainStorageChangeSetBucket, k, v); err != nil {
return err
}
} else {
if err = db.Append(dbutils.PlainStorageChangeSetBucket, k, v); err != nil {
return err
}
}
prevK = k
return nil
}); err != nil {
return err
}
return nil
}
func (w *PlainStateWriter) WriteHistory() error {
db := w.db
if w.changeSetsDB != nil {
db = w.changeSetsDB
}
accountChanges, err := w.csw.GetAccountChanges()
if err != nil {
return err
}
err = writeIndex(w.blockNumber, accountChanges, dbutils.AccountsHistoryBucket, db)
if err != nil {
return err
}
storageChanges, err := w.csw.GetStorageChanges()
if err != nil {
return err
}
err = writeIndex(w.blockNumber, storageChanges, dbutils.StorageHistoryBucket, db)
if err != nil {
return err
}
return nil
}
func (w *PlainStateWriter) ChangeSetWriter() *ChangeSetWriter {
return w.csw
}