implement address and msg.sender

Signed-off-by: Cyrill Leutwiler <bigcyrill@hotmail.com>
This commit is contained in:
Cyrill Leutwiler
2024-05-08 16:10:48 +02:00
parent ea63991617
commit 864e40901f
6 changed files with 58 additions and 10 deletions
+2 -2
View File
@@ -125,8 +125,8 @@ pub struct PreparedEvm {
pub fn prepare(code: Vec<u8>, data: Vec<u8>) -> PreparedEvm {
let state = RuntimeState {
context: Context {
address: H160::default(),
caller: H160::default(),
address: H160::from_slice(&[1u8; 20]),
caller: H160::from_slice(&[2u8; 20]),
apparent_value: U256::default(),
},
transaction_context: TransactionContext {
@@ -10,8 +10,12 @@ pub static DEPLOY: &str = "deploy";
/// Useful for configuring common attributes and linkage.
pub static EXPORTS: [&str; 2] = [CALL, DEPLOY];
pub static ADDRESS: &str = "address";
pub static BLOCK_NUMBER: &str = "block_number";
pub static CALLER: &str = "caller";
pub static GET_STORAGE: &str = "get_storage";
pub static HASH_KECCAK_256: &str = "hash_keccak_256";
@@ -621,9 +621,9 @@ where
) -> (Pointer<'ctx>, Pointer<'ctx>) {
let buffer_pointer = self.build_alloca(self.integer_type(bit_length), name);
let symbol = match bit_length {
256 => GLOBAL_I256_SIZE,
128 => GLOBAL_I128_SIZE,
64 => GLOBAL_I64_SIZE,
revive_common::BIT_LENGTH_WORD => GLOBAL_I256_SIZE,
revive_common::BIT_LENGTH_VALUE => GLOBAL_I128_SIZE,
revive_common::BIT_LENGTH_BLOCK_NUMBER => GLOBAL_I64_SIZE,
_ => unreachable!("invalid stack parameter bit width: {bit_length}"),
};
let length_pointer = self.get_global(symbol).expect("should be declared");
@@ -163,3 +163,41 @@ where
.build_int_z_extend(heap_size, context.word_type(), "heap_size_extended")?
.as_basic_value_enum())
}
/// Translates the `address` instruction.
pub fn address<'ctx, D>(
context: &mut Context<'ctx, D>,
) -> anyhow::Result<inkwell::values::BasicValueEnum<'ctx>>
where
D: Dependency + Clone,
{
let (output_pointer, output_length_pointer) =
context.build_stack_parameter(revive_common::BIT_LENGTH_WORD, "address_output");
context.build_runtime_call(
runtime_api::ADDRESS,
&[
output_pointer.to_int(context).into(),
output_length_pointer.to_int(context).into(),
],
);
context.build_load(output_pointer, "address")
}
/// Translates the `caller` instruction.
pub fn caller<'ctx, D>(
context: &mut Context<'ctx, D>,
) -> anyhow::Result<inkwell::values::BasicValueEnum<'ctx>>
where
D: Dependency + Clone,
{
let (output_pointer, output_length_pointer) =
context.build_stack_parameter(revive_common::BIT_LENGTH_WORD, "caller_output");
context.build_runtime_call(
runtime_api::CALLER,
&[
output_pointer.to_int(context).into(),
output_length_pointer.to_int(context).into(),
],
);
context.build_load(output_pointer, "caller")
}
@@ -1134,8 +1134,12 @@ where
.map(Some)
}
InstructionName::ADDRESS => todo!(),
InstructionName::CALLER => todo!(),
InstructionName::ADDRESS => {
revive_llvm_context::polkavm_evm_contract_context::address(context).map(Some)
}
InstructionName::CALLER => {
revive_llvm_context::polkavm_evm_contract_context::caller(context).map(Some)
}
InstructionName::CALLVALUE => {
revive_llvm_context::polkavm_evm_ether_gas::value(context).map(Some)
@@ -907,10 +907,12 @@ impl FunctionCall {
Ok(Some(arguments[0]))
}
Name::Address | Name::Caller => {
Ok(Some(context.integer_const(256, 0).as_basic_value_enum()))
Name::Address => {
revive_llvm_context::polkavm_evm_contract_context::address(context).map(Some)
}
Name::Caller => {
revive_llvm_context::polkavm_evm_contract_context::caller(context).map(Some)
}
Name::CallValue => revive_llvm_context::polkavm_evm_ether_gas::value(context).map(Some),
Name::Gas => revive_llvm_context::polkavm_evm_ether_gas::gas(context).map(Some),
Name::Balance => {