The test sees only one entry in listbanned, the added one. If it is moved above the previous test, there will also be a second entry in listbanned, e.g.
0[
1 {'address': '127.0.0.1/32', ...},
2 {'address': 'pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion', ...}
3]
Here’s a suggestion to do that, add logging, and also assert pre-add and post-remove (with the same number of lines):
0+TOR_ADDR = "pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion"
1+
2 class SetBanTests(BitcoinTestFramework):
3
4+ def banned_tor_addr(self, *, tor_addr=TOR_ADDR):
5+ return list(filter(lambda ban_entry: ban_entry["address"] == tor_addr, self.nodes[1].listbanned()))
6+
7 def run_test(self):
8@@ -35,6 +40,13 @@ class SetBanTests(BitcoinTestFramework):
9 peerinfo = self.nodes[1].getpeerinfo()[0]
10 assert('noban' in peerinfo['permissions'])
11
12+ self.log.info("Test that a Tor v3 address can be banned/unbanned")
13+ assert(not self.banned_tor_addr())
14+ self.nodes[1].setban(TOR_ADDR, "add")
15+ assert(self.banned_tor_addr())
16+ self.nodes[1].setban(TOR_ADDR, "remove")
17+ assert(not self.banned_tor_addr())
18+
19 # If we remove the ban, Node 0 should be able to reconnect even without noban permission
20@@ -42,15 +54,5 @@ class SetBanTests(BitcoinTestFramework):
21 assert(not 'noban' in peerinfo['permissions'])
22
23- tor_addr = "pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion"
24- self.nodes[1].setban(tor_addr, "add")
25- is_banned = False
26- for ban_entry in self.nodes[1].listbanned():
27- if ban_entry['address'] == tor_addr:
28- is_banned = True
29- break
30- assert(is_banned)
31- self.nodes[1].setban(tor_addr, "remove")
32
33 if __name__ == '__main__':