Skip to content

Commit bfb43ff

Browse files
committed
Merge branch 'master' of github.com:pipermerriam/py-geth
2 parents f1eea97 + 582250c commit bfb43ff

File tree

4 files changed

+92
-11
lines changed

4 files changed

+92
-11
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,6 @@ docs/_build
4949

5050
# Known Contracts
5151
**/known_contracts.json
52+
53+
# Created by tox run for geth logs
54+
logs

geth/accounts.py

+55
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,61 @@ def get_accounts(data_dir, **geth_kwargs):
3838

3939

4040
def create_new_account(data_dir, password, **geth_kwargs):
41+
"""Creates a new Ethereum account on geth.
42+
43+
This is useful for testing when you want to stress
44+
interaction (transfers) between Ethereum accounts.
45+
46+
This command communicates with ``geth`` command over
47+
terminal interaction. It creates keystore folder and new
48+
account there.
49+
50+
This function only works against offline geth processes,
51+
because geth builds an account cache when starting up.
52+
If geth process is already running you can create new
53+
accounts using
54+
`web3.personal.newAccount()
55+
<https://github.com/ethereum/go-ethereum/wiki/JavaScript-Console#personalnewaccount>_`
56+
RPC API.
57+
58+
59+
Example py.test fixture for tests:
60+
61+
.. code-block:: python
62+
63+
import os
64+
65+
from geth.wrapper import DEFAULT_PASSWORD_PATH
66+
from geth.accounts import create_new_account
67+
68+
69+
@pytest.fixture
70+
def target_account() -> str:
71+
'''Create a new Ethereum account on a running Geth node.
72+
73+
The account can be used as a withdrawal target for tests.
74+
75+
:return: 0x address of the account
76+
'''
77+
78+
# We store keystore files in the current working directory
79+
# of the test run
80+
data_dir = os.getcwd()
81+
82+
# Use the default password "this-is-not-a-secure-password"
83+
# as supplied in geth/default_blockchain_password file.
84+
# The supplied password must be bytes, not string,
85+
# as we only want ASCII characters and do not want to
86+
# deal encoding problems with passwords
87+
account = create_new_account(data_dir, DEFAULT_PASSWORD_PATH)
88+
return account
89+
90+
:param data_dir: Geth data fir path - where to keep "keystore" folder
91+
:param password: Path to a file containing the password
92+
for newly created account
93+
:param geth_kwargs: Extra command line arguments passwrord to geth
94+
:return: Account as 0x prefixed hex string
95+
"""
4196
if os.path.exists(password):
4297
geth_kwargs['password'] = password
4398

geth/chain.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,22 @@ def write_genesis_file(genesis_file_path,
8585
difficulty="0x400",
8686
mixhash="0x0000000000000000000000000000000000000000000000000000000000000000", # NOQA
8787
coinbase="0x3333333333333333333333333333333333333333",
88-
alloc=None):
88+
alloc=None,
89+
config=None):
8990

9091
if os.path.exists(genesis_file_path) and not overwrite:
9192
raise ValueError("Genesis file already present. call with `overwrite=True` to overwrite this file") # NOQA
9293

9394
if alloc is None:
9495
alloc = {}
9596

97+
if config is None:
98+
config = {
99+
'homesteadBlock': 0,
100+
'daoForkBlock': 0,
101+
'daoForSupport': True,
102+
}
103+
96104
genesis_data = {
97105
"nonce": nonce,
98106
"timestamp": timestamp,
@@ -103,6 +111,7 @@ def write_genesis_file(genesis_file_path,
103111
"mixhash": mixhash,
104112
"coinbase": coinbase,
105113
"alloc": alloc,
114+
"config": config,
106115
}
107116

108117
with open(genesis_file_path, 'w') as genesis_file:

geth/utils/proc.py

+24-10
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,40 @@
1-
import time
21
import signal
32

43
import gevent
54

65

7-
def wait_for_popen(proc, max_wait=5):
8-
wait_till = time.time() + 5
9-
while proc.poll() is None and time.time() < wait_till:
10-
gevent.sleep(0.1)
6+
def wait_for_popen(proc, timeout=30):
7+
try:
8+
with gevent.Timeout(30):
9+
while proc.poll() is None:
10+
gevent.sleep(0.1)
11+
except gevent.Timeout:
12+
pass
1113

1214

1315
def kill_proc(proc):
1416
try:
1517
if proc.poll() is None:
16-
proc.send_signal(signal.SIGINT)
17-
wait_for_popen(proc, 5)
18+
try:
19+
proc.send_signal(signal.SIGINT)
20+
wait_for_popen(proc, 30)
21+
except KeyboardInterrupt:
22+
print(
23+
"Trying to close geth process. Press Ctrl+C 2 more times "
24+
"to force quit"
25+
)
1826
if proc.poll() is None:
19-
proc.terminate()
20-
wait_for_popen(proc, 2)
27+
try:
28+
proc.terminate()
29+
wait_for_popen(proc, 10)
30+
except KeyboardInterrupt:
31+
print(
32+
"Trying to close geth process. Press Ctrl+C 1 more times "
33+
"to force quit"
34+
)
2135
if proc.poll() is None:
2236
proc.kill()
23-
wait_for_popen(proc, 1)
37+
wait_for_popen(proc, 2)
2438
except KeyboardInterrupt:
2539
proc.kill()
2640

0 commit comments

Comments
 (0)