implement block.number and block.timestamp

Signed-off-by: xermicus <cyrill@parity.io>
This commit is contained in:
xermicus
2024-05-07 18:03:17 +02:00
parent a7318f2ef6
commit 95ff85c6d1
20 changed files with 295 additions and 83 deletions
@@ -26,7 +26,7 @@ where
);
context
.build_load(offset, "calldata_value")
.map(|value| context.build_byte_swap(value))
.and_then(|value| context.build_byte_swap(value))
}
/// Translates the calldata size.
+35 -4
View File
@@ -4,6 +4,7 @@ use inkwell::values::BasicValue;
use crate::polkavm::context::Context;
use crate::polkavm::Dependency;
use crate::polkavm_const::runtime_api;
/// Translates the `gas_limit` instruction.
pub fn gas_limit<'ctx, D>(
@@ -47,22 +48,52 @@ where
/// Translates the `block_number` instruction.
pub fn block_number<'ctx, D>(
_context: &mut Context<'ctx, D>,
context: &mut Context<'ctx, D>,
) -> anyhow::Result<inkwell::values::BasicValueEnum<'ctx>>
where
D: Dependency + Clone,
{
todo!()
let (output_pointer, output_length_pointer) = context.build_stack_parameter(
revive_common::BIT_LENGTH_BLOCK_NUMBER,
"block_timestamp_output",
);
context.build_runtime_call(
runtime_api::BLOCK_NUMBER,
&[
output_pointer.to_int(context).into(),
output_length_pointer.to_int(context).into(),
],
);
context.build_load_word(
output_pointer,
revive_common::BIT_LENGTH_BLOCK_NUMBER,
"block_number",
)
}
/// Translates the `block_timestamp` instruction.
pub fn block_timestamp<'ctx, D>(
_context: &mut Context<'ctx, D>,
context: &mut Context<'ctx, D>,
) -> anyhow::Result<inkwell::values::BasicValueEnum<'ctx>>
where
D: Dependency + Clone,
{
todo!()
let (output_pointer, output_length_pointer) = context.build_stack_parameter(
revive_common::BIT_LENGTH_BLOCK_TIMESTAMP,
"block_timestamp_output",
);
context.build_runtime_call(
runtime_api::NOW,
&[
output_pointer.to_int(context).into(),
output_length_pointer.to_int(context).into(),
],
);
context.build_load_word(
output_pointer,
revive_common::BIT_LENGTH_BLOCK_TIMESTAMP,
"block_timestamp",
)
}
/// Translates the `block_hash` instruction.
@@ -27,5 +27,5 @@ where
],
);
Ok(context.build_byte_swap(context.build_load(output_pointer, "sha3_output")?))
context.build_byte_swap(context.build_load(output_pointer, "sha3_output")?)
}
@@ -23,42 +23,20 @@ pub fn value<'ctx, D>(
where
D: Dependency + Clone,
{
let output_pointer = context.build_alloca(context.value_type(), "output_pointer");
let output_pointer_casted = context.builder().build_ptr_to_int(
output_pointer.value,
context.xlen_type(),
"output_pointer_casted",
)?;
let output_length_pointer = context.build_alloca(context.xlen_type(), "output_len_pointer");
let output_length_pointer_casted = context.builder().build_ptr_to_int(
output_length_pointer.value,
context.xlen_type(),
"output_pointer_casted",
)?;
context.build_store(
output_length_pointer,
context.integer_const(
crate::polkavm::XLEN,
revive_common::BYTE_LENGTH_VALUE as u64,
),
)?;
let (output_pointer, output_length_pointer) =
context.build_stack_parameter(revive_common::BIT_LENGTH_VALUE, "value_transferred_output");
context.build_runtime_call(
runtime_api::VALUE_TRANSFERRED,
&[
output_pointer_casted.into(),
output_length_pointer_casted.into(),
output_pointer.to_int(context).into(),
output_length_pointer.to_int(context).into(),
],
);
let value = context.build_load(output_pointer, "transferred_value")?;
let value_extended = context.builder().build_int_z_extend(
value.into_int_value(),
context.word_type(),
"transferred_value_extended",
)?;
Ok(value_extended.as_basic_value_enum())
context.build_load_word(
output_pointer,
revive_common::BIT_LENGTH_VALUE,
"value_transferred",
)
}
/// Translates the `balance` instructions.