combine common implementation of LC helpers (#4542)

* combine common implementation of LC helpers

Combine replicated helper code from Altair/Capella/EIP4844 into single
`Forky` based implementation. Also convert `template` to `func` to avoid
selection of incorrect overload.

* fix
This commit is contained in:
Etan Kissling 2023-01-23 20:38:03 +01:00 committed by GitHub
parent 285eec6512
commit 0b8bb11c50
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 72 additions and 198 deletions

View File

@ -743,71 +743,6 @@ chronicles.formatIt LightClientUpdate: shortLog(it)
chronicles.formatIt LightClientFinalityUpdate: shortLog(it) chronicles.formatIt LightClientFinalityUpdate: shortLog(it)
chronicles.formatIt LightClientOptimisticUpdate: shortLog(it) chronicles.formatIt LightClientOptimisticUpdate: shortLog(it)
template toFull*(
update: SomeLightClientUpdate): LightClientUpdate =
when update is LightClientUpdate:
update
elif update is SomeLightClientUpdateWithFinality:
LightClientUpdate(
attested_header: update.attested_header,
finalized_header: update.finalized_header,
finality_branch: update.finality_branch,
sync_aggregate: update.sync_aggregate,
signature_slot: update.signature_slot)
else:
LightClientUpdate(
attested_header: update.attested_header,
sync_aggregate: update.sync_aggregate,
signature_slot: update.signature_slot)
template toFinality*(
update: SomeLightClientUpdate): LightClientFinalityUpdate =
when update is LightClientFinalityUpdate:
update
elif update is SomeLightClientUpdateWithFinality:
LightClientFinalityUpdate(
attested_header: update.attested_header,
finalized_header: update.finalized_header,
finality_branch: update.finality_branch,
sync_aggregate: update.sync_aggregate,
signature_slot: update.signature_slot)
else:
LightClientFinalityUpdate(
attested_header: update.attested_header,
sync_aggregate: update.sync_aggregate,
signature_slot: update.signature_slot)
template toOptimistic*(
update: SomeLightClientUpdate): LightClientOptimisticUpdate =
when update is LightClientOptimisticUpdate:
update
else:
LightClientOptimisticUpdate(
attested_header: update.attested_header,
sync_aggregate: update.sync_aggregate,
signature_slot: update.signature_slot)
func matches*[A, B: SomeLightClientUpdate](a: A, b: B): bool =
if a.attested_header != b.attested_header:
return false
when a is SomeLightClientUpdateWithSyncCommittee and
b is SomeLightClientUpdateWithSyncCommittee:
if a.next_sync_committee != b.next_sync_committee:
return false
if a.next_sync_committee_branch != b.next_sync_committee_branch:
return false
when a is SomeLightClientUpdateWithFinality and
b is SomeLightClientUpdateWithFinality:
if a.finalized_header != b.finalized_header:
return false
if a.finality_branch != b.finality_branch:
return false
if a.sync_aggregate != b.sync_aggregate:
return false
if a.signature_slot != b.signature_slot:
return false
true
func clear*(info: var EpochInfo) = func clear*(info: var EpochInfo) =
info.validators.setLen(0) info.validators.setLen(0)
info.balances = UnslashedParticipatingBalances() info.balances = UnslashedParticipatingBalances()

View File

@ -640,71 +640,6 @@ chronicles.formatIt LightClientUpdate: shortLog(it)
chronicles.formatIt LightClientFinalityUpdate: shortLog(it) chronicles.formatIt LightClientFinalityUpdate: shortLog(it)
chronicles.formatIt LightClientOptimisticUpdate: shortLog(it) chronicles.formatIt LightClientOptimisticUpdate: shortLog(it)
template toFull*(
update: SomeLightClientUpdate): LightClientUpdate =
when update is LightClientUpdate:
update
elif update is SomeLightClientUpdateWithFinality:
LightClientUpdate(
attested_header: update.attested_header,
finalized_header: update.finalized_header,
finality_branch: update.finality_branch,
sync_aggregate: update.sync_aggregate,
signature_slot: update.signature_slot)
else:
LightClientUpdate(
attested_header: update.attested_header,
sync_aggregate: update.sync_aggregate,
signature_slot: update.signature_slot)
template toFinality*(
update: SomeLightClientUpdate): LightClientFinalityUpdate =
when update is LightClientFinalityUpdate:
update
elif update is SomeLightClientUpdateWithFinality:
LightClientFinalityUpdate(
attested_header: update.attested_header,
finalized_header: update.finalized_header,
finality_branch: update.finality_branch,
sync_aggregate: update.sync_aggregate,
signature_slot: update.signature_slot)
else:
LightClientFinalityUpdate(
attested_header: update.attested_header,
sync_aggregate: update.sync_aggregate,
signature_slot: update.signature_slot)
template toOptimistic*(
update: SomeLightClientUpdate): LightClientOptimisticUpdate =
when update is LightClientOptimisticUpdate:
update
else:
LightClientOptimisticUpdate(
attested_header: update.attested_header,
sync_aggregate: update.sync_aggregate,
signature_slot: update.signature_slot)
func matches*[A, B: SomeLightClientUpdate](a: A, b: B): bool =
if a.attested_header != b.attested_header:
return false
when a is SomeLightClientUpdateWithSyncCommittee and
b is SomeLightClientUpdateWithSyncCommittee:
if a.next_sync_committee != b.next_sync_committee:
return false
if a.next_sync_committee_branch != b.next_sync_committee_branch:
return false
when a is SomeLightClientUpdateWithFinality and
b is SomeLightClientUpdateWithFinality:
if a.finalized_header != b.finalized_header:
return false
if a.finality_branch != b.finality_branch:
return false
if a.sync_aggregate != b.sync_aggregate:
return false
if a.signature_slot != b.signature_slot:
return false
true
# https://github.com/ethereum/consensus-specs/blob/v1.3.0-rc.1/specs/capella/light-client/fork.md#upgrade_lc_store_to_capella # https://github.com/ethereum/consensus-specs/blob/v1.3.0-rc.1/specs/capella/light-client/fork.md#upgrade_lc_store_to_capella
func upgrade_lc_store_to_capella*( func upgrade_lc_store_to_capella*(
pre: altair.LightClientStore): LightClientStore = pre: altair.LightClientStore): LightClientStore =

View File

@ -653,71 +653,6 @@ chronicles.formatIt LightClientUpdate: shortLog(it)
chronicles.formatIt LightClientFinalityUpdate: shortLog(it) chronicles.formatIt LightClientFinalityUpdate: shortLog(it)
chronicles.formatIt LightClientOptimisticUpdate: shortLog(it) chronicles.formatIt LightClientOptimisticUpdate: shortLog(it)
template toFull*(
update: SomeLightClientUpdate): LightClientUpdate =
when update is LightClientUpdate:
update
elif update is SomeLightClientUpdateWithFinality:
LightClientUpdate(
attested_header: update.attested_header,
finalized_header: update.finalized_header,
finality_branch: update.finality_branch,
sync_aggregate: update.sync_aggregate,
signature_slot: update.signature_slot)
else:
LightClientUpdate(
attested_header: update.attested_header,
sync_aggregate: update.sync_aggregate,
signature_slot: update.signature_slot)
template toFinality*(
update: SomeLightClientUpdate): LightClientFinalityUpdate =
when update is LightClientFinalityUpdate:
update
elif update is SomeLightClientUpdateWithFinality:
LightClientFinalityUpdate(
attested_header: update.attested_header,
finalized_header: update.finalized_header,
finality_branch: update.finality_branch,
sync_aggregate: update.sync_aggregate,
signature_slot: update.signature_slot)
else:
LightClientFinalityUpdate(
attested_header: update.attested_header,
sync_aggregate: update.sync_aggregate,
signature_slot: update.signature_slot)
template toOptimistic*(
update: SomeLightClientUpdate): LightClientOptimisticUpdate =
when update is LightClientOptimisticUpdate:
update
else:
LightClientOptimisticUpdate(
attested_header: update.attested_header,
sync_aggregate: update.sync_aggregate,
signature_slot: update.signature_slot)
func matches*[A, B: SomeLightClientUpdate](a: A, b: B): bool =
if a.attested_header != b.attested_header:
return false
when a is SomeLightClientUpdateWithSyncCommittee and
b is SomeLightClientUpdateWithSyncCommittee:
if a.next_sync_committee != b.next_sync_committee:
return false
if a.next_sync_committee_branch != b.next_sync_committee_branch:
return false
when a is SomeLightClientUpdateWithFinality and
b is SomeLightClientUpdateWithFinality:
if a.finalized_header != b.finalized_header:
return false
if a.finality_branch != b.finality_branch:
return false
if a.sync_aggregate != b.sync_aggregate:
return false
if a.signature_slot != b.signature_slot:
return false
true
# https://github.com/ethereum/consensus-specs/blob/v1.3.0-rc.1/specs/eip4844/light-client/fork.md#upgrade_lc_store_to_eip4844 # https://github.com/ethereum/consensus-specs/blob/v1.3.0-rc.1/specs/eip4844/light-client/fork.md#upgrade_lc_store_to_eip4844
func upgrade_lc_store_to_eip4844*( func upgrade_lc_store_to_eip4844*(
pre: capella.LightClientStore): LightClientStore = pre: capella.LightClientStore): LightClientStore =

View File

@ -461,7 +461,25 @@ template withForkyStore*(
const lcDataFork {.inject, used.} = LightClientDataFork.None const lcDataFork {.inject, used.} = LightClientDataFork.None
body body
template toFull*( func toFull*(
update: SomeForkyLightClientUpdate): auto =
type ResultType = typeof(update).kind.LightClientUpdate
when update is ForkyLightClientUpdate:
update
elif update is SomeForkyLightClientUpdateWithFinality:
ResultType(
attested_header: update.attested_header,
finalized_header: update.finalized_header,
finality_branch: update.finality_branch,
sync_aggregate: update.sync_aggregate,
signature_slot: update.signature_slot)
else:
ResultType(
attested_header: update.attested_header,
sync_aggregate: update.sync_aggregate,
signature_slot: update.signature_slot)
func toFull*(
update: SomeForkedLightClientUpdate): ForkedLightClientUpdate = update: SomeForkedLightClientUpdate): ForkedLightClientUpdate =
when update is ForkyLightClientUpdate: when update is ForkyLightClientUpdate:
update update
@ -475,7 +493,25 @@ template toFull*(
else: else:
default(ForkedLightClientUpdate) default(ForkedLightClientUpdate)
template toFinality*( func toFinality*(
update: SomeForkyLightClientUpdate): auto =
type ResultType = typeof(update).kind.LightClientFinalityUpdate
when update is ForkyLightClientFinalityUpdate:
update
elif update is SomeForkyLightClientUpdateWithFinality:
ResultType(
attested_header: update.attested_header,
finalized_header: update.finalized_header,
finality_branch: update.finality_branch,
sync_aggregate: update.sync_aggregate,
signature_slot: update.signature_slot)
else:
ResultType(
attested_header: update.attested_header,
sync_aggregate: update.sync_aggregate,
signature_slot: update.signature_slot)
func toFinality*(
update: SomeForkedLightClientUpdate): ForkedLightClientFinalityUpdate = update: SomeForkedLightClientUpdate): ForkedLightClientFinalityUpdate =
when update is ForkyLightClientFinalityUpdate: when update is ForkyLightClientFinalityUpdate:
update update
@ -489,7 +525,18 @@ template toFinality*(
else: else:
default(ForkedLightClientFinalityUpdate) default(ForkedLightClientFinalityUpdate)
template toOptimistic*( func toOptimistic*(
update: SomeForkyLightClientUpdate): auto =
type ResultType = typeof(update).kind.LightClientOptimisticUpdate
when update is ForkyLightClientOptimisticUpdate:
update
else:
ResultType(
attested_header: update.attested_header,
sync_aggregate: update.sync_aggregate,
signature_slot: update.signature_slot)
func toOptimistic*(
update: SomeForkedLightClientUpdate): ForkedLightClientOptimisticUpdate = update: SomeForkedLightClientUpdate): ForkedLightClientOptimisticUpdate =
when update is ForkyLightClientOptimisticUpdate: when update is ForkyLightClientOptimisticUpdate:
update update
@ -503,6 +550,28 @@ template toOptimistic*(
else: else:
default(ForkedLightClientOptimisticUpdate) default(ForkedLightClientOptimisticUpdate)
func matches*[A, B: SomeForkyLightClientUpdate](a: A, b: B): bool =
static: doAssert typeof(A).kind == typeof(B).kind
if a.attested_header != b.attested_header:
return false
when a is SomeForkyLightClientUpdateWithSyncCommittee and
b is SomeForkyLightClientUpdateWithSyncCommittee:
if a.next_sync_committee != b.next_sync_committee:
return false
if a.next_sync_committee_branch != b.next_sync_committee_branch:
return false
when a is SomeForkyLightClientUpdateWithFinality and
b is SomeForkyLightClientUpdateWithFinality:
if a.finalized_header != b.finalized_header:
return false
if a.finality_branch != b.finality_branch:
return false
if a.sync_aggregate != b.sync_aggregate:
return false
if a.signature_slot != b.signature_slot:
return false
true
func matches*[A, B: SomeForkedLightClientUpdate](a: A, b: B): bool = func matches*[A, B: SomeForkedLightClientUpdate](a: A, b: B): bool =
if a.kind != b.kind: if a.kind != b.kind:
return false return false