-
Notifications
You must be signed in to change notification settings - Fork 835
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
[pallet-revive] implement the block hash API #6246
Merged
Merged
Changes from 8 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
61a0180
the blockhash api
xermicus 41d01d2
the contract api
xermicus acf5c16
mv
xermicus c76f4a2
Merge branch 'master' into cl/blockhash
xermicus e21aed7
".git/.scripts/commands/fmt/fmt.sh"
5e18e9e
Update from xermicus running command 'prdoc --audience runtime_dev --…
actions-user 2fe6e6c
".git/.scripts/commands/bench/bench.sh" --subcommand=pallet --runtime…
bc19c90
Merge branch 'master' into cl/blockhash
xermicus 821c251
Merge branch 'master' into cl/blockhash
xermicus 3249ebf
".git/.scripts/commands/bench/bench.sh" --subcommand=pallet --runtime…
e497aff
typo
xermicus 28606a4
".git/.scripts/commands/fmt/fmt.sh"
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
title: '[pallet-revive] implement the block hash API' | ||
doc: | ||
- audience: Runtime Dev | ||
description: |- | ||
- Bound T::Hash to H256 | ||
- Implement the block hash API | ||
crates: | ||
- name: pallet-revive | ||
bump: major | ||
- name: pallet-revive-fixtures | ||
bump: major | ||
- name: pallet-revive-uapi | ||
bump: major |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!(block_number: &[u8; 32], block_hash: &[u8; 32],); | ||
|
||
let mut buf = [0; 32]; | ||
api::block_hash(block_number, &mut &mut buf); | ||
|
||
assert_eq!(&buf[..], block_hash); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,7 +53,7 @@ use sp_core::{ | |
}; | ||
use sp_io::{crypto::secp256k1_ecdsa_recover_compressed, hashing::blake2_256}; | ||
use sp_runtime::{ | ||
traits::{BadOrigin, Convert, Dispatchable, Zero}, | ||
traits::{BadOrigin, Convert, Dispatchable, Saturating, Zero}, | ||
DispatchError, SaturatedConversion, | ||
}; | ||
|
||
|
@@ -350,6 +350,10 @@ pub trait Ext: sealing::Sealed { | |
/// Returns the current block number. | ||
fn block_number(&self) -> U256; | ||
|
||
/// Returns the block hash at the given `block_number` or `None` if | ||
/// `block_number` isn't within the range of the previous 256 blocks. | ||
fn block_hash(&self, block_number: U256) -> Option<H256>; | ||
|
||
/// Returns the maximum allowed size of a storage item. | ||
fn max_value_size(&self) -> u32; | ||
|
||
|
@@ -733,6 +737,7 @@ where | |
BalanceOf<T>: Into<U256> + TryFrom<U256>, | ||
MomentOf<T>: Into<U256>, | ||
E: Executable<T>, | ||
T::Hash: frame_support::traits::IsType<H256>, | ||
{ | ||
/// Create and run a new call stack by calling into `dest`. | ||
/// | ||
|
@@ -1323,6 +1328,24 @@ where | |
pub(crate) fn override_export(&mut self, export: ExportedFunction) { | ||
self.top_frame_mut().entry_point = export; | ||
} | ||
|
||
#[cfg(all(feature = "runtime-benchmarks", feature = "riscv"))] | ||
pub(crate) fn set_block_number(&mut self, block_number: BlockNumberFor<T>) { | ||
athei marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.block_number = block_number; | ||
} | ||
|
||
fn block_hash(&self, block_number: U256) -> Option<H256> { | ||
let Ok(block_number) = BlockNumberFor::<T>::try_from(block_number) else { | ||
return None; | ||
}; | ||
athei marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if block_number >= self.block_number { | ||
return None; | ||
} | ||
if block_number < self.block_number.saturating_sub(256u32.into()) { | ||
return None; | ||
} | ||
Comment on lines
+1350
to
+1352
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we need this ? why don't we just try t get it with System::::block_hash There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I implemented it to resemble the semantics of the corresponding EVM opcode |
||
Some(System::<T>::block_hash(&block_number).into()) | ||
} | ||
} | ||
|
||
impl<'a, T, E> Ext for Stack<'a, T, E> | ||
|
@@ -1331,6 +1354,7 @@ where | |
E: Executable<T>, | ||
BalanceOf<T>: Into<U256> + TryFrom<U256>, | ||
MomentOf<T>: Into<U256>, | ||
T::Hash: frame_support::traits::IsType<H256>, | ||
{ | ||
type T = T; | ||
|
||
|
@@ -1631,6 +1655,10 @@ where | |
self.block_number.into() | ||
} | ||
|
||
fn block_hash(&self, block_number: U256) -> Option<H256> { | ||
self.block_hash(block_number) | ||
} | ||
|
||
fn max_value_size(&self) -> u32 { | ||
limits::PAYLOAD_BYTES | ||
} | ||
|
@@ -4671,4 +4699,60 @@ mod tests { | |
.unwrap() | ||
}); | ||
} | ||
|
||
#[test] | ||
fn block_hash_returns_proper_values() { | ||
let bob_code_hash = MockLoader::insert(Call, |ctx, _| { | ||
ctx.ext.block_number = 1u32.into(); | ||
assert_eq!(ctx.ext.block_hash(U256::from(1)), None); | ||
assert_eq!(ctx.ext.block_hash(U256::from(0)), Some(H256::from([1; 32]))); | ||
|
||
ctx.ext.block_number = 300u32.into(); | ||
assert_eq!(ctx.ext.block_hash(U256::from(300)), None); | ||
assert_eq!(ctx.ext.block_hash(U256::from(43)), None); | ||
assert_eq!(ctx.ext.block_hash(U256::from(44)), Some(H256::from([2; 32]))); | ||
|
||
exec_success() | ||
}); | ||
|
||
ExtBuilder::default().build().execute_with(|| { | ||
frame_system::BlockHash::<Test>::insert( | ||
&BlockNumberFor::<Test>::from(0u32), | ||
<tests::Test as frame_system::Config>::Hash::from([1; 32]), | ||
); | ||
frame_system::BlockHash::<Test>::insert( | ||
&BlockNumberFor::<Test>::from(1u32), | ||
<tests::Test as frame_system::Config>::Hash::default(), | ||
); | ||
frame_system::BlockHash::<Test>::insert( | ||
&BlockNumberFor::<Test>::from(43u32), | ||
<tests::Test as frame_system::Config>::Hash::default(), | ||
); | ||
frame_system::BlockHash::<Test>::insert( | ||
&BlockNumberFor::<Test>::from(44u32), | ||
<tests::Test as frame_system::Config>::Hash::from([2; 32]), | ||
); | ||
frame_system::BlockHash::<Test>::insert( | ||
&BlockNumberFor::<Test>::from(300u32), | ||
<tests::Test as frame_system::Config>::Hash::default(), | ||
); | ||
|
||
place_contract(&BOB, bob_code_hash); | ||
|
||
let origin = Origin::from_account_id(ALICE); | ||
let mut storage_meter = storage::meter::Meter::new(&origin, 0, 0).unwrap(); | ||
assert_matches!( | ||
MockStack::run_call( | ||
origin, | ||
BOB_ADDR, | ||
&mut GasMeter::<Test>::new(GAS_LIMIT), | ||
&mut storage_meter, | ||
0, | ||
vec![0], | ||
None, | ||
), | ||
Ok(_) | ||
); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wouldn't
T::Hash: Into<H256>
suffice for all these added constraints?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Technically yes. But I think we are splitting hairs here :) I don't see where a runtime would fullfill
T::Hash: Into<H256>
butT::Hash
isn't actuallyH256
.