Skip to content

Commit 7422233

Browse files
author
Mikhail Tagirov
authored
Market actor v3 (#487)
Signed-off-by: Mikhail Tagirov <[email protected]>
1 parent 6d230bb commit 7422233

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+1412
-157
lines changed

core/adt/set.hpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,13 @@ namespace fc::adt {
1818
}
1919

2020
template <typename Keyer, size_t bit_width = storage::hamt::kDefaultBitWidth>
21-
struct Set : Map<SetValue, Keyer, bit_width> {};
21+
struct Set : Map<SetValue, Keyer, bit_width> {
22+
using Key = typename Keyer::Key;
23+
24+
outcome::result<void> set(const Key &key) {
25+
return Map<SetValue, Keyer, bit_width>::set(key, {});
26+
}
27+
};
2228
} // namespace fc::adt
2329

2430
namespace fc::cbor_blake {

core/miner/storage_fsm/impl/checks.cpp

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,12 @@ namespace fc::mining::checks {
2828
using sector_storage::zerocomm::getZeroPieceCommitment;
2929
using storage::ipfs::ApiIpfsDatastore;
3030
using vm::VMExitCode;
31+
using vm::actor::ActorVersion;
3132
using vm::actor::kStorageMarketAddress;
3233
using vm::actor::MethodParams;
3334
using vm::actor::builtin::states::MinerActorStatePtr;
3435
using vm::actor::builtin::types::miner::kChainFinality;
36+
using vm::actor::builtin::types::miner::kMaxProveCommitDuration;
3537
using vm::actor::builtin::types::miner::kPreCommitChallengeDelay;
3638
using vm::actor::builtin::types::miner::maxSealDuration;
3739
using vm::actor::builtin::types::miner::SectorPreCommitOnChainInfo;
@@ -43,23 +45,19 @@ namespace fc::mining::checks {
4345

4446
outcome::result<EpochDuration> getMaxProveCommitDuration(
4547
NetworkVersion network, const std::shared_ptr<SectorInfo> &sector_info) {
46-
auto version{Toolchain::getActorVersionForNetwork(network)};
48+
const auto version{actorVersion(network)};
4749
switch (version) {
48-
case vm::actor::ActorVersion::kVersion0:
50+
case ActorVersion::kVersion0:
4951
return maxSealDuration(sector_info->sector_type);
50-
case vm::actor::ActorVersion::kVersion2:
51-
return vm::actor::builtin::types::miner::kMaxProveCommitDuration;
52-
case vm::actor::ActorVersion::kVersion3:
53-
return vm::actor::builtin::types::miner::kMaxProveCommitDuration;
54-
case vm::actor::ActorVersion::kVersion4:
55-
return vm::actor::builtin::types::miner::kMaxProveCommitDuration;
56-
case vm::actor::ActorVersion::kVersion5:
57-
if (sector_info->sector_type
58-
>= api::RegisteredSealProof::kStackedDrg2KiBV1_1) {
59-
return 30 * kEpochsInDay + kPreCommitChallengeDelay;
60-
}
61-
62-
return vm::actor::builtin::types::miner::kMaxProveCommitDuration;
52+
case ActorVersion::kVersion2:
53+
case ActorVersion::kVersion3:
54+
case ActorVersion::kVersion4:
55+
return kMaxProveCommitDuration;
56+
case ActorVersion::kVersion5:
57+
return sector_info->sector_type
58+
>= api::RegisteredSealProof::kStackedDrg2KiBV1_1
59+
? 30 * kEpochsInDay + kPreCommitChallengeDelay
60+
: kMaxProveCommitDuration;
6361
}
6462
}
6563

@@ -160,7 +158,7 @@ namespace fc::mining::checks {
160158
OUTCOME_TRY(actor, api->StateGetActor(miner_address, tipset_key));
161159
auto ipfs = std::make_shared<ApiIpfsDatastore>(api);
162160
OUTCOME_TRY(network, api->StateNetworkVersion(tipset_key));
163-
ipfs->actor_version = Toolchain::getActorVersionForNetwork(network);
161+
ipfs->actor_version = actorVersion(network);
164162

165163
OUTCOME_TRY(state, getCbor<MinerActorStatePtr>(ipfs, actor.head));
166164

core/vm/actor/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ target_link_libraries(actor
2323
init_actor_v3
2424
market_actor_v0
2525
market_actor_v2
26+
market_actor_v3
2627
miner_actor_v0
2728
miner_actor_v2
2829
miner_actor_v3

core/vm/actor/builtin/states/market/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55

66
add_library(market_actor_state
77
market_actor_state.cpp
8+
v3/market_actor_state.cpp
89
bind_states.cpp
910
)
1011
target_link_libraries(market_actor_state
1112
cbor
1213
toolchain
14+
market_types
1315
)

core/vm/actor/builtin/states/market/bind_states.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
#include "vm/actor/builtin/states/market/market_actor_state.hpp"
99
#include "vm/actor/builtin/states/market/v0/market_actor_state.hpp"
1010
#include "vm/actor/builtin/states/market/v2/market_actor_state.hpp"
11-
// TODDO market actor v3
11+
#include "vm/actor/builtin/states/market/v3/market_actor_state.hpp"
1212

1313
UNIVERSAL_IMPL(states::MarketActorState,
1414
v0::market::MarketActorState,
1515
v2::market::MarketActorState,
16-
v2::market::MarketActorState,
17-
v2::market::MarketActorState,
18-
v2::market::MarketActorState)
16+
v3::market::MarketActorState,
17+
v3::market::MarketActorState,
18+
v3::market::MarketActorState)

core/vm/actor/builtin/states/market/market_actor_state.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ namespace fc::vm::actor::builtin::states {
1313
using primitives::kChainEpochUndefined;
1414
using runtime::Runtime;
1515
using toolchain::Toolchain;
16-
using types::market::BalanceLockingReason;
17-
using types::market::collateralPenaltyForDealActivationMissed;
18-
using types::market::kDealUpdatesInterval;
16+
using namespace types::market;
1917

2018
outcome::result<void> MarketActorState::unlockBalance(
2119
const Runtime &runtime,
@@ -124,7 +122,7 @@ namespace fc::vm::actor::builtin::states {
124122
const DealProposal &deal,
125123
const DealState &deal_state,
126124
ChainEpoch epoch) {
127-
TokenAmount slashed_sum;
125+
TokenAmount slashed_sum{0};
128126

129127
const auto updated{deal_state.last_updated_epoch != kChainEpochUndefined};
130128
const auto slashed{deal_state.slash_epoch != kChainEpochUndefined};

core/vm/actor/builtin/states/market/market_actor_state.hpp

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77

88
#include "adt/array.hpp"
99
#include "adt/balance_table.hpp"
10-
#include "adt/cid_key.hpp"
1110
#include "adt/set.hpp"
1211
#include "adt/uvarint_key.hpp"
1312
#include "primitives/address/address.hpp"
1413
#include "primitives/types.hpp"
1514
#include "vm/actor/builtin/types/market/deal.hpp"
15+
#include "vm/actor/builtin/types/market/pending_proposals.hpp"
1616
#include "vm/actor/builtin/types/universal/universal.hpp"
1717

1818
// Forward declaration
@@ -22,7 +22,6 @@ namespace fc::vm::runtime {
2222

2323
namespace fc::vm::actor::builtin::states {
2424
using adt::BalanceTable;
25-
using adt::CidKeyer;
2625
using adt::UvarintKeyer;
2726
using primitives::ChainEpoch;
2827
using primitives::DealId;
@@ -31,16 +30,21 @@ namespace fc::vm::actor::builtin::states {
3130
using types::market::BalanceLockingReason;
3231
using types::market::DealProposal;
3332
using types::market::DealState;
33+
using types::market::PendingProposals;
34+
using types::Universal;
35+
36+
constexpr size_t kProposalsAmtBitwidth = 5;
37+
constexpr size_t kStatesAmtBitwidth = 6;
38+
39+
using DealArray = adt::Array<DealProposal, kProposalsAmtBitwidth>;
40+
using DealSet = adt::Set<UvarintKeyer>;
3441

3542
struct MarketActorState {
3643
virtual ~MarketActorState() = default;
3744

38-
using DealSet = adt::Set<UvarintKeyer>;
39-
40-
adt::Array<DealProposal, 5> proposals;
41-
adt::Array<DealState, 6> states;
42-
adt::Map<DealProposal, CidKeyer> pending_proposals_0;
43-
adt::Set<CidKeyer> pending_proposals_3;
45+
DealArray proposals;
46+
adt::Array<DealState, kStatesAmtBitwidth> states;
47+
Universal<PendingProposals> pending_proposals;
4448
BalanceTable escrow_table;
4549
BalanceTable locked_table;
4650
DealId next_deal{0};
@@ -51,46 +55,43 @@ namespace fc::vm::actor::builtin::states {
5155
TokenAmount total_client_storage_fee{};
5256

5357
// Methods
54-
outcome::result<void> unlockBalance(const fc::vm::runtime::Runtime &runtime,
58+
outcome::result<void> unlockBalance(const runtime::Runtime &runtime,
5559
const Address &address,
5660
const TokenAmount &amount,
5761
BalanceLockingReason lock_reason);
5862

59-
outcome::result<void> slashBalance(const fc::vm::runtime::Runtime &runtime,
63+
outcome::result<void> slashBalance(const runtime::Runtime &runtime,
6064
const Address &address,
6165
const TokenAmount &amount,
6266
BalanceLockingReason reason);
6367

64-
outcome::result<void> transferBalance(
65-
const fc::vm::runtime::Runtime &runtime,
66-
const Address &from,
67-
const Address &to,
68-
const TokenAmount &amount);
68+
outcome::result<void> transferBalance(const runtime::Runtime &runtime,
69+
const Address &from,
70+
const Address &to,
71+
const TokenAmount &amount);
6972

7073
outcome::result<TokenAmount> processDealInitTimedOut(
71-
const fc::vm::runtime::Runtime &runtime, const DealProposal &deal);
74+
const runtime::Runtime &runtime, const DealProposal &deal);
7275

73-
outcome::result<void> processDealExpired(
74-
const fc::vm::runtime::Runtime &runtime,
75-
const DealProposal &deal,
76-
const DealState &deal_state);
76+
virtual outcome::result<void> processDealExpired(const runtime::Runtime &runtime,
77+
const DealProposal &deal,
78+
const DealState &deal_state);
7779

78-
outcome::result<std::tuple<TokenAmount, ChainEpoch, bool>>
79-
updatePendingDealState(fc::vm::runtime::Runtime &runtime,
80+
virtual outcome::result<std::tuple<TokenAmount, ChainEpoch, bool>>
81+
updatePendingDealState(runtime::Runtime &runtime,
8082
DealId deal_id,
8183
const DealProposal &deal,
8284
const DealState &deal_state,
8385
ChainEpoch epoch);
8486

85-
outcome::result<void> maybeLockBalance(
86-
const fc::vm::runtime::Runtime &runtime,
87-
const Address &address,
88-
const TokenAmount &amount);
87+
outcome::result<void> maybeLockBalance(const runtime::Runtime &runtime,
88+
const Address &address,
89+
const TokenAmount &amount);
8990

9091
outcome::result<void> lockClientAndProviderBalances(
91-
const fc::vm::runtime::Runtime &runtime, const DealProposal &deal);
92+
const runtime::Runtime &runtime, const DealProposal &deal);
9293
};
9394

94-
using MarketActorStatePtr = types::Universal<MarketActorState>;
95+
using MarketActorStatePtr = Universal<MarketActorState>;
9596

9697
} // namespace fc::vm::actor::builtin::states

core/vm/actor/builtin/states/market/v0/market_actor_state.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace fc::vm::actor::builtin::v0::market {
1515
CBOR_TUPLE(MarketActorState,
1616
proposals,
1717
states,
18-
pending_proposals_0,
18+
pending_proposals,
1919
escrow_table,
2020
locked_table,
2121
next_deal,
@@ -35,7 +35,7 @@ namespace fc::cbor_blake {
3535
const Visitor &visit) {
3636
visit(state.proposals);
3737
visit(state.states);
38-
visit(state.pending_proposals_0);
38+
visit(state.pending_proposals);
3939
visit(state.escrow_table);
4040
visit(state.locked_table);
4141
visit(state.deals_by_epoch);
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/**
2+
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#include "vm/actor/builtin/states/market/v3/market_actor_state.hpp"
7+
8+
#include "vm/actor/builtin/types/market/policy.hpp"
9+
#include "vm/runtime/runtime.hpp"
10+
#include "vm/toolchain/toolchain.hpp"
11+
12+
namespace fc::vm::actor::builtin::v3::market {
13+
using primitives::kChainEpochUndefined;
14+
using runtime::Runtime;
15+
using toolchain::Toolchain;
16+
using namespace types::market;
17+
18+
outcome::result<void> MarketActorState::processDealExpired(
19+
const Runtime &runtime,
20+
const DealProposal &deal,
21+
const DealState &deal_state) {
22+
OUTCOME_TRY(runtime.requireState(deal_state.sector_start_epoch
23+
!= kChainEpochUndefined));
24+
25+
REQUIRE_NO_ERROR(unlockBalance(runtime,
26+
deal.provider,
27+
deal.provider_collateral,
28+
BalanceLockingReason::kProviderCollateral),
29+
VMExitCode::kErrIllegalState);
30+
REQUIRE_NO_ERROR(unlockBalance(runtime,
31+
deal.client,
32+
deal.client_collateral,
33+
BalanceLockingReason::kClientCollateral),
34+
VMExitCode::kErrIllegalState);
35+
36+
return outcome::success();
37+
}
38+
39+
outcome::result<std::tuple<TokenAmount, ChainEpoch, bool>>
40+
MarketActorState::updatePendingDealState(Runtime &runtime,
41+
DealId deal_id,
42+
const DealProposal &deal,
43+
const DealState &deal_state,
44+
ChainEpoch epoch) {
45+
TokenAmount slashed_sum{0};
46+
47+
const auto updated{deal_state.last_updated_epoch != kChainEpochUndefined};
48+
const auto slashed{deal_state.slash_epoch != kChainEpochUndefined};
49+
50+
OUTCOME_TRY(runtime.requireState(
51+
!updated || (deal_state.last_updated_epoch <= epoch)));
52+
53+
if (deal.start_epoch > epoch) {
54+
return std::make_tuple(slashed_sum, kChainEpochUndefined, false);
55+
}
56+
57+
auto payment_end_epoch = deal.end_epoch;
58+
if (slashed) {
59+
OUTCOME_TRY(runtime.requireState(epoch >= deal_state.slash_epoch));
60+
OUTCOME_TRY(
61+
runtime.requireState(deal_state.slash_epoch <= deal.end_epoch));
62+
payment_end_epoch = deal_state.slash_epoch;
63+
} else if (epoch < payment_end_epoch) {
64+
payment_end_epoch = epoch;
65+
}
66+
67+
auto payment_start_epoch = deal.start_epoch;
68+
if (updated && (deal_state.last_updated_epoch > payment_start_epoch)) {
69+
payment_start_epoch = deal_state.last_updated_epoch;
70+
}
71+
72+
const auto epochs_elapsed = payment_end_epoch - payment_start_epoch;
73+
const TokenAmount total_payment =
74+
epochs_elapsed * deal.storage_price_per_epoch;
75+
76+
if (total_payment > 0) {
77+
REQUIRE_NO_ERROR(
78+
transferBalance(runtime, deal.client, deal.provider, total_payment),
79+
VMExitCode::kErrIllegalState);
80+
}
81+
82+
const auto utils = Toolchain::createMarketUtils(runtime);
83+
84+
if (slashed) {
85+
REQUIRE_NO_ERROR_A(
86+
remaining,
87+
utils->dealGetPaymentRemaining(deal, deal_state.slash_epoch),
88+
VMExitCode::kErrIllegalState);
89+
90+
REQUIRE_NO_ERROR(unlockBalance(runtime,
91+
deal.client,
92+
remaining,
93+
BalanceLockingReason::kClientStorageFee),
94+
VMExitCode::kErrIllegalState);
95+
96+
REQUIRE_NO_ERROR(unlockBalance(runtime,
97+
deal.client,
98+
deal.client_collateral,
99+
BalanceLockingReason::kClientCollateral),
100+
VMExitCode::kErrIllegalState);
101+
102+
slashed_sum = deal.provider_collateral;
103+
104+
REQUIRE_NO_ERROR(slashBalance(runtime,
105+
deal.provider,
106+
slashed_sum,
107+
BalanceLockingReason::kProviderCollateral),
108+
VMExitCode::kErrIllegalState);
109+
110+
return std::make_tuple(slashed_sum, kChainEpochUndefined, true);
111+
}
112+
113+
if (epoch >= deal.end_epoch) {
114+
OUTCOME_TRY(processDealExpired(runtime, deal, deal_state));
115+
return std::make_tuple(slashed_sum, kChainEpochUndefined, true);
116+
}
117+
118+
const auto next_epoch = epoch + kDealUpdatesInterval;
119+
120+
return std::make_tuple(slashed_sum, next_epoch, false);
121+
}
122+
123+
} // namespace fc::vm::actor::builtin::v3::market

0 commit comments

Comments
 (0)