forked from AstarNetwork/Astar
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
246 additions
and
24 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
[package] | ||
name = "chain-extension-block-number-provider" | ||
version = "0.1.0" | ||
license = "Apache-2.0" | ||
description = "Relay chain block number provider chain extension for WASM contracts" | ||
authors.workspace = true | ||
edition.workspace = true | ||
homepage.workspace = true | ||
repository.workspace = true | ||
|
||
[dependencies] | ||
cumulus-pallet-parachain-system ={ workspace = true } | ||
log = { workspace = true } | ||
num-traits = { workspace = true } | ||
frame-support = { workspace = true } | ||
frame-system = { workspace = true } | ||
pallet-contracts = { workspace = true } | ||
pallet-contracts-primitives = { workspace = true } | ||
parity-scale-codec = { workspace = true } | ||
scale-info = { workspace = true } | ||
sp-core = { workspace = true } | ||
sp-runtime = { workspace = true } | ||
sp-std = { workspace = true } | ||
|
||
[features] | ||
default = ["std"] | ||
std = [ | ||
"cumulus-pallet-parachain-system/std", | ||
"parity-scale-codec/std", | ||
"num-traits/std", | ||
"frame-support/std", | ||
"frame-system/std", | ||
"pallet-contracts/std", | ||
"pallet-contracts-primitives/std", | ||
"scale-info/std", | ||
"sp-std/std", | ||
"sp-core/std", | ||
"sp-runtime/std", | ||
] | ||
local = [] |
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,130 @@ | ||
// This file is part of Astar. | ||
|
||
// Copyright (C) 2019-2023 Stake Technologies Pte.Ltd. | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
// Astar is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
|
||
// Astar is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
|
||
// You should have received a copy of the GNU General Public License | ||
// along with Astar. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
#![cfg_attr(not(feature = "std"), no_std)] | ||
|
||
pub mod weights; | ||
|
||
use pallet_contracts::chain_extension::{ | ||
ChainExtension, Environment, Ext, InitState, RetVal, SysConfig, | ||
}; | ||
use parity_scale_codec::{Decode, Encode}; | ||
use sp_runtime::{traits::StaticLookup, DispatchError}; | ||
use sp_std::marker::PhantomData; | ||
|
||
#[cfg(not(feature = "local"))] | ||
use cumulus_pallet_parachain_system::RelaychainDataProvider; | ||
#[cfg(not(feature = "local"))] | ||
use sp_runtime::traits::BlockNumberProvider; | ||
|
||
type AccountIdLookup<T> = <<T as SysConfig>::Lookup as StaticLookup>::Source; | ||
|
||
enum BlockNumberProviderFunc { | ||
RelayChainBlockNumber, | ||
} | ||
|
||
#[derive(PartialEq, Eq, Copy, Clone, Encode, Decode, Debug)] | ||
#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] | ||
pub enum Outcome { | ||
/// Success | ||
Success = 0, | ||
/// Origin Caller is not supported | ||
OriginCannotBeCaller = 98, | ||
/// Unknown error | ||
RuntimeError = 99, | ||
} | ||
|
||
impl TryFrom<u16> for BlockNumberProviderFunc { | ||
type Error = DispatchError; | ||
|
||
fn try_from(value: u16) -> Result<Self, Self::Error> { | ||
match value { | ||
1 => Ok(Self::RelayChainBlockNumber), | ||
_ => Err(DispatchError::Other( | ||
"Unimplemented func_id for BlockNumberProvider", | ||
)), | ||
} | ||
} | ||
} | ||
|
||
/// Block number provider chain extension. | ||
pub struct BlockNumberProviderExtension<T, W>(PhantomData<(T, W)>); | ||
|
||
impl<T, W> Default for BlockNumberProviderExtension<T, W> { | ||
fn default() -> Self { | ||
BlockNumberProviderExtension(PhantomData) | ||
} | ||
} | ||
|
||
#[cfg(feature = "local")] | ||
impl<T, W> ChainExtension<T> for BlockNumberProviderExtension<T, W> | ||
where | ||
T: pallet_contracts::Config, | ||
AccountIdLookup<T>: From<<T as SysConfig>::AccountId>, | ||
<T as SysConfig>::AccountId: From<[u8; 32]>, | ||
W: weights::WeightInfo, | ||
{ | ||
fn call<E: Ext>(&mut self, env: Environment<E, InitState>) -> Result<RetVal, DispatchError> | ||
where | ||
E: Ext<T = T>, | ||
{ | ||
let func_id = env.func_id().try_into()?; | ||
let mut env = env.buf_in_buf_out(); | ||
|
||
match func_id { | ||
BlockNumberProviderFunc::RelayChainBlockNumber => { | ||
let base_weight = <W as weights::WeightInfo>::relay_chain_block_number(); | ||
env.charge_weight(base_weight)?; | ||
|
||
let current_block_number = frame_system::Pallet::<T>::block_number(); | ||
env.write(¤t_block_number.encode(), false, None)?; | ||
} | ||
} | ||
|
||
Ok(RetVal::Converging(Outcome::Success as u32)) | ||
} | ||
} | ||
|
||
#[cfg(not(feature = "local"))] | ||
impl<T, W> ChainExtension<T> for BlockNumberProviderExtension<T, W> | ||
where | ||
T: pallet_contracts::Config + cumulus_pallet_parachain_system::Config, | ||
AccountIdLookup<T>: From<<T as SysConfig>::AccountId>, | ||
<T as SysConfig>::AccountId: From<[u8; 32]>, | ||
W: weights::WeightInfo, | ||
{ | ||
fn call<E: Ext>(&mut self, env: Environment<E, InitState>) -> Result<RetVal, DispatchError> | ||
where | ||
E: Ext<T = T>, | ||
{ | ||
let func_id = env.func_id().try_into()?; | ||
let mut env = env.buf_in_buf_out(); | ||
|
||
match func_id { | ||
BlockNumberProviderFunc::RelayChainBlockNumber => { | ||
let base_weight = <W as weights::WeightInfo>::relay_chain_block_number(); | ||
env.charge_weight(base_weight)?; | ||
|
||
let current_block_number = RelaychainDataProvider::<T>::current_block_number(); | ||
env.write(¤t_block_number.encode(), false, None)?; | ||
} | ||
} | ||
|
||
Ok(RetVal::Converging(Outcome::Success as u32)) | ||
} | ||
} |
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 Astar. | ||
|
||
// Copyright (C) 2019-2023 Stake Technologies Pte.Ltd. | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
// Astar is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
|
||
// Astar is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
|
||
// You should have received a copy of the GNU General Public License | ||
// along with Astar. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
#![cfg_attr(rustfmt, rustfmt_skip)] | ||
#![allow(unused_parens)] | ||
#![allow(unused_imports)] | ||
|
||
use frame_support::{traits::Get, weights::{Weight, constants::RocksDbWeight}}; | ||
use sp_std::marker::PhantomData; | ||
|
||
/// Weight functions needed for block number provider chain-extension. | ||
pub trait WeightInfo { | ||
fn relay_chain_block_number() -> Weight; | ||
} | ||
|
||
/// Weights for block number provider chain-extension | ||
pub struct SubstrateWeight<T>(PhantomData<T>); | ||
impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> { | ||
fn relay_chain_block_number() -> Weight { | ||
T::DbWeight::get().reads(1 as u64) | ||
} | ||
} |
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
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