Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: weavedrive on-boot #376

Merged
merged 1 commit into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions extensions/weavedrive/package-lock.json

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

2 changes: 1 addition & 1 deletion extensions/weavedrive/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@permaweb/weavedrive",
"version": "0.0.10",
"version": "0.0.11",
"license": "MIT",
"main": "./dist/index.cjs",
"scripts": {
Expand Down
34 changes: 17 additions & 17 deletions extensions/weavedrive/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ module.exports = function weaveDrive(mod, FS) {

async customFetch(path, options) {
let urlList = null
if(mod.ARWEAVE.includes(',')) {
if (mod.ARWEAVE.includes(',')) {
urlList = mod.ARWEAVE.split(',').map(url => url.trim())
}
if(urlList && urlList.length > 0) {
if (urlList && urlList.length > 0) {
/**
* Try a list of gateways instead of a single one
*/
Expand Down Expand Up @@ -54,10 +54,10 @@ module.exports = function weaveDrive(mod, FS) {
// This check/mkdir was added for AOP 6 Boot loader because create is
// called first because were only loading Data, we needed to create
// the directory. See: https://github.com/permaweb/aos/issues/342
if(!FS.analyzePath('/data/').exists){
if (!FS.analyzePath('/data/').exists) {
FS.mkdir('/data/');
}

var node = FS.createFile('/', 'data/' + id, properties, true, false);
// Set initial parameters
var bytesLength = await this.customFetch(`/${id}`, { method: 'HEAD' }).then(res => res.headers.get('Content-Length'))
Expand Down Expand Up @@ -170,7 +170,7 @@ module.exports = function weaveDrive(mod, FS) {
}
}
}`
var variables = { transactionIds: [id] }
var variables = { transactionIds: [id] }
async function retry(x) {
return new Promise(r => {
setTimeout(function () {
Expand All @@ -195,23 +195,23 @@ module.exports = function weaveDrive(mod, FS) {
return res?.data?.transactions?.edges?.[0]?.node ? res.data.transactions.edges[0].node : 'No results'
})
.then(async entry => {
return typeof(entry) == 'string' ? entry : {
return typeof (entry) == 'string' ? entry : {
format: 3,
...entry
}
})
.then(x => {
return typeof(x) == 'string' ? x : JSON.stringify(x)
return typeof (x) == 'string' ? x : JSON.stringify(x)
});


if (result === 'No results') {
return result
}
FS.createDataFile('/', 'tx2/' + id, result, true, false);
var stream = FS.open('/tx2/' + id, 'r');

return stream;
if (result === 'No results') {
return result
}
FS.createDataFile('/', 'tx2/' + id, result, true, false);
var stream = FS.open('/tx2/' + id, 'r');

return stream;
},
async open(filename) {
const pathCategory = filename.split('/')[1];
Expand Down 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.Process.Tags.find((t) => t.name === 'On-Boot')?.value;
const bootTag = mod.spawn.tags['On-Boot'];
if (bootTag && (bootTag === ID)) return true;

// Check that this module or process set the WeaveDrive tag on spawn
Expand Down Expand Up @@ -554,7 +554,7 @@ module.exports = function weaveDrive(mod, FS) {
const json = JSON.parse(results)
return json.data.transactions.edges.length > 0
},

async gqlExists() {
const query = `query {
transactions(
Expand All @@ -566,7 +566,7 @@ module.exports = function weaveDrive(mod, FS) {
}
}
`

const gqlExists = await this.gqlQuery(query, {}).then((res) => res.ok)
return gqlExists
},
Expand Down
58 changes: 34 additions & 24 deletions extensions/weavedrive/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ const Module = {
Id: "MODULE",
Owner: "OWNER",
Tags: [
{ name: 'Data-Protocol', value: 'ao'},
{ name: 'Variant', value: 'ao.TN.1'},
{ name: 'Type', value: 'Module'},
{ name: 'Data-Protocol', value: 'ao' },
{ name: 'Variant', value: 'ao.TN.1' },
{ name: 'Type', value: 'Module' },
{ name: 'Authority', value: 'PROCESS' }
]
}
Expand Down Expand Up @@ -48,15 +48,22 @@ const Msg = {
}

const options = {
format: 'wasm64-unknown-emscripten-draft_2024_02_15',
WeaveDrive: weaveDrive,
ARWEAVE: 'https://arweave.net',
mode: "test",
blockHeight: 1000,
spawn: {
"Scheduler": "TEST_SCHED_ADDR"
},
process: Process
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"
}
},
module: {
tags: {

}
}
}

test('load client source', async () => {
Expand Down Expand Up @@ -148,8 +155,8 @@ return require('json').encode({ A = #results, B = #results2 })

// test weavedrive feature of acceptint multiple gateways
test('read block, multi url', async () => {
const handle = await AoLoader(wasm, {
...options,
const handle = await AoLoader(wasm, {
...options,
ARWEAVE: 'https://arweave.net,https://g8way.io'
})
const result = await handle(memory, {
Expand All @@ -163,8 +170,8 @@ test('read block, multi url', async () => {


test('read tx, multi url', async () => {
const handle = await AoLoader(wasm, {
...options,
const handle = await AoLoader(wasm, {
...options,
ARWEAVE: 'https://arweave.net,https://g8way.io'
})
const result = await handle(memory, {
Expand Down Expand Up @@ -242,10 +249,10 @@ return result

let memoryBootLoader = null

/*
* The Process is also the first message when aop 6 boot loader
* is enabled in the network
*/
/*
* The Process is also the first message when aop 6 boot loader
* is enabled in the network
*/
const ProcessBootLoaderData = {
Id: 'PROCESS',
Owner: 'PROCESS',
Expand All @@ -261,20 +268,22 @@ const ProcessBootLoaderData = {
],
Data: `
Test = 1
print("Test " .. Test)
`,
From: 'PROCESS',
Module: 'MODULE',
"Block-Height": 1000,
Timestamp: Date.now()
}

const optionsBootLoaderData = { ...options, Process: ProcessBootLoaderData, mode: null}
const optionsBootLoaderData = { ...options, mode: null }

test('boot loader set to Data', async function () {
const handle = await AoLoader(bootLoaderWasm, optionsBootLoaderData)
await handle(memoryBootLoader, {
const result = await handle(memoryBootLoader, {
...ProcessBootLoaderData
}, { Process: ProcessBootLoaderData, Module })
assert.equal(result.Output.data, 'Test 1')
})

const ProcessBootLoaderTx = {
Expand All @@ -299,11 +308,12 @@ Test = 1
Timestamp: Date.now()
}

const optionsBootLoaderTx = { ...options, Process: ProcessBootLoaderTx, mode: null}
const optionsBootLoaderTx = { ...options, mode: null }

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