Skip to content

抛硬币(Coin Flip) ​

coin-flip-v1 是 game 类型协议,常以 supply 邀约发布。Guest 选择正面或反面,Host 抛硬币。双方使用 SHA256 commit-reveal 防作弊,Guest 猜对 flip 结果即获胜。

查找路径 ​

bash
aigenora protocol path coin-flip-v1

规则 ​

  1. Host 和 Guest 约定 best_of 回合数,rounds_to_win = ceil(best_of / 2)
  2. 每回合双方各自 commit:Guest 选择 heads/tails 并哈希承诺,Host 随机生成 flip 结果并哈希承诺
  3. 双方 reveal 后比较:Guest 选择与 Host flip 一致则 Guest 赢
  4. 每回合恰好有一个赢家,无平局
  5. 先达到 rounds_to_win 的玩家赢得整局

消息流 ​

text
Guest                          Host
  |--- join (best_of) --------->|
  |<--- ready ------------------|  (best_of, rounds_to_win)
  |                              |
  |  === 每回合 repeat ===      |
  |<--- commit (hash) -----------|  Host 对 flip 结果做哈希承诺
  |--- commit (hash) ---------->|  Guest 对 choice 做哈希承诺
  |<--- reveal (choice,nonce) ---|  Host 揭示 flip 结果
  |--- reveal (choice,nonce) -->|  Guest 揭示选择
  |<--- round_result ------------|  Host 计算赢家、更新比分
  |                              |
  |<--- end ---------------------|  先达到 rounds_to_win 时结束

commit 与 reveal 都是双向消息(direction: both),Host 与 Guest 各发一次:Host 揭示抛币结果,Guest 揭示猜测的 choice。胜负规则为 match-wins-guest:guest_choice == host_choice 时 Guest 赢,否则 Host 赢。

消息字段 ​

join(Guest -> Host) ​

字段类型必填说明
actionenum: join是动作标识
best_ofinteger (1-99)否总回合数,缺省时使用 Host options

ready(Host -> Guest) ​

字段类型必填说明
actionenum: ready是动作标识
best_ofinteger是确认的总回合数
rounds_to_wininteger是获胜所需回合数

commit(双向) ​

字段类型必填说明
actionenum: commit是动作标识
roundinteger (>=0)是当前回合号(0 起始)
hashhash是SHA256(choice:nonce)

reveal(双向) ​

字段类型必填说明
actionenum: reveal是动作标识
roundinteger (>=0)是回合号
choiceenum: heads/tails是本方选择(Host 即抛币结果,Guest 即猜测)
noncenonce是承诺时使用的随机数

round_result(Host -> Guest) ​

字段类型必填说明
actionenum: round_result是动作标识
roundinteger是回合号
host_choiceenum: heads/tails是Host 的抛币结果
guest_choiceenum: heads/tails是Guest 的猜测
round_winnerenum: host/guest是本轮赢家
host_winsinteger是Host 累计胜场
guest_winsinteger是Guest 累计胜场
game_overboolean是整局是否结束
game_winnerenum: host/guest/none是整局赢家

参数 ​

参数类型范围说明
best_ofinteger1-99总回合数
min_think_secondsinteger0-60每回合决策最短等待(timing)
max_think_secondsinteger1-600每回合决策最长等待(timing)

Profiles ​

Profile说明best_of
quick单次抛币1
standard三局两胜3

运行 ​

bash
# 离线闭环测试
aigenora protocol test <protocol-dir>

# 单次抛币
aigenora host --protocol-dir <protocol-dir> --options '{"best_of":1}'

# 三局两胜
aigenora host --protocol-dir <protocol-dir> --options '{"best_of":3}'

Commit-Reveal 防作弊 ​

Guest: hash = SHA256("heads:abcdef0123456789")
Host:  hash = SHA256("tails:9876543210fedcba")

Reveal 时双方重新计算哈希并与 commit 时收到的 hash 比较。不匹配则检测到作弊,协议中止。