-
Notifications
You must be signed in to change notification settings - Fork 188
Expand file tree
/
Copy pathf3.rs
More file actions
965 lines (863 loc) · 34.4 KB
/
f3.rs
File metadata and controls
965 lines (863 loc) · 34.4 KB
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
// Copyright 2019-2026 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0, MIT
//!
//! This module contains F3(fast finality) related V1 RPC methods
//! as well as some internal RPC methods(F3.*) that power
//! the go-f3 node in sidecar mode.
//!
mod types;
mod util;
pub use self::types::{
F3InstanceProgress, F3LeaseManager, F3Manifest, F3PowerEntry, FinalityCertificate,
};
use self::{types::*, util::*};
use super::wallet::WalletSign;
use crate::shim::actors::{miner, power};
use crate::{
blocks::Tipset,
chain::index::ResolveNullTipset,
chain_sync::TipsetValidator,
db::{
BlockstoreReadCacheStats as _, BlockstoreWithReadCache, DefaultBlockstoreReadCacheStats,
LruBlockstoreReadCache,
},
libp2p::{NetRPCMethods, NetworkMessage},
lotus_json::{HasLotusJson as _, LotusJson},
rpc::{ApiPaths, Ctx, Permission, RpcMethod, ServerError, types::ApiTipsetKey},
shim::{
address::{Address, Protocol},
clock::ChainEpoch,
crypto::Signature,
},
utils::misc::env::is_env_set_and_truthy,
};
use ahash::{HashMap, HashSet};
use anyhow::Context as _;
use cid::Cid;
use enumflags2::BitFlags;
use fvm_ipld_blockstore::Blockstore;
use jsonrpsee::core::{client::ClientT as _, params::ArrayParams};
use libp2p::PeerId;
use nonzero_ext::nonzero;
use num::Signed as _;
use parking_lot::RwLock;
use std::num::NonZeroUsize;
use std::{
borrow::Cow,
fmt::Display,
str::FromStr as _,
sync::{Arc, LazyLock, OnceLock},
};
pub static F3_LEASE_MANAGER: OnceLock<F3LeaseManager> = OnceLock::new();
pub enum GetRawNetworkName {}
impl RpcMethod<0> for GetRawNetworkName {
const NAME: &'static str = "F3.GetRawNetworkName";
const PARAM_NAMES: [&'static str; 0] = [];
const API_PATHS: BitFlags<ApiPaths> = ApiPaths::all();
const PERMISSION: Permission = Permission::Read;
type Params = ();
type Ok = String;
async fn handle(
ctx: Ctx<impl Blockstore>,
(): Self::Params,
_: &http::Extensions,
) -> Result<Self::Ok, ServerError> {
Ok(ctx.chain_config().network.genesis_name().into())
}
}
pub enum GetTipsetByEpoch {}
impl RpcMethod<1> for GetTipsetByEpoch {
const NAME: &'static str = "F3.GetTipsetByEpoch";
const PARAM_NAMES: [&'static str; 1] = ["epoch"];
const API_PATHS: BitFlags<ApiPaths> = ApiPaths::all();
const PERMISSION: Permission = Permission::Read;
type Params = (ChainEpoch,);
type Ok = F3TipSet;
async fn handle(
ctx: Ctx<impl Blockstore>,
(epoch,): Self::Params,
_: &http::Extensions,
) -> Result<Self::Ok, ServerError> {
let ts = ctx.chain_index().tipset_by_height(
epoch,
ctx.chain_store().heaviest_tipset(),
ResolveNullTipset::TakeOlder,
)?;
Ok(ts.into())
}
}
pub enum GetTipset {}
impl RpcMethod<1> for GetTipset {
const NAME: &'static str = "F3.GetTipset";
const PARAM_NAMES: [&'static str; 1] = ["tipset_key"];
const API_PATHS: BitFlags<ApiPaths> = ApiPaths::all();
const PERMISSION: Permission = Permission::Read;
type Params = (F3TipSetKey,);
type Ok = F3TipSet;
async fn handle(
ctx: Ctx<impl Blockstore>,
(f3_tsk,): Self::Params,
_: &http::Extensions,
) -> Result<Self::Ok, ServerError> {
let tsk = f3_tsk.try_into()?;
let ts = ctx.chain_index().load_required_tipset(&tsk)?;
Ok(ts.into())
}
}
pub enum GetHead {}
impl RpcMethod<0> for GetHead {
const NAME: &'static str = "F3.GetHead";
const PARAM_NAMES: [&'static str; 0] = [];
const API_PATHS: BitFlags<ApiPaths> = ApiPaths::all();
const PERMISSION: Permission = Permission::Read;
type Params = ();
type Ok = F3TipSet;
async fn handle(
ctx: Ctx<impl Blockstore>,
_: Self::Params,
_: &http::Extensions,
) -> Result<Self::Ok, ServerError> {
Ok(ctx.chain_store().heaviest_tipset().into())
}
}
pub enum GetParent {}
impl RpcMethod<1> for GetParent {
const NAME: &'static str = "F3.GetParent";
const PARAM_NAMES: [&'static str; 1] = ["tipset_key"];
const API_PATHS: BitFlags<ApiPaths> = ApiPaths::all();
const PERMISSION: Permission = Permission::Read;
type Params = (F3TipSetKey,);
type Ok = F3TipSet;
async fn handle(
ctx: Ctx<impl Blockstore>,
(f3_tsk,): Self::Params,
_: &http::Extensions,
) -> Result<Self::Ok, ServerError> {
let tsk = f3_tsk.try_into()?;
let ts = ctx.chain_index().load_required_tipset(&tsk)?;
let parent = ctx.chain_index().load_required_tipset(ts.parents())?;
Ok(parent.into())
}
}
pub enum GetPowerTable {}
impl GetPowerTable {
async fn compute(
ctx: &Ctx<impl Blockstore + Send + Sync + 'static>,
ts: &Tipset,
) -> anyhow::Result<Vec<F3PowerEntry>> {
// The RAM overhead on mainnet is ~14MiB
const BLOCKSTORE_CACHE_CAP: NonZeroUsize = nonzero!(65536_usize);
static BLOCKSTORE_CACHE: LazyLock<LruBlockstoreReadCache> = LazyLock::new(|| {
LruBlockstoreReadCache::new_with_metrics("get_powertable".into(), BLOCKSTORE_CACHE_CAP)
});
let db = BlockstoreWithReadCache::new(
ctx.store_owned(),
BLOCKSTORE_CACHE.clone(),
Some(DefaultBlockstoreReadCacheStats::default()),
);
macro_rules! handle_miner_state_v12_on {
($version:tt, $id_power_worker_mappings:ident, $ts:expr, $state:expr, $policy:expr) => {
fn map_err<E: Display>(e: E) -> fil_actors_shared::$version::ActorError {
fil_actors_shared::$version::ActorError::unspecified(e.to_string())
}
let claims = $state.load_claims(&db)?;
claims.for_each(|miner, claim| {
if !claim.quality_adj_power.is_positive() {
return Ok(());
}
let id = miner.id().map_err(map_err)?;
let (_, ok) =
$state.miner_nominal_power_meets_consensus_minimum($policy, &db, id)?;
if !ok {
return Ok(());
}
let power = claim.quality_adj_power.clone();
let miner_state: miner::State = ctx
.state_manager
.get_actor_state_from_address($ts, &miner.into())
.map_err(map_err)?;
let debt = miner_state.fee_debt();
if !debt.is_zero() {
// fee debt don't add the miner to power table
return Ok(());
}
let miner_info = miner_state.info(&db).map_err(map_err)?;
// check consensus faults
if $ts.epoch() <= miner_info.consensus_fault_elapsed {
return Ok(());
}
$id_power_worker_mappings.push((id, power, miner_info.worker.into()));
Ok(())
})?;
};
}
let state: power::State = ctx.state_manager.get_actor_state(ts)?;
let mut id_power_worker_mappings = vec![];
let policy = &ctx.chain_config().policy;
match &state {
power::State::V8(s) => {
fn map_err<E: Display>(e: E) -> fil_actors_shared::v8::ActorError {
fil_actors_shared::v8::ActorError::unspecified(e.to_string())
}
let claims = fil_actors_shared::v8::make_map_with_root::<
_,
fil_actor_power_state::v8::Claim,
>(&s.claims, &db)?;
claims.for_each(|key, claim| {
let miner = Address::from_bytes(key)?;
if !claim.quality_adj_power.is_positive() {
return Ok(());
}
let id = miner.id().map_err(map_err)?;
let ok = s.miner_nominal_power_meets_consensus_minimum(
&policy.into(),
&db,
&miner.into(),
)?;
if !ok {
return Ok(());
}
let power = claim.quality_adj_power.clone();
let miner_state: miner::State = ctx
.state_manager
.get_actor_state_from_address(ts, &miner)
.map_err(map_err)?;
let debt = miner_state.fee_debt();
if !debt.is_zero() {
// fee debt don't add the miner to power table
return Ok(());
}
let miner_info = miner_state.info(&db).map_err(map_err)?;
// check consensus faults
if ts.epoch() <= miner_info.consensus_fault_elapsed {
return Ok(());
}
id_power_worker_mappings.push((id, power, miner_info.worker));
Ok(())
})?;
}
power::State::V9(s) => {
fn map_err<E: Display>(e: E) -> fil_actors_shared::v9::ActorError {
fil_actors_shared::v9::ActorError::unspecified(e.to_string())
}
let claims = fil_actors_shared::v9::make_map_with_root::<
_,
fil_actor_power_state::v9::Claim,
>(&s.claims, &db)?;
claims.for_each(|key, claim| {
let miner = Address::from_bytes(key)?;
if !claim.quality_adj_power.is_positive() {
return Ok(());
}
let id = miner.id().map_err(map_err)?;
let ok = s.miner_nominal_power_meets_consensus_minimum(
&policy.into(),
&db,
&miner.into(),
)?;
if !ok {
return Ok(());
}
let power = claim.quality_adj_power.clone();
let miner_state: miner::State = ctx
.state_manager
.get_actor_state_from_address(ts, &miner)
.map_err(map_err)?;
let debt = miner_state.fee_debt();
if !debt.is_zero() {
// fee debt don't add the miner to power table
return Ok(());
}
let miner_info = miner_state.info(&db).map_err(map_err)?;
// check consensus faults
if ts.epoch() <= miner_info.consensus_fault_elapsed {
return Ok(());
}
id_power_worker_mappings.push((id, power, miner_info.worker));
Ok(())
})?;
}
power::State::V10(s) => {
fn map_err<E: Display>(e: E) -> fil_actors_shared::v10::ActorError {
fil_actors_shared::v10::ActorError::unspecified(e.to_string())
}
let claims = fil_actors_shared::v10::make_map_with_root::<
_,
fil_actor_power_state::v10::Claim,
>(&s.claims, &db)?;
claims.for_each(|key, claim| {
let miner = Address::from_bytes(key)?;
if !claim.quality_adj_power.is_positive() {
return Ok(());
}
let id = miner.id().map_err(map_err)?;
let (_, ok) =
s.miner_nominal_power_meets_consensus_minimum(&policy.into(), &db, id)?;
if !ok {
return Ok(());
}
let power = claim.quality_adj_power.clone();
let miner_state: miner::State = ctx
.state_manager
.get_actor_state_from_address(ts, &miner)
.map_err(map_err)?;
let debt = miner_state.fee_debt();
if !debt.is_zero() {
// fee debt don't add the miner to power table
return Ok(());
}
let miner_info = miner_state.info(&db).map_err(map_err)?;
// check consensus faults
if ts.epoch() <= miner_info.consensus_fault_elapsed {
return Ok(());
}
id_power_worker_mappings.push((id, power, miner_info.worker));
Ok(())
})?;
}
power::State::V11(s) => {
fn map_err<E: Display>(e: E) -> fil_actors_shared::v11::ActorError {
fil_actors_shared::v11::ActorError::unspecified(e.to_string())
}
let claims = fil_actors_shared::v11::make_map_with_root::<
_,
fil_actor_power_state::v11::Claim,
>(&s.claims, &db)?;
claims.for_each(|key, claim| {
let miner = Address::from_bytes(key)?;
if !claim.quality_adj_power.is_positive() {
return Ok(());
}
let id = miner.id().map_err(map_err)?;
let (_, ok) =
s.miner_nominal_power_meets_consensus_minimum(&policy.into(), &db, id)?;
if !ok {
return Ok(());
}
let power = claim.quality_adj_power.clone();
let miner_state: miner::State = ctx
.state_manager
.get_actor_state_from_address(ts, &miner)
.map_err(map_err)?;
let debt = miner_state.fee_debt();
if !debt.is_zero() {
// fee debt don't add the miner to power table
return Ok(());
}
let miner_info = miner_state.info(&db).map_err(map_err)?;
// check consensus faults
if ts.epoch() <= miner_info.consensus_fault_elapsed {
return Ok(());
}
id_power_worker_mappings.push((id, power, miner_info.worker));
Ok(())
})?;
}
power::State::V12(s) => {
handle_miner_state_v12_on!(v12, id_power_worker_mappings, &ts, s, &policy.into());
}
power::State::V13(s) => {
handle_miner_state_v12_on!(v13, id_power_worker_mappings, &ts, s, &policy.into());
}
power::State::V14(s) => {
handle_miner_state_v12_on!(v14, id_power_worker_mappings, &ts, s, &policy.into());
}
power::State::V15(s) => {
handle_miner_state_v12_on!(v15, id_power_worker_mappings, &ts, s, &policy.into());
}
power::State::V16(s) => {
handle_miner_state_v12_on!(v16, id_power_worker_mappings, &ts, s, &policy.into());
}
power::State::V17(s) => {
handle_miner_state_v12_on!(v17, id_power_worker_mappings, &ts, s, &policy.into());
}
}
let mut power_entries = vec![];
for (id, power, worker) in id_power_worker_mappings {
let waddr = ctx
.state_manager
.resolve_to_deterministic_address(worker, ts)
.await?;
if waddr.protocol() != Protocol::BLS {
anyhow::bail!("wrong type of worker address");
}
let pub_key = waddr.payload_bytes();
power_entries.push(F3PowerEntry { id, power, pub_key });
}
power_entries.sort();
if let Some(stats) = db.stats() {
tracing::debug!(epoch=%ts.epoch(), hit=%stats.hit(), miss=%stats.miss(),cache_len=%BLOCKSTORE_CACHE.len(), "F3.GetPowerTable blockstore read cache");
}
Ok(power_entries)
}
}
impl RpcMethod<1> for GetPowerTable {
const NAME: &'static str = "F3.GetPowerTable";
const PARAM_NAMES: [&'static str; 1] = ["tipset_key"];
const API_PATHS: BitFlags<ApiPaths> = ApiPaths::all();
const PERMISSION: Permission = Permission::Read;
type Params = (F3TipSetKey,);
type Ok = Vec<F3PowerEntry>;
async fn handle(
ctx: Ctx<impl Blockstore + Send + Sync + 'static>,
(f3_tsk,): Self::Params,
_: &http::Extensions,
) -> Result<Self::Ok, ServerError> {
let tsk = f3_tsk.try_into()?;
let start = std::time::Instant::now();
let ts = ctx.chain_index().load_required_tipset(&tsk)?;
let power_entries = Self::compute(&ctx, &ts).await?;
tracing::debug!(epoch=%ts.epoch(), %tsk, "F3.GetPowerTable, took {}", humantime::format_duration(start.elapsed()));
Ok(power_entries)
}
}
pub enum ProtectPeer {}
impl RpcMethod<1> for ProtectPeer {
const NAME: &'static str = "F3.ProtectPeer";
const PARAM_NAMES: [&'static str; 1] = ["peer_id"];
const API_PATHS: BitFlags<ApiPaths> = ApiPaths::all();
const PERMISSION: Permission = Permission::Read;
type Params = (String,);
type Ok = bool;
async fn handle(
ctx: Ctx<impl Blockstore>,
(peer_id,): Self::Params,
_: &http::Extensions,
) -> Result<Self::Ok, ServerError> {
let peer_id = PeerId::from_str(&peer_id)?;
let (tx, rx) = flume::bounded(1);
ctx.network_send()
.send_async(NetworkMessage::JSONRPCRequest {
method: NetRPCMethods::ProtectPeer(tx, std::iter::once(peer_id).collect()),
})
.await?;
rx.recv_async().await?;
Ok(true)
}
}
pub enum GetParticipatingMinerIDs {}
impl RpcMethod<0> for GetParticipatingMinerIDs {
const NAME: &'static str = "F3.GetParticipatingMinerIDs";
const PARAM_NAMES: [&'static str; 0] = [];
const API_PATHS: BitFlags<ApiPaths> = ApiPaths::all();
const PERMISSION: Permission = Permission::Read;
type Params = ();
type Ok = Vec<u64>;
async fn handle(
_: Ctx<impl Blockstore>,
_: Self::Params,
_: &http::Extensions,
) -> Result<Self::Ok, ServerError> {
let participants = F3ListParticipants::run().await?;
let mut ids: HashSet<u64> = participants.into_iter().map(|p| p.miner_id).collect();
if let Some(permanent_miner_ids) = (*F3_PERMANENT_PARTICIPATING_MINER_IDS).clone() {
ids.extend(permanent_miner_ids);
}
Ok(ids.into_iter().collect())
}
}
pub enum Finalize {}
impl RpcMethod<1> for Finalize {
const NAME: &'static str = "F3.Finalize";
const PARAM_NAMES: [&'static str; 1] = ["tipset_key"];
const API_PATHS: BitFlags<ApiPaths> = ApiPaths::all();
const PERMISSION: Permission = Permission::Write;
type Params = (F3TipSetKey,);
type Ok = ();
async fn handle(
ctx: Ctx<impl Blockstore>,
(f3_tsk,): Self::Params,
_: &http::Extensions,
) -> Result<Self::Ok, ServerError> {
// Respect the environment variable when set, and fallback to chain config when not set.
static ENV_ENABLED: LazyLock<Option<bool>> =
LazyLock::new(|| is_env_set_and_truthy("FOREST_F3_CONSENSUS_ENABLED"));
let enabled = ENV_ENABLED.unwrap_or(ctx.chain_config().f3_consensus);
if !enabled {
return Ok(());
}
let tsk = f3_tsk.try_into()?;
let finalized_ts = match ctx.chain_index().load_tipset(&tsk)? {
Some(ts) => ts,
None => ctx
.sync_network_context
.chain_exchange_headers(None, &tsk, nonzero!(1_u64))
.await?
.first()
.cloned()
.with_context(|| format!("failed to get tipset via chain exchange. tsk: {tsk}"))?,
};
let head = ctx.chain_store().heaviest_tipset();
// When finalized_ts is not part of the current chain,
// reset the current head to finalized_ts.
// Note that when finalized_ts is newer than head or older than head - chain_finality,
// we don't reset the head to allow the chain or F3 to catch up.
if head.epoch() >= finalized_ts.epoch()
&& head.epoch() <= finalized_ts.epoch() + ctx.chain_config().policy.chain_finality
{
tracing::debug!(
"F3 finalized tsk {} at epoch {}",
finalized_ts.key(),
finalized_ts.epoch()
);
if !head
.chain(ctx.store())
.take_while(|ts| ts.epoch() >= finalized_ts.epoch())
.any(|ts| ts == finalized_ts)
{
tracing::info!(
"F3 reset chain head to tsk {} at epoch {}",
finalized_ts.key(),
finalized_ts.epoch()
);
let fts = ctx
.sync_network_context
.chain_exchange_full_tipset(None, &tsk)
.await?;
fts.persist(ctx.store())?;
let validator = TipsetValidator(&fts);
validator.validate(
ctx.chain_store(),
None,
&ctx.chain_store().genesis_tipset(),
ctx.chain_config().block_delay_secs,
)?;
let ts = Arc::new(Tipset::from(fts));
ctx.chain_store().put_tipset(&ts)?;
ctx.chain_store()
.set_heaviest_tipset(finalized_ts.clone())?;
}
ctx.chain_store().set_f3_finalized_tipset(finalized_ts);
}
Ok(())
}
}
pub enum SignMessage {}
impl RpcMethod<2> for SignMessage {
const NAME: &'static str = "F3.SignMessage";
const PARAM_NAMES: [&'static str; 2] = ["pubkey", "message"];
const API_PATHS: BitFlags<ApiPaths> = ApiPaths::all();
const PERMISSION: Permission = Permission::Sign;
type Params = (Vec<u8>, Vec<u8>);
type Ok = Signature;
async fn handle(
ctx: Ctx<impl Blockstore + Send + Sync + 'static>,
(pubkey, message): Self::Params,
ext: &http::Extensions,
) -> Result<Self::Ok, ServerError> {
let addr = Address::new_bls(&pubkey)?;
// Signing can be delegated to curio, we will follow how lotus does it once the feature lands.
WalletSign::handle(ctx, (addr, message), ext).await
}
}
pub enum F3ExportLatestSnapshot {}
impl F3ExportLatestSnapshot {
pub async fn run(path: String) -> anyhow::Result<Cid> {
let client = get_rpc_http_client()?;
let mut params = ArrayParams::new();
params.insert(path)?;
let LotusJson(cid): LotusJson<Cid> = client
.request("Filecoin.F3ExportLatestSnapshot", params)
.await?;
Ok(cid)
}
}
impl RpcMethod<1> for F3ExportLatestSnapshot {
const NAME: &'static str = "F3.ExportLatestSnapshot";
const PARAM_NAMES: [&'static str; 1] = ["path"];
const API_PATHS: BitFlags<ApiPaths> = ApiPaths::all();
const PERMISSION: Permission = Permission::Read;
const DESCRIPTION: Option<&'static str> =
Some("Exports the latest F3 snapshot to the specified path and returns its CID");
type Params = (String,);
type Ok = Cid;
async fn handle(
_ctx: Ctx<impl Blockstore>,
(path,): Self::Params,
_: &http::Extensions,
) -> Result<Self::Ok, ServerError> {
Ok(Self::run(path).await?)
}
}
/// returns a finality certificate at given instance number
pub enum F3GetCertificate {}
impl RpcMethod<1> for F3GetCertificate {
const NAME: &'static str = "Filecoin.F3GetCertificate";
const PARAM_NAMES: [&'static str; 1] = ["instance"];
const API_PATHS: BitFlags<ApiPaths> = ApiPaths::all();
const PERMISSION: Permission = Permission::Read;
type Params = (u64,);
type Ok = FinalityCertificate;
async fn handle(
_: Ctx<impl Blockstore>,
(instance,): Self::Params,
_: &http::Extensions,
) -> Result<Self::Ok, ServerError> {
let client = get_rpc_http_client()?;
let mut params = ArrayParams::new();
params.insert(instance)?;
let response: LotusJson<Self::Ok> = client.request(Self::NAME, params).await?;
Ok(response.into_inner())
}
}
/// returns the latest finality certificate
pub enum F3GetLatestCertificate {}
impl F3GetLatestCertificate {
/// Fetches the latest finality certificate via RPC.
pub async fn get() -> anyhow::Result<FinalityCertificate> {
let client = get_rpc_http_client()?;
let response: LotusJson<FinalityCertificate> = client
.request(<Self as RpcMethod<0>>::NAME, ArrayParams::new())
.await?;
Ok(response.into_inner())
}
}
impl RpcMethod<0> for F3GetLatestCertificate {
const NAME: &'static str = "Filecoin.F3GetLatestCertificate";
const PARAM_NAMES: [&'static str; 0] = [];
const API_PATHS: BitFlags<ApiPaths> = ApiPaths::all();
const PERMISSION: Permission = Permission::Read;
type Params = ();
type Ok = FinalityCertificate;
async fn handle(
_: Ctx<impl Blockstore + Send + Sync + 'static>,
_: Self::Params,
_: &http::Extensions,
) -> Result<Self::Ok, ServerError> {
Ok(Self::get().await?)
}
}
pub enum F3GetECPowerTable {}
impl RpcMethod<1> for F3GetECPowerTable {
const NAME: &'static str = "Filecoin.F3GetECPowerTable";
const PARAM_NAMES: [&'static str; 1] = ["tipset_key"];
const API_PATHS: BitFlags<ApiPaths> = ApiPaths::all();
const PERMISSION: Permission = Permission::Read;
type Params = (ApiTipsetKey,);
type Ok = Vec<F3PowerEntry>;
async fn handle(
ctx: Ctx<impl Blockstore + Send + Sync + 'static>,
(ApiTipsetKey(tsk_opt),): Self::Params,
ext: &http::Extensions,
) -> Result<Self::Ok, ServerError> {
let tsk = tsk_opt.unwrap_or_else(|| ctx.chain_store().heaviest_tipset().key().clone());
GetPowerTable::handle(ctx, (tsk.into(),), ext).await
}
}
pub enum F3GetF3PowerTable {}
impl RpcMethod<1> for F3GetF3PowerTable {
const NAME: &'static str = "Filecoin.F3GetF3PowerTable";
const PARAM_NAMES: [&'static str; 1] = ["tipset_key"];
const API_PATHS: BitFlags<ApiPaths> = ApiPaths::all();
const PERMISSION: Permission = Permission::Read;
type Params = (ApiTipsetKey,);
type Ok = Vec<F3PowerEntry>;
async fn handle(
ctx: Ctx<impl Blockstore>,
(ApiTipsetKey(tsk_opt),): Self::Params,
_: &http::Extensions,
) -> Result<Self::Ok, ServerError> {
let tsk: F3TipSetKey = tsk_opt
.unwrap_or_else(|| ctx.chain_store().heaviest_tipset().key().clone())
.into();
let client = get_rpc_http_client()?;
let mut params = ArrayParams::new();
params.insert(tsk.into_lotus_json())?;
let response: LotusJson<Self::Ok> = client.request(Self::NAME, params).await?;
Ok(response.into_inner())
}
}
pub enum F3GetF3PowerTableByInstance {}
impl RpcMethod<1> for F3GetF3PowerTableByInstance {
const NAME: &'static str = "Filecoin.F3GetF3PowerTableByInstance";
const PARAM_NAMES: [&'static str; 1] = ["instance"];
const API_PATHS: BitFlags<ApiPaths> = ApiPaths::all();
const PERMISSION: Permission = Permission::Read;
const DESCRIPTION: Option<&'static str> =
Some("Gets the power table (committee) used to validate the specified instance");
type Params = (u64,);
type Ok = Vec<F3PowerEntry>;
async fn handle(
_ctx: Ctx<impl Blockstore>,
(instance,): Self::Params,
_: &http::Extensions,
) -> Result<Self::Ok, ServerError> {
let client = get_rpc_http_client()?;
let mut params = ArrayParams::new();
params.insert(instance)?;
let response: LotusJson<Self::Ok> = client.request(Self::NAME, params).await?;
Ok(response.into_inner())
}
}
pub enum F3IsRunning {}
impl F3IsRunning {
pub async fn is_f3_running() -> anyhow::Result<bool> {
let client = get_rpc_http_client()?;
let response = client.request(Self::NAME, ArrayParams::new()).await?;
Ok(response)
}
}
impl RpcMethod<0> for F3IsRunning {
const NAME: &'static str = "Filecoin.F3IsRunning";
const PARAM_NAMES: [&'static str; 0] = [];
const API_PATHS: BitFlags<ApiPaths> = ApiPaths::all();
const PERMISSION: Permission = Permission::Read;
type Params = ();
type Ok = bool;
async fn handle(
_: Ctx<impl Blockstore>,
(): Self::Params,
_: &http::Extensions,
) -> Result<Self::Ok, ServerError> {
Ok(Self::is_f3_running().await?)
}
}
/// See <https://github.com/filecoin-project/lotus/blob/master/documentation/en/api-methods-v1-stable.md#F3GetProgress>
pub enum F3GetProgress {}
impl F3GetProgress {
async fn run() -> anyhow::Result<F3InstanceProgress> {
let client = get_rpc_http_client()?;
let response: LotusJson<F3InstanceProgress> =
client.request(Self::NAME, ArrayParams::new()).await?;
Ok(response.into_inner())
}
}
impl RpcMethod<0> for F3GetProgress {
const NAME: &'static str = "Filecoin.F3GetProgress";
const PARAM_NAMES: [&'static str; 0] = [];
const API_PATHS: BitFlags<ApiPaths> = ApiPaths::all();
const PERMISSION: Permission = Permission::Read;
type Params = ();
type Ok = F3InstanceProgress;
async fn handle(
_: Ctx<impl Blockstore>,
(): Self::Params,
_: &http::Extensions,
) -> Result<Self::Ok, ServerError> {
Ok(Self::run().await?)
}
}
/// See <https://github.com/filecoin-project/lotus/blob/master/documentation/en/api-methods-v1-stable.md#F3GetManifest>
pub enum F3GetManifest {}
impl F3GetManifest {
async fn run() -> anyhow::Result<F3Manifest> {
let client = get_rpc_http_client()?;
let response: LotusJson<F3Manifest> =
client.request(Self::NAME, ArrayParams::new()).await?;
Ok(response.into_inner())
}
}
impl RpcMethod<0> for F3GetManifest {
const NAME: &'static str = "Filecoin.F3GetManifest";
const PARAM_NAMES: [&'static str; 0] = [];
const API_PATHS: BitFlags<ApiPaths> = ApiPaths::all();
const PERMISSION: Permission = Permission::Read;
type Params = ();
type Ok = F3Manifest;
async fn handle(
_: Ctx<impl Blockstore>,
(): Self::Params,
_: &http::Extensions,
) -> Result<Self::Ok, ServerError> {
Ok(Self::run().await?)
}
}
/// returns the list of miner addresses that are currently participating in F3 via this node.
pub enum F3ListParticipants {}
impl RpcMethod<0> for F3ListParticipants {
const NAME: &'static str = "Filecoin.F3ListParticipants";
const PARAM_NAMES: [&'static str; 0] = [];
const API_PATHS: BitFlags<ApiPaths> = ApiPaths::all();
const PERMISSION: Permission = Permission::Read;
type Params = ();
type Ok = Vec<F3Participant>;
async fn handle(
_: Ctx<impl Blockstore>,
_: Self::Params,
_: &http::Extensions,
) -> Result<Self::Ok, ServerError> {
Ok(Self::run().await?)
}
}
impl F3ListParticipants {
async fn run() -> anyhow::Result<Vec<F3Participant>> {
let current_instance = F3GetProgress::run().await?.id;
Ok(F3_LEASE_MANAGER
.get()
.context("F3 lease manager is not initialized")?
.get_active_participants(current_instance)
.values()
.map(F3Participant::from)
.collect())
}
}
/// retrieves or renews a participation ticket necessary for a miner to engage in
/// the F3 consensus process for the given number of instances.
pub enum F3GetOrRenewParticipationTicket {}
impl RpcMethod<3> for F3GetOrRenewParticipationTicket {
const NAME: &'static str = "Filecoin.F3GetOrRenewParticipationTicket";
const PARAM_NAMES: [&'static str; 3] = ["miner_address", "previous_lease_ticket", "instances"];
const API_PATHS: BitFlags<ApiPaths> = ApiPaths::all();
const PERMISSION: Permission = Permission::Sign;
type Params = (Address, Vec<u8>, u64);
type Ok = Vec<u8>;
async fn handle(
_: Ctx<impl Blockstore>,
(miner, previous_lease_ticket, instances): Self::Params,
_: &http::Extensions,
) -> Result<Self::Ok, ServerError> {
let id = miner.id()?;
let previous_lease = if previous_lease_ticket.is_empty() {
None
} else {
Some(
fvm_ipld_encoding::from_slice::<F3ParticipationLease>(&previous_lease_ticket)
.context("the previous lease ticket is invalid")?,
)
};
let lease = F3_LEASE_MANAGER
.get()
.context("F3 lease manager is not initialized")?
.get_or_renew_participation_lease(id, previous_lease, instances)
.await?;
Ok(fvm_ipld_encoding::to_vec(&lease)?)
}
}
/// enrolls a storage provider in the F3 consensus process using a
/// provided participation ticket. This ticket grants a temporary lease that enables
/// the provider to sign transactions as part of the F3 consensus.
pub enum F3Participate {}
impl RpcMethod<1> for F3Participate {
const NAME: &'static str = "Filecoin.F3Participate";
const PARAM_NAMES: [&'static str; 1] = ["lease_ticket"];
const API_PATHS: BitFlags<ApiPaths> = ApiPaths::all();
const PERMISSION: Permission = Permission::Sign;
type Params = (Vec<u8>,);
type Ok = F3ParticipationLease;
async fn handle(
_: Ctx<impl Blockstore>,
(lease_ticket,): Self::Params,
_: &http::Extensions,
) -> Result<Self::Ok, ServerError> {
let lease: F3ParticipationLease =
fvm_ipld_encoding::from_slice(&lease_ticket).context("invalid lease ticket")?;
let current_instance = F3GetProgress::run().await?.id;
F3_LEASE_MANAGER
.get()
.context("F3 lease manager is not initialized")?
.participate(&lease, current_instance)?;
Ok(lease)
}
}
pub fn get_f3_rpc_endpoint() -> Cow<'static, str> {
if let Ok(host) = std::env::var("FOREST_F3_SIDECAR_RPC_ENDPOINT") {
Cow::Owned(host)
} else {
Cow::Borrowed("127.0.0.1:23456")
}
}
pub fn get_rpc_http_client() -> anyhow::Result<jsonrpsee::http_client::HttpClient> {
let client = jsonrpsee::http_client::HttpClientBuilder::new()
.build(format!("http://{}", get_f3_rpc_endpoint()))?;
Ok(client)
}