Skip to content

Commit

Permalink
Revert "evm: Move DFIIntrinsics contract, reserve space for additiona…
Browse files Browse the repository at this point in the history
…l contracts (#2374)" (#2381)

This reverts commit 0eef3af.
  • Loading branch information
Jouzo authored Aug 24, 2023
1 parent db41475 commit 2eff775
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 76 deletions.
14 changes: 3 additions & 11 deletions lib/ain-contracts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,22 +88,14 @@ pub fn dst20_address_from_token_id(token_id: u64) -> Result<H160> {
Ok(H160::from_str(&final_str)?)
}

pub fn intrinsics_address_from_id(id: u64) -> Result<H160> {
let number_str = format!("{:x}", id);
let padded_number_str = format!("{number_str:0>37}");
let final_str = format!("ff1{padded_number_str}");

Ok(H160::from_str(&final_str)?)
}

#[derive(Debug, PartialEq, Eq, Hash)]
pub enum Contracts {
Intrinsics,
CounterContract,
}

lazy_static! {
pub static ref CONTRACT_ADDRESSES: HashMap<Contracts, H160> = HashMap::from([(
Contracts::Intrinsics,
H160::from_str("0xff10000000000000000000000000000000000000").unwrap()
Contracts::CounterContract,
H160::from_str("0x0000000000000000000000000000000000000301").unwrap()
),]);
}
26 changes: 4 additions & 22 deletions lib/ain-evm/src/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,6 @@ impl EVMServices {
if is_evm_genesis_block {
// reserve DST20 namespace
self.reserve_dst20_namespace(&mut executor)?;
self.reserve_intrinsics_namespace(&mut executor)?;

let migration_txs = get_dst20_migration_txs(mnview_ptr)?;
queue.transactions.extend(migration_txs.into_iter());
Expand All @@ -202,13 +201,13 @@ impl EVMServices {
address,
storage,
bytecode,
} = EVMServices::intrinsics_contract(dvm_block_number, current_block_number)?;
} = EVMServices::counter_contract(dvm_block_number, current_block_number)?;
executor.deploy_contract(address, bytecode, storage)?;
} else {
// Ensure that state root changes by updating counter contract storage
let DeployContractInfo {
address, storage, ..
} = EVMServices::intrinsics_contract(dvm_block_number, current_block_number)?;
} = EVMServices::counter_contract(dvm_block_number, current_block_number)?;
executor.update_storage(address, storage)?;
}

Expand Down Expand Up @@ -462,11 +461,11 @@ impl EVMServices {
}

/// Returns address, bytecode and storage with incremented count for the counter contract
pub fn intrinsics_contract(
pub fn counter_contract(
dvm_block_number: u64,
evm_block_number: U256,
) -> Result<DeployContractInfo> {
let address = *CONTRACT_ADDRESSES.get(&Contracts::Intrinsics).unwrap();
let address = *CONTRACT_ADDRESSES.get(&Contracts::CounterContract).unwrap();
let bytecode = ain_contracts::get_counter_bytecode()?;
let count = SERVICES
.evm
Expand Down Expand Up @@ -642,23 +641,6 @@ impl EVMServices {

Ok(())
}

pub fn reserve_intrinsics_namespace(&self, executor: &mut AinExecutor) -> Result<()> {
let bytecode = ain_contracts::get_system_reserved_bytecode()?;
let addresses = (1..=127)
.map(|token_id| ain_contracts::intrinsics_address_from_id(token_id).unwrap())
.collect::<Vec<H160>>();

for address in addresses {
debug!(
"[reserve_intrinsics_namespace] Deploying address to {:#?}",
address
);
executor.deploy_contract(address, bytecode.clone().into(), Vec::new())?;
}

Ok(())
}
}

fn create_deploy_contract_tx(token_id: u64, base_fee: &U256) -> Result<(SignedTx, ReceiptV3)> {
Expand Down
45 changes: 2 additions & 43 deletions test/functional/feature_evm_dfi_intrinsics.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"""Test DFI intrinsics contract"""

import os
import json

from test_framework.test_framework import DefiTestFramework
from test_framework.util import assert_equal
Expand Down Expand Up @@ -44,32 +43,7 @@ def run_test(self):

# Activate EVM
node.setgov({"ATTRIBUTES": {"v0/params/feature/evm": "true"}})
node.generate(2)

# check reserved address space
reserved_bytecode = json.loads(
open(
f"{os.path.dirname(__file__)}/../../lib/ain-contracts/system_reserved/output/bytecode.json",
"r",
encoding="utf8",
).read()
)["object"]

for i in range(1, 128):
address = node.w3.to_checksum_address(generate_formatted_string(i))
assert (
self.nodes[0].w3.to_hex(self.nodes[0].w3.eth.get_code(address))
== reserved_bytecode
)

assert (
self.nodes[0].w3.to_hex(
self.nodes[0].w3.eth.get_code(
node.w3.to_checksum_address(generate_formatted_string(129))
)
)
!= reserved_bytecode
)
node.generate(1)

# check counter contract
# Temp. workaround
Expand All @@ -79,10 +53,7 @@ def run_test(self):
encoding="utf8",
).read()
counter_contract = node.w3.eth.contract(
address=node.w3.to_checksum_address(
"0xff10000000000000000000000000000000000000"
),
abi=abi,
address="0x0000000000000000000000000000000000000301", abi=abi
)

num_blocks = 5
Expand All @@ -109,17 +80,5 @@ def run_test(self):
assert_equal(len(state_roots), num_blocks)


def generate_formatted_string(input_number):
hex_representation = hex(input_number)[2:] # Convert to hex and remove '0x' prefix

if len(hex_representation) > 32:
hex_representation = hex_representation[:32] # Truncate if too long

padding = "0" * (37 - len(hex_representation))
formatted_string = f"0xff1{padding}{hex_representation}"

return formatted_string


if __name__ == "__main__":
DFIIntrinsicsTest().main()

0 comments on commit 2eff775

Please sign in to comment.