diff --git a/.editorconfig b/.editorconfig index f0b0ff4..09db437 100644 --- a/.editorconfig +++ b/.editorconfig @@ -11,3 +11,9 @@ insert_final_newline=true [Makefile] indent_style=tab + +[*.{yml, yaml}] +indent_style = space +indent_size = 2 +tab_width = 8 +end_of_line = lf diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..3125185 --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,48 @@ +name: Check + +on: + push: + branches: ["master"] + pull_request: + branches: ["master"] + +env: + CARGO_TERM_COLOR: always + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + submodules: recursive + + - name: Install toolchain + uses: dtolnay/rust-toolchain@nightly + with: + targets: wasm32-unknown-unknown + components: rustfmt, clippy + + - name: Install toolchain targeting RV32E + run: | + curl -sL https://github.com/paritytech/rustc-rv32e-toolchain/releases/download/v1.1.0/rust-rve-nightly-2024-01-05-x86_64-unknown-linux-gnu.tar.zst -o rv32e.tar.zst + tar --zstd -xf rv32e.tar.zst + mv rve-nightly ~/.rustup/toolchains/ + + - uses: Swatinem/rust-cache@v2 + + - name: Check format + run: cargo fmt --all -- --check + + - name: Install polkatool + run: make tools + + - name: Generate poc-guest.polkavm + run: make poc-guest + + - name: Custom cargo clippy + run: make clippy diff --git a/.gitmodules b/.gitmodules index 48e842b..499c8d5 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,3 @@ [submodule "vendor/polkavm"] path = vendor/polkavm - url = https://github.com/koute/polkavm + url = https://github.com/acalanetwork/polkavm diff --git a/Makefile b/Makefile index 11702ac..718aac0 100644 --- a/Makefile +++ b/Makefile @@ -13,13 +13,22 @@ tools: cargo install --git https://github.com/koute/polkavm --force polkatool cargo install --git https://github.com/paritytech/polkadot-sdk --tag polkadot-v1.9.0 --force staging-chain-spec-builder +fmt: + cargo fmt --all -- --check + check: cargo check --no-default-features --target=wasm32-unknown-unknown -p poc-executor SKIP_WASM_BUILD= cargo check cd poc/guest; cargo check +clippy: + cargo clippy --no-default-features --target=wasm32-unknown-unknown -p poc-executor + SKIP_WASM_BUILD= cargo clippy -- -D warnings + cd poc/guest; cargo clippy + chainspec: cargo build -p poc-runtime --release + mkdir -p output cp target/release/wbuild/poc-runtime/poc_runtime.compact.compressed.wasm output chain-spec-builder -c output/chainspec.json create -n poc-runtime -i poc-runtime -r ./output/poc_runtime.compact.compressed.wasm -s default cat output/chainspec.json | jq '.properties = {}' > output/chainspec.json.tmp diff --git a/poc/executor/src/lib.rs b/poc/executor/src/lib.rs index d0af2a9..93cb5f6 100644 --- a/poc/executor/src/lib.rs +++ b/poc/executor/src/lib.rs @@ -53,8 +53,8 @@ impl XcqExecutor { } pub fn execute(&mut self, raw_blob: &[u8], input: &[u8]) -> Result, XcqExecutorError> { - let blob = ProgramBlob::parse(raw_blob)?; - let module = Module::from_blob(&self.engine, &Default::default(), &blob)?; + let blob = ProgramBlob::parse(raw_blob.into())?; + let module = Module::from_blob(&self.engine, &Default::default(), blob)?; let instance_pre = self.linker.instantiate_pre(&module)?; let instance = instance_pre.instantiate()?; diff --git a/poc/guest/src/main.rs b/poc/guest/src/main.rs index 01eec91..5827d8f 100644 --- a/poc/guest/src/main.rs +++ b/poc/guest/src/main.rs @@ -51,7 +51,7 @@ extern "C" fn main(ptr: u32) -> u64 { let size = core::mem::size_of_val(val); let val_ptr = val.as_ptr(); - (val_ptr as u64) << 32 | size as u64 / core::mem::size_of::() as u64 + (val_ptr as u64) << 32 | (size as u64 / core::mem::size_of::() as u64) } 1 => { let val = unsafe { core::ptr::read_volatile((ptr + 1) as *const u8) }; @@ -63,7 +63,7 @@ extern "C" fn main(ptr: u32) -> u64 { host_call(guest_args, &mut ret); let res = ret.data0 as u32 + 1; let size = core::mem::size_of_val(&res); - (&res as *const u32 as u64) << 32 | size as u64 / core::mem::size_of::() as u64 + (&res as *const u32 as u64) << 32 | (size as u64 / core::mem::size_of::() as u64) } _ => 0, } diff --git a/poc/runtime/src/lib.rs b/poc/runtime/src/lib.rs index 506745e..7f44a8f 100644 --- a/poc/runtime/src/lib.rs +++ b/poc/runtime/src/lib.rs @@ -7,75 +7,77 @@ include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs")); mod xcq; use frame::{ - deps::frame_support::{ - genesis_builder_helper::{build_config, create_default_config}, - weights::{FixedFee, NoFee}, - }, - prelude::*, - runtime::{ - apis::{ - self, impl_runtime_apis, ApplyExtrinsicResult, CheckInherentsResult, - ExtrinsicInclusionMode, OpaqueMetadata, - }, - prelude::*, - }, + deps::frame_support::{ + genesis_builder_helper::{build_config, create_default_config}, + weights::{FixedFee, NoFee}, + }, + prelude::*, + runtime::{ + apis::{ + self, impl_runtime_apis, ApplyExtrinsicResult, CheckInherentsResult, ExtrinsicInclusionMode, OpaqueMetadata, + }, + prelude::*, + }, }; #[runtime_version] pub const VERSION: RuntimeVersion = RuntimeVersion { - spec_name: create_runtime_str!("xcq-poc"), - impl_name: create_runtime_str!("xcq-poc"), - authoring_version: 1, - spec_version: 0, - impl_version: 1, - apis: RUNTIME_API_VERSIONS, - transaction_version: 1, - state_version: 1, + spec_name: create_runtime_str!("xcq-poc"), + impl_name: create_runtime_str!("xcq-poc"), + authoring_version: 1, + spec_version: 0, + impl_version: 1, + apis: RUNTIME_API_VERSIONS, + transaction_version: 1, + state_version: 1, }; /// The version information used to identify this runtime when compiled natively. #[cfg(feature = "std")] pub fn native_version() -> NativeVersion { - NativeVersion { runtime_version: VERSION, can_author_with: Default::default() } + NativeVersion { + runtime_version: VERSION, + can_author_with: Default::default(), + } } type SignedExtra = ( - frame_system::CheckNonZeroSender, - frame_system::CheckSpecVersion, - frame_system::CheckTxVersion, - frame_system::CheckGenesis, - frame_system::CheckEra, - frame_system::CheckNonce, - frame_system::CheckWeight, - pallet_transaction_payment::ChargeTransactionPayment, + frame_system::CheckNonZeroSender, + frame_system::CheckSpecVersion, + frame_system::CheckTxVersion, + frame_system::CheckGenesis, + frame_system::CheckEra, + frame_system::CheckNonce, + frame_system::CheckWeight, + pallet_transaction_payment::ChargeTransactionPayment, ); construct_runtime!( - pub enum Runtime { - System: frame_system, - Timestamp: pallet_timestamp, - - Balances: pallet_balances, - Sudo: pallet_sudo, - TransactionPayment: pallet_transaction_payment, - } + pub enum Runtime { + System: frame_system, + Timestamp: pallet_timestamp, + + Balances: pallet_balances, + Sudo: pallet_sudo, + TransactionPayment: pallet_transaction_payment, + } ); parameter_types! { - pub const Version: RuntimeVersion = VERSION; + pub const Version: RuntimeVersion = VERSION; } #[derive_impl(frame_system::config_preludes::SolochainDefaultConfig as frame_system::DefaultConfig)] impl frame_system::Config for Runtime { - type Block = Block; - type Version = Version; - type BlockHashCount = ConstU32<1024>; - type AccountData = pallet_balances::AccountData<::Balance>; + type Block = Block; + type Version = Version; + type BlockHashCount = ConstU32<1024>; + type AccountData = pallet_balances::AccountData<::Balance>; } #[derive_impl(pallet_balances::config_preludes::TestDefaultConfig as pallet_balances::DefaultConfig)] impl pallet_balances::Config for Runtime { - type AccountStore = System; + type AccountStore = System; } #[derive_impl(pallet_sudo::config_preludes::TestDefaultConfig as pallet_sudo::DefaultConfig)] @@ -86,129 +88,128 @@ impl pallet_timestamp::Config for Runtime {} #[derive_impl(pallet_transaction_payment::config_preludes::TestDefaultConfig as pallet_transaction_payment::DefaultConfig)] impl pallet_transaction_payment::Config for Runtime { - type OnChargeTransaction = pallet_transaction_payment::CurrencyAdapter; - type WeightToFee = NoFee<::Balance>; - type LengthToFee = FixedFee<1, ::Balance>; + type OnChargeTransaction = pallet_transaction_payment::CurrencyAdapter; + type WeightToFee = NoFee<::Balance>; + type LengthToFee = FixedFee<1, ::Balance>; } type Block = frame::runtime::types_common::BlockOf; type Header = HeaderFor; -type RuntimeExecutive = - Executive, Runtime, AllPalletsWithSystem>; +type RuntimeExecutive = Executive, Runtime, AllPalletsWithSystem>; use pallet_transaction_payment::{FeeDetails, RuntimeDispatchInfo}; impl_runtime_apis! { - impl apis::Core for Runtime { - fn version() -> RuntimeVersion { - VERSION - } - - fn execute_block(block: Block) { - RuntimeExecutive::execute_block(block) - } - - fn initialize_block(header: &Header) -> ExtrinsicInclusionMode { - RuntimeExecutive::initialize_block(header) - } - } - impl apis::Metadata for Runtime { - fn metadata() -> OpaqueMetadata { - OpaqueMetadata::new(Runtime::metadata().into()) - } - - fn metadata_at_version(version: u32) -> Option { - Runtime::metadata_at_version(version) - } - - fn metadata_versions() -> Vec { - Runtime::metadata_versions() - } - } - - impl apis::BlockBuilder for Runtime { - fn apply_extrinsic(extrinsic: ExtrinsicFor) -> ApplyExtrinsicResult { - RuntimeExecutive::apply_extrinsic(extrinsic) - } - - fn finalize_block() -> HeaderFor { - RuntimeExecutive::finalize_block() - } - - fn inherent_extrinsics(data: InherentData) -> Vec> { - data.create_extrinsics() - } - - fn check_inherents( - block: Block, - data: InherentData, - ) -> CheckInherentsResult { - data.check_extrinsics(&block) - } - } - - impl apis::TaggedTransactionQueue for Runtime { - fn validate_transaction( - source: TransactionSource, - tx: ExtrinsicFor, - block_hash: ::Hash, - ) -> TransactionValidity { - RuntimeExecutive::validate_transaction(source, tx, block_hash) - } - } - - impl apis::OffchainWorkerApi for Runtime { - fn offchain_worker(header: &HeaderFor) { - RuntimeExecutive::offchain_worker(header) - } - } - - impl apis::SessionKeys for Runtime { - fn generate_session_keys(_seed: Option>) -> Vec { - Default::default() - } - - fn decode_session_keys( - _encoded: Vec, - ) -> Option, apis::KeyTypeId)>> { - Default::default() - } - } - - impl apis::AccountNonceApi for Runtime { - fn account_nonce(account: interface::AccountId) -> interface::Nonce { - System::account_nonce(account) - } - } - - impl pallet_transaction_payment_rpc_runtime_api::TransactionPaymentApi< - Block, - interface::Balance, - > for Runtime { - fn query_info(uxt: ExtrinsicFor, len: u32) -> RuntimeDispatchInfo { - TransactionPayment::query_info(uxt, len) - } - fn query_fee_details(uxt: ExtrinsicFor, len: u32) -> FeeDetails { - TransactionPayment::query_fee_details(uxt, len) - } - fn query_weight_to_fee(weight: Weight) -> interface::Balance { - TransactionPayment::weight_to_fee(weight) - } - fn query_length_to_fee(length: u32) -> interface::Balance { - TransactionPayment::length_to_fee(length) - } - } - - impl sp_genesis_builder::GenesisBuilder for Runtime { - fn create_default_config() -> Vec { - create_default_config::() - } - - fn build_config(config: Vec) -> sp_genesis_builder::Result { - build_config::(config) - } - } + impl apis::Core for Runtime { + fn version() -> RuntimeVersion { + VERSION + } + + fn execute_block(block: Block) { + RuntimeExecutive::execute_block(block) + } + + fn initialize_block(header: &Header) -> ExtrinsicInclusionMode { + RuntimeExecutive::initialize_block(header) + } + } + impl apis::Metadata for Runtime { + fn metadata() -> OpaqueMetadata { + OpaqueMetadata::new(Runtime::metadata().into()) + } + + fn metadata_at_version(version: u32) -> Option { + Runtime::metadata_at_version(version) + } + + fn metadata_versions() -> Vec { + Runtime::metadata_versions() + } + } + + impl apis::BlockBuilder for Runtime { + fn apply_extrinsic(extrinsic: ExtrinsicFor) -> ApplyExtrinsicResult { + RuntimeExecutive::apply_extrinsic(extrinsic) + } + + fn finalize_block() -> HeaderFor { + RuntimeExecutive::finalize_block() + } + + fn inherent_extrinsics(data: InherentData) -> Vec> { + data.create_extrinsics() + } + + fn check_inherents( + block: Block, + data: InherentData, + ) -> CheckInherentsResult { + data.check_extrinsics(&block) + } + } + + impl apis::TaggedTransactionQueue for Runtime { + fn validate_transaction( + source: TransactionSource, + tx: ExtrinsicFor, + block_hash: ::Hash, + ) -> TransactionValidity { + RuntimeExecutive::validate_transaction(source, tx, block_hash) + } + } + + impl apis::OffchainWorkerApi for Runtime { + fn offchain_worker(header: &HeaderFor) { + RuntimeExecutive::offchain_worker(header) + } + } + + impl apis::SessionKeys for Runtime { + fn generate_session_keys(_seed: Option>) -> Vec { + Default::default() + } + + fn decode_session_keys( + _encoded: Vec, + ) -> Option, apis::KeyTypeId)>> { + Default::default() + } + } + + impl apis::AccountNonceApi for Runtime { + fn account_nonce(account: interface::AccountId) -> interface::Nonce { + System::account_nonce(account) + } + } + + impl pallet_transaction_payment_rpc_runtime_api::TransactionPaymentApi< + Block, + interface::Balance, + > for Runtime { + fn query_info(uxt: ExtrinsicFor, len: u32) -> RuntimeDispatchInfo { + TransactionPayment::query_info(uxt, len) + } + fn query_fee_details(uxt: ExtrinsicFor, len: u32) -> FeeDetails { + TransactionPayment::query_fee_details(uxt, len) + } + fn query_weight_to_fee(weight: Weight) -> interface::Balance { + TransactionPayment::weight_to_fee(weight) + } + fn query_length_to_fee(length: u32) -> interface::Balance { + TransactionPayment::length_to_fee(length) + } + } + + impl sp_genesis_builder::GenesisBuilder for Runtime { + fn create_default_config() -> Vec { + create_default_config::() + } + + fn build_config(config: Vec) -> sp_genesis_builder::Result { + build_config::(config) + } + } impl xcq::XcqApi for Runtime { fn execute_query(query: Vec, input: Vec) -> xcq::XcqResult { @@ -223,14 +224,14 @@ impl_runtime_apis! { // TODO: this should be standardized in some way, see: // https://github.com/paritytech/substrate/issues/10579#issuecomment-1600537558 pub mod interface { - use super::Runtime; - use frame::deps::frame_system; - - pub type Block = super::Block; - pub use frame::runtime::types_common::OpaqueBlock; - pub type AccountId = ::AccountId; - pub type Nonce = ::Nonce; - pub type Hash = ::Hash; - pub type Balance = ::Balance; - pub type MinimumBalance = ::ExistentialDeposit; + use super::Runtime; + use frame::deps::frame_system; + + pub type Block = super::Block; + pub use frame::runtime::types_common::OpaqueBlock; + pub type AccountId = ::AccountId; + pub type Nonce = ::Nonce; + pub type Hash = ::Hash; + pub type Balance = ::Balance; + pub type MinimumBalance = ::ExistentialDeposit; } diff --git a/poc/runtime/src/xcq.rs b/poc/runtime/src/xcq.rs index 7602ac6..7e0126d 100644 --- a/poc/runtime/src/xcq.rs +++ b/poc/runtime/src/xcq.rs @@ -1,5 +1,6 @@ use frame::deps::sp_api::decl_runtime_apis; use frame::prelude::*; +#[allow(unused_imports)] use scale_info::prelude::{format, string::String}; pub type XcqResponse = Vec; diff --git a/vendor/polkavm b/vendor/polkavm index a871cf7..847eac7 160000 --- a/vendor/polkavm +++ b/vendor/polkavm @@ -1 +1 @@ -Subproject commit a871cf7c5b7bcc70fcc05ccf7431af27da78144b +Subproject commit 847eac7dd58606d48c4e24274eb336e848fe7fe1