Add tests for witness element and tree last methods. Strengthen testing by inserting a different commitment into the tree at each step.
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
#include "test/data/merkle_serialization.json.h"
|
||||
#include "test/data/merkle_witness_serialization.json.h"
|
||||
#include "test/data/merkle_path.json.h"
|
||||
#include "test/data/merkle_commitments.json.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
@@ -55,14 +56,20 @@ void expect_ser_test_vector(B& b, const C& c, const A& tree) {
|
||||
}
|
||||
|
||||
template<typename Tree, typename Witness>
|
||||
void test_tree(Array root_tests, Array ser_tests, Array witness_ser_tests, Array path_tests) {
|
||||
void test_tree(
|
||||
Array commitment_tests,
|
||||
Array root_tests,
|
||||
Array ser_tests,
|
||||
Array witness_ser_tests,
|
||||
Array path_tests
|
||||
)
|
||||
{
|
||||
Array::iterator commitment_iterator = commitment_tests.begin();
|
||||
Array::iterator root_iterator = root_tests.begin();
|
||||
Array::iterator ser_iterator = ser_tests.begin();
|
||||
Array::iterator witness_ser_iterator = witness_ser_tests.begin();
|
||||
Array::iterator path_iterator = path_tests.begin();
|
||||
|
||||
uint256 test_commitment = uint256S("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
|
||||
|
||||
Tree tree;
|
||||
|
||||
// The root of the tree at this point is expected to be the root of the
|
||||
@@ -75,12 +82,17 @@ void test_tree(Array root_tests, Array ser_tests, Array witness_ser_tests, Array
|
||||
vector<Witness> witnesses;
|
||||
|
||||
for (size_t i = 0; i < 16; i++) {
|
||||
uint256 test_commitment = uint256S((commitment_iterator++)->get_str());
|
||||
|
||||
// Witness here
|
||||
witnesses.push_back(tree.witness());
|
||||
|
||||
// Now append a commitment to the tree
|
||||
tree.append(test_commitment);
|
||||
|
||||
// Last element added to the tree was `test_commitment`
|
||||
ASSERT_TRUE(tree.last() == test_commitment);
|
||||
|
||||
// Check tree root consistency
|
||||
expect_test_vector(root_iterator, tree.root());
|
||||
|
||||
@@ -119,7 +131,8 @@ void test_tree(Array root_tests, Array ser_tests, Array witness_ser_tests, Array
|
||||
|
||||
std::vector<bool> commitment_bv;
|
||||
{
|
||||
std::vector<unsigned char> commitment_v(test_commitment.begin(), test_commitment.end());
|
||||
uint256 witnessed_commitment = wit.element();
|
||||
std::vector<unsigned char> commitment_v(witnessed_commitment.begin(), witnessed_commitment.end());
|
||||
commitment_bv = convertBytesVectorToVector(commitment_v);
|
||||
}
|
||||
|
||||
@@ -174,8 +187,9 @@ TEST(merkletree, vectors) {
|
||||
Array ser_tests = read_json(std::string(json_tests::merkle_serialization, json_tests::merkle_serialization + sizeof(json_tests::merkle_serialization)));
|
||||
Array witness_ser_tests = read_json(std::string(json_tests::merkle_witness_serialization, json_tests::merkle_witness_serialization + sizeof(json_tests::merkle_witness_serialization)));
|
||||
Array path_tests = read_json(std::string(json_tests::merkle_path, json_tests::merkle_path + sizeof(json_tests::merkle_path)));
|
||||
Array commitment_tests = read_json(std::string(json_tests::merkle_commitments, json_tests::merkle_commitments + sizeof(json_tests::merkle_commitments)));
|
||||
|
||||
test_tree<ZCTestingIncrementalMerkleTree, ZCTestingIncrementalWitness>(root_tests, ser_tests, witness_ser_tests, path_tests);
|
||||
test_tree<ZCTestingIncrementalMerkleTree, ZCTestingIncrementalWitness>(commitment_tests, root_tests, ser_tests, witness_ser_tests, path_tests);
|
||||
}
|
||||
|
||||
TEST(merkletree, emptyroots) {
|
||||
|
||||
Reference in New Issue
Block a user