-
Notifications
You must be signed in to change notification settings - Fork 372
Expand file tree
/
Copy pathdeploy.py
More file actions
executable file
·154 lines (121 loc) · 4.33 KB
/
deploy.py
File metadata and controls
executable file
·154 lines (121 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/env python
from __future__ import print_function
import json
import os
import click
from ethereum import slogging
from ethereum._solidity import compile_contract
from ethereum.utils import decode_hex
from raiden.network.rpc.client import (
patch_send_message,
patch_send_transaction,
JSONRPCClient,
)
from raiden.settings import GAS_PRICE
from raiden.utils import get_contract_path
log = slogging.getLogger(__name__)
# Source files for all to be deployed solidity contracts
RAIDEN_CONTRACT_FILES = [
'NettingChannelLibrary.sol',
'ChannelManagerLibrary.sol',
'Registry.sol',
'EndpointRegistry.sol'
]
# Top level contracts to be deployed. Dependencies are handled automatically
# in `JSONRPCClient.deploy_solidity_contract()`
CONTRACTS_TO_DEPLOY = [
'Registry.sol:Registry',
'EndpointRegistry.sol:EndpointRegistry'
]
def patch_deploy_solidity_contract():
"""
Patch `JSONRPCClient.deploy_solidity_contract()` to not create a copy of
the `libraries` dict parameter by removing the assignment in
`raiden.network.rpc.client.py:474` via AST manipulation.
This allows us to access the addresses of the deployed dependencies until
the rpc client is fixed.
"""
import ast
from ast import NodeTransformer
from inspect import getsource, getsourcefile
from textwrap import dedent
class RemoveLibraryDeref(NodeTransformer):
"""
Removes the AST node representing the line
` libraries = dict(libraries)`
"""
def visit_Assign(self, node): # pylint: disable=no-self-use
is_libraries = (
len(node.targets) == 1 and
isinstance(node.targets[0], ast.Name) and
node.targets[0].id == 'libraries'
)
if is_libraries:
return None
return node
ast_ = ast.parse(dedent(getsource(JSONRPCClient.deploy_solidity_contract)))
ast_ = RemoveLibraryDeref().visit(ast_)
code = compile(ast_, getsourcefile(JSONRPCClient.deploy_solidity_contract), 'exec')
ctx = {}
exec( # pylint: disable=exec-used
code,
JSONRPCClient.deploy_solidity_contract.im_func.__globals__,
ctx,
)
JSONRPCClient.deploy_solidity_contract = ctx['deploy_solidity_contract']
def name_from_file(filename):
return os.path.split(filename)[-1].partition('.')[0]
def allcontracts(contract_files):
return {
"{}:{}".format(c, name_from_file(c)): compile_contract(
get_contract_path(c),
name_from_file(c),
optimize=False
)
for c in contract_files
}
def deploy_file(contract, compiled_contracts, client, gas_price=GAS_PRICE):
libraries = dict()
filename, _, name = contract.partition(":")
log.info("Deploying %s", name)
proxy = client.deploy_solidity_contract(
client.sender,
name,
compiled_contracts,
libraries,
'',
contract_path=filename,
gasprice=gas_price
)
log.info("Deployed %s @ 0x%s", name, proxy.address.encode('hex'))
libraries[contract] = proxy.address.encode('hex')
return libraries
def deploy_all(client, gas_price=GAS_PRICE):
compiled_contracts = allcontracts(RAIDEN_CONTRACT_FILES)
deployed = {}
for contract in CONTRACTS_TO_DEPLOY:
deployed.update(deploy_file(contract, compiled_contracts, client, gas_price))
return deployed
@click.command(help="Deploy the Raiden smart contracts.\n\n"
"Requires the private key to an account with enough balance to deploy all "
"four contracts.")
@click.option("--pretty", is_flag=True)
@click.option("--gas-price", default=4, help="Gas price to use in GWei", show_default=True)
@click.option("--port", type=int, default=8545, show_default=True)
@click.argument("privatekey_hex")
def main(privatekey_hex, pretty, gas_price, port):
slogging.configure(':debug')
privatekey = decode_hex(privatekey_hex)
patch_deploy_solidity_contract()
host = '127.0.0.1'
client = JSONRPCClient(
host,
port,
privatekey,
)
patch_send_transaction(client)
patch_send_message(client)
deployed = deploy_all(client, gas_price)
print(json.dumps(deployed, indent=2 if pretty else None))
if __name__ == '__main__':
main() # pylint: disable=no-value-for-parameter