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
#![cfg_attr(not(feature = "std"), no_std)]
#[cfg(test)]
mod mock;
use core::marker::PhantomData;
use darwinia_evm::Runner;
use frame_support::{log, pallet_prelude::*, traits::Get};
use frame_system::pallet_prelude::*;
use sp_core::{H160, H256};
use sp_io::hashing;
use sp_std::vec;
#[frame_support::pallet]
pub mod pallet {
use crate::*;
#[pallet::config]
pub trait Config: frame_system::Config {}
#[pallet::storage]
#[pallet::getter(fn commitment_contract)]
pub type CommitmentContract<T> = StorageValue<_, H160, ValueQuery>;
#[cfg_attr(feature = "std", derive(Default))]
#[pallet::genesis_config]
pub struct GenesisConfig {
pub commitment_contract: H160,
}
#[pallet::genesis_build]
impl<T: Config> GenesisBuild<T> for GenesisConfig {
fn build(&self) {
<CommitmentContract<T>>::put(H160::default());
}
}
#[pallet::pallet]
pub struct Pallet<T>(_);
#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::weight(0)]
pub fn set_commitment_contract(
origin: OriginFor<T>,
commitment_contract: H160,
) -> DispatchResultWithPostInfo {
ensure_root(origin)?;
<CommitmentContract<T>>::put(commitment_contract);
Ok(().into())
}
}
}
pub use pallet::*;
const LOG_TARGET: &str = "runtime::message-gadget";
pub struct MessageRootGetter<T>(PhantomData<T>);
impl<T> Get<Option<H256>> for MessageRootGetter<T>
where
T: Config + darwinia_evm::Config,
{
fn get() -> Option<H256> {
if let Ok(info) = <T as darwinia_evm::Config>::Runner::call(
H160::default(),
<CommitmentContract<T>>::get(),
hashing::keccak_256(b"commitment()")[..4].to_vec(),
0.into(),
1_000_000_000_000,
None,
None,
None,
vec![],
false,
<T as darwinia_evm::Config>::config(),
) {
let raw_message_root = info.value;
if raw_message_root.len() != 32 {
log::warn!(
target: LOG_TARGET,
"Invalid raw message root: {:?}, return.",
raw_message_root
);
return None;
}
return Some(H256::from_slice(&raw_message_root));
}
None
}
}