Skip to content

Commit

Permalink
the runtime API method
Browse files Browse the repository at this point in the history
Signed-off-by: xermicus <[email protected]>
  • Loading branch information
xermicus committed Jan 16, 2025
1 parent 31217da commit 19d5074
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 1 deletion.
37 changes: 37 additions & 0 deletions substrate/frame/revive/fixtures/contracts/block_author.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// This file is part of Substrate.

// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#![no_std]
#![no_main]

use common::input;
use uapi::{HostFn, HostFnImpl as api};

#[no_mangle]
#[polkavm_derive::polkavm_export]
pub extern "C" fn deploy() {}

#[no_mangle]
#[polkavm_derive::polkavm_export]
pub extern "C" fn call() {
input!(expected: &[u8; 20],);

let mut received = [0; 20];
api::block_author(&mut received);

assert_eq!(expected, &received);
}
27 changes: 26 additions & 1 deletion substrate/frame/revive/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ use frame_support::{
traits::{
fungible::{BalancedHold, Inspect, Mutate, MutateHold},
tokens::Preservation,
ConstU32, ConstU64, Contains, OnIdle, OnInitialize, StorageVersion,
ConstU32, ConstU64, Contains, FindAuthor, OnIdle, OnInitialize, StorageVersion,
},
weights::{constants::WEIGHT_REF_TIME_PER_SECOND, FixedFee, IdentityFee, Weight, WeightMeter},
};
Expand Down Expand Up @@ -509,6 +509,15 @@ parameter_types! {
pub static UnstableInterface: bool = true;
}

impl FindAuthor<<Test as frame_system::Config>::AccountId> for Test {
fn find_author<'a, I>(_digests: I) -> Option<<Test as frame_system::Config>::AccountId>
where
I: 'a + IntoIterator<Item = (frame_support::ConsensusEngineId, &'a [u8])>,
{
Some(EVE)
}
}

#[derive_impl(crate::config_preludes::TestDefaultConfig)]
impl Config for Test {
type Time = Timestamp;
Expand All @@ -525,6 +534,7 @@ impl Config for Test {
type CodeHashLockupDepositPercent = CodeHashLockupDepositPercent;
type Debug = TestDebug;
type ChainId = ChainId;
type FindAuthor = Test;
}

impl TryFrom<RuntimeCall> for crate::Call<Test> {
Expand Down Expand Up @@ -4024,6 +4034,21 @@ fn block_hash_works() {
});
}

#[test]
fn block_author_works() {
let (code, _) = compile_module("block_author").unwrap();

ExtBuilder::default().existential_deposit(1).build().execute_with(|| {
let _ = <Test as Config>::Currency::set_balance(&ALICE, 1_000_000);

let Contract { addr, .. } =
builder::bare_instantiate(Code::Upload(code)).build_and_unwrap_contract();

// The fixture asserts the input to match the find_author API method output.
assert_ok!(builder::call(addr).data(EVE_ADDR.encode()).build());
});
}

#[test]
fn root_cannot_upload_code() {
let (wasm, _) = compile_module("dummy").unwrap();
Expand Down
7 changes: 7 additions & 0 deletions substrate/frame/revive/uapi/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,13 @@ pub trait HostFn: private::Sealed {
/// Returns the amount of ref_time left.
fn ref_time_left() -> u64;

/// Stores the current block author of into the supplied buffer.
///
/// # Parameters
///
/// - `output`: A reference to the output data buffer to write the block author.
fn block_author(output: &mut [u8; 20]);

/// Stores the current block number of the current contract into the supplied buffer.
///
/// # Parameters
Expand Down
5 changes: 5 additions & 0 deletions substrate/frame/revive/uapi/src/host/riscv64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ mod sys {
pub fn call_data_size() -> u64;
pub fn block_number(out_ptr: *mut u8);
pub fn block_hash(block_number_ptr: *const u8, out_ptr: *mut u8);
pub fn block_author(out_ptr: *mut u8);
pub fn hash_sha2_256(input_ptr: *const u8, input_len: u32, out_ptr: *mut u8);
pub fn hash_keccak_256(input_ptr: *const u8, input_len: u32, out_ptr: *mut u8);
pub fn hash_blake2_256(input_ptr: *const u8, input_len: u32, out_ptr: *mut u8);
Expand Down Expand Up @@ -411,6 +412,10 @@ impl HostFn for HostFnImpl {
unsafe { sys::block_number(output.as_mut_ptr()) }
}

fn block_author(output: &mut [u8; 20]) {
unsafe { sys::block_author(output.as_mut_ptr()) }
}

fn weight_to_fee(ref_time_limit: u64, proof_size_limit: u64, output: &mut [u8; 32]) {
unsafe { sys::weight_to_fee(ref_time_limit, proof_size_limit, output.as_mut_ptr()) };
}
Expand Down

0 comments on commit 19d5074

Please sign in to comment.