Skip to content

Commit

Permalink
add abort controller to attest_storage_challenge and update is_valid_…
Browse files Browse the repository at this point in the history
…report fn logic on chain
  • Loading branch information
ninabreznik committed May 21, 2024
1 parent e8ed93f commit d5ea549
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 22 deletions.
15 changes: 9 additions & 6 deletions src/node_modules/datdot-node-javascript-internal/chain.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 11 additions & 7 deletions src/roles/attester.js
Original file line number Diff line number Diff line change
Expand Up @@ -332,12 +332,15 @@ async function storageChallenge_handler (args) {
const attestation = { response: { storageChallengeID, status: undefined, reports: [] }, signer }
const pubkey = account.noisePublicKey.toString('hex')
var conn // set to true once connection with hoster is established
const controller = new AbortController();
const signal = controller.signal

const data = await get_storage_challenge_data({ chainAPI, storageChallenge, log })


const tid = setTimeout(async () => {
log({ type: 'timeout', data: { texts: 'storage challenge - timeout', storageChallengeID } })
controller.abort()
const { hosterkey, feedkey_1 } = data
const topic = derive_topic({ senderKey: hosterkey, feedkey: feedkey_1, receiverKey: attesterkey, id: storageChallengeID, log })
done_task_cleanup({ role: 'storage_attester', topic, remotestringkey: hosterkey.toString('hex'), state: account.state[pubkey], log })
Expand All @@ -348,7 +351,8 @@ async function storageChallenge_handler (args) {
return
}, DEFAULT_TIMEOUT)

const res = await attest_storage_challenge({ data, chainAPI, account, conn, log }).catch(async err => {
const res = await attest_storage_challenge({ data, chainAPI, account, conn, signal, log }).catch(async err => {
if (signal.aborted) return log({ type: 'storage challenge', data: { text: 'attest storage aborted', storageChallengeID }})
log({ type: 'storage challenge', data: { text: 'error: attest storage', storageChallengeID }})
attestation.nonce = await account.getNonce()
if (err.cause === 'invalid-proof' || err.cause === 'no-data') {
Expand All @@ -367,18 +371,17 @@ async function storageChallenge_handler (args) {
attestation.response.proof_of_contact = proof_of_contact
attestation.response.reports = reports
await chainAPI.submitStorageChallenge(attestation)
} else {
log({ type: 'storage challenge', data: { text: 'error: no response', storageChallengeID }})
attestation.nonce = await account.getNonce()
await chainAPI.submitStorageChallenge(attestation)
}
}
}
// connect to the hoster (get the feedkey, then start getting data)
// make a list of all checks (all feedkeys, amendmentIDs...)
// for each check, get data, verify and make a report => push report from each check into report_all
// when all checks are done, report to chain
async function attest_storage_challenge ({ data, chainAPI, account, conn, log: parent_log }) {
async function attest_storage_challenge ({ data, chainAPI, account, conn, signal, log: parent_log }) {
return new Promise(async (resolve, reject) => {
signal.addEventListener("abort", () => {
reject(signal.reason);
});
const { hyper } = account
const { id, attesterkey, hosterkey, hosterSigningKey, checks, feedkey_1 } = data
const pubkey = account.noisePublicKey.toString('hex')
Expand Down Expand Up @@ -1044,6 +1047,7 @@ async function hosterReplacement_handler (args) {
})

async function onhoster ({ feed, remotestringkey }) {
if (conn[remotestringkey]) return
conn[remotestringkey] = 'connected'
hosterFeed = feed
log({ type: 'attester', data: { text: `Connected to the replacement hoster`, remotestringkey, amendmentID } })
Expand Down
12 changes: 3 additions & 9 deletions src/roles/hoster.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,18 +145,13 @@ async function handle_storageChallenge (args) {
const hosterAddress = await chainAPI.getUserAddress(hosterID)
if (hosterAddress !== myAddress) return
log({ type: 'hoster', data: { text: `Hoster ${hosterID}: Event received: ${event.method} ${event.data.toString()}` } })
const controller = new AbortController()
const tid = setTimeout(() => {
log({ type: 'timeout', data: { texts: 'storage challenge - timeout', id } })
return
}, DEFAULT_TIMEOUT)

const data = await get_storage_challenge_data(storageChallenge)
data.tid = tid

await send_storage_proofs_to_attester({ data, account, log }).catch(err => {
log({ type: 'storage challenge', data: { text: 'error: provide storage proof', id }})
})
clearTimeout(tid)

log({ type: 'hoster', data: { text: `sendStorageChallengeToAttester completed` } })

async function get_storage_challenge_data (storageChallenge) {
Expand Down Expand Up @@ -188,7 +183,7 @@ async function handle_storageChallenge (args) {
async function send_storage_proofs_to_attester({ data, account, log: parent_log }) {
return new Promise(async (resolve, reject) => {
const { hyper } = account
const { challenge_id, attesterkey, hosterkey, checks, feedkey_1, tid } = data
const { challenge_id, attesterkey, hosterkey, checks, feedkey_1 } = data

const log = parent_log.sub(`<-hoster2attester storage challenge, me: ${account.noisePublicKey.toString('hex').substring(0,5)} peer: ${attesterkey.toString('hex').substring(0, 5)} `)

Expand Down Expand Up @@ -235,7 +230,6 @@ async function send_storage_proofs_to_attester({ data, account, log: parent_log
resolve()
} catch (err) {
log({ type: 'error', data: { text: `Error: ${err}` } })
clearTimeout(tid)
reject(err)
}
}
Expand Down

0 comments on commit d5ea549

Please sign in to comment.