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 } ;
0 commit comments