Skip to content

Commit

Permalink
Cherry pick PRs 0xPolygonHermez#3209 and 0xPolygonHermez#3197 into de…
Browse files Browse the repository at this point in the history
  • Loading branch information
tclemos authored Feb 8, 2024
1 parent 54ef1c4 commit 44248e0
Show file tree
Hide file tree
Showing 18 changed files with 211 additions and 54 deletions.
33 changes: 18 additions & 15 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,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 @@ -211,6 +204,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 @@ -458,9 +452,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 {
stateDb := pgstatestorage.NewPostgresStorage(c.State, sqlDB)

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) {
// Executor
var executorClient executor.ExecutorServiceClient
if needsExecutor {
Expand All @@ -477,25 +469,36 @@ 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,
}
stateDb := pgstatestorage.NewPostgresStorage(stateCfg, sqlDB)

st := state.NewState(stateCfg, stateDb, executorClient, stateTree, eventLog, nil)
// This is to force to build cache, and check that DB is ok before starting the application
l1inforoot, err := st.GetCurrentL1InfoRoot(ctx, nil)
l1InfoRoot, err := st.GetCurrentL1InfoRoot(ctx, nil)
if err != nil {
log.Fatal("error getting current L1InfoRoot. Error: ", err)
}
log.Infof("Starting L1InfoRoot: %v", l1inforoot.String())
log.Infof("Starting L1InfoRoot: %v", l1InfoRoot.String())

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
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
6 changes: 3 additions & 3 deletions jsonrpc/endpoints_eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ func (e *EthEndpoints) GetTransactionByBlockHashAndIndex(hash types.ArgHash, ind
if err != nil {
return RPCErrorResponse(types.DefaultErrorCode, "failed to get l2 transaction hash", err, true)
}
l2Hash = &l2h
l2Hash = l2h
}

res, err := types.NewTransaction(*tx, receipt, false, l2Hash)
Expand Down Expand Up @@ -628,7 +628,7 @@ func (e *EthEndpoints) GetTransactionByBlockNumberAndIndex(number *types.BlockNu
if err != nil {
return RPCErrorResponse(types.DefaultErrorCode, "failed to get l2 transaction hash", err, true)
}
l2Hash = &l2h
l2Hash = l2h
}

res, err := types.NewTransaction(*tx, receipt, false, l2Hash)
Expand Down Expand Up @@ -662,7 +662,7 @@ func (e *EthEndpoints) GetTransactionByHash(hash types.ArgHash, includeExtraInfo
if err != nil {
return RPCErrorResponse(types.DefaultErrorCode, "failed to get l2 transaction hash", err, true)
}
l2Hash = &l2h
l2Hash = l2h
}

res, err := types.NewTransaction(*tx, receipt, false, l2Hash)
Expand Down
4 changes: 2 additions & 2 deletions jsonrpc/endpoints_eth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1309,7 +1309,7 @@ func TestGetL2BlockByNumber(t *testing.T) {
for _, signedTx := range signedTransactions {
m.State.
On("GetL2TxHashByTxHash", context.Background(), signedTx.Hash(), m.DbTx).
Return(signedTx.Hash(), nil).
Return(state.Ptr(signedTx.Hash()), nil).
Once()
}
},
Expand Down Expand Up @@ -1349,7 +1349,7 @@ func TestGetL2BlockByNumber(t *testing.T) {
for _, signedTx := range signedTransactions {
m.State.
On("GetL2TxHashByTxHash", context.Background(), signedTx.Hash(), m.DbTx).
Return(signedTx.Hash(), nil).
Return(state.Ptr(signedTx.Hash()), nil).
Once()
}
},
Expand Down
4 changes: 2 additions & 2 deletions jsonrpc/endpoints_zkevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ func (z *ZKEVMEndpoints) GetTransactionByL2Hash(hash types.ArgHash) (interface{}
return RPCErrorResponse(types.DefaultErrorCode, "failed to get l2 transaction hash", err, true)
}

res, err := types.NewTransaction(*tx, receipt, false, &l2Hash)
res, err := types.NewTransaction(*tx, receipt, false, l2Hash)
if err != nil {
return RPCErrorResponse(types.DefaultErrorCode, "failed to build transaction response", err, true)
}
Expand Down Expand Up @@ -381,7 +381,7 @@ func (z *ZKEVMEndpoints) GetTransactionReceiptByL2Hash(hash types.ArgHash) (inte
return RPCErrorResponse(types.DefaultErrorCode, "failed to get l2 transaction hash", err, true)
}

receipt, err := types.NewReceipt(*tx, r, &l2Hash)
receipt, err := types.NewReceipt(*tx, r, l2Hash)
if err != nil {
return RPCErrorResponse(types.DefaultErrorCode, "failed to build the receipt response", err, true)
}
Expand Down
10 changes: 5 additions & 5 deletions jsonrpc/endpoints_zkevm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -777,7 +777,7 @@ func TestGetBatchByNumber(t *testing.T) {
Once()
m.State.
On("GetL2TxHashByTxHash", context.Background(), tx.Hash(), m.DbTx).
Return(tx.Hash(), nil).
Return(state.Ptr(tx.Hash()), nil).
Once()
}
m.State.
Expand Down Expand Up @@ -1063,7 +1063,7 @@ func TestGetBatchByNumber(t *testing.T) {

m.State.
On("GetL2TxHashByTxHash", context.Background(), tx.Hash(), m.DbTx).
Return(tx.Hash(), nil).
Return(state.Ptr(tx.Hash()), nil).
Once()
}

Expand Down Expand Up @@ -1980,7 +1980,7 @@ func TestGetTransactionByL2Hash(t *testing.T) {

m.State.
On("GetL2TxHashByTxHash", context.Background(), signedTx.Hash(), m.DbTx).
Return(l2Hash, nil).
Return(&l2Hash, nil).
Once()
},
},
Expand Down Expand Up @@ -2296,7 +2296,7 @@ func TestGetTransactionReceiptByL2Hash(t *testing.T) {

m.State.
On("GetL2TxHashByTxHash", context.Background(), signedTx.Hash(), m.DbTx).
Return(l2Hash, nil).
Return(&l2Hash, nil).
Once()
},
},
Expand Down Expand Up @@ -2426,7 +2426,7 @@ func TestGetTransactionReceiptByL2Hash(t *testing.T) {

m.State.
On("GetL2TxHashByTxHash", context.Background(), tx.Hash(), m.DbTx).
Return(l2Hash, nil).
Return(&l2Hash, nil).
Once()
},
},
Expand Down
10 changes: 5 additions & 5 deletions jsonrpc/mocks/mock_state.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion jsonrpc/types/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ type StateInterface interface {
GetLastVerifiedBatchNumberUntilL1Block(ctx context.Context, l1BlockNumber uint64, dbTx pgx.Tx) (uint64, error)
GetBatchTimestamp(ctx context.Context, batchNumber uint64, forcedForkId *uint64, dbTx pgx.Tx) (*time.Time, error)
GetLatestBatchGlobalExitRoot(ctx context.Context, dbTx pgx.Tx) (common.Hash, error)
GetL2TxHashByTxHash(ctx context.Context, hash common.Hash, dbTx pgx.Tx) (common.Hash, error)
GetL2TxHashByTxHash(ctx context.Context, hash common.Hash, dbTx pgx.Tx) (*common.Hash, error)
}

// EthermanInterface provides integration with L1
Expand Down
4 changes: 2 additions & 2 deletions jsonrpc/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ func NewBlock(ctx context.Context, st StateInterface, hash *common.Hash, b *stat
if err != nil {
return nil, err
}
l2Hash = &l2h
l2Hash = l2h
}

rpcTx, err := NewTransaction(*tx, receiptPtr, includeReceipts, l2Hash)
Expand Down Expand Up @@ -422,7 +422,7 @@ func NewBatch(ctx context.Context, st StateInterface, batch *state.Batch, virtua
if err != nil {
return nil, err
}
rpcTx, err := NewTransaction(tx, receiptPtr, includeReceipts, &l2Hash)
rpcTx, err := NewTransaction(tx, receiptPtr, includeReceipts, l2Hash)
if err != nil {
return nil, err
}
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
2 changes: 1 addition & 1 deletion state/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ type storage interface {
GetVirtualBatchParentHash(ctx context.Context, batchNumber uint64, dbTx pgx.Tx) (common.Hash, error)
GetForcedBatchParentHash(ctx context.Context, forcedBatchNumber uint64, dbTx pgx.Tx) (common.Hash, error)
GetLatestBatchGlobalExitRoot(ctx context.Context, dbTx pgx.Tx) (common.Hash, error)
GetL2TxHashByTxHash(ctx context.Context, hash common.Hash, dbTx pgx.Tx) (common.Hash, error)
GetL2TxHashByTxHash(ctx context.Context, hash common.Hash, dbTx pgx.Tx) (*common.Hash, error)
GetSyncInfoData(ctx context.Context, dbTx pgx.Tx) (SyncInfoDataOnStorage, error)
GetFirstL2BlockNumberForBatchNumber(ctx context.Context, batchNumber uint64, dbTx pgx.Tx) (uint64, error)
GetForkIDInMemory(forkId uint64) *ForkIDInterval
Expand Down
14 changes: 7 additions & 7 deletions state/mocks/mock_storage.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 44248e0

Please sign in to comment.