-
Notifications
You must be signed in to change notification settings - Fork 193
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(evm): randao support for evm #2151
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"_format": "hh-sol-artifact-1", | ||
"contractName": "TestRandom", | ||
"sourceName": "contracts/TestRandom.sol", | ||
"abi": [ | ||
{ | ||
"inputs": [], | ||
"name": "getRandom", | ||
"outputs": [ | ||
{ | ||
"internalType": "uint256", | ||
"name": "", | ||
"type": "uint256" | ||
} | ||
], | ||
"stateMutability": "view", | ||
"type": "function" | ||
} | ||
], | ||
"bytecode": "0x608060405234801561001057600080fd5b5060b58061001f6000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c8063aacc5a1714602d575b600080fd5b60336047565b604051603e91906066565b60405180910390f35b600044905090565b6000819050919050565b606081604f565b82525050565b6000602082019050607960008301846059565b9291505056fea264697066735822122021d2de67a73b1cbeefb199f56299f80c5f7aeb23a677b105ef78f6880f75491464736f6c63430008180033", | ||
"deployedBytecode": "0x6080604052348015600f57600080fd5b506004361060285760003560e01c8063aacc5a1714602d575b600080fd5b60336047565b604051603e91906066565b60405180910390f35b600044905090565b6000819050919050565b606081604f565b82525050565b6000602082019050607960008301846059565b9291505056fea264697066735822122021d2de67a73b1cbeefb199f56299f80c5f7aeb23a677b105ef78f6880f75491464736f6c63430008180033", | ||
"linkReferences": {}, | ||
"deployedLinkReferences": {} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// contracts/TestERC20.sol | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.24; | ||
|
||
contract TestRandom { | ||
|
||
function getRandom() public view returns (uint256) { | ||
return block.prevrandao; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Copyright (c) 2023-2024 Nibi, Inc. | ||
package keeper_test | ||
|
||
import ( | ||
"github.com/NibiruChain/nibiru/v2/x/evm/embeds" | ||
"github.com/NibiruChain/nibiru/v2/x/evm/evmtest" | ||
) | ||
|
||
// TestRandom tests the random value generation within the EVM. | ||
func (s *Suite) TestRandom() { | ||
deps := evmtest.NewTestDeps() | ||
deployResp, err := evmtest.DeployContract(&deps, embeds.SmartContract_TestRandom) | ||
s.Require().NoError(err) | ||
randomContractAddr := deployResp.ContractAddr | ||
|
||
// highjacked LoadERC20BigInt method as it perfectly fits the need of this test | ||
resp, err := deps.EvmKeeper.LoadERC20BigInt( | ||
deps.Ctx, embeds.SmartContract_TestRandom.ABI, randomContractAddr, "getRandom", | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Avoid repurposing ERC20 methods for non-ERC20 functionality The comment acknowledges that Consider creating a dedicated method for loading random values: - // highjacked LoadERC20BigInt method as it perfectly fits the need of this test
- resp, err := deps.EvmKeeper.LoadERC20BigInt(
+ resp, err := deps.EvmKeeper.LoadRandomValue(
deps.Ctx, embeds.SmartContract_TestRandom.ABI, randomContractAddr, "getRandom",
)
|
||
s.Require().NoError(err) | ||
s.Require().Greater(resp.Int64(), int64(0)) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Enhance test coverage for random value generation The current test only verifies that the random value is greater than zero. This is insufficient for testing randomness properties. Add comprehensive test cases: func (s *Suite) TestRandom() {
deps := evmtest.NewTestDeps()
deployResp, err := evmtest.DeployContract(&deps, embeds.SmartContract_TestRandom)
s.Require().NoError(err)
randomContractAddr := deployResp.ContractAddr
// Test multiple blocks to ensure values change
values := make(map[int64]bool)
for i := 0; i < 5; i++ {
resp, err := deps.EvmKeeper.LoadRandomValue(
deps.Ctx, embeds.SmartContract_TestRandom.ABI, randomContractAddr, "getRandom",
)
s.Require().NoError(err)
s.Require().Greater(resp.Int64(), int64(0))
// Verify uniqueness across blocks
s.Require().False(values[resp.Int64()], "Duplicate random value detected")
values[resp.Int64()] = true
// Advance block
deps.Ctx = deps.Ctx.WithBlockHeight(deps.Ctx.BlockHeight() + 1)
}
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Add documentation and security considerations for randomness generation
The
getRandom
function directly exposesblock.prevrandao
which has important security implications:Add comprehensive NatSpec documentation:
Consider implementing a more secure randomness mechanism:
📝 Committable suggestion