Skip to content

Commit

Permalink
Merge branch 'master' into rm_blockhashop
Browse files Browse the repository at this point in the history
  • Loading branch information
tsahee authored Dec 30, 2024
2 parents 15fccba + 57663a5 commit 80ec023
Show file tree
Hide file tree
Showing 11 changed files with 158 additions and 10 deletions.
18 changes: 18 additions & 0 deletions common/lru/blob_lru.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,21 @@ func (c *SizeConstrainedCache[K, V]) Get(key K) (V, bool) {

return c.lru.Get(key)
}

func (c *SizeConstrainedCache[K, V]) Remove(key K) {
c.lock.Lock()
defer c.lock.Unlock()

if v, ok := c.lru.Peek(key); ok {
c.size -= uint64(len(v))
c.lru.Remove(key)
}
}

func (c *SizeConstrainedCache[K, V]) Clear() {
c.lock.Lock()
defer c.lock.Unlock()

c.lru.Purge()
c.size = 0
}
16 changes: 16 additions & 0 deletions core/state/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,18 @@ func New(root common.Hash, db Database, snaps *snapshot.Tree) (*StateDB, error)
return sdb, nil
}

func (s *StateDB) FilterTx() {
s.arbExtraData.arbTxFilter = true
}

func (s *StateDB) ClearTxFilter() {
s.arbExtraData.arbTxFilter = false
}

func (s *StateDB) IsTxFiltered() bool {
return s.arbExtraData.arbTxFilter
}

// SetLogger sets the logger for account update hooks.
func (s *StateDB) SetLogger(l *tracing.Hooks) {
s.logger = l
Expand Down Expand Up @@ -736,6 +748,7 @@ func (s *StateDB) Copy() *StateDB {
recentWasms: s.arbExtraData.recentWasms.Copy(),
openWasmPages: s.arbExtraData.openWasmPages,
everWasmPages: s.arbExtraData.everWasmPages,
arbTxFilter: s.arbExtraData.arbTxFilter,
},

db: s.db,
Expand Down Expand Up @@ -1220,6 +1233,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.arbExtraData.arbTxFilter {
return common.Hash{}, ErrArbTxFilter
}
// 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)
Expand Down
3 changes: 3 additions & 0 deletions core/state/statedb_arbitrum.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,16 @@ func (s *StateDB) Deterministic() bool {
return s.deterministic
}

var ErrArbTxFilter error = errors.New("internal error")

type ArbitrumExtraData struct {
unexpectedBalanceDelta *big.Int // total balance change across all accounts
userWasms UserWasms // user wasms encountered during execution
openWasmPages uint16 // number of pages currently open
everWasmPages uint16 // largest number of pages ever allocated during this tx's execution
activatedWasms map[common.Hash]ActivatedWasm // newly activated WASMs
recentWasms RecentWasms
arbTxFilter bool
}

func (s *StateDB) SetArbFinalizer(f func(*ArbitrumExtraData)) {
Expand Down
43 changes: 41 additions & 2 deletions core/types/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,54 @@ type Transaction struct {
inner TxData // Consensus contents of a transaction
time time.Time // Time first seen locally (spam avoidance)

// Arbitrum cache: must be atomically accessed
CalldataUnits uint64
// Arbitrum cache of the calldata units at a brotli compression level.
// The top 8 bits are the brotli compression level last used to compute this,
// and the remaining 56 bits are the calldata units at that compression level.
calldataUnitsForBrotliCompressionLevel atomic.Uint64

// caches
hash atomic.Pointer[common.Hash]
size atomic.Uint64
from atomic.Pointer[sigCache]
}

// GetRawCachedCalldataUnits returns the cached brotli compression level and corresponding calldata units,
// or (0, 0) if the cache is empty.
func (tx *Transaction) GetRawCachedCalldataUnits() (uint64, uint64) {
repr := tx.calldataUnitsForBrotliCompressionLevel.Load()
cachedCompressionLevel := repr >> 56
calldataUnits := repr & ((1 << 56) - 1)
return cachedCompressionLevel, calldataUnits
}

// GetCachedCalldataUnits returns the cached calldata units for a given brotli compression level,
// returning nil if no cache is present or the cache is for a different compression level.
func (tx *Transaction) GetCachedCalldataUnits(requestedCompressionLevel uint64) *uint64 {
cachedCompressionLevel, cachedUnits := tx.GetRawCachedCalldataUnits()
if cachedUnits == 0 {
// empty cache
return nil
}
if cachedCompressionLevel != requestedCompressionLevel {
// wrong compression level
return nil
}
return &cachedUnits
}

// SetCachedCalldataUnits sets the cached brotli compression level and corresponding calldata units,
// or clears the cache if the values are too large to fit (at least 2**8 and 2**56 respectively).
// Note that a zero calldataUnits is also treated as an empty cache.
func (tx *Transaction) SetCachedCalldataUnits(compressionLevel uint64, calldataUnits uint64) {
var repr uint64
// Ensure the compressionLevel and calldataUnits will fit.
// Otherwise, just clear the cache.
if compressionLevel < 1<<8 && calldataUnits < 1<<56 {
repr = compressionLevel<<56 | calldataUnits
}
tx.calldataUnitsForBrotliCompressionLevel.Store(repr)
}

// NewTx creates a new transaction.
func NewTx(inner TxData) *Transaction {
tx := new(Transaction)
Expand Down
49 changes: 49 additions & 0 deletions core/types/transaction_arbitrum_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2014 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

package types

import "testing"

func TestTxCalldataUnitsCache(t *testing.T) {
tx := &Transaction{}
units := tx.GetCachedCalldataUnits(0)
if units != nil {
t.Errorf("unexpected initial cache present %v for compression 0", units)
}
units = tx.GetCachedCalldataUnits(1)
if units != nil {
t.Errorf("unexpected initial cache present %v for compression 1", units)
}
tx.SetCachedCalldataUnits(200, 1000)
units = tx.GetCachedCalldataUnits(100)
if units != nil {
t.Errorf("unexpected cached units %v present for incorrect compression 100", units)
}
units = tx.GetCachedCalldataUnits(0)
if units != nil {
t.Errorf("unexpected cached units %v present for incorrect compression 0", units)
}
units = tx.GetCachedCalldataUnits(200)
if units == nil || *units != 1000 {
t.Errorf("unexpected cached units %v for correct compression 200", units)
}
tx.SetCachedCalldataUnits(1, 1<<60)
units = tx.GetCachedCalldataUnits(1)
if units != nil {
t.Errorf("unexpected cache value %v present after reset", units)
}
}
5 changes: 5 additions & 0 deletions core/vm/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ type StateDB interface {
// Arbitrum: preserve old empty account behavior
CreateZombieIfDeleted(common.Address)

// Arbitrum
FilterTx()
ClearTxFilter()
IsTxFiltered() bool

Deterministic() bool
Database() state.Database

Expand Down
1 change: 1 addition & 0 deletions ethdb/pebble/extraoptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package pebble
import "time"

type ExtraOptions struct {
SyncMode bool
BytesPerSync int
L0CompactionFileThreshold int
L0CompactionThreshold int
Expand Down
2 changes: 1 addition & 1 deletion ethdb/pebble/pebble.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ func New(file string, cache int, handles int, namespace string, readonly bool, e
fn: file,
log: logger,
quitChan: make(chan chan error),
writeOptions: &pebble.WriteOptions{Sync: !ephemeral},
writeOptions: &pebble.WriteOptions{Sync: !ephemeral && extraOptions.SyncMode},
}
opt := &pebble.Options{
// Pebble has a single combined cache area and the write
Expand Down
4 changes: 2 additions & 2 deletions params/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -574,15 +574,15 @@ func (c *ChainConfig) IsTerminalPoWBlock(parentTotalDiff *big.Int, totalDiff *bi
// IsShanghai returns whether time is either equal to the Shanghai fork time or greater.
func (c *ChainConfig) IsShanghai(num *big.Int, time uint64, currentArbosVersion uint64) bool {
if c.IsArbitrum() {
return currentArbosVersion >= 11
return currentArbosVersion >= ArbosVersion_11
}
return c.IsLondon(num) && isTimestampForked(c.ShanghaiTime, time)
}

// IsCancun returns whether num is either equal to the Cancun fork time or greater.
func (c *ChainConfig) IsCancun(num *big.Int, time uint64, currentArbosVersion uint64) bool {
if c.IsArbitrum() {
return currentArbosVersion >= 20
return currentArbosVersion >= ArbosVersion_20
}
return c.IsLondon(num) && isTimestampForked(c.CancunTime, time)
}
Expand Down
25 changes: 21 additions & 4 deletions params/config_arbitrum.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,27 @@ import (
"github.com/ethereum/go-ethereum/common"
)

const ArbosVersion_FixRedeemGas = uint64(11)
const ArbosVersion_Stylus = uint64(30)
const ArbosVersion_StylusFixes = uint64(31)
const ArbosVersion_StylusChargingFixes = uint64(32)
const ArbosVersion_2 = uint64(2)
const ArbosVersion_3 = uint64(3)
const ArbosVersion_4 = uint64(4)
const ArbosVersion_5 = uint64(5)
const ArbosVersion_6 = uint64(6)
const ArbosVersion_7 = uint64(7)
const ArbosVersion_8 = uint64(8)
const ArbosVersion_9 = uint64(9)
const ArbosVersion_10 = uint64(10)
const ArbosVersion_11 = uint64(11)
const ArbosVersion_20 = uint64(20)
const ArbosVersion_30 = uint64(30)
const ArbosVersion_31 = uint64(31)
const ArbosVersion_32 = uint64(32)

const ArbosVersion_FixRedeemGas = ArbosVersion_11
const ArbosVersion_Stylus = ArbosVersion_30
const ArbosVersion_StylusFixes = ArbosVersion_31
const ArbosVersion_StylusChargingFixes = ArbosVersion_32
const MaxArbosVersionSupported = ArbosVersion_32
const MaxDebugArbosVersionSupported = ArbosVersion_32

type ArbitrumChainParams struct {
EnableArbOS bool
Expand Down
2 changes: 1 addition & 1 deletion params/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func TestConfigRules(t *testing.T) {
t.Errorf("expected %v to not be shanghai", currentArbosVersion)
}
stamp = 500
currentArbosVersion = 11
currentArbosVersion = ArbosVersion_11
if r := c.Rules(big.NewInt(0), true, stamp, currentArbosVersion); !r.IsShanghai {
t.Errorf("expected %v to be shanghai", currentArbosVersion)
}
Expand Down

0 comments on commit 80ec023

Please sign in to comment.