2022-06-30 13:04:39 +00:00
|
|
|
# beacon_chain
|
2023-01-10 11:26:25 +00:00
|
|
|
# Copyright (c) 2022-2023 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.
|
|
|
|
|
2022-07-29 10:53:42 +00:00
|
|
|
when (NimMajor, NimMinor) < (1, 4):
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
else:
|
|
|
|
{.push raises: [].}
|
|
|
|
|
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-12 17:11:38 +00:00
|
|
|
# `lc_altair_current_branches` holds merkle proofs needed to
|
|
|
|
# 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-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.
|
|
|
|
# Finally, the libp2p context bytes depend on `attested_header.slot` to derive
|
2023-01-12 17:11:38 +00:00
|
|
|
# the underlying fork digest; the `kind` column is not sufficient to derive
|
|
|
|
# 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-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.
|
|
|
|
|
|
|
|
type
|
|
|
|
CurrentSyncCommitteeBranchStore = object
|
|
|
|
containsStmt: SqliteStmt[int64, int64]
|
|
|
|
getStmt: SqliteStmt[int64, seq[byte]]
|
|
|
|
putStmt: SqliteStmt[(int64, seq[byte]), void]
|
|
|
|
keepFromStmt: SqliteStmt[int64, void]
|
|
|
|
|
|
|
|
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]
|
|
|
|
delFromStmt: SqliteStmt[int64, void]
|
|
|
|
keepFromStmt: SqliteStmt[int64, void]
|
|
|
|
|
|
|
|
SealedSyncCommitteePeriodStore = object
|
|
|
|
containsStmt: SqliteStmt[int64, int64]
|
|
|
|
putStmt: SqliteStmt[int64, void]
|
|
|
|
delFromStmt: 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
|
|
|
|
|
|
|
|
currentBranches: CurrentSyncCommitteeBranchStore
|
|
|
|
## Slot -> altair.CurrentSyncCommitteeBranch
|
|
|
|
## 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.
|
|
|
|
|
|
|
|
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`).
|
|
|
|
|
2022-07-04 20:46:32 +00:00
|
|
|
proc initCurrentBranchesStore(
|
|
|
|
backend: SqStoreRef,
|
|
|
|
name: string): KvResult[CurrentSyncCommitteeBranchStore] =
|
2022-06-30 13:04:39 +00:00
|
|
|
? backend.exec("""
|
2022-07-04 20:46:32 +00:00
|
|
|
CREATE TABLE IF NOT EXISTS `""" & name & """` (
|
2022-06-30 13:04:39 +00:00
|
|
|
`slot` INTEGER PRIMARY KEY, -- `Slot` (up through 2^63-1)
|
|
|
|
`branch` BLOB -- `altair.CurrentSyncCommitteeBranch` (SSZ)
|
|
|
|
);
|
|
|
|
""")
|
|
|
|
|
|
|
|
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("""
|
|
|
|
INSERT 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)
|
|
|
|
|
2022-07-04 20:46:32 +00:00
|
|
|
func close(store: CurrentSyncCommitteeBranchStore) =
|
|
|
|
store.containsStmt.dispose()
|
|
|
|
store.getStmt.dispose()
|
|
|
|
store.putStmt.dispose()
|
|
|
|
store.keepFromStmt.dispose()
|
|
|
|
|
2022-06-30 13:04:39 +00:00
|
|
|
func hasCurrentSyncCommitteeBranch*(
|
|
|
|
db: LightClientDataDB, slot: Slot): bool =
|
|
|
|
if not slot.isSupportedBySQLite:
|
|
|
|
return false
|
|
|
|
var exists: int64
|
|
|
|
for res in db.currentBranches.containsStmt.exec(slot.int64, exists):
|
|
|
|
res.expect("SQL query OK")
|
|
|
|
doAssert exists == 1
|
|
|
|
return true
|
|
|
|
false
|
|
|
|
|
|
|
|
proc getCurrentSyncCommitteeBranch*(
|
|
|
|
db: LightClientDataDB, slot: Slot): altair.CurrentSyncCommitteeBranch =
|
|
|
|
if not slot.isSupportedBySQLite:
|
|
|
|
return default(altair.CurrentSyncCommitteeBranch)
|
|
|
|
var branch: seq[byte]
|
|
|
|
for res in db.currentBranches.getStmt.exec(slot.int64, branch):
|
|
|
|
res.expect("SQL query OK")
|
|
|
|
try:
|
|
|
|
return SSZ.decode(branch, altair.CurrentSyncCommitteeBranch)
|
2022-11-30 03:45:03 +00:00
|
|
|
except SszError as exc:
|
|
|
|
error "LC data store corrupted", store = "currentBranches",
|
|
|
|
slot, exc = exc.msg
|
2022-06-30 13:04:39 +00:00
|
|
|
return default(altair.CurrentSyncCommitteeBranch)
|
|
|
|
|
|
|
|
func putCurrentSyncCommitteeBranch*(
|
|
|
|
db: LightClientDataDB, slot: Slot,
|
|
|
|
branch: altair.CurrentSyncCommitteeBranch) =
|
|
|
|
if not slot.isSupportedBySQLite:
|
|
|
|
return
|
|
|
|
let res = db.currentBranches.putStmt.exec((slot.int64, SSZ.encode(branch)))
|
|
|
|
res.expect("SQL query OK")
|
|
|
|
|
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] =
|
2022-06-30 13:04:39 +00:00
|
|
|
? backend.exec("""
|
2022-07-04 20:46:32 +00:00
|
|
|
CREATE TABLE IF NOT EXISTS `""" & name & """` (
|
2022-06-30 13:04:39 +00:00
|
|
|
`period` INTEGER PRIMARY KEY, -- `SyncCommitteePeriod`
|
2023-01-12 17:11:38 +00:00
|
|
|
`kind` INTEGER, -- `LightClientDataFork`
|
|
|
|
`update` BLOB -- `LightClientUpdate` (SSZ)
|
2022-06-30 13:04:39 +00:00
|
|
|
);
|
|
|
|
""")
|
2023-01-12 17:11:38 +00:00
|
|
|
if backend.hasTable(legacyAltairName).expect("SQL query OK"):
|
|
|
|
info "Importing Altair light client data"
|
|
|
|
# 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 & """`;
|
|
|
|
""")
|
|
|
|
? backend.exec("""
|
|
|
|
DROP TABLE `""" & legacyAltairName & """`;
|
|
|
|
""")
|
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")
|
|
|
|
delFromStmt = 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,
|
|
|
|
delFromStmt: delFromStmt,
|
|
|
|
keepFromStmt: keepFromStmt)
|
|
|
|
|
2022-07-04 20:46:32 +00:00
|
|
|
func close(store: BestLightClientUpdateStore) =
|
|
|
|
store.getStmt.dispose()
|
|
|
|
store.putStmt.dispose()
|
|
|
|
store.delStmt.dispose()
|
|
|
|
store.delFromStmt.dispose()
|
|
|
|
store.keepFromStmt.dispose()
|
|
|
|
|
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-12 17:11:38 +00:00
|
|
|
var update: (int64, seq[byte])
|
2022-06-30 13:04:39 +00:00
|
|
|
for res in db.bestUpdates.getStmt.exec(period.int64, update):
|
|
|
|
res.expect("SQL query OK")
|
|
|
|
try:
|
2023-01-12 17:11:38 +00:00
|
|
|
case update[0]
|
|
|
|
of ord(LightClientDataFork.Altair).int64:
|
|
|
|
return ForkedLightClientUpdate(
|
|
|
|
kind: LightClientDataFork.Altair,
|
|
|
|
altairData: SSZ.decode(update[1], altair.LightClientUpdate))
|
|
|
|
else:
|
|
|
|
warn "Unsupported LC data store kind", store = "bestUpdates",
|
|
|
|
period, kind = update[0]
|
|
|
|
return default(ForkedLightClientUpdate)
|
2022-11-30 03:45:03 +00:00
|
|
|
except SszError as exc:
|
|
|
|
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
|
|
|
|
|
|
|
func putBestUpdate*(
|
|
|
|
db: LightClientDataDB, period: SyncCommitteePeriod,
|
2023-01-12 17:11:38 +00:00
|
|
|
update: ForkedLightClientUpdate) =
|
2022-06-30 13:04:39 +00:00
|
|
|
doAssert period.isSupportedBySQLite
|
2023-01-12 17:11:38 +00:00
|
|
|
withForkyUpdate(update):
|
|
|
|
when lcDataFork >= LightClientDataFork.Altair:
|
|
|
|
let numParticipants = forkyUpdate.sync_aggregate.num_active_participants
|
|
|
|
if numParticipants < MIN_SYNC_COMMITTEE_PARTICIPANTS:
|
|
|
|
let res = db.bestUpdates.delStmt.exec(period.int64)
|
|
|
|
res.expect("SQL query OK")
|
|
|
|
else:
|
|
|
|
let res = db.bestUpdates.putStmt.exec(
|
|
|
|
(period.int64, lcDataFork.int64, SSZ.encode(forkyUpdate)))
|
|
|
|
res.expect("SQL query OK")
|
|
|
|
else:
|
|
|
|
let res = db.bestUpdates.delStmt.exec(period.int64)
|
|
|
|
res.expect("SQL query OK")
|
2022-06-30 13:04:39 +00:00
|
|
|
|
|
|
|
proc putUpdateIfBetter*(
|
|
|
|
db: LightClientDataDB, period: SyncCommitteePeriod,
|
2023-01-12 17:11:38 +00:00
|
|
|
update: ForkedLightClientUpdate) =
|
2022-06-30 13:04:39 +00:00
|
|
|
let existing = db.getBestUpdate(period)
|
|
|
|
if is_better_update(update, existing):
|
|
|
|
db.putBestUpdate(period, update)
|
|
|
|
|
2022-07-04 20:46:32 +00:00
|
|
|
proc initSealedPeriodsStore(
|
|
|
|
backend: SqStoreRef,
|
|
|
|
name: string): KvResult[SealedSyncCommitteePeriodStore] =
|
2022-06-30 13:04:39 +00:00
|
|
|
? backend.exec("""
|
2022-07-04 20:46:32 +00:00
|
|
|
CREATE TABLE IF NOT EXISTS `""" & name & """` (
|
2022-06-30 13:04:39 +00:00
|
|
|
`period` INTEGER PRIMARY KEY -- `SyncCommitteePeriod`
|
|
|
|
);
|
|
|
|
""")
|
|
|
|
|
|
|
|
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("""
|
|
|
|
INSERT 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")
|
|
|
|
delFromStmt = 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 SealedSyncCommitteePeriodStore(
|
|
|
|
containsStmt: containsStmt,
|
|
|
|
putStmt: putStmt,
|
|
|
|
delFromStmt: delFromStmt,
|
|
|
|
keepFromStmt: keepFromStmt)
|
|
|
|
|
2022-07-04 20:46:32 +00:00
|
|
|
func close(store: SealedSyncCommitteePeriodStore) =
|
|
|
|
store.containsStmt.dispose()
|
|
|
|
store.putStmt.dispose()
|
|
|
|
store.delFromStmt.dispose()
|
|
|
|
store.keepFromStmt.dispose()
|
|
|
|
|
2022-06-30 13:04:39 +00:00
|
|
|
func isPeriodSealed*(
|
|
|
|
db: LightClientDataDB, period: SyncCommitteePeriod): bool =
|
|
|
|
doAssert period.isSupportedBySQLite
|
|
|
|
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) =
|
|
|
|
doAssert period.isSupportedBySQLite
|
|
|
|
let res = db.sealedPeriods.putStmt.exec(period.int64)
|
|
|
|
res.expect("SQL query OK")
|
|
|
|
|
2023-01-12 17:11:38 +00:00
|
|
|
func delNonFinalizedPeriodsFrom*(
|
2022-06-30 13:04:39 +00:00
|
|
|
db: LightClientDataDB, minPeriod: SyncCommitteePeriod) =
|
|
|
|
doAssert minPeriod.isSupportedBySQLite
|
|
|
|
let res1 = db.sealedPeriods.delFromStmt.exec(minPeriod.int64)
|
|
|
|
res1.expect("SQL query OK")
|
|
|
|
let res2 = db.bestUpdates.delFromStmt.exec(minPeriod.int64)
|
|
|
|
res2.expect("SQL query OK")
|
2023-01-12 17:11:38 +00:00
|
|
|
# `currentBranches` only has finalized data
|
2022-06-30 13:04:39 +00:00
|
|
|
|
|
|
|
func keepPeriodsFrom*(
|
|
|
|
db: LightClientDataDB, minPeriod: SyncCommitteePeriod) =
|
|
|
|
doAssert minPeriod.isSupportedBySQLite
|
|
|
|
let res1 = db.sealedPeriods.keepFromStmt.exec(minPeriod.int64)
|
|
|
|
res1.expect("SQL query OK")
|
|
|
|
let res2 = db.bestUpdates.keepFromStmt.exec(minPeriod.int64)
|
|
|
|
res2.expect("SQL query OK")
|
|
|
|
let
|
|
|
|
minSlot = min(minPeriod.start_slot, int64.high.Slot)
|
|
|
|
res3 = db.currentBranches.keepFromStmt.exec(minSlot.int64)
|
|
|
|
res3.expect("SQL query OK")
|
|
|
|
|
2022-07-04 20:46:32 +00:00
|
|
|
type LightClientDataDBNames* = object
|
|
|
|
altairCurrentBranches*: 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] =
|
2022-06-30 13:04:39 +00:00
|
|
|
let
|
2022-07-04 20:46:32 +00:00
|
|
|
currentBranches =
|
|
|
|
? backend.initCurrentBranchesStore(names.altairCurrentBranches)
|
|
|
|
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(
|
|
|
|
backend: backend,
|
|
|
|
currentBranches: currentBranches,
|
|
|
|
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:
|
2022-07-04 20:46:32 +00:00
|
|
|
db.currentBranches.close()
|
|
|
|
db.bestUpdates.close()
|
|
|
|
db.sealedPeriods.close()
|
|
|
|
db[].reset()
|