Skip to content

Commit

Permalink
Merge pull request #2401 from dedis/js-cothority-arrange
Browse files Browse the repository at this point in the history
@cothority/byzcoin meaningful instruction print
  • Loading branch information
nkcr committed Nov 17, 2020
2 parents 708118b + a6f3228 commit 1c1247b
Show file tree
Hide file tree
Showing 9 changed files with 274 additions and 3 deletions.
13 changes: 13 additions & 0 deletions external/js/cothority/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions external/js/cothority/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"@ethersproject/bignumber": "^5.0.5",
"@stablelib/blake2xs": "^0.10.4",
"@types/ethereumjs-abi": "^0.6.3",
"@types/varint": "^5.0.0",
"buffer": "^5.2.1",
"crypto-browserify": "^3.12.0",
"elliptic": "^6.5.3",
Expand All @@ -56,6 +57,7 @@
"tslib": "^1.10.0",
"url-parse": "^1.4.7",
"util": "^0.11.1",
"varint": "^6.0.0",
"ws": "^6.1.2"
},
"devDependencies": {
Expand Down
67 changes: 67 additions & 0 deletions external/js/cothority/src/byzcoin/beautifier/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { Darc } from "../../darc";
import { Roster } from "../../network";
import { Argument } from "../index";
import { ChainConfig } from "../index";
import { IBeautifyArgument } from "./utils";
// tslint:disable-next-line
const varint = require("varint");

/**
* Arrange arguments for a config contract, ie. provide a meaningful
* representation of its arguments.
*/
export class ConfigBeautifier {
static Spawn(args: Argument[]): IBeautifyArgument[] {
const res = Array<IBeautifyArgument>();

args.forEach((arg) => {
switch (arg.name) {
case "darc":
const darc = Darc.decode(arg.value);
res.push({name: "darc", value: darc.description.toString(), full: darc.toString()});
break;
case "block_interval":
res.push({name: "block_interval", value: `${varint.decode(arg.value, 0) / 1e6} ms`});
break;
case "max_block_size":
res.push({name: "max_block_size", value: `${varint.decode(arg.value, 0)} bytes`});
break;
case "roster":
const r = Roster.decode(arg.value);
res.push({name: "roster", value: r.id.toString("hex"), full: r.toTOML()});
break;
case "trie_nonce":
res.push({name: "trie_nonce", value: `${varint.decode(arg.value, 0)}`});
break;
case "darc_contracts":
res.push({name: "darc_contracts", value: arg.value.toString("hex")});
break;
default:
res.push({name: arg.name, value: "unspecified", full: arg.value.toString("hex")});
break;
}
});

return res;
}

static Invoke(args: Argument[]): IBeautifyArgument[] {
const res = Array<IBeautifyArgument>();

args.forEach((arg) => {
switch (arg.name) {
case "config":
// Calypso tests won't pass if we use ChainConfig.
// const config = ChainConfig.decode(arg.value);
// res.push({name: "darc", value: "chain config", full: config.toString()});
res.push({name: "darc", value: "chain config"});
break;
default:
res.push({name: arg.name, value: "unspecified", full: arg.value.toString("hex")});
break;
}
});

return res;
}
}
45 changes: 45 additions & 0 deletions external/js/cothority/src/byzcoin/beautifier/darc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { Darc as d } from "../../darc";
import { Roster } from "../../network";
import { Argument } from "../index";
import { IBeautifyArgument } from "./utils";

/**
* Arrange arguments for a Darc contract, ie. provide a meaningful
* representation of its arguments.
*/
export class DarcBeautifier {
static Spawn(args: Argument[]): IBeautifyArgument[] {
const res = Array<IBeautifyArgument>();

args.forEach((arg) => {
switch (arg.name) {
case "darc":
const darc = d.decode(arg.value);
res.push({name: "darc", value: darc.description.toString(), full: darc.toString()});
break;
default:
res.push({name: arg.name, value: "unspecified", full: arg.value.toString("hex")});
break;
}
});

return res;
}
static Invoke(args: Argument[]): IBeautifyArgument[] {
const res = Array<IBeautifyArgument>();

args.forEach((arg) => {
switch (arg.name) {
case "darc":
const darc = d.decode(arg.value);
res.push({name: "darc", value: darc.description.toString(), full: darc.toString()});
break;
default:
res.push({name: arg.name, value: "unspecified", full: arg.value.toString("hex")});
break;
}
});

return res;
}
}
17 changes: 17 additions & 0 deletions external/js/cothority/src/byzcoin/beautifier/default.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Argument } from "../index";
import { IBeautifyArgument } from "./utils";

/**
* Default representation of arguments
*/
export class DefaultBeautifier {
static Hex(args: Argument[]): IBeautifyArgument[] {
const res = Array<IBeautifyArgument>();

args.forEach((arg) => {
res.push({name: arg.name, value: arg.value.toString("hex")});
});

return res;
}
}
12 changes: 12 additions & 0 deletions external/js/cothority/src/byzcoin/beautifier/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { ConfigBeautifier } from "./config";
import { DarcBeautifier } from "./darc";
import { DefaultBeautifier } from "./default";
import { IBeautifierSchema, IBeautifyArgument } from "./utils";

export {
DefaultBeautifier,
DarcBeautifier,
ConfigBeautifier,
IBeautifierSchema,
IBeautifyArgument,
};
14 changes: 14 additions & 0 deletions external/js/cothority/src/byzcoin/beautifier/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Argument } from "../index";

export interface IBeautifierSchema {
status: 0 | 1;
type: "spawn" | "invoke" | "delete";
contract: string;
args: IBeautifyArgument[];
}

export interface IBeautifyArgument {
name: string;
value: string;
full?: string;
}
94 changes: 91 additions & 3 deletions external/js/cothority/src/byzcoin/client-transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Message, Properties } from "protobufjs/light";
import IdentityWrapper, { IIdentity } from "../darc/identity-wrapper";
import Signer from "../darc/signer";
import { EMPTY_BUFFER, registerMessage } from "../protobuf";
import { ConfigBeautifier, DarcBeautifier, DefaultBeautifier, IBeautifierSchema } from "./beautifier/index";
import Instance, { InstanceID } from "./instance";

export interface ICounterUpdater {
Expand Down Expand Up @@ -122,9 +123,6 @@ type InstructionType = 0 | 1 | 2;
* An instruction represents one action
*/
export class Instruction extends Message<Instruction> {
static readonly typeSpawn = 0;
static readonly typeInvoke = 1;
static readonly typeDelete = 2;

/**
* Get the type of the instruction
Expand All @@ -142,6 +140,9 @@ export class Instruction extends Message<Instruction> {
}
throw new Error("instruction without type");
}
static readonly typeSpawn = 0;
static readonly typeInvoke = 1;
static readonly typeDelete = 2;

/**
* @see README#Message classes
Expand Down Expand Up @@ -320,6 +321,93 @@ export class Instruction extends Message<Instruction> {
return this.deriveId(what);
}

/**
* Return a general-purpose json representation of an instruction that is
* contracts aware, ie. knows how to render specific contracts arguments. It
* returns the following status:
*
* 0: the default hex representation is used
* 1: has been specifically interpreted
*
* The JSON has the following structure:
*
* {
* status: 0 | 1
* type: "spawn|invoke|delete",
* contract: "config|darc|..."
* args: [
* {
* "name": "<NAME>",
* "value": "<VALUE>", // Arg value or its summary
* "full": "<FULL VALUE>" // Optional. useful if the argument
* // have a very long representation.
* }
* ...
* ]
* }
*
*/
beautify(): IBeautifierSchema {
const res: IBeautifierSchema = {status: 0, type: "spawn", contract: "", args: []};

switch (this.type) {
case Instruction.typeSpawn:
res.type = "spawn";

switch (this.spawn.contractID) {
case "config":
res.contract = "config";
const args = Array<Argument>();
res.args = ConfigBeautifier.Spawn(args);
break;
case "darc":
res.contract = "darc";
res.args = DarcBeautifier.Spawn(this.spawn.args);
break;
default:
res.status = 1;
res.args = DefaultBeautifier.Hex(this.spawn.args);
}

break;

case Instruction.typeInvoke:
res.type = "invoke";

switch (this.invoke.contractID) {
case "config":
res.contract = "config";
res.args = ConfigBeautifier.Invoke(this.invoke.args);
break;
case "darc":
res.contract = "darc";
res.args = DarcBeautifier.Invoke(this.invoke.args);
break;
default:
res.status = 1;
res.args = DefaultBeautifier.Hex(this.spawn.args);
}

break;

case Instruction.typeDelete:
res.type = "delete";

switch (this.delete.contractID) {
default:
res.status = 1;
res.args = DefaultBeautifier.Hex(this.spawn.args);
}

break;

default:
throw new Error("unknown instruction type");
}

return res;
}

protected hashForVersion(version: number): Buffer {
const h = createHash("sha256");
h.update(this.instanceID);
Expand Down
13 changes: 13 additions & 0 deletions external/js/cothority/src/byzcoin/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,19 @@ export default class ChainConfig extends Message<ChainConfig> {
},
});
}

/**
* Return a string representation of a config
*/
toString(): string {
let res = "Chainconfig:\n";
res += "- roster:\n";
res += this.roster.toTOML() + "\n";
res += "- blockInterval: " + this.blockInterval + "\n";
res += "- maxBlockSize: " + this.maxBlockSize + "\n";

return res;
}
}

ChainConfig.register();

0 comments on commit 1c1247b

Please sign in to comment.