|
10 | 10 | import |
11 | 11 | chronicles, |
12 | 12 | chronos, |
| 13 | + eth/common/keys, # used for keys.rng |
13 | 14 | beacon_chain/gossip_processing/light_client_processor, |
14 | | - beacon_chain/beacon_clock, |
| 15 | + beacon_chain/[beacon_clock, conf], |
15 | 16 | ./lc_manager # use the modified light client manager |
16 | 17 |
|
17 | 18 | type |
|
20 | 21 | ) {.gcsafe, raises: [].} |
21 | 22 |
|
22 | 23 | LightClient* = ref object |
23 | | - cfg: RuntimeConfig |
24 | | - forkDigests: ref ForkDigests |
| 24 | + cfg*: RuntimeConfig |
| 25 | + forkDigests*: ref ForkDigests |
25 | 26 | getBeaconTime*: GetBeaconTimeFn |
26 | 27 | store*: ref ForkedLightClientStore |
27 | 28 | processor*: ref LightClientProcessor |
@@ -144,16 +145,59 @@ proc new*( |
144 | 145 |
|
145 | 146 | lightClient |
146 | 147 |
|
| 148 | +proc new*( |
| 149 | + T: type LightClient, chain: Option[string], trustedBlockRoot: Option[Eth2Digest] |
| 150 | +): T = |
| 151 | + let metadata = loadEth2Network(chain) |
| 152 | + |
| 153 | + # just for short hand convenience |
| 154 | + template cfg(): auto = |
| 155 | + metadata.cfg |
| 156 | + |
| 157 | + # initialize beacon node genesis data, beacon clock and forkDigests |
| 158 | + let |
| 159 | + genesisState = |
| 160 | + try: |
| 161 | + template genesisData(): auto = |
| 162 | + metadata.genesis.bakedBytes |
| 163 | + |
| 164 | + newClone( |
| 165 | + readSszForkedHashedBeaconState( |
| 166 | + cfg, genesisData.toOpenArray(genesisData.low, genesisData.high) |
| 167 | + ) |
| 168 | + ) |
| 169 | + except CatchableError as err: |
| 170 | + raiseAssert "Invalid baked-in state: " & err.msg |
| 171 | + |
| 172 | + # getStateField reads seeks info directly from a byte array |
| 173 | + # get genesis time and instantiate the beacon clock |
| 174 | + genesisTime = getStateField(genesisState[], genesis_time) |
| 175 | + beaconClock = BeaconClock.init(cfg.timeParams, genesisTime).valueOr: |
| 176 | + error "Invalid genesis time in state", genesisTime |
| 177 | + quit QuitFailure |
| 178 | + |
| 179 | + # get the function that itself get the current beacon time |
| 180 | + getBeaconTime = beaconClock.getBeaconTimeFn() |
| 181 | + genesis_validators_root = getStateField(genesisState[], genesis_validators_root) |
| 182 | + forkDigests = newClone ForkDigests.init(cfg, genesis_validators_root) |
| 183 | + |
| 184 | + rng = keys.newRng() |
| 185 | + |
| 186 | + # light client is set to optimistic finalization mode |
| 187 | + lightClient = LightClient.new( |
| 188 | + rng, cfg, forkDigests, getBeaconTime, genesis_validators_root, |
| 189 | + LightClientFinalizationMode.Optimistic, |
| 190 | + ) |
| 191 | + |
| 192 | + lightClient.trustedBlockRoot = trustedBlockRoot |
| 193 | + lightClient |
| 194 | + |
147 | 195 | proc setBackend*(lightClient: LightClient, backend: EthLCBackend) = |
148 | 196 | lightClient.manager.backend = backend |
149 | 197 |
|
150 | | -proc start*(lightClient: LightClient) = |
| 198 | +proc start*(lightClient: LightClient) {.async: (raises: [CancelledError]).} = |
151 | 199 | info "Starting beacon light client", trusted_block_root = lightClient.trustedBlockRoot |
152 | | - lightClient.manager.start() |
153 | | - |
154 | | -proc stop*(lightClient: LightClient) {.async: (raises: []).} = |
155 | | - info "Stopping beacon light client" |
156 | | - await lightClient.manager.stop() |
| 200 | + await lightClient.manager.start() |
157 | 201 |
|
158 | 202 | proc resetToFinalizedHeader*( |
159 | 203 | lightClient: LightClient, |
|
0 commit comments