forked from 0xPolygonHermez/zkevm-node
-
Notifications
You must be signed in to change notification settings - Fork 216
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
Showing
7 changed files
with
118 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |