Skip to content

Commit 0e0f5e4

Browse files
committed
Auto merge of zcash#3496 - bitcartel:3442_sapling_note_locking, r=str4d
Add Sapling note locking to the CWallet class Closes zcash#3442.
2 parents d2b5a2d + 761f8c8 commit 0e0f5e4

File tree

3 files changed

+86
-18
lines changed

3 files changed

+86
-18
lines changed

src/wallet/gtest/test_wallet.cpp

+33-1
Original file line numberDiff line numberDiff line change
@@ -2014,7 +2014,39 @@ TEST(WalletTests, SproutNoteLocking) {
20142014
EXPECT_TRUE(wallet.IsLockedNote(jsoutpt2));
20152015

20162016
// Test unlock all
2017-
wallet.UnlockAllNotes();
2017+
wallet.UnlockAllSproutNotes();
20182018
EXPECT_FALSE(wallet.IsLockedNote(jsoutpt));
20192019
EXPECT_FALSE(wallet.IsLockedNote(jsoutpt2));
20202020
}
2021+
2022+
TEST(WalletTests, SaplingNoteLocking) {
2023+
TestWallet wallet;
2024+
SaplingOutPoint sop1 {uint256(), 1};
2025+
SaplingOutPoint sop2 {uint256(), 2};
2026+
2027+
// Test selective locking
2028+
wallet.LockNote(sop1);
2029+
EXPECT_TRUE(wallet.IsLockedNote(sop1));
2030+
EXPECT_FALSE(wallet.IsLockedNote(sop2));
2031+
2032+
// Test selective unlocking
2033+
wallet.UnlockNote(sop1);
2034+
EXPECT_FALSE(wallet.IsLockedNote(sop1));
2035+
2036+
// Test multiple locking
2037+
wallet.LockNote(sop1);
2038+
wallet.LockNote(sop2);
2039+
EXPECT_TRUE(wallet.IsLockedNote(sop1));
2040+
EXPECT_TRUE(wallet.IsLockedNote(sop2));
2041+
2042+
// Test list
2043+
auto v = wallet.ListLockedSaplingNotes();
2044+
EXPECT_EQ(v.size(), 2);
2045+
EXPECT_TRUE(std::find(v.begin(), v.end(), sop1) != v.end());
2046+
EXPECT_TRUE(std::find(v.begin(), v.end(), sop2) != v.end());
2047+
2048+
// Test unlock all
2049+
wallet.UnlockAllSaplingNotes();
2050+
EXPECT_FALSE(wallet.IsLockedNote(sop1));
2051+
EXPECT_FALSE(wallet.IsLockedNote(sop2));
2052+
}

src/wallet/wallet.cpp

+43-12
Original file line numberDiff line numberDiff line change
@@ -3942,36 +3942,67 @@ void CWallet::ListLockedCoins(std::vector<COutPoint>& vOutpts)
39423942

39433943
void CWallet::LockNote(const JSOutPoint& output)
39443944
{
3945-
AssertLockHeld(cs_wallet); // setLockedNotes
3946-
setLockedNotes.insert(output);
3945+
AssertLockHeld(cs_wallet); // setLockedSproutNotes
3946+
setLockedSproutNotes.insert(output);
39473947
}
39483948

39493949
void CWallet::UnlockNote(const JSOutPoint& output)
39503950
{
3951-
AssertLockHeld(cs_wallet); // setLockedNotes
3952-
setLockedNotes.erase(output);
3951+
AssertLockHeld(cs_wallet); // setLockedSproutNotes
3952+
setLockedSproutNotes.erase(output);
39533953
}
39543954

3955-
void CWallet::UnlockAllNotes()
3955+
void CWallet::UnlockAllSproutNotes()
39563956
{
3957-
AssertLockHeld(cs_wallet); // setLockedNotes
3958-
setLockedNotes.clear();
3957+
AssertLockHeld(cs_wallet); // setLockedSproutNotes
3958+
setLockedSproutNotes.clear();
39593959
}
39603960

39613961
bool CWallet::IsLockedNote(const JSOutPoint& outpt) const
39623962
{
3963-
AssertLockHeld(cs_wallet); // setLockedNotes
3963+
AssertLockHeld(cs_wallet); // setLockedSproutNotes
39643964

3965-
return (setLockedNotes.count(outpt) > 0);
3965+
return (setLockedSproutNotes.count(outpt) > 0);
39663966
}
39673967

3968-
std::vector<JSOutPoint> CWallet::ListLockedNotes()
3968+
std::vector<JSOutPoint> CWallet::ListLockedSproutNotes()
39693969
{
3970-
AssertLockHeld(cs_wallet); // setLockedNotes
3971-
std::vector<JSOutPoint> vOutpts(setLockedNotes.begin(), setLockedNotes.end());
3970+
AssertLockHeld(cs_wallet); // setLockedSproutNotes
3971+
std::vector<JSOutPoint> vOutpts(setLockedSproutNotes.begin(), setLockedSproutNotes.end());
39723972
return vOutpts;
39733973
}
39743974

3975+
void CWallet::LockNote(const SaplingOutPoint& output)
3976+
{
3977+
AssertLockHeld(cs_wallet);
3978+
setLockedSaplingNotes.insert(output);
3979+
}
3980+
3981+
void CWallet::UnlockNote(const SaplingOutPoint& output)
3982+
{
3983+
AssertLockHeld(cs_wallet);
3984+
setLockedSaplingNotes.erase(output);
3985+
}
3986+
3987+
void CWallet::UnlockAllSaplingNotes()
3988+
{
3989+
AssertLockHeld(cs_wallet);
3990+
setLockedSaplingNotes.clear();
3991+
}
3992+
3993+
bool CWallet::IsLockedNote(const SaplingOutPoint& output) const
3994+
{
3995+
AssertLockHeld(cs_wallet);
3996+
return (setLockedSaplingNotes.count(output) > 0);
3997+
}
3998+
3999+
std::vector<SaplingOutPoint> CWallet::ListLockedSaplingNotes()
4000+
{
4001+
AssertLockHeld(cs_wallet);
4002+
std::vector<SaplingOutPoint> vOutputs(setLockedSaplingNotes.begin(), setLockedSaplingNotes.end());
4003+
return vOutputs;
4004+
}
4005+
39754006
/** @} */ // end of Actions
39764007

39774008
class CAffectedKeysVisitor : public boost::static_visitor<void> {

src/wallet/wallet.h

+10-5
Original file line numberDiff line numberDiff line change
@@ -948,7 +948,8 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
948948
CPubKey vchDefaultKey;
949949

950950
std::set<COutPoint> setLockedCoins;
951-
std::set<JSOutPoint> setLockedNotes;
951+
std::set<JSOutPoint> setLockedSproutNotes;
952+
std::set<SaplingOutPoint> setLockedSaplingNotes;
952953

953954
int64_t nTimeFirstKey;
954955

@@ -970,13 +971,17 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
970971
void UnlockAllCoins();
971972
void ListLockedCoins(std::vector<COutPoint>& vOutpts);
972973

973-
974974
bool IsLockedNote(const JSOutPoint& outpt) const;
975975
void LockNote(const JSOutPoint& output);
976976
void UnlockNote(const JSOutPoint& output);
977-
void UnlockAllNotes();
978-
std::vector<JSOutPoint> ListLockedNotes();
979-
977+
void UnlockAllSproutNotes();
978+
std::vector<JSOutPoint> ListLockedSproutNotes();
979+
980+
bool IsLockedNote(const SaplingOutPoint& output) const;
981+
void LockNote(const SaplingOutPoint& output);
982+
void UnlockNote(const SaplingOutPoint& output);
983+
void UnlockAllSaplingNotes();
984+
std::vector<SaplingOutPoint> ListLockedSaplingNotes();
980985

981986
/**
982987
* keystore implementation

0 commit comments

Comments
 (0)