Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
14 changes: 14 additions & 0 deletions crates/forge/tests/it/revive/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,20 @@ async fn test_contract_deployment_in_different_modes(#[case] runtime_mode: Reviv
TestConfig::with_filter(runner, filter).spec_id(SpecId::PRAGUE).run().await;
}

#[rstest]
#[case::pvm(ReviveRuntimeMode::Pvm)]
#[case::evm(ReviveRuntimeMode::Evm)]
#[tokio::test(flavor = "multi_thread")]
async fn test_cheatcode_access_restriction(#[case] runtime_mode: ReviveRuntimeMode) {
let runner = TEST_DATA_REVIVE.runner_revive(runtime_mode);
let filter = Filter::new(
"testScriptCheatcodesShouldFailInPolkadotMode",
"CheatcodeAccessRestrictionTest",
".*/revive/.*",
);
TestConfig::with_filter(runner, filter).spec_id(SpecId::PRAGUE).run().await;
}

#[rstest]
#[case::pvm(ReviveRuntimeMode::Pvm)]
#[case::evm(ReviveRuntimeMode::Evm)]
Expand Down
9 changes: 9 additions & 0 deletions crates/revive-strategy/src/cheatcodes/mock_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::{

use alloy_primitives::{Address, Bytes, map::foldhash::HashMap, ruint::aliases::U256};
use foundry_cheatcodes::{Ecx, MockCallDataContext, MockCallReturnData};
use foundry_evm::constants::CHEATCODE_ADDRESS;
use polkadot_sdk::{
frame_system,
pallet_revive::{
Expand Down Expand Up @@ -81,6 +82,14 @@ impl MockHandler<Runtime> for MockHandlerImpl {
call_data: &[u8],
value_transferred: polkadot_sdk::pallet_revive::U256,
) -> Option<pallet_revive::ExecReturnValue> {
// Check if trying to call cheatcode address from pallet-revive
if Address::from_slice(callee.as_bytes()) == CHEATCODE_ADDRESS {
return Some(ExecReturnValue {
flags: ReturnFlags::REVERT,
data: b"Cheatcodes are not available in polkadot runtime.".to_vec(),
});
}

let mut mock_inner = self.inner.borrow_mut();
let ctx = MockCallDataContext {
calldata: call_data.to_vec().into(),
Expand Down
32 changes: 32 additions & 0 deletions testdata/default/revive/CheatcodeAccessRestriction.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity ^0.8.18;

import "ds-test/test.sol";
import "cheats/Vm.sol";

contract ScriptWithCheatcodes is DSTest {
address public derivedAddress;
uint256 private ownerKey;
Vm constant vm = Vm(HEVM_ADDRESS);

function setUp() public {
// This pattern tries to use vm.envUint and vm.addr from a non-test contract
ownerKey = vm.envUint("TEST_OWNER_KEY");
derivedAddress = vm.addr(ownerKey);
}
}

contract CheatcodeAccessRestrictionTest is DSTest {
ScriptWithCheatcodes script;
Vm constant vm = Vm(HEVM_ADDRESS);

function setUp() public {
vm.setEnv("TEST_OWNER_KEY", vm.toString(uint256(12345)));
}

function testScriptCheatcodesShouldFailInPolkadotMode() public {
script = new ScriptWithCheatcodes();
vm.expectRevert("Cheatcodes are not available in polkadot runtime.");
script.setUp();
}
}
Loading