Skip to content

Commit

Permalink
Create class hierarchy for SproutNotePlaintext.
Browse files Browse the repository at this point in the history
BaseNotePlaintext contains member variable for common attribute, value.
  • Loading branch information
bitcartel committed Apr 26, 2018
1 parent 5020a93 commit d266f40
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 16 deletions.
14 changes: 14 additions & 0 deletions src/gtest/test_joinsplit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

#include "zcash/prf.h"
#include "util.h"
#include "streams.h"
#include "version.h"
#include "serialize.h"

#include "zcash/JoinSplit.hpp"
#include "zcash/Note.hpp"
Expand Down Expand Up @@ -546,6 +549,17 @@ TEST(joinsplit, note_plaintexts)
ASSERT_TRUE(decrypted_note.value() == note.value());

ASSERT_TRUE(decrypted.memo == note_pt.memo);

// Check serialization of note plaintext
CDataStream ss(SER_DISK, PROTOCOL_VERSION);
ss << note_pt;
SproutNotePlaintext note_pt2;
ss >> note_pt2;
ASSERT_EQ(note_pt.value(), note.value());
ASSERT_EQ(note_pt.value(), note_pt2.value());
ASSERT_EQ(note_pt.memo, note_pt2.memo);
ASSERT_EQ(note_pt.rho, note_pt2.rho);
ASSERT_EQ(note_pt.r, note_pt2.r);
}

TEST(joinsplit, note_class)
Expand Down
4 changes: 2 additions & 2 deletions src/wallet/asyncrpcoperation_mergetoaddress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -466,11 +466,11 @@ bool AsyncRPCOperation_mergetoaddress::main_impl()
info.notes.push_back(note);
info.zkeys.push_back(changeKey);

jsInputValue += plaintext.value;
jsInputValue += plaintext.value();

LogPrint("zrpcunsafe", "%s: spending change (amount=%s)\n",
getId(),
FormatMoney(plaintext.value));
FormatMoney(plaintext.value()));

} catch (const std::exception& e) {
throw JSONRPCError(RPC_WALLET_ERROR, strprintf("Error decrypting output note of previous JoinSplit: %s", e.what()));
Expand Down
8 changes: 4 additions & 4 deletions src/wallet/asyncrpcoperation_sendmany.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -581,11 +581,11 @@ bool AsyncRPCOperation_sendmany::main_impl() {
SproutNote note = plaintext.note(frompaymentaddress_);
info.notes.push_back(note);

jsInputValue += plaintext.value;
jsInputValue += plaintext.value();

LogPrint("zrpcunsafe", "%s: spending change (amount=%s)\n",
getId(),
FormatMoney(plaintext.value)
FormatMoney(plaintext.value())
);

} catch (const std::exception& e) {
Expand Down Expand Up @@ -884,14 +884,14 @@ bool AsyncRPCOperation_sendmany::find_unspent_notes() {
}

for (CSproutNotePlaintextEntry & entry : entries) {
z_inputs_.push_back(SendManyInputJSOP(entry.jsop, entry.plaintext.note(frompaymentaddress_), CAmount(entry.plaintext.value)));
z_inputs_.push_back(SendManyInputJSOP(entry.jsop, entry.plaintext.note(frompaymentaddress_), CAmount(entry.plaintext.value())));
std::string data(entry.plaintext.memo.begin(), entry.plaintext.memo.end());
LogPrint("zrpcunsafe", "%s: found unspent note (txid=%s, vjoinsplit=%d, ciphertext=%d, amount=%s, memo=%s)\n",
getId(),
entry.jsop.hash.ToString().substr(0, 10),
entry.jsop.js,
int(entry.jsop.n), // uint8_t
FormatMoney(entry.plaintext.value),
FormatMoney(entry.plaintext.value()),
HexStr(data).substr(0, 10)
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/wallet/rpcdisclosure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ UniValue z_validatepaymentdisclosure(const UniValue& params, bool fHelp)

string memoHexString = HexStr(npt.memo.data(), npt.memo.data() + npt.memo.size());
o.push_back(Pair("memo", memoHexString));
o.push_back(Pair("value", ValueFromAmount(npt.value)));
o.push_back(Pair("value", ValueFromAmount(npt.value())));

// Check the blockchain commitment matches decrypted note commitment
uint256 cm_blockchain = jsdesc.commitments[pd.payload.n];
Expand Down
8 changes: 4 additions & 4 deletions src/wallet/rpcwallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2549,7 +2549,7 @@ UniValue z_listunspent(const UniValue& params, bool fHelp)
obj.push_back(Pair("confirmations", entry.nHeight));
obj.push_back(Pair("spendable", pwalletMain->HaveSpendingKey(entry.address)));
obj.push_back(Pair("address", CZCPaymentAddress(entry.address).ToString()));
obj.push_back(Pair("amount", ValueFromAmount(CAmount(entry.plaintext.value))));
obj.push_back(Pair("amount", ValueFromAmount(CAmount(entry.plaintext.value()))));
std::string data(entry.plaintext.memo.begin(), entry.plaintext.memo.end());
obj.push_back(Pair("memo", HexStr(data)));
results.push_back(obj);
Expand Down Expand Up @@ -3179,7 +3179,7 @@ CAmount getBalanceZaddr(std::string address, int minDepth = 1, bool ignoreUnspen
LOCK2(cs_main, pwalletMain->cs_wallet);
pwalletMain->GetFilteredNotes(entries, address, minDepth, true, ignoreUnspendable);
for (auto & entry : entries) {
balance += CAmount(entry.plaintext.value);
balance += CAmount(entry.plaintext.value());
}
return balance;
}
Expand Down Expand Up @@ -3240,7 +3240,7 @@ UniValue z_listreceivedbyaddress(const UniValue& params, bool fHelp)
for (CSproutNotePlaintextEntry & entry : entries) {
UniValue obj(UniValue::VOBJ);
obj.push_back(Pair("txid",entry.jsop.hash.ToString()));
obj.push_back(Pair("amount", ValueFromAmount(CAmount(entry.plaintext.value))));
obj.push_back(Pair("amount", ValueFromAmount(CAmount(entry.plaintext.value()))));
std::string data(entry.plaintext.memo.begin(), entry.plaintext.memo.end());
obj.push_back(Pair("memo", HexStr(data)));
// (txid, jsindex, jsoutindex) is needed to globally identify a note
Expand Down Expand Up @@ -4146,7 +4146,7 @@ UniValue z_mergetoaddress(const UniValue& params, bool fHelp)
// Find unspent notes and update estimated size
for (CSproutNotePlaintextEntry& entry : entries) {
noteCounter++;
CAmount nValue = entry.plaintext.value;
CAmount nValue = entry.plaintext.value();

if (!maxedOutNotesFlag) {
// If we haven't added any notes yet and the merge is to a
Expand Down
4 changes: 2 additions & 2 deletions src/zcash/Note.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ SproutNotePlaintext::SproutNotePlaintext(
const SproutNote& note,
boost::array<unsigned char, ZC_MEMO_SIZE> memo) : memo(memo)
{
value = note.value();
value_ = note.value();
rho = note.rho;
r = note.r;
}

SproutNote SproutNotePlaintext::note(const PaymentAddress& addr) const
{
return SproutNote(addr.a_pk, value, rho, r);
return SproutNote(addr.a_pk, value_, rho, r);
}

SproutNotePlaintext SproutNotePlaintext::decrypt(const ZCNoteDecryption& decryptor,
Expand Down
17 changes: 14 additions & 3 deletions src/zcash/Note.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,18 @@ class SproutNote : public BaseNote {
uint256 nullifier(const SpendingKey& a_sk) const;
};

class SproutNotePlaintext {
class BaseNotePlaintext {
protected:
uint64_t value_ = 0;
public:
BaseNotePlaintext() {}
virtual ~BaseNotePlaintext() {}

inline uint64_t value() const { return value_; }
};

class SproutNotePlaintext : public BaseNotePlaintext {
public:
uint64_t value = 0;
uint256 rho;
uint256 r;
boost::array<unsigned char, ZC_MEMO_SIZE> memo;
Expand All @@ -51,6 +60,8 @@ class SproutNotePlaintext {

SproutNote note(const PaymentAddress& addr) const;

virtual ~SproutNotePlaintext() {}

ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
Expand All @@ -62,7 +73,7 @@ class SproutNotePlaintext {
throw std::ios_base::failure("lead byte of SproutNotePlaintext is not recognized");
}

READWRITE(value);
READWRITE(value_);
READWRITE(rho);
READWRITE(r);
READWRITE(memo);
Expand Down

0 comments on commit d266f40

Please sign in to comment.