Changeset View
Changeset View
Standalone View
Standalone View
test/functional/test_framework/test_node.py
#!/usr/bin/env python3 | #!/usr/bin/env python3 | ||||
# Copyright (c) 2017 The Bitcoin Core developers | # Copyright (c) 2017 The Bitcoin Core developers | ||||
# Distributed under the MIT software license, see the accompanying | # Distributed under the MIT software license, see the accompanying | ||||
# file COPYING or http://www.opensource.org/licenses/mit-license.php. | # file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
"""Class for bitcoind node under test""" | """Class for bitcoind node under test""" | ||||
import decimal | import decimal | ||||
import errno | import errno | ||||
import http.client | import http.client | ||||
import json | import json | ||||
import logging | import logging | ||||
import os | import os | ||||
import subprocess | import subprocess | ||||
import time | import time | ||||
from .authproxy import JSONRPCException | from .authproxy import JSONRPCException | ||||
from .mininode import COIN, ToHex, FromHex, CTransaction, NodeConn | from .mininode import COIN, ToHex, FromHex, CTransaction | ||||
from .util import ( | from .util import ( | ||||
assert_equal, | assert_equal, | ||||
get_rpc_proxy, | get_rpc_proxy, | ||||
rpc_url, | rpc_url, | ||||
wait_until, | wait_until, | ||||
p2p_port, | p2p_port, | ||||
) | ) | ||||
▲ Show 20 Lines • Show All 157 Lines • ▼ Show 20 Lines | def calculate_fee(self, tx): | ||||
billable_size_estimate += len(tx.vin) * 81 | billable_size_estimate += len(tx.vin) * 81 | ||||
return int(self.relay_fee() / 1000 * billable_size_estimate * COIN) | return int(self.relay_fee() / 1000 * billable_size_estimate * COIN) | ||||
def calculate_fee_from_txid(self, txid): | def calculate_fee_from_txid(self, txid): | ||||
ctx = FromHex(CTransaction(), self.getrawtransaction(txid)) | ctx = FromHex(CTransaction(), self.getrawtransaction(txid)) | ||||
return self.calculate_fee(ctx) | return self.calculate_fee(ctx) | ||||
def add_p2p_connection(self, p2p_conn, **kwargs): | def add_p2p_connection(self, p2p_conn, *args, **kwargs): | ||||
"""Add a p2p connection to the node. | """Add a p2p connection to the node. | ||||
This method adds the p2p connection to the self.p2ps list and also | This method adds the p2p connection to the self.p2ps list and also | ||||
returns the connection to the caller.""" | returns the connection to the caller.""" | ||||
if 'dstport' not in kwargs: | if 'dstport' not in kwargs: | ||||
kwargs['dstport'] = p2p_port(self.index) | kwargs['dstport'] = p2p_port(self.index) | ||||
if 'dstaddr' not in kwargs: | if 'dstaddr' not in kwargs: | ||||
kwargs['dstaddr'] = '127.0.0.1' | kwargs['dstaddr'] = '127.0.0.1' | ||||
p2p_conn.peer_connect(*args, **kwargs) | |||||
self.p2ps.append(p2p_conn) | self.p2ps.append(p2p_conn) | ||||
kwargs.update({'callback': p2p_conn}) | |||||
p2p_conn.add_connection(NodeConn(**kwargs)) | |||||
return p2p_conn | return p2p_conn | ||||
@property | @property | ||||
def p2p(self): | def p2p(self): | ||||
"""Return the first p2p connection | """Return the first p2p connection | ||||
Convenience property - most tests only use a single p2p connection to each | Convenience property - most tests only use a single p2p connection to each | ||||
node, so this saves having to write node.p2ps[0] many times.""" | node, so this saves having to write node.p2ps[0] many times.""" | ||||
assert self.p2ps, "No p2p connection" | assert self.p2ps, "No p2p connection" | ||||
return self.p2ps[0] | return self.p2ps[0] | ||||
def disconnect_p2ps(self): | def disconnect_p2ps(self): | ||||
"""Close all p2p connections to the node.""" | """Close all p2p connections to the node.""" | ||||
for p in self.p2ps: | for p in self.p2ps: | ||||
# Connection could have already been closed by other end. | p.peer_disconnect() | ||||
if p.connection is not None: | del self.p2ps[:] | ||||
p.connection.disconnect_node() | |||||
self.p2ps = [] | |||||
class TestNodeCLI(): | class TestNodeCLI(): | ||||
"""Interface to bitcoin-cli for an individual node""" | """Interface to bitcoin-cli for an individual node""" | ||||
def __init__(self, binary, datadir): | def __init__(self, binary, datadir): | ||||
self.args = [] | self.args = [] | ||||
self.binary = binary | self.binary = binary | ||||
Show All 36 Lines |