Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sync runtime with the pallet #54

Merged
merged 4 commits into from
Sep 24, 2024
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
980 changes: 546 additions & 434 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ log = { version = "0.4" }
# polkadot-sdk and friends
codec = { version = "3.6.12", default-features = false, package = "parity-scale-codec" }
scale-info = { version = "2.11.1", default-features = false }
polkadot-sdk = { git = "https://github.com/paritytech/polkadot-sdk", rev = "aec2b10539251fc20450f8efa453f21dee6b95a1" }
polkadot-sdk = { git = "https://github.com/paritytech/polkadot-sdk", rev = "71c768a9e1a467c629adc68423e47e37c855cd77" }

# llvm
[workspace.dependencies.inkwell]
Expand Down
2 changes: 1 addition & 1 deletion crates/differential/genesis.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"config": {
"chainId": 1,
"chainId": 420420420,
"homesteadBlock": 0,
"eip150Block": 0,
"eip155Block": 0,
Expand Down
2 changes: 1 addition & 1 deletion crates/linker/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ fn invoke_lld(cmd_args: &[&str]) -> bool {
fn polkavm_linker<T: AsRef<[u8]>>(code: T) -> anyhow::Result<Vec<u8>> {
let mut config = polkavm_linker::Config::default();
config.set_strip(true);
config.set_optimize(false);
config.set_optimize(true);

polkavm_linker::program_from_elf(config, code.as_ref())
.map_err(|reason| anyhow::anyhow!("polkavm linker failed: {}", reason))
Expand Down
5 changes: 4 additions & 1 deletion crates/llvm-context/src/polkavm/const/runtime_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
16 changes: 16 additions & 0 deletions crates/llvm-context/src/polkavm/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
62 changes: 9 additions & 53 deletions crates/llvm-context/src/polkavm/evm/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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")?
Expand All @@ -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)
}
Binary file modified crates/runner/fixtures/Baseline.pvm
Binary file not shown.
1 change: 0 additions & 1 deletion crates/runner/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ impl pallet_revive::Config for Runtime {
type AddressMapper = AccountId;
type RuntimeMemory = ConstU32<{ 512 * 1024 * 1024 }>;
type PVFMemory = ConstU32<{ 1024 * 1024 * 1024 }>;
type MaxCodeLen = ConstU32<{ 256 * 1024 }>;
type UnsafeUnstableInterface = UnstableInterface;
type UploadOrigin = EnsureSigned<AccountId32>;
type InstantiateOrigin = EnsureSigned<AccountId32>;
Expand Down
10 changes: 6 additions & 4 deletions crates/runtime-api/src/polkavm_imports.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ POLKAVM_IMPORT(uint32_t, instantiate, uint32_t)

POLKAVM_IMPORT(void, terminate, uint32_t)

POLKAVM_IMPORT(void, caller, uint32_t, uint32_t)
POLKAVM_IMPORT(void, caller, uint32_t)

POLKAVM_IMPORT(uint32_t, is_contract, uint32_t)

Expand All @@ -91,21 +91,23 @@ POLKAVM_IMPORT(uint32_t, caller_is_origin)

POLKAVM_IMPORT(uint32_t, caller_is_root)

POLKAVM_IMPORT(void, address, uint32_t, uint32_t)
POLKAVM_IMPORT(void, address, uint32_t)

POLKAVM_IMPORT(void, weight_to_fee, uint64_t, uint32_t, uint32_t)

POLKAVM_IMPORT(void, gas_left, uint32_t, uint32_t)

POLKAVM_IMPORT(void, balance, uint32_t)

POLKAVM_IMPORT(void, now, uint32_t, uint32_t)
POLKAVM_IMPORT(void, chain_id, uint32_t)

POLKAVM_IMPORT(void, now, uint32_t)

POLKAVM_IMPORT(void, minimum_balance, uint32_t, uint32_t)

POLKAVM_IMPORT(void, deposit_event, uint32_t, uint32_t, uint32_t, uint32_t)

POLKAVM_IMPORT(void, block_number, uint32_t, uint32_t)
POLKAVM_IMPORT(void, block_number, uint32_t)

POLKAVM_IMPORT(void, hash_sha2_256, uint32_t, uint32_t, uint32_t)

Expand Down