EIPs Explained

ERC-5018 · Stagnant(정체) · ERC · 2022-04-18

ERC-5018: Filesystem-like Interface for Contracts

임의 크기의 이름 있는 파일을 청크 단위로 온체인에 저장하는 파일시스템 스타일의 컨트랙트 인터페이스. 웹사이트 등의 자산을 여러 트랜잭션에 걸쳐 업로드하고 한 번의 호출로 읽어 올 수 있다.

어떤 문제를 해결하나요?

ERC-4804는 web3:// URL로 컨트랙트를 가리키고 웹 페이지처럼 콘텐츠를 가져올 수 있게 합니다. 하지만 실제 웹사이트는 상대 경로로 서로를 참조하는 파일(HTML, CSS, 이미지)의 디렉터리이고, 파일 하나가 한 트랜잭션에 담을 수 있는 크기보다 클 수도 있습니다.

ERC-5018은 디렉터리 컨트랙트를 표준화합니다. 파일은 이름을 키로 하여 한 번의 호출로 읽을 수 있고 청크 단위로 쓸 수 있습니다. 그러면 로컬 폴더와 그 온체인 사본이 똑같이 보이고, 상대 링크도 계속 동작합니다.

어떻게 동작하나요?

각 파일은 bytes 이름으로 식별되며 순서가 있는 청크 목록으로 저장됩니다.

파일 전체 단위 작업:

  • write(name, data): 파일 내용을 교체합니다(payable, 쓰기 권한자만).
  • read(name) → (data, exist): 모든 청크를 이어 붙여 반환합니다.
  • size(name) → (size, chunks)와 remove(name) → chunksRemoved.
  • calldata를 /로 시작하는 경로로 간주해 파일을 반환하는 fallback. 이것이 web3:// 경로 방식 접근을 가능하게 합니다.

큰 파일을 위한 청크 단위 작업:

  • writeChunk(name, chunkId, data): 기존 청크를 교체하거나 다음 청크를 추가하는 것만 가능하며, chunkId > countChunks는 실패합니다.
  • readChunk, chunkSize, countChunks, removeChunk, truncate(name, fromChunk).
  • getChunkHash(name, chunkId): 클라이언트가 바뀌지 않은 청크의 재업로드를 건너뛸 수 있게 합니다.

write는 writeChunk(name, 0, data) 다음에 truncate(name, 1)을 호출하는 방식으로 구현할 수 있습니다. 누구에게 쓰기 권한이 있는지는 구현에 달려 있습니다.

샘플 코드

인터페이스와, ethers v6로 큰 파일을 청크 단위로 업로드하는 코드:

interface IERC5018 {
    function write(bytes memory name, bytes memory data) external payable;
    function read(bytes memory name) external view returns (bytes memory data, bool exist);
    function size(bytes memory name) external view returns (uint256 size, uint256 chunks);
    function remove(bytes memory name) external returns (uint256 numOfChunksRemoved);
    function countChunks(bytes memory name) external view returns (uint256);
    function writeChunk(bytes memory name, uint256 chunkId, bytes memory chunkData) external payable;
    function readChunk(bytes memory name, uint256 chunkId) external view returns (bytes memory, bool);
    function chunkSize(bytes memory name, uint256 chunkId) external view returns (uint256, bool);
    function removeChunk(bytes memory name, uint256 chunkId) external returns (bool);
    function truncate(bytes memory name, uint256 chunkId) external returns (uint256);
    function getChunkHash(bytes memory name, uint256 chunkId) external view returns (bytes32);
}
import { Contract, toUtf8Bytes, keccak256 } from "ethers";

const CHUNK = 24 * 1024;
async function upload(dir: Contract, path: string, data: Uint8Array) {
  const name = toUtf8Bytes(path);
  for (let i = 0; i * CHUNK < data.length; i++) {
    const part = data.slice(i * CHUNK, (i + 1) * CHUNK);
    if ((await dir.getChunkHash(name, i)) === keccak256(part)) continue; // unchanged (if impl hashes this way)
    await (await dir.writeChunk(name, i, part)).wait();
  }
  await (await dir.truncate(name, Math.ceil(data.length / CHUNK))).wait(); // drop leftovers
}

알아둘 점

  • 상태는 Stagnant입니다. 제안이 최종 확정을 향해 진행되지 않고 있습니다.
  • 명세는 청크를 어떻게 저장하거나 해시하는지 정의하지 않습니다. 위의 해시 비교는 원본 청크의 keccak256을 가정하므로 실제 구현에 맞는지 확인해야 합니다.
  • 온체인 스토리지는 비쌉니다. 구현은 다른 스토리지 백엔드를 사용할 수 있지만 인터페이스는 어느 것인지 알려 주지 않습니다.
  • "쓰기 권한이 있는 계정" 이상의 권한 모델은 정의되어 있지 않습니다.
  • URL 쪽은 ERC-4804와 그 후속 초안인 ERC-6860을 참고하세요. ERC-7617은 Web3 URL의 청크 응답을 다룹니다.

#storage #web3-url #files #onchain-web

상세 정보

저자
Qi Zhou (@qizhou)
상태
Stagnant(정체)
유형
Standards Track / ERC
작성일
2022-04-18

기계 판독용 데이터: Markdown · JSON