---
eip: 801
label: ERC-801
title: Canary Standard
status: Stagnant
type: Standards Track
category: ERC
created: 2017-12-16
official: https://eips.ethereum.org/EIPS/eip-801
source: https://github.com/ethereum/ERCs/blob/master/ERCS/erc-801.md
canonical: https://eips.huatuo.xyz/zh/eip-801/
lang: zh
tldr: "为链上\"金丝雀\"合约设计的最小接口：只有在有人持续\"喂食\"时它才保持存活，使应用能够检测到认证金丝雀（warrant canary）或心跳何时中断。"
tags: [canary, heartbeat, monitoring, interface]
---

# ERC-801: Canary Standard

> A standard interface for canary contracts.

**TL;DR:** 为链上"金丝雀"合约设计的最小接口：只有在有人持续"喂食"时它才保持存活，使应用能够检测到认证金丝雀（warrant canary）或心跳何时中断。

## 它解决什么问题？

**认证金丝雀（warrant canary）**是一种声明，例如"我们没有收到秘密传票"，由组织按计划定期重复发布。一旦它停止，人们即使没有听到组织明说，也可以推断发生了什么。同样的思路也适用于心跳："这个运营方仍然活跃"，或"这台设备仍在签到"。

在以太坊上，金丝雀可以是一个必须有人通过定期交易来"喂食"的合约。ERC-801 为这类合约提供了通用接口，因此仪表盘、告警服务、喂食应用甚至保险合约，都能以同样的方式读取任何金丝雀的状态。

## 它如何运作

接口刻意做得极小：

| 成员 | 含义 |
| --- | --- |
| `isAlive()` | 金丝雀被正常喂食时为 `true` |
| `getBlockOfDeath()` | 它死亡时的区块号。在仍然存活时会抛出异常 |
| `getType()` | 金丝雀的类型（见下） |
| `event RIP()` | 必须在金丝雀死亡后的第一次调用时触发 |

提议的 `getType()` 取值：

- `1` Simple：只有这个接口
- `2` 单个喂食者
- `3` 带"坏食物"的单个喂食者
- `4` 多个喂食者
- `5` 多个必选喂食者
- `6` IoT

类型 2 到 6 指向后续的 ERC（"ERC-TBD"），而这些 ERC 从未写成。金丝雀**如何**被喂食，以及它可以饿多久，由具体实现决定。

## 示例代码

一个单喂食者金丝雀，如果在 `interval` 个区块内没有被喂食就会死亡：

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

contract SimpleCanary {
    event RIP();

    address public immutable feeder;
    uint256 public immutable interval;   // max blocks between feedings
    uint256 public lastFed;
    bool private ripEmitted;

    constructor(uint256 _interval) {
        feeder = msg.sender;
        interval = _interval;
        lastFed = block.number;
    }

    function isAlive() public view returns (bool) {
        return block.number <= lastFed + interval;
    }

    function getBlockOfDeath() external view returns (uint256) {
        require(!isAlive(), "still alive");
        return lastFed + interval + 1;
    }

    function getType() external pure returns (uint8) {
        return 2; // single feeder
    }

    function feed() external {
        require(msg.sender == feeder, "not feeder");
        if (!isAlive()) {
            if (!ripEmitted) { ripEmitted = true; emit RIP(); }
            return;                      // a dead canary stays dead
        }
        lastFed = block.number;
    }
}
```

## 需要注意

- 该提案状态为 **Stagnant**，从未最终确定。规范自己的实现一节至今仍写着"TODO"。
- 规范使用的是 0.5 之前的 Solidity（`constant` 函数）。现代代码使用 `view`。
- 由于合约无法自行行动，只有在死亡之后有人调用合约时才能触发 `RIP`。链下的监控方应轮询 `isAlive()`，而不是等待该事件。
- 金丝雀只能证明喂食者一直在发送交易。错过喂食意味着什么，是社会或法律层面的问题，不是合约能够决定的。


---

This is an unofficial plain-language summary. Read the full specification: https://eips.ethereum.org/EIPS/eip-801
