forked from status-im/nimbus-eth2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ncli.nim
232 lines (198 loc) · 7.37 KB
/
ncli.nim
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
import
std/[os, strutils, stats],
confutils, chronicles, json_serialization,
stew/[byteutils, io2],
snappy,
../research/simutils,
../beacon_chain/spec/eth2_apis/eth2_rest_serialization,
../beacon_chain/spec/datatypes/[phase0, altair, bellatrix],
../beacon_chain/spec/[
eth2_ssz_serialization, forks, helpers, state_transition],
../beacon_chain/networking/network_metadata
type
Cmd* = enum
hashTreeRoot = "Compute hash tree root of SSZ object"
pretty = "Pretty-print SSZ object"
transition = "Run state transition function"
slots = "Apply empty slots"
NcliConf* = object
eth2Network* {.
desc: "The Eth2 network preset to use"
name: "network" }: Option[string]
printTimes* {.
defaultValue: false # false to avoid polluting minimal output
name: "print-times"
desc: "Print timing information".}: bool
# TODO confutils argument pragma doesn't seem to do much; also, the cases
# are largely equivalent, but this helps create command line usage text
case cmd* {.command}: Cmd
of hashTreeRoot:
htrKind* {.
argument
desc: "kind of SSZ object: attester_slashing, attestation, signed_block, block, block_body, block_header, deposit, deposit_data, eth1_data, state, proposer_slashing, or voluntary_exit"}: string
htrFile* {.
argument
desc: "filename of SSZ or JSON-encoded object of which to compute hash tree root"}: string
of pretty:
prettyKind* {.
argument
desc: "kind of SSZ object: attester_slashing, attestation, signed_block, block, block_body, block_header, deposit, deposit_data, eth1_data, state, proposer_slashing, or voluntary_exit"}: string
prettyFile* {.
argument
desc: "filename of SSZ or JSON-encoded object to pretty-print"}: string
of transition:
preState* {.
argument
desc: "State to which to apply specified block"}: string
blck* {.
argument
desc: "Block to apply to preState"}: string
postState* {.
argument
desc: "Filename of state resulting from applying blck to preState"}: string
verifyStateRoot* {.
argument
desc: "Verify state root (default true)"
defaultValue: true}: bool
of slots:
preState2* {.
argument
desc: "State to which to apply specified block"}: string
slot* {.
argument
desc: "Block to apply to preState"}: uint64
postState2* {.
argument
desc: "Filename of state resulting from applying blck to preState"}: string
template saveSSZFile(filename: string, value: ForkedHashedBeaconState) =
case value.kind:
of BeaconStateFork.Phase0: SSZ.saveFile(filename, value.phase0Data.data)
of BeaconStateFork.Altair: SSZ.saveFile(filename, value.altairData.data)
of BeaconStateFork.Bellatrix: SSZ.saveFile(filename, value.bellatrixData.data)
proc loadFile(filename: string, T: type): T =
let
ext = splitFile(filename).ext
bytes = readAllBytes(filename).expect("file exists")
if cmpIgnoreCase(ext, ".ssz") == 0:
SSZ.decode(bytes, T)
elif cmpIgnoreCase(ext, ".ssz_snappy") == 0:
SSZ.decode(snappy.decode(bytes), T)
elif cmpIgnoreCase(ext, ".json") == 0:
# JSON.loadFile(file, t)
echo "TODO needs porting to RestJson"
quit 1
else:
echo "Unknown file type: ", ext
quit 1
proc doTransition(conf: NcliConf) =
type
Timers = enum
tLoadState = "Load state from file"
tTransition = "Apply slot"
tSaveState = "Save state to file"
var timers: array[Timers, RunningStat]
let
cfg = getRuntimeConfig(conf.eth2Network)
stateY = withTimerRet(timers[tLoadState]):
newClone(readSszForkedHashedBeaconState(
cfg, readAllBytes(conf.preState).tryGet()))
blckX = readSszForkedSignedBeaconBlock(
cfg, readAllBytes(conf.blck).tryGet())
flags = if not conf.verifyStateRoot: {skipStateRootValidation} else: {}
var
cache = StateCache()
info = ForkedEpochInfo()
let res = withTimerRet(timers[tTransition]): withBlck(blckX):
state_transition(
cfg, stateY[], blck, cache, info, flags, noRollback)
if res.isErr():
error "State transition failed", error = res.error()
quit 1
else:
withTimer(timers[tSaveState]):
saveSSZFile(conf.postState, stateY[])
if conf.printTimes:
printTimers(false, timers)
proc doSlots(conf: NcliConf) =
type
Timers = enum
tLoadState = "Load state from file"
tApplySlot = "Apply slot"
tApplyEpochSlot = "Apply epoch slot"
tSaveState = "Save state to file"
var timers: array[Timers, RunningStat]
let
cfg = getRuntimeConfig(conf.eth2Network)
stateY = withTimerRet(timers[tLoadState]):
newClone(readSszForkedHashedBeaconState(
cfg, readAllBytes(conf.preState2).tryGet()))
var
cache = StateCache()
info = ForkedEpochInfo()
for i in 0'u64..<conf.slot:
let isEpoch = (getStateField(stateY[], slot) + 1).is_epoch
withTimer(timers[if isEpoch: tApplyEpochSlot else: tApplySlot]):
process_slots(
cfg, stateY[], getStateField(stateY[], slot) + 1,
cache, info, {}).expect("should be able to advance slot")
withTimer(timers[tSaveState]):
saveSSZFile(conf.postState2, stateY[])
if conf.printTimes:
printTimers(false, timers)
proc doSSZ(conf: NcliConf) =
type Timers = enum
tLoad = "Load file"
tCompute = "Compute"
var timers: array[Timers, RunningStat]
let (kind, file) =
case conf.cmd:
of hashTreeRoot: (conf.htrKind, conf.htrFile)
of pretty: (conf.prettyKind, conf.prettyFile)
else:
raiseAssert "doSSZ() only implements hashTreeRoot and pretty commands"
template printit(t: untyped) {.dirty.} =
let v = withTimerRet(timers[tLoad]):
newClone(loadFile(file, t))
case conf.cmd:
of hashTreeRoot:
let root = withTimerRet(timers[tCompute]):
when t is ForkySignedBeaconBlock:
hash_tree_root(v[].message)
else:
hash_tree_root(v[])
echo root.data.toHex()
of pretty:
echo RestJson.encode(v[], pretty = true)
else:
raiseAssert "doSSZ() only implements hashTreeRoot and pretty commands"
if conf.printTimes:
printTimers(false, timers)
case kind
of "attester_slashing": printit(AttesterSlashing)
of "attestation": printit(Attestation)
of "phase0_signed_block": printit(phase0.SignedBeaconBlock)
of "altair_signed_block": printit(altair.SignedBeaconBlock)
of "bellatrix_signed_block": printit(bellatrix.SignedBeaconBlock)
of "phase0_block": printit(phase0.BeaconBlock)
of "altair_block": printit(altair.BeaconBlock)
of "bellatrix_block": printit(bellatrix.BeaconBlock)
of "phase0_block_body": printit(phase0.BeaconBlockBody)
of "altair_block_body": printit(altair.BeaconBlockBody)
of "bellatrix_block_body": printit(bellatrix.BeaconBlockBody)
of "block_header": printit(BeaconBlockHeader)
of "deposit": printit(Deposit)
of "deposit_data": printit(DepositData)
of "eth1_data": printit(Eth1Data)
of "phase0_state": printit(phase0.BeaconState)
of "altair_state": printit(altair.BeaconState)
of "bellatrix_state": printit(bellatrix.BeaconState)
of "proposer_slashing": printit(ProposerSlashing)
of "voluntary_exit": printit(VoluntaryExit)
when isMainModule:
let
conf = NcliConf.load()
case conf.cmd:
of hashTreeRoot: doSSZ(conf)
of pretty: doSSZ(conf)
of transition: doTransition(conf)
of slots: doSlots(conf)