Skip to content

adding additional endpoint to get the latest 10 timestamps #2076

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
18 changes: 17 additions & 1 deletion api/src/httpd/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { restoreBackup } from "../system/restoreBackup";

import { AuthenticatedRequest, HttpResponse } from "./lib";
import { getSchema, getSchemaWithoutAuth } from "./schema";
import { getTimestamps } from "../system/getTimestamps";

const send = (res, httpResponse: HttpResponse): void => {
const [code, body] = httpResponse;
Expand Down Expand Up @@ -227,7 +228,7 @@ export const registerRoutes = (
storageServiceClient: StorageServiceClient,
invalidateCache: () => void,
): FastifyInstance => {
server.register(async function () {
server.register(async function() {
const multichainClient = conn.multichainClient;

// ------------------------------------------------------------
Expand Down Expand Up @@ -280,6 +281,21 @@ export const registerRoutes = (
},
);

server.get(
`${urlPrefix}/timestamps`,
silentRouteSettings(getSchema(server, "timestamps")),
(request, reply) => {
getTimestamps(
multichainClient,
)
.then((response) => {
send(reply, response);
})
.catch((err) => handleError(request, reply, err));
},
)


// ------------------------------------------------------------
// network
// ------------------------------------------------------------
Expand Down
34 changes: 34 additions & 0 deletions api/src/httpd/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,40 @@ const schemas: Schemas = {
},
},

timestamps :{
schema: {
description: "Returns the latest 10 timestamps",
tags: ["system"],
summary: "Get timestamps",
security: [
{
bearerToken: [],
},
],
response: {
200: {
description: "successful response",
type: "object",
properties: {
apiVersion: { type: "string", example: "1.0" },
data: {
type: "object",
properties: {
timestamps: {
type: "array",
items: {
type: "number",
example: 1613000000,
},
},
},
},
},
},
},
}
},

registerNode: {
schema: {
description: "Used by non-alpha MultiChain nodes to register their wallet address.",
Expand Down
8 changes: 8 additions & 0 deletions api/src/service/Client_storage_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ export default class StorageServiceClient implements StorageServiceClientI {
return result?.data as Version;
}

public async getTimestamp(): Promise<number[]> {
const result: AxiosResponse<unknown> = await this.axiosInstance.get("/timestamp");
if (result.status !== 200) {
return [];
}
return result.data as number[];
}

public async uploadObject(file: File): Promise<Result.Type<UploadResponse>> {
logger.debug(`Uploading Object "${file.fileName}"`);

Expand Down
24 changes: 24 additions & 0 deletions api/src/system/getTimestamps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { HttpResponse } from "../httpd/lib";
import { MultichainClient } from "../service/Client.h";

type TimestampData = number;

const multichainTimestampData = async (
multichainClient: MultichainClient,
): Promise<TimestampData[]> => {
const { height } = await multichainClient.getLastBlockInfo();
const blocks = await multichainClient.listBlocksByHeight(Math.max(0, height - 10));
const timestamps = blocks.map((block) => block.time);
return timestamps
};

export const getTimestamps = async (
multichainClient: MultichainClient,
): Promise<HttpResponse> => {
return [
200, {
apiVersion: "1.0",
data: await multichainTimestampData(multichainClient),
}
]
}