ERC-2157 dType storage registry extension standard (dType Storage Extension - Decentralized Type System for EVM)
Stagnant · Other · created 2019-06-28
An extension proposal to ERC-1900. It standardizes the ABI of storage contracts so that anyone can store and look up data for the decentralized type system dType in the same way.
Why it is needed
Ethereum currently has no standard for determining where public data is stored. While ERC-1900 standardized the type definitions themselves, where the actual data instances of that type are stored, which contract and which path, varies by project. As a result, finding data externally is limited to relying on a centralized cache such as a blockchain explorer, or directly analyzing the blockchain to find contracts that use a particular type. This approach also blocks on-chain interoperability that would standardly reference data paths. For example, even if a marketplace contract wants to fetch data for a particular product type, there is no fixed rule for where that data is stored, so custom lookup logic must be written from scratch each time.
What to implement
constructor(address _library, address _storage) public {: Deploy the TypeRootContract specifying the type definition library address (libraryAddress) and the type data storage contract address (storageAddress).event LogNew(bytes32 indexed identifier, uint256 indexed index);: Records the identifier and index when a new type instance is added to storage.event LogUpdate(bytes32 indexed identifier, uint256 indexed index);: Records the identifier and index when an existing type instance is modified.event LogRemove(bytes32 indexed identifier, uint256 indexed index);: Records the identifier and index when a type instance is deleted.function insert(TypeALib.TypeA memory data) public returns (bytes32 identifier);: Takes data in the form of a type library struct, adds it as a new record in storage, and returns the identifier.function insertBytes(bytes memory data) public returns (bytes32 identifier);: Takes data encoded as bytes, adds it as a new record in storage, and returns the identifier.function remove(bytes32 identifier) public returns(uint256 index);: Deletes the record specified by the identifier from storage and returns its index.function update(bytes32 identifier, TypeALib.TypeA memory data) public returns(bytes32 identifier): Updates the data of the record specified by the identifier with a new value.function isStored(bytes32 identifier) public view returns(bool stored);: Checks whether a record with the given identifier exists in storage.function getByHash(bytes32 identifier) public view returns(TypeALib.TypeA memory data);: Looks up a record by identifier and returns the type data.function getByIndex(uint256 index) public view returns(TypeALib.TypeA memory data);: Looks up a record by its monotonically increasing index number and returns the type data.function count() public view returns(uint256 counter);: Returns the total number of records stored in storage.
In depth
What the TypeRootContract does
ERC-1900 has a contractAddress field in its type metadata, which originally held the type definition library address. This proposal redefines that field to hold the address of a TypeRootContract instead. The TypeRootContract is a simple contract holding only two values: libraryAddress (the ERC-1900 type definition library address) and storageAddress (the address of the storage contract holding the type data). This lets anyone look at a type's registry entry and immediately find where its definition code and its actual data each live.
The CRUD pattern of the TypeStorageContract
The TypeStorageContract follows the Solidity CRUD pattern and is defined with a common ABI comprising the functions insert, insertBytes, remove, update, isStored, getByHash, getByIndex, and count. insert takes the struct value from the type library as is, while insertBytes takes data encoded as bytes. Because every storage contract implements this ABI identically, any type's data can be handled with the same function names and signatures.
How is the primary identifier determined
Each record's primary identifier is not an arbitrarily assigned value; it is computed by the type library's getIdentifier function based on the actual values of the type instance. Instances with the same values always get the same identifier, so the data path is deterministic and anyone can locate the same record the same way.
typeIndex and index-based lookup
Storage keeps the identifiers of records in order in a bytes32 array called typeIndex, and each record also carries a monotonically increasing index number. getByHash looks up a record by identifier and getByIndex by this index number, so the full set of records can be iterated and retrieved even without knowing the identifier. The count function reports the total number of records stored.
Why a separate contract was pulled out instead of extending the registry
Rather than adding a storage contract address field to the dType registry itself, a separate TypeRootContract was introduced because it makes it easier to change the interface in the future and is more extensible. It also lets dType itself and all TypeStorage contracts share the same storage pattern, lowering the cost of writing, testing, and auditing code.
Backwards compatibility, implementation status, and current status
This proposal does not affect existing Ethereum standards or implementations, but it uses ABIEncoderV2, which was experimental at the time. Test cases have not yet been written, and it states that a work-in-progress implementation exists in the pipeos-one/dType repository and will be updated once consensus on the specification is reached. This proposal is currently Stagnant, a draft that stalled without further progress or finalization.