2022-11-30 03:45:03 +00:00
|
|
|
# beacon_chain
|
2023-01-13 15:46:35 +00:00
|
|
|
# Copyright (c) 2022-2023 Status Research & Development GmbH
|
2022-11-30 03:45:03 +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-11-30 03:45:03 +00:00
|
|
|
|
|
|
|
import
|
|
|
|
# Status libraries
|
2023-01-16 15:53:45 +00:00
|
|
|
stew/base10,
|
2022-11-30 03:45:03 +00:00
|
|
|
chronicles,
|
|
|
|
eth/db/kvstore_sqlite3,
|
|
|
|
# Beacon chain internals
|
|
|
|
spec/datatypes/altair,
|
|
|
|
spec/[eth2_ssz_serialization, helpers],
|
|
|
|
./db_limits
|
|
|
|
|
|
|
|
logScope: topics = "lcdb"
|
|
|
|
|
2023-01-16 15:53:45 +00:00
|
|
|
# `lc_headers` holds the latest `LightClientStore.finalized_header`.
|
2022-11-30 03:45:03 +00:00
|
|
|
#
|
|
|
|
# `altair_sync_committees` holds finalized `SyncCommittee` by period, needed to
|
|
|
|
# continue an interrupted sync process without having to obtain bootstrap info.
|
|
|
|
|
|
|
|
type
|
2023-01-16 15:53:45 +00:00
|
|
|
LightClientHeaderKey {.pure.} = enum # Append only, used in DB data!
|
|
|
|
Finalized = 1 # Latest finalized header
|
2022-11-30 03:45:03 +00:00
|
|
|
|
2023-01-16 15:53:45 +00:00
|
|
|
LegacyLightClientHeadersStore = object
|
2023-01-18 02:06:23 +00:00
|
|
|
getStmt: SqliteStmt[int64, (int64, seq[byte])]
|
2022-11-30 03:45:03 +00:00
|
|
|
putStmt: SqliteStmt[(int64, seq[byte]), void]
|
|
|
|
|
2023-01-16 15:53:45 +00:00
|
|
|
LightClientHeadersStore = object
|
|
|
|
getStmt: SqliteStmt[int64, (int64, seq[byte])]
|
|
|
|
putStmt: SqliteStmt[(int64, int64, seq[byte]), void]
|
|
|
|
|
2022-11-30 03:45:03 +00:00
|
|
|
SyncCommitteeStore = object
|
|
|
|
getStmt: SqliteStmt[int64, seq[byte]]
|
|
|
|
putStmt: SqliteStmt[(int64, seq[byte]), void]
|
|
|
|
keepFromStmt: SqliteStmt[int64, void]
|
|
|
|
|
|
|
|
LightClientDB* = ref object
|
|
|
|
backend: SqStoreRef
|
|
|
|
## SQLite backend
|
|
|
|
|
2023-01-16 15:53:45 +00:00
|
|
|
legacyHeaders: LegacyLightClientHeadersStore
|
|
|
|
## LightClientHeaderKey -> altair.LightClientHeader
|
|
|
|
## Used through Bellatrix.
|
|
|
|
|
2022-11-30 03:45:03 +00:00
|
|
|
headers: LightClientHeadersStore
|
2023-01-16 15:53:45 +00:00
|
|
|
## LightClientHeaderKey -> (LightClientDataFork, LightClientHeader)
|
2022-11-30 03:45:03 +00:00
|
|
|
## Stores the latest light client headers.
|
|
|
|
|
|
|
|
syncCommittees: SyncCommitteeStore
|
|
|
|
## SyncCommitteePeriod -> altair.SyncCommittee
|
|
|
|
## Stores finalized `SyncCommittee` by sync committee period.
|
|
|
|
|
2023-08-17 17:46:58 +00:00
|
|
|
template disposeSafe(s: var SqliteStmt): untyped =
|
2023-01-18 02:06:23 +00:00
|
|
|
if distinctBase(s) != nil:
|
|
|
|
s.dispose()
|
2023-08-17 17:46:58 +00:00
|
|
|
s = typeof(s)(nil)
|
2023-01-18 02:06:23 +00:00
|
|
|
|
2023-01-16 15:53:45 +00:00
|
|
|
proc initLegacyLightClientHeadersStore(
|
2022-11-30 03:45:03 +00:00
|
|
|
backend: SqStoreRef,
|
2023-01-16 15:53:45 +00:00
|
|
|
name: string): KvResult[LegacyLightClientHeadersStore] =
|
2023-01-24 02:52:08 +00:00
|
|
|
if not backend.readOnly:
|
|
|
|
? backend.exec("""
|
|
|
|
CREATE TABLE IF NOT EXISTS `""" & name & """` (
|
|
|
|
`kind` INTEGER PRIMARY KEY, -- `LightClientHeaderKey`
|
|
|
|
`header` BLOB -- `altair.LightClientHeader` (SSZ)
|
|
|
|
);
|
|
|
|
""")
|
|
|
|
if not ? backend.hasTable(name):
|
2023-01-18 02:06:23 +00:00
|
|
|
return ok LegacyLightClientHeadersStore()
|
|
|
|
|
|
|
|
const legacyKind = Base10.toString(ord(LightClientDataFork.Altair).uint)
|
2022-11-30 03:45:03 +00:00
|
|
|
let
|
2023-01-18 02:06:23 +00:00
|
|
|
getStmt = backend.prepareStmt("""
|
|
|
|
SELECT """ & legacyKind & """ AS `kind`, `header`
|
|
|
|
FROM `""" & name & """`
|
|
|
|
WHERE `""" & name & """`.`kind` = ?;
|
|
|
|
""", int64, (int64, seq[byte]), managed = false).expect("SQL query OK")
|
2022-11-30 03:45:03 +00:00
|
|
|
putStmt = backend.prepareStmt("""
|
|
|
|
REPLACE INTO `""" & name & """` (
|
|
|
|
`kind`, `header`
|
|
|
|
) VALUES (?, ?);
|
2023-01-16 15:53:45 +00:00
|
|
|
""", (int64, seq[byte]), void, managed = false)
|
|
|
|
.expect("SQL query OK")
|
|
|
|
|
|
|
|
ok LegacyLightClientHeadersStore(
|
2023-01-18 02:06:23 +00:00
|
|
|
getStmt: getStmt,
|
2023-01-16 15:53:45 +00:00
|
|
|
putStmt: putStmt)
|
|
|
|
|
2023-01-18 02:06:23 +00:00
|
|
|
func close(store: var LegacyLightClientHeadersStore) =
|
|
|
|
store.getStmt.disposeSafe()
|
|
|
|
store.putStmt.disposeSafe()
|
2023-01-16 15:53:45 +00:00
|
|
|
|
|
|
|
proc initLightClientHeadersStore(
|
|
|
|
backend: SqStoreRef,
|
|
|
|
name, legacyAltairName: string): KvResult[LightClientHeadersStore] =
|
2023-01-24 02:52:08 +00:00
|
|
|
if not backend.readOnly:
|
2023-01-16 15:53:45 +00:00
|
|
|
? backend.exec("""
|
2023-01-24 02:52:08 +00:00
|
|
|
CREATE TABLE IF NOT EXISTS `""" & name & """` (
|
2023-01-27 09:44:57 +00:00
|
|
|
`key` INTEGER PRIMARY KEY, -- `LightClientHeaderKey`
|
|
|
|
`kind` INTEGER, -- `LightClientDataFork`
|
|
|
|
`header` BLOB -- `LightClientHeader` (SSZ)
|
2023-01-24 02:52:08 +00:00
|
|
|
);
|
2023-01-16 15:53:45 +00:00
|
|
|
""")
|
2023-01-24 02:52:08 +00:00
|
|
|
if ? backend.hasTable(legacyAltairName):
|
|
|
|
# LightClientHeaderKey -> altair.LightClientHeader
|
|
|
|
const legacyKind = Base10.toString(ord(LightClientDataFork.Altair).uint)
|
|
|
|
? backend.exec("""
|
|
|
|
INSERT OR IGNORE INTO `""" & name & """` (
|
|
|
|
`key`, `kind`, `header`
|
|
|
|
)
|
|
|
|
SELECT `kind` AS `key`, """ & legacyKind & """ AS `kind`, `header`
|
|
|
|
FROM `""" & legacyAltairName & """`;
|
|
|
|
""")
|
|
|
|
if not ? backend.hasTable(name):
|
|
|
|
return ok LightClientHeadersStore()
|
2023-01-16 15:53:45 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
getStmt = backend.prepareStmt("""
|
|
|
|
SELECT `kind`, `header`
|
|
|
|
FROM `""" & name & """`
|
|
|
|
WHERE `key` = ?;
|
|
|
|
""", int64, (int64, seq[byte]), managed = false).expect("SQL query OK")
|
|
|
|
putStmt = backend.prepareStmt("""
|
|
|
|
REPLACE INTO `""" & name & """` (
|
|
|
|
`key`, `kind`, `header`
|
|
|
|
) VALUES (?, ?, ?);
|
|
|
|
""", (int64, int64, seq[byte]), void, managed = false)
|
|
|
|
.expect("SQL query OK")
|
2022-11-30 03:45:03 +00:00
|
|
|
|
|
|
|
ok LightClientHeadersStore(
|
|
|
|
getStmt: getStmt,
|
|
|
|
putStmt: putStmt)
|
|
|
|
|
2023-01-18 02:06:23 +00:00
|
|
|
func close(store: var LightClientHeadersStore) =
|
|
|
|
store.getStmt.disposeSafe()
|
|
|
|
store.putStmt.disposeSafe()
|
2022-11-30 03:45:03 +00:00
|
|
|
|
2023-01-13 15:46:35 +00:00
|
|
|
proc getLatestFinalizedHeader*(
|
2023-01-16 15:53:45 +00:00
|
|
|
db: LightClientDB): ForkedLightClientHeader =
|
|
|
|
const key = LightClientHeaderKey.Finalized
|
2023-01-18 02:06:23 +00:00
|
|
|
|
2023-01-16 15:53:45 +00:00
|
|
|
var header: (int64, seq[byte])
|
2023-02-24 19:50:46 +00:00
|
|
|
proc processHeader(): ForkedLightClientHeader =
|
2022-11-30 03:45:03 +00:00
|
|
|
try:
|
2023-01-16 15:53:45 +00:00
|
|
|
withAll(LightClientDataFork):
|
|
|
|
when lcDataFork > LightClientDataFork.None:
|
|
|
|
if header[0] == ord(lcDataFork).int64:
|
|
|
|
var obj = ForkedLightClientHeader(kind: lcDataFork)
|
|
|
|
obj.forky(lcDataFork) = SSZ.decode(
|
|
|
|
header[1], lcDataFork.LightClientHeader)
|
|
|
|
return obj
|
|
|
|
warn "Unsupported LC store kind", store = "headers",
|
|
|
|
key, kind = header[0]
|
|
|
|
return default(ForkedLightClientHeader)
|
2023-06-15 16:29:13 +00:00
|
|
|
except SerializationError as exc:
|
2022-11-30 03:45:03 +00:00
|
|
|
error "LC store corrupted", store = "headers",
|
2023-01-16 15:53:45 +00:00
|
|
|
key, kind = header[0], exc = exc.msg
|
|
|
|
return default(ForkedLightClientHeader)
|
2022-11-30 03:45:03 +00:00
|
|
|
|
2023-01-18 02:06:23 +00:00
|
|
|
if distinctBase(db.headers.getStmt) != nil:
|
|
|
|
for res in db.headers.getStmt.exec(key.int64, header):
|
2023-02-24 19:50:46 +00:00
|
|
|
res.expect("SQL query OK")
|
|
|
|
return processHeader()
|
|
|
|
if distinctBase(db.legacyHeaders.getStmt) != nil:
|
2023-01-18 02:06:23 +00:00
|
|
|
for res in db.legacyHeaders.getStmt.exec(key.int64, header):
|
2023-02-24 19:50:46 +00:00
|
|
|
res.expect("SQL query OK")
|
|
|
|
return processHeader()
|
|
|
|
default(ForkedLightClientHeader)
|
2023-01-18 02:06:23 +00:00
|
|
|
|
2022-11-30 03:45:03 +00:00
|
|
|
func putLatestFinalizedHeader*(
|
2023-01-16 15:53:45 +00:00
|
|
|
db: LightClientDB, header: ForkedLightClientHeader) =
|
2023-01-18 02:06:23 +00:00
|
|
|
doAssert not db.backend.readOnly # All `stmt` are non-nil
|
2023-01-16 15:53:45 +00:00
|
|
|
withForkyHeader(header):
|
|
|
|
when lcDataFork > LightClientDataFork.None:
|
|
|
|
block:
|
|
|
|
const key = LightClientHeaderKey.Finalized
|
|
|
|
block:
|
|
|
|
let res = db.headers.putStmt.exec(
|
|
|
|
(key.int64, lcDataFork.int64, SSZ.encode(forkyHeader)))
|
|
|
|
res.expect("SQL query OK")
|
|
|
|
when lcDataFork == LightClientDataFork.Altair:
|
|
|
|
let res = db.legacyHeaders.putStmt.exec(
|
|
|
|
(key.int64, SSZ.encode(forkyHeader)))
|
|
|
|
res.expect("SQL query OK")
|
2023-01-18 02:06:23 +00:00
|
|
|
else:
|
|
|
|
# Keep legacy table at best Altair header.
|
|
|
|
discard
|
2023-01-16 15:53:45 +00:00
|
|
|
block:
|
|
|
|
let period = forkyHeader.beacon.slot.sync_committee_period
|
|
|
|
doAssert period.isSupportedBySQLite
|
|
|
|
let res = db.syncCommittees.keepFromStmt.exec(period.int64)
|
|
|
|
res.expect("SQL query OK")
|
|
|
|
else: raiseAssert "Cannot store empty `LightClientHeader`"
|
2022-11-30 03:45:03 +00:00
|
|
|
|
|
|
|
func initSyncCommitteesStore(
|
|
|
|
backend: SqStoreRef,
|
|
|
|
name: string): KvResult[SyncCommitteeStore] =
|
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`
|
|
|
|
`sync_committee` BLOB -- `altair.SyncCommittee` (SSZ)
|
|
|
|
);
|
|
|
|
""")
|
|
|
|
if not ? backend.hasTable(name):
|
2023-01-18 02:06:23 +00:00
|
|
|
return ok SyncCommitteeStore()
|
|
|
|
|
2022-11-30 03:45:03 +00:00
|
|
|
let
|
|
|
|
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(
|
|
|
|
getStmt: getStmt,
|
|
|
|
putStmt: putStmt,
|
|
|
|
keepFromStmt: keepFromStmt)
|
|
|
|
|
2023-01-18 02:06:23 +00:00
|
|
|
func close(store: var SyncCommitteeStore) =
|
|
|
|
store.getStmt.disposeSafe()
|
|
|
|
store.putStmt.disposeSafe()
|
|
|
|
store.keepFromStmt.disposeSafe()
|
2022-11-30 03:45:03 +00:00
|
|
|
|
|
|
|
proc getSyncCommittee*(
|
|
|
|
db: LightClientDB, period: SyncCommitteePeriod): Opt[altair.SyncCommittee] =
|
|
|
|
doAssert period.isSupportedBySQLite
|
2023-01-18 02:06:23 +00:00
|
|
|
if distinctBase(db.syncCommittees.getStmt) == nil:
|
|
|
|
return Opt.none(altair.SyncCommittee)
|
2022-11-30 03:45:03 +00:00
|
|
|
var syncCommittee: seq[byte]
|
|
|
|
for res in db.syncCommittees.getStmt.exec(period.int64, syncCommittee):
|
|
|
|
res.expect("SQL query OK")
|
|
|
|
try:
|
|
|
|
return ok SSZ.decode(syncCommittee, altair.SyncCommittee)
|
2023-06-15 16:29:13 +00:00
|
|
|
except SerializationError as exc:
|
2022-11-30 03:45:03 +00:00
|
|
|
error "LC store corrupted", store = "syncCommittees",
|
|
|
|
period, exc = exc.msg
|
2023-01-16 15:53:45 +00:00
|
|
|
return Opt.none(altair.SyncCommittee)
|
2022-11-30 03:45:03 +00:00
|
|
|
|
|
|
|
func putSyncCommittee*(
|
|
|
|
db: LightClientDB, period: SyncCommitteePeriod,
|
|
|
|
syncCommittee: altair.SyncCommittee) =
|
2023-01-18 02:06:23 +00:00
|
|
|
doAssert not db.backend.readOnly # All `stmt` are non-nil
|
2022-11-30 03:45:03 +00:00
|
|
|
doAssert period.isSupportedBySQLite
|
|
|
|
let res = db.syncCommittees.putStmt.exec(
|
|
|
|
(period.int64, SSZ.encode(syncCommittee)))
|
|
|
|
res.expect("SQL query OK")
|
|
|
|
|
|
|
|
type LightClientDBNames* = object
|
2023-01-16 15:53:45 +00:00
|
|
|
legacyAltairHeaders*: string
|
|
|
|
headers*: string
|
2022-11-30 03:45:03 +00:00
|
|
|
altairSyncCommittees*: string
|
|
|
|
|
2023-01-16 15:53:45 +00:00
|
|
|
proc initLightClientDB*(
|
2022-11-30 03:45:03 +00:00
|
|
|
backend: SqStoreRef,
|
|
|
|
names: LightClientDBNames): KvResult[LightClientDB] =
|
|
|
|
let
|
2023-01-16 15:53:45 +00:00
|
|
|
legacyHeaders =
|
|
|
|
? backend.initLegacyLightClientHeadersStore(names.legacyAltairHeaders)
|
2022-11-30 03:45:03 +00:00
|
|
|
headers =
|
2023-01-16 15:53:45 +00:00
|
|
|
? backend.initLightClientHeadersStore(
|
|
|
|
names.headers, names.legacyAltairHeaders)
|
2022-11-30 03:45:03 +00:00
|
|
|
syncCommittees =
|
|
|
|
? backend.initSyncCommitteesStore(names.altairSyncCommittees)
|
|
|
|
|
|
|
|
ok LightClientDB(
|
|
|
|
backend: backend,
|
2023-01-16 15:53:45 +00:00
|
|
|
legacyHeaders: legacyHeaders,
|
2022-11-30 03:45:03 +00:00
|
|
|
headers: headers,
|
|
|
|
syncCommittees: syncCommittees)
|
|
|
|
|
|
|
|
func close*(db: LightClientDB) =
|
|
|
|
if db.backend != nil:
|
2023-01-16 15:53:45 +00:00
|
|
|
db.legacyHeaders.close()
|
2022-11-30 03:45:03 +00:00
|
|
|
db.headers.close()
|
|
|
|
db.syncCommittees.close()
|
|
|
|
db[].reset()
|