From 2ef80a2256313f5ea97708d14b4e45f3f197989f Mon Sep 17 00:00:00 2001 From: Vadim Yavorsky Date: Tue, 17 Dec 2024 15:52:06 +0000 Subject: [PATCH] Add ability to run geth in dev mode --- predeployed/test/tools/test_predeployed.py | 45 +++++++++++++++------- 1 file changed, 32 insertions(+), 13 deletions(-) diff --git a/predeployed/test/tools/test_predeployed.py b/predeployed/test/tools/test_predeployed.py index c722718..aea703e 100644 --- a/predeployed/test/tools/test_predeployed.py +++ b/predeployed/test/tools/test_predeployed.py @@ -5,7 +5,7 @@ import string -class GethInstance: +class GethInstance: def __init__(self, geth): self.geth = geth def __enter__(self): @@ -23,23 +23,23 @@ class TestPredeployed: def generate_extradata(self): self.password = ''.join(random.choices(string.ascii_lowercase + string.digits, k=12)) - password_filename = os.path.join(self.datadir, 'base_genesis.json') - with open(password_filename, 'w') as password_f: + self.password_filename = os.path.join(self.datadir, 'password.txt') + with open(self.password_filename, 'w') as password_f: password_f.writelines([self.password]) - process = subprocess.run(['geth', 'account', 'new', '--datadir', self.datadir, '--password', password_filename]) + process = subprocess.run(['geth', 'account', 'new', '--datadir', self.datadir, '--password', self.password_filename]) assert process.returncode == 0 - process = subprocess.Popen(['geth', 'account', 'list', '--datadir', self.datadir], stdout=subprocess.PIPE, universal_newlines=True) + process = subprocess.Popen(['geth', 'account', 'list', '--datadir', self.datadir], stdout=subprocess.PIPE, + universal_newlines=True) account0 = process.stdout.readline() - address = account0.split()[2][1:-1] - return '0x' + '00' * 32 + address + '00' * 65 + self.author_address = account0.split()[2][1:-1] + return '0x' + '00' * 32 + self.author_address + '00' * 65 def generate_genesis(self, allocations: dict = {}): base_genesis_filename = os.path.join(os.path.dirname(__file__), 'base_genesis.json') with open(base_genesis_filename) as base_genesis_file: genesis = json.load(base_genesis_file) genesis['alloc'].update(allocations) - genesis['extradata'] = self.generate_extradata() return genesis def run_geth(self, tmpdir, genesis): @@ -52,13 +52,32 @@ def run_geth(self, tmpdir, genesis): assert process.returncode == 0 # run geth - self.geth = subprocess.Popen(['geth', '--datadir', tmpdir, '--http'], stderr=subprocess.PIPE, universal_newlines=True) + self.geth = subprocess.Popen( + [ + 'geth', + '--datadir', tmpdir, + '--dev', + '--http' + ], + stderr=subprocess.PIPE, + universal_newlines=True + ) + output = [] while True: - assert self.geth.poll() is None - output_line = self.geth.stderr.readline() - if 'HTTP server started' in output_line: - break + return_code = self.geth.poll() + if return_code is None: + output_line = self.geth.stderr.readline() + output.append(output_line) + if 'HTTP server started' in output_line: + break + else: + # geth stopped + for line in output: + print(line) + for line in self.geth.stderr.readlines(): + print(line) + raise RuntimeError("Geth was not started") return GethInstance(self.geth)