Skip to content

Commit

Permalink
feat(weavedrive): add l2 header access to weavedrive
Browse files Browse the repository at this point in the history
  • Loading branch information
jfrain99 committed Sep 26, 2024
1 parent a53fb0c commit 9fd7eba
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 2 deletions.
14 changes: 14 additions & 0 deletions extensions/weavedrive/client/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,18 @@ function drive.getData(txId)
return contents
end

function drive.getDataItem(txId)
local file = io.open('/txDataItem/' .. txId)
if not file then
return nil, "File not found!"
end
if file == 'GQL Not Found!' then
return nil, 'GraphQL not Found!'
end
local contents = file:read(
file:seek('end')
)
file:close()
return contents
end
return drive
111 changes: 109 additions & 2 deletions extensions/weavedrive/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,79 @@ module.exports = function weaveDrive(mod, FS) {
var stream = FS.open('/tx/' + id, 'r');
return stream;
},
async createDataItemTxHeader(id) {
const gqlQuery = this.gqlQuery
async function toAddress(owner) {
return Arweave.utils.bufferTob64Url(
await Arweave.crypto.hash(Arweave.utils.b64UrlToBuffer(owner))
);
}
async function retry(x) {
return new Promise(r => {
setTimeout(function () {
r(gqlQuery(`/tx/${id}`))
}, x * 10000)
})
}

const gqlExists = this.gqlExists()
if (!gqlExists) {
return 'GQL Not Found!'
}
var GET_TRANSACTION_QUERY = `
query GetTransactions ($transactionIds: [ID!]!) {
transactions(ids: $transactionIds) {
edges {
node {
id
owner {
address
}
quantity {
winston
ar
}
signature
tags {
name
value
}
block {
id
height
timestamp
}
data {
size
type
}
bundledIn {
id
}
fee {
winston
ar
}
}
}
}
}`
var variables = { transactionIds: [id] }
// todo: add a bunch of retries
var result = await this.gqlQuery(GET_TRANSACTION_QUERY, variables)
.then(res => !res.ok ? retry(1) : res)
.then(res => !res.ok ? retry(2) : res)
.then(res => !res.ok ? retry(3) : res)
.then(res => !res.ok ? retry(4) : res)
.then(res => res.data.transactions.edges[0].node)
.then(async entry => ({ ...entry, ownerAddress: await toAddress(entry.owner.address) }))
.then(x => JSON.stringify(x));

var node = FS.createDataFile('/', 'txDataItem/' + id, result, true, false);
var stream = FS.open('/txDataItem/' + id, 'r');

return stream;
},
async open(filename) {
const pathCategory = filename.split('/')[1];
const id = filename.split('/')[2];
Expand All @@ -147,6 +219,20 @@ module.exports = function weaveDrive(mod, FS) {
}

}
if (pathCategory === 'txDataItem') {
FS.createPath('/', 'txDataItem', true, false);
if (FS.analyzePath(filename).exists) {
for (var i = 0; i < FS.streams.length; i++) {
if (FS.streams[i].node.name === id) {
return FS.streams[i].fd;
}
}
return 0;
} else {
const stream = await this.createDataItemTxHeader(id);
return stream.fd;
}
}
if (pathCategory === 'block') {
FS.createPath('/', 'block', true, false);
if (FS.analyzePath(filename).exists) {
Expand Down Expand Up @@ -189,7 +275,6 @@ module.exports = function weaveDrive(mod, FS) {
return 0;
}
},

async read(fd, raw_dst_ptr, raw_length) {

// Note: The length and dst_ptr are 53 bit integers in JS, so this _should_ be ok into a large memspace.
Expand Down Expand Up @@ -468,6 +553,28 @@ module.exports = function weaveDrive(mod, FS) {
const results = await mod.arweave.api.post('/graphql', query);
const json = JSON.parse(results)
return json.data.transactions.edges.length > 0
},

async gqlExists() {
const query = `query {
transactions(
first: 1
) {
pageInfo {
hasNextPage
}
}
}
`

const gqlExists = await mod.arweave.api.post('/graphql', query).then((res) => res.ok)
return gqlExists
},

async gqlQuery(query, variables) {
const results = await mod.arweave.api.post('/graphql', JSON.stringify({ query, variables }))
const json = JSON.parse(results)
return json
}
}
}
}

0 comments on commit 9fd7eba

Please sign in to comment.