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"
6#include "kv/committable_tx.h"
8#include "kv_types.h"
12
13#include <vector>
14
15namespace ccf::kv
16{
18 {
19 public:
20 virtual bool fill_maps(
21 const std::vector<uint8_t>& data,
22 bool public_only,
25 ccf::kv::EntryFlags& entry_flags,
27 ccf::kv::MapCollection& new_maps,
28 ccf::ClaimsDigest& claims_digest,
29 std::optional<ccf::crypto::Sha256Hash>& commit_evidence_digest,
30 bool ignore_strict_versions = false) = 0;
31
32 virtual bool commit_deserialised(
35 ccf::kv::Term term,
36 const MapCollection& new_maps,
38 bool track_deletes_on_missing_keys) = 0;
39 };
40
42 {
43 private:
45 std::shared_ptr<TxHistory> history;
46 std::shared_ptr<ILedgerChunker> chunker;
47 const std::vector<uint8_t> data;
48 bool public_only;
49 ccf::kv::Version version;
50 Term term;
51 EntryFlags entry_flags;
52 OrderedChanges changes;
53 MapCollection new_maps;
55 ccf::ClaimsDigest claims_digest;
56 std::optional<ccf::crypto::Sha256Hash> commit_evidence_digest = {};
57
58 const std::optional<TxID> expected_txid;
59
60 public:
63 std::shared_ptr<TxHistory> history_,
64 std::shared_ptr<ILedgerChunker> chunker_,
65 const std::vector<uint8_t>& data_,
66 bool public_only_,
67 const std::optional<TxID>& expected_txid_) :
68 store(store_),
69 history(history_),
70 chunker(chunker_),
71 data(data_),
72 public_only(public_only_),
73 expected_txid(expected_txid_)
74 {}
75
77 {
78 return std::move(claims_digest);
79 }
80
81 std::optional<ccf::crypto::Sha256Hash>&& consume_commit_evidence_digest()
82 override
83 {
84 return std::move(commit_evidence_digest);
85 }
86
87 ApplyResult apply(bool track_deletes_on_missing_keys) override
88 {
89 if (!store->fill_maps(
90 data,
91 public_only,
92 version,
93 term,
94 entry_flags,
95 changes,
96 new_maps,
97 claims_digest,
98 commit_evidence_digest,
99 true))
100 {
101 return ApplyResult::FAIL;
102 }
103
104 if (expected_txid.has_value())
105 {
106 if (term != expected_txid->term || version != expected_txid->version)
107 {
109 "TxID mismatch during deserialisation. Expected {}.{}, got {}.{}",
110 expected_txid->term,
111 expected_txid->version,
112 term,
113 version);
114 return ApplyResult::FAIL;
115 }
116 }
117
118 if (!store->commit_deserialised(
119 changes,
120 version,
121 term,
122 new_maps,
123 hooks,
124 track_deletes_on_missing_keys))
125 {
126 return ApplyResult::FAIL;
127 }
128 auto success = ApplyResult::PASS;
129
130 auto search = changes.find(ccf::Tables::SIGNATURES);
131 if (search != changes.end())
132 {
133 switch (changes.size())
134 {
135 case 2:
136 if (
137 changes.find(ccf::Tables::SERIALISED_MERKLE_TREE) !=
138 changes.end())
139 {
140 break;
141 }
142 case 3:
143 if (
144 changes.find(ccf::Tables::SERIALISED_MERKLE_TREE) !=
145 changes.end() &&
146 changes.find(ccf::Tables::COSE_SIGNATURES) != changes.end())
147 {
148 break;
149 }
150 default:
152 "Unexpected contents in signature transaction {}", version);
153 return ApplyResult::FAIL;
154 }
155
156 if (history)
157 {
158 if (!history->verify_root_signatures(version))
159 {
160 LOG_FAIL_FMT("Failed to deserialise");
162 "Signature in transaction {} failed to verify", version);
163 return ApplyResult::FAIL;
164 }
165 }
167 }
168
169 search = changes.find(ccf::Tables::ENCRYPTED_PAST_LEDGER_SECRET);
170 if (search != changes.end())
171 {
173 }
174
175 if (history)
176 {
177 history->append_entry(
178 ccf::entry_leaf(data, commit_evidence_digest, claims_digest));
179 }
180
181 if (chunker)
182 {
183 chunker->append_entry_size(data.size());
184
186 {
187 chunker->produced_chunk_at(version - 1);
188 }
189
191 {
192 chunker->produced_chunk_at(version);
193 }
194 }
195
196 return success;
197 }
198
200 {
201 return hooks;
202 }
203
204 const std::vector<uint8_t>& get_entry() override
205 {
206 return data;
207 }
208
209 Term get_term() override
210 {
211 return term;
212 }
213
215 {
216 return version;
217 }
218
220 {
221 return false;
222 }
223
224 bool is_public_only() override
225 {
226 return public_only;
227 }
228
230 {
231 return false;
232 }
233 };
234}
Definition claims_digest.h:10
Definition kv_types.h:648
Definition deserialise.h:42
std::optional< ccf::crypto::Sha256Hash > && consume_commit_evidence_digest() override
Definition deserialise.h:81
ccf::kv::ConsensusHookPtrs & get_hooks() override
Definition deserialise.h:199
const std::vector< uint8_t > & get_entry() override
Definition deserialise.h:204
bool support_async_execution() override
Definition deserialise.h:219
ccf::kv::Version get_index() override
Definition deserialise.h:214
bool is_public_only() override
Definition deserialise.h:224
ApplyResult apply(bool track_deletes_on_missing_keys) override
Definition deserialise.h:87
Term get_term() override
Definition deserialise.h:209
bool should_rollback_to_last_committed() override
Definition deserialise.h:229
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:61
ccf::ClaimsDigest && consume_claims_digest() override
Definition deserialise.h:76
Definition deserialise.h:18
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 logger.h:357
#define LOG_FAIL_FMT
Definition logger.h:363
Definition app_interface.h:19
uint64_t Term
Definition kv_types.h:48
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
ApplyResult
Definition kv_types.h:341
@ PASS_SIGNATURE
Definition kv_types.h:343
@ FAIL
Definition kv_types.h:350
@ PASS
Definition kv_types.h:342
@ PASS_ENCRYPTED_PAST_LEDGER_SECRET
Definition kv_types.h:348
std::map< std::string, MapChanges > OrderedChanges
Definition tx.h:41
std::vector< ConsensusHookPtr > ConsensusHookPtrs
Definition hooks.h:22
view
Definition signatures.h:54