Skip to content

Commit 2465572

Browse files
committed
model for a randomness-beacon.
1 parent c80617d commit 2465572

File tree

5 files changed

+197
-1
lines changed

5 files changed

+197
-1
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import { Hashing, HashedObject, MutableObject, MutationOp } from 'data/model';
2+
3+
import { BeaconValueOp } from './BeaconValueOp';
4+
5+
class Beacon extends MutableObject {
6+
7+
static className = 'hhs/v0/examples/Beacon';
8+
static opClasses = [BeaconValueOp.className];
9+
10+
steps?: number;
11+
12+
_lastOp?: BeaconValueOp;
13+
_values: string[];
14+
15+
constructor(seed?: string, steps?: number) {
16+
super(Beacon.opClasses);
17+
18+
if (seed !== undefined && steps !== undefined) {
19+
this.setId(seed);
20+
this.steps = steps;
21+
}
22+
23+
this._values = [];
24+
}
25+
26+
27+
async mutate(op: MutationOp, isNew: boolean): Promise<void> {
28+
29+
if (op instanceof BeaconValueOp) {
30+
31+
if (!isNew && this._lastOp !== undefined &&
32+
!this._lastOp.equals(op)) {
33+
34+
if (op.prevOps?.size() === 0) {
35+
if (this._lastOp !== undefined) {
36+
throw new Error('Initial BeaconValueOp received, but there are already other ops in this beacon.');
37+
}
38+
39+
40+
} else {
41+
if (this._lastOp === undefined) {
42+
throw new Error('Non-initial BeaconValueOp received, but there are no values in this beacon.');
43+
}
44+
45+
if (!this._lastOp.equals(op.prevOps?.values().next().value)) {
46+
throw new Error('Received BeaconValueOp does not point to last known beacon value.');
47+
}
48+
}
49+
50+
this._lastOp = op;
51+
this._values.push(Hashing.toHex(op.hash()));
52+
53+
}
54+
55+
56+
}
57+
58+
}
59+
60+
getClassName(): string {
61+
throw new Error('Method not implemented.');
62+
}
63+
64+
init(): void {
65+
66+
}
67+
68+
validate(references: Map<string, HashedObject>): boolean {
69+
references;
70+
71+
return this.steps !== undefined && this.getId() !== undefined;
72+
}
73+
74+
}
75+
76+
export { Beacon };
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
2+
import { Hashing, Hash, HashedObject, MutationOp } from 'data/model';
3+
4+
import { Beacon } from './Beacon';
5+
6+
const createVdf = require('@subspace/vdf');
7+
8+
9+
class BeaconValueOp extends MutationOp {
10+
11+
static className = 'hhs/v0/examples/BeaconValueOp';
12+
13+
static vdfInit = async () => {
14+
BeaconValueOp.vdfVerifier = await createVdf();
15+
};
16+
static vdfVerifier: any;
17+
18+
19+
seq?: number;
20+
vdfResult?: string;
21+
22+
constructor(target?: Beacon, seq?: number, vdfResult?: string) {
23+
super(target);
24+
25+
if (seq !== undefined && vdfResult !== undefined) {
26+
this.seq = seq;
27+
this.vdfResult = vdfResult;
28+
}
29+
}
30+
31+
getClassName(): string {
32+
return BeaconValueOp.className;
33+
}
34+
35+
init(): void {
36+
37+
}
38+
39+
validate(references: Map<Hash, HashedObject>): boolean {
40+
41+
if (this.seq === undefined || this.vdfResult === undefined) {
42+
return false;
43+
}
44+
45+
if (this.seq < 0) {
46+
return false;
47+
}
48+
49+
if (!super.validate(references)) {
50+
return false;
51+
}
52+
53+
if (! (this.getTarget() instanceof Beacon)) {
54+
return false;
55+
}
56+
57+
if (this.getAuthor() !== undefined) {
58+
return false;
59+
}
60+
61+
if (this.prevOps === undefined) {
62+
return false;
63+
}
64+
65+
let challenge: string;
66+
67+
if (this.prevOps.size() === 0) {
68+
if (this.seq !== 0) {
69+
return false;
70+
}
71+
72+
challenge = this.getTarget().getId() as string;
73+
} else {
74+
if (this.prevOps.size() !== 1) {
75+
return false;
76+
}
77+
78+
let prev = this.prevOps.values().next().value;
79+
80+
if (!(prev instanceof BeaconValueOp)) {
81+
return false;
82+
}
83+
84+
if (prev.getTarget().equals(this.getTarget())) {
85+
return false;
86+
}
87+
88+
if ((prev.seq as number) + 1 !== this.seq) {
89+
return false;
90+
}
91+
92+
challenge = Hashing.toHex(prev.hash());
93+
}
94+
95+
const steps = (this.getTarget() as Beacon).steps as number;
96+
97+
if (!BeaconValueOp.vdfVerifier.verify(steps, challenge, this.vdfResult, 2048, true)) {
98+
return false;
99+
}
100+
101+
return true;
102+
103+
}
104+
105+
}
106+
107+
export { BeaconValueOp };

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
},
2424
"devDependencies": {
2525
"@hyper-hyper-space/node-env": "^0.8.0",
26+
"@subspace/vdf": "^0.1.0",
2627
"@types/jest": "^26.0.0",
2728
"@types/ws": "^7.2.6",
2829
"dts-generator": "^3.0.0",

src/data/containers/MutableSet.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ MutableSetDeleteOp.registerClass(MutableSetDeleteOp.className, MutableSetDeleteO
199199

200200
class MutableSet<T extends HashedObject> extends MutableObject {
201201

202-
static className = 'hss/MutableSet';
202+
static className = 'hss/v0/MutableSet';
203203
static opClasses = [MutableSetAddOp.className, MutableSetDeleteOp.className];
204204
static logger = new Logger(MutableSet.className, LogLevel.INFO);
205205

yarn.lock

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2103,6 +2103,13 @@
21032103
dependencies:
21042104
"@sinonjs/commons" "^1.7.0"
21052105

2106+
"@subspace/vdf@^0.1.0":
2107+
version "0.1.0"
2108+
resolved "https://registry.yarnpkg.com/@subspace/vdf/-/vdf-0.1.0.tgz#f341dc742e84df7eac9bf9653a08ddfea2b615c1"
2109+
integrity sha512-iKYdYOcAtALmxxgreWqnHphT7m8HIyBsAYLrPSRvOJM9hTY+Rfg+EsLiqXLgHoIXyy980dQhoE4yIwu8xXDmvg==
2110+
dependencies:
2111+
"@types/node" "^11.13.4"
2112+
21062113
"@svgr/babel-plugin-add-jsx-attribute@^5.4.0":
21072114
version "5.4.0"
21082115
resolved "https://registry.yarnpkg.com/@svgr/babel-plugin-add-jsx-attribute/-/babel-plugin-add-jsx-attribute-5.4.0.tgz#81ef61947bb268eb9d50523446f9c638fb355906"
@@ -2301,6 +2308,11 @@
23012308
resolved "https://registry.yarnpkg.com/@types/node/-/node-14.0.13.tgz#ee1128e881b874c371374c1f72201893616417c9"
23022309
integrity sha512-rouEWBImiRaSJsVA+ITTFM6ZxibuAlTuNOCyxVbwreu6k6+ujs7DfnU9o+PShFhET78pMBl3eH+AGSI5eOTkPA==
23032310

2311+
"@types/node@^11.13.4":
2312+
version "11.15.35"
2313+
resolved "https://registry.yarnpkg.com/@types/node/-/node-11.15.35.tgz#282b97494e4967649d776c3cc91da5f861a03c40"
2314+
integrity sha512-kmxE30w+R//Vh/+dlVS5DPPyzPtWtrMrJ9+io7jvkPtWv3X43ERA4HTcFEn/R7dtHO3HCMU1y3WKf3NDLYhQEQ==
2315+
23042316
"@types/normalize-package-data@^2.4.0":
23052317
version "2.4.0"
23062318
resolved "https://registry.yarnpkg.com/@types/normalize-package-data/-/normalize-package-data-2.4.0.tgz#e486d0d97396d79beedd0a6e33f4534ff6b4973e"

0 commit comments

Comments
 (0)