Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f4997c0

Browse files
committed
Fix path serialised_size calculation
1 parent 1a8a3a0 commit f4997c0

2 files changed

Lines changed: 12 additions & 1 deletion

File tree

merklecpp.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,14 @@ namespace merkle
424424
/// @brief The size of the serialised path in number of bytes
425425
size_t serialised_size() const
426426
{
427-
return sizeof(_leaf) + elements.size() * sizeof(Element);
427+
return sizeof(_leaf) +
428+
sizeof(uint64_t) + // leaf index
429+
sizeof(uint64_t) + // max index
430+
sizeof(uint64_t) + // number of elements
431+
elements.size() * (
432+
sizeof(Element::hash) + // hash
433+
sizeof(uint8_t) // direction
434+
);
428435
}
429436

430437
/// @brief Index of the leaf of the path

test/paths.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ int main()
5656
auto path = mt.path(i);
5757
if (!path->verify(root))
5858
throw std::runtime_error("path verification failed");
59+
std::vector<uint8_t> serialised_path;
60+
path->serialise(serialised_path);
61+
if (path->serialised_size() != serialised_path.size())
62+
throw std::runtime_error("serialised_size() != serialised_path.size()");
5963
}
6064
}
6165

0 commit comments

Comments
 (0)