block.prevrandao
property, introduced in Solidity 0.8.18, allows contracts to access the randomness value derived from the previous block in the Ethereum Proof-of-Stake consensus mechanism. This feature is unavailable in versions earlier than 0.8.18.In the provided code from TwentyOne.sol
, the pragma directive specifies pragma solidity ^0.8.13;
. Since ^0.8.13
allows versions ranging from 0.8.13 to less than 0.9.0, it cannot utilize the block.prevrandao
property. Using this code with versions less than 0.8.18 will result in compilation errors.
Root Cause
The block.prevrandao
property is not supported in Solidity versions earlier than 0.8.18. If the compiler version is set to a lower version, any attempt to access this property will result in a compilation error.
Affected Code
The following pragma directive in TwentyOne.sol
is insufficient to guarantee compatibility with block.prevrandao
:
Behavior
In Solidity versions prior to 0.8.18, using block.prevrandao
will throw a compilation error:
In Solidity 0.8.18 and above, block.prevrandao
functions as expected.
Error of compiler with 0.8.13
Using a Solidity version earlier than 0.8.18 with block.prevrandao
will prevent the contract from compiling. This issue can result in:
Delays in development or deployment.
Misalignment with the intended Ethereum Proof-of-Stake mechanism.
Potential developer oversight leading to functionality not working as expected.
Static Analysis: Verified Solidity version in TwentyOne.sol
with a code review.
Solidity Documentation: Referred to the official Solidity documentation.
Testing Environment: Foundry
Update the Pragma Directive
Update the Solidity pragma directive in TwentyOne.sol
to ensure compatibility with block.prevrandao
. Replace the following line:
With:
Explicit Version Locking (Optional)
To avoid version conflicts in environments with multiple compiler versions, use an explicit version instead of a range:
Backward Compatibility Considerations
If block.prevrandao
is not strictly required, and you need backward compatibility with older versions, consider using alternative logic. Note that alternatives (e.g., blockhash
) may not offer secure randomness and are not suitable in all contexts.
Testing
Recompile and redeploy the contract using Solidity 0.8.18 or later. Confirm that the usage of block.prevrandao
behaves as expected.
The contest is live. Earn rewards by submitting a finding.
This is your time to appeal against judgements on your submissions.
Appeals are being carefully reviewed by our judges.