Skip to content
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

add unencoded voting round data endpoint for easier data visualization #3

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
31 changes: 31 additions & 0 deletions apps/ftso-data-provider/src/dto/data-provider-responses.dto.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
// PDP (Protocol Data Provider) Response

import { AbiDataInput } from "../../../../libs/ftso-core/src/utils/ABICache";
import { MerkleTree } from "../../../../libs/ftso-core/src/utils/MerkleTree";
import { TreeResult } from "../../../../libs/ftso-core/src/utils/MerkleTreeStructs";
import { MedianCalculationResult, RandomCalculationResult } from "../../../../libs/ftso-core/src/voting-types";

export interface BigInt {
toJSON: () => string;
}
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
BigInt.prototype.toJSON = function () {
return this.toString();
};

export enum PDPResponseStatusEnum {
OK = "OK",
Expand Down Expand Up @@ -40,6 +51,26 @@ interface ExternalResponseNotAvailable {

export type ExternalResponse = ExternalResponseOk | ExternalResponseTooEarly | ExternalResponseNotAvailable;

interface UnencodedResultDataOk {
status: ExternalResponseStatusEnum.OK;
votingRoundId: number;
medianData: MedianCalculationResult[];
randomData: RandomCalculationResult;
merkleTree: MerkleTree;
}
interface UnencodedResultDataTooEarly {
status: ExternalResponseStatusEnum.TOO_EARLY;
}

interface UnencodedResultDataNotAvailable {
status: ExternalResponseStatusEnum.NOT_AVAILABLE;
}

export type UnencodedResultDataResponse =
| UnencodedResultDataOk
| UnencodedResultDataTooEarly
| UnencodedResultDataNotAvailable;

export interface JSONAbiDefinition {
abiName: string;
data: AbiDataInput;
Expand Down
17 changes: 16 additions & 1 deletion apps/ftso-data-provider/src/ftso-data-provider.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
ExternalResponseStatusEnum,
PDPResponse,
PDPResponseStatusEnum,
UnencodedResultDataResponse,
} from "./dto/data-provider-responses.dto";
import { FtsoDataProviderService } from "./ftso-data-provider.service";
import { ProtocolMessageMerkleRoot } from "../../../libs/fsp-utils/src/ProtocolMessageMerkleRoot";
Expand Down Expand Up @@ -97,14 +98,28 @@ export class FtsoDataProviderController {
@ApiTags(ApiTagsEnum.EXTERNAL)
@Get("data/:votingRoundId")
async merkleTree(@Param("votingRoundId", ParseIntPipe) votingRoundId: number): Promise<ExternalResponse> {
// TODO: handle to early response as it is more informative, for now we respond with not available for to early cases
// TODO: handle to early response as it is more informative, for now we respond with not available for too early cases
const data = await this.ftsoDataProviderService.getFullMerkleTree(votingRoundId);
return {
status: data ? ExternalResponseStatusEnum.OK : ExternalResponseStatusEnum.NOT_AVAILABLE,
...data,
};
}

@ApiTags(ApiTagsEnum.EXTERNAL)
@Get("results/:votingRoundId")
async getResultsForVotingRound(
@Param("votingRoundId", ParseIntPipe) votingRoundId: number
): Promise<UnencodedResultDataResponse> {
// TODO: handle to early response as it is more informative, for now we respond with not available for too early cases
this.logger.log(`Calling GET on results with param: votingRoundId ${votingRoundId}`);
const data = await this.ftsoDataProviderService.getUnencodedResultData(votingRoundId);
return {
status: data ? ExternalResponseStatusEnum.OK : ExternalResponseStatusEnum.NOT_AVAILABLE,
...data,
};
}

@ApiTags(ApiTagsEnum.EXTERNAL)
@Get("data-abis")
async treeAbis(): Promise<AbiDefinitionsResponse> {
Expand Down
5 changes: 5 additions & 0 deletions apps/ftso-data-provider/src/ftso-data-provider.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,11 @@ export class FtsoDataProviderService {
return message;
}

async getUnencodedResultData(votingRoundId: number): Promise<EpochResult | undefined> {
const result = await this.prepareCalculationResultData(votingRoundId);
return result;
}

async getFullMerkleTree(votingRoundId: number): Promise<IProtocolMessageMerkleData | undefined> {
const result = await this.prepareCalculationResultData(votingRoundId);
if (result === undefined) {
Expand Down