1+ import '@hyper-hyper-space/node-env' ;
2+
13import { Hashing , HashedObject , MutableObject , MutationOp } from 'data/model' ;
24
35import { BeaconValueOp } from './BeaconValueOp' ;
46
7+ import { Worker , parentPort } from 'worker_threads' ;
8+ import { Logger , LogLevel } from 'util/logging' ;
9+
10+ const createVdf = require ( '@subspace/vdf' ) . default ;
11+ ( global as any ) . document = { } ; // yikes!
12+
513class Beacon extends MutableObject {
614
15+ static log = new Logger ( Beacon . name , LogLevel . DEBUG )
16+
17+
718 static className = 'hhs/v0/examples/Beacon' ;
819 static opClasses = [ BeaconValueOp . className ] ;
920
@@ -12,6 +23,28 @@ class Beacon extends MutableObject {
1223 _lastOp ?: BeaconValueOp ;
1324 _values : string [ ] ;
1425
26+ _computation ?: Worker ;
27+ _autoCompute : boolean ;
28+
29+ static computeVdf ( ) : void {
30+
31+ parentPort ?. on ( 'message' , async ( q : { challenge : string , steps : number } ) => {
32+
33+ const vdfInstance = await createVdf ( ) ;
34+ const result = vdfInstance . generate ( q . steps , Buffer . from ( q . challenge , 'hex' ) , 2048 , true ) ;
35+
36+ parentPort ?. postMessage (
37+ {
38+ challenge : q . challenge ,
39+ steps : q . steps ,
40+ result : Buffer . from ( result ) . toString ( 'hex' )
41+ }
42+ ) ;
43+ } ) ;
44+
45+
46+ }
47+
1548 constructor ( seed ?: string , steps ?: number ) {
1649 super ( Beacon . opClasses ) ;
1750
@@ -21,22 +54,89 @@ class Beacon extends MutableObject {
2154 }
2255
2356 this . _values = [ ] ;
57+ this . _autoCompute = false ;
58+ }
59+
60+ startCompute ( ) {
61+ this . _autoCompute = true ;
62+ this . race ( ) ;
63+ }
64+
65+ stopCompute ( ) {
66+ this . _autoCompute = false ;
67+ this . stopCompute ( ) ;
68+ }
69+
70+ race ( ) {
71+ if ( this . _computation === undefined ) {
72+
73+ Beacon . log . debug ( ( ) => 'Racing for challenge (' + this . steps + ' steps): "' + this . currentChallenge ( ) + '".' ) ;
74+
75+ this . _computation = new Worker ( './dist-examples/examples/randomness-beacon/model/worker.js' ) ;
76+
77+ this . _computation . postMessage ( { steps : this . steps , challenge : this . currentChallenge ( ) } ) ;
78+
79+ this . _computation . on ( 'message' , async ( msg : { challenge : string , result : string } ) => {
80+
81+ Beacon . log . debug ( ( ) => 'Solved challenge "' + msg . challenge + '" with: "' + msg . result + '".' ) ;
82+
83+ this . stopRace ( ) ;
84+
85+ if ( msg . challenge === this . currentChallenge ( ) ) {
86+ let op = new BeaconValueOp ( this , this . currentSeq ( ) , msg . result ) ;
87+
88+ if ( this . _lastOp !== undefined ) {
89+ op . setPrevOps ( new Set ( [ this . _lastOp . createReference ( ) ] ) . values ( ) ) ;
90+ }
91+
92+ await this . applyNewOp ( op ) ;
93+ if ( this . _autoCompute ) {
94+ this . race ( ) ;
95+ }
96+ }
97+ } ) ;
98+ }
99+ }
100+
101+ stopRace ( ) {
102+ if ( this . _computation !== undefined ) {
103+ this . _computation . terminate ( ) ;
104+ this . _computation = undefined ;
105+ }
106+ }
107+
108+ private currentChallenge ( ) : string {
109+ if ( this . _lastOp === undefined ) {
110+ return this . getId ( ) as string ;
111+ } else {
112+ return Hashing . toHex ( this . _lastOp . hash ( ) ) ;
113+ }
114+ }
115+
116+ private currentSeq ( ) {
117+ if ( this . _lastOp === undefined ) {
118+ return 0 ;
119+ } else {
120+ return ( this . _lastOp . seq as number ) + 1 ;
121+ }
24122 }
25123
26124
27125 async mutate ( op : MutationOp , isNew : boolean ) : Promise < void > {
28126
127+ isNew ;
128+
29129 if ( op instanceof BeaconValueOp ) {
30130
31- if ( ! isNew && this . _lastOp !== undefined &&
131+ if ( this . _lastOp === undefined ||
32132 ! this . _lastOp . equals ( op ) ) {
33133
34134 if ( op . prevOps ?. size ( ) === 0 ) {
135+
35136 if ( this . _lastOp !== undefined ) {
36137 throw new Error ( 'Initial BeaconValueOp received, but there are already other ops in this beacon.' ) ;
37138 }
38139
39-
40140 } else {
41141 if ( this . _lastOp === undefined ) {
42142 throw new Error ( 'Non-initial BeaconValueOp received, but there are no values in this beacon.' ) ;
@@ -48,7 +148,14 @@ class Beacon extends MutableObject {
48148 }
49149
50150 this . _lastOp = op ;
151+
51152 this . _values . push ( Hashing . toHex ( op . hash ( ) ) ) ;
153+
154+ this . stopRace ( ) ;
155+
156+ if ( this . _autoCompute ) {
157+ this . race ( ) ;
158+ }
52159
53160 }
54161
@@ -58,7 +165,7 @@ class Beacon extends MutableObject {
58165 }
59166
60167 getClassName ( ) : string {
61- throw new Error ( 'Method not implemented.' ) ;
168+ return Beacon . className ;
62169 }
63170
64171 init ( ) : void {
0 commit comments