RPChttps://rpc.vitruveo.ai
Chain ID1490
CurrencyVTRU
Protocol Smart Contract
Go To:

Protobuf

Protobuf wire codec and on-chain message storage0x00000000000000000000000000000000000000B0200 + 1 gas/byte stored

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

// Protobuf.sol from contracts/protobuf in the Vitruveo protocol repo.
// It wraps the precompile at 0x00000000000000000000000000000000000000B0 with low-level calls.
import {Protobuf} from "./Protobuf.sol";

contract ArtworkMetadata {
    using Protobuf for bytes32;

    // Stored messages are namespaced by this contract's address.
    function setMetadata(uint256 tokenId, string calldata name, uint256 width) external {
        bytes32 key = keccak256(abi.encode(tokenId));
        key.setString(Protobuf.path(1), name);        // name = 1
        key.setUint(Protobuf.path(2, 1), width);      // dims.width = 2.1
        key.addString(Protobuf.path(3), "landscape"); // repeated tags = 3
    }

    // Anyone can read any stored message.
    function nameOf(uint256 tokenId) external view returns (string memory) {
        bytes32 key = keccak256(abi.encode(tokenId));
        return Protobuf.getString(address(this), key, Protobuf.path(1));
    }

    // Messages in memory (no storage). Start from new bytes(0).
    function buildInMemory() external view returns (bytes memory m, uint256 w) {
        m = Protobuf.setString(new bytes(0), Protobuf.path(1), "Sunset");
        m = Protobuf.setUint(m, Protobuf.path(2, 1), 1920);
        w = Protobuf.getUint(m, Protobuf.path(2, 1));
    }
}

Why It Matters

Contracts can decode, build and edit schema-less protobuf messages without a Solidity decoder, and store whole messages on chain for 5,000 gas per write plus 1 gas per byte, instead of paying SSTORE for every 32-byte slot. Individual fields of a stored message can be read or replaced by path.

How It Works

  • Paths are field numbers from the outer message inward. Every element but the last must be an embedded message. The last occurrence of a field is the one read or replaced.
  • Values: varint/fixed fields are 32-byte big-endian words; bytes, strings and embedded messages are raw bytes. Wire types: 0 varint, 1 fixed64, 2 length-delimited, 5 fixed32. Groups are rejected.
  • Storage is namespaced by msg.sender: a contract can only write its own messages, and anyone can read any message. Writes revert inside STATICCALL.
  • Stored messages are limited to 1 MiB; paths to 64 levels.
  • Call it through the Protobuf library (low-level calls). Calling the interface directly fails for functions without return values, because Solidity checks that the target has code and the precompile has none.

Gas

Every call               200
Per byte of input/scan   1
Per field scanned/built  10
Stored message read      2600
Stored write or removal  5000
Per byte written         1

Interface

interface IProtobuf {
    // Codec
    function decode(bytes calldata data)
        external view returns (uint256[] memory fieldNumbers, uint256[] memory wireTypes, bytes[] memory values);
    function encode(uint256[] calldata fieldNumbers, uint256[] calldata wireTypes, bytes[] calldata values)
        external view returns (bytes memory data);
    function decodePacked(bytes calldata data, uint256 wireType) external view returns (uint256[] memory values);
    function encodePacked(uint256[] calldata values, uint256 wireType) external view returns (bytes memory data);

    // Field access on a message in memory
    function read(bytes calldata data, uint256[] calldata path)
        external view returns (bool found, uint256 wireType, bytes memory value);
    function readAll(bytes calldata data, uint256[] calldata path)
        external view returns (uint256[] memory wireTypes, bytes[] memory values);
    function write(bytes calldata data, uint256[] calldata path, uint256 wireType, bytes calldata value)
        external view returns (bytes memory);
    function append(bytes calldata data, uint256[] calldata path, uint256 wireType, bytes calldata value)
        external view returns (bytes memory);
    function clear(bytes calldata data, uint256[] calldata path) external view returns (bytes memory);

    // Storage
    function store(bytes32 key, bytes calldata data) external;
    function load(address owner, bytes32 key) external view returns (bytes memory data);
    function remove(bytes32 key) external;
    function loadField(address owner, bytes32 key, uint256[] calldata path)
        external view returns (bool found, uint256 wireType, bytes memory value);
    function loadAll(address owner, bytes32 key, uint256[] calldata path)
        external view returns (uint256[] memory wireTypes, bytes[] memory values);
    function storeField(bytes32 key, uint256[] calldata path, uint256 wireType, bytes calldata value) external;
    function appendField(bytes32 key, uint256[] calldata path, uint256 wireType, bytes calldata value) external;
    function clearField(bytes32 key, uint256[] calldata path) external;
}

Source: contracts/protobuf/IProtobuf.sol and the typed helper library contracts/protobuf/Protobuf.sol in the Vitruveo protocol repository.