Skip to content

Commit

Permalink
Add zkevm_getBatchSealTime (#222)
Browse files Browse the repository at this point in the history
* add zkevm_getBatchSealTime

* use block time instead of create time

* fix sql error and fix comment

* add the mock state xlayer

* storageMock add GetLastL2BlockTimeByBatchNumber

* format imports

* our batch number should closed

* fix compile error

* return error msg, do not record it

* add doc

* beautify error message
  • Loading branch information
giskook authored Jun 21, 2024
1 parent 8140d3a commit b281613
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 5 deletions.
24 changes: 22 additions & 2 deletions jsonrpc/endpoints_zkevm.openrpc.json
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,7 @@
"result": {
"$ref": "#/components/contentDescriptors/BatchDataResult"
}
},
{
}, {
"name": "zkevm_getBatchByNumber",
"summary": "Gets a batch for a given number",
"params": [
Expand Down Expand Up @@ -483,6 +482,22 @@
"$ref": "#/components/schemas/Integer"
}
}
},
{
"name": "zkevm_getBatchSealTime",
"summary": "Get Batch seal time",
"params": [
{
"$ref": "#/components/contentDescriptors/BatchNumber"
}
],
"result": {
"name": "result",
"description": "The batch seal time",
"schema": {
"$ref": "#/components/schemas/Timestamp"
}
}
}
],
"components": {
Expand Down Expand Up @@ -1470,6 +1485,11 @@
"$ref": "#/components/schemas/Integer"
}
}
},
"Timestamp": {
"title": "timestamp",
"type": "string",
"description": "The unix timestamp of the batch"
}
}
}
Expand Down
28 changes: 28 additions & 0 deletions jsonrpc/endpoints_zkevm_xlayer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package jsonrpc

import (
"context"
"fmt"

"github.com/0xPolygonHermez/zkevm-node/hex"
"github.com/0xPolygonHermez/zkevm-node/jsonrpc/types"
"github.com/jackc/pgx/v4"
)

// GetBatchSealTime returns the seal time
func (z *ZKEVMEndpoints) GetBatchSealTime(batchNumber types.BatchNumber) (interface{}, types.Error) {
return z.txMan.NewDbTxScope(z.state, func(ctx context.Context, dbTx pgx.Tx) (interface{}, types.Error) {
var err error
batchNumber, rpcErr := batchNumber.GetNumericBatchNumber(ctx, z.state, z.etherman, dbTx)
if rpcErr != nil {
return nil, rpcErr
}

sealTime, err := z.state.GetLastL2BlockTimeByBatchNumber(ctx, batchNumber, dbTx)
if err != nil {
return RPCErrorResponse(types.DefaultErrorCode, fmt.Sprintf("couldn't get batch number %v's seal time, error: %v", batchNumber, err), nil, false)
}

return hex.EncodeUint64(sealTime), nil
})
}
23 changes: 23 additions & 0 deletions jsonrpc/mocks/mock_state_xlayer.go

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

3 changes: 3 additions & 0 deletions jsonrpc/types/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ type StateInterface interface {
GetLatestBatchGlobalExitRoot(ctx context.Context, dbTx pgx.Tx) (common.Hash, error)
GetL2TxHashByTxHash(ctx context.Context, hash common.Hash, dbTx pgx.Tx) (*common.Hash, error)
PreProcessUnsignedTransaction(ctx context.Context, tx *types.Transaction, sender common.Address, l2BlockNumber *uint64, dbTx pgx.Tx) (*state.ProcessBatchResponse, error)

// GetLastL2BlockTimeByBatchNumber gets the last l2 block time in a batch by batch number X Layer handler
GetLastL2BlockTimeByBatchNumber(ctx context.Context, batchNumber uint64, dbTx pgx.Tx) (uint64, error)
}

// EthermanInterface provides integration with L1
Expand Down
1 change: 1 addition & 0 deletions state/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,5 @@ type storage interface {
// GetBatchL2DataByNumber is XLayer method
GetBatchL2DataByNumber(ctx context.Context, batchNumber uint64, dbTx pgx.Tx) ([]byte, error)
GetBatchL2DataByNumbers(ctx context.Context, batchNumbers []uint64, dbTx pgx.Tx) (map[uint64][]byte, error)
GetLastL2BlockTimeByBatchNumber(ctx context.Context, batchNumber uint64, dbTx pgx.Tx) (uint64, error)
}
10 changes: 7 additions & 3 deletions state/mocks/mock_storage_xlayer.go

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

34 changes: 34 additions & 0 deletions state/pgstatestorage/l2block_xlayer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package pgstatestorage

import (
"context"
"errors"
"fmt"

"github.com/0xPolygonHermez/zkevm-node/state"
"github.com/jackc/pgx/v4"
)

// GetLastL2BlockTimeByBatchNumber gets the last l2 block time in a batch by batch number
func (p *PostgresStorage) GetLastL2BlockTimeByBatchNumber(ctx context.Context, batchNumber uint64, dbTx pgx.Tx) (uint64, error) {
lastClosedBatchNumber, err := p.GetLastBatchNumber(ctx, dbTx)
if err != nil {
return 0, err
}
if batchNumber > lastClosedBatchNumber {
return 0, fmt.Errorf("%w. got %d, last batch should be %d", state.ErrUnexpectedBatch, batchNumber, lastClosedBatchNumber)
}
const query = "SELECT header FROM state.l2block b WHERE batch_num = $1 ORDER BY b.block_num DESC LIMIT 1"

header := &state.L2Header{}
q := p.getExecQuerier(dbTx)
err = q.QueryRow(ctx, query, batchNumber).Scan(&header)

if errors.Is(err, pgx.ErrNoRows) {
return 0, state.ErrNotFound
} else if err != nil {
return 0, err
}

return header.Time, nil
}

0 comments on commit b281613

Please sign in to comment.