-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathstore_impl.hpp
146 lines (126 loc) · 4.86 KB
/
store_impl.hpp
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
/**
* Copyright Quadrivium LLC
* All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include "parachain/backing/store.hpp"
#include <map>
#include <unordered_map>
#include <unordered_set>
/**
* @file store_impl.hpp
* @brief This file contains the BackingStoreImpl class, which is an
* implementation of the BackingStore interface.
*
* The BackingStoreImpl class is used to manage and store statements and
* backed candidates for active backing tasks. It provides methods to add,
* remove, and retrieve backed candidates and their associated statements.
*/
namespace kagome::parachain {
/**
* @class BackingStoreImpl
* @brief This class is an implementation of the BackingStore interface that
* is used to manage and store statements and backed candidates for active
* backing tasks.
*/
class BackingStoreImpl : public BackingStore {
using ValidatorIndex = network::ValidatorIndex;
public:
enum Error {
UNAUTHORIZED_STATEMENT = 1,
DOUBLE_VOTE,
MULTIPLE_CANDIDATES,
CRITICAL_ERROR,
};
BackingStoreImpl(std::shared_ptr<crypto::Hasher> hasher);
/**
* @brief This method is used to add a statement to the store. It checks if
* the statement is valid and if it is, adds it to the store.
* @param groups A map of parachain IDs to validator indices.
* @param statement The statement to add to the store.
* @return An optional ImportResult object containing the result of the
* import operation
*/
std::optional<ImportResult> put(
const RelayHash &relay_parent,
GroupIndex group_id,
const std::unordered_map<CoreIndex, std::vector<ValidatorIndex>>
&groups,
Statement statement,
bool allow_multiple_seconded) override;
/**
* @method get
* @brief This method is used to retrieve backed candidates associated with
* a given relay parent.
* @param relay_parent The relay parent associated with the backed
* candidates.
* @return A vector of BackedCandidate objects.
*/
std::vector<BackedCandidate> get(
const BlockHash &relay_parent) const override;
/**
* @method add
* @brief This method is used to add a backed candidate to the store.
* @param relay_parent The relay parent associated with the backed
* candidate.
* @param candidate The backed candidate to be added to the store.
*/
void add(const BlockHash &relay_parent,
BackedCandidate &&candidate) override;
std::optional<std::reference_wrapper<const StatementInfo>> getCadidateInfo(
const RelayHash &relay_parent,
const network::CandidateHash &candidate_hash) const override;
void onActivateLeaf(const RelayHash &relay_parent) override;
void onDeactivateLeaf(const RelayHash &relay_parent) override;
void printStoragesLoad() override;
private:
struct AuthorityData {
std::deque<std::pair<CandidateHash, ValidatorSignature>> proposals;
};
struct PerRelayParent {
std::vector<BackedCandidate> backed_candidates_;
std::unordered_map<ValidatorIndex, AuthorityData> authority_data_;
std::unordered_map<CandidateHash, StatementInfo> candidate_votes_;
};
template <typename F>
void forRelayState(const RelayHash &relay_parent, F &&f) {
if (auto it = per_relay_parent_.find(relay_parent);
it != per_relay_parent_.end()) {
std::forward<F>(f)(it->second);
}
}
template <typename F>
void forRelayState(const RelayHash &relay_parent, F &&f) const {
if (auto it = per_relay_parent_.find(relay_parent);
it != per_relay_parent_.end()) {
std::forward<F>(f)(it->second);
}
}
bool is_member_of(
const std::unordered_map<ParachainId, std::vector<ValidatorIndex>>
&groups,
GroupIndex group,
ValidatorIndex authority) const;
outcome::result<std::optional<BackingStore::ImportResult>> validity_vote(
PerRelayParent &state,
const std::unordered_map<ParachainId, std::vector<ValidatorIndex>>
&groups,
ValidatorIndex from,
const CandidateHash &digest,
const ValidityVote &vote);
outcome::result<std::optional<BackingStore::ImportResult>> import_candidate(
PerRelayParent &state,
GroupIndex group_id,
const std::unordered_map<CoreIndex, std::vector<ValidatorIndex>>
&groups,
ValidatorIndex authority,
const network::CommittedCandidateReceipt &candidate,
const ValidatorSignature &signature,
bool allow_multiple_seconded);
std::shared_ptr<crypto::Hasher> hasher_;
std::unordered_map<RelayHash, PerRelayParent> per_relay_parent_;
log::Logger logger = log::createLogger("BackingStore", "parachain");
};
} // namespace kagome::parachain
OUTCOME_HPP_DECLARE_ERROR(kagome::parachain, BackingStoreImpl::Error)