erigon-pulse/core/state/plain_state_writer.go

144 lines
3.6 KiB
Go
Raw Normal View History

package state
import (
"encoding/binary"
"github.com/holiman/uint256"
2021-07-29 11:53:13 +00:00
"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/erigon/common"
"github.com/ledgerwatch/erigon/common/dbutils"
"github.com/ledgerwatch/erigon/core/types/accounts"
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 14:53:18 +00:00
"github.com/ledgerwatch/erigon/turbo/shards"
)
var _ WriterWithChangeSets = (*PlainStateWriter)(nil)
type putDel interface {
kv.Putter
kv.Deleter
}
type PlainStateWriter struct {
db putDel
2021-05-05 07:31:40 +00:00
csw *ChangeSetWriter
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 14:53:18 +00:00
accumulator *shards.Accumulator
}
func NewPlainStateWriter(db putDel, changeSetsDB kv.RwTx, blockNumber uint64) *PlainStateWriter {
return &PlainStateWriter{
db: db,
csw: NewChangeSetWriterPlain(changeSetsDB, blockNumber),
}
}
func NewPlainStateWriterNoHistory(db putDel) *PlainStateWriter {
return &PlainStateWriter{
db: db,
}
}
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 14:53:18 +00:00
func (w *PlainStateWriter) SetAccumulator(accumulator *shards.Accumulator) *PlainStateWriter {
w.accumulator = accumulator
return w
}
func (w *PlainStateWriter) UpdateAccountData(address common.Address, original, account *accounts.Account) error {
if w.csw != nil {
if err := w.csw.UpdateAccountData(address, original, account); err != nil {
return err
}
}
value := make([]byte, account.EncodingLengthForStorage())
account.EncodeForStorage(value)
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 14:53:18 +00:00
if w.accumulator != nil {
w.accumulator.ChangeAccount(address, account.Incarnation, value)
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 14:53:18 +00:00
}
2021-07-28 03:43:51 +00:00
return w.db.Put(kv.PlainState, 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
}
}
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 14:53:18 +00:00
if w.accumulator != nil {
w.accumulator.ChangeCode(address, incarnation, code)
}
2021-07-28 03:43:51 +00:00
if err := w.db.Put(kv.Code, codeHash[:], code); err != nil {
return err
}
return w.db.Put(kv.PlainContractCode, dbutils.PlainGenerateStoragePrefix(address[:], incarnation), codeHash[:])
}
func (w *PlainStateWriter) DeleteAccount(address common.Address, original *accounts.Account) error {
if w.csw != nil {
if err := w.csw.DeleteAccount(address, original); err != nil {
return err
}
}
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 14:53:18 +00:00
if w.accumulator != nil {
w.accumulator.DeleteAccount(address)
}
2021-07-28 03:43:51 +00:00
if err := w.db.Delete(kv.PlainState, 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(kv.IncarnationMap, address[:], b[:]); err != nil {
return err
}
}
return nil
}
func (w *PlainStateWriter) WriteAccountStorage(address common.Address, incarnation uint64, key *common.Hash, original, value *uint256.Int) error {
if w.csw != nil {
if err := w.csw.WriteAccountStorage(address, incarnation, key, original, value); err != nil {
return err
}
}
if *original == *value {
return nil
}
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
compositeKey := dbutils.PlainGenerateCompositeStorageKey(address.Bytes(), incarnation, key.Bytes())
v := value.Bytes()
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 14:53:18 +00:00
if w.accumulator != nil {
w.accumulator.ChangeStorage(address, incarnation, *key, v)
}
if len(v) == 0 {
2021-07-28 03:43:51 +00:00
return w.db.Delete(kv.PlainState, compositeKey, nil)
}
2021-07-28 03:43:51 +00:00
return w.db.Put(kv.PlainState, 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
}