sync runtime with the pallet (#54)

This commit is contained in:
Cyrill Leutwiler
2024-09-24 09:14:36 +02:00
committed by GitHub
parent f621971a13
commit 287272b789
11 changed files with 588 additions and 500 deletions
@@ -19,6 +19,8 @@ pub mod imports {
pub static BLOCK_NUMBER: &str = "block_number";
pub static CHAIN_ID: &str = "chain_id";
pub static CALL: &str = "call";
pub static CALLER: &str = "caller";
@@ -47,12 +49,13 @@ pub mod imports {
/// All imported runtime API symbols.
/// Useful for configuring common attributes and linkage.
pub static IMPORTS: [&str; 16] = [
pub static IMPORTS: [&str; 17] = [
ADDRESS,
BALANCE,
BLOCK_NUMBER,
CALL,
CALLER,
CHAIN_ID,
CODE_SIZE,
DEPOSIT_EVENT,
GET_STORAGE,
@@ -23,11 +23,11 @@ impl Entry {
/// The number of mandatory arguments.
pub const MANDATORY_ARGUMENTS_COUNT: usize = 2;
/// Reserve 1mb for calldata.
pub const MAX_CALLDATA_SIZE: usize = 1024 * 1024;
/// Reserve 1kb for calldata.
pub const MAX_CALLDATA_SIZE: usize = 1024;
/// Reserve 1mb for returndata.
pub const MAX_RETURNDATA_SIZE: usize = 1024 * 1024;
/// Reserve 1kb for returndata.
pub const MAX_RETURNDATA_SIZE: usize = 1024;
/// Initializes the global variables.
/// The pointers are not initialized, because it's not possible to create a null pointer.
@@ -950,6 +950,22 @@ where
.left()
}
/// Builds a call to the runtime API `import`, where `import` is a "getter" API.
/// This means that the supplied API method just writes back a single word.
/// `import` is thus expect to have a single parameter, the 32 bytes output buffer,
/// and no return value.
pub fn build_runtime_call_to_getter(
&self,
import: &'static str,
) -> anyhow::Result<inkwell::values::BasicValueEnum<'ctx>>
where
D: Dependency + Clone,
{
let pointer = self.build_alloca_at_entry(self.word_type(), &format!("{import}_output"));
self.build_runtime_call(import, &[pointer.to_int(self).into()]);
self.build_load(pointer, import)
}
/// Builds a call.
pub fn build_call(
&self,
+9 -53
View File
@@ -53,22 +53,7 @@ pub fn block_number<'ctx, D>(
where
D: Dependency + Clone,
{
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::imports::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",
)
context.build_runtime_call_to_getter(runtime_api::imports::BLOCK_NUMBER)
}
/// Translates the `block_timestamp` instruction.
@@ -78,22 +63,7 @@ pub fn block_timestamp<'ctx, D>(
where
D: Dependency + Clone,
{
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::imports::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",
)
context.build_runtime_call_to_getter(runtime_api::imports::NOW)
}
/// Translates the `block_hash` instruction.
@@ -171,16 +141,15 @@ pub fn address<'ctx, D>(
where
D: Dependency + Clone,
{
let (output_pointer, output_length_pointer) =
context.build_stack_parameter(revive_common::BIT_LENGTH_ETH_ADDRESS, "address_output");
let pointer = context.build_alloca_at_entry(
context.integer_type(revive_common::BIT_LENGTH_ETH_ADDRESS),
"address_output",
);
context.build_runtime_call(
runtime_api::imports::ADDRESS,
&[
output_pointer.to_int(context).into(),
output_length_pointer.to_int(context).into(),
],
&[pointer.to_int(context).into()],
);
let value = context.build_byte_swap(context.build_load(output_pointer, "address")?)?;
let value = context.build_byte_swap(context.build_load(pointer, "address")?)?;
Ok(context
.builder()
.build_int_z_extend(value.into_int_value(), context.word_type(), "address_zext")?
@@ -194,18 +163,5 @@ pub fn caller<'ctx, D>(
where
D: Dependency + Clone,
{
let (output_pointer, output_length_pointer) =
context.build_stack_parameter(revive_common::BIT_LENGTH_ETH_ADDRESS, "caller_output");
context.build_runtime_call(
runtime_api::imports::CALLER,
&[
output_pointer.to_int(context).into(),
output_length_pointer.to_int(context).into(),
],
);
let value = context.build_byte_swap(context.build_load(output_pointer, "caller")?)?;
Ok(context
.builder()
.build_int_z_extend(value.into_int_value(), context.word_type(), "caller_zext")?
.into())
context.build_runtime_call_to_getter(runtime_api::imports::CALLER)
}