From ac91ebbe9254779d9a9f08b3b0896a23e3e49922 Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Thu, 1 Sep 2016 12:47:44 +1200 Subject: [PATCH] Add test showing that the witness cache isn't being serialised --- src/wallet/gtest/test_wallet.cpp | 23 +++++++++++++++++++ src/zcash/IncrementalMerkleTree.hpp | 35 +++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/src/wallet/gtest/test_wallet.cpp b/src/wallet/gtest/test_wallet.cpp index 289bd265..c47147b4 100644 --- a/src/wallet/gtest/test_wallet.cpp +++ b/src/wallet/gtest/test_wallet.cpp @@ -146,6 +146,29 @@ CWalletTx GetValidSpend(const libzcash::SpendingKey& sk, return wtx; } +TEST(wallet_tests, note_data_serialisation) { + auto sk = libzcash::SpendingKey::random(); + auto wtx = GetValidReceive(sk, 10, true); + auto note = GetNote(sk, wtx, 0, 1); + auto nullifier = note.nullifier(sk); + + mapNoteData_t noteData; + JSOutPoint jsoutpt {wtx.GetTxid(), 0, 1}; + CNoteData nd {sk.address(), nullifier}; + ZCIncrementalMerkleTree tree; + nd.witnesses.push_front(tree.witness()); + noteData[jsoutpt] = nd; + + CDataStream ss(SER_DISK, CLIENT_VERSION); + ss << noteData; + + mapNoteData_t noteData2; + ss >> noteData2; + + EXPECT_EQ(noteData, noteData2); + EXPECT_EQ(noteData[jsoutpt].witnesses, noteData2[jsoutpt].witnesses); +} + TEST(wallet_tests, set_note_addrs_in_cwallettx) { auto sk = libzcash::SpendingKey::random(); auto wtx = GetValidReceive(sk, 10, true); diff --git a/src/zcash/IncrementalMerkleTree.hpp b/src/zcash/IncrementalMerkleTree.hpp index ab491392..419d7484 100644 --- a/src/zcash/IncrementalMerkleTree.hpp +++ b/src/zcash/IncrementalMerkleTree.hpp @@ -43,10 +43,19 @@ public: Hash empty_root(size_t depth) { return empty_roots.at(depth); } + template + friend bool operator==(const EmptyMerkleRoots& a, + const EmptyMerkleRoots& b); private: boost::array empty_roots; }; +template +bool operator==(const EmptyMerkleRoots& a, + const EmptyMerkleRoots& b) { + return a.empty_roots == b.empty_roots; +} + template class IncrementalWitness; @@ -90,6 +99,10 @@ public: return emptyroots.empty_root(Depth); } + template + friend bool operator==(const IncrementalMerkleTree& a, + const IncrementalMerkleTree& b); + private: static EmptyMerkleRoots emptyroots; boost::optional left; @@ -104,6 +117,15 @@ private: void wfcheck() const; }; +template +bool operator==(const IncrementalMerkleTree& a, + const IncrementalMerkleTree& b) { + return (a.emptyroots == b.emptyroots && + a.left == b.left && + a.right == b.right && + a.parents == b.parents); +} + template class IncrementalWitness { friend class IncrementalMerkleTree; @@ -130,6 +152,10 @@ public: cursor_depth = tree.next_depth(filled.size()); } + template + friend bool operator==(const IncrementalWitness& a, + const IncrementalWitness& b); + private: IncrementalMerkleTree tree; std::vector filled; @@ -139,6 +165,15 @@ private: IncrementalWitness(IncrementalMerkleTree tree) : tree(tree) {} }; +template +bool operator==(const IncrementalWitness& a, + const IncrementalWitness& b) { + return (a.tree == b.tree && + a.filled == b.filled && + a.cursor == b.cursor && + a.cursor_depth == b.cursor_depth); +} + class SHA256Compress : public uint256 { public: SHA256Compress() : uint256() {}