Skip to content

Commit

Permalink
Adds View-State method to Operator Registry. Cleans up claimable fing…
Browse files Browse the repository at this point in the history
…erprints when claimed by operator address.
  • Loading branch information
jim-toth committed Nov 15, 2024
1 parent 871faf2 commit 336a8c5
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
22 changes: 22 additions & 0 deletions ao/src/contracts/operator-registry.lua
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ function OperatorRegistry.init()
.VerifiedFingerprintsToOperatorAddresses[fingerprint] = address
OperatorRegistry
.RegistrationCreditsFingerprintsToOperatorAddresses[fingerprint] = nil
OperatorRegistry
.ClaimableFingerprintsToOperatorAddresses[fingerprint] = nil

ao.send({
Target = msg.From,
Expand Down Expand Up @@ -376,6 +378,24 @@ function OperatorRegistry.init()
end
)

Handlers.add(
'View-State',
Handlers.utils.hasMatchingTag('Action', 'View-State'),
function (msg)
ao.send({
Target = msg.From,
Action = 'View-State-Response',
Data = json.encode({
ClaimableFingerprintsToOperatorAddresses = OperatorRegistry.ClaimableFingerprintsToOperatorAddresses,
VerifiedFingerprintsToOperatorAddresses = OperatorRegistry.VerifiedFingerprintsToOperatorAddresses,
BlockedOperatorAddresses = OperatorRegistry.BlockedOperatorAddresses,
RegistrationCreditsFingerprintsToOperatorAddresses = OperatorRegistry.RegistrationCreditsFingerprintsToOperatorAddresses,
VerifiedHardwareFingerprints = OperatorRegistry.VerifiedHardwareFingerprints
})
})
end
)

Handlers.add(
'Info',
Handlers.utils.hasMatchingTag('Action', 'Info'),
Expand All @@ -398,6 +418,8 @@ function OperatorRegistry.init()
info.total = info.total + 1
end

info.total = info.total + info.claimed

for _ in pairs(OperatorRegistry.VerifiedHardwareFingerprints) do
info.hardware = info.hardware + 1
end
Expand Down
66 changes: 66 additions & 0 deletions ao/test/spec/contracts/operator-registry.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1406,4 +1406,70 @@ describe('Operator Registry', () => {
.to.deep.equal({ claimed: 3, hardware: 2, total: 6 })
})
})

describe('View State', () => {
it('Provides reply to View-State messages', async () => {
const verifiedRelays = [
{ address: ALICE_ADDRESS, fingerprint: FINGERPRINT_A },
{ address: BOB_ADDRESS, fingerprint: FINGERPRINT_B },
{ address: CHARLS_ADDRESS, fingerprint: FINGERPRINT_C }
]
for (const { address, fingerprint } of verifiedRelays) {
await setupAdminAddOperatorCertificates(handle, address, fingerprint)
await addRegistrationCredit(handle, address, fingerprint)
await handle({
From: address,
Tags: [
{ name: 'Action', value: 'Submit-Fingerprint-Certificate' },
{
name: 'Fingerprint-Certificate',
value: fingerprint
}
]
})
}
const unclaimedRelays = [
{ address: ALICE_ADDRESS, fingerprint: FINGERPRINT_D },
{ address: BOB_ADDRESS, fingerprint: FINGERPRINT_E },
{ address: CHARLS_ADDRESS, fingerprint: FINGERPRINT_F },
]
for (const { address, fingerprint } of unclaimedRelays) {
await setupAdminAddOperatorCertificates(handle, address, fingerprint)
}

await handle({
From: OWNER_ADDRESS,
Tags: [
{ name: 'Action', value: 'Add-Verified-Hardware' }
],
Data: `${FINGERPRINT_B},${FINGERPRINT_E}`
})

const result = await handle({
From: ALICE_ADDRESS,
Tags: [{ name: 'Action', value: 'View-State' }]
})

const parsed = JSON.parse(result.Messages[0].Data)
expect(result.Messages).to.have.lengthOf(1)
expect(parsed).to.deep.equal({
ClaimableFingerprintsToOperatorAddresses: {
[FINGERPRINT_D]: ALICE_ADDRESS,
[FINGERPRINT_E]: BOB_ADDRESS,
[FINGERPRINT_F]: CHARLS_ADDRESS
},
VerifiedFingerprintsToOperatorAddresses: {
[FINGERPRINT_A]: ALICE_ADDRESS,
[FINGERPRINT_B]: BOB_ADDRESS,
[FINGERPRINT_C]: CHARLS_ADDRESS
},
BlockedOperatorAddresses: [],
RegistrationCreditsFingerprintsToOperatorAddresses: [],
VerifiedHardwareFingerprints: {
[FINGERPRINT_B]: true,
[FINGERPRINT_E]: true
}
})
})
})
})

0 comments on commit 336a8c5

Please sign in to comment.