erigon-pulse/core/state/plain_state_writer.go
ledgerwatch 835b954f5c
State stream to RPC daemon (#1986)
* Remove interfaces

* Squashed 'interfaces/' content from commit e06631eb4

git-subtree-dir: interfaces
git-subtree-split: e06631eb4d0926c2d6a4f552497b920b4ed8d1bd

* Update KV interface

* Squashed 'interfaces/' changes from e06631eb4..014677ffe

014677ffe Merge remote-tracking branch 'origin/master' into stream
08c32a09e add version method to txPool and ethbackend
5b6bf70b9 Update README.md
7712cb267 Update README.md
f895ece4c save (#37)

git-subtree-dir: interfaces
git-subtree-split: 014677ffe5bff0dee1a333f06c92e6110d791468

* Fix old interfaces

* Squashed 'interfaces/' changes from 014677ffe..df31e1146

df31e1146 remove action from storage change

git-subtree-dir: interfaces
git-subtree-split: df31e1146c368eda2e2b15ab252b78fba7a0a6f3

* add Accumulator

* add location

* Squashed 'interfaces/' changes from df31e1146..472584639

472584639 Merge remote-tracking branch 'origin/master' into stream
dd6a42724 Refactor of consensus interface (#28)

git-subtree-dir: interfaces
git-subtree-split: 472584639f637189dfb906ef1ed03665f98d55d2

* Fix compilation in cons

* Pass accumulator to Execution stage

* Fix test

* Pass accumulator to unwind and plain writer

* Add accumulator use to plain writer

* Squashed 'interfaces/' changes from 472584639f..5c36f038b8

5c36f038b8 State change stream to KV (#38)
REVERT: 472584639f Merge remote-tracking branch 'origin/master' into stream
REVERT: df31e1146c remove action from storage change
REVERT: 014677ffe5 Merge remote-tracking branch 'origin/master' into stream
REVERT: e06631eb4d Fix
REVERT: 9c10d79d2d Fix
REVERT: 61ae9307de Fix
REVERT: 4fcf34ecc5 State change stream to KV

git-subtree-dir: interfaces
git-subtree-split: 5c36f038b87096ffb6b07e90c6762c21b864cd3b

* Add state.stream flag

Co-authored-by: Alex Sharp <alexsharp@Alexs-MacBook-Pro.local>
2021-05-23 15:53:18 +01:00

144 lines
3.9 KiB
Go

package state
import (
"context"
"encoding/binary"
"github.com/holiman/uint256"
"github.com/ledgerwatch/erigon/common"
"github.com/ledgerwatch/erigon/common/dbutils"
"github.com/ledgerwatch/erigon/core/types/accounts"
"github.com/ledgerwatch/erigon/ethdb"
"github.com/ledgerwatch/erigon/turbo/shards"
)
var _ WriterWithChangeSets = (*PlainStateWriter)(nil)
type PlainStateWriter struct {
db ethdb.Database
csw *ChangeSetWriter
blockNumber uint64
accumulator *shards.Accumulator
}
func NewPlainStateWriter(db ethdb.Database, changeSetsDB ethdb.RwTx, blockNumber uint64) *PlainStateWriter {
return &PlainStateWriter{
db: db,
csw: NewChangeSetWriterPlain(changeSetsDB, blockNumber),
blockNumber: blockNumber,
}
}
func NewPlainStateWriterNoHistory(db ethdb.Database, blockNumber uint64) *PlainStateWriter {
return &PlainStateWriter{
db: db,
blockNumber: blockNumber,
}
}
func (w *PlainStateWriter) SetAccumulator(accumulator *shards.Accumulator) *PlainStateWriter {
w.accumulator = accumulator
return w
}
func (w *PlainStateWriter) UpdateAccountData(ctx context.Context, address common.Address, original, account *accounts.Account) error {
if w.csw != nil {
if err := w.csw.UpdateAccountData(ctx, address, original, account); err != nil {
return err
}
}
value := make([]byte, account.EncodingLengthForStorage())
account.EncodeForStorage(value)
if w.accumulator != nil {
w.accumulator.ChangeAccount(address, 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 w.csw != nil {
if err := w.csw.UpdateAccountCode(address, incarnation, codeHash, code); err != nil {
return err
}
}
if w.accumulator != nil {
w.accumulator.ChangeCode(address, incarnation, code)
}
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 w.csw != nil {
if err := w.csw.DeleteAccount(ctx, address, original); err != nil {
return err
}
}
if w.accumulator != nil {
w.accumulator.DeleteAccount(address)
}
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 w.csw != nil {
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 w.accumulator != nil {
w.accumulator.ChangeStorage(address, incarnation, *key, v)
}
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 w.csw != nil {
if err := w.csw.CreateContract(address); err != nil {
return err
}
}
return nil
}
func (w *PlainStateWriter) WriteChangeSets() error {
if w.csw != nil {
return w.csw.WriteChangeSets()
}
return nil
}
func (w *PlainStateWriter) WriteHistory() error {
if w.csw != nil {
return w.csw.WriteHistory()
}
return nil
}
func (w *PlainStateWriter) ChangeSetWriter() *ChangeSetWriter {
return w.csw
}