-
Notifications
You must be signed in to change notification settings - Fork 200
Expand file tree
/
Copy pathstate.rs
More file actions
3512 lines (3148 loc) · 135 KB
/
Copy pathstate.rs
File metadata and controls
3512 lines (3148 loc) · 135 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
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2019-2026 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0, MIT
mod types;
use futures::stream::FuturesOrdered;
pub use types::*;
use super::chain::ChainGetTipSetV2;
use crate::beacon::Beacon as _;
use crate::blocks::{Tipset, TipsetKey};
use crate::chain::index::ResolveNullTipset;
use crate::cid_collections::CidHashSet;
use crate::eth::EthChainId;
use crate::interpreter::{MessageCallbackCtx, VMTrace};
use crate::libp2p::NetworkMessage;
use crate::lotus_json::{LotusJson, lotus_json_with_self};
use crate::networks::{ChainConfig, NetworkChain};
use crate::prelude::*;
use crate::rpc::registry::actors_reg::load_and_serialize_actor_state;
use crate::shim::actors::market::DealState;
use crate::shim::actors::market::ext::MarketStateExt as _;
use crate::shim::actors::miner::ext::DeadlineExt;
use crate::shim::actors::state_load::*;
use crate::shim::actors::verifreg::ext::VerifiedRegistryStateExt as _;
use crate::shim::actors::verifreg::{Allocation, AllocationID, Claim};
use crate::shim::actors::{init, system};
use crate::shim::actors::{
market, miner,
miner::{MinerInfo, MinerPower},
power, reward, verifreg,
};
use crate::shim::actors::{
market::ext::BalanceTableExt as _, miner::ext::MinerStateExt as _,
power::ext::PowerStateExt as _,
};
use crate::shim::address::Payload;
use crate::shim::machine::BuiltinActorManifest;
use crate::shim::message::{Message, MethodNum};
use crate::shim::sector::{SectorNumber, SectorSize};
use crate::shim::state_tree::{ActorID, StateTree};
use crate::shim::{
address::Address, clock::ChainEpoch, deal::DealID, econ::TokenAmount, executor::Receipt,
state_tree::ActorState, version::NetworkVersion,
};
use crate::state_manager::{ExecutedTipset, NO_CALLBACK};
use crate::state_manager::{
MarketBalance, StateManager, circulating_supply::GenesisInfo, utils::structured,
};
use crate::utils::db::car_stream::{CarBlock, CarWriter};
use crate::{
beacon::BeaconEntry,
rpc::{ApiPaths, Ctx, Permission, RpcMethod, ServerError, types::*},
};
use ahash::{HashMap, HashSet};
use anyhow::Result;
use enumflags2::{BitFlags, make_bitflags};
use fil_actor_miner_state::v10::{qa_power_for_weight, qa_power_max};
use fil_actor_verifreg_state::v13::ClaimID;
use fil_actors_shared::fvm_ipld_amt::Amt;
use fil_actors_shared::fvm_ipld_bitfield::BitField;
use futures::{StreamExt as _, TryStreamExt as _};
use fvm_ipld_encoding::{CborStore, DAG_CBOR};
pub use fvm_shared3::sector::StoragePower;
use ipld_core::ipld::Ipld;
use jsonrpsee::types::error::ErrorObject;
use num_bigint::BigInt;
use num_traits::Euclid;
use nunny::vec as nonempty;
use parking_lot::Mutex;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use std::num::NonZeroUsize;
use std::ops::Mul;
use std::path::PathBuf;
use std::time::Duration;
use tokio::task::JoinSet;
const INITIAL_PLEDGE_NUM: u64 = 110;
const INITIAL_PLEDGE_DEN: u64 = 100;
pub enum StateCall {}
impl StateCall {
pub fn run(
state_manager: &StateManager,
message: &Message,
tsk: Option<TipsetKey>,
) -> anyhow::Result<ApiInvocResult> {
let mut tipset = state_manager
.chain_store()
.load_required_tipset_or_heaviest(&tsk)?;
// Match Lotus' `StateCall` behavior: if the call refuses due to an expensive
// state fork between the parent and the target tipset, walk back to the parent
// tipset and retry. This loop terminates when the call returns a non-`ExpensiveFork`
// result (success or different error), or when we fail to load the parent tipset
// (e.g. we walked back past genesis).
//
// See: <https://github.com/filecoin-project/lotus/blob/797feebc63bfbd4fdfb742b674c97bfb7846cccb/node/impl/full/state.go#L147>
loop {
match state_manager.call(message, Some(tipset.shallow_clone())) {
Err(crate::state_manager::Error::ExpensiveFork) => {
tipset = state_manager
.chain_index()
.load_required_tipset(tipset.parents())
.map_err(|e| anyhow::anyhow!("getting parent tipset: {e}"))?;
}
result => return Ok(result?),
}
}
}
}
impl RpcMethod<2> for StateCall {
const NAME: &'static str = "Filecoin.StateCall";
const PARAM_NAMES: [&'static str; 2] = ["message", "tipsetKey"];
const API_PATHS: BitFlags<ApiPaths> = ApiPaths::all();
const PERMISSION: Permission = Permission::Read;
const DESCRIPTION: Option<&'static str> = Some(
"Runs the given message and returns its result without persisting changes. The message is applied to the tipset's parent state.",
);
type Params = (Message, ApiTipsetKey);
type Ok = ApiInvocResult;
async fn handle(
ctx: Ctx,
(message, ApiTipsetKey(tsk)): Self::Params,
_: &http::Extensions,
) -> Result<Self::Ok, ServerError> {
Ok(Self::run(&ctx.state_manager, &message, tsk)?)
}
}
pub enum StateReplay {}
impl RpcMethod<2> for StateReplay {
const NAME: &'static str = "Filecoin.StateReplay";
const PARAM_NAMES: [&'static str; 2] = ["tipsetKey", "messageCid"];
const API_PATHS: BitFlags<ApiPaths> = ApiPaths::all();
const PERMISSION: Permission = Permission::Read;
const DESCRIPTION: Option<&'static str> = Some(
"Replays a given message, assuming it was included in a block in the specified tipset.",
);
type Params = (ApiTipsetKey, Cid);
type Ok = ApiInvocResult;
/// returns the result of executing the indicated message, assuming it was
/// executed in the indicated tipset.
async fn handle(
ctx: Ctx,
(ApiTipsetKey(tsk), message_cid): Self::Params,
_: &http::Extensions,
) -> Result<Self::Ok, ServerError> {
let tipset = ctx.chain_store().load_required_tipset_or_heaviest(&tsk)?;
Ok(ctx.state_manager.replay(tipset, message_cid).await?)
}
}
pub enum StateNetworkName {}
impl RpcMethod<0> for StateNetworkName {
const NAME: &'static str = "Filecoin.StateNetworkName";
const PARAM_NAMES: [&'static str; 0] = [];
const API_PATHS: BitFlags<ApiPaths> = ApiPaths::all();
const PERMISSION: Permission = Permission::Read;
const DESCRIPTION: Option<&'static str> =
Some("Returns the name of the network the node is synced to.");
type Params = ();
type Ok = String;
async fn handle(
ctx: Ctx,
(): Self::Params,
_: &http::Extensions,
) -> Result<Self::Ok, ServerError> {
let heaviest_tipset = ctx.chain_store().heaviest_tipset();
Ok(ctx
.state_manager
.get_network_state_name(*heaviest_tipset.parent_state())?
.into())
}
}
pub enum StateNetworkVersion {}
impl RpcMethod<1> for StateNetworkVersion {
const NAME: &'static str = "Filecoin.StateNetworkVersion";
const PARAM_NAMES: [&'static str; 1] = ["tipsetKey"];
const API_PATHS: BitFlags<ApiPaths> = ApiPaths::all();
const PERMISSION: Permission = Permission::Read;
const DESCRIPTION: Option<&'static str> =
Some("Returns the network version at the given tipset.");
type Params = (ApiTipsetKey,);
type Ok = NetworkVersion;
async fn handle(
ctx: Ctx,
(ApiTipsetKey(tsk),): Self::Params,
_: &http::Extensions,
) -> Result<Self::Ok, ServerError> {
let ts = ctx.chain_store().load_required_tipset_or_heaviest(&tsk)?;
Ok(ctx.state_manager.get_network_version(ts.epoch()))
}
}
/// gets the public key address of the given ID address
/// See <https://github.com/filecoin-project/lotus/blob/master/documentation/en/api-methods-v0-deprecated.md#StateAccountKey>
pub enum StateAccountKey {}
impl RpcMethod<2> for StateAccountKey {
const NAME: &'static str = "Filecoin.StateAccountKey";
const PARAM_NAMES: [&'static str; 2] = ["address", "tipsetKey"];
const API_PATHS: BitFlags<ApiPaths> = ApiPaths::all();
const PERMISSION: Permission = Permission::Read;
const DESCRIPTION: Option<&'static str> =
Some("Returns the public key address for the given ID address (secp and bls accounts).");
type Params = (Address, ApiTipsetKey);
type Ok = Address;
async fn handle(
ctx: Ctx,
(address, ApiTipsetKey(tsk)): Self::Params,
_: &http::Extensions,
) -> Result<Self::Ok, ServerError> {
let ts = ctx.chain_store().load_required_tipset_or_heaviest(&tsk)?;
Ok(ctx
.state_manager
.resolve_to_deterministic_address(address, &ts)
.await?)
}
}
/// retrieves the ID address of the given address
/// See <https://github.com/filecoin-project/lotus/blob/master/documentation/en/api-methods-v0-deprecated.md#StateLookupID>
pub enum StateLookupID {}
impl RpcMethod<2> for StateLookupID {
const NAME: &'static str = "Filecoin.StateLookupID";
const PARAM_NAMES: [&'static str; 2] = ["address", "tipsetKey"];
const API_PATHS: BitFlags<ApiPaths> = ApiPaths::all();
const PERMISSION: Permission = Permission::Read;
const DESCRIPTION: Option<&'static str> =
Some("Retrieves the ID address of the given address.");
type Params = (Address, ApiTipsetKey);
type Ok = Address;
async fn handle(
ctx: Ctx,
(address, ApiTipsetKey(tsk)): Self::Params,
_: &http::Extensions,
) -> Result<Self::Ok, ServerError> {
let ts = ctx.chain_store().load_required_tipset_or_heaviest(&tsk)?;
Ok(ctx.state_manager.lookup_required_id(&address, &ts)?)
}
}
/// `StateVerifiedRegistryRootKey` returns the address of the Verified Registry's root key
pub enum StateVerifiedRegistryRootKey {}
impl RpcMethod<1> for StateVerifiedRegistryRootKey {
const NAME: &'static str = "Filecoin.StateVerifiedRegistryRootKey";
const PARAM_NAMES: [&'static str; 1] = ["tipsetKey"];
const API_PATHS: BitFlags<ApiPaths> = ApiPaths::all();
const PERMISSION: Permission = Permission::Read;
const DESCRIPTION: Option<&'static str> =
Some("Returns the address of the Verified Registry's root key.");
type Params = (ApiTipsetKey,);
type Ok = Address;
async fn handle(
ctx: Ctx,
(ApiTipsetKey(tsk),): Self::Params,
_: &http::Extensions,
) -> Result<Self::Ok, ServerError> {
let ts = ctx.chain_store().load_required_tipset_or_heaviest(&tsk)?;
let state: verifreg::State = ctx.state_manager.get_actor_state(&ts)?;
Ok(state.root_key())
}
}
// StateVerifiedClientStatus returns the data cap for the given address.
// Returns zero if there is no entry in the data cap table for the address.
pub enum StateVerifierStatus {}
impl RpcMethod<2> for StateVerifierStatus {
const NAME: &'static str = "Filecoin.StateVerifierStatus";
const PARAM_NAMES: [&'static str; 2] = ["address", "tipsetKey"];
const API_PATHS: BitFlags<ApiPaths> = ApiPaths::all();
const PERMISSION: Permission = Permission::Read;
const DESCRIPTION: Option<&'static str> = Some("Returns the data cap for the given address.");
type Params = (Address, ApiTipsetKey);
type Ok = Option<StoragePower>;
async fn handle(
ctx: Ctx,
(address, ApiTipsetKey(tsk)): Self::Params,
_: &http::Extensions,
) -> Result<Self::Ok, ServerError> {
let ts = ctx.chain_store().load_required_tipset_or_heaviest(&tsk)?;
let aid = ctx.state_manager.lookup_required_id(&address, &ts)?;
let verifreg_state: verifreg::State = ctx.state_manager.get_actor_state(&ts)?;
Ok(verifreg_state.verifier_data_cap(ctx.db(), aid)?)
}
}
pub enum StateGetActor {}
impl RpcMethod<2> for StateGetActor {
const NAME: &'static str = "Filecoin.StateGetActor";
const PARAM_NAMES: [&'static str; 2] = ["address", "tipsetKey"];
const API_PATHS: BitFlags<ApiPaths> = ApiPaths::all();
const PERMISSION: Permission = Permission::Read;
const DESCRIPTION: Option<&'static str> =
Some("Returns the nonce and balance for the specified actor.");
type Params = (Address, ApiTipsetKey);
type Ok = Option<ActorState>;
async fn handle(
ctx: Ctx,
(address, ApiTipsetKey(tsk)): Self::Params,
_: &http::Extensions,
) -> Result<Self::Ok, ServerError> {
let ts = ctx.chain_store().load_required_tipset_or_heaviest(&tsk)?;
let state = ctx.state_manager.get_actor(&address, *ts.parent_state())?;
Ok(state)
}
}
pub enum StateGetActorV2 {}
impl RpcMethod<2> for StateGetActorV2 {
const NAME: &'static str = "Filecoin.StateGetActor";
const PARAM_NAMES: [&'static str; 2] = ["address", "tipsetSelector"];
const API_PATHS: BitFlags<ApiPaths> = make_bitflags!(ApiPaths::{ V2 });
const PERMISSION: Permission = Permission::Read;
const DESCRIPTION: Option<&'static str> =
Some("Returns the nonce and balance for the specified actor.");
type Params = (Address, TipsetSelector);
type Ok = Option<ActorState>;
async fn handle(
ctx: Ctx,
(address, selector): Self::Params,
_: &http::Extensions,
) -> Result<Self::Ok, ServerError> {
let ts = ChainGetTipSetV2::get_tipset(&ctx, &selector).await?;
Ok(ctx.state_manager.get_actor(&address, *ts.parent_state())?)
}
}
pub enum StateGetID {}
impl RpcMethod<2> for StateGetID {
const NAME: &'static str = "Filecoin.StateGetID";
const PARAM_NAMES: [&'static str; 2] = ["address", "tipsetSelector"];
const API_PATHS: BitFlags<ApiPaths> = make_bitflags!(ApiPaths::{ V2 });
const PERMISSION: Permission = Permission::Read;
const DESCRIPTION: Option<&'static str> =
Some("Retrieves the ID address for the specified address at the selected tipset.");
type Params = (Address, TipsetSelector);
type Ok = Address;
async fn handle(
ctx: Ctx,
(address, selector): Self::Params,
_: &http::Extensions,
) -> Result<Self::Ok, ServerError> {
let ts = ChainGetTipSetV2::get_tipset(&ctx, &selector).await?;
Ok(ctx.state_manager.lookup_required_id(&address, &ts)?)
}
}
pub enum StateLookupRobustAddress {}
macro_rules! get_robust_address {
($store:expr, $id_addr_decoded:expr, $state:expr, $make_map_with_root:path, $robust_addr:expr) => {{
let map = $make_map_with_root(&$state.address_map, &$store)?;
map.for_each(|addr, v| {
if *v == $id_addr_decoded {
$robust_addr = Address::from_bytes(addr)?;
return Ok(());
}
Ok(())
})?;
Ok($robust_addr)
}};
}
impl RpcMethod<2> for StateLookupRobustAddress {
const NAME: &'static str = "Filecoin.StateLookupRobustAddress";
const PARAM_NAMES: [&'static str; 2] = ["address", "tipsetKey"];
const API_PATHS: BitFlags<ApiPaths> = ApiPaths::all();
const PERMISSION: Permission = Permission::Read;
const DESCRIPTION: Option<&'static str> =
Some("Returns the public key address for non-account addresses (e.g., multisig, miners).");
type Params = (Address, ApiTipsetKey);
type Ok = Address;
async fn handle(
ctx: Ctx,
(addr, ApiTipsetKey(tsk)): Self::Params,
_: &http::Extensions,
) -> Result<Self::Ok, ServerError> {
let ts = ctx.chain_store().load_required_tipset_or_heaviest(&tsk)?;
let store = ctx.db();
let state_tree = StateTree::new_from_root(ctx.db(), ts.parent_state())?;
if let &Payload::ID(id_addr_decoded) = addr.payload() {
let init_state: init::State = state_tree.get_actor_state()?;
let mut robust_addr = Address::default();
match init_state {
init::State::V0(_) => Err(ServerError::internal_error(
"StateLookupRobustAddress is not implemented for init state v0",
None,
)),
init::State::V8(state) => get_robust_address!(
store,
id_addr_decoded,
state,
fil_actors_shared::v8::make_map_with_root::<_, ActorID>,
robust_addr
),
init::State::V9(state) => get_robust_address!(
store,
id_addr_decoded,
state,
fil_actors_shared::v9::make_map_with_root::<_, ActorID>,
robust_addr
),
init::State::V10(state) => get_robust_address!(
store,
id_addr_decoded,
state,
fil_actors_shared::v10::make_map_with_root::<_, ActorID>,
robust_addr
),
init::State::V11(state) => get_robust_address!(
store,
id_addr_decoded,
state,
fil_actors_shared::v11::make_map_with_root::<_, ActorID>,
robust_addr
),
init::State::V12(state) => get_robust_address!(
store,
id_addr_decoded,
state,
fil_actors_shared::v12::make_map_with_root::<_, ActorID>,
robust_addr
),
init::State::V13(state) => get_robust_address!(
store,
id_addr_decoded,
state,
fil_actors_shared::v13::make_map_with_root::<_, ActorID>,
robust_addr
),
init::State::V14(state) => {
let map = fil_actor_init_state::v14::AddressMap::load(
&store,
&state.address_map,
fil_actors_shared::v14::DEFAULT_HAMT_CONFIG,
"address_map",
)
.context("Failed to load address map")?;
map.for_each(|addr, v| {
if *v == id_addr_decoded {
robust_addr = addr.into();
return Ok(());
}
Ok(())
})
.context("Robust address not found")?;
Ok(robust_addr)
}
init::State::V15(state) => {
let map = fil_actor_init_state::v15::AddressMap::load(
&store,
&state.address_map,
fil_actors_shared::v15::DEFAULT_HAMT_CONFIG,
"address_map",
)
.context("Failed to load address map")?;
map.for_each(|addr, v| {
if *v == id_addr_decoded {
robust_addr = addr.into();
return Ok(());
}
Ok(())
})
.context("Robust address not found")?;
Ok(robust_addr)
}
init::State::V16(state) => {
let map = fil_actor_init_state::v16::AddressMap::load(
&store,
&state.address_map,
fil_actors_shared::v16::DEFAULT_HAMT_CONFIG,
"address_map",
)
.context("Failed to load address map")?;
map.for_each(|addr, v| {
if *v == id_addr_decoded {
robust_addr = addr.into();
return Ok(());
}
Ok(())
})
.context("Robust address not found")?;
Ok(robust_addr)
}
init::State::V17(state) => {
let map = fil_actor_init_state::v17::AddressMap::load(
&store,
&state.address_map,
fil_actors_shared::v17::DEFAULT_HAMT_CONFIG,
"address_map",
)
.context("Failed to load address map")?;
map.for_each(|addr, v| {
if *v == id_addr_decoded {
robust_addr = addr.into();
return Ok(());
}
Ok(())
})
.context("Robust address not found")?;
Ok(robust_addr)
}
init::State::V18(state) => {
let map = fil_actor_init_state::v18::AddressMap::load(
&store,
&state.address_map,
fil_actors_shared::v18::DEFAULT_HAMT_CONFIG,
"address_map",
)
.context("Failed to load address map")?;
map.for_each(|addr, v| {
if *v == id_addr_decoded {
robust_addr = addr.into();
return Ok(());
}
Ok(())
})
.context("Robust address not found")?;
Ok(robust_addr)
}
}
} else {
Ok(Address::default())
}
}
}
/// looks up the Escrow and Locked balances of the given address in the Storage
/// Market
pub enum StateMarketBalance {}
impl RpcMethod<2> for StateMarketBalance {
const NAME: &'static str = "Filecoin.StateMarketBalance";
const PARAM_NAMES: [&'static str; 2] = ["address", "tipsetKey"];
const API_PATHS: BitFlags<ApiPaths> = ApiPaths::all();
const PERMISSION: Permission = Permission::Read;
const DESCRIPTION: Option<&'static str> = Some(
"Returns the Escrow and Locked balances of the specified address in the Storage Market.",
);
type Params = (Address, ApiTipsetKey);
type Ok = MarketBalance;
async fn handle(
ctx: Ctx,
(address, ApiTipsetKey(tsk)): Self::Params,
_: &http::Extensions,
) -> Result<Self::Ok, ServerError> {
let ts = ctx.chain_store().load_required_tipset_or_heaviest(&tsk)?;
ctx.state_manager
.market_balance(&address, &ts)
.map_err(From::from)
}
}
pub enum StateMarketDeals {}
impl RpcMethod<1> for StateMarketDeals {
const NAME: &'static str = "Filecoin.StateMarketDeals";
const PARAM_NAMES: [&'static str; 1] = ["tipsetKey"];
const API_PATHS: BitFlags<ApiPaths> = ApiPaths::all();
const PERMISSION: Permission = Permission::Read;
const DESCRIPTION: Option<&'static str> =
Some("Returns information about every deal in the Storage Market.");
type Params = (ApiTipsetKey,);
type Ok = HashMap<String, ApiMarketDeal>;
async fn handle(
ctx: Ctx,
(ApiTipsetKey(tsk),): Self::Params,
_: &http::Extensions,
) -> Result<Self::Ok, ServerError> {
let ts = ctx.chain_store().load_required_tipset_or_heaviest(&tsk)?;
let market_state: market::State = ctx.state_manager.get_actor_state(&ts)?;
let da = market_state.proposals(ctx.db())?;
let sa = market_state.states(ctx.db())?;
let mut out = HashMap::new();
da.for_each(|deal_id, d| {
let s = sa.get(deal_id)?.unwrap_or(market::DealState {
sector_start_epoch: -1,
last_updated_epoch: -1,
slash_epoch: -1,
verified_claim: 0,
sector_number: 0,
});
out.insert(
deal_id.to_string(),
MarketDeal {
proposal: d?,
state: s,
}
.into(),
);
Ok(())
})?;
Ok(out)
}
}
/// looks up the miner info of the given address.
pub enum StateMinerInfo {}
impl RpcMethod<2> for StateMinerInfo {
const NAME: &'static str = "Filecoin.StateMinerInfo";
const PARAM_NAMES: [&'static str; 2] = ["minerAddress", "tipsetKey"];
const API_PATHS: BitFlags<ApiPaths> = ApiPaths::all();
const PERMISSION: Permission = Permission::Read;
const DESCRIPTION: Option<&'static str> =
Some("Returns information about the specified miner.");
type Params = (Address, ApiTipsetKey);
type Ok = MinerInfo;
async fn handle(
ctx: Ctx,
(address, ApiTipsetKey(tsk)): Self::Params,
_: &http::Extensions,
) -> Result<Self::Ok, ServerError> {
let ts = ctx.chain_store().load_required_tipset_or_heaviest(&tsk)?;
Ok(ctx.state_manager.miner_info(&address, &ts)?)
}
}
pub enum StateMinerActiveSectors {}
impl RpcMethod<2> for StateMinerActiveSectors {
const NAME: &'static str = "Filecoin.StateMinerActiveSectors";
const PARAM_NAMES: [&'static str; 2] = ["minerAddress", "tipsetKey"];
const API_PATHS: BitFlags<ApiPaths> = ApiPaths::all();
const PERMISSION: Permission = Permission::Read;
const DESCRIPTION: Option<&'static str> =
Some("Returns information about sectors actively proven by a given miner.");
type Params = (Address, ApiTipsetKey);
type Ok = Vec<SectorOnChainInfo>;
async fn handle(
ctx: Ctx,
(address, ApiTipsetKey(tsk)): Self::Params,
_: &http::Extensions,
) -> Result<Self::Ok, ServerError> {
let ts = ctx.chain_store().load_required_tipset_or_heaviest(&tsk)?;
let policy = &ctx.chain_config().policy;
let miner_state: miner::State = ctx
.state_manager
.get_actor_state_from_address(&ts, &address)?;
// Collect active sectors from each partition in each deadline.
let mut active_sectors = vec![];
miner_state.for_each_deadline(policy, ctx.db(), |_dlidx, deadline| {
deadline.for_each(ctx.db(), |_partidx, partition| {
active_sectors.push(partition.active_sectors());
Ok(())
})
})?;
let sectors =
miner_state.load_sectors_ext(ctx.db(), Some(&BitField::union(&active_sectors)))?;
Ok(sectors)
}
}
/// Returns a bitfield containing all sector numbers marked as allocated in miner state
pub enum StateMinerAllocated {}
impl RpcMethod<2> for StateMinerAllocated {
const NAME: &'static str = "Filecoin.StateMinerAllocated";
const PARAM_NAMES: [&'static str; 2] = ["minerAddress", "tipsetKey"];
const API_PATHS: BitFlags<ApiPaths> = ApiPaths::all();
const PERMISSION: Permission = Permission::Read;
const DESCRIPTION: Option<&'static str> = Some(
"Returns a bitfield containing all sector numbers marked as allocated to the provided miner ID.",
);
type Params = (Address, ApiTipsetKey);
type Ok = BitField;
async fn handle(
ctx: Ctx,
(address, ApiTipsetKey(tsk)): Self::Params,
_: &http::Extensions,
) -> Result<Self::Ok, ServerError> {
let ts = ctx.chain_store().load_required_tipset_or_heaviest(&tsk)?;
let miner_state: miner::State = ctx
.state_manager
.get_actor_state_from_address(&ts, &address)?;
Ok(miner_state.load_allocated_sector_numbers(ctx.db())?)
}
}
/// Return all partitions in the specified deadline
pub enum StateMinerPartitions {}
impl RpcMethod<3> for StateMinerPartitions {
const NAME: &'static str = "Filecoin.StateMinerPartitions";
const PARAM_NAMES: [&'static str; 3] = ["minerAddress", "deadlineIndex", "tipsetKey"];
const API_PATHS: BitFlags<ApiPaths> = ApiPaths::all();
const PERMISSION: Permission = Permission::Read;
const DESCRIPTION: Option<&'static str> =
Some("Returns all partitions in the specified deadline.");
type Params = (Address, u64, ApiTipsetKey);
type Ok = Vec<MinerPartitions>;
async fn handle(
ctx: Ctx,
(address, dl_idx, ApiTipsetKey(tsk)): Self::Params,
_: &http::Extensions,
) -> Result<Self::Ok, ServerError> {
let ts = ctx.chain_store().load_required_tipset_or_heaviest(&tsk)?;
let policy = &ctx.chain_config().policy;
let miner_state: miner::State = ctx
.state_manager
.get_actor_state_from_address(&ts, &address)?;
let deadline = miner_state.load_deadline(policy, ctx.db(), dl_idx)?;
let mut all_partitions = Vec::new();
deadline.for_each(ctx.db(), |_partidx, partition| {
all_partitions.push(MinerPartitions::new(
partition.all_sectors(),
partition.faulty_sectors(),
partition.recovering_sectors(),
partition.live_sectors(),
partition.active_sectors(),
));
Ok(())
})?;
Ok(all_partitions)
}
}
pub enum StateMinerSectors {}
impl RpcMethod<3> for StateMinerSectors {
const NAME: &'static str = "Filecoin.StateMinerSectors";
const PARAM_NAMES: [&'static str; 3] = ["minerAddress", "sectors", "tipsetKey"];
const API_PATHS: BitFlags<ApiPaths> = ApiPaths::all();
const PERMISSION: Permission = Permission::Read;
const DESCRIPTION: Option<&'static str> = Some(
"Returns information about the given miner's sectors. If no filter is provided, all sectors are included.",
);
type Params = (Address, Option<BitField>, ApiTipsetKey);
type Ok = Vec<SectorOnChainInfo>;
async fn handle(
ctx: Ctx,
(address, sectors, ApiTipsetKey(tsk)): Self::Params,
_: &http::Extensions,
) -> Result<Self::Ok, ServerError> {
let ts = ctx.chain_store().load_required_tipset_or_heaviest(&tsk)?;
let miner_state: miner::State = ctx
.state_manager
.get_actor_state_from_address(&ts, &address)?;
Ok(miner_state.load_sectors_ext(ctx.db(), sectors.as_ref())?)
}
}
/// Returns the number of sectors in a miner's sector set and proving set
pub enum StateMinerSectorCount {}
impl RpcMethod<2> for StateMinerSectorCount {
const NAME: &'static str = "Filecoin.StateMinerSectorCount";
const PARAM_NAMES: [&'static str; 2] = ["minerAddress", "tipsetKey"];
const API_PATHS: BitFlags<ApiPaths> = ApiPaths::all();
const PERMISSION: Permission = Permission::Read;
const DESCRIPTION: Option<&'static str> =
Some("Returns the number of sectors in a miner's sector and proving sets.");
type Params = (Address, ApiTipsetKey);
type Ok = MinerSectors;
async fn handle(
ctx: Ctx,
(address, ApiTipsetKey(tsk)): Self::Params,
_: &http::Extensions,
) -> Result<Self::Ok, ServerError> {
let ts = ctx.chain_store().load_required_tipset_or_heaviest(&tsk)?;
let policy = &ctx.chain_config().policy;
let miner_state: miner::State = ctx
.state_manager
.get_actor_state_from_address(&ts, &address)?;
// Collect live, active and faulty sectors count from each partition in each deadline.
let mut live_count = 0;
let mut active_count = 0;
let mut faulty_count = 0;
miner_state.for_each_deadline(policy, ctx.db(), |_dlidx, deadline| {
deadline.for_each(ctx.db(), |_partidx, partition| {
live_count += partition.live_sectors().len();
active_count += partition.active_sectors().len();
faulty_count += partition.faulty_sectors().len();
Ok(())
})
})?;
Ok(MinerSectors::new(live_count, active_count, faulty_count))
}
}
/// Checks if a sector is allocated
pub enum StateMinerSectorAllocated {}
impl RpcMethod<3> for StateMinerSectorAllocated {
const NAME: &'static str = "Filecoin.StateMinerSectorAllocated";
const PARAM_NAMES: [&'static str; 3] = ["minerAddress", "sectorNumber", "tipsetKey"];
const API_PATHS: BitFlags<ApiPaths> = ApiPaths::all();
const PERMISSION: Permission = Permission::Read;
const DESCRIPTION: Option<&'static str> =
Some("Checks if a sector number is marked as allocated.");
type Params = (Address, SectorNumber, ApiTipsetKey);
type Ok = bool;
async fn handle(
ctx: Ctx,
(miner_address, sector_number, ApiTipsetKey(tsk)): Self::Params,
_: &http::Extensions,
) -> Result<Self::Ok, ServerError> {
let ts = ctx.chain_store().load_required_tipset_or_heaviest(&tsk)?;
let miner_state: miner::State = ctx
.state_manager
.get_actor_state_from_address(&ts, &miner_address)?;
let allocated_sector_numbers: BitField =
miner_state.load_allocated_sector_numbers(ctx.db())?;
Ok(allocated_sector_numbers.get(sector_number))
}
}
/// looks up the miner power of the given address.
pub enum StateMinerPower {}
impl RpcMethod<2> for StateMinerPower {
const NAME: &'static str = "Filecoin.StateMinerPower";
const PARAM_NAMES: [&'static str; 2] = ["minerAddress", "tipsetKey"];
const API_PATHS: BitFlags<ApiPaths> = ApiPaths::all();
const PERMISSION: Permission = Permission::Read;
const DESCRIPTION: Option<&'static str> = Some("Returns the power of the specified miner.");
type Params = (Address, ApiTipsetKey);
type Ok = MinerPower;
async fn handle(
ctx: Ctx,
(address, ApiTipsetKey(tsk)): Self::Params,
_: &http::Extensions,
) -> Result<Self::Ok, ServerError> {
let ts = ctx.chain_store().load_required_tipset_or_heaviest(&tsk)?;
ctx.state_manager
.miner_power(&address, &ts)
.map_err(From::from)
}
}
pub enum StateMinerDeadlines {}
impl RpcMethod<2> for StateMinerDeadlines {
const NAME: &'static str = "Filecoin.StateMinerDeadlines";
const PARAM_NAMES: [&'static str; 2] = ["minerAddress", "tipsetKey"];
const API_PATHS: BitFlags<ApiPaths> = ApiPaths::all();
const PERMISSION: Permission = Permission::Read;
const DESCRIPTION: Option<&'static str> =
Some("Returns all proving deadlines for the given miner.");
type Params = (Address, ApiTipsetKey);
type Ok = Vec<ApiDeadline>;
async fn handle(
ctx: Ctx,
(address, ApiTipsetKey(tsk)): Self::Params,
_: &http::Extensions,
) -> Result<Self::Ok, ServerError> {
let ts = ctx.chain_store().load_required_tipset_or_heaviest(&tsk)?;
let policy = &ctx.chain_config().policy;
let state: miner::State = ctx
.state_manager
.get_actor_state_from_address(&ts, &address)?;
let mut res = Vec::new();
state.for_each_deadline(policy, ctx.db(), |_idx, deadline| {
res.push(ApiDeadline {
post_submissions: deadline.partitions_posted(),
disputable_proof_count: deadline.disputable_proof_count(ctx.db())?,
daily_fee: deadline.daily_fee(),
});
Ok(())
})?;
Ok(res)
}
}
pub enum StateMinerProvingDeadline {}
impl RpcMethod<2> for StateMinerProvingDeadline {
const NAME: &'static str = "Filecoin.StateMinerProvingDeadline";
const PARAM_NAMES: [&'static str; 2] = ["minerAddress", "tipsetKey"];
const API_PATHS: BitFlags<ApiPaths> = ApiPaths::all();
const PERMISSION: Permission = Permission::Read;
const DESCRIPTION: Option<&'static str> = Some(
"Calculates the deadline and related details for a given epoch during a proving period.",
);
type Params = (Address, ApiTipsetKey);
type Ok = ApiDeadlineInfo;
async fn handle(
ctx: Ctx,
(address, ApiTipsetKey(tsk)): Self::Params,
_: &http::Extensions,
) -> Result<Self::Ok, ServerError> {
let ts = ctx.chain_store().load_required_tipset_or_heaviest(&tsk)?;
let policy = &ctx.chain_config().policy;
let state: miner::State = ctx
.state_manager
.get_actor_state_from_address(&ts, &address)?;
Ok(ApiDeadlineInfo(
state
.recorded_deadline_info(policy, ts.epoch())
.next_not_elapsed(),
))
}
}
/// looks up the miner power of the given address.
pub enum StateMinerFaults {}
impl RpcMethod<2> for StateMinerFaults {
const NAME: &'static str = "Filecoin.StateMinerFaults";
const PARAM_NAMES: [&'static str; 2] = ["minerAddress", "tipsetKey"];
const API_PATHS: BitFlags<ApiPaths> = ApiPaths::all();
const PERMISSION: Permission = Permission::Read;
const DESCRIPTION: Option<&'static str> =
Some("Returns a bitfield of the faulty sectors for the given miner.");
type Params = (Address, ApiTipsetKey);
type Ok = BitField;
async fn handle(
ctx: Ctx,
(address, ApiTipsetKey(tsk)): Self::Params,
_: &http::Extensions,
) -> Result<Self::Ok, ServerError> {
let ts = ctx.chain_store().load_required_tipset_or_heaviest(&tsk)?;
ctx.state_manager
.miner_faults(&address, &ts)
.map_err(From::from)
}
}
pub enum StateMinerRecoveries {}
impl RpcMethod<2> for StateMinerRecoveries {
const NAME: &'static str = "Filecoin.StateMinerRecoveries";
const PARAM_NAMES: [&'static str; 2] = ["minerAddress", "tipsetKey"];
const API_PATHS: BitFlags<ApiPaths> = ApiPaths::all();
const PERMISSION: Permission = Permission::Read;
const DESCRIPTION: Option<&'static str> =
Some("Returns a bitfield of recovering sectors for the given miner.");
type Params = (Address, ApiTipsetKey);
type Ok = BitField;
async fn handle(
ctx: Ctx,
(address, ApiTipsetKey(tsk)): Self::Params,
_: &http::Extensions,
) -> Result<Self::Ok, ServerError> {
let ts = ctx.chain_store().load_required_tipset_or_heaviest(&tsk)?;