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
mod actor_code;
pub use self::actor_code::*;
use address::Address;
use cid::Cid;
use clock::ChainEpoch;
use commcid::data_commitment_v1_to_cid;
use crypto::{DomainSeparationTag, Signature};
use fil_types::{
zero_piece_commitment, NetworkVersion, PaddedPieceSize, PieceInfo, Randomness,
RegisteredSealProof, SealVerifyInfo, WindowPoStVerifyInfo,
};
use filecoin_proofs_api::seal::compute_comm_d;
use filecoin_proofs_api::{self as proofs};
use forest_encoding::{blake2b_256, de, Cbor};
use ipld_blockstore::BlockStore;
use std::collections::HashMap;
use std::convert::{TryFrom, TryInto};
use std::error::Error as StdError;
use vm::{ActorError, ExitCode, MethodNum, Serialized, TokenAmount};
pub trait Runtime<BS: BlockStore>: Syscalls {
fn network_version(&self) -> NetworkVersion;
fn message(&self) -> &dyn MessageInfo;
fn curr_epoch(&self) -> ChainEpoch;
fn validate_immediate_caller_accept_any(&mut self) -> Result<(), ActorError>;
fn validate_immediate_caller_is<'a, I>(&mut self, addresses: I) -> Result<(), ActorError>
where
I: IntoIterator<Item = &'a Address>;
fn validate_immediate_caller_type<'a, I>(&mut self, types: I) -> Result<(), ActorError>
where
I: IntoIterator<Item = &'a Cid>;
fn current_balance(&self) -> Result<TokenAmount, ActorError>;
fn resolve_address(&self, address: &Address) -> Result<Option<Address>, ActorError>;
fn get_actor_code_cid(&self, addr: &Address) -> Result<Option<Cid>, ActorError>;
fn get_randomness_from_tickets(
&self,
personalization: DomainSeparationTag,
rand_epoch: ChainEpoch,
entropy: &[u8],
) -> Result<Randomness, ActorError>;
fn get_randomness_from_beacon(
&self,
personalization: DomainSeparationTag,
rand_epoch: ChainEpoch,
entropy: &[u8],
) -> Result<Randomness, ActorError>;
fn create<C: Cbor>(&mut self, obj: &C) -> Result<(), ActorError>;
fn state<C: Cbor>(&self) -> Result<C, ActorError>;
fn transaction<C, RT, F>(&mut self, f: F) -> Result<RT, ActorError>
where
C: Cbor,
F: FnOnce(&mut C, &mut Self) -> Result<RT, ActorError>;
fn store(&self) -> &BS;
fn send(
&self,
to: Address,
method: MethodNum,
params: Serialized,
value: TokenAmount,
) -> Result<Serialized, ActorError>;
fn new_actor_address(&mut self) -> Result<Address, ActorError>;
fn create_actor(&mut self, code_id: Cid, address: &Address) -> Result<(), ActorError>;
fn delete_actor(&mut self, beneficiary: &Address) -> Result<(), ActorError>;
fn total_fil_circ_supply(&self) -> Result<TokenAmount, ActorError>;
fn charge_gas(&mut self, name: &'static str, compute: i64) -> Result<(), ActorError>;
fn deserialize_params<O: de::DeserializeOwned>(
&self,
params: &Serialized,
) -> Result<O, ActorError> {
params.deserialize().map_err(|e| {
if self.network_version() < NetworkVersion::V7 {
ActorError::new(
ExitCode::SysErrSenderInvalid,
format!("failed to decode parameters: {}", e),
)
} else {
ActorError::from(e).wrap("failed to decode parameters")
}
})
}
fn base_fee(&self) -> &TokenAmount;
}
pub trait MessageInfo {
fn caller(&self) -> &Address;
fn receiver(&self) -> &Address;
fn value_received(&self) -> &TokenAmount;
}
pub trait Syscalls {
fn verify_signature(
&self,
signature: &Signature,
signer: &Address,
plaintext: &[u8],
) -> Result<(), Box<dyn StdError>>;
fn hash_blake2b(&self, data: &[u8]) -> Result<[u8; 32], Box<dyn StdError>> {
Ok(blake2b_256(data))
}
fn compute_unsealed_sector_cid(
&self,
proof_type: RegisteredSealProof,
pieces: &[PieceInfo],
) -> Result<Cid, Box<dyn StdError>> {
compute_unsealed_sector_cid(proof_type, pieces)
}
fn verify_seal(&self, vi: &SealVerifyInfo) -> Result<(), Box<dyn StdError>>;
fn verify_post(&self, verify_info: &WindowPoStVerifyInfo) -> Result<bool, Box<dyn StdError>>;
fn verify_consensus_fault(
&self,
h1: &[u8],
h2: &[u8],
extra: &[u8],
) -> Result<Option<ConsensusFault>, Box<dyn StdError>>;
fn batch_verify_seals(
&self,
vis: &[(&Address, &Vec<SealVerifyInfo>)],
) -> Result<HashMap<Address, Vec<bool>>, Box<dyn StdError>> {
let mut verified = HashMap::new();
for (&addr, s) in vis.iter() {
let vals = s.iter().map(|si| self.verify_seal(si).is_ok()).collect();
verified.insert(addr, vals);
}
Ok(verified)
}
fn verify_aggregate_seals(
&self,
aggregate: &fil_types::AggregateSealVerifyProofAndInfos,
) -> Result<(), Box<dyn StdError>>;
}
#[derive(Clone)]
pub struct ConsensusFault {
pub target: Address,
pub epoch: ChainEpoch,
pub fault_type: ConsensusFaultType,
}
#[derive(Clone, Copy)]
pub enum ConsensusFaultType {
DoubleForkMining = 1,
ParentGrinding = 2,
TimeOffsetMining = 3,
}
fn get_required_padding(
old_length: PaddedPieceSize,
new_piece_length: PaddedPieceSize,
) -> (Vec<PaddedPieceSize>, PaddedPieceSize) {
let mut sum = 0;
let mut to_fill = 0u64.wrapping_sub(old_length.0) % new_piece_length.0;
let n = to_fill.count_ones();
let mut pad_pieces = Vec::with_capacity(n as usize);
for _ in 0..n {
let next = to_fill.trailing_zeros();
let p_size = 1 << next;
to_fill ^= p_size;
let padded = PaddedPieceSize(p_size);
pad_pieces.push(padded);
sum += padded.0;
}
(pad_pieces, PaddedPieceSize(sum))
}
pub fn compute_unsealed_sector_cid(
proof_type: RegisteredSealProof,
pieces: &[PieceInfo],
) -> Result<Cid, Box<dyn StdError>> {
let ssize = proof_type.sector_size()? as u64;
let mut all_pieces = Vec::<proofs::PieceInfo>::with_capacity(pieces.len());
let pssize = PaddedPieceSize(ssize);
if pieces.is_empty() {
all_pieces.push(proofs::PieceInfo {
size: pssize.unpadded().into(),
commitment: zero_piece_commitment(pssize),
})
} else {
let mut sum = PaddedPieceSize(0);
let pad_to = |pads: Vec<PaddedPieceSize>,
all_pieces: &mut Vec<proofs::PieceInfo>,
sum: &mut PaddedPieceSize| {
for p in pads {
all_pieces.push(proofs::PieceInfo {
size: p.unpadded().into(),
commitment: zero_piece_commitment(p),
});
sum.0 += p.0;
}
};
for p in pieces {
let (ps, _) = get_required_padding(sum, p.size);
pad_to(ps, &mut all_pieces, &mut sum);
all_pieces.push(proofs::PieceInfo::try_from(p)?);
sum.0 += p.size.0;
}
let (ps, _) = get_required_padding(sum, pssize);
pad_to(ps, &mut all_pieces, &mut sum);
}
let comm_d = compute_comm_d(proof_type.try_into()?, &all_pieces)?;
Ok(data_commitment_v1_to_cid(&comm_d)?)
}