Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow rpc to access the ForkID directly from DB #3196

Merged
merged 2 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 16 additions & 11 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,8 @@ func start(cliCtx *cli.Context) error {
log.Fatal(err)
}

st := newState(cliCtx.Context, c, l2ChainID, []state.ForkIDInterval{}, stateSqlDB, eventLog, needsExecutor, needsStateTree)
forkIDIntervals, err := forkIDIntervals(cliCtx.Context, st, etherman, c.NetworkConfig.Genesis.BlockNumber)
if err != nil {
log.Fatal("error getting forkIDs. Error: ", err)
}
st.UpdateForkIDIntervalsInMemory(forkIDIntervals)
st, currentForkID := newState(cliCtx.Context, c, etherman, l2ChainID, stateSqlDB, eventLog, needsExecutor, needsStateTree, false)

currentForkID := forkIDIntervals[len(forkIDIntervals)-1].ForkId
log.Infof("Fork ID read from POE SC = %v", forkIDIntervals[len(forkIDIntervals)-1].ForkId)
c.Aggregator.ChainID = l2ChainID
c.Sequencer.StreamServer.ChainID = l2ChainID
log.Infof("Chain ID read from POE SC = %v", l2ChainID)
Expand Down Expand Up @@ -212,6 +205,7 @@ func start(cliCtx *cli.Context) error {
for _, a := range cliCtx.StringSlice(config.FlagHTTPAPI) {
apis[a] = true
}
st, _ := newState(cliCtx.Context, c, etherman, l2ChainID, stateSqlDB, eventLog, needsExecutor, needsStateTree, true)
go runJSONRPCServer(*c, etherman, l2ChainID, poolInstance, st, apis)
case SYNCHRONIZER:
ev.Component = event.Component_Synchronizer
Expand Down Expand Up @@ -459,7 +453,7 @@ func waitSignal(cancelFuncs []context.CancelFunc) {
}
}

func newState(ctx context.Context, c *config.Config, l2ChainID uint64, forkIDIntervals []state.ForkIDInterval, sqlDB *pgxpool.Pool, eventLog *event.EventLog, needsExecutor, needsStateTree bool) *state.State {
func newState(ctx context.Context, c *config.Config, etherman *etherman.Client, l2ChainID uint64, sqlDB *pgxpool.Pool, eventLog *event.EventLog, needsExecutor, needsStateTree, avoidForkIDInMemory bool) (*state.State, uint64) {
stateDb := pgstatestorage.NewPostgresStorage(c.State, sqlDB)

// Executor
Expand All @@ -478,14 +472,15 @@ func newState(ctx context.Context, c *config.Config, l2ChainID uint64, forkIDInt
stateCfg := state.Config{
MaxCumulativeGasUsed: c.State.Batch.Constraints.MaxCumulativeGasUsed,
ChainID: l2ChainID,
ForkIDIntervals: forkIDIntervals,
ForkIDIntervals: []state.ForkIDInterval{},
MaxResourceExhaustedAttempts: c.Executor.MaxResourceExhaustedAttempts,
WaitOnResourceExhaustion: c.Executor.WaitOnResourceExhaustion,
ForkUpgradeBatchNumber: c.ForkUpgradeBatchNumber,
ForkUpgradeNewForkId: c.ForkUpgradeNewForkId,
MaxLogsCount: c.RPC.MaxLogsCount,
MaxLogsBlockRange: c.RPC.MaxLogsBlockRange,
MaxNativeBlockHashBlockRange: c.RPC.MaxNativeBlockHashBlockRange,
AvoidForkIDInMemory: avoidForkIDInMemory,
}
allLeaves, err := stateDb.GetAllL1InfoRootEntries(ctx, nil)
if err != nil {
Expand All @@ -501,7 +496,17 @@ func newState(ctx context.Context, c *config.Config, l2ChainID uint64, forkIDInt
}

st := state.NewState(stateCfg, stateDb, executorClient, stateTree, eventLog, mt)
return st

forkIDIntervals, err := forkIDIntervals(ctx, st, etherman, c.NetworkConfig.Genesis.BlockNumber)
if err != nil {
log.Fatal("error getting forkIDs. Error: ", err)
}
st.UpdateForkIDIntervalsInMemory(forkIDIntervals)

currentForkID := forkIDIntervals[len(forkIDIntervals)-1].ForkId
log.Infof("Fork ID read from POE SC = %v", forkIDIntervals[len(forkIDIntervals)-1].ForkId)

return st, currentForkID
}

func createPool(cfgPool pool.Config, constraintsCfg state.BatchConstraintsCfg, l2ChainID uint64, st *state.State, eventLog *event.EventLog) *pool.Pool {
Expand Down
2 changes: 1 addition & 1 deletion docs/config-file/node-config-doc.html

Large diffs are not rendered by default.

16 changes: 16 additions & 0 deletions docs/config-file/node-config-doc.md
Original file line number Diff line number Diff line change
Expand Up @@ -3420,6 +3420,7 @@ MaxConns=200
| - [MaxLogsCount](#State_MaxLogsCount ) | No | integer | No | - | MaxLogsCount is a configuration to set the max number of logs that can be returned<br />in a single call to the state, if zero it means no limit |
| - [MaxLogsBlockRange](#State_MaxLogsBlockRange ) | No | integer | No | - | MaxLogsBlockRange is a configuration to set the max range for block number when querying TXs<br />logs in a single call to the state, if zero it means no limit |
| - [MaxNativeBlockHashBlockRange](#State_MaxNativeBlockHashBlockRange ) | No | integer | No | - | MaxNativeBlockHashBlockRange is a configuration to set the max range for block number when querying<br />native block hashes in a single call to the state, if zero it means no limit |
| - [AvoidForkIDInMemory](#State_AvoidForkIDInMemory ) | No | boolean | No | - | AvoidForkIDInMemory is a configuration that forces the ForkID information to be loaded<br />from the DB every time it's needed |

### <a name="State_MaxCumulativeGasUsed"></a>20.1. `State.MaxCumulativeGasUsed`

Expand Down Expand Up @@ -3884,5 +3885,20 @@ native block hashes in a single call to the state, if zero it means no limit
MaxNativeBlockHashBlockRange=0
```

### <a name="State_AvoidForkIDInMemory"></a>20.13. `State.AvoidForkIDInMemory`

**Type:** : `boolean`

**Default:** `false`

**Description:** AvoidForkIDInMemory is a configuration that forces the ForkID information to be loaded
from the DB every time it's needed

**Example setting the default value** (false):
```
[State]
AvoidForkIDInMemory=false
```

----------------------------------------------------------------------------------------------------------------------------
Generated using [json-schema-for-humans](https://github.com/coveooss/json-schema-for-humans)
5 changes: 5 additions & 0 deletions docs/config-file/node-config-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1541,6 +1541,11 @@
"type": "integer",
"description": "MaxNativeBlockHashBlockRange is a configuration to set the max range for block number when querying\nnative block hashes in a single call to the state, if zero it means no limit",
"default": 0
},
"AvoidForkIDInMemory": {
"type": "boolean",
"description": "AvoidForkIDInMemory is a configuration that forces the ForkID information to be loaded\nfrom the DB every time it's needed",
"default": false
}
},
"additionalProperties": false,
Expand Down
4 changes: 4 additions & 0 deletions state/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ type Config struct {
// MaxNativeBlockHashBlockRange is a configuration to set the max range for block number when querying
// native block hashes in a single call to the state, if zero it means no limit
MaxNativeBlockHashBlockRange uint64

// AvoidForkIDInMemory is a configuration that forces the ForkID information to be loaded
// from the DB every time it's needed
AvoidForkIDInMemory bool
}

// BatchConfig represents the configuration of the batch constraints
Expand Down
64 changes: 64 additions & 0 deletions state/pgstatestorage/forkid.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,32 @@ func (p *PostgresStorage) AddForkIDInterval(ctx context.Context, newForkID state

// GetForkIDByBlockNumber returns the fork id for a given block number
func (p *PostgresStorage) GetForkIDByBlockNumber(blockNumber uint64) uint64 {
if p.cfg.AvoidForkIDInMemory {
const query = `
SELECT fork_id
FROM state.fork_id
WHERE block_num < $1
ORDER BY fork_id DESC
LIMIT 1`
q := p.getExecQuerier(nil)

var forkID uint64
err := q.QueryRow(context.Background(), query, blockNumber).Scan(&forkID)
if errors.Is(err, pgx.ErrNoRows) {
return 1
} else if err != nil {
log.Warnf("failed to get forkID by blockNumber from db, falling back to in memory information, err: %v", err)
return p.GetForkIDByBlockNumberInMemory(blockNumber)
}

return forkID
} else {
return p.GetForkIDByBlockNumberInMemory(blockNumber)
}
}

// GetForkIDByBlockNumber returns the fork id for a given block number in memory
func (p *PostgresStorage) GetForkIDByBlockNumberInMemory(blockNumber uint64) uint64 {
for _, index := range sortIndexForForkdIDSortedByBlockNumber(p.cfg.ForkIDIntervals) {
// reverse travesal
interval := p.cfg.ForkIDIntervals[len(p.cfg.ForkIDIntervals)-1-index]
Expand All @@ -130,6 +156,44 @@ func sortIndexForForkdIDSortedByBlockNumber(forkIDs []state.ForkIDInterval) []in

// GetForkIDByBatchNumber returns the fork id for a given batch number
func (p *PostgresStorage) GetForkIDByBatchNumber(batchNumber uint64) uint64 {
if p.cfg.AvoidForkIDInMemory {
const query = `
SELECT fork_id FROM state.fork_id
WHERE from_batch_num <= $1 AND to_batch_num >= $1
ORDER BY fork_id DESC
LIMIT 1`
q := p.getExecQuerier(nil)

var forkID uint64
err := q.QueryRow(context.Background(), query, batchNumber).Scan(&forkID)
if errors.Is(err, pgx.ErrNoRows) {
const query = `
SELECT fork_id
FROM state.fork_id
ORDER BY fork_id DESC
LIMIT 1`
q := p.getExecQuerier(nil)
err := q.QueryRow(context.Background(), query).Scan(&forkID)
if errors.Is(err, pgx.ErrNoRows) {
log.Warnf("can't find forkID by batchNumber in the db, falling back to in memory information, err: %v", err)
return p.GetForkIDByBatchNumberInMemory(batchNumber)
} else if err != nil {
log.Warnf("failed to get forkID by batchNumber from db, falling back to in memory information, err: %v", err)
return p.GetForkIDByBatchNumberInMemory(batchNumber)
}
} else if err != nil {
log.Warnf("failed to get forkID by batchNumber from db, falling back to in memory information, err: %v", err)
return p.GetForkIDByBatchNumberInMemory(batchNumber)
}

return forkID
} else {
return p.GetForkIDByBatchNumberInMemory(batchNumber)
}
}

// GetForkIDByBatchNumberInMemory returns the fork id for a given batch number
func (p *PostgresStorage) GetForkIDByBatchNumberInMemory(batchNumber uint64) uint64 {
// If NumBatchForkIdUpgrade is defined (!=0) we are performing forkid upgrade process
// In this case, if the batchNumber is the next to the NumBatchForkIdUpgrade, we need to return the
// new "future" forkId (ForkUpgradeNewForkId)
Expand Down
Loading