Skip to content

Commit de0581d

Browse files
committed
beacon - WIP
1 parent e83f737 commit de0581d

File tree

4 files changed

+19
-30
lines changed

4 files changed

+19
-30
lines changed

examples/randomness-beacon/model/Beacon.ts

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -66,19 +66,11 @@ class Beacon extends MutableObject implements SpaceEntryPoint {
6666
Beacon.log.debug(() => 'Racing for challenge (' + this.steps + ' steps): "' + this.currentChallenge() + '".');
6767

6868
this._computation = new Worker('./dist-examples/examples/randomness-beacon/model/worker.js');
69-
console.log('is death immediate?')
70-
this._computation.on('online', () => {console.log('worker is online')});
7169
this._computation.on('error', (err: Error) => { console.log('ERR');console.log(err)});
72-
this._computation.on('exit', (exitCode: number) => {
73-
console.log('worker exited with ' + exitCode);
74-
})
75-
console.log('created worker')
7670
this._computation.on('message', async (msg: {challenge: string, result: string}) => {
7771

7872
Beacon.log.debug(() => 'Solved challenge "' + msg.challenge + '" with: "' + msg.result + '".');
7973

80-
81-
8274
if (msg.challenge === this.currentChallenge()) {
8375
let op = new BeaconValueOp(this, this.currentSeq(), msg.result);
8476

@@ -92,25 +84,23 @@ class Beacon extends MutableObject implements SpaceEntryPoint {
9284
await this.getStore().save(this);
9385

9486
} else {
95-
console.log('mismatched challenge');
87+
Beacon.log.debug('Mismatched challenge - could be normal.');
9688
}
9789
});
9890
this._computation.postMessage({steps: this.steps, challenge: this.currentChallenge()});
99-
console.log('posted message to worker')
10091

10192
} else {
102-
console.log('race was called but a computation is running');
93+
Beacon.log.debug('Race was called but a computation is running.');
10394
}
10495
}
10596

10697
stopRace() {
107-
console.log('stopRace()');
98+
Beacon.log.debug('Going to stop current VDF computation.');
10899
if (this._computation !== undefined) {
109100
if (this._computationTermination === undefined) {
110-
console.log('need to stop')
111101
this._computationTermination = this._computation.terminate().then(
112102
(ret: number) => {
113-
console.log('stopped');
103+
Beacon.log.trace('Stopped VDF computation');
114104
this._computation = undefined;
115105
this._computationTermination = undefined;
116106
return ret;
@@ -174,15 +164,14 @@ class Beacon extends MutableObject implements SpaceEntryPoint {
174164

175165
if (this._autoCompute) {
176166
if (this._computation === undefined) {
177-
console.log('computation was finished');
178167
this.race();
179168
} else {
180-
console.log('chaining');
181169
this.stopRace();
182-
this._computationTermination?.then(() => { console.log('finished now!');this.race(); });
170+
this._computationTermination?.then(() => { this.race(); });
183171
}
184-
185172
}
173+
174+
Beacon.log.debug('Challenge now is "' + this.currentChallenge() + '".');
186175

187176
}
188177

examples/randomness-beacon/model/BeaconValueOp.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ class BeaconValueOp extends MutationOp {
114114
const challengeBuffer = Buffer.from(challenge, 'hex');
115115
const resultBuffer = Buffer.from(this.vdfResult, 'hex');
116116

117-
if (!BeaconValueOp.vdfVerifier.verify(steps, challengeBuffer, resultBuffer, 2048, true)) {
117+
if (!BeaconValueOp.vdfVerifier.verify(steps, challengeBuffer, resultBuffer, 2048, false)) {
118118
BeaconValueOp.log.trace('VDF verification failed.');
119119
return false;
120120
}

examples/randomness-beacon/model/VDF.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,28 @@
1+
import { Logger, LogLevel } from "util/logging";
2+
13
const createVdf = require('@subspace/vdf').default;
24
(global as any).document = { }; // yikes!
35

46
class VDF {
7+
8+
static log = new Logger(VDF.name, LogLevel.TRACE)
9+
510
static async compute(challenge: string, steps: number): Promise<string> {
611

7-
console.log('computing...')
12+
VDF.log.debug('Computing VDF...');
813

914
const vdfInstance = await createVdf();
10-
const result = vdfInstance.generate(steps, Buffer.from(challenge, 'hex'), 2048, true);
15+
const result = vdfInstance.generate(steps, Buffer.from(challenge, 'hex'), 2048, false);
1116

12-
console.log('done!')
17+
VDF.log.debug('Done computing VDF.')
1318

1419
const t = Date.now();
1520

16-
console.log('verification: ' + vdfInstance.verify(steps, Buffer.from(challenge, 'hex'), result, 2048, true));
21+
VDF.log.debug('VDF sekf verification: ' + vdfInstance.verify(steps, Buffer.from(challenge, 'hex'), result, 2048, true));
1722

1823
const elapsed = Date.now() - t;
1924

20-
console.log('verification took ' + elapsed + ' millis');
25+
VDF.log.debug('verification took ' + elapsed + ' millis');
2126

2227
return Buffer.from(result).toString('hex');
2328
}

examples/randomness-beacon/model/worker.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,9 @@ class VDFWorker {
55
static start() {
66

77
parentPort?.on('message', async (q: {challenge: string, steps: number}) => {
8-
9-
10-
console.log('worker woke up')
8+
119

1210
let result = await VDF.compute(q.challenge, q.steps);
13-
14-
console.log('worker has worked')
1511

1612
if (parentPort !== undefined && parentPort !== null) {
1713
parentPort.postMessage(
@@ -22,7 +18,6 @@ class VDFWorker {
2218
}
2319
);
2420

25-
console.log('worker has answered')
2621
}
2722

2823

0 commit comments

Comments
 (0)