ERC-7985 · Draft(草案) · ERC · 2024-07-04
ERC-7985: Gateway Attributes for Message Control
ERC-7786 跨链消息的标准属性:取消、投递窗口、重试策略、回滚行为、依赖关系和最低 gas,让应用可以向任何网关请求相同的控制。
它解决什么问题?
ERC-7786 定义了一个统一接口,用于通过任意桥或消息协议("网关")在链之间发送消息。它有意把协议特有的功能留给属性:附加在每条消息上的额外 bytes 条目,每个条目以 4 字节的键开头。ERC-7786 建议在单独的 ERC 中把常见属性标准化。
ERC-7985 正是针对几乎每个跨链应用最终都需要的控制能力的后续标准:取消消息、防止消息到达得太晚或太早、失败时重试、决定投递失败时如何处理、强制执行顺序以及保证有足够的 gas。如果没有共同的定义,每个网关都会自创一套,应用也不得不为每个提供方单独处理。
它如何运作
每个属性的键是一个描述性签名的函数选择器,其值是参数的 ABI 编码,因此可以用 abi.encodeWithSignature 构造条目。网关可以支持任意子集,必须校验自己所支持属性的编码,并且在编码错误时必须回滚。
| 属性 | 选择器 | 含义 |
|---|---|---|
cancellable(bool) |
0xde986d7f |
若为 true,网关必须提供取消待处理消息的方式。默认为 false。 |
deliverBefore(uint256) |
0x3e97d7ee |
禁止在此 Unix 时间戳之后投递;0 表示不过期。 |
deliverAfter(uint256) |
0x745910eb |
最早投递时间戳;0 表示无延迟。与 deliverBefore 一起构成一个窗口。 |
retryPolicy(bytes) |
0xf002c055 |
紧凑打包的 uint16 maxRetries、uint32 retryDelay(秒)、uint32 backoffMultiplier(基点,10000 = 1 倍)。 |
revertBehavior(uint8) |
0x9e521a77 |
0 失败时回滚(默认),1 触发 MessageFailed 并继续,2 静默失败。 |
dependsOn(bytes32[]) |
0xa9fed7b9 |
必须先成功投递的消息 ID。 |
minGasLimit(uint256) |
0x39f87ba1 |
网关必须为投递提供的最低 gas。 |
不认识这些属性的网关仍按 ERC-7786 的规则正常工作;发送方可以先检查 supportsAttribute(selector)。
示例代码
发送一条带有投递窗口、gas 下限和依赖关系的消息:
// SPDX-License-Identifier: CC0-1.0
pragma solidity ^0.8.20;
interface IERC7786GatewaySource {
function supportsAttribute(bytes4 selector) external view returns (bool);
function sendMessage(bytes calldata recipient, bytes calldata payload, bytes[] calldata attributes)
external payable returns (bytes32 sendId);
}
contract Sender {
IERC7786GatewaySource public immutable gateway;
constructor(IERC7786GatewaySource g) { gateway = g; }
function send(bytes calldata recipient, bytes calldata payload, bytes32 previous)
external payable returns (bytes32)
{
bytes32[] memory deps = new bytes32[](1);
deps[0] = previous;
bytes[] memory attrs = new bytes[](4);
attrs[0] = abi.encodeWithSignature("deliverAfter(uint256)", block.timestamp + 10 minutes);
attrs[1] = abi.encodeWithSignature("deliverBefore(uint256)", block.timestamp + 1 days);
attrs[2] = abi.encodeWithSignature("minGasLimit(uint256)", 200_000);
attrs[3] = abi.encodeWithSignature("dependsOn(bytes32[])", deps);
require(gateway.supportsAttribute(0xa9fed7b9), "no dependsOn support");
return gateway.sendMessage{value: msg.value}(recipient, payload, attrs);
}
}
recipient 是 ERC-7786 使用的二进制 Interoperable Address(参见 ERC-7930)。
需要注意
- 状态:Draft。 选择器由其签名固定,但语义仍可能改变。
- Security Considerations 尚未撰写。草案中提到的一个未决问题是网关应如何处理循环的
dependsOn链。 retryPolicy的默认值描述存在歧义:空值被描述为"无限重试",而maxRetries = 0被描述为"不重试"。不要依赖默认值;如果你的网关支持,请显式设置。- 属性只是请求。是否会被强制执行取决于网关和底层的桥;在依赖任何属性之前,请先检查
supportsAttribute。