-
Notifications
You must be signed in to change notification settings - Fork 2
/
lib.rs
179 lines (151 loc) · 6.93 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#![cfg_attr(not(feature = "std"), no_std)]
//! # Propogation Pallet
//!
//! ## Overview
//!
//! Propgates messages to signing client through offchain worker
pub use pallet::*;
#[cfg(test)]
mod mock;
#[cfg(test)]
mod tests;
#[frame_support::pallet]
pub mod pallet {
use codec::Encode;
use entropy_shared::{OcwMessageDkg, OcwMessageProactiveRefresh, ValidatorInfo};
use frame_support::{dispatch::Vec, pallet_prelude::*, sp_runtime::traits::Saturating};
use frame_system::pallet_prelude::*;
use scale_info::prelude::vec;
use sp_core;
use sp_runtime::{
offchain::{http, Duration},
sp_std::str,
};
#[pallet::config]
pub trait Config:
frame_system::Config
+ pallet_authorship::Config
+ pallet_relayer::Config
+ pallet_staking_extension::Config
{
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
}
#[pallet::pallet]
pub struct Pallet<T>(_);
#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn offchain_worker(block_number: BlockNumberFor<T>) {
let _ = Self::post_dkg(block_number);
let _ = Self::post_proactive_refresh(block_number);
}
fn on_initialize(block_number: BlockNumberFor<T>) -> Weight {
pallet_relayer::Dkg::<T>::remove(block_number.saturating_sub(2u32.into()));
pallet_staking_extension::ProactiveRefresh::<T>::take();
T::DbWeight::get().writes(2)
}
}
#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
/// DKG Message passed to validators
/// parameters. [OcwMessageDkg]
DkgMessagePassed(OcwMessageDkg),
/// Proactive Refresh Message passed to validators
/// parameters. [OcwMessageProactiveRefresh]
ProactiveRefreshMessagePassed(OcwMessageProactiveRefresh),
}
#[pallet::call]
impl<T: Config> Pallet<T> {}
impl<T: Config> Pallet<T> {
pub fn post_dkg(block_number: BlockNumberFor<T>) -> Result<(), http::Error> {
let messages =
pallet_relayer::Pallet::<T>::dkg(block_number.saturating_sub(1u32.into()));
let deadline = sp_io::offchain::timestamp().add(Duration::from_millis(2_000));
let kind = sp_core::offchain::StorageKind::PERSISTENT;
let from_local = sp_io::offchain::local_storage_get(kind, b"propagation")
.unwrap_or_else(|| b"http://localhost:3001/user/new".to_vec());
let url = str::from_utf8(&from_local).unwrap_or("http://localhost:3001/user/new");
log::warn!("propagation::post::messages: {:?}", &messages);
let converted_block_number: u32 =
BlockNumberFor::<T>::try_into(block_number).unwrap_or_default();
let (servers_info, _i) =
pallet_relayer::Pallet::<T>::get_validator_info().unwrap_or_default();
let validators_info = servers_info
.iter()
.map(|server_info| ValidatorInfo {
x25519_public_key: server_info.x25519_public_key,
ip_address: server_info.endpoint.clone(),
tss_account: server_info.tss_account.encode(),
})
.collect::<Vec<_>>();
// the data is serialized / encoded to Vec<u8> by parity-scale-codec::encode()
let req_body = OcwMessageDkg {
// subtract 1 from blocknumber since the request is from the last block
block_number: converted_block_number.saturating_sub(1),
sig_request_accounts: messages,
validators_info,
};
log::warn!("propagation::post::req_body: {:?}", &[req_body.encode()]);
// We construct the request
// important: the header->Content-Type must be added and match that of the receiving
// party!!
let pending = http::Request::post(url, vec![req_body.encode()])
.deadline(deadline)
.send()
.map_err(|_| http::Error::IoError)?;
// We await response, same as in fn get()
let response =
pending.try_wait(deadline).map_err(|_| http::Error::DeadlineReached)??;
// check response code
if response.code != 200 {
log::warn!("Unexpected status code: {}", response.code);
return Err(http::Error::Unknown);
}
let _res_body = response.body().collect::<Vec<u8>>();
Self::deposit_event(Event::DkgMessagePassed(req_body));
Ok(())
}
pub fn post_proactive_refresh(_block_number: BlockNumberFor<T>) -> Result<(), http::Error> {
let refresh_info = pallet_staking_extension::Pallet::<T>::proactive_refresh();
if refresh_info.is_empty() {
return Ok(());
}
let deadline = sp_io::offchain::timestamp().add(Duration::from_millis(2_000));
let kind = sp_core::offchain::StorageKind::PERSISTENT;
let from_local = sp_io::offchain::local_storage_get(kind, b"refresh")
.unwrap_or_else(|| b"http://localhost:3001/signer/proactive_refresh".to_vec());
let url = str::from_utf8(&from_local)
.unwrap_or("http://localhost:3001/signer/proactive_refresh");
let (servers_info, _i) =
pallet_relayer::Pallet::<T>::get_validator_info().unwrap_or_default();
let validators_info = servers_info
.iter()
.map(|server_info| ValidatorInfo {
x25519_public_key: server_info.x25519_public_key,
ip_address: server_info.endpoint.clone(),
tss_account: server_info.tss_account.encode(),
})
.collect::<Vec<_>>();
log::warn!("propagation::post proactive refresh: {:?}", &[validators_info.encode()]);
let req_body = OcwMessageProactiveRefresh { validators_info };
// We construct the request
// important: the header->Content-Type must be added and match that of the receiving
// party!!
let pending = http::Request::post(url, vec![req_body.encode()])
.deadline(deadline)
.send()
.map_err(|_| http::Error::IoError)?;
// We await response, same as in fn get()
let response =
pending.try_wait(deadline).map_err(|_| http::Error::DeadlineReached)??;
// check response code
if response.code != 200 {
log::warn!("Unexpected status code: {}", response.code);
return Err(http::Error::Unknown);
}
let _res_body = response.body().collect::<Vec<u8>>();
Self::deposit_event(Event::ProactiveRefreshMessagePassed(req_body));
Ok(())
}
}
}