ERC-5018 Contract filesystem interface (Filesystem-like Interface for Contracts)

Stagnant · Other · created 2022-04-18

A standard that allows binary data to be stored, read, and written inside a contract like files in a directory structure. Large files can be split into chunks and uploaded across multiple transactions.

Why it is needed

To put binary web content such as HTML or SVG on chain, a common method is needed to store and retrieve it like files. But if each contract stores binary data differently, content stored by one dApp becomes hard for another dApp to reuse. There is also a limit on the amount of data that can fit in a single transaction (around 1.8MB according to the source), so files larger than that cannot be uploaded in a single write. EIP-4804 made it possible to point to on-chain resources via HTTP-style URIs, but without a standardized way for those resources to reference each other through directories and relative paths, the same web content cannot be mapped consistently between local and on-chain environments.

What to implement

In depth

Directory and file model

The original spec represents the contract as a single directory, with the target of write/read as a file (file name). Both the file name and the data are of type bytes, so arbitrary binary data can be handled. write is declared payable so a value can be sent along with the write, and read is a view function that returns both the data and an existence flag (exist), distinguishing between a missing file and an empty file.

Chunk based writing rules

Because of the transaction calldata limit (about 1.8MB per the original spec), large content is uploaded in pieces using writeChunk(name, chunkId, chunkData). If chunkId is greater than the current numOfChunks the write fails; writing to an existing chunkId replaces that chunk; and writing with a chunkId equal to numOfChunks appends to the end of the file. The original spec's example shows that for "hello.txt", writeChunk(0,"abc") followed by read returns "abc", writeChunk(1,"efg") then returns "abcefg", replacing with writeChunk(0,"aaa") then returns "aaaefg", and writeChunk(3,"hij") fails because it is out of range.

read returns the concatenated chunks

The read method must return, in a single call, the full data formed by concatenating the chunks written via writeChunk. In other words, an upload can be split across multiple transactions, but reading can be done in one call through a single EIP-4804 Web3 URL.

Relationship between write and the chunk functions

write(name, data) is described as replacing all chunks of the file with new data. The implementation example given in the original spec is a two step process: write new data to chunk 0 with writeChunk(name, 0, data), then remove all remaining chunks after it with truncate(name, 1).

Removal and truncate

remove(name) deletes the entire file and returns the number of chunks deleted, returning 0 if the file does not exist. removeChunk(name, chunkId) deletes only a single chunk and returns false if that chunk does not exist. truncate(name, chunkId) deletes the given chunkId and all subsequent chunks; if chunkId is 0 it behaves the same as remove().

Lookup functions

size(name) returns both the total file size and the number of chunks, while countChunks(name) returns only the number of chunks. chunkSize(name, chunkId) returns the size of a specific chunk along with whether it exists. getChunkHash(name, chunkId) returns the hash value of the chunk data, allowing data integrity to be verified externally.

Fallback reads and write permission

fallback(prefixedName) is a fallback function that reads a file via a path starting with "/"; combined with EIP-4804's HTTP style Web3 URL, a single URI can point to an on-chain file. Meanwhile, write, remove, writeChunk, removeChunk, and truncate are all specified as callable only by accounts with write permission, but how that permission is defined or checked is outside the scope of this standard.

Proposal status

This proposal is currently in Stagnant status, meaning it is no longer actively discussed and changes to the spec or its adoption have stalled.

Original ERC · ← ERCs