1
1
/* Imports: External */
2
- import { Contract , Wallet , BigNumber , providers } from 'ethers'
2
+ import { BigNumber , Contract , Wallet , providers } from 'ethers'
3
3
import fs from "fs" ;
4
4
import { promisify } from "util" ;
5
5
import { exec } from "child_process" ;
@@ -57,6 +57,7 @@ const emptyAddress = '0x0000000000000000000000000000000000000000'
57
57
const bytes32Zero = "0x" + "0" . repeat ( 64 ) ;
58
58
const gasLimitLow = 500000
59
59
const gasLimitHigh = 3000000
60
+ const zero = BigNumber . from ( 0 ) ;
60
61
61
62
export class AdminZkRandService extends BaseService < AdminZkRandOptions > {
62
63
constructor ( options : AdminZkRandOptions ) {
@@ -110,16 +111,15 @@ export class AdminZkRandService extends BaseService<AdminZkRandOptions> {
110
111
async _start ( ) : Promise < void > {
111
112
console . log ( '\n------------------------------ admin starts ------------------------------' )
112
113
113
- let adminFromContract = await this . state . zkRandContract . owner ( )
114
+ const adminFromContract = await this . state . zkRandContract . owner ( )
114
115
console . log ( "admin in contract:" , adminFromContract )
115
116
if ( adminFromContract !== this . options . l2Wallet . address ) {
116
117
throw new Error (
117
118
`ADMIN_PRIVATE_KEY is not set to zkRand admin ${ adminFromContract } `
118
119
)
119
120
}
120
121
121
-
122
- let currentIndexFromContract = await this . state . zkRandContract . currentIndex ( )
122
+ const currentIndexFromContract = await this . state . zkRandContract . currentIndex ( )
123
123
console . log ( "currentIndexFromContract" , currentIndexFromContract )
124
124
125
125
if ( currentIndexFromContract != this . options . numberMembers ) {
@@ -163,7 +163,7 @@ export class AdminZkRandService extends BaseService<AdminZkRandOptions> {
163
163
164
164
while ( this . running ) {
165
165
try {
166
- let contractPhase = await this . state . zkRandContract . contractPhase ( )
166
+ const contractPhase = await this . state . zkRandContract . contractPhase ( )
167
167
console . log ( "contractPhase" , contractPhase )
168
168
if ( contractPhase == Status . Registered ) {
169
169
// all the nodes have registered; start nidkg
@@ -174,29 +174,29 @@ export class AdminZkRandService extends BaseService<AdminZkRandOptions> {
174
174
// nidkg has completed; calculate global public parameters
175
175
await this . createGpp ( )
176
176
} else if ( contractPhase == Status . Ready ) {
177
- let currentRoundNum = await this . state . zkRandContract . currentRoundNum ( )
177
+ const currentRoundNum : BigNumber = await this . state . zkRandContract . currentRoundNum ( )
178
178
console . log ( "currentRoundNum" , currentRoundNum . toString ( ) )
179
179
if ( Date . now ( ) < this . state . startDate ) {
180
180
const begin = new Date ( this . state . startDate ) ;
181
181
console . log ( "randomness generation will begin at" , begin . toUTCString ( ) )
182
182
} else {
183
- if ( currentRoundNum == 0 ) {
183
+ if ( currentRoundNum . eq ( zero ) ) {
184
184
// random generation starts from 1
185
185
await this . initiateRand ( )
186
186
} else {
187
- let submissionCount = await this . state . zkRandContract . roundSubmissionCount ( currentRoundNum )
188
- let roundToRandom = await this . state . zkRandContract . roundToRandom ( currentRoundNum )
187
+ const submissionCount = await this . state . zkRandContract . roundSubmissionCount ( currentRoundNum )
188
+ const roundToRandom = await this . state . zkRandContract . roundToRandom ( currentRoundNum )
189
189
190
190
if ( roundToRandom . value === bytes32Zero && submissionCount >= this . options . threshold ) {
191
191
await this . createRandom ( currentRoundNum )
192
192
}
193
193
194
- let secondsElapsed = Math . floor (
194
+ const secondsElapsed = Math . floor (
195
195
( Date . now ( ) - this . state . timeOfLastRound ) / 1000
196
196
)
197
197
console . log ( 'Seconds elapsed since last random initiation:' , secondsElapsed )
198
198
199
- if ( secondsElapsed > this . options . randGenInterval ) {
199
+ if ( secondsElapsed > this . options . randGenInterval && roundToRandom . value !== bytes32Zero ) {
200
200
await this . initiateRand ( ) ;
201
201
}
202
202
}
@@ -276,13 +276,13 @@ export class AdminZkRandService extends BaseService<AdminZkRandOptions> {
276
276
}
277
277
278
278
async check_config ( ) {
279
- let threshold = await this . state . zkRandContract . threshold ( )
279
+ const threshold = await this . state . zkRandContract . threshold ( )
280
280
if ( threshold != this . options . threshold ) {
281
281
throw new Error (
282
282
`threshold=${ this . options . threshold } does not match threshold=${ threshold } from contract`
283
283
)
284
284
}
285
- let memberCountFromContract = await this . state . zkRandContract . memberCount ( )
285
+ const memberCountFromContract = await this . state . zkRandContract . memberCount ( )
286
286
if ( memberCountFromContract != this . options . numberMembers ) {
287
287
throw new Error (
288
288
`number_of_members=${ this . options . numberMembers } does not match number_of_members=${ memberCountFromContract } from contract`
@@ -308,7 +308,7 @@ export class AdminZkRandService extends BaseService<AdminZkRandOptions> {
308
308
// derive global public parameters
309
309
const cmd = `${ this . cmdPrefix } dkg derive`
310
310
console . log ( "running command <" , cmd , ">..." )
311
- let result = await execPromise ( cmd )
311
+ const result = await execPromise ( cmd )
312
312
console . log ( result [ `stderr` ] )
313
313
314
314
const filePath = dkgDir + "gpk.json"
@@ -355,7 +355,7 @@ export class AdminZkRandService extends BaseService<AdminZkRandOptions> {
355
355
this . state . zkRandContract . on ( eventRandThreshold , async ( roundNum , input , event ) => {
356
356
console . log ( "\nevent" , eventRandThreshold , `round ${ roundNum } input "${ input } "` )
357
357
358
- let memberCountFromContract = await this . state . zkRandContract . memberCount ( )
358
+ const memberCountFromContract = await this . state . zkRandContract . memberCount ( )
359
359
const evals : Eval [ ] = [ ]
360
360
for ( let i = 0 ; i < memberCountFromContract ; i ++ ) {
361
361
const evalFromContract = await this . state . zkRandContract . roundToEval ( roundNum , i )
@@ -408,9 +408,9 @@ export class AdminZkRandService extends BaseService<AdminZkRandOptions> {
408
408
} ) ;
409
409
}
410
410
411
- async createRandom ( roundNum : number ) {
412
- let memberCountFromContract = await this . state . zkRandContract . memberCount ( )
413
- let input = await this . state . zkRandContract . roundInput ( roundNum )
411
+ async createRandom ( roundNum : BigNumber ) {
412
+ const memberCountFromContract = await this . state . zkRandContract . memberCount ( )
413
+ const input = await this . state . zkRandContract . roundInput ( roundNum )
414
414
415
415
const evals : Eval [ ] = [ ]
416
416
for ( let i = 0 ; i < memberCountFromContract ; i ++ ) {
0 commit comments