Skip to content
This repository has been archived by the owner on Nov 18, 2022. It is now read-only.

fix: get code, code hash and code size from an unregistered address #167

Merged
merged 2 commits into from
Jun 28, 2022
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
12 changes: 12 additions & 0 deletions c/polyjuice.h
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,10 @@ size_t get_code_size(struct evmc_host_context* context,
uint32_t account_id = 0;
int ret = load_account_id_by_eth_address(context->gw_ctx,
address->bytes, &account_id);
if (ret == GW_ERROR_NOT_FOUND) {
ckb_debug("END get_code_size");
return 0;
}
if (ret != 0) {
ckb_debug("get contract account id failed");
return 0;
Expand All @@ -441,6 +445,10 @@ evmc_bytes32 get_code_hash(struct evmc_host_context* context,
uint32_t account_id = 0;
int ret = load_account_id_by_eth_address(context->gw_ctx,
address->bytes, &account_id);
if (ret == GW_ERROR_NOT_FOUND) {
ckb_debug("END get_code_hash");
return hash;
}
if (ret != 0) {
ckb_debug("get contract account id failed");
context->error_code = ret;
Expand Down Expand Up @@ -489,6 +497,10 @@ size_t copy_code(struct evmc_host_context* context, const evmc_address* address,
uint32_t account_id = 0;
int ret = load_account_id_by_eth_address(context->gw_ctx,
address->bytes, &account_id);
if (ret == GW_ERROR_NOT_FOUND) {
ckb_debug("END copy_code");
return 0;
}
if (ret != 0) {
ckb_debug("get contract account id failed");
context->error_code = ret;
Expand Down
2 changes: 1 addition & 1 deletion c/polyjuice_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ int load_script_hash_by_eth_address(gw_context_t *ctx,
if (_is_zero_hash(script_hash)) {
return GW_ERROR_NOT_FOUND;
}
ckb_debug("load_script_hash_by_eth_address success");
return 0;
ckb_debug("load_script_hash_by_eth_address success");
}
magicalne marked this conversation as resolved.
Show resolved Hide resolved

int load_eth_address_by_script_hash(gw_context_t *ctx,
Expand Down
158 changes: 158 additions & 0 deletions polyjuice-tests/src/test_cases/absent_address.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
use ckb_vm::Bytes;
use gw_common::state::State;
use gw_store::{chain_view::ChainView, traits::chain_store::ChainStore};
use gw_types::{
packed::RawL2Transaction,
prelude::{Builder, Entity, Pack},
};

use crate::helper::{
create_block_producer, deploy, new_block_info, setup, MockContractInfo, PolyjuiceArgsBuilder,
CREATOR_ACCOUNT_ID, L2TX_MAX_CYCLES,
};

const INIT_CODE: &str = include_str!("./evm-contracts/AbsentAddress.bin");
#[test]
fn absent_address_test() -> anyhow::Result<()> {
let (store, mut state, generator) = setup();
let block_producer = create_block_producer(&mut state);

let from_eth_address = [1u8; 20];
let (from_id, _from_script_hash) =
crate::helper::create_eth_eoa_account(&mut state, &from_eth_address, 200000000u64.into());

// Deploy Contract
let _run_result = deploy(
&generator,
&store,
&mut state,
CREATOR_ACCOUNT_ID,
from_id,
INIT_CODE,
1284600,
0,
block_producer.clone(),
0,
);

let contract_account = MockContractInfo::create(&from_eth_address, 0);
let new_account_id = state
.get_account_id_by_script_hash(&contract_account.script_hash)?
.unwrap();

// 0xdB81D2b8154A10C6f25bC2a9225F403D954D0B65 is an unregistered eth_address.
// call getBalance("0xdB81D2b8154A10C6f25bC2a9225F403D954D0B65")
let input =
hex::decode("f8b2cb4f000000000000000000000000db81d2b8154a10c6f25bc2a9225f403d954d0b65")?;
let block_info = new_block_info(block_producer.clone(), 1, 0);
let args = PolyjuiceArgsBuilder::default()
.gas_limit(228060)
.gas_price(1)
.value(0)
.input(&input)
.build();
let raw_tx = RawL2Transaction::new_builder()
.from_id(from_id.pack())
.to_id(new_account_id.pack())
.args(Bytes::from(args).pack())
.build();
let db = store.begin_transaction();
let tip_block_hash = db.get_tip_block_hash().unwrap();
let run_result = generator
.execute_transaction(
&ChainView::new(&db, tip_block_hash),
&state,
&block_info,
&raw_tx,
L2TX_MAX_CYCLES,
)
.expect("Call getBalance");
assert_eq!(run_result.return_data.as_ref(), &[0u8; 32]);

//call getCodeSize("0xdB81D2b8154A10C6f25bC2a9225F403D954D0B65")
let input =
hex::decode("b51c4f96000000000000000000000000db81d2b8154a10c6f25bc2a9225f403d954d0b65")?;
let block_info = new_block_info(block_producer.clone(), 1, 0);
let args = PolyjuiceArgsBuilder::default()
.gas_limit(228060)
.gas_price(1)
.value(0)
.input(&input)
.build();
let raw_tx = RawL2Transaction::new_builder()
.from_id(from_id.pack())
.to_id(new_account_id.pack())
.args(Bytes::from(args).pack())
.build();
let db = store.begin_transaction();
let tip_block_hash = db.get_tip_block_hash().unwrap();
let run_result = generator
.execute_transaction(
&ChainView::new(&db, tip_block_hash),
&state,
&block_info,
&raw_tx,
L2TX_MAX_CYCLES,
)
.expect("Call getCodeSize");
assert_eq!(run_result.return_data.as_ref(), &[0u8; 32]);

//call getCodeHash("0xdB81D2b8154A10C6f25bC2a9225F403D954D0B65")
let input =
hex::decode("81ea4408000000000000000000000000db81d2b8154a10c6f25bc2a9225f403d954d0b65")?;
let block_info = new_block_info(block_producer.clone(), 1, 0);
let args = PolyjuiceArgsBuilder::default()
.gas_limit(228060)
.gas_price(1)
.value(0)
.input(&input)
.build();
let raw_tx = RawL2Transaction::new_builder()
.from_id(from_id.pack())
.to_id(new_account_id.pack())
.args(Bytes::from(args).pack())
.build();
let db = store.begin_transaction();
let tip_block_hash = db.get_tip_block_hash().unwrap();
let run_result = generator
.execute_transaction(
&ChainView::new(&db, tip_block_hash),
&state,
&block_info,
&raw_tx,
L2TX_MAX_CYCLES,
)
.expect("Call getCodeHash");
assert_eq!(run_result.return_data.as_ref(), &[0u8; 32]);

//call getCode("0xdB81D2b8154A10C6f25bC2a9225F403D954D0B65")
let input =
hex::decode("7e105ce2000000000000000000000000db81d2b8154a10c6f25bc2a9225f403d954d0b65")?;
let block_info = new_block_info(block_producer, 1, 0);
let args = PolyjuiceArgsBuilder::default()
.gas_limit(228060)
.gas_price(1)
.value(0)
.input(&input)
.build();
let raw_tx = RawL2Transaction::new_builder()
.from_id(from_id.pack())
.to_id(new_account_id.pack())
.args(Bytes::from(args).pack())
.build();
let db = store.begin_transaction();
let tip_block_hash = db.get_tip_block_hash().unwrap();
let run_result = generator
.execute_transaction(
&ChainView::new(&db, tip_block_hash),
&state,
&block_info,
&raw_tx,
L2TX_MAX_CYCLES,
)
.expect("Call getCode");
let mut target = [0u8; 64];
Flouse marked this conversation as resolved.
Show resolved Hide resolved
target[31] = 32;
assert_eq!(run_result.return_data.as_ref(), &target);
Ok(())
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"inputs":[],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[{"internalType":"address","name":"addr","type":"address"}],"name":"getBalance","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"addr","type":"address"}],"name":"getCode","outputs":[{"internalType":"bytes","name":"","type":"bytes"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"addr","type":"address"}],"name":"getCodeHash","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"addr","type":"address"}],"name":"getCodeSize","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"}]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
608060405234801561001057600080fd5b50610390806100206000396000f3fe608060405234801561001057600080fd5b506004361061004c5760003560e01c80637e105ce21461005157806381ea440814610081578063b51c4f96146100b1578063f8b2cb4f146100e1575b600080fd5b61006b600480360381019061006691906101c5565b610111565b6040516100789190610260565b60405180910390f35b61009b600480360381019061009691906101c5565b61014d565b6040516100a89190610245565b60405180910390f35b6100cb60048036038101906100c691906101c5565b61016e565b6040516100d89190610282565b60405180910390f35b6100fb60048036038101906100f691906101c5565b61018f565b6040516101089190610282565b60405180910390f35b60608173ffffffffffffffffffffffffffffffffffffffff16803b806020016040519081016040528181526000908060200190933c9050919050565b60008173ffffffffffffffffffffffffffffffffffffffff163f9050919050565b60008173ffffffffffffffffffffffffffffffffffffffff163b9050919050565b60008173ffffffffffffffffffffffffffffffffffffffff16319050919050565b6000813590506101bf81610343565b92915050565b6000602082840312156101d757600080fd5b60006101e5848285016101b0565b91505092915050565b6101f7816102cb565b82525050565b60006102088261029d565b61021281856102a8565b93506102228185602086016102ff565b61022b81610332565b840191505092915050565b61023f816102f5565b82525050565b600060208201905061025a60008301846101ee565b92915050565b6000602082019050818103600083015261027a81846101fd565b905092915050565b60006020820190506102976000830184610236565b92915050565b600081519050919050565b600082825260208201905092915050565b60006102c4826102d5565b9050919050565b6000819050919050565b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b6000819050919050565b60005b8381101561031d578082015181840152602081019050610302565b8381111561032c576000848401525b50505050565b6000601f19601f8301169050919050565b61034c816102b9565b811461035757600080fd5b5056fea2646970667358221220277d2dd1bc317b2709aed8a9fd981cc4478e14df998b3ab21860373233439cc364736f6c63430008040033
21 changes: 21 additions & 0 deletions polyjuice-tests/src/test_cases/evm-contracts/AbsentAddress.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
pragma solidity ^0.8.4;

contract AbsentAddress {
constructor() {}

function getBalance(address addr) public view returns (uint256) {
return addr.balance;
}

function getCode(address addr) public view returns (bytes memory) {
return addr.code;
}

function getCodeHash(address addr) public view returns (bytes32) {
return addr.codehash;
}

function getCodeSize(address addr) public view returns (uint256) {
return addr.code.length;
}
}
1 change: 1 addition & 0 deletions polyjuice-tests/src/test_cases/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub(crate) mod invalid_sudt_erc20_proxy;
pub(crate) mod sudt_erc20_proxy;
pub(crate) mod sudt_erc20_proxy_attack_allowance;

mod absent_address;
mod address_collision;
mod beacon_proxy;
mod error;
Expand Down