Skip to content

Commit

Permalink
qa: mininode learns when a socket connects, not its first action
Browse files Browse the repository at this point in the history
  • Loading branch information
TheBlueMatt authored and theuni committed Feb 13, 2017
1 parent cbfc5a6 commit 5b5e4f8
Showing 1 changed file with 24 additions and 12 deletions.
36 changes: 24 additions & 12 deletions qa/rpc-tests/test_framework/mininode.py
Original file line number Diff line number Diff line change
Expand Up @@ -1614,7 +1614,7 @@ class NodeConn(asyncore.dispatcher):
"regtest": b"\xfa\xbf\xb5\xda", # regtest
}

def __init__(self, dstaddr, dstport, rpc, callback, net="regtest", services=NODE_NETWORK):
def __init__(self, dstaddr, dstport, rpc, callback, net="regtest", services=NODE_NETWORK, send_version=True):
asyncore.dispatcher.__init__(self, map=mininode_socket_map)
self.log = logging.getLogger("NodeConn(%s:%d)" % (dstaddr, dstport))
self.dstaddr = dstaddr
Expand All @@ -1631,14 +1631,16 @@ def __init__(self, dstaddr, dstport, rpc, callback, net="regtest", services=NODE
self.disconnect = False
self.nServices = 0

# stuff version msg into sendbuf
vt = msg_version()
vt.nServices = services
vt.addrTo.ip = self.dstaddr
vt.addrTo.port = self.dstport
vt.addrFrom.ip = "0.0.0.0"
vt.addrFrom.port = 0
self.send_message(vt, True)
if send_version:
# stuff version msg into sendbuf
vt = msg_version()
vt.nServices = services
vt.addrTo.ip = self.dstaddr
vt.addrTo.port = self.dstport
vt.addrFrom.ip = "0.0.0.0"
vt.addrFrom.port = 0
self.send_message(vt, True)

print('MiniNode: Connecting to Bitcoin Node IP # ' + dstaddr + ':' \
+ str(dstport))

Expand All @@ -1652,8 +1654,9 @@ def show_debug_msg(self, msg):
self.log.debug(msg)

def handle_connect(self):
self.show_debug_msg("MiniNode: Connected & Listening: \n")
self.state = "connected"
if self.state != "connected":
self.show_debug_msg("MiniNode: Connected & Listening: \n")
self.state = "connected"

def handle_close(self):
self.show_debug_msg("MiniNode: Closing Connection to %s:%d... "
Expand Down Expand Up @@ -1681,11 +1684,20 @@ def readable(self):

def writable(self):
with mininode_lock:
pre_connection = self.state == "connecting"
length = len(self.sendbuf)
return (length > 0)
return (length > 0 or pre_connection)

def handle_write(self):
with mininode_lock:
# asyncore does not expose socket connection, only the first read/write
# event, thus we must check connection manually here to know when we
# actually connect
if self.state == "connecting":
self.handle_connect()
if not self.writable():
return

try:
sent = self.send(self.sendbuf)
except:
Expand Down

0 comments on commit 5b5e4f8

Please sign in to comment.