-
Notifications
You must be signed in to change notification settings - Fork 1
/
types.rs
executable file
·271 lines (236 loc) · 7.98 KB
/
types.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
// Copyright 2021 PolyCrypt GmbH
//
// 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.
//! Channel types and type defs.
use crate::*;
use crate::pallet::Config;
use codec::{Decode, Encode};
use sp_core::{ByteArray, Hasher};
use sp_runtime::{
traits::{IdentifyAccount, Verify},
RuntimeDebug,
};
use sp_std::prelude::*;
// Type alias.
pub type BalanceOf<T> =
<<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;
pub type CurrencyOf<T> = <T as Config>::Currency;
pub type WeightInfoOf<T> = <T as Config>::WeightInfo;
pub type VersionOf<T> = <T as pallet::Config>::Version;
pub type NonceOf<T> = <T as pallet::Config>::Nonce;
pub type ChannelIdOf<T> = <T as pallet::Config>::HashValue;
pub type FundingIdOf<T> = <T as pallet::Config>::HashValue;
pub type SecondsOf<T> = <T as pallet::Config>::Seconds;
pub type AccountIdOf<T> = <T as frame_system::Config>::AccountId;
pub type HasherOf<T> = <T as pallet::Config>::Hasher;
pub type PkOf<T> = <T as pallet::Config>::PK;
pub type SigOf<T> = <T as pallet::Config>::Signature;
pub type ParticipantIndex = u32;
pub type ParamsOf<T> = Params<NonceOf<T>, PkOf<T>, SecondsOf<T>, AppIdOf<T>>;
pub type StateOf<T> = State<ChannelIdOf<T>, VersionOf<T>, BalanceOf<T>>;
pub type RegisteredStateOf<T> = RegisteredState<StateOf<T>, SecondsOf<T>>;
pub type WithdrawalOf<T> = Withdrawal<ChannelIdOf<T>, PkOf<T>, AccountIdOf<T>>;
pub type FundingOf<T> = Funding<ChannelIdOf<T>, PkOf<T>>;
pub type AppIdOf<T> = <T as Config>::AppId;
pub type AppData = Vec<u8>;
pub trait AppId: Encode + Decode + TypeInfo + Member + PartialEq {}
impl<T: Encode + Decode + TypeInfo + Member + PartialEq> AppId for T {}
pub trait AppRegistry<T: pallet::Config> {
fn valid_transition(
params: &ParamsOf<T>,
from: &StateOf<T>,
to: &StateOf<T>,
signer: ParticipantIndex,
) -> bool;
fn transition_weight(params: &ParamsOf<T>) -> Weight;
}
#[derive(Encode, Decode, Clone, PartialEq, RuntimeDebug, TypeInfo)]
#[codec(dumb_trait_bound)]
/// Fixed parameters of a channel.
///
/// The values are agreed on by all participants before opening a channel.
pub struct Params<Nonce, PK, Seconds, AppId> {
/// Nonce to make these Params unique. Should be picked randomly.
pub nonce: Nonce,
/// Vector of all participants of the channel.
pub participants: Vec<PK>,
/// Challenge duration of the channel.
pub challenge_duration: Seconds,
// App in channel.
pub app: AppId,
}
impl<Nonce, PK, Seconds, AppId> Default for Params<Nonce, PK, Seconds, AppId>
where
Nonce: Default,
Vec<PK>: Default, // This is crucial for initializing an empty vector of PK
Seconds: Default,
AppId: Default,
{
fn default() -> Self {
Self {
nonce: Nonce::default(),
participants: Vec::default(), // Initialize an empty vector of PK
challenge_duration: Seconds::default(),
app: AppId::default(),
}
}
}
#[derive(Encode, Decode, Default, Clone, PartialEq, RuntimeDebug, TypeInfo)]
#[codec(dumb_trait_bound)]
/// Off-Chain state of a channel.
pub struct State<ChannelId, Version, Balance> {
/// Unique channel ID.
///
/// It is calculated from the channel's [Params] with [Params::channel_id].
/// This locks all parameters in place and ensures that a participant
/// that signed a state also signed the parameters of a channel.
pub channel_id: ChannelId,
/// Version of the state.
///
/// Higher version values can override states with lower versions.
/// An honest participant will never sign two states with the same version.
pub version: Version,
/// Balance distribution per participants.
///
/// Must be the same size as [Params::participants].
/// The `balances` of a final state describe the 'outcome' of a channel.
pub balances: Vec<Balance>,
/// Whether or not this state is final.
///
/// Final states define the last state of a channel.
/// An honest participant will never sign another state after he signed a
/// final state.
pub finalized: bool,
// App data.
pub data: AppData,
}
#[derive(Encode, Decode, Copy, Clone, PartialEq, RuntimeDebug, TypeInfo)]
pub enum Phase {
Register,
Progress,
Conclude,
}
#[derive(Encode, Decode, Copy, Clone, PartialEq, RuntimeDebug, TypeInfo)]
#[codec(dumb_trait_bound)]
/// Off-chain [State] that was registered on-chain.
///
/// Is used to track disputes and concluded channels.
pub struct RegisteredState<State, Seconds> {
// The protocol phase.
pub phase: Phase,
/// The registered state.
pub state: State,
/// Timeout in case that it is a dispute.
///
/// Has no meaning for final states.
pub timeout: Seconds,
}
#[derive(Encode, Decode, Copy, Clone, PartialEq, RuntimeDebug, TypeInfo)]
#[codec(dumb_trait_bound)]
/// Withdrawal authorization for on-chain funds.
///
/// This is signed by an off-chain participant too authorize
/// on-chain funds withdrawal to a specific on-chain account.
///
/// NOTE: The signature is not part of the struct.
pub struct Withdrawal<ChannelId, PK, AccountId> {
/// Channel from with to withdraw.
pub channel_id: ChannelId,
/// Off-chain participant to debit.
pub part: PK,
/// On-Chain Account to credited.
pub receiver: AccountId,
}
impl<ChannelId, PK, AccountId> Default for Withdrawal<ChannelId, PK, AccountId>
where
ChannelId: Default,
PK: ByteArray + MaxEncodedLen,
AccountId: Default,
{
fn default() -> Self {
let array_len = PK::max_encoded_len();
// Create a zero-initialized byte array of the appropriate length
let zero_array = vec![0u8; array_len];
// Attempt to create a `PK` instance from the zero-initialized byte array
let part = match PK::from_slice(&zero_array) {
Ok(part) => part,
Err(_) => {
// If creation fails, handle the error (e.g., log an error message or panic)
panic!("Error creating PK instance from zero-initialized array");
}
};
Self {
channel_id: ChannelId::default(),
part,
receiver: AccountId::default(),
}
}
}
#[derive(Encode, Decode, Default, Copy, Clone, PartialEq, RuntimeDebug)]
#[codec(dumb_trait_bound)]
/// Funding is exclusively used to calculate funding ids via [Funding::id].
pub struct Funding<ChannelId, PK> {
pub channel: ChannelId,
pub part: PK,
}
impl<Nonce, PK, Seconds, AppId: crate::AppId> Params<Nonce, PK, Seconds, AppId>
where
Params<Nonce, PK, Seconds, AppId>: Encode,
{
/// Calculates the Channel ID of the Params.
pub fn channel_id<T: Hasher>(&self) -> T::Out {
let encoded = Encode::encode(&self);
T::hash(&encoded)
}
pub fn has_app<T: Config<AppId = AppId>>(&self) -> bool {
let no_app = T::NoApp::get();
self.app != no_app
}
}
impl<ChannelId, Version, Balance> State<ChannelId, Version, Balance>
where
State<ChannelId, Version, Balance>: Encode,
{
/// Returns whether `sig` is a valid signature for this State and was
/// created by `PK`.
pub fn validate_sig<Sig: Verify<Signer = PK>, PK: IdentifyAccount<AccountId = PK>>(
&self,
sig: &Sig,
pk: &PK,
) -> bool {
let msg = Encode::encode(&self);
sig.verify(&*msg, pk)
}
}
impl<ChannelId, Pk, AccountId> Withdrawal<ChannelId, Pk, AccountId>
where
Withdrawal<ChannelId, Pk, AccountId>: Encode,
Pk: IdentifyAccount<AccountId = Pk>,
{
/// Returns whether `sig` is a valid signature for this Withdrawal
/// and was created by the participant that wants to claim the funds.
pub fn validate_sig<Sig: Verify<Signer = Pk>>(&self, sig: &Sig) -> bool {
let msg = Encode::encode(&self);
sig.verify(&*msg, &self.part)
}
}
impl<ChannelId, PK> Funding<ChannelId, PK>
where
Funding<ChannelId, PK>: Encode,
{
/// Calculates the funding id of a participant in a channel.
pub fn id<H: Hasher>(&self) -> H::Out {
let encoded = Encode::encode(&self);
H::hash(&encoded)
}
}