Skip to content

Commit

Permalink
Add ability to run geth in dev mode
Browse files Browse the repository at this point in the history
  • Loading branch information
yavrsky committed Dec 17, 2024
1 parent 3478c1e commit 2ef80a2
Showing 1 changed file with 32 additions and 13 deletions.
45 changes: 32 additions & 13 deletions predeployed/test/tools/test_predeployed.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import string


class GethInstance:
class GethInstance:
def __init__(self, geth):
self.geth = geth
def __enter__(self):
Expand All @@ -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):
Expand All @@ -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)

Expand Down

0 comments on commit 2ef80a2

Please sign in to comment.