Skip to content

Commit

Permalink
txdag: clean codes;
Browse files Browse the repository at this point in the history
  • Loading branch information
galaio committed Sep 4, 2024
1 parent 8083406 commit 9570ab0
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 197 deletions.
15 changes: 9 additions & 6 deletions core/state/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -947,6 +947,7 @@ func (s *StateDB) Finalise(deleteEmptyObjects bool) {
s.mvStates.RecordAccountWrite(addr, types.AccountSuicide)
}
}
s.stateObjectsDestructDirty = make(map[common.Address]*types.StateAccount)
for addr := range s.journal.dirties {
obj, exist := s.stateObjects[addr]
if !exist {
Expand Down Expand Up @@ -992,7 +993,9 @@ func (s *StateDB) Finalise(deleteEmptyObjects bool) {
// the commit-phase will be a lot faster
addressesToPrefetch = append(addressesToPrefetch, common.CopyBytes(addr[:])) // Copy needed for closure
}

if s.mvStates != nil {
s.mvStates.RecordWriteDone()
}
if s.prefetcher != nil && len(addressesToPrefetch) > 0 {
s.prefetcher.prefetch(common.Hash{}, s.originalRoot, common.Address{}, addressesToPrefetch)
}
Expand Down Expand Up @@ -1709,6 +1712,7 @@ func (s *StateDB) BeginTxRecorder(isExcludeTx bool) {
if s.mvStates == nil {
return
}
log.Debug("BeginTxRecorder", "tx", s.txIndex)
if isExcludeTx {
rwSet := types.NewRWSet(s.txIndex).WithExcludedTxFlag()
if err := s.mvStates.FinaliseWithRWSet(rwSet); err != nil {
Expand All @@ -1724,9 +1728,9 @@ func (s *StateDB) ResetMVStates(txCount int, feeReceivers []common.Address) *typ
return s.mvStates
}

func (s *StateDB) CheckFeeReceiversRWSet() error {
func (s *StateDB) CheckFeeReceiversRWSet() {
if s.mvStates == nil {
return nil
return
}
if metrics.EnabledExpensive {
defer func(start time.Time) {
Expand All @@ -1740,17 +1744,16 @@ func (s *StateDB) CheckFeeReceiversRWSet() error {
continue
}
s.mvStates.RecordCannotDelayGasFee()
return nil
return
}

for _, addr := range feeReceivers {
if _, ok := s.journal.dirties[addr]; !ok {
continue
}
s.mvStates.RecordCannotDelayGasFee()
return nil
return
}
return nil
}

func (s *StateDB) getStateObjectsDestruct(addr common.Address) (*types.StateAccount, bool) {
Expand Down
6 changes: 2 additions & 4 deletions core/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
cmath "github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/params"
)

Expand Down Expand Up @@ -535,9 +534,8 @@ func (st *StateTransition) innerTransitionDb() (*ExecutionResult, error) {
}

// check fee receiver rwSet here
if ferr := st.state.CheckFeeReceiversRWSet(); ferr != nil {
log.Error("CheckFeeReceiversRWSet err", "block", st.evm.Context.BlockNumber, "tx", st.evm.StateDB.TxIndex(), "err", ferr)
}
st.state.CheckFeeReceiversRWSet()

effectiveTip := msg.GasPrice
if rules.IsLondon {
effectiveTip = cmath.BigMin(msg.GasTipCap, new(big.Int).Sub(msg.GasFeeCap, st.evm.Context.BaseFee))
Expand Down
112 changes: 26 additions & 86 deletions core/types/mvstates.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/log"
"github.com/holiman/uint256"
"golang.org/x/exp/slices"
)

Expand Down Expand Up @@ -56,10 +55,6 @@ func NewRWSet(index int) *RWSet {
}
}

func (s *RWSet) Index() int {
return s.index
}

func (s *RWSet) RecordAccountRead(addr common.Address, state AccountState) {
// only record the first read version
sub, ok := s.accReadSet[addr]
Expand Down Expand Up @@ -105,26 +100,6 @@ func (s *RWSet) RecordStorageWrite(addr common.Address, slot common.Hash) {
s.slotWriteSet[addr][slot] = struct{}{}
}

func (s *RWSet) queryAccReadItem(addr common.Address, state AccountState) bool {
sub, ok := s.accReadSet[addr]
if !ok {
return false
}

_, ok = sub[state]
return ok
}

func (s *RWSet) querySlotReadItem(addr common.Address, slot common.Hash) bool {
sub, ok := s.slotReadSet[addr]
if !ok {
return false
}

_, ok = sub[slot]
return ok
}

func (s *RWSet) ReadSet() (map[common.Address]map[AccountState]struct{}, map[common.Address]map[common.Hash]struct{}) {
return s.accReadSet, s.slotReadSet
}
Expand Down Expand Up @@ -275,11 +250,13 @@ type MVStates struct {
txDepCache []TxDep

// async rw event recorder
// these fields are only used in one routine
asyncRWSet *RWSet
rwEventCh chan []RWEventItem
rwEventCache []RWEventItem
rwEventCacheIndex int
recordeReadDone bool
recordingRead bool
recordingWrite bool

// execution stat infos
lock sync.RWMutex
Expand Down Expand Up @@ -407,6 +384,7 @@ func (s *MVStates) finalisePreviousRWSet() {
}
if _, exist := s.asyncRWSet.accReadSet[addr][AccountSelf]; exist {
s.cannotGasFeeDelay = true
break
}
}
s.resolveDepsMapCacheByWrites(index, s.asyncRWSet)
Expand All @@ -423,12 +401,21 @@ func (s *MVStates) RecordNewTx(index int) {
})
}
s.rwEventCacheIndex++
s.recordeReadDone = false
s.recordingRead = true
s.recordingWrite = true
s.BatchRecordHandle()
}

func (s *MVStates) RecordReadDone() {
s.recordingRead = false
}

func (s *MVStates) RecordWriteDone() {
s.recordingWrite = false
}

func (s *MVStates) RecordAccountRead(addr common.Address, state AccountState) {
if s.recordeReadDone {
if !s.recordingRead {
return
}
if s.rwEventCacheIndex < len(s.rwEventCache) {
Expand All @@ -447,7 +434,7 @@ func (s *MVStates) RecordAccountRead(addr common.Address, state AccountState) {
}

func (s *MVStates) RecordStorageRead(addr common.Address, slot common.Hash) {
if s.recordeReadDone {
if !s.recordingRead {
return
}
if s.rwEventCacheIndex < len(s.rwEventCache) {
Expand All @@ -465,11 +452,10 @@ func (s *MVStates) RecordStorageRead(addr common.Address, slot common.Hash) {
s.rwEventCacheIndex++
}

func (s *MVStates) RecordReadDone() {
s.recordeReadDone = true
}

func (s *MVStates) RecordAccountWrite(addr common.Address, state AccountState) {
if !s.recordingWrite {
return
}
if s.rwEventCacheIndex < len(s.rwEventCache) {
s.rwEventCache[s.rwEventCacheIndex].Event = WriteAccRWEvent
s.rwEventCache[s.rwEventCacheIndex].Addr = addr
Expand All @@ -486,6 +472,9 @@ func (s *MVStates) RecordAccountWrite(addr common.Address, state AccountState) {
}

func (s *MVStates) RecordStorageWrite(addr common.Address, slot common.Hash) {
if !s.recordingWrite {
return
}
if s.rwEventCacheIndex < len(s.rwEventCache) {
s.rwEventCache[s.rwEventCacheIndex].Event = WriteSlotRWEvent
s.rwEventCache[s.rwEventCacheIndex].Addr = addr
Expand All @@ -502,6 +491,9 @@ func (s *MVStates) RecordStorageWrite(addr common.Address, slot common.Hash) {
}

func (s *MVStates) RecordCannotDelayGasFee() {
if !s.recordingWrite {
return
}
if s.rwEventCacheIndex < len(s.rwEventCache) {
s.rwEventCache[s.rwEventCacheIndex].Event = CannotGasFeeDelayRWEvent
s.rwEventCacheIndex++
Expand All @@ -514,7 +506,7 @@ func (s *MVStates) RecordCannotDelayGasFee() {
}

func (s *MVStates) BatchRecordHandle() {
if len(s.rwEventCache) == 0 {
if s.rwEventCacheIndex == 0 {
return
}
s.rwEventCh <- s.rwEventCache[:s.rwEventCacheIndex]
Expand Down Expand Up @@ -543,9 +535,6 @@ func (s *MVStates) quickFinaliseWithRWSet(rwSet *RWSet) error {
func (s *MVStates) FinaliseWithRWSet(rwSet *RWSet) error {
s.lock.Lock()
defer s.lock.Unlock()
if s.asyncRWSet == nil {
s.asyncRWSet = nil
}
index := rwSet.index
if s.nextFinaliseIndex > index {
return fmt.Errorf("finalise in wrong order, next: %d, input: %d", s.nextFinaliseIndex, index)
Expand All @@ -561,7 +550,6 @@ func (s *MVStates) FinaliseWithRWSet(rwSet *RWSet) error {
"readCnt", len(s.rwSets[i].accReadSet)+len(s.rwSets[i].slotReadSet),
"writeCnt", len(s.rwSets[i].accWriteSet)+len(s.rwSets[i].slotWriteSet))
}
s.rwSets[index] = rwSet

return nil
}
Expand Down Expand Up @@ -813,15 +801,6 @@ func checkSlotDependency(writeSet map[common.Address]map[common.Hash]struct{}, r
return false
}

type TxDepMaker interface {
add(index uint64)
exist(index uint64) bool
deps() []uint64
remove(index uint64)
len() int
reset()
}

type TxDepMap struct {
tm map[uint64]struct{}
cache []uint64
Expand Down Expand Up @@ -864,42 +843,3 @@ func (m *TxDepMap) remove(index uint64) {
func (m *TxDepMap) len() int {
return len(m.tm)
}

func (m *TxDepMap) reset() {
m.cache = nil
m.tm = make(map[uint64]struct{})
}

// isEqualRWVal compare state
func isEqualRWVal(accState *AccountState, src interface{}, compared interface{}) bool {
if accState != nil {
switch *accState {
case AccountBalance:
if src != nil && compared != nil {
return equalUint256(src.(*uint256.Int), compared.(*uint256.Int))
}
return src == compared
case AccountNonce:
return src.(uint64) == compared.(uint64)
case AccountCodeHash:
if src != nil && compared != nil {
return slices.Equal(src.([]byte), compared.([]byte))
}
return src == compared
}
return false
}

if src != nil && compared != nil {
return src.(common.Hash) == compared.(common.Hash)
}
return src == compared
}

func equalUint256(s, c *uint256.Int) bool {
if s != nil && c != nil {
return s.Eq(c)
}

return s == c
}
Loading

0 comments on commit 9570ab0

Please sign in to comment.