Skip to content

Commit

Permalink
Turn NoPruneContracts into DepositContract (#12150)
Browse files Browse the repository at this point in the history
Cherry pick #10062
  • Loading branch information
yperbasis authored and AskAlexSharov committed Oct 21, 2024
1 parent 2a86092 commit 926f414
Show file tree
Hide file tree
Showing 12 changed files with 58 additions and 69 deletions.
2 changes: 1 addition & 1 deletion cmd/integration/commands/stages.go
Original file line number Diff line number Diff line change
Expand Up @@ -1268,7 +1268,7 @@ func stageLogIndex(db kv.RwDB, ctx context.Context, logger log.Logger) error {
logger.Info("Stage exec", "progress", execAt)
logger.Info("Stage", "name", s.ID, "progress", s.BlockNumber)

cfg := stagedsync.StageLogIndexCfg(db, pm, dirs.Tmp, chainConfig.NoPruneContracts)
cfg := stagedsync.StageLogIndexCfg(db, pm, dirs.Tmp, chainConfig.DepositContract)
if unwind > 0 {
u := sync.NewUnwindState(stages.LogIndex, s.BlockNumber-unwind, s.BlockNumber)
err = stagedsync.UnwindLogIndex(u, s, tx, cfg, ctx)
Expand Down
12 changes: 5 additions & 7 deletions erigon-lib/chain/chain_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,17 @@ type Config struct {
// (Optional) governance contract where EIP-1559 fees will be sent to that otherwise would be burnt since the London fork
BurntContract map[string]common.Address `json:"burntContract,omitempty"`

// (Optional) deposit contract of PoS chains
// See also EIP-6110: Supply validator deposits on chain
DepositContract *common.Address `json:"depositContract,omitempty"`

// Various consensus engines
Ethash *EthashConfig `json:"ethash,omitempty"`
Clique *CliqueConfig `json:"clique,omitempty"`
Aura *AuRaConfig `json:"aura,omitempty"`

Bor BorConfig `json:"-"`
BorJSON json.RawMessage `json:"bor,omitempty"`

// For not pruning the logs of these contracts
// For deposit contract logs are needed by CL to validate/produce blocks.
// All logs should be available to a validating node through eth_getLogs
NoPruneContracts map[common.Address]bool `json:"noPruneContracts,omitempty"`
}

type BorConfig interface {
Expand All @@ -104,7 +103,7 @@ type BorConfig interface {
func (c *Config) String() string {
engine := c.getEngine()

return fmt.Sprintf("{ChainID: %v, Homestead: %v, DAO: %v, Tangerine Whistle: %v, Spurious Dragon: %v, Byzantium: %v, Constantinople: %v, Petersburg: %v, Istanbul: %v, Muir Glacier: %v, Berlin: %v, London: %v, Arrow Glacier: %v, Gray Glacier: %v, Terminal Total Difficulty: %v, Merge Netsplit: %v, Shanghai: %v, Cancun: %v, Prague: %v, Osaka: %v, Engine: %v, NoPruneContracts: %v}",
return fmt.Sprintf("{ChainID: %v, Homestead: %v, DAO: %v, Tangerine Whistle: %v, Spurious Dragon: %v, Byzantium: %v, Constantinople: %v, Petersburg: %v, Istanbul: %v, Muir Glacier: %v, Berlin: %v, London: %v, Arrow Glacier: %v, Gray Glacier: %v, Terminal Total Difficulty: %v, Merge Netsplit: %v, Shanghai: %v, Cancun: %v, Prague: %v, Osaka: %v, Engine: %v}",
c.ChainID,
c.HomesteadBlock,
c.DAOForkBlock,
Expand All @@ -126,7 +125,6 @@ func (c *Config) String() string {
c.PragueTime,
c.OsakaTime,
engine,
c.NoPruneContracts,
)
}

Expand Down
7 changes: 3 additions & 4 deletions eth/stagedsync/stage_execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,17 +204,16 @@ func executeBlock(
return nil
}

// Filters out and keeps receipts of contracts that may be needed by CL, such as deposit contrac,
// The list of contracts to filter is config-specified
// Filters out and keeps receipts of the contracts that may be needed by CL, namely of the deposit contract.
func gatherNoPruneReceipts(receipts *types.Receipts, chainCfg *chain.Config) bool {
cr := types.Receipts{}
for _, r := range *receipts {
toStore := false
if chainCfg.NoPruneContracts != nil && chainCfg.NoPruneContracts[r.ContractAddress] {
if chainCfg.DepositContract != nil && *chainCfg.DepositContract == r.ContractAddress {
toStore = true
} else {
for _, l := range r.Logs {
if chainCfg.NoPruneContracts != nil && chainCfg.NoPruneContracts[l.Address] {
if chainCfg.DepositContract != nil && *chainCfg.DepositContract == l.Address {
toStore = true
break
}
Expand Down
51 changes: 27 additions & 24 deletions eth/stagedsync/stage_log_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ import (
"slices"
"time"

"github.com/ledgerwatch/erigon-lib/kv/dbutils"

"github.com/RoaringBitmap/roaring"
"github.com/c2h5oh/datasize"
"github.com/ledgerwatch/log/v3"

libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/common/dbg"
"github.com/ledgerwatch/erigon-lib/common/hexutility"
"github.com/ledgerwatch/erigon-lib/etl"
"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/erigon-lib/kv/bitmapdb"
"github.com/ledgerwatch/log/v3"
"github.com/ledgerwatch/erigon-lib/kv/dbutils"

"github.com/ledgerwatch/erigon/core/types"
"github.com/ledgerwatch/erigon/ethdb/cbor"
Expand All @@ -32,22 +32,25 @@ const (
)

type LogIndexCfg struct {
tmpdir string
db kv.RwDB
prune prune.Mode
bufLimit datasize.ByteSize
flushEvery time.Duration
noPruneContracts map[libcommon.Address]bool
tmpdir string
db kv.RwDB
prune prune.Mode
bufLimit datasize.ByteSize
flushEvery time.Duration

// For not pruning the logs of this contract since deposit contract logs are needed by CL to validate/produce blocks.
// All logs should be available to a validating node through eth_getLogs
depositContract *libcommon.Address
}

func StageLogIndexCfg(db kv.RwDB, prune prune.Mode, tmpDir string, noPruneContracts map[libcommon.Address]bool) LogIndexCfg {
func StageLogIndexCfg(db kv.RwDB, prune prune.Mode, tmpDir string, depositContract *libcommon.Address) LogIndexCfg {
return LogIndexCfg{
db: db,
prune: prune,
bufLimit: bitmapsBufLimit,
flushEvery: bitmapsFlushEvery,
tmpdir: tmpDir,
noPruneContracts: noPruneContracts,
db: db,
prune: prune,
bufLimit: bitmapsBufLimit,
flushEvery: bitmapsFlushEvery,
tmpdir: tmpDir,
depositContract: depositContract,
}
}

Expand Down Expand Up @@ -106,7 +109,7 @@ func SpawnLogIndex(s *StageState, tx kv.RwTx, cfg LogIndexCfg, ctx context.Conte
return nil
}

// Add the topics and address index for logs, if not in prune range or addr in noPruneContracts
// Add the topics and address index for logs, if not in prune range or addr is the deposit contract
func promoteLogIndex(logPrefix string, tx kv.RwTx, start uint64, endBlock uint64, pruneBlock uint64, cfg LogIndexCfg, ctx context.Context, logger log.Logger) error {
quit := ctx.Done()
logEvery := time.NewTicker(30 * time.Second)
Expand Down Expand Up @@ -179,15 +182,15 @@ func promoteLogIndex(logPrefix string, tx kv.RwTx, start uint64, endBlock uint64
}

toStore := true
// if pruning is enabled, and noPruneContracts isn't configured for the chain, don't index
// if pruning is enabled, and depositContract isn't configured for the chain, don't index
if blockNum < pruneBlock {
toStore = false
if cfg.noPruneContracts == nil {
if cfg.depositContract == nil {
continue
}
for _, l := range ll {
// if any of the log address is in noPrune, store and index all logs for this txId
if cfg.noPruneContracts[l.Address] {
if *cfg.depositContract == l.Address {
toStore = true
break
}
Expand Down Expand Up @@ -431,7 +434,7 @@ func PruneLogIndex(s *PruneState, tx kv.RwTx, cfg LogIndexCfg, ctx context.Conte
}

pruneTo := cfg.prune.Receipts.PruneTo(s.ForwardProgress)
if err = pruneLogIndex(logPrefix, tx, cfg.tmpdir, s.PruneProgress, pruneTo, ctx, logger, cfg.noPruneContracts); err != nil {
if err = pruneLogIndex(logPrefix, tx, cfg.tmpdir, s.PruneProgress, pruneTo, ctx, logger, cfg.depositContract); err != nil {
return err
}
if err = s.DoneAt(tx, pruneTo); err != nil {
Expand All @@ -447,7 +450,7 @@ func PruneLogIndex(s *PruneState, tx kv.RwTx, cfg LogIndexCfg, ctx context.Conte
}

// Prune log indexes as well as logs within the prune range
func pruneLogIndex(logPrefix string, tx kv.RwTx, tmpDir string, pruneFrom, pruneTo uint64, ctx context.Context, logger log.Logger, noPruneContracts map[libcommon.Address]bool) error {
func pruneLogIndex(logPrefix string, tx kv.RwTx, tmpDir string, pruneFrom, pruneTo uint64, ctx context.Context, logger log.Logger, depositContract *libcommon.Address) error {
logEvery := time.NewTicker(logInterval)
defer logEvery.Stop()

Expand Down Expand Up @@ -490,8 +493,8 @@ func pruneLogIndex(logPrefix string, tx kv.RwTx, tmpDir string, pruneFrom, prune
toPrune := true
for _, l := range logs {
// No logs (or sublogs) for this txId should be pruned
// if one of the logs belongs to noPruneContracts lis
if noPruneContracts != nil && noPruneContracts[l.Address] {
// if one of the logs belongs to the deposit contract
if depositContract != nil && *depositContract == l.Address {
toPrune = false
break
}
Expand Down
3 changes: 2 additions & 1 deletion eth/stagedsync/stage_log_index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ func TestPruneLogIndex(t *testing.T) {
require.NoError(err)

// Mode test
err = pruneLogIndex("", tx, tmpDir, 0, 45, ctx, logger, map[libcommon.Address]bool{{1}: true}) // using addr {1} from genReceipts
depositContract := libcommon.Address{1} // using addr {1} from genReceipts
err = pruneLogIndex("", tx, tmpDir, 0, 45, ctx, logger, &depositContract)
require.NoError(err)

{
Expand Down
8 changes: 3 additions & 5 deletions params/chainspecs/chiado.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
"istanbulBlock": 0,
"berlinBlock": 0,
"londonBlock": 0,
"burntContract": {
"0": "0x1559000000000000000000000000000000000000"
},
"terminalTotalDifficulty": 231707791542740786049188744689299064356246512,
"terminalTotalDifficultyPassed": true,
"shanghaiTime": 1684934220,
Expand All @@ -22,9 +19,10 @@
"maxBlobGasPerBlock": 262144,
"targetBlobGasPerBlock": 131072,
"blobGasPriceUpdateFraction": 1112826,
"noPruneContracts": {
"0xb97036A26259B7147018913bD58a774cf91acf25": true
"burntContract": {
"0": "0x1559000000000000000000000000000000000000"
},
"depositContract": "0xb97036A26259B7147018913bD58a774cf91acf25",
"aura": {
"stepDuration": 5,
"blockReward": 0,
Expand Down
8 changes: 3 additions & 5 deletions params/chainspecs/gnosis.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
"istanbulBlock": 7298030,
"berlinBlock": 16101500,
"londonBlock": 19040000,
"burntContract": {
"19040000": "0x6BBe78ee9e474842Dbd4AB4987b3CeFE88426A92"
},
"terminalTotalDifficulty": 8626000000000000000000058750000000000000000000,
"terminalTotalDifficultyPassed": true,
"shanghaiTime": 1690889660,
Expand All @@ -22,9 +19,10 @@
"maxBlobGasPerBlock": 262144,
"targetBlobGasPerBlock": 131072,
"blobGasPriceUpdateFraction": 1112826,
"noPruneContracts": {
"0x0B98057eA310F4d31F2a452B414647007d1645d9": true
"burntContract": {
"19040000": "0x6BBe78ee9e474842Dbd4AB4987b3CeFE88426A92"
},
"depositContract": "0x0B98057eA310F4d31F2a452B414647007d1645d9",
"aura": {
"stepDuration": 5,
"blockReward": 0,
Expand Down
4 changes: 1 addition & 3 deletions params/chainspecs/goerli.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@
"terminalTotalDifficultyPassed": true,
"shanghaiTime": 1678832736,
"cancunTime": 1705473120,
"depositContract": "0xff50ed3d0ec03aC01D4C79aAd74928BFF48a7b2b",
"clique": {
"period": 15,
"epoch": 30000
},
"noPruneContracts": {
"0xff50ed3d0ec03aC01D4C79aAd74928BFF48a7b2b": true
}
}
6 changes: 2 additions & 4 deletions params/chainspecs/holesky.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,5 @@
"terminalTotalDifficultyPassed": true,
"shanghaiTime": 1696000704,
"cancunTime": 1707305664,
"noPruneContracts": {
"0x4242424242424242424242424242424242424242": true
}
}
"depositContract": "0x4242424242424242424242424242424242424242"
}
6 changes: 2 additions & 4 deletions params/chainspecs/mainnet.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
"terminalTotalDifficultyPassed": true,
"shanghaiTime": 1681338455,
"cancunTime": 1710338135,
"ethash": {},
"noPruneContracts": {
"0x00000000219ab540356cBB839Cbe05303d7705Fa": true
}
"depositContract": "0x00000000219ab540356cBB839Cbe05303d7705Fa",
"ethash": {}
}
6 changes: 2 additions & 4 deletions params/chainspecs/sepolia.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
"mergeNetsplitBlock": 1735371,
"shanghaiTime": 1677557088,
"cancunTime": 1706655072,
"ethash": {},
"noPruneContracts": {
"0x7f02C3E3c98b133055B8B348B2Ac625669Ed295D": true
}
"depositContract": "0x7f02C3E3c98b133055B8B348B2Ac625669Ed295D",
"ethash": {}
}
14 changes: 7 additions & 7 deletions turbo/stages/stageloop.go
Original file line number Diff line number Diff line change
Expand Up @@ -491,9 +491,9 @@ func NewDefaultStages(ctx context.Context,
}
}

var noPruneContracts map[libcommon.Address]bool
var depositContract *libcommon.Address
if cfg.Genesis != nil {
noPruneContracts = cfg.Genesis.Config.NoPruneContracts
depositContract = cfg.Genesis.Config.DepositContract
}

return stagedsync.DefaultStages(ctx,
Expand Down Expand Up @@ -526,7 +526,7 @@ func NewDefaultStages(ctx context.Context,
stagedsync.StageHashStateCfg(db, dirs, cfg.HistoryV3),
stagedsync.StageTrieCfg(db, true, true, false, dirs.Tmp, blockReader, controlServer.Hd, cfg.HistoryV3, agg),
stagedsync.StageHistoryCfg(db, cfg.Prune, dirs.Tmp),
stagedsync.StageLogIndexCfg(db, cfg.Prune, dirs.Tmp, noPruneContracts),
stagedsync.StageLogIndexCfg(db, cfg.Prune, dirs.Tmp, depositContract),
stagedsync.StageCallTracesCfg(db, cfg.Prune, 0, dirs.Tmp),
stagedsync.StageTxLookupCfg(db, cfg.Prune, dirs.Tmp, controlServer.ChainConfig.Bor, blockReader),
stagedsync.StageFinishCfg(db, dirs.Tmp, forkValidator),
Expand Down Expand Up @@ -572,9 +572,9 @@ func NewPipelineStages(ctx context.Context,
}
}

var noPruneContracts map[libcommon.Address]bool
var depositContract *libcommon.Address
if cfg.Genesis != nil {
noPruneContracts = cfg.Genesis.Config.NoPruneContracts
depositContract = cfg.Genesis.Config.DepositContract
}

if len(cfg.Sync.UploadLocation) == 0 {
Expand Down Expand Up @@ -605,7 +605,7 @@ func NewPipelineStages(ctx context.Context,
stagedsync.StageHashStateCfg(db, dirs, cfg.HistoryV3),
stagedsync.StageTrieCfg(db, checkStateRoot, true, false, dirs.Tmp, blockReader, controlServer.Hd, cfg.HistoryV3, agg),
stagedsync.StageHistoryCfg(db, cfg.Prune, dirs.Tmp),
stagedsync.StageLogIndexCfg(db, cfg.Prune, dirs.Tmp, noPruneContracts),
stagedsync.StageLogIndexCfg(db, cfg.Prune, dirs.Tmp, depositContract),
stagedsync.StageCallTracesCfg(db, cfg.Prune, 0, dirs.Tmp),
stagedsync.StageTxLookupCfg(db, cfg.Prune, dirs.Tmp, controlServer.ChainConfig.Bor, blockReader),
stagedsync.StageFinishCfg(db, dirs.Tmp, forkValidator),
Expand Down Expand Up @@ -641,7 +641,7 @@ func NewPipelineStages(ctx context.Context,
stagedsync.StageHashStateCfg(db, dirs, cfg.HistoryV3),
stagedsync.StageTrieCfg(db, checkStateRoot, true, false, dirs.Tmp, blockReader, controlServer.Hd, cfg.HistoryV3, agg),
stagedsync.StageHistoryCfg(db, cfg.Prune, dirs.Tmp),
stagedsync.StageLogIndexCfg(db, cfg.Prune, dirs.Tmp, noPruneContracts),
stagedsync.StageLogIndexCfg(db, cfg.Prune, dirs.Tmp, depositContract),
stagedsync.StageCallTracesCfg(db, cfg.Prune, 0, dirs.Tmp),
stagedsync.StageTxLookupCfg(db, cfg.Prune, dirs.Tmp, controlServer.ChainConfig.Bor, blockReader),
stagedsync.StageFinishCfg(db, dirs.Tmp, forkValidator),
Expand Down

0 comments on commit 926f414

Please sign in to comment.