@ -1,6 +1,8 @@
import { GetObjectCommand , S3Client } from '@aws-sdk/client-s3' ;
import { GetObjectCommand , S3Client } from '@aws-sdk/client-s3' ;
import yargs from 'yargs' ;
import yargs from 'yargs' ;
import { mean , median , stdDev , streamToString } from '../src/utils/utils' ;
const MAX_MISSING_CHECKPOINTS = 10 ;
const MAX_MISSING_CHECKPOINTS = 10 ;
interface Checkpoint {
interface Checkpoint {
@ -48,22 +50,6 @@ function isValidHashStr(s: string): boolean {
return ! ! s . match ( /^0x[0-9a-f]{1,64}$/im ) ;
return ! ! s . match ( /^0x[0-9a-f]{1,64}$/im ) ;
}
}
function getArgs() {
return yargs ( process . argv . slice ( 2 ) )
. alias ( 'a' , 'address' )
. describe ( 'a' , 'address of the validator to inspect' )
. demandOption ( 'a' )
. string ( 'a' )
. alias ( 'p' , 'prospective' )
. describe ( 'p' , 'S3 bucket of the prospective validator' )
. demandOption ( 'p' )
. string ( 'p' )
. alias ( 'c' , 'control' )
. describe ( 'c' , 'S3 bucket of the the known (control) validator' )
. demandOption ( 'c' )
. string ( 'c' ) . argv ;
}
class S3Wrapper {
class S3Wrapper {
private readonly client : S3Client ;
private readonly client : S3Client ;
readonly region : string ;
readonly region : string ;
@ -104,201 +90,304 @@ class S3Wrapper {
}
}
}
}
async function main() {
class Validator {
const {
private readonly controlS3BucketClientWrapper : S3Wrapper ;
a : _validatorAddress ,
private readonly prospectiveS3BucketClientWrapper : S3Wrapper ;
p : prospectiveBucket ,
c : controlBucket ,
} = await getArgs ( ) ;
const cClient = new S3Wrapper ( controlBucket ) ;
// accumulators for stats
const pClient = new S3Wrapper ( prospectiveBucket ) ;
/** Checkpoints the prospective validator has that the control validator does not */
private extraCheckpoints ! : number [ ] ;
/** Checkpoints the prospective validator does not have that the control validator does have */
private missingCheckpoints ! : number [ ] ;
/** Checkpoints the prospective validator has but for which we detected an issue */
private invalidCheckpoints ! : number [ ] ;
/ * * T h e d i f f e r e n c e i n m o d i f i c a t i o n t i m e s o n t h e s 3 o b j e c t s b e t w e e n t h e c o n t r o l a n d v a l i d a t o r
* buckets . ( validator time - control time ) .
* /
private modTimeDeltasS ! : number [ ] ;
/** The checkpoints which were, as far as this validation logic is concerned, present and valid */
private validCheckpoints ! : number [ ] ;
/ * * T h e n u m b e r o f c h e c k p o i n t s t h a t t h e c o n t r o l h a d t h a t t h e v a l i d a t o r d i d n o t h a v e i n a r o w .
* ( Not necessarily consecutive indexes ) * /
private missingInARow ! : number ;
/** Index of the last index we found an entry for from the prospective validator */
private lastNonMissingCheckpointIndex ! : number ;
const [ { obj : controlLatestCheckpoint } , { obj : prospectiveLastCheckpoint } ] =
constructor (
await Promise . all ( [
public readonly validatorAddress : string ,
cClient . getS3Obj < number > ( 'checkpoint_latest_index.json' ) . catch ( ( err ) = > {
public readonly controlS3BucketAddress : string ,
console . error (
public readonly prospectiveS3BucketAddress : string ,
"Failed to get control validator's latest checkpoint." ,
) {
err ,
this . controlS3BucketClientWrapper = new S3Wrapper (
) ;
this . controlS3BucketAddress ,
process . exit ( 1 ) ;
) ;
} ) ,
this . prospectiveS3BucketClientWrapper = new S3Wrapper (
pClient . getS3Obj < number > ( 'checkpoint_latest_index.json' ) . catch ( ( err ) = > {
this . prospectiveS3BucketAddress ,
console . error (
) ;
"Failed to get prospective validator's latest checkpoint." ,
}
err ,
) ;
process . exit ( 1 ) ;
} ) ,
] ) ;
if (
initStatsState() {
! isLatestCheckpoint ( controlLatestCheckpoint ) ||
this . extraCheckpoints = [ ] ;
! isLatestCheckpoint ( prospectiveLastCheckpoint )
this . missingCheckpoints = [ ] ;
)
this . invalidCheckpoints = [ ] ;
process . exit ( 1 ) ;
this . modTimeDeltasS = [ ] ;
this . validCheckpoints = [ ] ;
this . missingInARow = 0 ;
this . lastNonMissingCheckpointIndex = Infinity ;
}
console . log ( ` Latest Index ` ) ;
/ * *
console . log ( ` control: ${ controlLatestCheckpoint } ` ) ;
* Validate that the control and prospective validators are in agreement . Will throw an error on
console . log ( ` prospective: ${ prospectiveLastCheckpoint } \ n ` ) ;
* any critical failures and will log stats to the console as it goes .
*
let extraCheckpoints = [ ] ;
* If we want to make this callable from outside the script later I would suggest making this
const missingCheckpoints = [ ] ;
* return a stats object or something .
let invalidCheckpoints = [ ] ;
* /
const modTimeDeltasS = [ ] ;
async validate ( ) : Promise < void > {
const fullyCorrectCheckpoints = [ ] ;
this . initStatsState ( ) ;
let missingInARow = 0 ;
let lastNonMissingCheckpointIndex = Infinity ;
for (
let i = Math . max ( controlLatestCheckpoint , prospectiveLastCheckpoint ) ;
i >= 0 ;
-- i
) {
if ( missingInARow == MAX_MISSING_CHECKPOINTS ) {
missingCheckpoints . length -= MAX_MISSING_CHECKPOINTS ;
invalidCheckpoints = invalidCheckpoints . filter (
( j ) = > j < lastNonMissingCheckpointIndex ,
) ;
extraCheckpoints = extraCheckpoints . filter (
( j ) = > j < lastNonMissingCheckpointIndex ,
) ;
break ;
}
const key = ` checkpoint_ ${ i } .json ` ;
const { controlLatestCheckpoint , prospectiveLastCheckpoint } =
await this . getLatestCheckpoints ( ) ;
let control : Checkpoint | null ;
const maxCheckpointIndex = Math . max (
let controlLastMod : Date | null ;
controlLatestCheckpoint ,
try {
prospectiveLastCheckpoint ,
const t = await cClient . getS3Obj ( key ) ;
) ;
if ( isCheckpoint ( t . obj ) ) {
for (
if ( t . obj . checkpoint . index != i ) {
let checkpointIndex = maxCheckpointIndex ;
console . log ( ` ${ i } : Control index is invalid ` , t ) ;
checkpointIndex >= 0 ;
process . exit ( 1 ) ;
-- checkpointIndex
}
) {
[ control , controlLastMod ] = [ t . obj , t . modified ] ;
if ( this . missingInARow == MAX_MISSING_CHECKPOINTS ) {
} else {
this . missingCheckpoints . length -= MAX_MISSING_CHECKPOINTS ;
console . log ( ` ${ i } : Invalid control checkpoint ` , t ) ;
this . extraCheckpoints = this . extraCheckpoints . filter (
process . exit ( 1 ) ;
( j ) = > j < this . lastNonMissingCheckpointIndex ,
) ;
break ;
}
}
} catch ( err ) {
control = controlLastMod = null ;
await this . validateCheckpointIndex ( checkpointIndex ) ;
}
}
let prospective : Checkpoint ;
console . log (
let prospectiveLastMod : Date ;
` Fully correct checkpoints ( ${ this . validCheckpoints . length } ): ${ this . validCheckpoints } \ n ` ,
try {
) ;
const t = await pClient . getS3Obj ( key ) ;
if ( this . extraCheckpoints . length )
if ( isCheckpoint ( t . obj ) ) {
console . log (
[ prospective , prospectiveLastMod ] = [ t . obj , t . modified ] ;
` Extra checkpoints ( ${ this . extraCheckpoints . length } ): ${ this . extraCheckpoints } \ n ` ,
lastNonMissingCheckpointIndex = i ;
) ;
} else {
if ( this . missingCheckpoints . length )
console . log ( ` ${ i } : Invalid prospective checkpoint ` , t . obj ) ;
console . log (
invalidCheckpoints . push ( i ) ;
` Missing checkpoints ( ${ this . missingCheckpoints . length } ): ${ this . missingCheckpoints } \ n ` ,
continue ;
) ;
}
if ( this . invalidCheckpoints . length )
if ( ! control ) {
console . log (
extraCheckpoints . push ( i ) ;
` Invalid checkpoints ( ${ this . invalidCheckpoints . length } ): ${ this . invalidCheckpoints } \ n ` ,
}
) ;
missingInARow = 0 ;
} catch ( err ) {
if ( this . modTimeDeltasS . length > 1 ) {
if ( control ) {
// Drop the time of the first one since it is probably way off
missingCheckpoints . push ( i ) ;
this . modTimeDeltasS . length -- ;
missingInARow ++ ;
console . log (
}
` Time deltas (∆ < 0 -> prospective came earlier than the control) ` ,
continue ;
) ;
console . log ( this . modTimeDeltasS ) ;
console . log ( ` Median: ${ median ( this . modTimeDeltasS ) } s ` ) ;
console . log ( ` Mean: ${ mean ( this . modTimeDeltasS ) } s ` ) ;
console . log ( ` Stdev: ${ stdDev ( this . modTimeDeltasS ) } s ` ) ;
}
}
}
private async validateCheckpointIndex (
checkpointIndex : number ,
) : Promise < void > {
const { control , controlLastMod } = ( await this . getControlCheckpoint (
checkpointIndex ,
) ) ? ? { control : null , controlLastMod : null } ;
const getProspectiveCheckpointResult = await this . getProspectiveCheckpoint (
checkpointIndex ,
! ! control ,
) ;
if ( ! getProspectiveCheckpointResult ) return ;
const { prospective , prospectiveLastMod } = getProspectiveCheckpointResult ;
console . assert (
console . assert (
prospective . checkpoint . index == i ,
prospective . checkpoint . index == checkpo intIndex ,
` ${ i } : checkpoint indexes do not match ` ,
` ${ checkpo intIndex } : checkpoint indexes do not match ` ,
) ;
) ;
// TODO: verify signature
// TODO: verify signature
if ( ! control ) {
if ( ! control ) {
continue ;
return ;
}
}
// compare against the control
// compare against the control
console . assert (
console . assert (
control . checkpoint . outbox_domain == prospective . checkpoint . outbox_domain ,
control . checkpoint . outbox_domain == prospective . checkpoint . outbox_domain ,
` ${ i } : outbox_domains do not match ` ,
` ${ checkpo intIndex } : outbox_domains do not match ` ,
) ;
) ;
console . assert (
console . assert (
control . checkpoint . root == prospective . checkpoint . root ,
control . checkpoint . root == prospective . checkpoint . root ,
` ${ i } : checkpoint roots do not match ` ,
` ${ checkpo intIndex } : checkpoint roots do not match ` ,
) ;
) ;
const diffS =
const diffS =
( prospectiveLastMod . valueOf ( ) - controlLastMod ! . valueOf ( ) ) / 1000 ;
( prospectiveLastMod . valueOf ( ) - controlLastMod ! . valueOf ( ) ) / 1000 ;
if ( Math . abs ( diffS ) > 10 ) {
if ( Math . abs ( diffS ) > 10 ) {
console . log ( ` ${ i } : Modification times differ by ${ diffS } s ` ) ;
console . log ( ` ${ checkpo intIndex } : Modification times differ by ${ diffS } s ` ) ;
}
}
modTimeDeltasS . push ( diffS ) ;
this . modTimeDeltasS . push ( diffS ) ;
fullyCorrect Checkpoints. push ( i ) ;
this . valid Checkpoints. push ( checkpo intIndex ) ;
}
}
console . log (
private async getLatestCheckpoints ( ) : Promise < {
` Fully correct checkpoints ( ${ fullyCorrectCheckpoints . length } ): ${ fullyCorrectCheckpoints } \ n ` ,
controlLatestCheckpoint : number ;
) ;
prospectiveLastCheckpoint : number ;
if ( extraCheckpoints . length )
} > {
console . log (
const [
` Extra checkpoints ( ${ extraCheckpoints . length } ): ${ extraCheckpoints } \ n ` ,
{ obj : controlLatestCheckpoint } ,
) ;
{ obj : prospectiveLastCheckpoint } ,
if ( missingCheckpoints . length )
] = await Promise . all ( [
console . log (
this . controlS3BucketClientWrapper
` Missing checkpoints ( ${ missingCheckpoints . length } ): ${ missingCheckpoints } \ n ` ,
. getS3Obj < number > ( 'checkpoint_latest_index.json' )
) ;
. catch ( ( err ) = > {
if ( invalidCheckpoints . length )
console . error (
console . log (
"Failed to get control validator's latest checkpoint." ,
` Invalid checkpoints ( ${ invalidCheckpoints . length } ): ${ invalidCheckpoints } \ n ` ,
err ,
) ;
) ;
process . exit ( 1 ) ;
} ) ,
this . prospectiveS3BucketClientWrapper
. getS3Obj < number > ( 'checkpoint_latest_index.json' )
. catch ( ( err ) = > {
console . error (
"Failed to get prospective validator's latest checkpoint." ,
err ,
) ;
process . exit ( 1 ) ;
} ) ,
] ) ;
if ( modTimeDeltasS . length > 1 ) {
if (
// Drop the time of the first one since it is probably way off
! isLatestCheckpoint ( controlLatestCheckpoint ) ||
modTimeDeltasS . length -- ;
! isLatestCheckpoint ( prospectiveLastCheckpoint )
console . log (
)
` Time deltas (∆ < 0 -> prospective came earlier than the control) ` ,
throw new Error ( 'Invalid latest checkpoint data' ) ;
) ;
console . log ( modTimeDeltasS ) ;
console . log ( ` Latest Index ` ) ;
console . log ( ` Median: ${ median ( modTimeDeltasS ) } s ` ) ;
console . log ( ` control: ${ controlLatestCheckpoint } ` ) ;
console . log ( ` Mean: ${ mean ( modTimeDeltasS ) } s ` ) ;
console . log ( ` prospective: ${ prospectiveLastCheckpoint } \ n ` ) ;
console . log ( ` Stdev: ${ stdDev ( modTimeDeltasS ) } s ` ) ;
return { controlLatestCheckpoint , prospectiveLastCheckpoint } ;
}
}
}
function median ( a : number [ ] ) : number {
private async getControlCheckpoint (
a = [ . . . a ] ; // clone
checkpointIndex : number ,
a . sort ( ( a , b ) = > a - b ) ;
) : Promise < { control : Checkpoint ; controlLastMod : Date } | null > {
if ( a . length <= 0 ) {
let control : Checkpoint , controlLastMod : Date , unrecoverableError ;
return 0 ;
try {
} else if ( a . length % 2 == 0 ) {
const s3Object = await this . controlS3BucketClientWrapper . getS3Obj (
return ( a [ a . length / 2 ] + a [ a . length / 2 - 1 ] ) / 2 ;
this . checkpointKey ( checkpointIndex ) ,
} else {
) ;
return a [ ( a . length - 1 ) / 2 ] ;
if ( isCheckpoint ( s3Object . obj ) ) {
if ( s3Object . obj . checkpoint . index != checkpointIndex ) {
console . log ( ` ${ checkpointIndex } : Control index is invalid ` , s3Object ) ;
process . exit ( 1 ) ;
}
[ control , controlLastMod ] = [ s3Object . obj , s3Object . modified ] ;
} else {
console . log ( ` ${ checkpointIndex } : Invalid control checkpoint ` , s3Object ) ;
unrecoverableError = new Error ( 'Invalid control checkpoint.' ) ;
}
} catch ( err ) {
return null ;
}
if ( unrecoverableError ) throw unrecoverableError ;
return { control : control ! , controlLastMod : controlLastMod ! } ;
}
private async getProspectiveCheckpoint (
checkpointIndex : number ,
controlFoundForIndex : boolean ,
) : Promise < { prospective : Checkpoint ; prospectiveLastMod : Date } | null > {
let prospective : Checkpoint , prospectiveLastMod : Date ;
try {
const s3Object = await this . prospectiveS3BucketClientWrapper . getS3Obj (
this . checkpointKey ( checkpointIndex ) ,
) ;
if ( isCheckpoint ( s3Object . obj ) ) {
[ prospective , prospectiveLastMod ] = [ s3Object . obj , s3Object . modified ] ;
this . lastNonMissingCheckpointIndex = checkpointIndex ;
} else {
console . log (
` ${ checkpointIndex } : Invalid prospective checkpoint ` ,
s3Object . obj ,
) ;
this . invalidCheckpoints . push ( checkpointIndex ) ;
return null ;
}
if ( ! controlFoundForIndex ) {
this . extraCheckpoints . push ( checkpointIndex ) ;
}
this . missingInARow = 0 ;
} catch ( err ) {
if ( controlFoundForIndex ) {
this . missingCheckpoints . push ( checkpointIndex ) ;
this . missingInARow ++ ;
}
return null ;
}
return {
prospective : prospective ! ,
prospectiveLastMod : prospectiveLastMod ! ,
} ;
}
private checkpointKey ( checkpointIndex : number ) : string {
return ` checkpoint_ ${ checkpointIndex } .json ` ;
}
}
}
}
function mean ( a : number [ ] ) : number {
////
return a . reduce ( ( acc , i ) = > acc + i , 0 ) / a . length ;
// Bootstrapper
////
function getArgs() {
return yargs ( process . argv . slice ( 2 ) )
. alias ( 'a' , 'address' )
. describe ( 'a' , 'address of the validator to inspect' )
. demandOption ( 'a' )
. string ( 'a' )
. alias ( 'p' , 'prospective' )
. describe ( 'p' , 'S3 bucket of the prospective validator' )
. demandOption ( 'p' )
. string ( 'p' )
. alias ( 'c' , 'control' )
. describe ( 'c' , 'S3 bucket of the the known (control) validator' )
. demandOption ( 'c' )
. string ( 'c' ) . argv ;
}
}
function stdDev ( a : number [ ] ) : number {
async function main() {
return Math . sqrt (
const {
a . map ( ( i ) = > i * i ) . reduce ( ( acc , i ) = > acc + i , 0 ) / a . length ,
a : validatorAddress ,
p : prospectiveBucket ,
c : controlBucket ,
} = await getArgs ( ) ;
const validator = new Validator (
validatorAddress ,
prospectiveBucket ,
controlBucket ,
) ;
) ;
}
function streamToString ( stream : NodeJS.ReadableStream ) : Promise < string > {
try {
return new Promise ( ( resolve , reject ) = > {
await validator . validate ( ) ;
const chunks : string [ ] = [ ] ;
} catch ( err ) {
stream
console . error ( err ) ;
. setEncoding ( 'utf8' )
process . exit ( 1 ) ;
. on ( 'data' , ( chunk ) = > chunks . push ( chunk ) )
}
. on ( 'error' , ( err ) = > reject ( err ) )
. on ( 'end' , ( ) = > resolve ( String . prototype . concat ( . . . chunks ) ) ) ;
} ) ;
}
}
main ( ) . catch ( console . error ) ;
main ( ) . catch ( console . error ) ;