Skip to content
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

Merged
merged 3 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- [#2144](https://github.com/NibiruChain/nibiru/pull/2144) - feat(token-registry): Implement strongly typed Nibiru Token Registry and generation command
- [#2145](https://github.com/NibiruChain/nibiru/pull/2145) - chore(token-registry): add xNIBI Astrovault LST to registry
- [#2147](https://github.com/NibiruChain/nibiru/pull/2147) - fix(simapp): manually add x/vesting Cosmos-SDK module types to the codec in simulation tests since they are expected by default
- [#2151](https://github.com/NibiruChain/nibiru/pull/2151) - feat(evm): randao support for evm

#### Nibiru EVM | Before Audit 2 - 2024-12-06

Expand Down
24 changes: 24 additions & 0 deletions x/evm/embeds/artifacts/contracts/TestRandom.sol/TestRandom.json
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": {}
}
10 changes: 10 additions & 0 deletions x/evm/embeds/contracts/TestRandom.sol
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;
}
Comment on lines +7 to +9
Copy link
Contributor

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 exposes block.prevrandao which has important security implications:

  1. The value is predictable by validators
  2. Can be manipulated in certain scenarios
  3. Should not be used directly for high-stakes randomness

Add comprehensive NatSpec documentation:

+    /// @notice Retrieves the previous block's RANDAO value
+    /// @dev WARNING: This randomness source has limitations:
+    ///      - Predictable by validators
+    ///      - Should not be used for high-stakes applications
+    ///      - Consider using a commit-reveal scheme for better unpredictability
+    /// @return uint256 The previous block's RANDAO value
     function getRandom() public view returns (uint256) {
         return block.prevrandao;
     }

Consider implementing a more secure randomness mechanism:

  1. Use a commit-reveal scheme
  2. Combine multiple entropy sources
  3. Consider using VRF (Verifiable Random Function) for critical applications
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
function getRandom() public view returns (uint256) {
return block.prevrandao;
}
/// @notice Retrieves the previous block's RANDAO value
/// @dev WARNING: This randomness source has limitations:
/// - Predictable by validators
/// - Should not be used for high-stakes applications
/// - Consider using a commit-reveal scheme for better unpredictability
/// @return uint256 The previous block's RANDAO value
function getRandom() public view returns (uint256) {
return block.prevrandao;
}

}
8 changes: 8 additions & 0 deletions x/evm/embeds/embeds.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ var (
testInfiniteRecursionERC20Json []byte
//go:embed artifacts/contracts/TestERC20TransferWithFee.sol/TestERC20TransferWithFee.json
testERC20TransferWithFee []byte
//go:embed artifacts/contracts/TestRandom.sol/TestRandom.json
testRandom []byte
)

var (
Expand Down Expand Up @@ -134,6 +136,11 @@ var (
Name: "TestERC20TransferWithFee.sol",
EmbedJSON: testERC20TransferWithFee,
}
// SmartContract_TestRandom is a test contract which tests random function
SmartContract_TestRandom = CompiledEvmContract{
Name: "TestRandom.sol",
EmbedJSON: testRandom,
}
)

func init() {
Expand All @@ -150,6 +157,7 @@ func init() {
SmartContract_TestPrecompileSelfCallRevert.MustLoad()
SmartContract_TestInfiniteRecursionERC20.MustLoad()
SmartContract_TestERC20TransferWithFee.MustLoad()
SmartContract_TestRandom.MustLoad()
}

type CompiledEvmContract struct {
Expand Down
1 change: 1 addition & 0 deletions x/evm/embeds/embeds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ func TestLoadContracts(t *testing.T) {
embeds.SmartContract_TestERC20TransferThenPrecompileSend.MustLoad()
embeds.SmartContract_TestInfiniteRecursionERC20.MustLoad()
embeds.SmartContract_TestERC20TransferWithFee.MustLoad()
embeds.SmartContract_TestRandom.MustLoad()
})
}
7 changes: 6 additions & 1 deletion x/evm/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keeper

import (
"context"
"encoding/binary"
"encoding/json"
"fmt"
"math/big"
Expand Down Expand Up @@ -121,6 +122,10 @@ func (k *Keeper) NewEVM(
tracer vm.EVMLogger,
stateDB vm.StateDB,
) *vm.EVM {
pseudoRandomBytes := make([]byte, 8)
binary.BigEndian.PutUint64(pseudoRandomBytes, uint64(ctx.BlockHeader().Time.UnixNano()))
pseudoRandom := crypto.Keccak256Hash(append(pseudoRandomBytes, ctx.BlockHeader().LastCommitHash...))

blockCtx := vm.BlockContext{
CanTransfer: core.CanTransfer,
Transfer: core.Transfer,
Expand All @@ -131,7 +136,7 @@ func (k *Keeper) NewEVM(
Time: big.NewInt(ctx.BlockHeader().Time.Unix()),
Difficulty: big.NewInt(0), // unused. Only required in PoW context
BaseFee: evmConfig.BaseFeeWei,
Random: nil, // not supported
Random: &pseudoRandom,
}

txCtx := core.NewEVMTxContext(msg)
Expand Down
22 changes: 22 additions & 0 deletions x/evm/keeper/random_test.go
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",
)
Copy link
Contributor

Choose a reason for hiding this comment

The 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 LoadERC20BigInt is being "hijacked" for this test. This is a code smell that could lead to maintenance issues.

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",
 	)

Committable suggestion skipped: line range outside the PR's diff.

s.Require().NoError(err)
s.Require().Greater(resp.Int64(), int64(0))
}
Copy link
Contributor

Choose a reason for hiding this comment

The 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)
    }
}

Loading