Skip to content

Commit fc8d3d5

Browse files
authoredSep 12, 2022
parachain availability store (#1333)
Signed-off-by: turuslan <[email protected]>
1 parent 0c3398f commit fc8d3d5

File tree

6 files changed

+183
-4
lines changed

6 files changed

+183
-4
lines changed
 

‎core/injector/application_injector.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@
109109
#include "offchain/impl/offchain_worker_impl.hpp"
110110
#include "offchain/impl/offchain_worker_pool_impl.hpp"
111111
#include "outcome/outcome.hpp"
112+
#include "parachain/availability/store/store_impl.hpp"
112113
#include "parachain/validator/parachain_observer.hpp"
113114
#include "parachain/validator/parachain_processor.hpp"
114115
#include "runtime/binaryen/binaryen_memory_provider.hpp"
@@ -1231,6 +1232,7 @@ namespace {
12311232
di::bind<storage::changes_trie::ChangesTracker>.template to<storage::changes_trie::StorageChangesTrackerImpl>(),
12321233
bind_by_lambda<network::StateProtocolObserver>(get_state_observer_impl),
12331234
bind_by_lambda<network::SyncProtocolObserver>(get_sync_observer_impl),
1235+
di::bind<parachain::AvailabilityStore>.template to<parachain::AvailabilityStoreImpl>(),
12341236
di::bind<parachain::ParachainObserverImpl>.to([](auto const &injector) {
12351237
return get_parachain_observer_impl(injector);
12361238
}),

‎core/network/types/collator_messages.hpp

+14-4
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,6 @@
1919
#include "scale/tie.hpp"
2020
#include "storage/trie/types.hpp"
2121

22-
/*
23-
*
24-
25-
* */
2622
namespace kagome::network {
2723
using Signature = crypto::Sr25519Signature;
2824
using ParachainId = uint32_t;
@@ -31,6 +27,8 @@ namespace kagome::network {
3127
using UpwardMessage = common::Buffer;
3228
using ParachainRuntime = common::Buffer;
3329
using HeadData = common::Buffer;
30+
using CandidateHash = primitives::BlockHash;
31+
using ChunkProof = std::vector<common::Buffer>;
3432

3533
/// NU element.
3634
using Dummy = std::tuple<>;
@@ -61,6 +59,18 @@ namespace kagome::network {
6159
/// collators node.
6260
};
6361

62+
/// A chunk of erasure-encoded block data.
63+
struct ErasureChunk {
64+
SCALE_TIE(3);
65+
66+
/// The erasure-encoded chunk of data belonging to the candidate block.
67+
common::Buffer chunk;
68+
/// The index of this erasure-encoded chunk of data.
69+
ValidatorIndex index;
70+
/// Proof for this chunk's branch in the Merkle tree.
71+
ChunkProof proof;
72+
};
73+
6474
/**
6575
* PoV
6676
*/

‎core/parachain/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

22
add_library(validator_parachain
3+
availability/store/store_impl.cpp
34
validator/impl/parachain_observer.cpp
45
validator/impl/parachain_processor.cpp
56
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#ifndef KAGOME_PARACHAIN_AVAILABILITY_STORE_STORE_HPP
7+
#define KAGOME_PARACHAIN_AVAILABILITY_STORE_STORE_HPP
8+
9+
#include <optional>
10+
11+
#include "network/types/collator_messages.hpp"
12+
#include "runtime/runtime_api/parachain_host_types.hpp"
13+
14+
namespace kagome::parachain {
15+
/// Stores ErasureChunk, PoV and PersistedValidationData
16+
class AvailabilityStore {
17+
public:
18+
using CandidateHash = network::CandidateHash;
19+
using ValidatorIndex = network::ValidatorIndex;
20+
using ErasureChunk = network::ErasureChunk;
21+
using ParachainBlock = network::ParachainBlock;
22+
using PersistedValidationData = runtime::PersistedValidationData;
23+
24+
virtual ~AvailabilityStore() = 0;
25+
26+
/// Has ErasureChunk
27+
virtual bool hasChunk(const CandidateHash &candidate_hash,
28+
ValidatorIndex index) = 0;
29+
/// Has PoV
30+
virtual bool hasPov(const CandidateHash &candidate_hash) = 0;
31+
/// Has PersistedValidationData
32+
virtual bool hasData(const CandidateHash &candidate_hash) = 0;
33+
/// Get ErasureChunk
34+
virtual std::optional<ErasureChunk> getChunk(
35+
const CandidateHash &candidate_hash, ValidatorIndex index) = 0;
36+
/// Get PoV
37+
virtual std::optional<ParachainBlock> getPov(
38+
const CandidateHash &candidate_hash) = 0;
39+
/// Store ErasureChunk
40+
virtual void putChunk(const CandidateHash &candidate_hash,
41+
const ErasureChunk &chunk) = 0;
42+
/// Store PoV
43+
virtual void putPov(const CandidateHash &candidate_hash,
44+
const ParachainBlock &pov) = 0;
45+
/// Store PersistedValidationData
46+
virtual void putData(const CandidateHash &candidate_hash,
47+
const PersistedValidationData &data) = 0;
48+
};
49+
} // namespace kagome::parachain
50+
51+
#endif // KAGOME_PARACHAIN_AVAILABILITY_STORE_STORE_HPP
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**
2+
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#include "parachain/availability/store/store_impl.hpp"
7+
8+
namespace kagome::parachain {
9+
bool AvailabilityStoreImpl::hasChunk(const CandidateHash &candidate_hash,
10+
ValidatorIndex index) {
11+
auto it = per_candidate_.find(candidate_hash);
12+
if (it == per_candidate_.end()) {
13+
return false;
14+
}
15+
return it->second.chunks.count(index) != 0;
16+
}
17+
18+
bool AvailabilityStoreImpl::hasPov(const CandidateHash &candidate_hash) {
19+
auto it = per_candidate_.find(candidate_hash);
20+
if (it == per_candidate_.end()) {
21+
return false;
22+
}
23+
return it->second.pov.has_value();
24+
}
25+
26+
bool AvailabilityStoreImpl::hasData(const CandidateHash &candidate_hash) {
27+
auto it = per_candidate_.find(candidate_hash);
28+
if (it == per_candidate_.end()) {
29+
return false;
30+
}
31+
return it->second.data.has_value();
32+
}
33+
34+
std::optional<AvailabilityStore::ErasureChunk>
35+
AvailabilityStoreImpl::getChunk(const CandidateHash &candidate_hash,
36+
ValidatorIndex index) {
37+
auto it = per_candidate_.find(candidate_hash);
38+
if (it == per_candidate_.end()) {
39+
return std::nullopt;
40+
}
41+
auto it2 = it->second.chunks.find(index);
42+
if (it2 == it->second.chunks.end()) {
43+
return std::nullopt;
44+
}
45+
return it2->second;
46+
}
47+
48+
std::optional<AvailabilityStore::ParachainBlock>
49+
AvailabilityStoreImpl::getPov(const CandidateHash &candidate_hash) {
50+
auto it = per_candidate_.find(candidate_hash);
51+
if (it == per_candidate_.end()) {
52+
return std::nullopt;
53+
}
54+
return it->second.pov;
55+
}
56+
57+
void AvailabilityStoreImpl::putChunk(const CandidateHash &candidate_hash,
58+
const ErasureChunk &chunk) {
59+
per_candidate_[candidate_hash].chunks[chunk.index] = chunk;
60+
}
61+
62+
void AvailabilityStoreImpl::putPov(const CandidateHash &candidate_hash,
63+
const ParachainBlock &pov) {
64+
per_candidate_[candidate_hash].pov = pov;
65+
}
66+
67+
void AvailabilityStoreImpl::putData(const CandidateHash &candidate_hash,
68+
const PersistedValidationData &data) {
69+
per_candidate_[candidate_hash].data = data;
70+
}
71+
} // namespace kagome::parachain
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#ifndef KAGOME_PARACHAIN_AVAILABILITY_STORE_STORE_IMPL_HPP
7+
#define KAGOME_PARACHAIN_AVAILABILITY_STORE_STORE_IMPL_HPP
8+
9+
#include "parachain/availability/store/store.hpp"
10+
11+
#include <unordered_map>
12+
13+
namespace kagome::parachain {
14+
class AvailabilityStoreImpl : public AvailabilityStore {
15+
public:
16+
~AvailabilityStoreImpl() override = default;
17+
18+
bool hasChunk(const CandidateHash &candidate_hash,
19+
ValidatorIndex index) override;
20+
bool hasPov(const CandidateHash &candidate_hash) override;
21+
bool hasData(const CandidateHash &candidate_hash) override;
22+
std::optional<ErasureChunk> getChunk(const CandidateHash &candidate_hash,
23+
ValidatorIndex index) override;
24+
std::optional<ParachainBlock> getPov(
25+
const CandidateHash &candidate_hash) override;
26+
void putChunk(const CandidateHash &candidate_hash,
27+
const ErasureChunk &chunk) override;
28+
void putPov(const CandidateHash &candidate_hash,
29+
const ParachainBlock &pov) override;
30+
void putData(const CandidateHash &candidate_hash,
31+
const PersistedValidationData &data) override;
32+
33+
private:
34+
struct PerCandidate {
35+
std::unordered_map<ValidatorIndex, ErasureChunk> chunks;
36+
std::optional<ParachainBlock> pov;
37+
std::optional<PersistedValidationData> data;
38+
};
39+
40+
std::unordered_map<CandidateHash, PerCandidate> per_candidate_;
41+
};
42+
} // namespace kagome::parachain
43+
44+
#endif // KAGOME_PARACHAIN_AVAILABILITY_STORE_STORE_IMPL_HPP

0 commit comments

Comments
 (0)
Please sign in to comment.