Open wanderer opened 7 years ago
@jbenet @kumavis @nicola @diasdavid
@axic
This is really interesting! I didn't have time to really looking into this, it is awesome to start working on this again now. The ideal would be, be able to compile the current resolvers into this language (or whatever candidate language) at some point, so no need to write the GO/JS parser.
The libc on eWASM (soon to be uploaded) can be reused for this to compile C snippets.
Webassembly DAG interfaces
related
https://github.com/ipld/specs/issues/35 https://github.com/ipld/specs/issues/38
Overview
This note presents an initial Webassembly interface for generic Merkle DAGs. There are two interfaces proposed here. 1) The translator (resolver) API which allows the wasm VM to parse links from binary data. 2) The Selector API allow for traversal of the graph and selecting of a subgraph
These interfaces are low level and should allow for upper layers to be implemented on top of them.
glossary
Rational
It would be conducive to implement Selectors and Transforms in webassembly so that 1) they can be loaded at runtime reducing the bootstrap binary size 2) enable universal implementations of Selectors and Transforms 3) reduces the trusted computing base 4) present an deterministic way to limit resource usage based on metering
Challenges
1) Its still the early days of Webassembly and Webassembly VM usage outside the browser is still largely uncharted territory 2) Language support is still limited to C/C++/rust although potential anything that llvm can compile can be used in a wasm VM
API Overview
Data types
We define the following Webassembly data types:
i32
: same asi32
in Webassemblyi32ptr
: same asi32
in Webassembly, but treated as a pointer to a Webassembly memory offseti32ref
: same asi32
in Webassembly, but treated as an opaque reference and should be replaced with an opaque reference after it has been specified in Webassemblyi64
: same asi64
in WebAssemblyTables
A table named 'callback' must be exported if any callbacks are used. All callbacks functions have a parameter of a single i32 which will contain the error code of the orginal operation. If there where no errors then the return value will be 0 other wise it will be 1. All operation that involve state look ups are asynchronous
Translator API
A translators maps some given data into method that can be consumed by the underlining IPFS implementations.
the Webassembly binary MUST export the following methods to be compatible DAG service. Like wise the host environment must also provided the following API to wasm binary that are intended to be translators. The translator's imports MUST use the namespace "translator".
createVertex
Creates a mutable vertex reference
Returns
vertexRefence
i32refcreateEdge
Given a link and some metadata this creates an edge.
Parameters
link
metaDataOffset
i32ptrmetaDatalength
i32Returns
edgeReference
i32refaddEdge
Adds an edge to a vertex given an edge reference and a label
Parameters
Selector API
Selectors traverses the graph and selects some subset of vertices. The Selector's imports MUST use the namespace "selector".
select
Adds a vertex reference to the array of reference to be returned
parameters
vertexReference
i32refroot
the root vertex of the DAG we are operating on
Returns
result
i32ref an opaque reference to the root vertexresolve
Given an edge reference returns a vertex reference
Parameters
link
i32ref a reference to a merkle linkcallBackIndex
i32 an index of the callback functionCallback Signature
error
i32 reserved for error codevertex
i32ref a reference to the resolved vertexgetEdge
Gets an edge reference given the edge name
Parameters
namePtr
i32prtlength
i32return
edgeReferance
i32refgetEdgeDataLength
Gets the metadata attached from to an edge
Parameters
edgeReference
i32ref an opaque reference to the edgereturn
length
i32 the length of the an edgegetEdgeData
Gets the data from an edge and writes it to memory
Parameters
edgeReference
i32ref an opaque reference to the edgewriteOffset
i32ptr the memory location to write the datagetLink
Unwrap a link from an edge
returns
link
i32ferisNullLink
checks if a link is null or not Parameters
linkReference
i32ref an opaque reference to a linkreturns
isNull
i32Iterator API
This enables iteration of a vertex's edges
edges
Return
edgeIterator
i32ref a reference to an edge iteratornext
Advances the iterator
Parameters
edgeItr
i32refgetEdgeName
Parameters
edgeItr
i32refwriteOffset
i32ptrgetEdgeNameLength
Parameters
edgeItr
i32refReturn
length
i32