Naive random implementation in smart contracts and its flaws.

By akohad Jan17,2023

[ad_1]

Why there is no random implemented into solidity?

Each node produces different results based on a random number and no decision can be made

Randomness by obscurity

function random(string memory input) internal pure returns (uint256) {
return uint256(keccak256(abi.encodePacked(input)));
}

function getOS(uint256 tokenId) public view returns (string memory) {
return pluck(tokenId, "OS", osses);
}

//....

function pluck(uint256 tokenId, string memory keyPrefix, string[] memory sourceArray) internal pure returns (string memory) {
uint256 rand = random(string(abi.encodePacked(keyPrefix, toString(tokenId))));
string memory output = sourceArray[rand % sourceArray.length];
return output;
}

Randomness based on block hash

Despite popular opinion, it is not true that only miners can attack random based on block hash
  1. Increasing odds (easy & ineffective)
using a smart contract to gain benefit

Is there any way to improve randomness in smart contracts?

  1. Decentralized oracle i.e. chainlink VRF. This approach is pretty complex and requires much more implementation than any block hash-based solution plus it costs gas and makes the whole experience even more async which again might require additional implementation of UI/UX.
  2. Commit and reveal of results — there is a certain number of slots provided with hidden content. At some point in time users pick their slots and the content is revealed by the owner later on. This experience is worse as one needs to wait for the publication of the results. It may also decrease users’ engagement in case buying the next slot is possible.

Summary

New to trading? Try crypto trading bots or copy trading on best crypto exchanges

[ad_2]

Source link

By akohad

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *