2022-06-30 13:04:39 +00:00
|
|
|
# beacon_chain
|
2024-01-06 14:26:56 +00:00
|
|
|
# Copyright (c) 2022-2024 Status Research & Development GmbH
|
2022-06-30 13:04:39 +00:00
|
|
|
# Licensed and distributed under either of
|
|
|
|
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
2023-01-20 14:14:37 +00:00
|
|
|
{.push raises: [].}
|
2022-07-29 10:53:42 +00:00
|
|
|
|
2022-06-30 13:04:39 +00:00
|
|
|
import
|
|
|
|
# Status libraries
|
2023-01-12 17:11:38 +00:00
|
|
|
stew/base10,
|
2022-06-30 13:04:39 +00:00
|
|
|
chronicles,
|
|
|
|
eth/db/kvstore_sqlite3,
|
|
|
|
# Beacon chain internals
|
|
|
|
spec/datatypes/altair,
|
2022-11-30 03:45:03 +00:00
|
|
|
spec/[eth2_ssz_serialization, helpers],
|
|
|
|
./db_limits
|
2022-06-30 13:04:39 +00:00
|
|
|
|
|
|
|
logScope: topics = "lcdata"
|
|
|
|
|
2023-01-27 09:44:57 +00:00
|
|
|
# `lc_xxxxx_headers` contains a copy of historic `LightClientHeader`.
|
|
|
|
# Data is only kept for blocks that are used in `LightClientBootstrap` objects.
|
|
|
|
# Caching is necessary to support longer retention for LC data than state data.
|
|
|
|
# SSZ because this data does not compress well, and because this data
|
|
|
|
# needs to be bundled together with other data to fulfill requests.
|
|
|
|
# Mainnet data size (all columns):
|
|
|
|
# - Altair: ~38 KB per `SyncCommitteePeriod` (~1.0 MB per month)
|
2024-06-21 12:39:38 +00:00
|
|
|
# - Capella: ~221 KB per `SyncCommitteePeriod` (~6.0 MB per month)
|
|
|
|
# - Deneb: ~225 KB per `SyncCommitteePeriod` (~6.2 MB per month)
|
2024-06-21 13:02:01 +00:00
|
|
|
# - Electra: ~249 KB per `SyncCommitteePeriod` (~6.8 MB per month)
|
2023-01-27 09:44:57 +00:00
|
|
|
#
|
2024-06-21 13:02:01 +00:00
|
|
|
# `lc_xxxxx_current_branches` holds Merkle proofs needed to
|
2023-01-12 17:11:38 +00:00
|
|
|
# construct `LightClientBootstrap` objects.
|
2022-06-30 13:04:39 +00:00
|
|
|
# SSZ because this data does not compress well, and because this data
|
|
|
|
# needs to be bundled together with other data to fulfill requests.
|
2023-01-27 09:44:57 +00:00
|
|
|
# Mainnet data size (all columns):
|
2023-03-10 17:16:26 +00:00
|
|
|
# - Altair ... Deneb: ~42 KB per `SyncCommitteePeriod` (~1.1 MB per month)
|
2024-06-21 13:02:01 +00:00
|
|
|
# - Electra: ~50 KB per `SyncCommitteePeriod` (~1.4 MB per month)
|
2023-01-27 09:44:57 +00:00
|
|
|
#
|
|
|
|
# `lc_altair_sync_committees` contains a copy of finalized sync committees.
|
|
|
|
# They are initially populated from the main DAG (usually a fast state access).
|
|
|
|
# Caching is necessary to support longer retention for LC data than state data.
|
|
|
|
# SSZ because this data does not compress well, and because this data
|
|
|
|
# needs to be bundled together with other data to fulfill requests.
|
|
|
|
# Mainnet data size (all columns):
|
2024-06-21 13:02:01 +00:00
|
|
|
# - Altair ... Electra: ~24 KB per `SyncCommitteePeriod` (~0.7 MB per month)
|
2022-06-30 13:04:39 +00:00
|
|
|
#
|
2023-01-12 17:11:38 +00:00
|
|
|
# `lc_best_updates` holds full `LightClientUpdate` objects in SSZ form.
|
|
|
|
# These objects are frequently queried in bulk, but there is only one per
|
2022-06-30 13:04:39 +00:00
|
|
|
# sync committee period, so storing the full sync committee is acceptable.
|
|
|
|
# This data could be stored as SZSSZ to avoid on-the-fly compression when a
|
|
|
|
# libp2p request is handled. However, the space savings are quite small.
|
|
|
|
# Furthermore, `LightClientUpdate` is consulted on each new block to attempt
|
|
|
|
# improving it. Continuously decompressing and recompressing seems inefficient.
|
2023-01-13 15:46:35 +00:00
|
|
|
# Finally, the libp2p context bytes depend on `attested_header.beacon.slot` for
|
|
|
|
# deriving the fork digest; the `kind` column is not sufficient to derive
|
2023-01-12 17:11:38 +00:00
|
|
|
# the fork digest, because the same storage format may be used across forks.
|
2022-06-30 13:04:39 +00:00
|
|
|
# SSZ storage selected due to the small size and reduced logic complexity.
|
2023-01-27 09:44:57 +00:00
|
|
|
# Mainnet data size (all columns):
|
2024-06-21 12:39:38 +00:00
|
|
|
# - Altair: ~25 KB per `SyncCommitteePeriod` (~0.7 MB per month)
|
|
|
|
# - Capella: ~26 KB per `SyncCommitteePeriod` (~0.7 MB per month)
|
|
|
|
# - Deneb: ~26 KB per `SyncCommitteePeriod` (~0.7 MB per month)
|
2024-06-21 13:02:01 +00:00
|
|
|
# - Electra: ~27 KB per `SyncCommitteePeriod` (~0.7 MB per month)
|
2022-06-30 13:04:39 +00:00
|
|
|
#
|
2023-01-12 17:11:38 +00:00
|
|
|
# `lc_sealed_periods` contains the sync committee periods for which
|
2022-06-30 13:04:39 +00:00
|
|
|
# full light client data was imported. Data for these periods may no longer
|
|
|
|
# improve regardless of further block processing. The listed periods are skipped
|
|
|
|
# when restarting the program.
|
2023-01-27 09:44:57 +00:00
|
|
|
# Mainnet data size (all columns):
|
|
|
|
# - All forks: 8 bytes per `SyncCommitteePeriod` (~0.0 MB per month)
|
2024-06-21 12:39:38 +00:00
|
|
|
#
|
|
|
|
# Header computations:
|
|
|
|
# - Altair: 256*(112+40)/1024*28/1024
|
|
|
|
# - Capella: 256*(112+4+600+128+40)/1024*28/1024
|
|
|
|
# 600 = 32+20+32+32+256+32+8+8+8+8+4+32+32+32+32+32
|
|
|
|
# - Deneb: 256*(112+4+616+128+40)/1024*28/1024
|
|
|
|
# 616 = 32+20+32+32+256+32+8+8+8+8+4+32+32+32+32+32+8+8
|
2024-06-21 13:02:01 +00:00
|
|
|
# - Electra: 256*(112+4+712+128+40)/1024*28/1024
|
|
|
|
# 712 = 32+20+32+32+256+32+8+8+8+8+4+32+32+32+32+32+8+8+32+32+32
|
2024-06-21 12:39:38 +00:00
|
|
|
#
|
|
|
|
# Committee branch computations:
|
|
|
|
# - Altair: 256*(5*32+8)/1024*28/1024
|
2024-06-21 13:02:01 +00:00
|
|
|
# - Electra: 256*(6*32+8)/1024*28/1024
|
2024-06-21 12:39:38 +00:00
|
|
|
#
|
|
|
|
# Finality branch computations:
|
|
|
|
# - Altair: 256*(6*32+8)/1024*28/1024
|
2024-06-21 13:02:01 +00:00
|
|
|
# - Electra: 256*(7*32+8)/1024*28/1024
|
2024-06-21 12:39:38 +00:00
|
|
|
#
|
|
|
|
# Committee computations:
|
|
|
|
# - Altair: (24624+8)/1024*28/1024
|
|
|
|
# 513*48 = 24624
|
|
|
|
#
|
|
|
|
# Aggregate computations:
|
|
|
|
# - Altair: 112 = 512/8+48
|
|
|
|
#
|
|
|
|
# Update computations:
|
|
|
|
# - Altair: (112+24624+5*32+112+6*32+112+8+9)/1024*28/1024
|
|
|
|
# - Capella: (4+884+24624+5*32+4+884+6*32+112+8+9)/1024*28/1024
|
|
|
|
# - Deneb: (4+900+24624+5*32+4+900+6*32+112+8+9)/1024*28/1024
|
2024-06-21 13:02:01 +00:00
|
|
|
# - Electra: (4+996+24624+6*32+4+996+7*32+112+8+9)/1024*28/1024
|
2022-06-30 13:04:39 +00:00
|
|
|
|
|
|
|
type
|
2023-01-27 09:44:57 +00:00
|
|
|
LightClientHeaderStore = object
|
|
|
|
getStmt: SqliteStmt[array[32, byte], seq[byte]]
|
|
|
|
putStmt: SqliteStmt[(array[32, byte], int64, seq[byte]), void]
|
|
|
|
keepFromStmt: SqliteStmt[int64, void]
|
|
|
|
|
2024-06-21 13:02:01 +00:00
|
|
|
BranchFork {.pure.} = enum
|
|
|
|
None = 0,
|
|
|
|
Altair,
|
|
|
|
Electra
|
|
|
|
|
2022-06-30 13:04:39 +00:00
|
|
|
CurrentSyncCommitteeBranchStore = object
|
|
|
|
containsStmt: SqliteStmt[int64, int64]
|
|
|
|
getStmt: SqliteStmt[int64, seq[byte]]
|
|
|
|
putStmt: SqliteStmt[(int64, seq[byte]), void]
|
|
|
|
keepFromStmt: SqliteStmt[int64, void]
|
|
|
|
|
2023-01-27 09:44:57 +00:00
|
|
|
SyncCommitteeStore = object
|
|
|
|
containsStmt: SqliteStmt[int64, int64]
|
|
|
|
getStmt: SqliteStmt[int64, seq[byte]]
|
|
|
|
putStmt: SqliteStmt[(int64, seq[byte]), void]
|
|
|
|
keepFromStmt: SqliteStmt[int64, void]
|
|
|
|
|
2023-01-16 15:53:45 +00:00
|
|
|
LegacyBestLightClientUpdateStore = object
|
2023-01-18 02:06:23 +00:00
|
|
|
getStmt: SqliteStmt[int64, (int64, seq[byte])]
|
2023-01-16 15:53:45 +00:00
|
|
|
putStmt: SqliteStmt[(int64, seq[byte]), void]
|
|
|
|
delStmt: SqliteStmt[int64, void]
|
|
|
|
keepFromStmt: SqliteStmt[int64, void]
|
|
|
|
|
2022-06-30 13:04:39 +00:00
|
|
|
BestLightClientUpdateStore = object
|
2023-01-12 17:11:38 +00:00
|
|
|
getStmt: SqliteStmt[int64, (int64, seq[byte])]
|
|
|
|
putStmt: SqliteStmt[(int64, int64, seq[byte]), void]
|
2022-06-30 13:04:39 +00:00
|
|
|
delStmt: SqliteStmt[int64, void]
|
|
|
|
keepFromStmt: SqliteStmt[int64, void]
|
|
|
|
|
|
|
|
SealedSyncCommitteePeriodStore = object
|
|
|
|
containsStmt: SqliteStmt[int64, int64]
|
|
|
|
putStmt: SqliteStmt[int64, void]
|
|
|
|
keepFromStmt: SqliteStmt[int64, void]
|
|
|
|
|
2022-07-04 20:46:32 +00:00
|
|
|
LightClientDataDB* = ref object
|
2022-06-30 13:04:39 +00:00
|
|
|
backend: SqStoreRef
|
|
|
|
## SQLite backend
|
|
|
|
|
2023-01-27 09:44:57 +00:00
|
|
|
headers: array[LightClientDataFork, LightClientHeaderStore]
|
|
|
|
## Eth2Digest -> (Slot, LightClientHeader)
|
|
|
|
## Cached block headers to support longer retention than block storage.
|
|
|
|
|
2024-06-21 13:02:01 +00:00
|
|
|
currentBranches: array[BranchFork, CurrentSyncCommitteeBranchStore]
|
|
|
|
## Slot -> CurrentSyncCommitteeBranch
|
2022-06-30 13:04:39 +00:00
|
|
|
## Cached data for creating future `LightClientBootstrap` instances.
|
|
|
|
## Key is the block slot of which the post state was used to get the data.
|
|
|
|
## Data stored for all finalized epoch boundary blocks.
|
|
|
|
|
2023-01-27 09:44:57 +00:00
|
|
|
syncCommittees: SyncCommitteeStore
|
|
|
|
## SyncCommitteePeriod -> altair.SyncCommittee
|
|
|
|
## Cached sync committees to support longer retention than state storage.
|
|
|
|
|
2023-01-16 15:53:45 +00:00
|
|
|
legacyBestUpdates: LegacyBestLightClientUpdateStore
|
|
|
|
## SyncCommitteePeriod -> altair.LightClientUpdate
|
|
|
|
## Used through Bellatrix.
|
|
|
|
|
2022-06-30 13:04:39 +00:00
|
|
|
bestUpdates: BestLightClientUpdateStore
|
2023-01-12 17:11:38 +00:00
|
|
|
## SyncCommitteePeriod -> (LightClientDataFork, LightClientUpdate)
|
2022-06-30 13:04:39 +00:00
|
|
|
## Stores the `LightClientUpdate` with the most `sync_committee_bits` per
|
|
|
|
## `SyncCommitteePeriod`. Sync committee finality gives precedence.
|
|
|
|
|
|
|
|
sealedPeriods: SealedSyncCommitteePeriodStore
|
|
|
|
## {SyncCommitteePeriod}
|
|
|
|
## Tracks the finalized sync committee periods for which complete data
|
|
|
|
## has been imported (from `dag.tail.slot`).
|
|
|
|
|
2023-01-18 02:06:23 +00:00
|
|
|
template disposeSafe(s: untyped): untyped =
|
|
|
|
if distinctBase(s) != nil:
|
|
|
|
s.dispose()
|
2023-07-12 17:33:19 +00:00
|
|
|
s = typeof(s)(nil)
|
2023-01-18 02:06:23 +00:00
|
|
|
|
2023-01-27 09:44:57 +00:00
|
|
|
proc initHeadersStore(
|
|
|
|
backend: SqStoreRef,
|
|
|
|
name, typeName: string): KvResult[LightClientHeaderStore] =
|
|
|
|
if name == "":
|
|
|
|
return ok LightClientHeaderStore()
|
|
|
|
if not backend.readOnly:
|
|
|
|
? backend.exec("""
|
|
|
|
CREATE TABLE IF NOT EXISTS `""" & name & """` (
|
|
|
|
`block_root` BLOB PRIMARY KEY, -- `Eth2Digest`
|
|
|
|
`slot` INTEGER, -- `Slot`
|
|
|
|
`header` BLOB -- `""" & typeName & """` (SSZ)
|
|
|
|
);
|
|
|
|
""")
|
|
|
|
if not ? backend.hasTable(name):
|
|
|
|
return ok LightClientHeaderStore()
|
|
|
|
|
|
|
|
let
|
|
|
|
getStmt = backend.prepareStmt("""
|
|
|
|
SELECT `header`
|
|
|
|
FROM `""" & name & """`
|
|
|
|
WHERE `block_root` = ?;
|
|
|
|
""", array[32, byte], seq[byte], managed = false).expect("SQL query OK")
|
|
|
|
putStmt = backend.prepareStmt("""
|
|
|
|
REPLACE INTO `""" & name & """` (
|
|
|
|
`block_root`, `slot`, `header`
|
|
|
|
) VALUES (?, ?, ?);
|
|
|
|
""", (array[32, byte], int64, seq[byte]), void, managed = false)
|
|
|
|
.expect("SQL query OK")
|
|
|
|
keepFromStmt = backend.prepareStmt("""
|
|
|
|
DELETE FROM `""" & name & """`
|
|
|
|
WHERE `slot` < ?;
|
|
|
|
""", int64, void, managed = false).expect("SQL query OK")
|
|
|
|
|
|
|
|
ok LightClientHeaderStore(
|
|
|
|
getStmt: getStmt,
|
|
|
|
putStmt: putStmt,
|
|
|
|
keepFromStmt: keepFromStmt)
|
|
|
|
|
|
|
|
func close(store: var LightClientHeaderStore) =
|
|
|
|
store.getStmt.disposeSafe()
|
|
|
|
store.putStmt.disposeSafe()
|
|
|
|
store.keepFromStmt.disposeSafe()
|
|
|
|
|
|
|
|
proc getHeader*[T: ForkyLightClientHeader](
|
|
|
|
db: LightClientDataDB, blockRoot: Eth2Digest): Opt[T] =
|
|
|
|
if distinctBase(db.headers[T.kind].getStmt) == nil:
|
|
|
|
return Opt.none(T)
|
|
|
|
var header: seq[byte]
|
|
|
|
for res in db.headers[T.kind].getStmt.exec(blockRoot.data, header):
|
|
|
|
res.expect("SQL query OK")
|
|
|
|
try:
|
|
|
|
return ok SSZ.decode(header, T)
|
2023-06-15 16:29:13 +00:00
|
|
|
except SerializationError as exc:
|
2023-01-27 09:44:57 +00:00
|
|
|
error "LC data store corrupted", store = "headers", kind = T.kind,
|
|
|
|
blockRoot, exc = exc.msg
|
|
|
|
return Opt.none(T)
|
|
|
|
|
|
|
|
func putHeader*[T: ForkyLightClientHeader](
|
|
|
|
db: LightClientDataDB, header: T) =
|
|
|
|
doAssert not db.backend.readOnly and
|
|
|
|
distinctBase(db.headers[T.kind].putStmt) != nil
|
|
|
|
let
|
|
|
|
blockRoot = hash_tree_root(header.beacon)
|
|
|
|
slot = header.beacon.slot
|
|
|
|
res = db.headers[T.kind].putStmt.exec(
|
|
|
|
(blockRoot.data, slot.int64, SSZ.encode(header)))
|
|
|
|
res.expect("SQL query OK")
|
|
|
|
|
2022-07-04 20:46:32 +00:00
|
|
|
proc initCurrentBranchesStore(
|
|
|
|
backend: SqStoreRef,
|
2024-06-21 13:02:01 +00:00
|
|
|
name, typeName: string): KvResult[CurrentSyncCommitteeBranchStore] =
|
|
|
|
if name == "":
|
|
|
|
return ok CurrentSyncCommitteeBranchStore()
|
2023-01-24 02:52:08 +00:00
|
|
|
if not backend.readOnly:
|
|
|
|
? backend.exec("""
|
|
|
|
CREATE TABLE IF NOT EXISTS `""" & name & """` (
|
|
|
|
`slot` INTEGER PRIMARY KEY, -- `Slot` (up through 2^63-1)
|
2024-06-21 13:02:01 +00:00
|
|
|
`branch` BLOB -- `""" & typeName & """` (SSZ)
|
2023-01-24 02:52:08 +00:00
|
|
|
);
|
|
|
|
""")
|
|
|
|
if not ? backend.hasTable(name):
|
2023-01-18 02:06:23 +00:00
|
|
|
return ok CurrentSyncCommitteeBranchStore()
|
|
|
|
|
2022-06-30 13:04:39 +00:00
|
|
|
let
|
2022-07-04 20:46:32 +00:00
|
|
|
containsStmt = backend.prepareStmt("""
|
2022-06-30 13:04:39 +00:00
|
|
|
SELECT 1 AS `exists`
|
2022-07-04 20:46:32 +00:00
|
|
|
FROM `""" & name & """`
|
2022-06-30 13:04:39 +00:00
|
|
|
WHERE `slot` = ?;
|
2022-07-04 20:46:32 +00:00
|
|
|
""", int64, int64, managed = false).expect("SQL query OK")
|
|
|
|
getStmt = backend.prepareStmt("""
|
2022-06-30 13:04:39 +00:00
|
|
|
SELECT `branch`
|
2022-07-04 20:46:32 +00:00
|
|
|
FROM `""" & name & """`
|
2022-06-30 13:04:39 +00:00
|
|
|
WHERE `slot` = ?;
|
2022-07-04 20:46:32 +00:00
|
|
|
""", int64, seq[byte], managed = false).expect("SQL query OK")
|
|
|
|
putStmt = backend.prepareStmt("""
|
2023-01-27 09:44:57 +00:00
|
|
|
REPLACE INTO `""" & name & """` (
|
2022-06-30 13:04:39 +00:00
|
|
|
`slot`, `branch`
|
|
|
|
) VALUES (?, ?);
|
2022-07-04 20:46:32 +00:00
|
|
|
""", (int64, seq[byte]), void, managed = false).expect("SQL query OK")
|
|
|
|
keepFromStmt = backend.prepareStmt("""
|
|
|
|
DELETE FROM `""" & name & """`
|
2022-06-30 13:04:39 +00:00
|
|
|
WHERE `slot` < ?;
|
2022-07-04 20:46:32 +00:00
|
|
|
""", int64, void, managed = false).expect("SQL query OK")
|
2022-06-30 13:04:39 +00:00
|
|
|
|
|
|
|
ok CurrentSyncCommitteeBranchStore(
|
|
|
|
containsStmt: containsStmt,
|
|
|
|
getStmt: getStmt,
|
|
|
|
putStmt: putStmt,
|
|
|
|
keepFromStmt: keepFromStmt)
|
|
|
|
|
2023-01-18 02:06:23 +00:00
|
|
|
func close(store: var CurrentSyncCommitteeBranchStore) =
|
|
|
|
store.containsStmt.disposeSafe()
|
|
|
|
store.getStmt.disposeSafe()
|
|
|
|
store.putStmt.disposeSafe()
|
|
|
|
store.keepFromStmt.disposeSafe()
|
2022-07-04 20:46:32 +00:00
|
|
|
|
2024-06-21 13:02:01 +00:00
|
|
|
template kind(x: typedesc[altair.CurrentSyncCommitteeBranch]): BranchFork =
|
|
|
|
BranchFork.Altair
|
|
|
|
|
|
|
|
template kind(x: typedesc[electra.CurrentSyncCommitteeBranch]): BranchFork =
|
|
|
|
BranchFork.Electra
|
|
|
|
|
|
|
|
func hasCurrentSyncCommitteeBranch*[T: ForkyCurrentSyncCommitteeBranch](
|
2022-06-30 13:04:39 +00:00
|
|
|
db: LightClientDataDB, slot: Slot): bool =
|
2023-01-18 02:06:23 +00:00
|
|
|
if not slot.isSupportedBySQLite or
|
2024-06-21 13:02:01 +00:00
|
|
|
distinctBase(db.currentBranches[T.kind].containsStmt) == nil:
|
2022-06-30 13:04:39 +00:00
|
|
|
return false
|
|
|
|
var exists: int64
|
2024-06-21 13:02:01 +00:00
|
|
|
for res in db.currentBranches[T.kind].containsStmt.exec(slot.int64, exists):
|
2022-06-30 13:04:39 +00:00
|
|
|
res.expect("SQL query OK")
|
|
|
|
doAssert exists == 1
|
|
|
|
return true
|
|
|
|
false
|
|
|
|
|
2024-06-21 13:02:01 +00:00
|
|
|
proc getCurrentSyncCommitteeBranch*[T: ForkyCurrentSyncCommitteeBranch](
|
|
|
|
db: LightClientDataDB, slot: Slot): Opt[T] =
|
2023-01-18 02:06:23 +00:00
|
|
|
if not slot.isSupportedBySQLite or
|
2024-06-21 13:02:01 +00:00
|
|
|
distinctBase(db.currentBranches[T.kind].getStmt) == nil:
|
|
|
|
return Opt.none(T)
|
2022-06-30 13:04:39 +00:00
|
|
|
var branch: seq[byte]
|
2024-06-21 13:02:01 +00:00
|
|
|
for res in db.currentBranches[T.kind].getStmt.exec(slot.int64, branch):
|
2022-06-30 13:04:39 +00:00
|
|
|
res.expect("SQL query OK")
|
|
|
|
try:
|
2024-06-21 13:02:01 +00:00
|
|
|
return ok SSZ.decode(branch, T)
|
2023-06-15 16:29:13 +00:00
|
|
|
except SerializationError as exc:
|
2024-06-21 13:02:01 +00:00
|
|
|
error "LC data store corrupted", store = "currentBranches", kind = T.kind,
|
2022-11-30 03:45:03 +00:00
|
|
|
slot, exc = exc.msg
|
2024-06-21 13:02:01 +00:00
|
|
|
return Opt.none(T)
|
2022-06-30 13:04:39 +00:00
|
|
|
|
2024-06-21 13:02:01 +00:00
|
|
|
func putCurrentSyncCommitteeBranch*[T: ForkyCurrentSyncCommitteeBranch](
|
|
|
|
db: LightClientDataDB, slot: Slot, branch: T) =
|
2023-01-18 02:06:23 +00:00
|
|
|
doAssert not db.backend.readOnly # All `stmt` are non-nil
|
2024-06-22 08:19:35 +00:00
|
|
|
if not slot.isSupportedBySQLite:
|
2022-06-30 13:04:39 +00:00
|
|
|
return
|
2024-06-21 13:02:01 +00:00
|
|
|
let res = db.currentBranches[T.kind].putStmt.exec(
|
|
|
|
(slot.int64, SSZ.encode(branch)))
|
2022-06-30 13:04:39 +00:00
|
|
|
res.expect("SQL query OK")
|
|
|
|
|
2023-01-27 09:44:57 +00:00
|
|
|
proc initSyncCommitteesStore(
|
|
|
|
backend: SqStoreRef,
|
|
|
|
name: string): KvResult[SyncCommitteeStore] =
|
|
|
|
if not backend.readOnly:
|
|
|
|
? backend.exec("""
|
|
|
|
CREATE TABLE IF NOT EXISTS `""" & name & """` (
|
|
|
|
`period` INTEGER PRIMARY KEY, -- `SyncCommitteePeriod`
|
|
|
|
`sync_committee` BLOB -- `altair.SyncCommittee` (SSZ)
|
|
|
|
);
|
|
|
|
""")
|
|
|
|
if not ? backend.hasTable(name):
|
|
|
|
return ok SyncCommitteeStore()
|
|
|
|
|
|
|
|
let
|
|
|
|
containsStmt = backend.prepareStmt("""
|
|
|
|
SELECT 1 AS `exists`
|
|
|
|
FROM `""" & name & """`
|
|
|
|
WHERE `period` = ?;
|
|
|
|
""", int64, int64, managed = false).expect("SQL query OK")
|
|
|
|
getStmt = backend.prepareStmt("""
|
|
|
|
SELECT `sync_committee`
|
|
|
|
FROM `""" & name & """`
|
|
|
|
WHERE `period` = ?;
|
|
|
|
""", int64, seq[byte], managed = false).expect("SQL query OK")
|
|
|
|
putStmt = backend.prepareStmt("""
|
|
|
|
REPLACE INTO `""" & name & """` (
|
|
|
|
`period`, `sync_committee`
|
|
|
|
) VALUES (?, ?);
|
|
|
|
""", (int64, seq[byte]), void, managed = false).expect("SQL query OK")
|
|
|
|
keepFromStmt = backend.prepareStmt("""
|
|
|
|
DELETE FROM `""" & name & """`
|
|
|
|
WHERE `period` < ?;
|
|
|
|
""", int64, void, managed = false).expect("SQL query OK")
|
|
|
|
|
|
|
|
ok SyncCommitteeStore(
|
|
|
|
containsStmt: containsStmt,
|
|
|
|
getStmt: getStmt,
|
|
|
|
putStmt: putStmt,
|
|
|
|
keepFromStmt: keepFromStmt)
|
|
|
|
|
|
|
|
func close(store: var SyncCommitteeStore) =
|
|
|
|
store.containsStmt.disposeSafe()
|
|
|
|
store.getStmt.disposeSafe()
|
|
|
|
store.putStmt.disposeSafe()
|
|
|
|
store.keepFromStmt.disposeSafe()
|
|
|
|
|
|
|
|
func hasSyncCommittee*(
|
|
|
|
db: LightClientDataDB, period: SyncCommitteePeriod): bool =
|
|
|
|
doAssert period.isSupportedBySQLite
|
|
|
|
if distinctBase(db.syncCommittees.containsStmt) == nil:
|
|
|
|
return false
|
|
|
|
var exists: int64
|
|
|
|
for res in db.syncCommittees.containsStmt.exec(period.int64, exists):
|
|
|
|
res.expect("SQL query OK")
|
|
|
|
doAssert exists == 1
|
|
|
|
return true
|
|
|
|
false
|
|
|
|
|
|
|
|
proc getSyncCommittee*(
|
|
|
|
db: LightClientDataDB, period: SyncCommitteePeriod
|
|
|
|
): Opt[altair.SyncCommittee] =
|
|
|
|
doAssert period.isSupportedBySQLite
|
|
|
|
if distinctBase(db.syncCommittees.getStmt) == nil:
|
|
|
|
return Opt.none(altair.SyncCommittee)
|
|
|
|
var branch: seq[byte]
|
|
|
|
for res in db.syncCommittees.getStmt.exec(period.int64, branch):
|
|
|
|
res.expect("SQL query OK")
|
|
|
|
try:
|
|
|
|
return ok SSZ.decode(branch, altair.SyncCommittee)
|
2023-06-15 16:29:13 +00:00
|
|
|
except SerializationError as exc:
|
2023-01-27 09:44:57 +00:00
|
|
|
error "LC data store corrupted", store = "syncCommittees",
|
|
|
|
period, exc = exc.msg
|
|
|
|
return Opt.none(altair.SyncCommittee)
|
|
|
|
|
|
|
|
func putSyncCommittee*(
|
|
|
|
db: LightClientDataDB, period: SyncCommitteePeriod,
|
|
|
|
syncCommittee: altair.SyncCommittee) =
|
|
|
|
doAssert not db.backend.readOnly # All `stmt` are non-nil
|
|
|
|
doAssert period.isSupportedBySQLite
|
|
|
|
let res = db.syncCommittees.putStmt.exec(
|
|
|
|
(period.int64, SSZ.encode(syncCommittee)))
|
|
|
|
res.expect("SQL query OK")
|
|
|
|
|
2023-01-16 15:53:45 +00:00
|
|
|
proc initLegacyBestUpdatesStore(
|
|
|
|
backend: SqStoreRef,
|
|
|
|
name: string,
|
|
|
|
): KvResult[LegacyBestLightClientUpdateStore] =
|
2023-01-24 02:52:08 +00:00
|
|
|
if not backend.readOnly:
|
|
|
|
? backend.exec("""
|
|
|
|
CREATE TABLE IF NOT EXISTS `""" & name & """` (
|
|
|
|
`period` INTEGER PRIMARY KEY, -- `SyncCommitteePeriod`
|
|
|
|
`update` BLOB -- `altair.LightClientUpdate` (SSZ)
|
|
|
|
);
|
|
|
|
""")
|
|
|
|
if not ? backend.hasTable(name):
|
2023-01-18 02:06:23 +00:00
|
|
|
return ok LegacyBestLightClientUpdateStore()
|
|
|
|
|
|
|
|
const legacyKind = Base10.toString(ord(LightClientDataFork.Altair).uint)
|
2023-01-16 15:53:45 +00:00
|
|
|
let
|
2023-01-18 02:06:23 +00:00
|
|
|
getStmt = backend.prepareStmt("""
|
|
|
|
SELECT """ & legacyKind & """ AS `kind`, `update`
|
|
|
|
FROM `""" & name & """`
|
|
|
|
WHERE `period` = ?;
|
|
|
|
""", int64, (int64, seq[byte]), managed = false).expect("SQL query OK")
|
2023-01-16 15:53:45 +00:00
|
|
|
putStmt = backend.prepareStmt("""
|
|
|
|
REPLACE INTO `""" & name & """` (
|
|
|
|
`period`, `update`
|
|
|
|
) VALUES (?, ?);
|
|
|
|
""", (int64, seq[byte]), void, managed = false).expect("SQL query OK")
|
|
|
|
delStmt = backend.prepareStmt("""
|
|
|
|
DELETE FROM `""" & name & """`
|
|
|
|
WHERE `period` = ?;
|
|
|
|
""", int64, void, managed = false).expect("SQL query OK")
|
|
|
|
keepFromStmt = backend.prepareStmt("""
|
|
|
|
DELETE FROM `""" & name & """`
|
|
|
|
WHERE `period` < ?;
|
|
|
|
""", int64, void, managed = false).expect("SQL query OK")
|
|
|
|
|
|
|
|
ok LegacyBestLightClientUpdateStore(
|
2023-01-18 02:06:23 +00:00
|
|
|
getStmt: getStmt,
|
2023-01-16 15:53:45 +00:00
|
|
|
putStmt: putStmt,
|
|
|
|
delStmt: delStmt,
|
|
|
|
keepFromStmt: keepFromStmt)
|
|
|
|
|
2023-01-18 02:06:23 +00:00
|
|
|
func close(store: var LegacyBestLightClientUpdateStore) =
|
|
|
|
store.getStmt.disposeSafe()
|
|
|
|
store.putStmt.disposeSafe()
|
|
|
|
store.delStmt.disposeSafe()
|
|
|
|
store.keepFromStmt.disposeSafe()
|
2023-01-16 15:53:45 +00:00
|
|
|
|
2022-07-04 20:46:32 +00:00
|
|
|
proc initBestUpdatesStore(
|
|
|
|
backend: SqStoreRef,
|
2023-01-12 17:11:38 +00:00
|
|
|
name, legacyAltairName: string,
|
|
|
|
): KvResult[BestLightClientUpdateStore] =
|
2023-01-24 02:52:08 +00:00
|
|
|
if not backend.readOnly:
|
2023-01-12 17:11:38 +00:00
|
|
|
? backend.exec("""
|
2023-01-24 02:52:08 +00:00
|
|
|
CREATE TABLE IF NOT EXISTS `""" & name & """` (
|
|
|
|
`period` INTEGER PRIMARY KEY, -- `SyncCommitteePeriod`
|
|
|
|
`kind` INTEGER, -- `LightClientDataFork`
|
|
|
|
`update` BLOB -- `LightClientUpdate` (SSZ)
|
|
|
|
);
|
2023-01-12 17:11:38 +00:00
|
|
|
""")
|
2023-01-24 02:52:08 +00:00
|
|
|
if ? backend.hasTable(legacyAltairName):
|
|
|
|
# SyncCommitteePeriod -> altair.LightClientUpdate
|
|
|
|
const legacyKind = Base10.toString(ord(LightClientDataFork.Altair).uint)
|
|
|
|
? backend.exec("""
|
|
|
|
INSERT OR IGNORE INTO `""" & name & """` (
|
|
|
|
`period`, `kind`, `update`
|
|
|
|
)
|
|
|
|
SELECT `period`, """ & legacyKind & """ AS `kind`, `update`
|
|
|
|
FROM `""" & legacyAltairName & """`;
|
|
|
|
""")
|
|
|
|
if not ? backend.hasTable(name):
|
|
|
|
return ok BestLightClientUpdateStore()
|
2022-06-30 13:04:39 +00:00
|
|
|
|
|
|
|
let
|
2022-07-04 20:46:32 +00:00
|
|
|
getStmt = backend.prepareStmt("""
|
2023-01-12 17:11:38 +00:00
|
|
|
SELECT `kind`, `update`
|
2022-07-04 20:46:32 +00:00
|
|
|
FROM `""" & name & """`
|
2022-06-30 13:04:39 +00:00
|
|
|
WHERE `period` = ?;
|
2023-01-12 17:11:38 +00:00
|
|
|
""", int64, (int64, seq[byte]), managed = false)
|
|
|
|
.expect("SQL query OK")
|
2022-07-04 20:46:32 +00:00
|
|
|
putStmt = backend.prepareStmt("""
|
|
|
|
REPLACE INTO `""" & name & """` (
|
2023-01-12 17:11:38 +00:00
|
|
|
`period`, `kind`, `update`
|
|
|
|
) VALUES (?, ?, ?);
|
|
|
|
""", (int64, int64, seq[byte]), void, managed = false)
|
|
|
|
.expect("SQL query OK")
|
2022-07-04 20:46:32 +00:00
|
|
|
delStmt = backend.prepareStmt("""
|
|
|
|
DELETE FROM `""" & name & """`
|
2022-06-30 13:04:39 +00:00
|
|
|
WHERE `period` = ?;
|
2022-07-04 20:46:32 +00:00
|
|
|
""", int64, void, managed = false).expect("SQL query OK")
|
|
|
|
keepFromStmt = backend.prepareStmt("""
|
|
|
|
DELETE FROM `""" & name & """`
|
2022-06-30 13:04:39 +00:00
|
|
|
WHERE `period` < ?;
|
2022-07-04 20:46:32 +00:00
|
|
|
""", int64, void, managed = false).expect("SQL query OK")
|
2022-06-30 13:04:39 +00:00
|
|
|
|
|
|
|
ok BestLightClientUpdateStore(
|
|
|
|
getStmt: getStmt,
|
|
|
|
putStmt: putStmt,
|
|
|
|
delStmt: delStmt,
|
|
|
|
keepFromStmt: keepFromStmt)
|
|
|
|
|
2023-01-18 02:06:23 +00:00
|
|
|
func close(store: var BestLightClientUpdateStore) =
|
|
|
|
store.getStmt.disposeSafe()
|
|
|
|
store.putStmt.disposeSafe()
|
|
|
|
store.delStmt.disposeSafe()
|
|
|
|
store.keepFromStmt.disposeSafe()
|
2022-07-04 20:46:32 +00:00
|
|
|
|
2022-06-30 13:04:39 +00:00
|
|
|
proc getBestUpdate*(
|
|
|
|
db: LightClientDataDB, period: SyncCommitteePeriod
|
2023-01-12 17:11:38 +00:00
|
|
|
): ForkedLightClientUpdate =
|
2022-06-30 13:04:39 +00:00
|
|
|
doAssert period.isSupportedBySQLite
|
2023-01-18 02:06:23 +00:00
|
|
|
|
2023-01-12 17:11:38 +00:00
|
|
|
var update: (int64, seq[byte])
|
2023-02-24 19:50:46 +00:00
|
|
|
proc processUpdate(): ForkedLightClientUpdate =
|
2022-06-30 13:04:39 +00:00
|
|
|
try:
|
2023-01-14 21:19:50 +00:00
|
|
|
withAll(LightClientDataFork):
|
|
|
|
when lcDataFork > LightClientDataFork.None:
|
|
|
|
if update[0] == ord(lcDataFork).int64:
|
2023-10-04 16:11:45 +00:00
|
|
|
return ForkedLightClientUpdate.init(SSZ.decode(
|
|
|
|
update[1], lcDataFork.LightClientUpdate))
|
2023-01-14 21:19:50 +00:00
|
|
|
warn "Unsupported LC data store kind", store = "bestUpdates",
|
|
|
|
period, kind = update[0]
|
|
|
|
return default(ForkedLightClientUpdate)
|
2023-06-15 16:29:13 +00:00
|
|
|
except SerializationError as exc:
|
2022-11-30 03:45:03 +00:00
|
|
|
error "LC data store corrupted", store = "bestUpdates",
|
2023-01-12 17:11:38 +00:00
|
|
|
period, kind = update[0], exc = exc.msg
|
|
|
|
return default(ForkedLightClientUpdate)
|
2022-06-30 13:04:39 +00:00
|
|
|
|
2023-01-18 02:06:23 +00:00
|
|
|
if distinctBase(db.bestUpdates.getStmt) != nil:
|
|
|
|
for res in db.bestUpdates.getStmt.exec(period.int64, update):
|
2023-02-24 19:50:46 +00:00
|
|
|
res.expect("SQL query OK")
|
|
|
|
return processUpdate()
|
|
|
|
if distinctBase(db.legacyBestUpdates.getStmt) != nil:
|
2023-01-18 02:06:23 +00:00
|
|
|
for res in db.legacyBestUpdates.getStmt.exec(period.int64, update):
|
2023-02-24 19:50:46 +00:00
|
|
|
res.expect("SQL query OK")
|
|
|
|
return processUpdate()
|
|
|
|
default(ForkedLightClientUpdate)
|
2023-01-18 02:06:23 +00:00
|
|
|
|
2022-06-30 13:04:39 +00:00
|
|
|
func putBestUpdate*(
|
|
|
|
db: LightClientDataDB, period: SyncCommitteePeriod,
|
2023-01-12 17:11:38 +00:00
|
|
|
update: ForkedLightClientUpdate) =
|
2023-01-18 02:06:23 +00:00
|
|
|
doAssert not db.backend.readOnly # All `stmt` are non-nil
|
2022-06-30 13:04:39 +00:00
|
|
|
doAssert period.isSupportedBySQLite
|
2023-01-12 17:11:38 +00:00
|
|
|
withForkyUpdate(update):
|
2023-01-14 21:19:50 +00:00
|
|
|
when lcDataFork > LightClientDataFork.None:
|
2023-01-12 17:11:38 +00:00
|
|
|
let numParticipants = forkyUpdate.sync_aggregate.num_active_participants
|
|
|
|
if numParticipants < MIN_SYNC_COMMITTEE_PARTICIPANTS:
|
2023-01-16 15:53:45 +00:00
|
|
|
block:
|
|
|
|
let res = db.bestUpdates.delStmt.exec(period.int64)
|
|
|
|
res.expect("SQL query OK")
|
|
|
|
block:
|
|
|
|
let res = db.legacyBestUpdates.delStmt.exec(period.int64)
|
|
|
|
res.expect("SQL query OK")
|
|
|
|
else:
|
|
|
|
block:
|
|
|
|
let res = db.bestUpdates.putStmt.exec(
|
|
|
|
(period.int64, lcDataFork.int64, SSZ.encode(forkyUpdate)))
|
|
|
|
res.expect("SQL query OK")
|
|
|
|
when lcDataFork == LightClientDataFork.Altair:
|
|
|
|
let res = db.legacyBestUpdates.putStmt.exec(
|
|
|
|
(period.int64, SSZ.encode(forkyUpdate)))
|
|
|
|
res.expect("SQL query OK")
|
|
|
|
else:
|
|
|
|
# Keep legacy table at best Altair update.
|
|
|
|
discard
|
|
|
|
else:
|
|
|
|
block:
|
2023-01-12 17:11:38 +00:00
|
|
|
let res = db.bestUpdates.delStmt.exec(period.int64)
|
|
|
|
res.expect("SQL query OK")
|
2023-01-16 15:53:45 +00:00
|
|
|
block:
|
|
|
|
let res = db.legacyBestUpdates.delStmt.exec(period.int64)
|
2023-01-12 17:11:38 +00:00
|
|
|
res.expect("SQL query OK")
|
2022-06-30 13:04:39 +00:00
|
|
|
|
2022-07-04 20:46:32 +00:00
|
|
|
proc initSealedPeriodsStore(
|
|
|
|
backend: SqStoreRef,
|
|
|
|
name: string): KvResult[SealedSyncCommitteePeriodStore] =
|
2023-01-24 02:52:08 +00:00
|
|
|
if not backend.readOnly:
|
|
|
|
? backend.exec("""
|
|
|
|
CREATE TABLE IF NOT EXISTS `""" & name & """` (
|
|
|
|
`period` INTEGER PRIMARY KEY -- `SyncCommitteePeriod`
|
|
|
|
);
|
|
|
|
""")
|
|
|
|
if not ? backend.hasTable(name):
|
2023-01-18 02:06:23 +00:00
|
|
|
return ok SealedSyncCommitteePeriodStore()
|
|
|
|
|
2022-06-30 13:04:39 +00:00
|
|
|
let
|
2022-07-04 20:46:32 +00:00
|
|
|
containsStmt = backend.prepareStmt("""
|
2022-06-30 13:04:39 +00:00
|
|
|
SELECT 1 AS `exists`
|
2022-07-04 20:46:32 +00:00
|
|
|
FROM `""" & name & """`
|
2022-06-30 13:04:39 +00:00
|
|
|
WHERE `period` = ?;
|
2022-07-04 20:46:32 +00:00
|
|
|
""", int64, int64, managed = false).expect("SQL query OK")
|
|
|
|
putStmt = backend.prepareStmt("""
|
2023-01-27 09:44:57 +00:00
|
|
|
REPLACE INTO `""" & name & """` (
|
2022-06-30 13:04:39 +00:00
|
|
|
`period`
|
|
|
|
) VALUES (?);
|
2022-07-04 20:46:32 +00:00
|
|
|
""", int64, void, managed = false).expect("SQL query OK")
|
|
|
|
keepFromStmt = backend.prepareStmt("""
|
|
|
|
DELETE FROM `""" & name & """`
|
2022-06-30 13:04:39 +00:00
|
|
|
WHERE `period` < ?;
|
2022-07-04 20:46:32 +00:00
|
|
|
""", int64, void, managed = false).expect("SQL query OK")
|
2022-06-30 13:04:39 +00:00
|
|
|
|
|
|
|
ok SealedSyncCommitteePeriodStore(
|
|
|
|
containsStmt: containsStmt,
|
|
|
|
putStmt: putStmt,
|
|
|
|
keepFromStmt: keepFromStmt)
|
|
|
|
|
2023-01-18 02:06:23 +00:00
|
|
|
func close(store: var SealedSyncCommitteePeriodStore) =
|
|
|
|
store.containsStmt.disposeSafe()
|
|
|
|
store.putStmt.disposeSafe()
|
|
|
|
store.keepFromStmt.disposeSafe()
|
2022-07-04 20:46:32 +00:00
|
|
|
|
2022-06-30 13:04:39 +00:00
|
|
|
func isPeriodSealed*(
|
|
|
|
db: LightClientDataDB, period: SyncCommitteePeriod): bool =
|
|
|
|
doAssert period.isSupportedBySQLite
|
2023-01-18 02:06:23 +00:00
|
|
|
if distinctBase(db.sealedPeriods.containsStmt) == nil:
|
|
|
|
return false
|
2022-06-30 13:04:39 +00:00
|
|
|
var exists: int64
|
|
|
|
for res in db.sealedPeriods.containsStmt.exec(period.int64, exists):
|
|
|
|
res.expect("SQL query OK")
|
|
|
|
doAssert exists == 1
|
|
|
|
return true
|
|
|
|
false
|
|
|
|
|
|
|
|
func sealPeriod*(
|
|
|
|
db: LightClientDataDB, period: SyncCommitteePeriod) =
|
2023-01-18 02:06:23 +00:00
|
|
|
doAssert not db.backend.readOnly # All `stmt` are non-nil
|
2022-06-30 13:04:39 +00:00
|
|
|
doAssert period.isSupportedBySQLite
|
|
|
|
let res = db.sealedPeriods.putStmt.exec(period.int64)
|
|
|
|
res.expect("SQL query OK")
|
|
|
|
|
|
|
|
func keepPeriodsFrom*(
|
|
|
|
db: LightClientDataDB, minPeriod: SyncCommitteePeriod) =
|
2023-01-18 02:06:23 +00:00
|
|
|
doAssert not db.backend.readOnly # All `stmt` are non-nil
|
2022-06-30 13:04:39 +00:00
|
|
|
doAssert minPeriod.isSupportedBySQLite
|
2023-01-16 15:53:45 +00:00
|
|
|
block:
|
|
|
|
let res = db.sealedPeriods.keepFromStmt.exec(minPeriod.int64)
|
|
|
|
res.expect("SQL query OK")
|
|
|
|
block:
|
|
|
|
let res = db.bestUpdates.keepFromStmt.exec(minPeriod.int64)
|
|
|
|
res.expect("SQL query OK")
|
|
|
|
block:
|
|
|
|
let res = db.legacyBestUpdates.keepFromStmt.exec(minPeriod.int64)
|
|
|
|
res.expect("SQL query OK")
|
2023-01-27 09:44:57 +00:00
|
|
|
block:
|
|
|
|
let res = db.syncCommittees.keepFromStmt.exec(minPeriod.int64)
|
|
|
|
res.expect("SQL query OK")
|
2023-01-16 15:53:45 +00:00
|
|
|
let minSlot = min(minPeriod.start_slot, int64.high.Slot)
|
2024-06-21 13:02:01 +00:00
|
|
|
for branchFork, store in db.currentBranches:
|
|
|
|
if branchFork > BranchFork.None and
|
|
|
|
distinctBase(store.keepFromStmt) != nil:
|
|
|
|
let res = store.keepFromStmt.exec(minSlot.int64)
|
|
|
|
res.expect("SQL query OK")
|
2023-01-27 09:44:57 +00:00
|
|
|
for lcDataFork, store in db.headers:
|
|
|
|
if lcDataFork > LightClientDataFork.None and
|
|
|
|
distinctBase(store.keepFromStmt) != nil:
|
|
|
|
let res = store.keepFromStmt.exec(minSlot.int64)
|
|
|
|
res.expect("SQL query OK")
|
2022-06-30 13:04:39 +00:00
|
|
|
|
2022-07-04 20:46:32 +00:00
|
|
|
type LightClientDataDBNames* = object
|
2023-01-27 09:44:57 +00:00
|
|
|
altairHeaders*: string
|
|
|
|
capellaHeaders*: string
|
2023-03-10 17:16:26 +00:00
|
|
|
denebHeaders*: string
|
2024-06-21 13:02:01 +00:00
|
|
|
electraHeaders*: string
|
2022-07-04 20:46:32 +00:00
|
|
|
altairCurrentBranches*: string
|
2024-06-21 13:02:01 +00:00
|
|
|
electraCurrentBranches*: string
|
2023-01-27 09:44:57 +00:00
|
|
|
altairSyncCommittees*: string
|
2023-01-12 17:11:38 +00:00
|
|
|
legacyAltairBestUpdates*: string
|
|
|
|
bestUpdates*: string
|
2022-07-04 20:46:32 +00:00
|
|
|
sealedPeriods*: string
|
|
|
|
|
2022-06-30 13:04:39 +00:00
|
|
|
proc initLightClientDataDB*(
|
2022-07-04 20:46:32 +00:00
|
|
|
backend: SqStoreRef,
|
|
|
|
names: LightClientDataDBNames): KvResult[LightClientDataDB] =
|
2024-06-21 13:02:01 +00:00
|
|
|
static: doAssert LightClientDataFork.high == LightClientDataFork.Electra
|
2022-06-30 13:04:39 +00:00
|
|
|
let
|
2023-01-27 09:44:57 +00:00
|
|
|
headers = [
|
|
|
|
# LightClientDataFork.None
|
|
|
|
LightClientHeaderStore(),
|
|
|
|
# LightClientDataFork.Altair
|
|
|
|
? backend.initHeadersStore(
|
|
|
|
names.altairHeaders, "altair.LightClientHeader"),
|
|
|
|
# LightClientDataFork.Capella
|
|
|
|
? backend.initHeadersStore(
|
|
|
|
names.capellaHeaders, "capella.LightClientHeader"),
|
2023-03-10 17:16:26 +00:00
|
|
|
# LightClientDataFork.Deneb
|
2023-01-27 09:44:57 +00:00
|
|
|
? backend.initHeadersStore(
|
2024-06-21 13:02:01 +00:00
|
|
|
names.denebHeaders, "deneb.LightClientHeader"),
|
|
|
|
# LightClientDataFork.Electra
|
|
|
|
? backend.initHeadersStore(
|
|
|
|
names.electraHeaders, "electra.LightClientHeader"),
|
|
|
|
]
|
|
|
|
currentBranches = [
|
|
|
|
# BranchFork.None
|
|
|
|
CurrentSyncCommitteeBranchStore(),
|
|
|
|
# BranchFork.Altair
|
|
|
|
? backend.initCurrentBranchesStore(
|
|
|
|
names.altairCurrentBranches, "altair.CurrentSyncCommitteeBranch"),
|
|
|
|
# BranchFork.Electra
|
|
|
|
? backend.initCurrentBranchesStore(
|
|
|
|
names.electraCurrentBranches, "electra.CurrentSyncCommitteeBranch"),
|
2023-01-27 09:44:57 +00:00
|
|
|
]
|
|
|
|
syncCommittees =
|
|
|
|
? backend.initSyncCommitteesStore(names.altairSyncCommittees)
|
2023-01-16 15:53:45 +00:00
|
|
|
legacyBestUpdates =
|
|
|
|
? backend.initLegacyBestUpdatesStore(names.legacyAltairBestUpdates)
|
2022-07-04 20:46:32 +00:00
|
|
|
bestUpdates =
|
2023-01-12 17:11:38 +00:00
|
|
|
? backend.initBestUpdatesStore(
|
|
|
|
names.bestUpdates, names.legacyAltairBestUpdates)
|
2022-07-04 20:46:32 +00:00
|
|
|
sealedPeriods =
|
|
|
|
? backend.initSealedPeriodsStore(names.sealedPeriods)
|
2022-06-30 13:04:39 +00:00
|
|
|
|
|
|
|
ok LightClientDataDB(
|
2023-01-27 09:44:57 +00:00
|
|
|
headers: headers,
|
2022-06-30 13:04:39 +00:00
|
|
|
backend: backend,
|
|
|
|
currentBranches: currentBranches,
|
2023-01-27 09:44:57 +00:00
|
|
|
syncCommittees: syncCommittees,
|
2023-01-16 15:53:45 +00:00
|
|
|
legacyBestUpdates: legacyBestUpdates,
|
2022-06-30 13:04:39 +00:00
|
|
|
bestUpdates: bestUpdates,
|
|
|
|
sealedPeriods: sealedPeriods)
|
|
|
|
|
2022-07-04 20:46:32 +00:00
|
|
|
proc close*(db: LightClientDataDB) =
|
2022-06-30 13:04:39 +00:00
|
|
|
if db.backend != nil:
|
2023-01-27 09:44:57 +00:00
|
|
|
for lcDataFork in LightClientDataFork:
|
|
|
|
if lcDataFork > LightClientDataFork.None:
|
|
|
|
db.headers[lcDataFork].close()
|
2024-06-21 13:02:01 +00:00
|
|
|
for branchFork in BranchFork:
|
|
|
|
if branchFork > BranchFork.None:
|
|
|
|
db.currentBranches[branchFork].close()
|
2023-01-27 09:44:57 +00:00
|
|
|
db.syncCommittees.close()
|
2023-01-16 15:53:45 +00:00
|
|
|
db.legacyBestUpdates.close()
|
2022-07-04 20:46:32 +00:00
|
|
|
db.bestUpdates.close()
|
|
|
|
db.sealedPeriods.close()
|
|
|
|
db[].reset()
|