Skip to content

Commit

Permalink
Merge pull request #38 from ATOR-Development/36-add-hardware-relays-t…
Browse files Browse the repository at this point in the history
…o-distribution-contract

WIP: add hardware relays to distribution contract
  • Loading branch information
jim-toth authored Aug 1, 2024
2 parents 9a1eb6c + 20d672b commit 35f4370
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 20 deletions.
58 changes: 58 additions & 0 deletions src/distribution/distribution.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { DistributionData } from './schemas/distribution-data'
import { ScoreData } from './schemas/score-data'
import { Contract, LoggerFactory, Warp, WarpFactory } from 'warp-contracts'
import {
AddFingerprintsToBonus,
AddScores,
DistributionResult,
DistributionState,
Expand Down Expand Up @@ -46,6 +47,7 @@ export class DistributionService {
private static readonly scoresPerBatch = 8
public static readonly maxDistributionRetries = 6
private static readonly familiesPerBatch = 4
private static readonly fingerprintsPerBatch = 50

private distributionWarp: Warp
private distributionContract: Contract<DistributionState>
Expand Down Expand Up @@ -591,4 +593,60 @@ export class DistributionService {
relaysWithFamilyUpdates.map(relay => ({ relay, result: 'OK' }))
)
}

public async setHardwareBonusRelays(
relays: ValidatedRelay[]
): Promise<VerificationResults> {
const results: VerificationResults = []

if (!this.distributionContract) {
this.logger.error('Distribution contract not initialized')

return relays.map(relay => ({ relay, result: 'Failed' }))
}

if (!this.operator) {
this.logger.error('Distribution operator not defined')
}

if (this.isLive === 'true') {
try {
const batches = _.chunk(
relays,
DistributionService.fingerprintsPerBatch
)

for (const batch of batches) {
await setTimeout(5000)
this.logger.debug(
`Starting to set hardware bonus relays for ${batch.length} relays [${batch.map(r => r.fingerprint)}]`
)
const response = await this.distributionContract
.writeInteraction<AddFingerprintsToBonus>({
function: 'addFingerprintsToBonus',
bonusName: 'hardware',
fingerprints: batch.map(r => r.fingerprint)
})
this.logger.log(
`Set hardware bonus relays for ${batch.length} relays: ${response?.originalTxId}`
)
}
} catch (error) {
this.logger.error(
`Exception setting relay families for ${relays.length} relays [${relays.map(r => r.fingerprint)}]`,
error.stack,
)

return results.concat(
relays.map(relay => ({ relay, result: 'Failed' }))
)
}
} else {
this.logger.warn(
`NOT LIVE - skipped setting hardware bonus relays for ${relays.length} relays [${relays.map(r => r.fingerprint)}]`
)
}

return results.concat(relays.map(relay => ({ relay, result: 'OK' })))
}
}
39 changes: 29 additions & 10 deletions src/tasks/processors/verification-queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ export class VerificationQueue extends WorkerHost {
public static readonly JOB_RECOVER_PERSIST_VERIFICATION =
'recover-persist-verification'
public static readonly JOB_SET_RELAY_FAMILIES = 'set-relay-families'
public static readonly JOB_SET_HARDWARE_BONUS_RELAYS =
'set-hardware-bonus-relays'

constructor(
private readonly tasks: TasksService,
Expand Down Expand Up @@ -88,13 +90,32 @@ export class VerificationQueue extends WorkerHost {

return []

case VerificationQueue.JOB_SET_HARDWARE_BONUS_RELAYS:
try {
const {
['verify-relays']: verificationResults
} = await job.getChildrenValues<VerificationResults>()

const validatedHardwareRelays = verificationResults
.filter(({ relay }) => relay.hardware_validated)
.map(({ relay }) => relay)

return await this.distribution
.setHardwareBonusRelays(validatedHardwareRelays)
} catch (error) {
this.logger.error(
`Exception while setting hardware bonus relays`,
error
)
}

return []

case VerificationQueue.JOB_CONFIRM_VERIFICATION:
try {
const verificationResults: VerificationResults =
Object.values(await job.getChildrenValues()).reduce(
(prev, curr) => (prev as []).concat(curr as []),
[],
)
const {
['verify-relays']: verificationResults
} = await job.getChildrenValues<VerificationResults>()

if (verificationResults.length > 0) {
this.logger.debug(`Finalizing verification ${job.data}`)
Expand All @@ -115,11 +136,9 @@ export class VerificationQueue extends WorkerHost {

case VerificationQueue.JOB_PERSIST_VERIFICATION:
try {
const verificationResults: VerificationResults =
Object.values(await job.getChildrenValues()).reduce(
(prev, curr) => (prev as []).concat(curr as []),
[],
)
const {
['verify-relays']: verificationResults
} = await job.getChildrenValues<VerificationResults>()

if (verificationResults.length > 0) {
this.logger.log(
Expand Down
26 changes: 16 additions & 10 deletions src/tasks/tasks.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,16 +103,23 @@ export class TasksService implements OnApplicationBootstrap {
opts: TasksService.jobOpts,
children: [
{
name: 'verify-relays',
name: 'set-hardware-bonus-relays',
queueName: 'verification-queue',
opts: TasksService.jobOpts,
data: validation.relays
},
{
name: 'set-relay-families',
queueName: 'verification-queue',
opts: TasksService.jobOpts,
data: validation.relays
data: validation.relays,
children: [
{
name: 'verify-relays',
queueName: 'verification-queue',
opts: TasksService.jobOpts,
data: validation.relays
},
{
name: 'set-relay-families',
queueName: 'verification-queue',
opts: TasksService.jobOpts,
data: validation.relays
}
]
}
]
},
Expand All @@ -122,7 +129,6 @@ export class TasksService implements OnApplicationBootstrap {
}
}


public static DISTRIBUTION_FLOW(
stamp: number,
total: number,
Expand Down
6 changes: 6 additions & 0 deletions src/validation/schemas/validated-relay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ export class ValidatedRelay {

@Prop({ type: Object, required: false })
hardware_info?: RelayHardwareInfo

@Prop({ type: Boolean, required: false, default: false })
hardware_validated?: boolean

@Prop({ type: Number, required: false })
hardware_validated_at?: number
}

export const ValidatedRelaySchema = SchemaFactory.createForClass(ValidatedRelay)
2 changes: 2 additions & 0 deletions src/verification/verification.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,8 @@ export class VerificationService {
.hardwareVerificationService
.isHardwareProofValid(relay)
if (isHardwareProofValid) {
relay.hardware_validated = true
relay.hardware_validated_at = Date.now()
relaysToAddAsClaimable.push({relay, isHardwareProofValid })
} else {
results.push({ relay, result: 'HardwareProofFailed' })
Expand Down

0 comments on commit 35f4370

Please sign in to comment.