mirror of
https://github.com/basicswap/basicswap.git
synced 2025-01-18 16:44:34 +00:00
coins: Remove asyncore code.
This commit is contained in:
parent
d668a2f342
commit
c387bfec71
1 changed files with 0 additions and 366 deletions
|
@ -24,7 +24,6 @@
|
|||
|
||||
import struct
|
||||
import socket
|
||||
import asyncore
|
||||
from collections import namedtuple
|
||||
import time
|
||||
import sys
|
||||
|
@ -1801,368 +1800,3 @@ class msg_islock(object):
|
|||
|
||||
def __repr__(self):
|
||||
return "msg_islock(inputs=%s, txid=%064x)" % (repr(self.inputs), self.txid)
|
||||
|
||||
# This is what a callback should look like for NodeConn
|
||||
# Reimplement the on_* functions to provide handling for events
|
||||
class NodeConnCB(object):
|
||||
def __init__(self):
|
||||
self.verack_received = False
|
||||
# deliver_sleep_time is helpful for debugging race conditions in p2p
|
||||
# tests; it causes message delivery to sleep for the specified time
|
||||
# before acquiring the global lock and delivering the next message.
|
||||
self.deliver_sleep_time = None
|
||||
# Remember the services our peer has advertised
|
||||
self.peer_services = None
|
||||
|
||||
def set_deliver_sleep_time(self, value):
|
||||
with mininode_lock:
|
||||
self.deliver_sleep_time = value
|
||||
|
||||
def get_deliver_sleep_time(self):
|
||||
with mininode_lock:
|
||||
return self.deliver_sleep_time
|
||||
|
||||
# Spin until verack message is received from the node.
|
||||
# Tests may want to use this as a signal that the test can begin.
|
||||
# This can be called from the testing thread, so it needs to acquire the
|
||||
# global lock.
|
||||
def wait_for_verack(self):
|
||||
tm = 0
|
||||
while tm < 180:
|
||||
with mininode_lock:
|
||||
if self.verack_received:
|
||||
return
|
||||
time.sleep(0.05)
|
||||
tm += 0.05
|
||||
assert self.verack_received
|
||||
|
||||
def deliver(self, conn, message):
|
||||
deliver_sleep = self.get_deliver_sleep_time()
|
||||
if deliver_sleep is not None:
|
||||
time.sleep(deliver_sleep)
|
||||
with mininode_lock:
|
||||
try:
|
||||
print("Delivering ", 'on_' + message.command.decode('ascii'))
|
||||
getattr(self, 'on_' + message.command.decode('ascii'))(conn, message)
|
||||
except:
|
||||
print("ERROR delivering %s (%s)" % (repr(message),
|
||||
sys.exc_info()[0]))
|
||||
|
||||
def on_version(self, conn, message):
|
||||
if message.nVersion >= 209:
|
||||
conn.send_message(msg_verack())
|
||||
conn.ver_send = min(MY_VERSION, message.nVersion)
|
||||
if message.nVersion < 209:
|
||||
conn.ver_recv = conn.ver_send
|
||||
conn.nServices = message.nServices
|
||||
|
||||
def on_verack(self, conn, message):
|
||||
conn.ver_recv = conn.ver_send
|
||||
self.verack_received = True
|
||||
|
||||
def on_inv(self, conn, message):
|
||||
want = msg_getdata()
|
||||
for i in message.inv:
|
||||
if i.type != 0:
|
||||
want.inv.append(i)
|
||||
if len(want.inv):
|
||||
conn.send_message(want)
|
||||
|
||||
def on_addr(self, conn, message): pass
|
||||
def on_alert(self, conn, message): pass
|
||||
def on_getdata(self, conn, message): pass
|
||||
def on_getblocks(self, conn, message): pass
|
||||
def on_tx(self, conn, message): pass
|
||||
def on_block(self, conn, message): pass
|
||||
def on_getaddr(self, conn, message): pass
|
||||
def on_headers(self, conn, message): pass
|
||||
def on_getheaders(self, conn, message): pass
|
||||
def on_ping(self, conn, message):
|
||||
if conn.ver_send > BIP0031_VERSION:
|
||||
conn.send_message(msg_pong(message.nonce))
|
||||
def on_reject(self, conn, message): pass
|
||||
def on_open(self, conn): pass
|
||||
def on_close(self, conn): pass
|
||||
def on_mempool(self, conn): pass
|
||||
def on_notfound(self, message): pass
|
||||
def on_pong(self, conn, message): pass
|
||||
def on_feefilter(self, conn, message): pass
|
||||
def on_sendheaders(self, conn, message): pass
|
||||
def on_sendcmpct(self, conn, message): pass
|
||||
def on_cmpctblock(self, conn, message): pass
|
||||
def on_dandeliontx(self, conn, message): pass
|
||||
def on_getblocktxn(self, conn, message): pass
|
||||
def on_blocktxn(self, conn, message): pass
|
||||
def on_mnlistdiff(self, conn, message): pass
|
||||
def on_clsig(self, conn, message): pass
|
||||
def on_islock(self, conn, message): pass
|
||||
|
||||
class msg_witness_blocktxn(msg_blocktxn):
|
||||
def serialize(self):
|
||||
r = b""
|
||||
r += self.block_transactions.serialize(with_witness=True)
|
||||
return r
|
||||
|
||||
|
||||
# More useful callbacks and functions for NodeConnCB's which have a single NodeConn
|
||||
class SingleNodeConnCB(NodeConnCB):
|
||||
def __init__(self):
|
||||
NodeConnCB.__init__(self)
|
||||
self.connection = None
|
||||
self.ping_counter = 1
|
||||
self.last_pong = msg_pong()
|
||||
|
||||
def add_connection(self, conn):
|
||||
self.connection = conn
|
||||
|
||||
# Wrapper for the NodeConn's send_message function
|
||||
def send_message(self, message):
|
||||
self.connection.send_message(message)
|
||||
|
||||
def send_and_ping(self, message):
|
||||
self.send_message(message)
|
||||
self.sync_with_ping()
|
||||
|
||||
def on_pong(self, conn, message):
|
||||
self.last_pong = message
|
||||
|
||||
# Sync up with the node
|
||||
def sync_with_ping(self, timeout=30):
|
||||
def received_pong():
|
||||
return (self.last_pong.nonce == self.ping_counter)
|
||||
self.send_message(msg_ping(nonce=self.ping_counter))
|
||||
success = wait_until(received_pong, timeout=timeout)
|
||||
self.ping_counter += 1
|
||||
return success
|
||||
|
||||
# The actual NodeConn class
|
||||
# This class provides an interface for a p2p connection to a specified node
|
||||
class NodeConn(asyncore.dispatcher):
|
||||
messagemap = {
|
||||
b"version": msg_version,
|
||||
b"verack": msg_verack,
|
||||
b"addr": msg_addr,
|
||||
b"alert": msg_alert,
|
||||
b"inv": msg_inv,
|
||||
b"getdata": msg_getdata,
|
||||
b"getblocks": msg_getblocks,
|
||||
b"tx": msg_tx,
|
||||
b"block": msg_block,
|
||||
b"getaddr": msg_getaddr,
|
||||
b"ping": msg_ping,
|
||||
b"pong": msg_pong,
|
||||
b"headers": msg_headers,
|
||||
b"getheaders": msg_getheaders,
|
||||
b"reject": msg_reject,
|
||||
b"mempool": msg_mempool,
|
||||
b"notfound": msg_notfound,
|
||||
b"feefilter": msg_feefilter,
|
||||
b"sendheaders": msg_sendheaders,
|
||||
b"sendcmpct": msg_sendcmpct,
|
||||
b"cmpctblock": msg_cmpctblock,
|
||||
b"dandeliontx": msg_dandeliontx,
|
||||
b"getblocktxn": msg_getblocktxn,
|
||||
b"blocktxn": msg_blocktxn,
|
||||
b"mnlistdiff": msg_mnlistdiff,
|
||||
b"clsig": msg_clsig,
|
||||
b"islock": msg_islock
|
||||
}
|
||||
MAGIC_BYTES = {
|
||||
"mainnet": b"\xe3\xd9\xfe\xf1", # mainnet
|
||||
"testnet3": b"\xcf\xfc\xbe\xea", # testnet3
|
||||
"regtest": b"\xfa\xbf\xb5\xda", # regtest
|
||||
}
|
||||
|
||||
def __init__(self, dstaddr, dstport, rpc, callback, net="regtest", services=NODE_NETWORK, send_version=True):
|
||||
asyncore.dispatcher.__init__(self, map=mininode_socket_map)
|
||||
self.dstaddr = dstaddr
|
||||
self.dstport = dstport
|
||||
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
self.sendbuf = b""
|
||||
self.recvbuf = b""
|
||||
self.ver_send = 209
|
||||
self.ver_recv = 209
|
||||
self.last_sent = 0
|
||||
self.state = "connecting"
|
||||
self.network = net
|
||||
self.cb = callback
|
||||
self.disconnect = False
|
||||
self.nServices = 0
|
||||
|
||||
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))
|
||||
|
||||
try:
|
||||
self.connect((dstaddr, dstport))
|
||||
print("Connection to " + dstaddr + ':' + \
|
||||
str(dstport) + " successful. State is " + self.state)
|
||||
except:
|
||||
print("Connection to " + dstaddr + ':' + str(dstport) + " failed.")
|
||||
self.handle_close()
|
||||
self.rpc = rpc
|
||||
|
||||
def handle_connect(self):
|
||||
if self.state != "connected":
|
||||
logger.debug("MiniNode: Connected & Listening: \n")
|
||||
self.state = "connected"
|
||||
self.cb.on_open(self)
|
||||
|
||||
def handle_close(self):
|
||||
print("MiniNode: Closing Connection to %s:%d... "
|
||||
% (self.dstaddr, self.dstport))
|
||||
logger.debug("MiniNode: Closing Connection to %s:%d... "
|
||||
% (self.dstaddr, self.dstport))
|
||||
self.state = "closed"
|
||||
self.recvbuf = b""
|
||||
self.sendbuf = b""
|
||||
try:
|
||||
self.close()
|
||||
except:
|
||||
pass
|
||||
self.cb.on_close(self)
|
||||
|
||||
def handle_read(self):
|
||||
try:
|
||||
t = self.recv(8192)
|
||||
if len(t) > 0:
|
||||
self.recvbuf += t
|
||||
self.got_data()
|
||||
except:
|
||||
pass
|
||||
|
||||
def readable(self):
|
||||
return True
|
||||
|
||||
def writable(self):
|
||||
with mininode_lock:
|
||||
pre_connection = self.state == "connecting"
|
||||
length = len(self.sendbuf)
|
||||
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:
|
||||
print("Closing connection in handle_write")
|
||||
self.handle_close()
|
||||
return
|
||||
self.sendbuf = self.sendbuf[sent:]
|
||||
|
||||
def got_data(self):
|
||||
try:
|
||||
while True:
|
||||
if len(self.recvbuf) < 4:
|
||||
return
|
||||
if self.recvbuf[:4] != self.MAGIC_BYTES[self.network]:
|
||||
raise ValueError("got garbage %s" % repr(self.recvbuf))
|
||||
if self.ver_recv < 209:
|
||||
if len(self.recvbuf) < 4 + 12 + 4:
|
||||
return
|
||||
command = self.recvbuf[4:4+12].split(b"\x00", 1)[0]
|
||||
msglen = struct.unpack("<i", self.recvbuf[4+12:4+12+4])[0]
|
||||
checksum = None
|
||||
if len(self.recvbuf) < 4 + 12 + 4 + msglen:
|
||||
return
|
||||
msg = self.recvbuf[4+12+4:4+12+4+msglen]
|
||||
self.recvbuf = self.recvbuf[4+12+4+msglen:]
|
||||
else:
|
||||
if len(self.recvbuf) < 4 + 12 + 4 + 4:
|
||||
return
|
||||
command = self.recvbuf[4:4+12].split(b"\x00", 1)[0]
|
||||
msglen = struct.unpack("<i", self.recvbuf[4+12:4+12+4])[0]
|
||||
checksum = self.recvbuf[4+12+4:4+12+4+4]
|
||||
if len(self.recvbuf) < 4 + 12 + 4 + 4 + msglen:
|
||||
return
|
||||
msg = self.recvbuf[4+12+4+4:4+12+4+4+msglen]
|
||||
th = sha256(msg)
|
||||
h = sha256(th)
|
||||
if checksum != h[:4]:
|
||||
raise ValueError("got bad checksum " + repr(self.recvbuf))
|
||||
self.recvbuf = self.recvbuf[4+12+4+4+msglen:]
|
||||
if command in self.messagemap:
|
||||
f = BytesIO(msg)
|
||||
t = self.messagemap[command]()
|
||||
t.deserialize(f)
|
||||
self.got_message(t)
|
||||
else:
|
||||
logger.debug("Unknown command: '%s' %s" % (command, repr(msg)))
|
||||
except Exception as e:
|
||||
logger.error('got_data:', repr(e))
|
||||
# import traceback
|
||||
# traceback.print_tb(sys.exc_info()[2])
|
||||
|
||||
def send_message(self, message, pushbuf=False):
|
||||
if self.state != "connected" and not pushbuf:
|
||||
print("State of connection is ", self.state)
|
||||
raise IOError('Not connected, no pushbuf')
|
||||
logger.debug("Send %s" % repr(message))
|
||||
command = message.command
|
||||
data = message.serialize()
|
||||
tmsg = self.MAGIC_BYTES[self.network]
|
||||
tmsg += command
|
||||
tmsg += b"\x00" * (12 - len(command))
|
||||
tmsg += struct.pack("<I", len(data))
|
||||
if self.ver_send >= 209:
|
||||
th = sha256(data)
|
||||
h = sha256(th)
|
||||
tmsg += h[:4]
|
||||
tmsg += data
|
||||
with mininode_lock:
|
||||
self.sendbuf += tmsg
|
||||
self.last_sent = time.time()
|
||||
|
||||
def got_message(self, message):
|
||||
if message.command == b"version":
|
||||
if message.nVersion <= BIP0031_VERSION:
|
||||
self.messagemap[b'ping'] = msg_ping_prebip31
|
||||
if self.last_sent + 30 * 60 < time.time():
|
||||
self.send_message(self.messagemap[b'ping']())
|
||||
logger.debug("Recv %s" % repr(message))
|
||||
self.cb.deliver(self, message)
|
||||
|
||||
def disconnect_node(self):
|
||||
self.disconnect = True
|
||||
|
||||
|
||||
class NetworkThread(Thread):
|
||||
def run(self):
|
||||
while mininode_socket_map:
|
||||
# We check for whether to disconnect outside of the asyncore
|
||||
# loop to workaround the behavior of asyncore when using
|
||||
# select
|
||||
disconnected = []
|
||||
for fd, obj in mininode_socket_map.items():
|
||||
if obj.disconnect:
|
||||
disconnected.append(obj)
|
||||
print("NetworkThread:run disconnecting %s %s" % (fd, obj.disconnect))
|
||||
[ obj.handle_close() for obj in disconnected ]
|
||||
asyncore.loop(0.1, use_poll=True, map=mininode_socket_map, count=1)
|
||||
|
||||
|
||||
# An exception we can raise if we detect a potential disconnect
|
||||
# (p2p or rpc) before the test is complete
|
||||
class EarlyDisconnectError(Exception):
|
||||
def __init__(self, value):
|
||||
self.value = value
|
||||
|
||||
def __str__(self):
|
||||
return repr(self.value)
|
||||
|
|
Loading…
Reference in a new issue