test: Add multiple transactions and error handling tests for getreceivedbyaddress - #8
Conversation
d45ec3f to
fc2eb57
Compare
- Add test for multiple transactions to same address - Add test for invalid address format error
fc2eb57 to
9c68325
Compare
Review checklistThese steps can be useful to review any PR!
PR specific questionsThese are some questions to guide through the review of this specific PR :)
|
| self.nodes[0].sendtoaddress(addr_with_multiple_txs, Decimal("0.2")) | ||
| self.generatetoaddress(self.nodes[0], 1, addr_with_multiple_txs) | ||
| self.generate(self.nodes[0], 1) | ||
| balance = self.nodes[1].getreceivedbyaddress(addr_with_multiple_txs, 5, True) |
There was a problem hiding this comment.
min confirmation set to 5, and only 2 blocks has been mined. This is making the test to break
| addr_with_multiple_txs = self.nodes[1].getnewaddress() | ||
| self.nodes[0].sendtoaddress(addr_with_multiple_txs, Decimal("0.1")) | ||
| self.nodes[0].sendtoaddress(addr_with_multiple_txs, Decimal("0.2")) | ||
| self.generatetoaddress(self.nodes[0], 1, addr_with_multiple_txs) |
There was a problem hiding this comment.
Remove this and just call generate with 5 blocks. Otherwise the test fails.
| received_by_label_json) | ||
|
|
||
| # getreceivedbyaddress should return same balance because of 0 confirmations | ||
| # getreceivedbylabel should return same balance because of 0 confirmations |
There was a problem hiding this comment.
This is a necessary cleanup, but don't forget to add it in the PR description.
Also, it could be nice to put preparatory cleanups on a separate commit.
| self.nodes[0].sendtoaddress(addr_with_multiple_txs, Decimal("0.2")) | ||
| self.generatetoaddress(self.nodes[0], 1, addr_with_multiple_txs) | ||
| self.generate(self.nodes[0], 1) | ||
| balance = self.nodes[1].getreceivedbyaddress(addr_with_multiple_txs, 5, True) |
There was a problem hiding this comment.
This has two issues:
- first, min_confirmations is set to 5, and only 2 blocks were mined, this will make the total balance
0, failing the test. - second, even if the first issue is fixed, the
include_immature_coinbaseis set toTrue; this causes the total balance to be25.3 + feeinstead of0.3sincenode[1]is receiving the coinbase amount inaddr_with_multiple_txsfromnode[0]'s mining.
Make sure tests in CI are all passing!
There was a problem hiding this comment.
This has two issues:
- first, min_confirmations is set to 5, and only 2 blocks were mined, this will make the total balance 0, failing the test.
- second, even if the first issue is fixed, the
include_immature_coinbaseis set to True; this causes the total balance to be 25.3 + fee instead of 0.3 sincenode[1]is receiving the coinbase amount inaddr_with_multiple_txsfromnode[0]'s mining.Make sure tests in CI are all passing!
Can confirm, tested locally and this is exactly what happened.
qatkk
left a comment
There was a problem hiding this comment.
Agree with the previous comments. Just a small take on how the commits shall be done.
| assert_equal(balance, Decimal("0.3")) | ||
|
|
||
| # Test invalid address format error | ||
| assert_raises_rpc_error(-5, "Invalid Bitcoin address", self.nodes[1].getreceivedbyaddress, "invalid_address") |
There was a problem hiding this comment.
Maybe it would make sense to have this change in a separate commit for each commit to address only one of the issues presented in the goals of the PR.
|
Hey! I think maybe the description could be a little bit clearer.
Also maybe you can split up the commits as @qatkk mentions and add a specific description for both. In any case, it still has to address the comments made by other reviewers to fix the errors in the code. Also, please remember to run the tests locally before pushing to make sure everything passes :) |
0xlaga
left a comment
There was a problem hiding this comment.
Review of PR #8: test: Add multiple transactions and error handling tests for getreceivedbyaddress
Concept ACK
The idea of adding test coverage for getreceivedbyaddress (multi-tx accumulation, invalid address error) and fixing the comment typo is good. However, the test has a bug and doesn't pass locally. See below.
1. PR Description
The current description — "This PR adds comprehensive functional test coverage for the getreceivedbyaddress RPC method" — is too vague. A reader opening the PR can't tell what's actually changing without clicking into the diff. The commit message has better context but is easy to miss.
Suggested description:
Add two new test cases to the
getreceivedbyaddresssection ofwallet_listreceivedby.py:
- Multiple transactions to the same address: verify that
getreceivedbyaddresscorrectly sums the amounts from two separate sends to a single address.- Invalid address error: verify that passing a malformed address string returns the expected RPC error (
-5, "Invalid Bitcoin address").Also fix an incorrect comment that referenced
getreceivedbyaddresswhere the code actually callsgetreceivedbylabel.
2. Bug in the Test
Running the test locally, it fails:
AssertionError: not(0E-8 == 0.3)
at line 134. There are two interacting problems in this code:
self.generatetoaddress(self.nodes[0], 1, addr_with_multiple_txs) # (A)
self.generate(self.nodes[0], 1)
balance = self.nodes[1].getreceivedbyaddress(addr_with_multiple_txs, 5, True) # (B)
assert_equal(balance, Decimal("0.3"))(B) Primary failure — minconf=5 but only 2 confirmations:
Only 2 blocks are mined after the two sendtoaddress calls, so depth=2 < min_depth=5 and GetReceived skips the transactions entirely → returns 0.
(A) Hidden bug — coinbase reward to the test address:
generatetoaddress mines a block whose 25 BTC coinbase reward goes to addr_with_multiple_txs. Since include_immature_coinbase=True is passed, after fixing (B) the balance would be 25.3, not 0.3.
Suggested fix: use self.generate (no coinbase to the test address) and use the default minconf=1:
# Test multiple transactions to the same address
addr_with_multiple_txs = self.nodes[1].getnewaddress()
self.nodes[0].sendtoaddress(addr_with_multiple_txs, Decimal("0.1"))
self.nodes[0].sendtoaddress(addr_with_multiple_txs, Decimal("0.2"))
self.generate(self.nodes[0], 1)
balance = self.nodes[1].getreceivedbyaddress(addr_with_multiple_txs)
assert_equal(balance, Decimal("0.3"))3. Commit Organization
Per CONTRIBUTING.md, each commit should be a single logical change. This PR bundles three things into one commit:
- New test: multiple transactions to the same address
- New test: invalid address error path
- Unrelated comment typo fix (
getreceivedbyaddress→getreceivedbylabel)
The typo fix should be a separate commit since it's logically independent:
- Commit 1:
doc: Fix incorrect RPC name in wallet_listreceivedby comment - Commit 2:
test: Add getreceivedbyaddress tests for multi-tx accumulation and invalid address
|
Closing the PR after the workshop is finished, thank you everyone for interacting with it! :) |
This PR adds comprehensive functional test coverage for the getreceivedbyaddress RPC method.