Skip to content

Commit

Permalink
feat: [WIP] build onRequestCreated
Browse files Browse the repository at this point in the history
  • Loading branch information
0xyaco committed Aug 5, 2024
1 parent 70dd4b6 commit 1fe0161
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 23 deletions.
37 changes: 29 additions & 8 deletions packages/automated-dispute/src/eboActor.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,44 @@
import assert from "assert";
import { BlockNumberService } from "@ebo-agent/blocknumber";

import { ProtocolProvider } from "./protocolProvider.js";
import { EboEvent } from "./types/events.js";
import { Dispute, Response } from "./types/prophet.js";
import { Dispute, Request, Response } from "./types/prophet.js";

export class EboActor {
private requestActivity: unknown[];

constructor(
private readonly protocolProvider: ProtocolProvider,
private readonly blockNumberService: BlockNumberService,
private readonly requestId: string,
) {
this.requestActivity = [];
private readonly request: Request,
) {}

public async onRequestCreated(event: EboEvent<"RequestCreated">): Promise<void> {
assert(
event.metadata.requestId == this.requestId,
`Actor handling request {this.requestId} recieved a ${event.metadata.requestId} event.`,
);

const { chainId } = event.metadata;
const { currentEpochTimestamp } = await this.protocolProvider.getCurrentEpoch();
const epochBlockNumber = await this.blockNumberService.getEpochBlockNumber(
currentEpochTimestamp,
chainId,
);

const response = this.buildResponse(chainId, epochBlockNumber);

this.protocolProvider.proposeResponse(this.request, response);
}

public async onRequestCreated(_event: EboEvent<"RequestCreated">): Promise<void> {
// TODO: implement
return;
private buildResponse(_chainId: string, _epochBlockNumber: bigint): Response {
// const response = encodeResponse(...)

return {
proposer: "0x1234567890", // FIXME
requestId: this.requestId,
response: "????", // TODO
};
}

public async onResponseProposed(_event: EboEvent<"ResponseDisputed">): Promise<void> {
Expand Down
9 changes: 9 additions & 0 deletions packages/automated-dispute/src/eboRegistry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Dispute, Request, Response } from "./types/prophet.js";

class EboRegistry {
private requests: Map<string, Request>;
private responses: Map<string, Response>;
private dispute: Map<string, Dispute>;

constructor() {}
}
18 changes: 14 additions & 4 deletions packages/automated-dispute/src/protocolProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,27 @@ export class ProtocolProvider {
}

/**
* Gets the current epoch and the block number of the current epoch
* Gets the current epoch, the block number and its timestamp of the current epoch
* @returns The current epoch and the block number of the current epoch
*/
async getCurrentEpoch(): Promise<{ currentEpoch: bigint; currentEpochBlock: bigint }> {
const [currentEpoch, currentEpochBlock] = await Promise.all([
async getCurrentEpoch(): Promise<{
currentEpoch: bigint;
currentEpochBlockNumber: bigint;
currentEpochTimestamp: bigint;
}> {
const [currentEpoch, currentEpochBlockNumber] = await Promise.all([
this.epochManagerContract.read.currentEpoch(),
this.epochManagerContract.read.currentEpochBlock(),
]);

const currentEpochBlock = await this.client.getBlock({
blockNumber: currentEpochBlockNumber,
});

return {
currentEpoch,
currentEpochBlock,
currentEpochBlockNumber,
currentEpochTimestamp: currentEpochBlock.timestamp,
};
}

Expand Down
11 changes: 7 additions & 4 deletions packages/automated-dispute/src/types/events.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Caip2ChainId } from "@ebo-agent/blocknumber/dist/types.js";
import { Log } from "viem";

import { Dispute, Request } from "./prophet.js";
import { Dispute, Request, Response } from "./prophet.js";

export type EboEventName =
| "NewEpoch"
Expand All @@ -17,14 +18,16 @@ export interface NewEpoch {
epochBlockNumber: bigint;
}

export interface ResponseCreated {
export interface ResponseProposed {
requestId: string;
request: Request;
responseId: Response;
response: Response;
}

export interface RequestCreated {
epoch: string;
chainId: Caip2ChainId;
requestId: string;
request: Request;
}

export interface ResponseDisputed {
Expand Down
2 changes: 1 addition & 1 deletion packages/blocknumber/src/providers/blockNumberProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ export interface BlockNumberProvider {
*
* @returns the corresponding block number of a chain at a specific timestamp
*/
getEpochBlockNumber(timestamp: number): Promise<bigint>;
getEpochBlockNumber(timestamp: bigint): Promise<bigint>;
}
11 changes: 5 additions & 6 deletions packages/blocknumber/src/providers/evmBlockNumberProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,8 @@ export class EvmBlockNumberProvider implements BlockNumberProvider {
this.firstBlock = null;
}

async getEpochBlockNumber(timestamp: number): Promise<bigint> {
async getEpochBlockNumber(timestamp: bigint): Promise<bigint> {
// An optimized binary search is used to look for the epoch block.
const _timestamp = BigInt(timestamp);

// The EBO agent looks only for finalized blocks to avoid handling reorgs
const upperBoundBlock = await this.client.getBlock({ blockTag: "finalized" });
Expand All @@ -71,16 +70,16 @@ export class EvmBlockNumberProvider implements BlockNumberProvider {

const firstBlock = await this.getFirstBlock();

if (_timestamp < firstBlock.timestamp) throw new InvalidTimestamp(_timestamp);
if (_timestamp >= upperBoundBlock.timestamp) throw new LastBlockEpoch(upperBoundBlock);
if (timestamp < firstBlock.timestamp) throw new InvalidTimestamp(timestamp);
if (timestamp >= upperBoundBlock.timestamp) throw new LastBlockEpoch(upperBoundBlock);

// Reduces the search space by estimating a lower bound for the binary search.
//
// Performing a binary search between block 0 and last block is not efficient.
const lowerBoundBlock = await this.calculateLowerBoundBlock(_timestamp, upperBoundBlock);
const lowerBoundBlock = await this.calculateLowerBoundBlock(timestamp, upperBoundBlock);

// Searches for the timestamp with a binary search
return this.searchTimestamp(_timestamp, {
return this.searchTimestamp(timestamp, {
fromBlock: lowerBoundBlock.number,
toBlock: upperBoundBlock.number,
});
Expand Down

0 comments on commit 1fe0161

Please sign in to comment.