Skip to content

Commit 3e8ac07

Browse files
EverCBOR wrapper in historical test (#7563)
Co-authored-by: Amaury Chamayou <amaury@xargs.fr>
1 parent 7c82ba5 commit 3e8ac07

3 files changed

Lines changed: 40 additions & 66 deletions

File tree

src/crypto/cbor.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,19 @@ namespace ccf::cbor
304304
return as_string;
305305
}
306306

307+
bool simple_to_boolean(const Simple& value)
308+
{
309+
switch (value)
310+
{
311+
case SimpleValue::False:
312+
return false;
313+
case SimpleValue::True:
314+
return true;
315+
default:
316+
throw CBORDecodeError("Simple value cannot be matched to boolean");
317+
}
318+
}
319+
307320
const Value& ValueImpl::array_at(size_t index) const
308321
{
309322
if (!std::holds_alternative<Array>(value))

src/crypto/cbor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ namespace ccf::cbor
7676

7777
Value parse(std::span<const uint8_t> raw);
7878
std::string to_string(const Value& value);
79+
bool simple_to_boolean(const Simple& value);
7980

8081
decltype(auto) rethrow_with_msg(auto&& f, std::string_view msg = {})
8182
{

src/node/test/historical_queries.cpp

Lines changed: 26 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "ccf/crypto/rsa_key_pair.h"
1111
#include "ccf/pal/locking.h"
1212
#include "ccf/receipt.h"
13+
#include "crypto/cbor.h"
1314
#include "crypto/openssl/hash.h"
1415
#include "ds/messaging.h"
1516
#include "ds/test/stub_writer.h"
@@ -254,90 +255,49 @@ size_t get_cache_limit_for_entries(
254255

255256
struct MerkleProofData
256257
{
258+
using PathItem =
259+
std::pair</* left/right */ bool, /* digest */ std::vector<uint8_t>>;
260+
257261
std::vector<uint8_t> write_set_digest;
258262
std::string commit_evidence;
259263
std::vector<uint8_t> claims_digest;
260-
std::vector<std::pair<int64_t, std::vector<uint8_t>>> path;
264+
std::vector<PathItem> path;
261265
};
262266

263-
std::vector<uint8_t> bstring_to_bytes(QCBORItem& item)
264-
{
265-
return {
266-
static_cast<const uint8_t*>(item.val.string.ptr),
267-
static_cast<const uint8_t*>(item.val.string.ptr) + item.val.string.len};
268-
}
269-
270-
std::string tstring_to_string(QCBORItem& item)
271-
{
272-
return {
273-
static_cast<const char*>(item.val.string.ptr),
274-
static_cast<const char*>(item.val.string.ptr) + item.val.string.len};
275-
}
276-
277267
MerkleProofData decode_merkle_proof(const std::vector<uint8_t>& encoded)
278268
{
279-
q_useful_buf_c buf{encoded.data(), encoded.size()};
280-
QCBORDecodeContext ctx;
281-
QCBORDecode_Init(&ctx, buf, QCBOR_DECODE_MODE_NORMAL);
282-
struct q_useful_buf_c params;
283-
QCBORDecode_EnterMap(&ctx, NULL);
284-
QCBORDecode_EnterArrayFromMapN(
285-
&ctx, ccf::MerkleProofLabel::MERKLE_PROOF_LEAF_LABEL);
286-
QCBORItem item;
287269
MerkleProofData data;
288270

289-
QCBORDecode_GetNext(&ctx, &item);
290-
REQUIRE(item.uDataType == QCBOR_TYPE_BYTE_STRING);
291-
data.write_set_digest = bstring_to_bytes(item);
271+
auto decoded = ccf::cbor::parse(encoded);
292272

293-
QCBORDecode_GetNext(&ctx, &item);
294-
REQUIRE(item.uDataType == QCBOR_TYPE_TEXT_STRING);
295-
data.commit_evidence = tstring_to_string(item);
273+
const auto& leaf = decoded->map_at(
274+
ccf::cbor::make_signed(ccf::MerkleProofLabel::MERKLE_PROOF_LEAF_LABEL));
296275

297-
QCBORDecode_GetNext(&ctx, &item);
298-
REQUIRE(item.uDataType == QCBOR_TYPE_BYTE_STRING);
299-
data.claims_digest = bstring_to_bytes(item);
276+
REQUIRE_EQ(leaf->size(), 3);
300277

301-
QCBORDecode_ExitArray(&ctx);
302-
QCBORDecode_EnterArrayFromMapN(
303-
&ctx, ccf::MerkleProofLabel::MERKLE_PROOF_PATH_LABEL);
278+
const auto& wsd = leaf->array_at(0)->as_bytes();
279+
data.write_set_digest.assign(wsd.begin(), wsd.end());
304280

305-
for (;;)
306-
{
307-
QCBORDecode_EnterArray(&ctx, &item);
308-
if (QCBORDecode_GetError(&ctx) != QCBOR_SUCCESS)
309-
break;
281+
data.commit_evidence = leaf->array_at(1)->as_string();
310282

311-
std::pair<int64_t, std::vector<uint8_t>> path_item;
283+
const auto& cd = leaf->array_at(2)->as_bytes();
284+
data.claims_digest.assign(cd.begin(), cd.end());
312285

313-
REQUIRE(QCBORDecode_GetNext(&ctx, &item) == QCBOR_SUCCESS);
314-
if (item.uDataType == CBOR_SIMPLEV_TRUE)
315-
{
316-
path_item.first = true;
317-
}
318-
else if (item.uDataType == CBOR_SIMPLEV_FALSE)
319-
{
320-
path_item.first = false;
321-
}
322-
else
323-
{
324-
// Not a valid CBOR boolean
325-
REQUIRE(false);
326-
}
286+
const auto& path = decoded->map_at(
287+
ccf::cbor::make_signed(ccf::MerkleProofLabel::MERKLE_PROOF_PATH_LABEL));
327288

328-
REQUIRE(QCBORDecode_GetNext(&ctx, &item) == QCBOR_SUCCESS);
329-
REQUIRE(item.uDataType == QCBOR_TYPE_BYTE_STRING);
330-
path_item.second = bstring_to_bytes(item);
331-
332-
data.path.push_back(path_item);
333-
QCBORDecode_ExitArray(&ctx);
289+
for (size_t i = 0; i < path->size(); i++)
290+
{
291+
const auto& node = path->array_at(i);
292+
const auto& dir = node->array_at(0)->as_simple();
293+
const auto& hash = node->array_at(1)->as_bytes();
294+
295+
MerkleProofData::PathItem item;
296+
item.first = ccf::cbor::simple_to_boolean(dir);
297+
item.second.assign(hash.begin(), hash.end());
298+
data.path.push_back(item);
334299
}
335300

336-
QCBORDecode_ExitArray(&ctx);
337-
QCBORDecode_ExitMap(&ctx);
338-
339-
REQUIRE(QCBORDecode_Finish(&ctx) == QCBOR_ERR_NO_MORE_ITEMS);
340-
341301
return data;
342302
}
343303

0 commit comments

Comments
 (0)