-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor get_payments for future separation from wallet2 #5598
Changes from all commits
3104f4c
1a82ad0
b476443
2e2e1d9
0c43135
e65fd7f
0710b8f
e32ff8f
c0eed5b
0b573e3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,235 @@ | ||
// Copyright (c) 2014-2019, The Monero Project | ||
// | ||
// All rights reserved. | ||
// | ||
// Redistribution and use in source and binary forms, with or without modification, are | ||
// permitted provided that the following conditions are met: | ||
// | ||
// 1. Redistributions of source code must retain the above copyright notice, this list of | ||
// conditions and the following disclaimer. | ||
// | ||
// 2. Redistributions in binary form must reproduce the above copyright notice, this list | ||
// of conditions and the following disclaimer in the documentation and/or other | ||
// materials provided with the distribution. | ||
// | ||
// 3. Neither the name of the copyright holder nor the names of its contributors may be | ||
// used to endorse or promote products derived from this software without specific | ||
// prior written permission. | ||
// | ||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY | ||
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | ||
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL | ||
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | ||
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | ||
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF | ||
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
|
||
// FIXME: move this into a full wallet2 unit test suite, if possible | ||
|
||
#include "gtest/gtest.h" | ||
|
||
#include "wallet/wallet2.h" | ||
#include <string> | ||
|
||
|
||
class GetPayments : public ::testing::Test | ||
{ | ||
protected: | ||
virtual void SetUp() | ||
{ | ||
try | ||
{ | ||
m_wallet.generate("", password, recovery_key, true, false); | ||
} | ||
catch (const std::exception& e) | ||
{ | ||
LOG_ERROR("failed to generate wallet: " << e.what()); | ||
throw; | ||
} | ||
|
||
payment.m_block_height = 7; | ||
transfer.m_block_height = 7; | ||
block_height_containing_transaction = payment.m_block_height; | ||
|
||
actual_payments.insert( {{payment_id, payment}} ); | ||
actual_confirmed_txs.insert( {{payment_id, transfer}} ); | ||
|
||
matched_payments.clear(); | ||
confirmed_payments.clear(); | ||
ASSERT_EQ(0, matched_payments.size()); | ||
ASSERT_EQ(0, confirmed_payments.size()); | ||
} | ||
|
||
virtual void TearDown() | ||
{ | ||
} | ||
|
||
|
||
tools::wallet2 m_wallet; | ||
const std::string password = "testpass"; | ||
crypto::secret_key recovery_key = crypto::secret_key(); | ||
crypto::hash payment_id; | ||
|
||
uint64_t min_height = 0; | ||
uint64_t max_height = (uint64_t)-1; | ||
|
||
uint64_t block_height_containing_transaction; | ||
|
||
// for use with get_payments() | ||
tools::wallet2::payment_details payment; | ||
std::list<std::pair<crypto::hash, tools::wallet2::payment_details>> matched_payments; | ||
tools::wallet2::payment_container actual_payments; | ||
|
||
// for use with get_payments_out() | ||
tools::wallet2::confirmed_transfer_details transfer; | ||
std::list<std::pair<crypto::hash, tools::wallet2::confirmed_transfer_details>> confirmed_payments; | ||
std::unordered_map<crypto::hash, tools::wallet2::confirmed_transfer_details> actual_confirmed_txs; | ||
}; | ||
|
||
TEST_F(GetPayments, FindsMultiplePaymentsAtSameHeight) | ||
{ | ||
payment.m_block_height = 6; | ||
actual_payments.insert( {{payment_id, payment}} ); | ||
|
||
// One payment is already added by SetUp(). This adds another at the same height. | ||
payment.m_block_height = block_height_containing_transaction; | ||
actual_payments.insert( {{payment_id, payment}} ); | ||
|
||
m_wallet.get_payments(matched_payments, actual_payments, | ||
--block_height_containing_transaction, block_height_containing_transaction); | ||
ASSERT_EQ(2, matched_payments.size()); | ||
} | ||
TEST_F(GetPayments, FindsMultiplePaymentsAtSameHeight_Out) | ||
{ | ||
crypto::hash payment_id2; | ||
crypto::hash payment_id3; | ||
crypto::hash payment_id4; | ||
|
||
transfer.m_block_height = 6; | ||
actual_confirmed_txs.insert( {{payment_id2, transfer}} ); | ||
|
||
transfer.m_block_height = 8; | ||
actual_confirmed_txs.insert( {{payment_id3, transfer}} ); | ||
|
||
// One payment is already added by SetUp(). This adds another at the same height. | ||
transfer.m_block_height = block_height_containing_transaction; | ||
actual_confirmed_txs.insert( {{payment_id4, transfer}} ); | ||
|
||
m_wallet.get_payments_out(confirmed_payments, actual_confirmed_txs, | ||
--block_height_containing_transaction, block_height_containing_transaction); | ||
ASSERT_EQ(2, confirmed_payments.size()); | ||
} | ||
|
||
|
||
TEST_F(GetPayments, NotFoundBelowMinHeight) | ||
{ | ||
m_wallet.get_payments(matched_payments, actual_payments, | ||
++block_height_containing_transaction, block_height_containing_transaction); | ||
ASSERT_EQ(0, matched_payments.size()); | ||
} | ||
TEST_F(GetPayments, NotFoundBelowMinHeight_Out) | ||
{ | ||
m_wallet.get_payments_out(confirmed_payments, actual_confirmed_txs, | ||
++block_height_containing_transaction, block_height_containing_transaction); | ||
ASSERT_EQ(0, confirmed_payments.size()); | ||
} | ||
|
||
|
||
TEST_F(GetPayments, NotFoundAboveMaxHeight) | ||
{ | ||
m_wallet.get_payments(matched_payments, actual_payments, | ||
block_height_containing_transaction, --block_height_containing_transaction); | ||
ASSERT_EQ(0, matched_payments.size()); | ||
} | ||
TEST_F(GetPayments, NotFoundAboveMaxHeight_Out) | ||
{ | ||
m_wallet.get_payments_out(confirmed_payments, actual_confirmed_txs, | ||
block_height_containing_transaction, --block_height_containing_transaction); | ||
ASSERT_EQ(0, confirmed_payments.size()); | ||
} | ||
|
||
|
||
TEST_F(GetPayments, NotFoundIfNegative) | ||
{ | ||
m_wallet.get_payments(matched_payments, actual_payments, | ||
-1, -9999); | ||
ASSERT_EQ(0, matched_payments.size()); | ||
} | ||
TEST_F(GetPayments, NotFoundIfNegative_Out) | ||
{ | ||
m_wallet.get_payments_out(confirmed_payments, actual_confirmed_txs, | ||
-1, -9999); | ||
ASSERT_EQ(0, confirmed_payments.size()); | ||
} | ||
|
||
|
||
TEST_F(GetPayments, NotFoundIfNegativeReversed) | ||
{ | ||
m_wallet.get_payments(matched_payments, actual_payments, | ||
-9999, -1); | ||
ASSERT_EQ(0, matched_payments.size()); | ||
} | ||
TEST_F(GetPayments, NotFoundIfNegativeReversed_Out) | ||
{ | ||
m_wallet.get_payments_out(confirmed_payments, actual_confirmed_txs, | ||
-9999, -1); | ||
ASSERT_EQ(0, confirmed_payments.size()); | ||
} | ||
|
||
|
||
TEST_F(GetPayments, NotFoundIfMinHeightAboveMaxHeight) | ||
{ | ||
m_wallet.get_payments(matched_payments, actual_payments, | ||
max_height, min_height); | ||
ASSERT_EQ(0, matched_payments.size()); | ||
} | ||
TEST_F(GetPayments, NotFoundIfMinHeightAboveMaxHeight_Out) | ||
{ | ||
m_wallet.get_payments_out(confirmed_payments, actual_confirmed_txs, | ||
max_height, min_height); | ||
ASSERT_EQ(0, confirmed_payments.size()); | ||
} | ||
|
||
|
||
TEST_F(GetPayments, IsFoundWithinZeroAndMax) | ||
{ | ||
m_wallet.get_payments(matched_payments, actual_payments, | ||
min_height, max_height); | ||
ASSERT_EQ(1, matched_payments.size()); | ||
} | ||
TEST_F(GetPayments, IsFoundWithinZeroAndMax_Out) | ||
{ | ||
m_wallet.get_payments_out(confirmed_payments, actual_confirmed_txs, | ||
min_height, max_height); | ||
ASSERT_EQ(1, confirmed_payments.size()); | ||
} | ||
|
||
|
||
TEST_F(GetPayments, NotFoundAtMinHeight) | ||
{ | ||
m_wallet.get_payments(matched_payments, actual_payments, | ||
block_height_containing_transaction, block_height_containing_transaction); | ||
ASSERT_EQ(0, matched_payments.size()); | ||
} | ||
TEST_F(GetPayments, NotFoundAtMinHeight_Out) | ||
{ | ||
m_wallet.get_payments_out(confirmed_payments, actual_confirmed_txs, | ||
block_height_containing_transaction, block_height_containing_transaction); | ||
ASSERT_EQ(0, confirmed_payments.size()); | ||
} | ||
|
||
|
||
TEST_F(GetPayments, IsFoundAtMaxHeight) | ||
{ | ||
m_wallet.get_payments(matched_payments, actual_payments, | ||
min_height, block_height_containing_transaction); | ||
ASSERT_EQ(1, matched_payments.size()); | ||
} | ||
TEST_F(GetPayments, IsFoundAtMaxHeight_Out) | ||
{ | ||
m_wallet.get_payments_out(confirmed_payments, actual_confirmed_txs, | ||
min_height, block_height_containing_transaction); | ||
ASSERT_EQ(1, confirmed_payments.size()); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add more of them ? At the moment it's just one, does not test much. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should do: e65fd7f I cleaned the structure for the tests and added several new variants, all of which are now also implemented for the get_payments_out function. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's just one payment in the list. I'd expect a test to test when there are no payments, more than one payment per height, payments both in and out of the range you query, etc. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since you're breaking compatibility anyway, please remove the "min_height_inclusive" suffix.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done (0c43135).