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