Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
51 changes: 17 additions & 34 deletions crates/anvil-polkadot/tests/it/state_injector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -804,12 +804,9 @@ async fn test_set_immutable_storage() {
));

// Verify initial immutable value
let call_tx = TransactionRequest::default()
.from(alith_addr)
.to(contract_address)
.input(TransactionInput::both(
ImmutableStorage::getImmutableValueCall {}.abi_encode().into(),
));
let call_tx = TransactionRequest::default().from(alith_addr).to(contract_address).input(
TransactionInput::both(ImmutableStorage::getImmutableValueCall {}.abi_encode().into()),
);
let result = unwrap_response::<Bytes>(
node.eth_rpc(EthRequest::EthCall(call_tx.into(), None, None, None)).await.unwrap(),
)
Expand All @@ -820,12 +817,9 @@ async fn test_set_immutable_storage() {
);

// Verify initial immutable address
let call_tx = TransactionRequest::default()
.from(alith_addr)
.to(contract_address)
.input(TransactionInput::both(
ImmutableStorage::getImmutableAddressCall {}.abi_encode().into(),
));
let call_tx = TransactionRequest::default().from(alith_addr).to(contract_address).input(
TransactionInput::both(ImmutableStorage::getImmutableAddressCall {}.abi_encode().into()),
);
let result = unwrap_response::<Bytes>(
node.eth_rpc(EthRequest::EthCall(call_tx.into(), None, None, None)).await.unwrap(),
)
Expand All @@ -842,28 +836,20 @@ async fn test_set_immutable_storage() {
Account::from(subxt_signer::eth::dev::charleth()).address(),
));

let immutables = vec![
Bytes::from(new_value.abi_encode()),
Bytes::from(new_address.abi_encode()),
];
let immutables =
vec![Bytes::from(new_value.abi_encode()), Bytes::from(new_address.abi_encode())];

unwrap_response::<()>(
node.eth_rpc(EthRequest::SetImmutableStorageAt(
contract_address,
immutables,
))
.await
.unwrap(),
node.eth_rpc(EthRequest::SetImmutableStorageAt(contract_address, immutables))
.await
.unwrap(),
)
.unwrap();

// Verify new immutable value
let call_tx = TransactionRequest::default()
.from(alith_addr)
.to(contract_address)
.input(TransactionInput::both(
ImmutableStorage::getImmutableValueCall {}.abi_encode().into(),
));
let call_tx = TransactionRequest::default().from(alith_addr).to(contract_address).input(
TransactionInput::both(ImmutableStorage::getImmutableValueCall {}.abi_encode().into()),
);
let result = unwrap_response::<Bytes>(
node.eth_rpc(EthRequest::EthCall(call_tx.into(), None, None, None)).await.unwrap(),
)
Expand All @@ -874,12 +860,9 @@ async fn test_set_immutable_storage() {
);

// Verify new immutable address
let call_tx = TransactionRequest::default()
.from(alith_addr)
.to(contract_address)
.input(TransactionInput::both(
ImmutableStorage::getImmutableAddressCall {}.abi_encode().into(),
));
let call_tx = TransactionRequest::default().from(alith_addr).to(contract_address).input(
TransactionInput::both(ImmutableStorage::getImmutableAddressCall {}.abi_encode().into()),
);
let result = unwrap_response::<Bytes>(
node.eth_rpc(EthRequest::EthCall(call_tx.into(), None, None, None)).await.unwrap(),
)
Expand Down
10 changes: 3 additions & 7 deletions crates/anvil-polkadot/tests/it/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -465,16 +465,12 @@ fn load_contract_json(name: &str) -> Value {
let contract_path =
std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")).join(format!("test-data/{name}.json"));

serde_json::from_reader(std::io::BufReader::new(
std::fs::File::open(contract_path).unwrap(),
))
.unwrap()
serde_json::from_reader(std::io::BufReader::new(std::fs::File::open(contract_path).unwrap()))
.unwrap()
}

fn decode_hex_field(json: &Value, field: &str) -> Option<Vec<u8>> {
json.get(field)
.and_then(|v| v.as_str())
.map(|s| hex::decode(s).unwrap())
json.get(field).and_then(|v| v.as_str()).map(|s| hex::decode(s).unwrap())
}

pub fn get_contract_code(name: &str) -> ContractCode {
Expand Down
2 changes: 1 addition & 1 deletion crates/forge/tests/it/revive/cheat_etch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use rstest::rstest;
#[tokio::test(flavor = "multi_thread")]
async fn test_etch(#[case] runtime_mode: ReviveRuntimeMode) {
let runner: forge::MultiContractRunner = TEST_DATA_REVIVE.runner_revive(runtime_mode);
let filter = Filter::new(".*", "EtchTest", ".*/revive/EtchTest.t.sol");
let filter = Filter::new(".*", ".*", ".*/revive/EtchTest.t.sol");

TestConfig::with_filter(runner, filter).spec_id(SpecId::PRAGUE).run().await;
}
13 changes: 13 additions & 0 deletions crates/revive-strategy/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,23 @@ impl TestEnv {
contract_info
};
contract_info.code_hash = *contract_blob.code_hash();

// Calculate and set the base deposit for the contract.
let code_deposit = contract_blob.code_info().deposit();
let base_deposit = contract_info.update_base_deposit(code_deposit);

AccountInfo::<Runtime>::insert_contract(
&H160::from_slice(target.as_slice()),
contract_info,
);

// Fund the contract with the calculated base deposit.
let current_balance = Pallet::<Runtime>::evm_balance(&target_address);
let deposit_u256 = sp_core::U256::from(base_deposit);
if current_balance < deposit_u256 {
Pallet::<Runtime>::set_evm_balance(&target_address, deposit_u256)
.map_err(|_| <&str as Into<Error>>::into("Could not fund etched contract"))?;
}
Ok::<(), Error>(())
})?;
Ok(Default::default())
Expand Down
27 changes: 27 additions & 0 deletions testdata/default/revive/EtchTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,30 @@ contract EtchTest is DSTest {
assertEq(nested_call_result2, 3);
}
}

// Simple contract that writes to storage
contract StorageWriter {
uint256 public value;

function setValue(uint256 _value) external {
value = _value;
}
}

contract MinimalStorageDeposit is DSTest {
Vm constant vm = Vm(address(bytes20(uint160(uint256(keccak256("hevm cheat code"))))));

function testEtchAndCallWritesStorage() public {

bytes memory code = vm.getDeployedCode(
"EtchTest.t.sol:StorageWriter"
);

// Etch bytecode to a new unfunded address
address etched = address(0x1234567890123456789012345678901234567890);
vm.etch(etched, code);

StorageWriter(etched).setValue(42);
assertEq(StorageWriter(etched).value(), 42);
}
}
Loading