CCF
Loading...
Searching...
No Matches
deserialise.h
Go to the documentation of this file.
1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the Apache 2.0 License.
3#pragma once
4
5#include "apply_changes.h"
7#include "kv/committable_tx.h"
9#include "kv_types.h"
13
14#include <vector>
15
16namespace ccf::kv
17{
19 {
20 public:
21 virtual ~ExecutionWrapperStore() = default;
22
23 virtual bool fill_maps(
24 const std::vector<uint8_t>& data,
25 bool public_only,
27 ccf::kv::Term& view,
28 ccf::kv::EntryFlags& entry_flags,
30 ccf::kv::MapCollection& new_maps,
31 ccf::ClaimsDigest& claims_digest,
32 std::optional<ccf::crypto::Sha256Hash>& commit_evidence_digest,
33 bool ignore_strict_versions = false) = 0;
34
35 virtual bool commit_deserialised(
38 ccf::kv::Term term,
39 const MapCollection& new_maps,
41 bool track_deletes_on_missing_keys) = 0;
42 };
43
45 {
46 private:
48 std::shared_ptr<TxHistory> history;
49 std::shared_ptr<ILedgerChunker> chunker;
50 const std::vector<uint8_t> data;
51 bool public_only;
52 ccf::kv::Version version{0};
53 Term term{0};
54 EntryFlags entry_flags{0};
55 OrderedChanges changes;
56 MapCollection new_maps;
58 ccf::ClaimsDigest claims_digest;
59 std::optional<ccf::crypto::Sha256Hash> commit_evidence_digest;
60
61 const std::optional<TxID> expected_txid;
62
63 public:
66 std::shared_ptr<TxHistory> history_,
67 std::shared_ptr<ILedgerChunker> chunker_,
68 const std::vector<uint8_t>& data_,
69 bool public_only_,
70 const std::optional<TxID>& expected_txid_) :
71 store(store_),
72 history(std::move(history_)),
73 chunker(std::move(chunker_)),
74 data(data_),
75 public_only(public_only_),
76 expected_txid(expected_txid_)
77 {}
78
80 {
81 return std::move(claims_digest);
82 }
83
84 std::optional<ccf::crypto::Sha256Hash>&& consume_commit_evidence_digest()
85 override
86 {
87 return std::move(commit_evidence_digest);
88 }
89
90 ApplyResult apply(bool track_deletes_on_missing_keys) override
91 {
92 if (!store->fill_maps(
93 data,
94 public_only,
95 version,
96 term,
97 entry_flags,
98 changes,
99 new_maps,
100 claims_digest,
101 commit_evidence_digest,
102 true))
103 {
104 return ApplyResult::FAIL;
105 }
106
107 if (expected_txid.has_value())
108 {
109 if (term != expected_txid->view || version != expected_txid->seqno)
110 {
112 "TxID mismatch during deserialisation. Expected {}.{}, got {}.{}",
113 expected_txid->view,
114 expected_txid->seqno,
115 term,
116 version);
117 return ApplyResult::FAIL;
118 }
119 }
120
121 if (!store->commit_deserialised(
122 changes,
123 version,
124 term,
125 new_maps,
126 hooks,
127 track_deletes_on_missing_keys))
128 {
129 return ApplyResult::FAIL;
130 }
131 auto success = ApplyResult::PASS;
132
133 const bool signature_in =
134 (changes.find(ccf::Tables::SIGNATURES) != changes.end());
135 const bool cose_signature_in =
136 (changes.find(ccf::Tables::COSE_SIGNATURES) != changes.end());
137
138 if (signature_in || cose_signature_in)
139 {
140 const bool merkle_tree_in =
141 changes.find(ccf::Tables::SERIALISED_MERKLE_TREE) != changes.end();
142 switch (changes.size())
143 {
144 case 2:
145 if (merkle_tree_in && (cose_signature_in != signature_in))
146 {
147 break;
148 }
149 case 3:
150 if (merkle_tree_in && cose_signature_in && signature_in)
151 {
152 break;
153 }
154 default:
156 "Unexpected contents in signature transaction {}", version);
157 return ApplyResult::FAIL;
158 }
159
160 if (history)
161 {
162 if (!history->verify_root_signatures(version))
163 {
164 LOG_FAIL_FMT("Failed to deserialise");
166 "Signature in transaction {} failed to verify", version);
167 return ApplyResult::FAIL;
168 }
169 }
171 }
172
173 auto search = changes.find(ccf::Tables::ENCRYPTED_PAST_LEDGER_SECRET);
174 if (search != changes.end())
175 {
177 }
178
179 if (history)
180 {
181 history->append_entry(
182 ccf::entry_leaf(data, commit_evidence_digest, claims_digest));
183 }
184
185 if (chunker)
186 {
187 chunker->append_entry_size(data.size());
188
189 if ((entry_flags & ccf::kv::EntryFlags::FORCE_LEDGER_CHUNK_BEFORE) != 0)
190 {
191 chunker->produced_chunk_at(version - 1);
192 }
193
194 if ((entry_flags & ccf::kv::EntryFlags::FORCE_LEDGER_CHUNK_AFTER) != 0)
195 {
196 chunker->produced_chunk_at(version);
197 }
198 }
199
200 return success;
201 }
202
204 {
205 return hooks;
206 }
207
208 const std::vector<uint8_t>& get_entry() override
209 {
210 return data;
211 }
212
213 Term get_term() override
214 {
215 return term;
216 }
217
219 {
220 return version;
221 }
222
224 {
225 return false;
226 }
227
228 bool is_public_only() override
229 {
230 return public_only;
231 }
232
234 {
235 return false;
236 }
237 };
238}
Definition claims_digest.h:10
Definition kv_types.h:600
Definition deserialise.h:45
std::optional< ccf::crypto::Sha256Hash > && consume_commit_evidence_digest() override
Definition deserialise.h:84
ccf::kv::ConsensusHookPtrs & get_hooks() override
Definition deserialise.h:203
const std::vector< uint8_t > & get_entry() override
Definition deserialise.h:208
bool support_async_execution() override
Definition deserialise.h:223
ccf::kv::Version get_index() override
Definition deserialise.h:218
bool is_public_only() override
Definition deserialise.h:228
ApplyResult apply(bool track_deletes_on_missing_keys) override
Definition deserialise.h:90
Term get_term() override
Definition deserialise.h:213
bool should_rollback_to_last_committed() override
Definition deserialise.h:233
CFTExecutionWrapper(ExecutionWrapperStore *store_, std::shared_ptr< TxHistory > history_, std::shared_ptr< ILedgerChunker > chunker_, const std::vector< uint8_t > &data_, bool public_only_, const std::optional< TxID > &expected_txid_)
Definition deserialise.h:64
ccf::ClaimsDigest && consume_claims_digest() override
Definition deserialise.h:79
Definition deserialise.h:19
virtual ~ExecutionWrapperStore()=default
virtual bool commit_deserialised(ccf::kv::OrderedChanges &changes, ccf::kv::Version v, ccf::kv::Term term, const MapCollection &new_maps, ccf::kv::ConsensusHookPtrs &hooks, bool track_deletes_on_missing_keys)=0
virtual bool fill_maps(const std::vector< uint8_t > &data, bool public_only, ccf::kv::Version &v, ccf::kv::Term &view, ccf::kv::EntryFlags &entry_flags, ccf::kv::OrderedChanges &changes, ccf::kv::MapCollection &new_maps, ccf::ClaimsDigest &claims_digest, std::optional< ccf::crypto::Sha256Hash > &commit_evidence_digest, bool ignore_strict_versions=false)=0
#define LOG_DEBUG_FMT
Definition internal_logger.h:14
#define LOG_FAIL_FMT
Definition internal_logger.h:16
Definition app_interface.h:18
uint64_t Term
Definition kv_types.h:46
uint64_t Version
Definition version.h:10
std::map< std::string, std::shared_ptr< AbstractMap > > MapCollection
Definition apply_changes.h:16
EntryFlags
Definition serialised_entry_format.h:15
@ FORCE_LEDGER_CHUNK_BEFORE
Definition serialised_entry_format.h:17
@ FORCE_LEDGER_CHUNK_AFTER
Definition serialised_entry_format.h:16
std::map< std::string, MapChanges > OrderedChanges
Definition tx.h:41
ApplyResult
Definition kv_types.h:305
@ PASS_SIGNATURE
Definition kv_types.h:307
@ FAIL
Definition kv_types.h:314
@ PASS
Definition kv_types.h:306
@ PASS_ENCRYPTED_PAST_LEDGER_SECRET
Definition kv_types.h:312
std::vector< ConsensusHookPtr > ConsensusHookPtrs
Definition hooks.h:22
STL namespace.