Skip to content

Commit

Permalink
Merge pull request #378 from permaweb/tillathehun0/weavedrive-bootloa…
Browse files Browse the repository at this point in the history
…der-tx

fix(weavedrive): correctly check and load On-Boot tag #342
  • Loading branch information
TillaTheHun0 authored Oct 18, 2024
2 parents 4a68ac0 + 11fb5ea commit c4113b6
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 20 deletions.
35 changes: 25 additions & 10 deletions extensions/weavedrive/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ module.exports = function weaveDrive(mod, FS) {

// Check if we are attempting to load the On-Boot id, if so allow it
// this was added for AOP 6 Boot loader See: https://github.com/permaweb/aos/issues/342
const bootTag = mod.spawn.tags['On-Boot'];
const bootTag = this.getTagValue('On-Boot', mod.spawn.tags)
if (bootTag && (bootTag === ID)) return true;

// Check that this module or process set the WeaveDrive tag on spawn
Expand All @@ -452,28 +452,34 @@ module.exports = function weaveDrive(mod, FS) {
const modes = ["Assignments", "Individual", "Library"]
// Get the Availability-Type from the spawned process's Module or Process item
// First check the module for its defaults
const moduleMode = mod.module.tags['Availability-Type']
? mod.module.tags['Availability-Type']
const moduleAvailabilityType = this.getTagValue('Availability-Type', mod.module.tags)
const moduleMode = moduleAvailabilityType
? moduleAvailabilityType
: "Assignments" // Default to assignments

// Now check the process's spawn item. These settings override Module item settings.
const processMode = mod.spawn.tags['Availability-Type']
? mod.spawn.tags['Availability-Type']
const processAvailabilityType = this.getTagValue('Availability-Type', mod.spawn.tags)
const processMode = processAvailabilityType
? processAvailabilityType
: moduleMode

if (!modes.includes(processMode)) {
throw `Unsupported WeaveDrive mode: ${processMode}`
}

var attestors = [mod.spawn.tags["Scheduler"]]
attestors.push(this.getTagValues("Attestor", mod.spawn.tags))
const attestors = this.serializeStringArr(
[
this.getTagValue('Scheduler', mod.spawn.tags),
...this.getTagValues("Attestor", mod.spawn.tags)
]
)

// Init a set of GraphQL queries to run in order to find a valid attestation
// Every WeaveDrive process has at least the "Assignments" availability check form.
const assignmentsHaveID = await this.queryHasResult(
`query {
transactions(
owners:["${mod.spawn.tags["Scheduler"]}"],
owners: ${attestors},
block: {min: 0, max: ${blockHeight}},
tags: [
{ name: "Data-Protocol", values: ["ao"] },
Expand Down Expand Up @@ -501,7 +507,7 @@ module.exports = function weaveDrive(mod, FS) {
const individualsHaveID = await this.queryHasResult(
`query {
transactions(
owners:["${mod.spawn.tags["Scheduler"]}"],
owners: ${attestors},
block: {min: 0, max: ${blockHeight}},
tags: [
{ name: "Data-Protocol", values: ["WeaveDrive"],
Expand Down Expand Up @@ -539,16 +545,25 @@ module.exports = function weaveDrive(mod, FS) {
return false
},

serializeStringArr (arr = []) {
return`[${arr.map((s) => `"${s}"`).join(', ')}]`
},

getTagValues(key, tags) {
var values = []
for (i = 0; i < tags.length; i++) {
if (tags[i].key == key) {
if (tags[i].name == key) {
values.push(tags[i].value)
}
}
return values
},

getTagValue (key, tags) {
const values = this.getTagValues(key, tags)
return values.pop()
},

async queryHasResult(query) {
const results = await mod.arweave.api.post('/graphql', query);
const json = JSON.parse(results)
Expand Down
33 changes: 23 additions & 10 deletions extensions/weavedrive/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,22 @@ const Msg = {
Timestamp: Date.now()
}

const BootLoadedFromTx = 'Fmtgzy1Chs-5ZuUwHpQjQrQ7H7v1fjsP0Bi8jVaDIKA'

const options = {
format: 'wasm64-unknown-emscripten-draft_2024_02_15',
WeaveDrive: weaveDrive,
ARWEAVE: 'https://arweave.net',
mode: "test",
blockHeight: 1000,
spawn: {
tags: {
"Scheduler": "TEST_SCHED_ADDR",
"On-Boot": "Fmtgzy1Chs-5ZuUwHpQjQrQ7H7v1fjsP0Bi8jVaDIKA"
}
tags: [
{ name: 'Scheduler', value: 'TEST_SCHED_ADDR' },
{ name: 'On-Boot', value: BootLoadedFromTx }
],
},
module: {
tags: {

}
tags: []
}
}

Expand Down Expand Up @@ -295,7 +295,7 @@ const ProcessBootLoaderTx = {
{ name: 'Variant', value: 'ao.TN.1' },
{ name: 'Type', value: 'Process' },
{ name: 'Extension', value: 'WeaveDrive' },
{ name: 'On-Boot', value: 'Fmtgzy1Chs-5ZuUwHpQjQrQ7H7v1fjsP0Bi8jVaDIKA' },
{ name: 'On-Boot', value: BootLoadedFromTx },
{ name: 'Module', value: 'MODULE' },
{ name: 'Authority', value: 'PROCESS' }
],
Expand All @@ -312,8 +312,21 @@ const optionsBootLoaderTx = { ...options, mode: null }

test('boot loader set to tx id', async function () {
const handle = await AoLoader(bootLoaderWasm, optionsBootLoaderTx)
const result = await handle(memoryBootLoader, {
const { Memory } = await handle(memoryBootLoader, {
...ProcessBootLoaderTx
}, { Process: ProcessBootLoaderTx, Module })
assert.equal(result.Output.data, '')

/**
* Now access a value set by the On-Boot tx's
* evaluation
*/
const result = await handle(Memory, {
...Msg,
Owner: 'PROCESS',
Target: 'PROCESS',
From: 'PROCESS',
Data: 'Ticker'
}, { Process: ProcessBootLoaderTx, Module })

assert.equal(result.Output.data, '<TICKER>')
})

0 comments on commit c4113b6

Please sign in to comment.