ERC-1900 · Stagnant(停滞) · ERC · 2019-03-28
ERC-1900: dType - Decentralized Type System for EVM
dType 提出一个 Solidity 类型(结构体及其库)的链上注册表,让项目可以共享数据定义,工具也能一致地解码数据。
它解决什么问题?
以太坊上的每个项目都定义自己的结构体,即使它们描述的是同一样东西。两个都有“余额”或“代币数量”类型的协议,没有定制的粘合代码就无法互相传递数据,区块浏览器也必须被分别告知每个合约的 ABI 才能解码。
dType(“去中心化类型系统”)提出一个共享的链上类型注册表。一个类型只需连同足以重建其 ABI 的元数据注册一次,任何人都可以复用它、把它组合成更大的类型,或解码使用它的数据。
它如何运作
类型库。 每个类型由一个 Solidity 库定义,其中包含 struct,以及 isInstanceOf(验证)、structureBytes / destructureBytes(编码与解码)等辅助函数,还有 map、filter 和 reduce 这样的高阶函数。类型可以包含其他类型,始终构成一个有向无环图。
注册表记录。 对于每个类型,注册表存储:
name:Solidity 类型名,可选带版本typeChoice:BaseType、PayableFunction、StateFunction、ViewFunction、PureFunction或EventcontractAddress:类型库的地址source:源代码的 Swarm 哈希types:直接的子字段,每个都有name、label和数组dimensions([""]表示T[],["2","3"]表示T[2][3])
记录的标识符是 keccak256(abi.encodePacked(name))。接口提供 insert、remove、count、get、getByIdentifier、getTypeIdentifier 和 isRegistered,并带有 LogNew / LogUpdate / LogRemove 事件。
规范中的组合示例。 myBalance = (string accountName, uint256 amount) 和 myToken = (address token, myBalance balance) 使 myToken 的 ABI 元组为 (address,(string,uint256))。
示例代码
组合类型的注册表元数据(JSON):
{
"typeChoice": 0,
"contractAddress": "0x...",
"source": "0x...",
"name": "myToken",
"types": [
{ "name": "address", "label": "token", "dimensions": [] },
{ "name": "myBalance", "label": "balance", "dimensions": [] }
]
}
注册表接口的精简版:
interface IDType {
enum TypeChoices { BaseType, PayableFunction, StateFunction, ViewFunction, PureFunction, Event }
struct dTypes { string name; string label; string[] dimensions; }
struct dType { TypeChoices typeChoice; address contractAddress; bytes32 source;
string name; dTypes[] types; }
event LogNew(bytes32 indexed identifier, uint256 indexed index);
function insert(dType calldata data) external returns (bytes32 identifier);
function get(string memory name) external view returns (dType memory);
function isRegistered(bytes32 identifier) external view returns (bool);
}
需要注意
- 状态:Stagnant。 它是针对 Solidity 0.5 以及当时仍属实验性的 ABIEncoderV2 编写的,测试用例从未添加。
- 治理(谁可以添加或删除类型)和权限被明确排除在范围之外;作者建议不支持更新,并指出
remove有风险。 - 用普通名称作为键会招致抢注;规范只建议起初禁止单个英文单词。
- 它被设计为一个更大构想(类型化存储、函数注册表)的第一步,后续提案对此有所描述,这些提案不在当前 EIP 索引中。