Skip to content

Commit

Permalink
refactor: update code to support latest IRuntime interface
Browse files Browse the repository at this point in the history
- move proto from types to protocal
- updated proto to work with new voting functionality
- updated client/server to work with new proto

Signed-off-by: Kostas Christopoulos <k.christopoulos@rocketfueldev.com>
  • Loading branch information
kchrist-rocketfueldev committed Oct 26, 2023
1 parent 0c02986 commit de2cbb2
Show file tree
Hide file tree
Showing 10 changed files with 688 additions and 548 deletions.
5 changes: 4 additions & 1 deletion common/docker/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@
"dependencies": {
"@grpc/grpc-js": "^1.9.2",
"@grpc/proto-loader": "^0.7.9",
"@kyvejs/protocol": "1.0.11"
"@kyvejs/protocol": "1.0.14",
"ajv": "^8.12.0",
"axios": "^0.27.2",
"dotenv": "^16.3.1"
},
"devDependencies": {
"pkg": "^5.8.0",
Expand Down
310 changes: 178 additions & 132 deletions common/docker/src/protos/runtime.ts

Large diffs are not rendered by default.

9 changes: 3 additions & 6 deletions common/docker/src/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ export default class Docker implements IRuntime {

async validateSetConfig(rawConfig: string): Promise<string> {
return new Promise<string>((resolve, reject) => {
console.log(rawConfig);
this.grpcClient.validateSetConfig(
{
raw_config: rawConfig,
Expand All @@ -84,7 +83,6 @@ export default class Docker implements IRuntime {

async getDataItem(key: string): Promise<DataItem> {
return new Promise<DataItem>((resolve, reject) => {
console.log(this.config);
this.grpcClient.getDataItem(
{
config: {
Expand All @@ -101,7 +99,6 @@ export default class Docker implements IRuntime {
throw new Error('runtimeResponse.dataItem is undefined');
}

console.log(runtimeResponse);
const responseDataItem: DataItem = {
key: runtimeResponse.data_item.key,
value: JSON.parse(runtimeResponse.data_item.value),
Expand Down Expand Up @@ -174,7 +171,7 @@ export default class Docker implements IRuntime {
async validateDataItem(
proposedDataItem: DataItem,
validationDataItem: DataItem
): Promise<boolean> {
): Promise<number> {
const request_proposed_data_item = {
key: proposedDataItem.key,
value: JSON.stringify(proposedDataItem.value),
Expand All @@ -183,7 +180,7 @@ export default class Docker implements IRuntime {
key: validationDataItem.key,
value: JSON.stringify(validationDataItem.value),
};
return new Promise<boolean>((resolve, reject) => {
return new Promise<number>((resolve, reject) => {
this.grpcClient.validateDataItem(
{
config: {
Expand All @@ -197,7 +194,7 @@ export default class Docker implements IRuntime {
// Handle the error here if needed
reject(error);
} else {
resolve(runtimeResponse.valid);
resolve(runtimeResponse.vote);
}
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ message RuntimeConfig {
string serialized_config = 1;
}


/* Defines the vote enum */
enum VOTE {
UNSPECIFIED = 0;
VALID = 1;
INVALID = 2;
ABSTAIN = 3;
}

/* getRuntimeName
* Request returning the name of the runtime
* returns the runtime name as a string */
Expand Down Expand Up @@ -89,7 +98,7 @@ message ValidateDataItemRequest {
}

message ValidateDataItemResponse {
bool valid = 1;
int32 vote = 1;
}

/* summarizeDataBundle
Expand Down
4 changes: 2 additions & 2 deletions docker-integrations/tendermint/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
"export-grpc-stubs": "protoc --plugin=./node_modules/.bin/protoc-gen-ts_proto --ts_proto_opt=snakeToCamel=false --ts_proto_opt=outputServices=grpc-js --ts_proto_out=./src/proto --proto_path=./src/proto runtime.proto",
"test-build": "yarn build && rimraf out"
},
"bin": "./dist/index.js",
"bin": "./dist/src/index.js",
"pkg": {
"scripts": "./dist/index.js",
"scripts": "./dist/src/index.js",
"targets": [
"latest-linux-x64",
"latest-linux-arm64",
Expand Down
64 changes: 55 additions & 9 deletions docker-integrations/tendermint/src/protos/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,52 @@ import * as _m0 from "protobufjs/minimal";

export const protobufPackage = "";

/** Defines the vote enum */
export enum VOTE {
UNSPECIFIED = 0,
VALID = 1,
INVALID = 2,
ABSTAIN = 3,
UNRECOGNIZED = -1,
}

export function vOTEFromJSON(object: any): VOTE {
switch (object) {
case 0:
case "UNSPECIFIED":
return VOTE.UNSPECIFIED;
case 1:
case "VALID":
return VOTE.VALID;
case 2:
case "INVALID":
return VOTE.INVALID;
case 3:
case "ABSTAIN":
return VOTE.ABSTAIN;
case -1:
case "UNRECOGNIZED":
default:
return VOTE.UNRECOGNIZED;
}
}

export function vOTEToJSON(object: VOTE): string {
switch (object) {
case VOTE.UNSPECIFIED:
return "UNSPECIFIED";
case VOTE.VALID:
return "VALID";
case VOTE.INVALID:
return "INVALID";
case VOTE.ABSTAIN:
return "ABSTAIN";
case VOTE.UNRECOGNIZED:
default:
return "UNRECOGNIZED";
}
}

/**
* The main data entity served by the gRPC service
* Contains the block key and the block value as a serialized value
Expand Down Expand Up @@ -112,7 +158,7 @@ export interface ValidateDataItemRequest {
}

export interface ValidateDataItemResponse {
valid: boolean;
vote: number;
}

/**
Expand Down Expand Up @@ -1098,13 +1144,13 @@ export const ValidateDataItemRequest = {
};

function createBaseValidateDataItemResponse(): ValidateDataItemResponse {
return { valid: false };
return { vote: 0 };
}

export const ValidateDataItemResponse = {
encode(message: ValidateDataItemResponse, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
if (message.valid === true) {
writer.uint32(8).bool(message.valid);
if (message.vote !== 0) {
writer.uint32(8).int32(message.vote);
}
return writer;
},
Expand All @@ -1121,7 +1167,7 @@ export const ValidateDataItemResponse = {
break;
}

message.valid = reader.bool();
message.vote = reader.int32();
continue;
}
if ((tag & 7) === 4 || tag === 0) {
Expand All @@ -1133,13 +1179,13 @@ export const ValidateDataItemResponse = {
},

fromJSON(object: any): ValidateDataItemResponse {
return { valid: isSet(object.valid) ? globalThis.Boolean(object.valid) : false };
return { vote: isSet(object.vote) ? globalThis.Number(object.vote) : 0 };
},

toJSON(message: ValidateDataItemResponse): unknown {
const obj: any = {};
if (message.valid === true) {
obj.valid = message.valid;
if (message.vote !== 0) {
obj.vote = Math.round(message.vote);
}
return obj;
},
Expand All @@ -1149,7 +1195,7 @@ export const ValidateDataItemResponse = {
},
fromPartial<I extends Exact<DeepPartial<ValidateDataItemResponse>, I>>(object: I): ValidateDataItemResponse {
const message = createBaseValidateDataItemResponse();
message.valid = object.valid ?? false;
message.vote = object.vote ?? 0;
return message;
},
};
Expand Down
39 changes: 32 additions & 7 deletions docker-integrations/tendermint/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@

import * as grpc from '@grpc/grpc-js';
import axios from 'axios';

import { name, version } from '../package.json';
import { DataItem } from './protos/runtime';
import { DataItem, VOTE } from './protos/runtime';

type EmptyRequest = Record<string, never>;

Expand Down Expand Up @@ -76,14 +77,18 @@ export class TendermintServer {
const blockResponse = await axios.get(
`${config.rpc}/block?height=${key}`
);

const block = blockResponse.data.result;


// Fetch block results from rpc at the given block height
const blockResultsResponse = await axios.get(
`${config.rpc}/block_results?height=${key}`
);

const blockResults = blockResultsResponse.data.result;


// Construct the Value message
const value = {
block: block,
Expand Down Expand Up @@ -246,9 +251,9 @@ export class TendermintServer {
proposed_data_item: DataItem;
validation_data_item: DataItem;
},
{ valid: boolean }
{ vote: number }
>,
callback: grpc.sendUnaryData<{ valid: boolean }>
callback: grpc.sendUnaryData<{ vote: number }>
) {
try {
const request_proposed_data_item = call.request.proposed_data_item;
Expand All @@ -262,11 +267,31 @@ export class TendermintServer {
value: JSON.parse(request_validation_data_item.value),
};

// Apply equal comparison
const isValid =
JSON.stringify(proposedDataItem) === JSON.stringify(validationDataItem);
if (
JSON.stringify(proposedDataItem) === JSON.stringify(validationDataItem)
) {
callback(null, { vote: VOTE.VALID });
return;
}

// prevent nondeterministic misbehaviour due to osmosis-1 specific problems
if (validationDataItem.value.block.block.header.chain_id === "osmosis-1") {
// remove nondeterministic begin_block_events to prevent incorrect invalid vote
delete validationDataItem.value.block_results.begin_block_events;
delete proposedDataItem.value.block_results.begin_block_events;

if (
JSON.stringify(proposedDataItem) === JSON.stringify(validationDataItem)
) {
// vote abstain if begin_block_events are not equal
callback(null, { vote: VOTE.ABSTAIN });
return;

}
}

callback(null, { valid: isValid });
// vote invalid if data does not match
callback(null, { vote: VOTE.INVALID });
} catch (error: any) {
callback({
code: grpc.status.INTERNAL,
Expand Down
1 change: 1 addition & 0 deletions integrations/tendermint/src/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Ajv from 'ajv';
import block_schema from './schemas/block.json';
import block_results_schema from './schemas/block_result.json';


const ajv = new Ajv();

// Tendermint config
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"lint": "lerna run lint",
"lint:fix": "lerna run lint:fix",
"custom-version": "yarn lerna version --conventional-commits=false",
"export-grpc-stubs": "for dir in docker-integrations/*; do if [ -d \"$dir\" ]; then protoc --proto_path=./common/types/src --plugin=./node_modules/.bin/protoc-gen-ts_proto --ts_proto_opt=snakeToCamel=false --ts_proto_opt=outputServices=grpc-js --ts_proto_out=$dir/src ./common/types/src/protos/runtime.proto; fi; done"
"export-grpc-stubs": "protoc --proto_path=./common/protocol/src/types --plugin=./node_modules/.bin/protoc-gen-ts_proto --ts_proto_opt=snakeToCamel=false --ts_proto_opt=outputServices=grpc-js --ts_proto_out=./common/docker/src common/protocol/src/types/protos/runtime.proto"
},

"version": "0.0.0"
Expand Down
Loading

0 comments on commit de2cbb2

Please sign in to comment.