diff --git a/beacon_chain/spec/datatypes/altair.nim b/beacon_chain/spec/datatypes/altair.nim index c8cfaf9e9..6a65cec4c 100644 --- a/beacon_chain/spec/datatypes/altair.nim +++ b/beacon_chain/spec/datatypes/altair.nim @@ -743,71 +743,6 @@ chronicles.formatIt LightClientUpdate: shortLog(it) chronicles.formatIt LightClientFinalityUpdate: 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) = info.validators.setLen(0) info.balances = UnslashedParticipatingBalances() diff --git a/beacon_chain/spec/datatypes/capella.nim b/beacon_chain/spec/datatypes/capella.nim index 6d8a325cc..f07183471 100644 --- a/beacon_chain/spec/datatypes/capella.nim +++ b/beacon_chain/spec/datatypes/capella.nim @@ -640,71 +640,6 @@ chronicles.formatIt LightClientUpdate: shortLog(it) chronicles.formatIt LightClientFinalityUpdate: 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 func upgrade_lc_store_to_capella*( pre: altair.LightClientStore): LightClientStore = diff --git a/beacon_chain/spec/datatypes/eip4844.nim b/beacon_chain/spec/datatypes/eip4844.nim index 0b3f4b199..2b8e51c62 100644 --- a/beacon_chain/spec/datatypes/eip4844.nim +++ b/beacon_chain/spec/datatypes/eip4844.nim @@ -653,71 +653,6 @@ chronicles.formatIt LightClientUpdate: shortLog(it) chronicles.formatIt LightClientFinalityUpdate: 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 func upgrade_lc_store_to_eip4844*( pre: capella.LightClientStore): LightClientStore = diff --git a/beacon_chain/spec/forks_light_client.nim b/beacon_chain/spec/forks_light_client.nim index 9c47d24e8..3bbb9f541 100644 --- a/beacon_chain/spec/forks_light_client.nim +++ b/beacon_chain/spec/forks_light_client.nim @@ -461,7 +461,25 @@ template withForkyStore*( const lcDataFork {.inject, used.} = LightClientDataFork.None 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 = when update is ForkyLightClientUpdate: update @@ -475,7 +493,25 @@ template toFull*( else: 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 = when update is ForkyLightClientFinalityUpdate: update @@ -489,7 +525,18 @@ template toFinality*( else: 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 = when update is ForkyLightClientOptimisticUpdate: update @@ -503,6 +550,28 @@ template toOptimistic*( else: 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 = if a.kind != b.kind: return false