This repository has been archived by the owner on Nov 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: get code, code hash and code size from an unregistered address (#…
…167) * fix: get code, code hash and code size from an unregistered address Co-authored-by: Flouse <[email protected]>
- Loading branch information
Showing
7 changed files
with
195 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
target[31] = 32; | ||
assert_eq!(run_result.return_data.as_ref(), &target); | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"}] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
608060405234801561001057600080fd5b50610390806100206000396000f3fe608060405234801561001057600080fd5b506004361061004c5760003560e01c80637e105ce21461005157806381ea440814610081578063b51c4f96146100b1578063f8b2cb4f146100e1575b600080fd5b61006b600480360381019061006691906101c5565b610111565b6040516100789190610260565b60405180910390f35b61009b600480360381019061009691906101c5565b61014d565b6040516100a89190610245565b60405180910390f35b6100cb60048036038101906100c691906101c5565b61016e565b6040516100d89190610282565b60405180910390f35b6100fb60048036038101906100f691906101c5565b61018f565b6040516101089190610282565b60405180910390f35b60608173ffffffffffffffffffffffffffffffffffffffff16803b806020016040519081016040528181526000908060200190933c9050919050565b60008173ffffffffffffffffffffffffffffffffffffffff163f9050919050565b60008173ffffffffffffffffffffffffffffffffffffffff163b9050919050565b60008173ffffffffffffffffffffffffffffffffffffffff16319050919050565b6000813590506101bf81610343565b92915050565b6000602082840312156101d757600080fd5b60006101e5848285016101b0565b91505092915050565b6101f7816102cb565b82525050565b60006102088261029d565b61021281856102a8565b93506102228185602086016102ff565b61022b81610332565b840191505092915050565b61023f816102f5565b82525050565b600060208201905061025a60008301846101ee565b92915050565b6000602082019050818103600083015261027a81846101fd565b905092915050565b60006020820190506102976000830184610236565b92915050565b600081519050919050565b600082825260208201905092915050565b60006102c4826102d5565b9050919050565b6000819050919050565b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b6000819050919050565b60005b8381101561031d578082015181840152602081019050610302565b8381111561032c576000848401525b50505050565b6000601f19601f8301169050919050565b61034c816102b9565b811461035757600080fd5b5056fea2646970667358221220277d2dd1bc317b2709aed8a9fd981cc4478e14df998b3ab21860373233439cc364736f6c63430008040033 |
21 changes: 21 additions & 0 deletions
21
polyjuice-tests/src/test_cases/evm-contracts/AbsentAddress.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters