-
Notifications
You must be signed in to change notification settings - Fork 2
/
run_parity_ethereum.go
403 lines (381 loc) · 11 KB
/
run_parity_ethereum.go
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
// Copyright (c) [2019] [dabank.io]
// [devtools4chains] is licensed under the Mulan PSL v1.
// You can use this software according to the terms and conditions of the Mulan PSL v1.
// You may obtain a copy of Mulan PSL v1 at:
// http://license.coscl.org.cn/MulanPSL
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
// PURPOSE.
// See the Mulan PSL v1 for more details.
package devtools4chains
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"strings"
"time"
)
//some parity related const
const (
ParityCMD = "parity"
ParityDefaultJSONRPCPort = "8548"
ParityDefaultJSONRPCPortInt = 8548
ParityDefaultRichAddress = "00a329c0648769a73afac7f9381e08fb43dbea72"
ParityDefaultRichAddressPrivateKey = "4d5db4107d237df6a3d58ee5f70ae63d73d7658d4026f2eefd2f204c81682cb7"
)
// ParityEthereumDefaultArgs .
func ParityEthereumDefaultArgs() map[string]*string {
return map[string]*string{
"--no-ws": nil,
"--no-ipc": nil,
"--jsonrpc-port": pstring(ParityDefaultJSONRPCPort),
"--jsonrpc-interface": pstring("127.0.0.1"),
}
}
// RunParityEthereumConfig parity 以太坊/以太经典运行配置
type RunParityEthereumConfig struct {
DataDir DataDirOption
Args map[string]*string //k-v ,v 为nil时为flag
NotPrint2stdout bool //不打印到stdout(cmd 的stdout不会指定到os.stdout)
// NetworkID string
ChainJSON string //创建临时目录后,json 会写入临时目录下的parity.json里,通过命令行引用 (如果不使用临时目录,该选项不会生效)
}
// RunParityEthereum .
func RunParityEthereum(optionsP *RunParityEthereumConfig) (func(), error) {
if _, err := exec.LookPath(ParityCMD); err != nil {
return nil, fmt.Errorf("look path err %v", err)
}
killHooks := []killHook{}
var options RunParityEthereumConfig
var err error
if optionsP == nil {
options = RunParityEthereumConfig{}
} else {
options = *optionsP
}
if options.Args == nil {
options.Args = map[string]*string{}
}
var dataDir string
if options.DataDir.NewTmpDir {
for k, v := range options.Args {
if k == "-d" || k == "--base-path" {
return nil, fmt.Errorf("datadir specified in args (%v), NewTmpDir not work", v)
}
}
tmpDirPrefix := strings.TrimLeft(options.DataDir.TmpDirPrefix, "/")
dataDir = strings.TrimRight(os.TempDir(), "/") + "/" + tmpDirPrefix + "parity_data_tmp_" + time.Now().Format(rfc3339Variant) + "/"
err := os.MkdirAll(dataDir, 0777)
if err != nil {
return nil, fmt.Errorf("cannot create tmp dir: %v, err: %v", dataDir, err)
}
options.Args["-d"] = &dataDir
options.Args["--log-file"] = pstring(dataDir + "out.log")
if options.DataDir.NotRemoveTmpDirWhenKilling {
} else {
killHooks = append(killHooks, func() error {
return os.RemoveAll(dataDir)
})
}
if options.ChainJSON != "" {
jsonFile := dataDir + "parity.json"
if e := ioutil.WriteFile(jsonFile, []byte(options.ChainJSON), 0777); e != nil {
return nil, fmt.Errorf("failed to create parity.json in created tmp dir, %v", e)
}
options.Args["--chain"] = &jsonFile
}
}
args := []string{}
for k, v := range options.Args {
if v == nil {
args = append(args, k)
} else {
args = append(args, k+"="+*v)
}
}
closeChan := make(chan struct{})
cmd := exec.Command(ParityCMD, args...)
fmt.Println("[debug] parity-node args", cmd.Args)
if options.NotPrint2stdout {
} else {
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
}
err = cmd.Start()
if err != nil {
return nil, err
}
go func() {
fmt.Println("Waiting for message to kill parity")
<-closeChan
fmt.Println("Received message,killing parity server")
if e := cmd.Process.Kill(); e != nil {
fmt.Println("关闭 parity 时发生异常", e)
}
closeChan <- struct{}{}
}()
// err = cmd.Wait()
return func() {
closeChan <- struct{}{}
for _, hook := range killHooks {
hook()
}
<-closeChan
}, nil
}
//some chain json
const (
//copied from https://github.com/paritytech/parity-ethereum/blob/master/ethcore/res/instant_seal.json
// removed: account 0000000000000000000000000000000000001337
ParityDevChainJSON = `
{
"name": "DevelopmentChain",
"engine": {
"instantSeal": {
"params": {}
}
},
"params": {
"gasLimitBoundDivisor": "0x0400",
"accountStartNonce": "0x0",
"maximumExtraDataSize": "0x20",
"minGasLimit": "0x1388",
"networkID" : "0x11",
"registrar" : "0x0000000000000000000000000000000000001337",
"eip150Transition": "0x0",
"eip160Transition": "0x0",
"eip161abcTransition": "0x0",
"eip161dTransition": "0x0",
"eip155Transition": "0x0",
"eip98Transition": "0x7fffffffffffff",
"maxCodeSize": 24576,
"maxCodeSizeTransition": "0x0",
"eip140Transition": "0x0",
"eip211Transition": "0x0",
"eip214Transition": "0x0",
"eip658Transition": "0x0",
"eip145Transition": "0x0",
"eip1014Transition": "0x0",
"eip1052Transition": "0x0",
"wasmActivationTransition": "0x0"
},
"genesis": {
"seal": {
"generic": "0x0"
},
"difficulty": "0x20000",
"author": "0x0000000000000000000000000000000000000000",
"timestamp": "0x00",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"extraData": "0x",
"gasLimit": "0x7A1200"
},
"accounts": {
"0000000000000000000000000000000000000001": { "balance": "1", "builtin": { "name": "ecrecover", "pricing": { "linear": { "base": 3000, "word": 0 } } } },
"0000000000000000000000000000000000000002": { "balance": "1", "builtin": { "name": "sha256", "pricing": { "linear": { "base": 60, "word": 12 } } } },
"0000000000000000000000000000000000000003": { "balance": "1", "builtin": { "name": "ripemd160", "pricing": { "linear": { "base": 600, "word": 120 } } } },
"0000000000000000000000000000000000000004": { "balance": "1", "builtin": { "name": "identity", "pricing": { "linear": { "base": 15, "word": 3 } } } },
"0000000000000000000000000000000000000005": { "balance": "1", "builtin": { "name": "modexp", "activate_at": 0, "pricing": { "modexp": { "divisor": 20 } } } },
"0000000000000000000000000000000000000006": {
"balance": "1",
"builtin": {
"name": "alt_bn128_add",
"pricing": {
"0": {
"price": { "alt_bn128_const_operations": { "price": 500 }}
},
"0x7fffffffffffff": {
"info": "EIP 1108 transition",
"price": { "alt_bn128_const_operations": { "price": 150 }}
}
}
}
},
"0000000000000000000000000000000000000007": {
"balance": "1",
"builtin": {
"name": "alt_bn128_mul",
"pricing": {
"0": {
"price": { "alt_bn128_const_operations": { "price": 40000 }}
},
"0x7fffffffffffff": {
"info": "EIP 1108 transition",
"price": { "alt_bn128_const_operations": { "price": 6000 }}
}
}
}
},
"0000000000000000000000000000000000000008": {
"balance": "1",
"builtin": {
"name": "alt_bn128_pairing",
"pricing": {
"0": {
"price": { "alt_bn128_pairing": { "base": 100000, "pair": 80000 }}
},
"0x7fffffffffffff": {
"info": "EIP 1108 transition",
"price": { "alt_bn128_pairing": { "base": 45000, "pair": 34000 }}
}
}
}
},
"00a329c0648769a73afac7f9381e08fb43dbea72": { "balance": "1606938044258990275541962092341162602522202993782792835301376" }
}
}
`
// etc单元测试用这个
// 修改自:https://github.com/eth-classic/mordor/blob/master/parity.json
// 去除了nodes,engine改为instantSeal,networkID改为0x77,chainID改为0x4f,增加了默认有余额的地址
ParityMordorVariantChainJSON = `
{
"name":"Local Mordor Classic Testnet",
"dataDir":"mordor_private",
"engine":{
"instantSeal": {
"params": {
}
}
},
"params":{
"gasLimitBoundDivisor":"0x400",
"accountStartNonce":"0x0",
"maximumExtraDataSize":"0x20",
"minGasLimit":"0x1388",
"networkID":"0x77",
"chainID":"0x4f",
"eip150Transition":"0x0",
"eip160Transition":"0x0",
"eip161abcTransition":"0x0",
"eip161dTransition":"0x0",
"eip155Transition":"0x0",
"maxCodeSize":"0x6000",
"maxCodeSizeTransition":"0x0",
"eip140Transition":"0x0",
"eip211Transition":"0x0",
"eip214Transition":"0x0",
"eip658Transition":"0x0",
"eip145Transition": "0x498bb",
"eip1014Transition": "0x498bb",
"eip1052Transition": "0x498bb"
},
"genesis":{
"seal":{
"ethereum":{
"nonce":"0x0000000000000000",
"mixHash":"0x0000000000000000000000000000000000000000000000000000000000000000"
}
},
"difficulty":"0x20000",
"author":"0x0000000000000000000000000000000000000000",
"timestamp":"0x5d9676db",
"parentHash":"0x0000000000000000000000000000000000000000000000000000000000000000",
"extraData":"0x70686f656e697820636869636b656e206162737572642062616e616e61",
"gasLimit":"0x2fefd8"
},
"nodes":[
],
"accounts":{
"0x0000000000000000000000000000000000000001":{
"builtin":{
"name":"ecrecover",
"pricing":{
"linear":{
"base":3000,
"word":0
}
}
}
},
"0x0000000000000000000000000000000000000002":{
"builtin":{
"name":"sha256",
"pricing":{
"linear":{
"base":60,
"word":12
}
}
}
},
"0x0000000000000000000000000000000000000003":{
"builtin":{
"name":"ripemd160",
"pricing":{
"linear":{
"base":600,
"word":120
}
}
}
},
"0x0000000000000000000000000000000000000004":{
"builtin":{
"name":"identity",
"pricing":{
"linear":{
"base":15,
"word":3
}
}
}
},
"0x0000000000000000000000000000000000000005":{
"builtin":{
"activate_at":"0x0",
"name":"modexp",
"pricing":{
"modexp":{
"divisor":20
}
}
}
},
"0x0000000000000000000000000000000000000006":{
"builtin":{
"activate_at":"0x0",
"name":"alt_bn128_add",
"eip1108_transition": "0x7fffffffffffff",
"pricing":{
"alt_bn128_const_operations": {
"price": 500,
"eip1108_transition_price": 150
}
}
}
},
"0x0000000000000000000000000000000000000007":{
"builtin":{
"activate_at":"0x0",
"eip1108_transition": "0x7fffffffffffff",
"name":"alt_bn128_mul",
"pricing":{
"alt_bn128_const_operations": {
"price": 40000,
"eip1108_transition_price": 6000
}
}
}
},
"0x0000000000000000000000000000000000000008":{
"builtin":{
"activate_at":"0x0",
"eip1108_transition": "0x7fffffffffffff",
"name":"alt_bn128_pairing",
"pricing":{
"alt_bn128_pairing": {
"base": 100000,
"pair": 80000,
"eip1108_transition_base": 45000,
"eip1108_transition_pair": 34000
}
}
}
},
"00a329c0648769a73afac7f9381e08fb43dbea72": {"balance": "1606938044258990275541962092341162602522202993782792835301376"}
}
}
`
)