2020-05-15 07:52:45 +00:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
2020-05-22 15:11:00 +00:00
|
|
|
"encoding/binary"
|
2020-05-15 07:52:45 +00:00
|
|
|
|
2020-05-25 11:12:25 +00:00
|
|
|
"github.com/holiman/uint256"
|
2021-05-20 18:25:53 +00:00
|
|
|
"github.com/ledgerwatch/erigon/common"
|
|
|
|
"github.com/ledgerwatch/erigon/common/dbutils"
|
|
|
|
"github.com/ledgerwatch/erigon/core/types/accounts"
|
|
|
|
"github.com/ledgerwatch/erigon/ethdb"
|
2021-05-23 14:53:18 +00:00
|
|
|
"github.com/ledgerwatch/erigon/turbo/shards"
|
2020-05-15 07:52:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var _ WriterWithChangeSets = (*PlainStateWriter)(nil)
|
|
|
|
|
2021-06-04 13:32:48 +00:00
|
|
|
type plainStateWriterDB interface {
|
|
|
|
ethdb.Putter
|
|
|
|
ethdb.Deleter
|
|
|
|
}
|
2020-05-15 07:52:45 +00:00
|
|
|
type PlainStateWriter struct {
|
2021-06-04 13:32:48 +00:00
|
|
|
db plainStateWriterDB
|
2021-05-05 07:31:40 +00:00
|
|
|
csw *ChangeSetWriter
|
2021-05-23 14:53:18 +00:00
|
|
|
accumulator *shards.Accumulator
|
2020-05-15 07:52:45 +00:00
|
|
|
}
|
|
|
|
|
2021-06-04 13:32:48 +00:00
|
|
|
func NewPlainStateWriter(db plainStateWriterDB, changeSetsDB ethdb.RwTx, blockNumber uint64) *PlainStateWriter {
|
2020-05-15 07:52:45 +00:00
|
|
|
return &PlainStateWriter{
|
2021-06-04 08:13:19 +00:00
|
|
|
db: db,
|
|
|
|
csw: NewChangeSetWriterPlain(changeSetsDB, blockNumber),
|
2020-05-15 07:52:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-04 08:13:19 +00:00
|
|
|
func NewPlainStateWriterNoHistory(db ethdb.Database) *PlainStateWriter {
|
2021-05-22 10:57:10 +00:00
|
|
|
return &PlainStateWriter{
|
2021-06-04 08:13:19 +00:00
|
|
|
db: db,
|
2021-05-22 10:57:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-23 14:53:18 +00:00
|
|
|
func (w *PlainStateWriter) SetAccumulator(accumulator *shards.Accumulator) *PlainStateWriter {
|
|
|
|
w.accumulator = accumulator
|
|
|
|
return w
|
|
|
|
}
|
|
|
|
|
2021-07-05 08:42:44 +00:00
|
|
|
func (w *PlainStateWriter) UpdateAccountData(address common.Address, original, account *accounts.Account) error {
|
2021-05-22 10:57:10 +00:00
|
|
|
if w.csw != nil {
|
2021-07-05 08:42:44 +00:00
|
|
|
if err := w.csw.UpdateAccountData(address, original, account); err != nil {
|
2021-05-22 10:57:10 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-05-15 07:52:45 +00:00
|
|
|
}
|
|
|
|
value := make([]byte, account.EncodingLengthForStorage())
|
|
|
|
account.EncodeForStorage(value)
|
2021-05-23 14:53:18 +00:00
|
|
|
if w.accumulator != nil {
|
|
|
|
w.accumulator.ChangeAccount(address, value)
|
|
|
|
}
|
2020-06-09 13:11:09 +00:00
|
|
|
return w.db.Put(dbutils.PlainStateBucket, address[:], value)
|
2020-05-15 07:52:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *PlainStateWriter) UpdateAccountCode(address common.Address, incarnation uint64, codeHash common.Hash, code []byte) error {
|
2021-05-22 10:57:10 +00:00
|
|
|
if w.csw != nil {
|
|
|
|
if err := w.csw.UpdateAccountCode(address, incarnation, codeHash, code); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-05-15 07:52:45 +00:00
|
|
|
}
|
2021-05-23 14:53:18 +00:00
|
|
|
if w.accumulator != nil {
|
|
|
|
w.accumulator.ChangeCode(address, incarnation, code)
|
|
|
|
}
|
2020-06-09 13:11:09 +00:00
|
|
|
if err := w.db.Put(dbutils.CodeBucket, codeHash[:], code); err != nil {
|
2020-05-15 07:52:45 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-06-09 13:11:09 +00:00
|
|
|
return w.db.Put(dbutils.PlainContractCodeBucket, dbutils.PlainGenerateStoragePrefix(address[:], incarnation), codeHash[:])
|
2020-05-15 07:52:45 +00:00
|
|
|
}
|
|
|
|
|
2021-07-05 08:42:44 +00:00
|
|
|
func (w *PlainStateWriter) DeleteAccount(address common.Address, original *accounts.Account) error {
|
2021-05-22 10:57:10 +00:00
|
|
|
if w.csw != nil {
|
2021-07-05 08:42:44 +00:00
|
|
|
if err := w.csw.DeleteAccount(address, original); err != nil {
|
2021-05-22 10:57:10 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-05-15 07:52:45 +00:00
|
|
|
}
|
2021-05-23 14:53:18 +00:00
|
|
|
if w.accumulator != nil {
|
|
|
|
w.accumulator.DeleteAccount(address)
|
|
|
|
}
|
2020-10-29 13:19:31 +00:00
|
|
|
if err := w.db.Delete(dbutils.PlainStateBucket, address[:], nil); err != nil {
|
2020-05-26 12:27:21 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if original.Incarnation > 0 {
|
|
|
|
var b [8]byte
|
|
|
|
binary.BigEndian.PutUint64(b[:], original.Incarnation)
|
2020-06-09 13:11:09 +00:00
|
|
|
if err := w.db.Put(dbutils.IncarnationMapBucket, address[:], b[:]); err != nil {
|
2020-05-26 12:27:21 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2020-05-15 07:52:45 +00:00
|
|
|
}
|
|
|
|
|
2021-07-05 08:42:44 +00:00
|
|
|
func (w *PlainStateWriter) WriteAccountStorage(address common.Address, incarnation uint64, key *common.Hash, original, value *uint256.Int) error {
|
2021-05-22 10:57:10 +00:00
|
|
|
if w.csw != nil {
|
2021-07-05 08:42:44 +00:00
|
|
|
if err := w.csw.WriteAccountStorage(address, incarnation, key, original, value); err != nil {
|
2021-05-22 10:57:10 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-05-15 07:52:45 +00:00
|
|
|
}
|
|
|
|
if *original == *value {
|
|
|
|
return nil
|
|
|
|
}
|
2020-12-08 09:44:29 +00:00
|
|
|
compositeKey := dbutils.PlainGenerateCompositeStorageKey(address.Bytes(), incarnation, key.Bytes())
|
2020-05-15 07:52:45 +00:00
|
|
|
|
2020-05-25 11:12:25 +00:00
|
|
|
v := value.Bytes()
|
2021-05-23 14:53:18 +00:00
|
|
|
if w.accumulator != nil {
|
|
|
|
w.accumulator.ChangeStorage(address, incarnation, *key, v)
|
|
|
|
}
|
2020-05-15 07:52:45 +00:00
|
|
|
if len(v) == 0 {
|
2020-10-29 13:19:31 +00:00
|
|
|
return w.db.Delete(dbutils.PlainStateBucket, compositeKey, nil)
|
2020-05-15 07:52:45 +00:00
|
|
|
}
|
2020-06-09 13:11:09 +00:00
|
|
|
return w.db.Put(dbutils.PlainStateBucket, compositeKey, v)
|
2020-05-15 07:52:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *PlainStateWriter) CreateContract(address common.Address) error {
|
2021-05-22 10:57:10 +00:00
|
|
|
if w.csw != nil {
|
|
|
|
if err := w.csw.CreateContract(address); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-05-26 12:27:21 +00:00
|
|
|
}
|
|
|
|
return nil
|
2020-05-15 07:52:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *PlainStateWriter) WriteChangeSets() error {
|
2021-05-22 10:57:10 +00:00
|
|
|
if w.csw != nil {
|
|
|
|
return w.csw.WriteChangeSets()
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2020-05-15 07:52:45 +00:00
|
|
|
}
|
2020-05-31 06:57:47 +00:00
|
|
|
|
|
|
|
func (w *PlainStateWriter) WriteHistory() error {
|
2021-05-22 10:57:10 +00:00
|
|
|
if w.csw != nil {
|
|
|
|
return w.csw.WriteHistory()
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2020-05-31 06:57:47 +00:00
|
|
|
}
|
2020-07-10 03:25:40 +00:00
|
|
|
|
|
|
|
func (w *PlainStateWriter) ChangeSetWriter() *ChangeSetWriter {
|
|
|
|
return w.csw
|
|
|
|
}
|