-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathlib.rs
333 lines (277 loc) · 11.6 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
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
// Copyright 2019-2022 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0, MIT
use fil_actors_runtime::runtime::builtins::Type;
use fil_actors_runtime::runtime::{ActorCode, Runtime};
use fil_actors_runtime::{
actor_dispatch, actor_error, deserialize_block, extract_send_result, resolve_to_actor_id,
ActorResult, ActorDowncast, ActorError, Array,
};
use fvm_ipld_blockstore::Blockstore;
use fvm_ipld_encoding::CBOR;
use fvm_shared::address::Address;
use fvm_ipld_encoding::ipld_block::IpldBlock;
use fvm_shared::econ::TokenAmount;
use fvm_shared::error::ExitCode;
use fvm_shared::sys::SendFlags;
use fvm_shared::{METHOD_CONSTRUCTOR, METHOD_SEND};
use num_derive::FromPrimitive;
use num_traits::Zero;
pub use self::state::{LaneState, Merge, State};
pub use self::types::*;
#[cfg(feature = "fil-actor")]
fil_actors_runtime::wasm_trampoline!(Actor);
pub mod ext;
mod state;
pub mod testing;
mod types;
// * Updated to specs-actors commit: f47f461b0588e9f0c20c999f6f129c85d669a7aa (v3.0.2)
/// Payment Channel actor methods available
#[derive(FromPrimitive)]
#[repr(u64)]
pub enum Method {
Constructor = METHOD_CONSTRUCTOR,
UpdateChannelState = 2,
Settle = 3,
Collect = 4,
}
pub const ERR_CHANNEL_STATE_UPDATE_AFTER_SETTLED: ExitCode = ExitCode::new(32);
/// Payment Channel actor
pub struct Actor;
impl Actor {
/// Constructor for Payment channel actor
pub fn constructor(rt: &impl Runtime, params: ConstructorParams) -> Result<(), ActorError> {
// Only InitActor can create a payment channel actor. It creates the actor on
// behalf of the payer/payee.
rt.validate_immediate_caller_type(std::iter::once(&Type::Init))?;
// Check both parties are capable of signing vouchers
let to = resolve_to_actor_id(rt, ¶ms.to, true).map(Address::new_id)?;
let from = resolve_to_actor_id(rt, ¶ms.from, true).map(Address::new_id)?;
let empty_arr_cid =
Array::<(), _>::new_with_bit_width(rt.store(), LANE_STATES_AMT_BITWIDTH)
.flush()
.map_err(|e| {
e.downcast_default(ExitCode::USR_ILLEGAL_STATE, "failed to create empty AMT")
})?;
rt.create(&State::new(from, to, empty_arr_cid))?;
Ok(())
}
pub fn update_channel_state(
rt: &impl Runtime,
params: UpdateChannelStateParams,
) -> Result<(), ActorError> {
let st: State = rt.state()?;
rt.validate_immediate_caller_is([st.from, st.to].iter())?;
let signer = if rt.message().caller() == st.from { st.to } else { st.from };
let sv = params.sv;
// Pull signature from signed voucher
let sig = &sv
.signature
.as_ref()
.ok_or_else(|| actor_error!(illegal_argument, "voucher has no signature"))?
.bytes;
if st.settling_at != 0 && rt.curr_epoch() >= st.settling_at {
return Err(ActorError::unchecked(
ERR_CHANNEL_STATE_UPDATE_AFTER_SETTLED,
"no vouchers can be processed after settling at epoch".to_string(),
));
}
if params.secret.len() > MAX_SECRET_SIZE {
return Err(actor_error!(illegal_argument, "secret must be at most 256 bytes long"));
}
// Generate unsigned bytes
let sv_bz = sv.signing_bytes().map_err(|e| {
ActorError::serialization(format!("failed to serialized SignedVoucher: {}", e))
})?;
// Validate signature
if !extract_send_result(rt.send(
&signer,
ext::account::AUTHENTICATE_MESSAGE_METHOD,
IpldBlock::serialize_cbor(&ext::account::AuthenticateMessageParams {
signature: sig.to_vec(),
message: sv_bz,
})?,
TokenAmount::zero(),
None,
SendFlags::READ_ONLY,
))
.and_then(deserialize_block)
.context("proposal authentication failed")?
{
return Err(actor_error!(illegal_argument, "voucher sig authentication failed"));
}
let pch_addr = rt.message().receiver();
let svpch_id = rt.resolve_address(&sv.channel_addr).ok_or_else(|| {
actor_error!(
illegal_argument,
"voucher payment channel address {} does not resolve to an ID address",
sv.channel_addr
)
})?;
if pch_addr != Address::new_id(svpch_id) {
return Err(actor_error!(illegal_argument;
"voucher payment channel address {} does not match receiver {}",
svpch_id, pch_addr));
}
if rt.curr_epoch() < sv.time_lock_min {
return Err(actor_error!(illegal_argument; "cannot use this voucher yet"));
}
if sv.time_lock_max != 0 && rt.curr_epoch() > sv.time_lock_max {
return Err(actor_error!(illegal_argument; "this voucher has expired"));
}
if sv.amount.is_negative() {
return Err(actor_error!(illegal_argument;
"voucher amount must be non-negative, was {}", sv.amount));
}
if !sv.secret_pre_image.is_empty() {
let hashed_secret: &[u8] = &rt.hash_blake2b(¶ms.secret);
if hashed_secret != sv.secret_pre_image.as_slice() {
return Err(actor_error!(illegal_argument; "incorrect secret"));
}
}
if let Some(extra) = &sv.extra {
extract_send_result(rt.send_simple(
&extra.actor,
extra.method,
Some(IpldBlock { codec: CBOR, data: extra.data.to_vec() }),
TokenAmount::zero(),
))
.map_err(|e| e.wrap("spend voucher verification failed"))?;
}
rt.transaction(|st: &mut State, rt| {
let mut l_states = Array::load(&st.lane_states, rt.store()).map_err(|e| {
e.downcast_default(ExitCode::USR_ILLEGAL_STATE, "failed to load lane states")
})?;
// Find the voucher lane, create and insert it in sorted order if necessary.
let lane_id = sv.lane;
let lane_state = find_lane(&l_states, lane_id)?;
let mut lane_state = if let Some(state) = lane_state {
if state.nonce >= sv.nonce {
return Err(actor_error!(illegal_argument;
"voucher has an outdated nonce, existing: {}, voucher: {}, cannot redeem",
state.nonce, sv.nonce));
}
state.clone()
} else {
LaneState::default()
};
// The next section actually calculates the payment amounts to update
// the payment channel state
// 1. (optional) sum already redeemed value of all merging lanes
let mut redeemed_from_others = TokenAmount::zero();
for merge in sv.merges {
if merge.lane == sv.lane {
return Err(actor_error!(illegal_argument;
"voucher cannot merge lanes into it's own lane"));
}
let mut other_ls = find_lane(&l_states, merge.lane)?
.ok_or_else(|| {
actor_error!(illegal_argument;
"voucher specifies invalid merge lane {}", merge.lane)
})?
.clone();
if other_ls.nonce >= merge.nonce {
return Err(actor_error!(illegal_argument;
"merged lane in voucher has outdated nonce, cannot redeem"));
}
redeemed_from_others += &other_ls.redeemed;
other_ls.nonce = merge.nonce;
l_states.set(merge.lane, other_ls).map_err(|e| {
e.downcast_default(
ExitCode::USR_ILLEGAL_STATE,
format!("failed to store lane {}", merge.lane),
)
})?;
}
// 2. To prevent double counting, remove already redeemed amounts (from
// voucher or other lanes) from the voucher amount
lane_state.nonce = sv.nonce;
let balance_delta = &sv.amount - (redeemed_from_others + &lane_state.redeemed);
// 3. set new redeemed value for merged-into lane
lane_state.redeemed = sv.amount;
// 4. check operation validity
let new_send_balance = balance_delta + &st.to_send;
if new_send_balance < TokenAmount::zero() {
return Err(actor_error!(illegal_argument;
"voucher would leave channel balance negative"));
}
if new_send_balance > rt.current_balance() {
return Err(actor_error!(illegal_argument;
"not enough funds in channel to cover voucher"));
}
// 5. add new redemption ToSend
st.to_send = new_send_balance;
// update channel settlingAt and MinSettleHeight if delayed by voucher
if sv.min_settle_height != 0 {
if st.settling_at != 0 && st.settling_at < sv.min_settle_height {
st.settling_at = sv.min_settle_height;
}
if st.min_settle_height < sv.min_settle_height {
st.min_settle_height = sv.min_settle_height;
}
}
l_states.set(lane_id, lane_state).map_err(|e| {
e.downcast_default(
ExitCode::USR_ILLEGAL_STATE,
format!("failed to store lane {}", lane_id),
)
})?;
st.lane_states = l_states.flush().map_err(|e| {
e.downcast_default(ExitCode::USR_ILLEGAL_STATE, "failed to save lanes")
})?;
Ok(())
})
}
pub fn settle(rt: &impl Runtime) -> Result<(), ActorError> {
rt.transaction(|st: &mut State, rt| {
rt.validate_immediate_caller_is([st.from, st.to].iter())?;
if st.settling_at != 0 {
return Err(actor_error!(illegal_state; "channel already settling"));
}
st.settling_at = rt.curr_epoch() + SETTLE_DELAY;
if st.settling_at < st.min_settle_height {
st.settling_at = st.min_settle_height;
}
Ok(())
})
}
pub fn collect(rt: &impl Runtime) -> Result<(), ActorError> {
let st: State = rt.state()?;
rt.validate_immediate_caller_is(&[st.from, st.to])?;
if st.settling_at == 0 || rt.curr_epoch() < st.settling_at {
return Err(actor_error!(forbidden; "payment channel not settling or settled"));
}
// send ToSend to `to`
extract_send_result(rt.send_simple(&st.to, METHOD_SEND, None, st.to_send))
.map_err(|e| e.wrap("Failed to send funds to `to` address"))?;
// the remaining balance will be returned to "From" upon deletion.
rt.delete_actor(&st.from)?;
Ok(())
}
}
#[inline]
fn find_lane<'a, BS>(
ls: &'a Array<LaneState, BS>,
id: u64,
) -> Result<Option<&'a LaneState>, ActorError>
where
BS: Blockstore,
{
if id > MAX_LANE {
return Err(actor_error!(illegal_argument; "maximum lane ID is 2^63-1"));
}
ls.get(id).map_err(|e| {
e.downcast_default(ExitCode::USR_ILLEGAL_STATE, format!("failed to load lane {}", id))
})
}
impl ActorCode for Actor {
type Methods = Method;
fn name() -> &'static str {
"PaymentChannel"
}
actor_dispatch! {
Constructor => constructor,
UpdateChannelState => update_channel_state,
Settle => settle,
Collect => collect,
}
}