ncli_db: fix readonly properly (#4543)

🐡

Co-authored-by: Etan Kissling <etan@status.im>
This commit is contained in:
Jacek Sieka 2023-01-24 03:52:08 +01:00 committed by GitHub
parent 0b8bb11c50
commit ba0496105a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 75 additions and 75 deletions

View File

@ -100,16 +100,16 @@ template disposeSafe(s: untyped): untyped =
proc initCurrentBranchesStore(
backend: SqStoreRef,
name: string): KvResult[CurrentSyncCommitteeBranchStore] =
if backend.readOnly and not ? backend.hasTable(name):
if not backend.readOnly:
? backend.exec("""
CREATE TABLE IF NOT EXISTS `""" & name & """` (
`slot` INTEGER PRIMARY KEY, -- `Slot` (up through 2^63-1)
`branch` BLOB -- `altair.CurrentSyncCommitteeBranch` (SSZ)
);
""")
if not ? backend.hasTable(name):
return ok CurrentSyncCommitteeBranchStore()
? backend.exec("""
CREATE TABLE IF NOT EXISTS `""" & name & """` (
`slot` INTEGER PRIMARY KEY, -- `Slot` (up through 2^63-1)
`branch` BLOB -- `altair.CurrentSyncCommitteeBranch` (SSZ)
);
""")
let
containsStmt = backend.prepareStmt("""
SELECT 1 AS `exists`
@ -183,16 +183,16 @@ proc initLegacyBestUpdatesStore(
backend: SqStoreRef,
name: string,
): KvResult[LegacyBestLightClientUpdateStore] =
if backend.readOnly and not ? backend.hasTable(name):
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):
return ok LegacyBestLightClientUpdateStore()
? backend.exec("""
CREATE TABLE IF NOT EXISTS `""" & name & """` (
`period` INTEGER PRIMARY KEY, -- `SyncCommitteePeriod`
`update` BLOB -- `altair.LightClientUpdate` (SSZ)
);
""")
const legacyKind = Base10.toString(ord(LightClientDataFork.Altair).uint)
let
getStmt = backend.prepareStmt("""
@ -236,26 +236,26 @@ proc initBestUpdatesStore(
backend: SqStoreRef,
name, legacyAltairName: string,
): KvResult[BestLightClientUpdateStore] =
if backend.readOnly and not ? backend.hasTable(name):
return ok BestLightClientUpdateStore()
? backend.exec("""
CREATE TABLE IF NOT EXISTS `""" & name & """` (
`period` INTEGER PRIMARY KEY, -- `SyncCommitteePeriod`
`kind` INTEGER, -- `LightClientDataFork`
`update` BLOB -- `LightClientUpdate` (SSZ)
);
""")
if ? backend.hasTable(legacyAltairName):
# SyncCommitteePeriod -> altair.LightClientUpdate
const legacyKind = Base10.toString(ord(LightClientDataFork.Altair).uint)
if not backend.readOnly:
? backend.exec("""
INSERT OR IGNORE INTO `""" & name & """` (
`period`, `kind`, `update`
)
SELECT `period`, """ & legacyKind & """ AS `kind`, `update`
FROM `""" & legacyAltairName & """`;
CREATE TABLE IF NOT EXISTS `""" & name & """` (
`period` INTEGER PRIMARY KEY, -- `SyncCommitteePeriod`
`kind` INTEGER, -- `LightClientDataFork`
`update` BLOB -- `LightClientUpdate` (SSZ)
);
""")
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()
let
getStmt = backend.prepareStmt("""
@ -375,15 +375,15 @@ proc putUpdateIfBetter*(
proc initSealedPeriodsStore(
backend: SqStoreRef,
name: string): KvResult[SealedSyncCommitteePeriodStore] =
if backend.readOnly and not ? backend.hasTable(name):
if not backend.readOnly:
? backend.exec("""
CREATE TABLE IF NOT EXISTS `""" & name & """` (
`period` INTEGER PRIMARY KEY -- `SyncCommitteePeriod`
);
""")
if not ? backend.hasTable(name):
return ok SealedSyncCommitteePeriodStore()
? backend.exec("""
CREATE TABLE IF NOT EXISTS `""" & name & """` (
`period` INTEGER PRIMARY KEY -- `SyncCommitteePeriod`
);
""")
let
containsStmt = backend.prepareStmt("""
SELECT 1 AS `exists`

View File

@ -65,16 +65,16 @@ template disposeSafe(s: untyped): untyped =
proc initLegacyLightClientHeadersStore(
backend: SqStoreRef,
name: string): KvResult[LegacyLightClientHeadersStore] =
if backend.readOnly and not ? backend.hasTable(name):
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):
return ok LegacyLightClientHeadersStore()
? backend.exec("""
CREATE TABLE IF NOT EXISTS `""" & name & """` (
`kind` INTEGER PRIMARY KEY, -- `LightClientHeaderKey`
`header` BLOB -- `altair.LightClientHeader` (SSZ)
);
""")
const legacyKind = Base10.toString(ord(LightClientDataFork.Altair).uint)
let
getStmt = backend.prepareStmt("""
@ -100,26 +100,26 @@ func close(store: var LegacyLightClientHeadersStore) =
proc initLightClientHeadersStore(
backend: SqStoreRef,
name, legacyAltairName: string): KvResult[LightClientHeadersStore] =
if backend.readOnly and not ? backend.hasTable(name):
return ok LightClientHeadersStore()
? backend.exec("""
CREATE TABLE IF NOT EXISTS `""" & name & """` (
`key` INTEGER PRIMARY KEY, -- `LightClientHeaderKey`
`kind` INTEGER, -- `LightClientDataFork`
`header` BLOB -- `LightClientHeader` (SSZ)
);
""")
if ? backend.hasTable(legacyAltairName):
# LightClientHeaderKey -> altair.LightClientHeader
const legacyKind = Base10.toString(ord(LightClientDataFork.Altair).uint)
if not backend.readOnly:
? backend.exec("""
INSERT OR IGNORE INTO `""" & name & """` (
`key`, `kind`, `header`
)
SELECT `kind` AS `key`, """ & legacyKind & """ AS `kind`, `header`
FROM `""" & legacyAltairName & """`;
CREATE TABLE IF NOT EXISTS `""" & name & """` (
`key` INTEGER PRIMARY KEY, -- `LightClientHeaderKey`
`kind` INTEGER, -- `LightClientDataFork`
`header` BLOB -- `LightClientHeader` (SSZ)
);
""")
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()
let
getStmt = backend.prepareStmt("""
@ -202,16 +202,16 @@ func putLatestFinalizedHeader*(
func initSyncCommitteesStore(
backend: SqStoreRef,
name: string): KvResult[SyncCommitteeStore] =
if backend.readOnly and not ? backend.hasTable(name):
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()
? backend.exec("""
CREATE TABLE IF NOT EXISTS `""" & name & """` (
`period` INTEGER PRIMARY KEY, -- `SyncCommitteePeriod`
`sync_committee` BLOB -- `altair.SyncCommittee` (SSZ)
);
""")
let
getStmt = backend.prepareStmt("""
SELECT `sync_committee`