From 4e387ee51a284c94998908f79a91c5b475d9f8c6 Mon Sep 17 00:00:00 2001 From: Ganesh Vanahalli Date: Fri, 22 Nov 2024 21:14:36 +0530 Subject: [PATCH 1/4] Filter transaction --- core/state/statedb.go | 21 +++++++++++++++++++++ core/vm/interface.go | 4 ++++ 2 files changed, 25 insertions(+) diff --git a/core/state/statedb.go b/core/state/statedb.go index acdad20481..96cbca89c2 100644 --- a/core/state/statedb.go +++ b/core/state/statedb.go @@ -19,6 +19,7 @@ package state import ( "bytes" + "errors" "fmt" "maps" "math/big" @@ -86,6 +87,7 @@ func (m *mutation) isDelete() bool { // commit states. type StateDB struct { arbExtraData *ArbitrumExtraData // must be a pointer - can't be a part of StateDB allocation, otherwise its finalizer might not get called + arbTxFilter int db Database prefetcher *triePrefetcher @@ -219,6 +221,19 @@ func New(root common.Hash, db Database, snaps *snapshot.Tree) (*StateDB, error) return sdb, nil } +func (s *StateDB) FilterTx(withBlock bool) { + if s.arbTxFilter == 0 { + s.arbTxFilter = 1 + if withBlock { + s.arbTxFilter = 2 + } + } +} + +func (s *StateDB) IsTxValid() bool { + return s.arbTxFilter == 1 +} + // SetLogger sets the logger for account update hooks. func (s *StateDB) SetLogger(l *tracing.Hooks) { s.logger = l @@ -835,6 +850,9 @@ func (s *StateDB) RevertToSnapshot(revid int) { revision := s.validRevisions[idx] snapshot := revision.journalIndex s.arbExtraData.unexpectedBalanceDelta = new(big.Int).Set(revision.unexpectedBalanceDelta) + if s.arbTxFilter == 1 { + s.arbTxFilter = 0 + } // Replay the journal to undo changes and remove invalidated snapshots s.journal.revert(s, snapshot) @@ -1220,6 +1238,9 @@ func (s *StateDB) GetTrie() Trie { // The associated block number of the state transition is also provided // for more chain context. func (s *StateDB) Commit(block uint64, deleteEmptyObjects bool) (common.Hash, error) { + if s.arbTxFilter == 2 { + return common.Hash{}, errors.New("internal error") + } // Short circuit in case any database failure occurred earlier. if s.dbErr != nil { return common.Hash{}, fmt.Errorf("commit aborted due to earlier error: %v", s.dbErr) diff --git a/core/vm/interface.go b/core/vm/interface.go index 9b7e26ad59..99e94031cd 100644 --- a/core/vm/interface.go +++ b/core/vm/interface.go @@ -48,6 +48,10 @@ type StateDB interface { // Arbitrum: preserve old empty account behavior CreateZombieIfDeleted(common.Address) + // Arbitrum + FilterTx(bool) + IsTxValid() bool + Deterministic() bool Database() state.Database From 4bc2200ded26c931bff6f52ac30e2b1e1b5ef091 Mon Sep 17 00:00:00 2001 From: Ganesh Vanahalli Date: Fri, 22 Nov 2024 21:21:28 +0530 Subject: [PATCH 2/4] rectify method name --- core/state/statedb.go | 2 +- core/vm/interface.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/core/state/statedb.go b/core/state/statedb.go index 96cbca89c2..7ad7ff7e6c 100644 --- a/core/state/statedb.go +++ b/core/state/statedb.go @@ -230,7 +230,7 @@ func (s *StateDB) FilterTx(withBlock bool) { } } -func (s *StateDB) IsTxValid() bool { +func (s *StateDB) IsTxInvalid() bool { return s.arbTxFilter == 1 } diff --git a/core/vm/interface.go b/core/vm/interface.go index 99e94031cd..f36fc8ffcc 100644 --- a/core/vm/interface.go +++ b/core/vm/interface.go @@ -50,7 +50,7 @@ type StateDB interface { // Arbitrum FilterTx(bool) - IsTxValid() bool + IsTxInvalid() bool Deterministic() bool Database() state.Database From cf0ca286a6e6cb435fc2331078a382f5efdaadb3 Mon Sep 17 00:00:00 2001 From: Ganesh Vanahalli Date: Tue, 26 Nov 2024 15:29:08 +0530 Subject: [PATCH 3/4] address PR comments --- core/state/statedb.go | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/core/state/statedb.go b/core/state/statedb.go index 7ad7ff7e6c..c3534cabfd 100644 --- a/core/state/statedb.go +++ b/core/state/statedb.go @@ -74,6 +74,15 @@ func (m *mutation) isDelete() bool { return m.typ == deletion } +type arbFiltered int + +const ( + txFiltered arbFiltered = 1 + iota + blockFiltered +) + +var ErrArbTxFilter error = errors.New("internal error") + // StateDB structs within the ethereum protocol are used to store anything // within the merkle trie. StateDBs take care of caching and storing // nested states. It's the general query interface to retrieve: @@ -87,7 +96,7 @@ func (m *mutation) isDelete() bool { // commit states. type StateDB struct { arbExtraData *ArbitrumExtraData // must be a pointer - can't be a part of StateDB allocation, otherwise its finalizer might not get called - arbTxFilter int + arbTxFilter arbFiltered db Database prefetcher *triePrefetcher @@ -223,15 +232,15 @@ func New(root common.Hash, db Database, snaps *snapshot.Tree) (*StateDB, error) func (s *StateDB) FilterTx(withBlock bool) { if s.arbTxFilter == 0 { - s.arbTxFilter = 1 + s.arbTxFilter = txFiltered if withBlock { - s.arbTxFilter = 2 + s.arbTxFilter = blockFiltered } } } func (s *StateDB) IsTxInvalid() bool { - return s.arbTxFilter == 1 + return s.arbTxFilter == txFiltered } // SetLogger sets the logger for account update hooks. @@ -850,7 +859,7 @@ func (s *StateDB) RevertToSnapshot(revid int) { revision := s.validRevisions[idx] snapshot := revision.journalIndex s.arbExtraData.unexpectedBalanceDelta = new(big.Int).Set(revision.unexpectedBalanceDelta) - if s.arbTxFilter == 1 { + if s.arbTxFilter == txFiltered { s.arbTxFilter = 0 } @@ -1238,8 +1247,8 @@ func (s *StateDB) GetTrie() Trie { // The associated block number of the state transition is also provided // for more chain context. func (s *StateDB) Commit(block uint64, deleteEmptyObjects bool) (common.Hash, error) { - if s.arbTxFilter == 2 { - return common.Hash{}, errors.New("internal error") + if s.arbTxFilter == blockFiltered { + return common.Hash{}, ErrArbTxFilter } // Short circuit in case any database failure occurred earlier. if s.dbErr != nil { From b2541cfd2120a39bc7bbd1854b96718583506c56 Mon Sep 17 00:00:00 2001 From: Ganesh Vanahalli Date: Tue, 26 Nov 2024 16:06:59 +0530 Subject: [PATCH 4/4] add unfiltered to enum --- core/state/statedb.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/core/state/statedb.go b/core/state/statedb.go index c3534cabfd..cfb692e6b3 100644 --- a/core/state/statedb.go +++ b/core/state/statedb.go @@ -77,7 +77,8 @@ func (m *mutation) isDelete() bool { type arbFiltered int const ( - txFiltered arbFiltered = 1 + iota + unFiltered arbFiltered = iota + txFiltered blockFiltered ) @@ -231,7 +232,7 @@ func New(root common.Hash, db Database, snaps *snapshot.Tree) (*StateDB, error) } func (s *StateDB) FilterTx(withBlock bool) { - if s.arbTxFilter == 0 { + if s.arbTxFilter == unFiltered { s.arbTxFilter = txFiltered if withBlock { s.arbTxFilter = blockFiltered @@ -860,7 +861,7 @@ func (s *StateDB) RevertToSnapshot(revid int) { snapshot := revision.journalIndex s.arbExtraData.unexpectedBalanceDelta = new(big.Int).Set(revision.unexpectedBalanceDelta) if s.arbTxFilter == txFiltered { - s.arbTxFilter = 0 + s.arbTxFilter = unFiltered } // Replay the journal to undo changes and remove invalidated snapshots