This repository has been archived by the owner on Sep 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
/
lib.rs
132 lines (109 loc) · 4.63 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// 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)]
#![cfg_attr(test, feature(assert_matches))]
use fp_evm::{PrecompileHandle, PrecompileOutput};
use frame_support::dispatch::{Dispatchable, GetDispatchInfo, PostDispatchInfo};
use pallet_evm::{AddressMapping, Precompile};
use pallet_xvm::XvmContext;
use parity_scale_codec::Decode;
use sp_runtime::codec::Encode;
use sp_std::marker::PhantomData;
use sp_std::prelude::*;
use precompile_utils::{
revert, succeed, Bytes, EvmDataWriter, EvmResult, FunctionModifier, PrecompileHandleExt,
};
#[cfg(test)]
mod mock;
#[cfg(test)]
mod tests;
#[precompile_utils::generate_function_selector]
#[derive(Debug, PartialEq)]
pub enum Action {
XvmCall = "xvm_call(bytes,bytes,bytes)",
}
/// A precompile that expose XVM related functions.
pub struct XvmPrecompile<T>(PhantomData<T>);
impl<R> Precompile for XvmPrecompile<R>
where
R: pallet_evm::Config + pallet_xvm::Config,
<<R as frame_system::Config>::RuntimeCall as Dispatchable>::RuntimeOrigin:
From<Option<R::AccountId>>,
<R as frame_system::Config>::RuntimeCall:
From<pallet_xvm::Call<R>> + Dispatchable<PostInfo = PostDispatchInfo> + GetDispatchInfo,
{
fn execute(handle: &mut impl PrecompileHandle) -> EvmResult<PrecompileOutput> {
log::trace!(target: "xvm-precompile", "In XVM precompile");
let selector = handle.read_selector()?;
handle.check_function_modifier(FunctionModifier::NonPayable)?;
match selector {
// Dispatchables
Action::XvmCall => Self::xvm_call(handle),
}
}
}
impl<R> XvmPrecompile<R>
where
R: pallet_evm::Config + pallet_xvm::Config,
<<R as frame_system::Config>::RuntimeCall as Dispatchable>::RuntimeOrigin:
From<Option<R::AccountId>>,
<R as frame_system::Config>::RuntimeCall:
From<pallet_xvm::Call<R>> + Dispatchable<PostInfo = PostDispatchInfo> + GetDispatchInfo,
{
fn xvm_call(handle: &mut impl PrecompileHandle) -> EvmResult<PrecompileOutput> {
let mut input = handle.read_input()?;
input.expect_arguments(4)?;
// Read arguments and check it
// TODO: This approach probably needs to be revised - does contract call need to specify gas/weight? Usually it is implicit.
let context_raw = input.read::<Bytes>()?;
let context: XvmContext = Decode::decode(&mut context_raw.0.as_ref())
.map_err(|_| revert("can not decode XVM context"))?;
// Fetch the remaining gas (weight) available for execution
// TODO: rework
//let remaining_gas = handle.remaining_gas();
//let remaining_weight = R::GasWeightMapping::gas_to_weight(remaining_gas);
//context.max_weight = remaining_weight;
let call_to = input.read::<Bytes>()?.0;
let call_input = input.read::<Bytes>()?.0;
let from = R::AddressMapping::into_account_id(handle.context().caller);
match &pallet_xvm::Pallet::<R>::xvm_bare_call(context, from, call_to, call_input) {
Ok(success) => {
log::trace!(
target: "xvm-precompile::xvm_call",
"success: {:?}", success
);
Ok(succeed(
EvmDataWriter::new()
.write(true)
.write(Bytes(success.output().to_vec())) // TODO redundant clone
.build(),
))
}
Err(failure) => {
log::trace!(
target: "xvm-precompile::xvm_call",
"failure: {:?}", failure
);
let mut error_buffer = Vec::new();
failure.error().encode_to(&mut error_buffer);
Ok(succeed(
EvmDataWriter::new()
.write(false)
.write(Bytes(error_buffer))
.build(),
))
}
}
}
}