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,
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 auto search = changes.find(ccf::Tables::SIGNATURES);
134 if (search != changes.end())
135 {
136 switch (changes.size())
137 {
138 case 2:
139 if (
140 changes.find(ccf::Tables::SERIALISED_MERKLE_TREE) !=
141 changes.end())
142 {
143 break;
144 }
145 case 3:
146 if (
147 changes.find(ccf::Tables::SERIALISED_MERKLE_TREE) !=
148 changes.end() &&
149 changes.find(ccf::Tables::COSE_SIGNATURES) != changes.end())
150 {
151 break;
152 }
153 default:
155 "Unexpected contents in signature transaction {}", version);
156 return ApplyResult::FAIL;
157 }
158
159 if (history)
160 {
161 if (!history->verify_root_signatures(version))
162 {
163 LOG_FAIL_FMT("Failed to deserialise");
165 "Signature in transaction {} failed to verify", version);
166 return ApplyResult::FAIL;
167 }
168 }
170 }
171
172 search = changes.find(ccf::Tables::ENCRYPTED_PAST_LEDGER_SECRET);
173 if (search != changes.end())
174 {
176 }
177
178 if (history)
179 {
180 history->append_entry(
181 ccf::entry_leaf(data, commit_evidence_digest, claims_digest));
182 }
183
184 if (chunker)
185 {
186 chunker->append_entry_size(data.size());
187
188 if ((entry_flags & ccf::kv::EntryFlags::FORCE_LEDGER_CHUNK_BEFORE) != 0)
189 {
190 chunker->produced_chunk_at(version - 1);
191 }
192
193 if ((entry_flags & ccf::kv::EntryFlags::FORCE_LEDGER_CHUNK_AFTER) != 0)
194 {
195 chunker->produced_chunk_at(version);
196 }
197 }
198
199 return success;
200 }
201
203 {
204 return hooks;
205 }
206
207 const std::vector<uint8_t>& get_entry() override
208 {
209 return data;
210 }
211
212 Term get_term() override
213 {
214 return term;
215 }
216
218 {
219 return version;
220 }
221
223 {
224 return false;
225 }
226
227 bool is_public_only() override
228 {
229 return public_only;
230 }
231
233 {
234 return false;
235 }
236 };
237}
Definition claims_digest.h:10
Definition kv_types.h:578
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:202
const std::vector< uint8_t > & get_entry() override
Definition deserialise.h:207
bool support_async_execution() override
Definition deserialise.h:222
ccf::kv::Version get_index() override
Definition deserialise.h:217
bool is_public_only() override
Definition deserialise.h:227
ApplyResult apply(bool track_deletes_on_missing_keys) override
Definition deserialise.h:90
Term get_term() override
Definition deserialise.h:212
bool should_rollback_to_last_committed() override
Definition deserialise.h:232
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:19
uint64_t Term
Definition kv_types.h:43
uint64_t Version
Definition version.h:8
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:302
@ PASS_SIGNATURE
Definition kv_types.h:304
@ FAIL
Definition kv_types.h:311
@ PASS
Definition kv_types.h:303
@ PASS_ENCRYPTED_PAST_LEDGER_SECRET
Definition kv_types.h:309
std::vector< ConsensusHookPtr > ConsensusHookPtrs
Definition hooks.h:22
view
Definition signatures.h:54
STL namespace.