Skip to content

Commit

Permalink
remove ctx from state writer interface (erigontech#2302)
Browse files Browse the repository at this point in the history
  • Loading branch information
AskAlexSharov authored Jul 5, 2021
1 parent afadde6 commit 9683b26
Show file tree
Hide file tree
Showing 13 changed files with 65 additions and 81 deletions.
6 changes: 3 additions & 3 deletions cmd/rpcdaemon/commands/trace_adhoc.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ type StateDiff struct {
sdMap map[common.Address]*StateDiffAccount
}

func (sd *StateDiff) UpdateAccountData(ctx context.Context, address common.Address, original, account *accounts.Account) error {
func (sd *StateDiff) UpdateAccountData(address common.Address, original, account *accounts.Account) error {
if _, ok := sd.sdMap[address]; !ok {
sd.sdMap[address] = &StateDiffAccount{Storage: make(map[common.Hash]map[string]interface{})}
}
Expand All @@ -348,14 +348,14 @@ func (sd *StateDiff) UpdateAccountCode(address common.Address, incarnation uint6
return nil
}

func (sd *StateDiff) DeleteAccount(ctx context.Context, address common.Address, original *accounts.Account) error {
func (sd *StateDiff) DeleteAccount(address common.Address, original *accounts.Account) error {
if _, ok := sd.sdMap[address]; !ok {
sd.sdMap[address] = &StateDiffAccount{Storage: make(map[common.Hash]map[string]interface{})}
}
return nil
}

func (sd *StateDiff) WriteAccountStorage(ctx context.Context, address common.Address, incarnation uint64, key *common.Hash, original, value *uint256.Int) error {
func (sd *StateDiff) WriteAccountStorage(address common.Address, incarnation uint64, key *common.Hash, original, value *uint256.Int) error {
if *original == *value {
return nil
}
Expand Down
12 changes: 6 additions & 6 deletions cmd/snapshots/debug/debug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,29 +260,29 @@ func (d *DebugReaderWriter) WriteHistory() error {
return d.w.WriteHistory()
}

func (d *DebugReaderWriter) UpdateAccountData(ctx context.Context, address common.Address, original, account *accounts.Account) error {
func (d *DebugReaderWriter) UpdateAccountData(address common.Address, original, account *accounts.Account) error {
b, err := rlp.EncodeToBytes(account)
if err != nil {
return err
}
d.updatedAcc[address] = b
return d.w.UpdateAccountData(ctx, address, original, account)
return d.w.UpdateAccountData(address, original, account)
}

func (d *DebugReaderWriter) UpdateAccountCode(address common.Address, incarnation uint64, codeHash common.Hash, code []byte) error {
d.updatedCodes[codeHash] = code
return d.w.UpdateAccountCode(address, incarnation, codeHash, code)
}

func (d *DebugReaderWriter) DeleteAccount(ctx context.Context, address common.Address, original *accounts.Account) error {
func (d *DebugReaderWriter) DeleteAccount(address common.Address, original *accounts.Account) error {
d.updatedAcc[address] = nil
//d.deletedAcc[address]= struct{}{}
return d.w.DeleteAccount(ctx, address, original)
return d.w.DeleteAccount(address, original)
}

func (d *DebugReaderWriter) WriteAccountStorage(ctx context.Context, address common.Address, incarnation uint64, key *common.Hash, original, value *uint256.Int) error {
func (d *DebugReaderWriter) WriteAccountStorage(address common.Address, incarnation uint64, key *common.Hash, original, value *uint256.Int) error {
d.updatedStorage[string(dbutils.PlainGenerateCompositeStorageKey(address.Bytes(), incarnation, key.Bytes()))] = value.Bytes()
return d.w.WriteAccountStorage(ctx, address, incarnation, key, original, value)
return d.w.WriteAccountStorage(address, incarnation, key, original, value)
}

func (d *DebugReaderWriter) CreateContract(address common.Address) error {
Expand Down
14 changes: 6 additions & 8 deletions core/state/cached_writer.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package state

import (
"context"

"github.com/holiman/uint256"
"github.com/ledgerwatch/erigon/common"
"github.com/ledgerwatch/erigon/core/types/accounts"
Expand All @@ -20,8 +18,8 @@ func NewCachedWriter(w WriterWithChangeSets, cache *shards.StateCache) *CachedWr
return &CachedWriter{w: w, cache: cache}
}

func (cw *CachedWriter) UpdateAccountData(ctx context.Context, address common.Address, original, account *accounts.Account) error {
if err := cw.w.UpdateAccountData(ctx, address, original, account); err != nil {
func (cw *CachedWriter) UpdateAccountData(address common.Address, original, account *accounts.Account) error {
if err := cw.w.UpdateAccountData(address, original, account); err != nil {
return err
}
cw.cache.SetAccountWrite(address.Bytes(), account)
Expand All @@ -36,16 +34,16 @@ func (cw *CachedWriter) UpdateAccountCode(address common.Address, incarnation ui
return nil
}

func (cw *CachedWriter) DeleteAccount(ctx context.Context, address common.Address, original *accounts.Account) error {
if err := cw.w.DeleteAccount(ctx, address, original); err != nil {
func (cw *CachedWriter) DeleteAccount(address common.Address, original *accounts.Account) error {
if err := cw.w.DeleteAccount(address, original); err != nil {
return err
}
cw.cache.SetAccountDelete(address.Bytes())
return nil
}

func (cw *CachedWriter) WriteAccountStorage(ctx context.Context, address common.Address, incarnation uint64, key *common.Hash, original, value *uint256.Int) error {
if err := cw.w.WriteAccountStorage(ctx, address, incarnation, key, original, value); err != nil {
func (cw *CachedWriter) WriteAccountStorage(address common.Address, incarnation uint64, key *common.Hash, original, value *uint256.Int) error {
if err := cw.w.WriteAccountStorage(address, incarnation, key, original, value); err != nil {
return err
}
if *original == *value {
Expand Down
7 changes: 3 additions & 4 deletions core/state/change_set_writer.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package state

import (
"context"
"fmt"

"github.com/holiman/uint256"
Expand Down Expand Up @@ -84,7 +83,7 @@ func accountsEqual(a1, a2 *accounts.Account) bool {
return true
}

func (w *ChangeSetWriter) UpdateAccountData(ctx context.Context, address common.Address, original, account *accounts.Account) error {
func (w *ChangeSetWriter) UpdateAccountData(address common.Address, original, account *accounts.Account) error {
if !accountsEqual(original, account) || w.storageChanged[address] {
w.accountChanges[address] = originalAccountData(original, true /*omitHashes*/)
}
Expand All @@ -95,12 +94,12 @@ func (w *ChangeSetWriter) UpdateAccountCode(address common.Address, incarnation
return nil
}

func (w *ChangeSetWriter) DeleteAccount(ctx context.Context, address common.Address, original *accounts.Account) error {
func (w *ChangeSetWriter) DeleteAccount(address common.Address, original *accounts.Account) error {
w.accountChanges[address] = originalAccountData(original, false)
return nil
}

func (w *ChangeSetWriter) WriteAccountStorage(ctx context.Context, address common.Address, incarnation uint64, key *common.Hash, original, value *uint256.Int) error {
func (w *ChangeSetWriter) WriteAccountStorage(address common.Address, incarnation uint64, key *common.Hash, original, value *uint256.Int) error {
if *original == *value {
return nil
}
Expand Down
14 changes: 6 additions & 8 deletions core/state/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
package state

import (
"context"

"github.com/holiman/uint256"
"github.com/ledgerwatch/erigon/common"
"github.com/ledgerwatch/erigon/core/types/accounts"
Expand All @@ -41,10 +39,10 @@ type StateReader interface {
}

type StateWriter interface {
UpdateAccountData(ctx context.Context, address common.Address, original, account *accounts.Account) error
UpdateAccountData(address common.Address, original, account *accounts.Account) error
UpdateAccountCode(address common.Address, incarnation uint64, codeHash common.Hash, code []byte) error
DeleteAccount(ctx context.Context, address common.Address, original *accounts.Account) error
WriteAccountStorage(ctx context.Context, address common.Address, incarnation uint64, key *common.Hash, original, value *uint256.Int) error
DeleteAccount(address common.Address, original *accounts.Account) error
WriteAccountStorage(address common.Address, incarnation uint64, key *common.Hash, original, value *uint256.Int) error
CreateContract(address common.Address) error
}

Expand All @@ -63,19 +61,19 @@ func NewNoopWriter() *NoopWriter {
return noopWriter
}

func (nw *NoopWriter) UpdateAccountData(_ context.Context, address common.Address, original, account *accounts.Account) error {
func (nw *NoopWriter) UpdateAccountData(address common.Address, original, account *accounts.Account) error {
return nil
}

func (nw *NoopWriter) DeleteAccount(_ context.Context, address common.Address, original *accounts.Account) error {
func (nw *NoopWriter) DeleteAccount(address common.Address, original *accounts.Account) error {
return nil
}

func (nw *NoopWriter) UpdateAccountCode(address common.Address, incarnation uint64, codeHash common.Hash, code []byte) error {
return nil
}

func (nw *NoopWriter) WriteAccountStorage(_ context.Context, address common.Address, incarnation uint64, key *common.Hash, original, value *uint256.Int) error {
func (nw *NoopWriter) WriteAccountStorage(address common.Address, incarnation uint64, key *common.Hash, original, value *uint256.Int) error {
return nil
}

Expand Down
13 changes: 6 additions & 7 deletions core/state/db_state_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package state

import (
"bytes"
"context"
"encoding/binary"
"fmt"

Expand Down Expand Up @@ -60,8 +59,8 @@ func originalAccountData(original *accounts.Account, omitHashes bool) []byte {
return originalData
}

func (dsw *DbStateWriter) UpdateAccountData(ctx context.Context, address common.Address, original, account *accounts.Account) error {
if err := dsw.csw.UpdateAccountData(ctx, address, original, account); err != nil {
func (dsw *DbStateWriter) UpdateAccountData(address common.Address, original, account *accounts.Account) error {
if err := dsw.csw.UpdateAccountData(address, original, account); err != nil {
return err
}
addrHash, err := common.HashData(address[:])
Expand All @@ -76,8 +75,8 @@ func (dsw *DbStateWriter) UpdateAccountData(ctx context.Context, address common.
return nil
}

func (dsw *DbStateWriter) DeleteAccount(ctx context.Context, address common.Address, original *accounts.Account) error {
if err := dsw.csw.DeleteAccount(ctx, address, original); err != nil {
func (dsw *DbStateWriter) DeleteAccount(address common.Address, original *accounts.Account) error {
if err := dsw.csw.DeleteAccount(address, original); err != nil {
return err
}
addrHash, err := common.HashData(address[:])
Expand Down Expand Up @@ -116,9 +115,9 @@ func (dsw *DbStateWriter) UpdateAccountCode(address common.Address, incarnation
return nil
}

func (dsw *DbStateWriter) WriteAccountStorage(ctx context.Context, address common.Address, incarnation uint64, key *common.Hash, original, value *uint256.Int) error {
func (dsw *DbStateWriter) WriteAccountStorage(address common.Address, incarnation uint64, key *common.Hash, original, value *uint256.Int) error {
// 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 {
if err := dsw.csw.WriteAccountStorage(address, incarnation, key, original, value); err != nil {
return err
}
if *original == *value {
Expand Down
20 changes: 6 additions & 14 deletions core/state/history_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package state

import (
"context"
"fmt"
"math/rand"
"reflect"
Expand Down Expand Up @@ -31,11 +30,10 @@ func TestMutationDeleteTimestamp(t *testing.T) {
acc := make([]*accounts.Account, 10)
addr := make([]common.Address, 10)
blockWriter := NewPlainStateWriter(tx, tx, 1)
ctx := context.Background()
emptyAccount := accounts.NewAccount()
for i := range acc {
acc[i], addr[i] = randomAccount(t)
if err := blockWriter.UpdateAccountData(ctx, addr[i], &emptyAccount /* original */, acc[i]); err != nil {
if err := blockWriter.UpdateAccountData(addr[i], &emptyAccount, acc[i]); err != nil {
t.Fatal(err)
}
}
Expand Down Expand Up @@ -221,7 +219,6 @@ func generateAccountsWithStorageAndHistory(t *testing.T, blockWriter *PlainState
accStateStorage := make([]map[common.Hash]uint256.Int, numOfAccounts)
accHistoryStateStorage := make([]map[common.Hash]uint256.Int, numOfAccounts)
addrs := make([]common.Address, numOfAccounts)
ctx := context.Background()
for i := range accHistory {
accHistory[i], addrs[i] = randomAccount(t)
accHistory[i].Balance = *uint256.NewInt(100)
Expand All @@ -245,11 +242,11 @@ func generateAccountsWithStorageAndHistory(t *testing.T, blockWriter *PlainState

value := uint256.NewInt(uint64(10 + j))
accHistoryStateStorage[i][key] = *value
if err := blockWriter.WriteAccountStorage(ctx, addrs[i], accHistory[i].Incarnation, &key, value, newValue); err != nil {
if err := blockWriter.WriteAccountStorage(addrs[i], accHistory[i].Incarnation, &key, value, newValue); err != nil {
t.Fatal(err)
}
}
if err := blockWriter.UpdateAccountData(ctx, addrs[i], accHistory[i] /* original */, accState[i]); err != nil {
if err := blockWriter.UpdateAccountData(addrs[i], accHistory[i], accState[i]); err != nil {
t.Fatal(err)
}
}
Expand Down Expand Up @@ -1097,11 +1094,11 @@ type accData struct {
func writeBlockData(t *testing.T, blockWriter *PlainStateWriter, data []accData) {
for i := range data {
if data[i].newVal != nil {
if err := blockWriter.UpdateAccountData(context.Background(), data[i].addr, data[i].oldVal, data[i].newVal); err != nil {
if err := blockWriter.UpdateAccountData(data[i].addr, data[i].oldVal, data[i].newVal); err != nil {
t.Fatal(err)
}
} else {
if err := blockWriter.DeleteAccount(context.Background(), data[i].addr, data[i].oldVal); err != nil {
if err := blockWriter.DeleteAccount(data[i].addr, data[i].oldVal); err != nil {
t.Fatal(err)
}
}
Expand All @@ -1126,12 +1123,7 @@ type storageData struct {
func writeStorageBlockData(t *testing.T, blockWriter *PlainStateWriter, data []storageData) {

for i := range data {
if err := blockWriter.WriteAccountStorage(context.Background(),
data[i].addr,
data[i].inc,
&data[i].key,
data[i].oldVal,
data[i].newVal); err != nil {
if err := blockWriter.WriteAccountStorage(data[i].addr, data[i].inc, &data[i].key, data[i].oldVal, data[i].newVal); err != nil {
t.Fatal(err)
}
}
Expand Down
16 changes: 9 additions & 7 deletions core/state/intra_block_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -702,10 +702,10 @@ func (sdb *IntraBlockState) GetRefund() uint64 {
return sdb.refund
}

func updateAccount(ctx context.Context, stateWriter StateWriter, addr common.Address, stateObject *stateObject, isDirty bool) error {
emptyRemoval := params.GetForkFlag(ctx, params.IsEIP158Enabled) && stateObject.empty()
func updateAccount(EIP158Enabled bool, stateWriter StateWriter, addr common.Address, stateObject *stateObject, isDirty bool) error {
emptyRemoval := EIP158Enabled && stateObject.empty()
if stateObject.suicided || (isDirty && emptyRemoval) {
if err := stateWriter.DeleteAccount(ctx, addr, &stateObject.original); err != nil {
if err := stateWriter.DeleteAccount(addr, &stateObject.original); err != nil {
return err
}
stateObject.deleted = true
Expand All @@ -723,10 +723,10 @@ func updateAccount(ctx context.Context, stateWriter StateWriter, addr common.Add
return err
}
}
if err := stateObject.updateTrie(ctx, stateWriter); err != nil {
if err := stateObject.updateTrie(stateWriter); err != nil {
return err
}
if err := stateWriter.UpdateAccountData(ctx, addr, &stateObject.original, &stateObject.data); err != nil {
if err := stateWriter.UpdateAccountData(addr, &stateObject.original, &stateObject.data); err != nil {
return err
}
}
Expand All @@ -735,6 +735,7 @@ func updateAccount(ctx context.Context, stateWriter StateWriter, addr common.Add

// FinalizeTx should be called after every transaction.
func (sdb *IntraBlockState) FinalizeTx(ctx context.Context, stateWriter StateWriter) error {
EIP158Enabled := params.GetForkFlag(ctx, params.IsEIP158Enabled)
for addr := range sdb.journal.dirties {
stateObject, exist := sdb.stateObjects[addr]
if !exist {
Expand All @@ -747,7 +748,7 @@ func (sdb *IntraBlockState) FinalizeTx(ctx context.Context, stateWriter StateWri
continue
}

if err := updateAccount(ctx, stateWriter, addr, stateObject, true); err != nil {
if err := updateAccount(EIP158Enabled, stateWriter, addr, stateObject, true); err != nil {
return err
}

Expand All @@ -761,12 +762,13 @@ func (sdb *IntraBlockState) FinalizeTx(ctx context.Context, stateWriter StateWri
// CommitBlock finalizes the state by removing the self destructed objects
// and clears the journal as well as the refunds.
func (sdb *IntraBlockState) CommitBlock(ctx context.Context, stateWriter StateWriter) error {
EIP158Enabled := params.GetForkFlag(ctx, params.IsEIP158Enabled)
for addr := range sdb.journal.dirties {
sdb.stateObjectsDirty[addr] = struct{}{}
}
for addr, stateObject := range sdb.stateObjects {
_, isDirty := sdb.stateObjectsDirty[addr]
if err := updateAccount(ctx, stateWriter, addr, stateObject, isDirty); err != nil {
if err := updateAccount(EIP158Enabled, stateWriter, addr, stateObject, isDirty); err != nil {
return err
}
}
Expand Down
12 changes: 6 additions & 6 deletions core/state/plain_readonly.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,19 +262,19 @@ func (dbs *PlainDBState) ReadAccountIncarnation(address common.Address) (uint64,
return acc.Incarnation - 1, nil
}

func (dbs *PlainDBState) UpdateAccountData(_ context.Context, address common.Address, original, account *accounts.Account) error {
func (dbs *PlainDBState) UpdateAccountData(address common.Address, original, account *accounts.Account) error {
return nil
}

func (dbs *PlainDBState) DeleteAccount(_ context.Context, address common.Address, original *accounts.Account) error {
func (dbs *PlainDBState) DeleteAccount(address common.Address, original *accounts.Account) error {
return nil
}

func (dbs *PlainDBState) UpdateAccountCode(address common.Address, incarnation uint64, codeHash common.Hash, code []byte) error {
return nil
}

func (dbs *PlainDBState) WriteAccountStorage(_ context.Context, address common.Address, incarnation uint64, key *common.Hash, original, value *uint256.Int) error {
func (dbs *PlainDBState) WriteAccountStorage(address common.Address, incarnation uint64, key *common.Hash, original, value *uint256.Int) error {
t, ok := dbs.storage[address]
if !ok {
t = llrb.New()
Expand Down Expand Up @@ -470,19 +470,19 @@ func (s *PlainKVState) ReadAccountIncarnation(address common.Address) (uint64, e
return acc.Incarnation - 1, nil
}

func (s *PlainKVState) UpdateAccountData(_ context.Context, address common.Address, original, account *accounts.Account) error {
func (s *PlainKVState) UpdateAccountData(address common.Address, original, account *accounts.Account) error {
return nil
}

func (s *PlainKVState) DeleteAccount(_ context.Context, address common.Address, original *accounts.Account) error {
func (s *PlainKVState) DeleteAccount(address common.Address, original *accounts.Account) error {
return nil
}

func (s *PlainKVState) UpdateAccountCode(address common.Address, incarnation uint64, codeHash common.Hash, code []byte) error {
return nil
}

func (s *PlainKVState) WriteAccountStorage(_ context.Context, address common.Address, incarnation uint64, key *common.Hash, original, value *uint256.Int) error {
func (s *PlainKVState) WriteAccountStorage(address common.Address, incarnation uint64, key *common.Hash, original, value *uint256.Int) error {
t, ok := s.storage[address]
if !ok {
t = llrb.New()
Expand Down
Loading

0 comments on commit 9683b26

Please sign in to comment.