Fix: Add hash and reverse search order
This commit is contained in:
parent
19467de809
commit
5d1c5b51dd
@ -251,8 +251,12 @@ class MiningRoutes {
|
|||||||
private async $getHeightFromTimestamp(req: Request, res: Response) {
|
private async $getHeightFromTimestamp(req: Request, res: Response) {
|
||||||
try {
|
try {
|
||||||
const timestamp = parseInt(req.params.timestamp, 10);
|
const timestamp = parseInt(req.params.timestamp, 10);
|
||||||
|
// This will prevent people from entering milliseconds etc.
|
||||||
|
// Block timestamps are allowed to be up to 2 hours off, so 24 hours
|
||||||
|
// will never put the maximum value before the most recent block
|
||||||
|
const nowPlus1day = Math.floor(Date.now() / 1000) + 60 * 60 * 24;
|
||||||
// Prevent non-integers that are not seconds
|
// Prevent non-integers that are not seconds
|
||||||
if (!/^[1-9][0-9]*$/.test(req.params.timestamp) || timestamp >= 2 ** 32) {
|
if (!/^[1-9][0-9]*$/.test(req.params.timestamp) || timestamp > nowPlus1day) {
|
||||||
throw new Error(`Invalid timestamp, value must be Unix seconds`);
|
throw new Error(`Invalid timestamp, value must be Unix seconds`);
|
||||||
}
|
}
|
||||||
const result = await BlocksRepository.$getBlockHeightFromTimestamp(
|
const result = await BlocksRepository.$getBlockHeightFromTimestamp(
|
||||||
|
@ -399,17 +399,17 @@ class BlocksRepository {
|
|||||||
*/
|
*/
|
||||||
public async $getBlockHeightFromTimestamp(
|
public async $getBlockHeightFromTimestamp(
|
||||||
timestamp: number,
|
timestamp: number,
|
||||||
): Promise<{ height: number; timestamp: number }> {
|
): Promise<{ height: number; hash: string; timestamp: number }> {
|
||||||
try {
|
try {
|
||||||
// Get first block at or after the given timestamp
|
// Get first block at or after the given timestamp
|
||||||
const query = `SELECT height, blockTimestamp as timestamp FROM blocks
|
const query = `SELECT height, hash, blockTimestamp as timestamp FROM blocks
|
||||||
WHERE blockTimestamp >= FROM_UNIXTIME(?)
|
WHERE blockTimestamp <= FROM_UNIXTIME(?)
|
||||||
ORDER BY blockTimestamp ASC
|
ORDER BY blockTimestamp DESC
|
||||||
LIMIT 1`;
|
LIMIT 1`;
|
||||||
const params = [timestamp];
|
const params = [timestamp];
|
||||||
const [rows]: any[][] = await DB.query(query, params);
|
const [rows]: any[][] = await DB.query(query, params);
|
||||||
if (rows.length === 0) {
|
if (rows.length === 0) {
|
||||||
throw new Error(`No block was found after timestamp ${timestamp}`);
|
throw new Error(`No block was found before timestamp ${timestamp}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
return rows[0];
|
return rows[0];
|
||||||
|
Loading…
x
Reference in New Issue
Block a user