Skip to content

Commit 56f3acd

Browse files
authored
Add EVM to Revive bytecode migration (#344)
1 parent 4ef8860 commit 56f3acd

File tree

7 files changed

+204
-129
lines changed

7 files changed

+204
-129
lines changed

crates/forge/tests/cli/revive_vm.rs

Lines changed: 3 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,5 @@
11
use foundry_compilers::artifacts::EvmVersion;
22

3-
forgetest!(can_translate_balances_after_switch_to_pvm, |prj, cmd| {
4-
prj.insert_ds_test();
5-
prj.insert_vm();
6-
prj.insert_console();
7-
prj.add_source(
8-
"BalanceTranslationTest.t.sol",
9-
r#"
10-
import "./test.sol";
11-
import "./Vm.sol";
12-
import {console} from "./console.sol";
13-
14-
contract BalanceTranslationTest is DSTest {
15-
Vm constant vm = Vm(HEVM_ADDRESS);
16-
17-
function test_BalanceTranslationRevmPvm() public {
18-
uint256 amount = 10 ether;
19-
vm.deal(address(this), amount);
20-
21-
uint256 initialBalance = address(this).balance;
22-
assertEq(initialBalance, amount);
23-
24-
vm.pvm(true);
25-
26-
uint256 currentBalance = address(this).balance;
27-
console.log(initialBalance, currentBalance);
28-
assertEq(initialBalance, currentBalance);
29-
}
30-
}
31-
"#,
32-
)
33-
.unwrap();
34-
prj.update_config(|config| config.evm_version = EvmVersion::Cancun);
35-
36-
let res = cmd.args(["test", "--resolc", "-vvv"]).assert_success();
37-
res.stderr_eq(str![""]).stdout_eq(str![[r#"
38-
[COMPILING_FILES] with [SOLC_VERSION]
39-
[SOLC_VERSION] [ELAPSED]
40-
Compiler run successful!
41-
[COMPILING_FILES] with [RESOLC_VERSION]
42-
[RESOLC_VERSION] [ELAPSED]
43-
Compiler run successful!
44-
45-
Ran 1 test for src/BalanceTranslationTest.t.sol:BalanceTranslationTest
46-
[PASS] test_BalanceTranslationRevmPvm() ([GAS])
47-
Logs:
48-
10000000000000000000 10000000000000000000
49-
50-
Suite result: ok. 1 passed; 0 failed; 0 skipped; [ELAPSED]
51-
52-
Ran 1 test suite [ELAPSED]: 1 tests passed, 0 failed, 0 skipped (1 total tests)
53-
54-
"#]]);
55-
});
56-
573
forgetest!(counter_test, |prj, cmd| {
584
prj.insert_ds_test();
595
prj.insert_vm();
@@ -91,7 +37,6 @@ contract CounterTest is DSTest {
9137
Counter public counter;
9238
9339
function setUp() public {
94-
vm.pvm(true);
9540
counter = new Counter();
9641
counter.setNumber(5);
9742
assertEq(counter.number(), 5);
@@ -128,7 +73,7 @@ contract CounterTest is DSTest {
12873
.unwrap();
12974
prj.update_config(|config| config.evm_version = EvmVersion::Cancun);
13075

131-
let res = cmd.args(["test", "--resolc", "-vvv"]).assert();
76+
let res = cmd.args(["test", "--resolc", "-vvv", "--resolc-startup"]).assert();
13277
res.stderr_eq(str![""]).stdout_eq(str![[r#"
13378
[COMPILING_FILES] with [SOLC_VERSION]
13479
[SOLC_VERSION] [ELAPSED]
@@ -164,7 +109,6 @@ contract SetNonce is DSTest {
164109
Vm constant vm = Vm(HEVM_ADDRESS);
165110
166111
function test_SetNonce() public {
167-
vm.pvm(true);
168112
uint64 original = vm.getNonce(address(this));
169113
vm.setNonce(address(this), 64);
170114
uint64 newValue = vm.getNonce(address(this));
@@ -177,7 +121,7 @@ contract SetNonce is DSTest {
177121
.unwrap();
178122
prj.update_config(|config| config.evm_version = EvmVersion::Cancun);
179123

180-
let res = cmd.args(["test", "--resolc", "-vvv"]).assert_success();
124+
let res = cmd.args(["test", "--resolc", "-vvv", "--resolc-startup"]).assert_success();
181125
res.stderr_eq(str![""]).stdout_eq(str![[r#"
182126
[COMPILING_FILES] with [SOLC_VERSION]
183127
[SOLC_VERSION] [ELAPSED]
@@ -210,7 +154,6 @@ contract Roll is DSTest {
210154
Vm constant vm = Vm(HEVM_ADDRESS);
211155
212156
function test_Roll() public {
213-
vm.pvm(true);
214157
uint256 original = block.number;
215158
vm.roll(10);
216159
uint256 newValue = block.number;
@@ -222,7 +165,7 @@ contract Roll is DSTest {
222165
)
223166
.unwrap();
224167

225-
let res = cmd.args(["test", "--resolc", "-vvv"]).assert_success();
168+
let res = cmd.args(["test", "--resolc", "-vvv", "--resolc-startup"]).assert_success();
226169
res.stderr_eq(str![""]).stdout_eq(str![[r#"
227170
[COMPILING_FILES] with [SOLC_VERSION]
228171
[SOLC_VERSION] [ELAPSED]
@@ -255,7 +198,6 @@ contract Warp is DSTest {
255198
Vm constant vm = Vm(HEVM_ADDRESS);
256199
257200
function test_Warp() public {
258-
vm.pvm(true);
259201
uint256 original = block.timestamp;
260202
vm.warp(100);
261203
uint256 newValue = block.timestamp;
@@ -267,7 +209,6 @@ contract Warp is DSTest {
267209
)
268210
.unwrap();
269211

270-
cmd.env("RUST_LOG", "revive_strategy");
271212
let res = cmd.args(["test", "--resolc", "-vvv", "--resolc-startup"]).assert_success();
272213
res.stderr_eq(str![""]).stdout_eq(str![[r#"
273214
[COMPILING_FILES] with [SOLC_VERSION]
@@ -276,12 +217,6 @@ Compiler run successful!
276217
[COMPILING_FILES] with [RESOLC_VERSION]
277218
[RESOLC_VERSION] [ELAPSED]
278219
Compiler run successful!
279-
[..] INFO revive_strategy::cheatcodes: startup PVM migration initiated
280-
[..] INFO revive_strategy::cheatcodes: switching to PVM
281-
[..] INFO revive_strategy::cheatcodes: startup PVM migration completed
282-
[..] INFO revive_strategy::cheatcodes: cheatcode=pvmCall { enabled: true } using_pvm=true
283-
[..] INFO revive_strategy::cheatcodes: already in PVM
284-
[..] INFO revive_strategy::cheatcodes: cheatcode=warpCall { newTimestamp: 100 } using_pvm=true
285220
286221
Ran 1 test for src/Warp.t.sol:Warp
287222
[PASS] test_Warp() ([GAS])
@@ -319,7 +254,6 @@ function test_Balance() public {
319254
)
320255
.unwrap();
321256

322-
cmd.env("RUST_LOG", "revive_strategy");
323257
let res = cmd.args(["test", "--resolc", "-vvv", "--resolc-startup"]).assert_success();
324258
res.stderr_eq(str![""]).stdout_eq(str![[r#"
325259
[COMPILING_FILES] with [SOLC_VERSION]
@@ -328,13 +262,6 @@ Compiler run successful!
328262
[COMPILING_FILES] with [RESOLC_VERSION]
329263
[RESOLC_VERSION] [ELAPSED]
330264
Compiler run successful!
331-
[..] INFO revive_strategy::cheatcodes: startup PVM migration initiated
332-
[..] INFO revive_strategy::cheatcodes: switching to PVM
333-
[..] INFO revive_strategy::cheatcodes: startup PVM migration completed
334-
[..] INFO revive_strategy::cheatcodes: cheatcode=dealCall { account: 0x7fa9385be102ac3eac297483dd6233d62b3e1496, newBalance: 64000000000000000000 } using_pvm=true
335-
[..] INFO revive_strategy::cheatcodes: operation="get_balance" using_pvm=true target=0x7fa9385be102ac3eac297483dd6233d62b3e1496 balance=64000000000000000000
336-
[..] INFO revive_strategy::cheatcodes: cheatcode=dealCall { account: 0x7fa9385be102ac3eac297483dd6233d62b3e1496, newBalance: 65000000000000000000 } using_pvm=true
337-
[..] INFO revive_strategy::cheatcodes: operation="get_balance" using_pvm=true target=0x7fa9385be102ac3eac297483dd6233d62b3e1496 balance=65000000000000000000
338265
339266
Ran 1 test for src/Balance.t.sol:Balance
340267
[PASS] test_Balance() ([GAS])

crates/forge/tests/it/revive/migration.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,17 @@ async fn test_revive_nonce_migration() {
3333
#[tokio::test(flavor = "multi_thread")]
3434
async fn test_revive_bytecode_migration() {
3535
let runner = TEST_DATA_REVIVE.runner_revive();
36-
let filter = Filter::new("testBytecodeMigration", "EvmReviveMigrationTest", ".*/revive/.*");
36+
let filter =
37+
Filter::new("testBytecodeMigrationToEvm", "EvmReviveMigrationTest", ".*/revive/.*");
38+
39+
TestConfig::with_filter(runner, filter).spec_id(SpecId::SHANGHAI).run().await;
40+
}
41+
42+
#[tokio::test(flavor = "multi_thread")]
43+
async fn test_evm_bytecode_migration() {
44+
let runner = TEST_DATA_REVIVE.runner_revive();
45+
let filter =
46+
Filter::new("testBytecodeMigrationToRevive", "EvmReviveMigrationTest", ".*/revive/.*");
3747

3848
TestConfig::with_filter(runner, filter).spec_id(SpecId::SHANGHAI).run().await;
3949
}

crates/revive-env/src/lib.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
//! THIS IS WORK IN PROGRESS. It is not yet complete and may change in the future.
77
#![allow(clippy::disallowed_macros)]
88
use polkadot_sdk::{
9-
frame_system, pallet_balances,
9+
frame_support::traits::{OnGenesis, fungible::Mutate},
10+
frame_system::{self, Pallet},
11+
pallet_balances,
1012
pallet_revive::{self, AddressMapper},
1113
polkadot_runtime_common::BuildStorage,
1214
sp_core::H160,
@@ -53,7 +55,23 @@ impl ExtBuilder {
5355
pallet_revive::GenesisConfig::<Runtime>::default().assimilate_storage(&mut t).unwrap();
5456
let mut ext = sp_io::TestExternalities::new(t);
5557
ext.register_extension(KeystoreExt::new(MemoryKeystore::new()));
56-
ext.execute_with(|| System::set_block_number(0));
58+
ext.execute_with(|| {
59+
Pallet::<Runtime>::on_genesis();
60+
System::set_block_number(0);
61+
62+
// Set a large balance for pallet account to handle storage deposits during contract
63+
// migration Using a reasonable large value to avoid overflow when minting
64+
let pallet_account = pallet_revive::Pallet::<Runtime>::account_id();
65+
let large_balance: Balance = 1_000_000_000_000_000_000_000_000_000_u128;
66+
let _ = <Runtime as pallet_revive::Config>::Currency::mint_into(
67+
&pallet_account,
68+
large_balance,
69+
);
70+
71+
let _ = pallet_revive::Pallet::<Runtime>::map_account(
72+
frame_system::RawOrigin::Signed(pallet_account).into(),
73+
);
74+
});
5775
ext
5876
}
5977
}

crates/revive-env/src/runtime.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ parameter_types! {
6363
pub const DepositPerByte: Balance = 1;
6464
pub const DepositPerItem: Balance = 2;
6565
pub const CodeHashLockupDepositPercent: Perbill = Perbill::from_percent(0);
66+
pub const NativeToEthRatio: u32 = 1_000_000;
6667
pub BlockWeights: frame_system::limits::BlockWeights =
6768
frame_system::limits::BlockWeights::simple_max(
6869
Weight::from_parts(2u64 * WEIGHT_REF_TIME_PER_SECOND, u64::MAX),
@@ -83,6 +84,7 @@ impl pallet_revive::Config for Runtime {
8384
type InstantiateOrigin = EnsureSigned<AccountId32>;
8485
type CodeHashLockupDepositPercent = CodeHashLockupDepositPercent;
8586
type ChainId = ConstU64<420_420_420>;
87+
type NativeToEthRatio = ConstU32<1_000_000_000>;
8688
type FindAuthor = Self;
8789
}
8890

0 commit comments

Comments
 (0)