随机数发生器

(译者)随机数发生器作为一种极其重要的Oracle(预言机),是很多区块链应用必须的一个工具,EOS提供了一种方便的随机数生成方案。

游戏目的

本篇通过一个小游戏,双方比较谁的随机数大,来解释如何使用区块链随机数发生器。

操作步骤

第一步:产生密钥

在终端执行命令

  1. $ openssl rand 32 -hex
  2. $ 28349b1d4bcdc9905e4ef9719019e55743c84efa0c5e9a0b077f0b54fcd84905
  3. $ openssl rand 32 -hex
  4. $ 15fe76d25e124b08feb835f12e00a879bd15666a33786e64b655891fba7d6c12

第二步:生成SHA256密钥

  1. $ echo -n '28349b1d4bcdc9905e4ef9719019e55743c84efa0c5e9a0b077f0b54fcd84905' | xxd -r -p | sha256sum -b | awk '{print $1}'
  2. $ d533f24d6f28ddcef3f066474f7b8355383e485681ba8e793e037f5cf36e4883
  3. $ echo -n '15fe76d25e124b08feb835f12e00a879bd15666a33786e64b655891fba7d6c12' | xxd -r -p | sha256sum -b | awk '{print $1}'
  4. $ 50ed53fcdaf27f88d51ea4e835b1055efe779bb87e6cfdff47d28c88ffb27129

第三步:向智能合约发送密钥

  1. $ cleos push action chance submithash '[ "alice", "d533f24d6f28ddcef3f066474f7b8355383e485681ba8e793e037f5cf36e4883" ]' -p alice
  2. $ cleos push action chance submithash '[ "bob", "50ed53fcdaf27f88d51ea4e835b1055efe779bb87e6cfdff47d28c88ffb27129" ]' -p bob

第四步:定义数据结构

需要定义两个数据结构,第一个是player

  1. struct player {
  2. checksum256 hash;
  3. checksum256 secret;
  4. };

第二个是game

  1. struct game {
  2. uint64_t id;
  3. player player1;
  4. player player2;
  5. }

第五步:获取随机数,并比较

  1. // variable to get result from hashing all players hashes and secrets
  2. checksum256 result;
  3. // hash the contents in memory, starting at game.player1 and spanning for
  4. // sizeof(player)*2 bytes
  5. sha256( (char *)&game.player1, sizeof(player)*2, &result);
  6. // compares first and second 4 byte chunks in result to determine a winner
  7. int winner = result.hash[1] < result.hash[0] ? 0 : 1;
  8. // report appropriate winner
  9. if( winner ) {
  10. report_winner(game.player1);
  11. } else {
  12. report_winner(game.player2);
  13. }

注意:sha256是32位的,因此包括8个uint32位随机数。本例子中,获取了前2个作为随机数,当然,也可以获取hash[2]~hash[7]中的随机数。

EOS合约示例中有一个dice合约,下一节介绍dice合约使用。