Skip to content

Commit

Permalink
Merge pull request #349 from forta-network/forta-1339-js-bot-health-c…
Browse files Browse the repository at this point in the history
…heck

js bot health check
  • Loading branch information
haseebrabbani authored Oct 20, 2023
2 parents 594ab2e + 7672f90 commit 94a3768
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 12 deletions.
15 changes: 8 additions & 7 deletions cli/commands/run/server/agent.controller.d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { GetAgentHandlers } from "../../../utils/get.agent.handlers";

export = AgentController
export = AgentController;

declare class AgentController {
constructor(getAgentHandlers: GetAgentHandlers)
Initialize(call: any, callback: any): void
EvaluateBlock(call: any, callback: any): void
EvaluateTx(call: any, callback: any): void
EvaluateAlert(call: any, callback: any): void
}
constructor(getAgentHandlers: GetAgentHandlers);
Initialize(call: any, callback: any): void;
EvaluateBlock(call: any, callback: any): void;
EvaluateTx(call: any, callback: any): void;
EvaluateAlert(call: any, callback: any): void;
HealthCheck(call: any, callback: any): void;
}
28 changes: 27 additions & 1 deletion cli/commands/run/server/agent.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,31 @@ module.exports = class AgentController {
this.initializeResponse = {};
}

async HealthCheck(call, callback) {
let errors = [];
let status = "SUCCESS";

if (this.healthCheck) {
try {
const response = await this.healthCheck();
if (response && response.length > 0) {
status = "ERROR";
errors = response.map((r) => ({ message: r }));
}
} catch (e) {
console.log(`${new Date().toISOString()} healthCheck`);
console.log(e);
status = "ERROR";
errors = [{ message: e.message }];
}
}

callback(null, {
status,
errors,
});
}

async Initialize(call, callback) {
let status = "SUCCESS";

Expand All @@ -35,7 +60,7 @@ module.exports = class AgentController {
}

callback(null, {
status: status,
status,
alertConfig: this.initializeResponse
? this.initializeResponse.alertConfig
: undefined,
Expand Down Expand Up @@ -152,6 +177,7 @@ module.exports = class AgentController {
this.handleBlock = agentHandlers.handleBlock;
this.handleTransaction = agentHandlers.handleTransaction;
this.handleAlert = agentHandlers.handleAlert;
this.healthCheck = agentHandlers.healthCheck;
} catch (e) {
console.log(e);
}
Expand Down
29 changes: 26 additions & 3 deletions cli/commands/run/server/agent.proto
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ service Agent {
rpc EvaluateTx (EvaluateTxRequest) returns (EvaluateTxResponse) {}
rpc EvaluateBlock (EvaluateBlockRequest) returns (EvaluateBlockResponse) {}
rpc EvaluateAlert (EvaluateAlertRequest) returns (EvaluateAlertResponse) {}
rpc HealthCheck (HealthCheckRequest) returns (HealthCheckResponse) {}
}

message Error {
Expand All @@ -24,9 +25,23 @@ enum ResponseStatus {
SUCCESS = 2;
}

message HealthCheckRequest {}

message HealthCheckResponse {
enum ResponseStatus {
UNKNOWN = 0;
ERROR = 1;
SUCCESS = 2;
}

ResponseStatus status = 1;
repeated Error errors = 2;
}

message InitializeRequest {
string agentId = 1;
string proxyHost = 2;
int32 shardId = 3;
}

message InitializeResponse {
Expand All @@ -50,16 +65,20 @@ message CombinerBotSubscription {
message EvaluateTxRequest {
string requestId = 1;
TransactionEvent event = 2;
int32 shardId = 3;
}

message EvaluateBlockRequest {
string requestId = 1;
BlockEvent event = 2;
int32 shardId = 3;
}

message EvaluateAlertRequest {
string requestId = 1;
AlertEvent event = 2;
string targetBotId = 3;
int32 shardId = 4;
}

message EvaluateTxResponse {
Expand Down Expand Up @@ -122,6 +141,7 @@ message BlockEvent {
repeated string transactions = 18;
string transactionsRoot = 19;
repeated string uncles = 20;
string baseFeePerGas = 21;
}

EventType type = 1;
Expand All @@ -147,6 +167,7 @@ message TransactionEvent {
string blockHash = 1;
string blockNumber = 2;
string blockTimestamp = 3;
string baseFeePerGas = 4;
}

message EthTransaction {
Expand All @@ -162,6 +183,8 @@ message TransactionEvent {
string to = 10;
string hash = 11;
string from = 12;
string maxFeePerGas = 13;
string maxPriorityFeePerGas = 14;
}

message Log {
Expand Down Expand Up @@ -273,16 +296,16 @@ message AlertEvent {

message SourceAlertEvent {
string botId = 1;
string alertHash = 2;
string hash = 2;
string timestamp = 3;
string chainId = 4;
uint64 chainId = 4;
}

message Source {
string transactionHash = 1;
Bot bot = 2;
Block block = 3;
SourceAlertEvent sourceEvent = 4;
SourceAlertEvent sourceAlert = 4;
}

message Label {
Expand Down
13 changes: 13 additions & 0 deletions cli/commands/run/server/alert.proto
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ enum AlertType {
BLOCK = 2;
PRIVATE = 3;
COMBINATION = 4;
API = 5;
}

message AgentInfo {
Expand Down Expand Up @@ -162,4 +163,16 @@ message Finding {
repeated string relatedAlerts = 13;
string uniqueKey = 14;
Source source = 15;
string timestamp = 16;
}

message APIAlert {
message APIAlertAgent {
string id = 1;
}
string id = 1;
AlertType type = 2;
Finding finding = 3;
APIAlertAgent agent = 4;
string timestamp = 5;
}
3 changes: 2 additions & 1 deletion cli/utils/get.agent.handlers.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { HandleBlock, HandleTransaction, HandleAlert, Initialize, InitializeResponse } from "../../sdk"
import { HandleBlock, HandleTransaction, HandleAlert, Initialize, InitializeResponse, HealthCheck } from "../../sdk"
import { assertExists, assertIsNonEmptyString } from "."
import { GetPythonAgentHandlers } from './get.python.agent.handlers'

type AgentHandlers = {
initialize?: Initialize,
initializeResponse?: InitializeResponse | void,
healthCheck?: HealthCheck,
handleTransaction?: HandleTransaction,
handleBlock?: HandleBlock,
handleAlert?: HandleAlert,
Expand Down
1 change: 1 addition & 0 deletions sdk/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { InitializeResponse } from "./initialize.response"
import { TransactionEvent } from "./transaction.event"

export type Initialize = () => Promise<InitializeResponse | void>
export type HealthCheck = () => Promise<string[] | void>
export type HandleTransaction = (txEvent: TransactionEvent) => Promise<Finding[]>
export type HandleBlock = (blockEvent: BlockEvent) => Promise<Finding[]>
export type HandleAlert = (alertEvent: AlertEvent) => Promise<Finding[]>
2 changes: 2 additions & 0 deletions sdk/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ import {
HandleTransaction,
HandleBlock,
HandleAlert,
HealthCheck
} from "./handlers";

interface DiContainer {
Expand All @@ -77,6 +78,7 @@ export {
HandleTransaction,
HandleBlock,
HandleAlert,
HealthCheck,
Finding,
FindingSeverity,
FindingType,
Expand Down

0 comments on commit 94a3768

Please sign in to comment.