How can I get a pseudorandom number in a chaincode in Hyperledger Fabric?

476 Views Asked by At

Since chaincode has to be deterministic, is there any way to get same random number in all endorser nodes to implement something like a lottery? In Ethereum you can do something like that:

  function random() private view returns (uint) {
    return uint(keccak256(abi.encodePacked(block.difficulty, now, players)));
  }

By using the blocknumber, timestampt, block difficulty, gas , etc...But you dont have that in Hyperledger Fabric.

2

There are 2 best solutions below

0
On

You're quite correct that the deterministic nature of chaincode does cause issues with random numbers.

I'd look into doing this in one of two ways.

  1. You need to pass in the random number (or at least a seed) as part of the transaction request. You might want to send this in transient data so that's not recorded on the ledger.
  2. Pre-store the random numbers. i.e. generate a massive table of random numbers and put those into the ledger in some form of setup transaction. Maybe even into the private data collections. Work along those each time you need a number using a counter.

You can protect access to the set of generated numbers by various type of access control.

0
On

We can use the timestamp passed by proposer of transaction as the seed. Usage: stub.GetTxTimestamp()

// GetTxTimestamp returns the timestamp when the transaction was created. This
// is taken from the transaction ChannelHeader, therefore it will indicate the
// client's timestamp and will have the same value across all endorsers.
GetTxTimestamp() (*timestamp.Timestamp, error)