A Random Number Generator EOSIO Smart Contract that allows any number of oracles to contribute numbers that are used to generate a truly random number available on an EOSIO table.
The process works like such:
-
An oracle starts off by committing a hash of the number that they wish to contribute. As they contribute that hash, they will also specify the time block at which they will reveal the number. The time_block is an integer defined by the number of seconds that have passed since the Unix epoch in UTC.
For example, assuming that Bob wants to commit the number
123
, he would first commit the following transaction:cleos push action greymass commitnumber '["bob", "a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3", 3140484404]' -p bob@active
-
Once the current time in UTC belongs to the time block defined in #1, the number can be revealed by passing it as an argument. In the case of Bob, that would look like this:
cleos push action greymass revealnumber '["bob", 3140484404, 123]' -p bob@active
The smart contract will automatically validate that the number corresponds to the previously committed hash and that the current block time is also the one that was previously committed. If those conditions are met, then the number will be used in a XOR function to modify the number that is stored on the
randomnumber
table for the given time_block. This means that the number becomes truly random as soon as two independent oracles contribute to this smart contract. -
As soon as 60% of oracles commit a number for a given block, the
randomnumber
row will be flagged asvalid
.The
randomnumber
table can be queried for valid random numbers as such:cleos get table greymass greymass randomnumber --key-type i64 --index 2 -L "1"
Note:
- Steps 1 and 2 can be repeated at whatever interval you wish.
- At any time, the entry can be deleted with the erase function which in the case of our friend Bob would be done like this:
Eg.
cleos push action greymass erase '["bob"]' -p bob@active
There is a node process that can be used to ge
- Run
cp .env.sample .env
and change the placeholders for the credentials of the account that you wish to use. - Run
npm install
; - Start the oracle process with
Where
node scripts/oracle.js <interval_in_seconds>
<interval_in_seconds>
is the interval of time between each random number commits defined in seconds. It defaults to 2 seconds as that is the smallest cycle interval that we recommend. 1 second interval unfortunately doesn't work quite well cecause the oracle ends up missing blocks.