Clean-up/refactor in beacon lc db (#1830)

This commit is contained in:
Kim De Mey 2023-10-19 15:47:29 +02:00 committed by GitHub
parent 20db41878d
commit 14ee5f6820
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 36 additions and 42 deletions

View File

@ -248,57 +248,55 @@ proc putUpdateIfBetter*(
proc createGetHandler*(db: LightClientDb): DbGetHandler = proc createGetHandler*(db: LightClientDb): DbGetHandler =
return ( return (
proc(contentKey: ByteList, contentId: ContentId): results.Opt[seq[byte]] = proc(contentKey: ByteList, contentId: ContentId): results.Opt[seq[byte]] =
let contentKeyResult = decode(contentKey) let contentKey = contentKey.decode().valueOr:
# TODO: as this should not fail, maybe it is better to raiseAssert ? # TODO: as this should not fail, maybe it is better to raiseAssert ?
if contentKeyResult.isNone():
return Opt.none(seq[byte]) return Opt.none(seq[byte])
let ck = contentKeyResult.get() case contentKey.contentType:
of lightClientBootstrap:
if ck.contentType == lightClientUpdate: db.get(contentId)
of lightClientUpdate:
let let
# TODO: add validation that startPeriod is not from the future, # TODO: add validation that startPeriod is not from the future,
# this requires db to be aware off the current beacon time # this requires db to be aware off the current beacon time
startPeriod = ck.lightClientUpdateKey.startPeriod startPeriod = contentKey.lightClientUpdateKey.startPeriod
# get max 128 updates # get max 128 updates
numOfUpdates = min( numOfUpdates = min(
uint64(MAX_REQUEST_LIGHT_CLIENT_UPDATES), uint64(MAX_REQUEST_LIGHT_CLIENT_UPDATES),
ck.lightClientUpdateKey.count contentKey.lightClientUpdateKey.count
) )
to = startPeriod + numOfUpdates toPeriod = startPeriod + numOfUpdates # Not inclusive
updates = db.getLightClientUpdates(startPeriod, to) updates = db.getLightClientUpdates(startPeriod, toPeriod)
if len(updates) == 0: if len(updates) == 0:
return Opt.none(seq[byte]) Opt.none(seq[byte])
else: else:
return ok(SSZ.encode(updates)) Opt.some(SSZ.encode(updates))
elif ck.contentType == lightClientFinalityUpdate: of lightClientFinalityUpdate:
# TODO: # TODO:
# Return only when the update is better than what is requested by # Return only when the update is better than what is requested by
# contentKey. This is currently not possible as the contentKey does not # contentKey. This is currently not possible as the contentKey does not
# include best update information. # include best update information.
if db.finalityUpdateCache.isSome(): if db.finalityUpdateCache.isSome():
let slot = ck.lightClientFinalityUpdateKey.finalizedSlot let slot = contentKey.lightClientFinalityUpdateKey.finalizedSlot
let cache = db.finalityUpdateCache.get() let cache = db.finalityUpdateCache.get()
if cache.lastFinalityUpdateSlot >= slot: if cache.lastFinalityUpdateSlot >= slot:
return Opt.some(cache.lastFinalityUpdate) Opt.some(cache.lastFinalityUpdate)
else: else:
return Opt.none(seq[byte]) Opt.none(seq[byte])
else: else:
return Opt.none(seq[byte]) Opt.none(seq[byte])
elif ck.contentType == lightClientOptimisticUpdate: of lightClientOptimisticUpdate:
# TODO same as above applies here too. # TODO same as above applies here too.
if db.optimisticUpdateCache.isSome(): if db.optimisticUpdateCache.isSome():
let slot = ck.lightClientOptimisticUpdateKey.optimisticSlot let slot = contentKey.lightClientOptimisticUpdateKey.optimisticSlot
let cache = db.optimisticUpdateCache.get() let cache = db.optimisticUpdateCache.get()
if cache.lastOptimisticUpdateSlot >= slot: if cache.lastOptimisticUpdateSlot >= slot:
return Opt.some(cache.lastOptimisticUpdate) Opt.some(cache.lastOptimisticUpdate)
else: else:
return Opt.none(seq[byte]) Opt.none(seq[byte])
else: else:
return Opt.none(seq[byte]) Opt.none(seq[byte])
else:
return db.get(contentId)
) )
proc createStoreHandler*(db: LightClientDb): DbStoreHandler = proc createStoreHandler*(db: LightClientDb): DbStoreHandler =
@ -306,43 +304,39 @@ proc createStoreHandler*(db: LightClientDb): DbStoreHandler =
contentKey: ByteList, contentKey: ByteList,
contentId: ContentId, contentId: ContentId,
content: seq[byte]) {.raises: [], gcsafe.} = content: seq[byte]) {.raises: [], gcsafe.} =
let contentKeyResult = decode(contentKey) let contentKey = decode(contentKey).valueOr:
# TODO: as this should not fail, maybe it is better to raiseAssert ? # TODO: as this should not fail, maybe it is better to raiseAssert ?
if contentKeyResult.isNone():
return return
let ck = contentKeyResult.get() case contentKey.contentType:
of lightClientBootstrap:
db.put(contentId, content)
of lightClientUpdate:
let updates =
decodeSsz(content, ForkedLightClientUpdateBytesList).valueOr:
return
if ck.contentType == lightClientUpdate:
# Lot of assumptions here: # Lot of assumptions here:
# - that updates are continious i.e there is no period gaps # - that updates are continious i.e there is no period gaps
# - that updates start from startPeriod of content key # - that updates start from startPeriod of content key
var period = ck.lightClientUpdateKey.startPeriod var period = contentKey.lightClientUpdateKey.startPeriod
let updatesResult = decodeSsz(content, ForkedLightClientUpdateBytesList)
if updatesResult.isErr:
return
let updates = updatesResult.get()
for update in updates.asSeq(): for update in updates.asSeq():
# Only put the update if it is better, although in currently a new offer # Only put the update if it is better, although in currently a new offer
# should not be accepted as it is based on only the period. # should not be accepted as it is based on only the period.
db.putUpdateIfBetter(SyncCommitteePeriod(period), update.asSeq()) db.putUpdateIfBetter(SyncCommitteePeriod(period), update.asSeq())
inc period inc period
elif ck.contentType == lightClientFinalityUpdate: of lightClientFinalityUpdate:
db.finalityUpdateCache = db.finalityUpdateCache =
Opt.some(LightClientFinalityUpdateCache( Opt.some(LightClientFinalityUpdateCache(
lastFinalityUpdateSlot: ck.lightClientFinalityUpdateKey.finalizedSlot, lastFinalityUpdateSlot:
contentKey.lightClientFinalityUpdateKey.finalizedSlot,
lastFinalityUpdate: content lastFinalityUpdate: content
)) ))
elif ck.contentType == lightClientOptimisticUpdate: of lightClientOptimisticUpdate:
db.optimisticUpdateCache = db.optimisticUpdateCache =
Opt.some(LightClientOptimisticUpdateCache( Opt.some(LightClientOptimisticUpdateCache(
lastOptimisticUpdateSlot: ck.lightClientOptimisticUpdateKey.optimisticSlot, lastOptimisticUpdateSlot:
contentKey.lightClientOptimisticUpdateKey.optimisticSlot,
lastOptimisticUpdate: content lastOptimisticUpdate: content
)) ))
else:
db.put(contentId, content)
) )