Browse Source

cmdtest.py: reimplement etherscan server using WSGI framework

The MMGen Project 2 weeks ago
parent
commit
fe9664412d
3 changed files with 42 additions and 42 deletions
  1. 17 10
      test/cmdtest_d/ct_ethdev.py
  2. 0 32
      test/cmdtest_d/etherscan.py
  3. 25 0
      test/cmdtest_d/httpd/etherscan.py

+ 17 - 10
test/cmdtest_d/ct_ethdev.py

@@ -58,7 +58,9 @@ from .common import (
 )
 from .ct_base import CmdTestBase
 from .ct_shared import CmdTestShared
-from .etherscan import run_etherscan_server
+from .httpd.etherscan import EtherscanServer
+
+etherscan_server = EtherscanServer()
 
 del_addrs = ('4', '1')
 dfl_sid = '98831F3A'
@@ -179,12 +181,6 @@ token_bals_getbalance = lambda k: {
 
 coin = cfg.coin
 
-def etherscan_server_start():
-	import threading
-	t = threading.Thread(target=run_etherscan_server, name='Etherscan server thread')
-	t.daemon = True
-	t.start()
-
 class CmdTestEthdev(CmdTestBase, CmdTestShared):
 	'Ethereum transacting, token deployment and tracking wallet operations'
 	networks = ('eth', 'etc')
@@ -244,8 +240,12 @@ class CmdTestEthdev(CmdTestBase, CmdTestShared):
 		('txview1_sig',          'viewing the signed transaction'),
 		('tx_status0_bad',       'getting the transaction status'),
 		('txsign1_ni',           'signing the transaction (non-interactive)'),
+
+		('etherscan_server_start','starting the Etherscan server'),
 		('txsend_etherscan_test','sending the transaction via Etherscan (simulation, with --test)'),
 		('txsend_etherscan',     'sending the transaction via Etherscan (simulation)'),
+		('etherscan_server_stop','stopping the Etherscan server'),
+
 		('txsend1',              'sending the transaction'),
 		('bal1',                 f'the {coin} balance'),
 
@@ -459,9 +459,6 @@ class CmdTestEthdev(CmdTestBase, CmdTestShared):
 		self.message = 'attack at dawn'
 		self.spawn_env['MMGEN_BOGUS_SEND'] = ''
 
-		if type(self) is CmdTestEthdev:
-			etherscan_server_start() # TODO: stop server when test group finishes executing
-
 	@property
 	async def rpc(self):
 		from mmgen.rpc import rpc_init
@@ -778,10 +775,20 @@ class CmdTestEthdev(CmdTestBase, CmdTestShared):
 		return self.tx_status(ext='{}.regtest.sigtx', expect_str='neither in mempool nor blockchain', exit_val=1)
 	def txsign1_ni(self):
 		return self.txsign(ni=True, dev_send=True)
+
+	def etherscan_server_start(self):
+		self.spawn(msg_only=True)
+		etherscan_server.start()
+		return 'ok'
 	def txsend_etherscan_test(self):
 		return self.txsend(add_args=['--tx-proxy=ether', '--test'], test='tx_proxy')
 	def txsend_etherscan(self):
 		return self.txsend(add_args=['--tx-proxy=ethersc'])
+	def etherscan_server_stop(self):
+		self.spawn(msg_only=True)
+		etherscan_server.stop()
+		return 'ok'
+
 	def txsend1(self):
 		return self.txsend()
 	def txview1_sig(self): # do after send so that TxID is displayed

+ 0 - 32
test/cmdtest_d/etherscan.py

@@ -1,32 +0,0 @@
-#!/usr/bin/env python3
-
-from http.server import HTTPServer, CGIHTTPRequestHandler
-
-from mmgen.util import msg
-from mmgen.util2 import port_in_use
-
-class handler(CGIHTTPRequestHandler):
-	header = b'HTTP/1.1 200 OK\nContent-type: text/html\n\n'
-
-	def do_response(self, target):
-		with open(f'test/ref/ethereum/etherscan-{target}.html') as fh:
-			text = fh.read()
-		self.wfile.write(self.header + text.encode())
-
-	def do_GET(self):
-		return self.do_response('form')
-
-	def do_POST(self):
-		return self.do_response('result')
-
-def run_etherscan_server(server_class=HTTPServer, handler_class=handler):
-
-	if port_in_use(28800):
-		msg('Port 28800 in use. Assuming etherscan server is running')
-		return True
-
-	msg('Etherscan server listening on port 28800')
-	server_address = ('localhost', 28800)
-	httpd = server_class(server_address, handler_class)
-	httpd.serve_forever()
-	msg('Etherscan server exiting')

+ 25 - 0
test/cmdtest_d/httpd/etherscan.py

@@ -0,0 +1,25 @@
+#!/usr/bin/env python3
+#
+# MMGen Wallet, a terminal-based cryptocurrency wallet
+# Copyright (C)2013-2025 The MMGen Project <mmgen@tuta.io>
+# Licensed under the GNU General Public License, Version 3:
+#   https://www.gnu.org/licenses
+# Public project repositories:
+#   https://github.com/mmgen/mmgen-wallet
+#   https://gitlab.com/mmgen/mmgen-wallet
+
+"""
+test.cmdtest_d.httpd.etherscan: Etherscan WSGI http server
+"""
+
+from . import HTTPD
+
+class EtherscanServer(HTTPD):
+	name = 'etherscan server'
+	port = 28800
+	content_type = 'text/html'
+
+	def make_response_body(self, method, environ):
+		targets = {'GET': 'form', 'POST': 'result'}
+		with open(f'test/ref/ethereum/etherscan-{targets[method]}.html') as fh:
+			return fh.read().encode()