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
function write(bytes memory name, bytes memory data) external payable: An account with write permission writes binary data to a file name in the directory.function read(bytes memory name) external view returns (bytes memory data, bool exist): Returns the binary data and existence status for a file name in the directory.fallback(bytes calldata prefixedName) external returns (bytes memory data): A fallback read function that returns the data for a file at prefixedName, which starts with "/".function size(bytes memory name) external view returns (uint256 size, uint256 chunks): Returns the data size and chunk count for a file name.function remove(bytes memory name) external returns (uint256 numOfChunksRemoved): An account with write permission deletes a file name and returns the number of chunks deleted. A value of 0 means the file did not exist.function countChunks(bytes memory name) external view returns (uint256 numOfChunks);: Returns the number of chunks for a file name.function writeChunk(bytes memory name, uint256 chunkId, bytes memory chunkData) external payable;: An account with write permission writes chunk data to a file. This fails if chunkId is greater than the current chunk count.function readChunk(bytes memory name, uint256 chunkId) external view returns (bytes memory chunkData, bool exist);: Returns the data and existence status for chunk chunkId of a file name.function chunkSize(bytes memory name, uint256 chunkId) external view returns (uint256 chunkSize, bool exist);: Returns the size and existence status for chunk chunkId of a file name.function removeChunk(bytes memory name, uint256 chunkId) external returns (bool exist);: An account with write permission deletes a specific chunk of a file. Returns false if the chunk does not exist.function truncate(bytes memory name, uint256 chunkId) external returns (uint256 numOfChunksRemoved);: An account with write permission deletes the given chunkId and all subsequent chunks, returning the number of chunks deleted. If chunkId is 0, this behaves the same as remove().function getChunkHash(bytes memory name, uint256 chunkId) external view returns (bytes32);: Returns the hash value of the chunk data.
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.