Skip to content

Commit f9fc433

Browse files
committed
cr feedback
1 parent 60b5d78 commit f9fc433

File tree

4 files changed

+70
-86
lines changed

4 files changed

+70
-86
lines changed

internal/client/consensus/grandpa/grandpa.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -237,13 +237,7 @@ func blockImportWithAuthoritySetHardForks[
237237
chainInfo := client.Info()
238238
genesisHash := chainInfo.GenesisHash
239239

240-
persistentData, err := loadPersistent[H, N](client, genesisHash, 0, func() (primitives.AuthorityList, error) {
241-
authorities, err := genesisAuthoritySetProvider.Get()
242-
if err != nil {
243-
return nil, err
244-
}
245-
return authorities, nil
246-
})
240+
persistentData, err := loadPersistent[H, N](client, genesisHash, 0, genesisAuthoritySetProvider.Get)
247241
if err != nil {
248242
return nil, LinkHalf[H, N, Hasher, Header, E]{}, err
249243
}

internal/client/consensus/grandpa/import.go

Lines changed: 59 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,7 @@ func newGrandpaBlockImport[
129129
setID := hardFork.SetID
130130
if setID == SetID(sharedAuthoritySet.SetID()) {
131131
authoritySet, unlock := sharedAuthoritySet.inner.DataMut()
132-
// authoritySet.mtx.Lock()
133132
authoritySet.CurrentAuthorities = hardFork.PendingChange.NextAuthorities
134-
// authoritySet.mtx.Unlock()
135133
unlock()
136134
}
137135
}
@@ -183,7 +181,7 @@ func FindScheduledChange[
183181
id := runtime.OpaqueDigestItemIDConsensus(grandpa.GrandpaEngineID)
184182

185183
filterLog := func(log grandpa.ConsensusLog) *grandpa.ScheduledChange[N] {
186-
scheduledChange, ok := log.(grandpa.ConensusLogScheduledChange[N])
184+
scheduledChange, ok := log.(grandpa.ConsensusLogScheduledChange[N])
187185
if !ok {
188186
return nil
189187
}
@@ -405,10 +403,7 @@ func (gbi *GrandpaBlockImport[H, N, Hasher, Header, E]) makeAuthoritiesChanges(
405403
// with. we use the minimum between the median and the local
406404
// best finalized block.
407405
bestFinalizedNumber := gbi.inner.Info().FinalizedNumber
408-
canonNumber := bestFinalizedNumber
409-
if medianLastFinalizedNumber < bestFinalizedNumber {
410-
canonNumber = medianLastFinalizedNumber
411-
}
406+
canonNumber := min(medianLastFinalizedNumber, bestFinalizedNumber)
412407
canonHash, err := gbi.inner.Hash(canonNumber)
413408
if err != nil {
414409
return nil, err
@@ -536,69 +531,66 @@ func (gbi *GrandpaBlockImport[H, N, Hasher, Header, E]) importState(
536531
// Force imported state finality.
537532
block.Finalized = true
538533
importResult, err := gbi.inner.ImportBlock(block)
539-
if err == nil {
540-
switch importResult := importResult.(type) {
541-
case client_common.ImportResultImported:
542-
aux := client_common.ImportedAux(importResult)
543-
// We've just imported a new state. We trust the sync module has verified
544-
// finality proofs and that the state is correct and final.
545-
// So we can read the authority list and set id from the state.
546-
gbi.authoritySetHardForksMtx.Lock()
547-
gbi.authoritySetHardForks = make(map[H]PendingChange[H, N])
548-
gbi.authoritySetHardForksMtx.Unlock()
549-
authorities, err := gbi.inner.RuntimeAPI().GrandpaAuthorities(hash)
550-
if err != nil {
551-
return nil, err
552-
}
553-
setID, err := gbi.currentSetID(hash)
554-
if err != nil {
555-
return nil, err
556-
}
557-
authoritySet, err := NewAuthoritySet[H, N](
558-
authorities,
559-
uint64(setID),
560-
forktree.NewForkTree[H, N, PendingChange[H, N]](),
561-
[]PendingChange[H, N]{},
562-
AuthoritySetChanges[N]{},
563-
)
564-
if err != nil {
565-
return nil, err
566-
}
534+
if err != nil {
535+
return nil, err
536+
}
537+
switch importResult := importResult.(type) {
538+
case client_common.ImportResultImported:
539+
aux := client_common.ImportedAux(importResult)
540+
// We've just imported a new state. We trust the sync module has verified
541+
// finality proofs and that the state is correct and final.
542+
// So we can read the authority list and set id from the state.
543+
gbi.authoritySetHardForksMtx.Lock()
544+
gbi.authoritySetHardForks = make(map[H]PendingChange[H, N])
545+
gbi.authoritySetHardForksMtx.Unlock()
546+
authorities, err := gbi.inner.RuntimeAPI().GrandpaAuthorities(hash)
547+
if err != nil {
548+
return nil, err
549+
}
550+
setID, err := gbi.currentSetID(hash)
551+
if err != nil {
552+
return nil, err
553+
}
554+
authoritySet, err := NewAuthoritySet[H, N](
555+
authorities,
556+
uint64(setID),
557+
forktree.NewForkTree[H, N, PendingChange[H, N]](),
558+
[]PendingChange[H, N]{},
559+
AuthoritySetChanges[N]{},
560+
)
561+
if err != nil {
562+
return nil, err
563+
}
567564

568-
locked := gbi.authoritySet.inner.Locked()
569-
*locked.MutRef() = authoritySet.Clone()
570-
defer locked.Unlock()
565+
locked := gbi.authoritySet.inner.Locked()
566+
*locked.MutRef() = authoritySet.Clone()
567+
defer locked.Unlock()
571568

572-
err = updateAuthoritySet(
573-
locked.Data(),
574-
nil,
575-
func(insertions []api.KeyValue) error {
576-
return gbi.inner.InsertAux(insertions, nil)
577-
},
578-
)
579-
if err != nil {
580-
return nil, err
581-
}
582-
newSet := newAuthoritySet[H, N]{
583-
CanonNumber: number,
584-
CanonHash: hash,
585-
SetID: setID,
586-
Authorities: authorities,
587-
}
588-
gbi.sendVoterCommands <- voterCommandChangeAuthorities[H, N](newSet)
589-
return client_common.ImportResultImported(aux), nil
590-
case client_common.ImportResultAlreadyInChain,
591-
client_common.ImportResultKnownBad,
592-
client_common.ImportResultMissingState,
593-
client_common.ImportResultUnknownParent:
594-
// Ok(r) => Ok(r),
595-
return importResult, nil
596-
default:
597-
panic("unreachable")
569+
err = updateAuthoritySet(
570+
locked.Data(),
571+
nil,
572+
func(insertions []api.KeyValue) error {
573+
return gbi.inner.InsertAux(insertions, nil)
574+
},
575+
)
576+
if err != nil {
577+
return nil, err
598578
}
599-
600-
} else {
601-
return nil, err
579+
newSet := newAuthoritySet[H, N]{
580+
CanonNumber: number,
581+
CanonHash: hash,
582+
SetID: setID,
583+
Authorities: authorities,
584+
}
585+
gbi.sendVoterCommands <- voterCommandChangeAuthorities[H, N](newSet)
586+
return client_common.ImportResultImported(aux), nil
587+
case client_common.ImportResultAlreadyInChain,
588+
client_common.ImportResultKnownBad,
589+
client_common.ImportResultMissingState,
590+
client_common.ImportResultUnknownParent:
591+
return importResult, nil
592+
default:
593+
panic("unreachable")
602594
}
603595
}
604596

@@ -712,7 +704,6 @@ func (gbi *GrandpaBlockImport[H, N, Hasher, Header, E]) ImportBlock(
712704
importedAux.ClearJustificationRequests = true
713705

714706
case importAppliedChangesStandard:
715-
// this is a standard change, we don't apply it yet, but we will send a
716707
// we can't apply this change yet since there are other dependent changes that we
717708
// need to apply first, drop any justification that might have been provided with
718709
// the block to make sure we request them from `sync` which will ensure they'll be

internal/primitives/consensus/grandpa/grandpa.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ type ConsensusLog interface {
116116
// This should be a pure function: i.e. as long as the runtime can interpret
117117
// the digest type it should return the same result regardless of the current
118118
// state.
119-
type ConensusLogScheduledChange[N runtime.Number] ScheduledChange[N]
119+
type ConsensusLogScheduledChange[N runtime.Number] ScheduledChange[N]
120120

121121
// Force an authority set change.
122122
//
@@ -152,19 +152,19 @@ type ConsensusLogResume[N runtime.Number] struct {
152152
Delay N
153153
}
154154

155-
func (ConensusLogScheduledChange[N]) isConsensusLog() {}
156-
func (ConsensusLogForcedChange[N]) isConsensusLog() {}
157-
func (ConsensusLogOnDisabled) isConsensusLog() {}
158-
func (ConsensusLogPause[N]) isConsensusLog() {}
159-
func (ConsensusLogResume[N]) isConsensusLog() {}
155+
func (ConsensusLogScheduledChange[N]) isConsensusLog() {}
156+
func (ConsensusLogForcedChange[N]) isConsensusLog() {}
157+
func (ConsensusLogOnDisabled) isConsensusLog() {}
158+
func (ConsensusLogPause[N]) isConsensusLog() {}
159+
func (ConsensusLogResume[N]) isConsensusLog() {}
160160

161161
type ConsensusLogVDT[N runtime.Number] struct {
162162
inner ConsensusLog
163163
}
164164

165165
func (mvdt *ConsensusLogVDT[N]) SetValue(value any) (err error) {
166166
switch value := value.(type) {
167-
case ConensusLogScheduledChange[N]:
167+
case ConsensusLogScheduledChange[N]:
168168
mvdt.inner = value
169169
return
170170
case ConsensusLogForcedChange[N]:
@@ -186,7 +186,7 @@ func (mvdt *ConsensusLogVDT[N]) SetValue(value any) (err error) {
186186

187187
func (mvdt ConsensusLogVDT[N]) IndexValue() (index uint, value any, err error) {
188188
switch mvdt.inner.(type) {
189-
case ConensusLogScheduledChange[N]:
189+
case ConsensusLogScheduledChange[N]:
190190
return 1, mvdt.inner, nil
191191
case ConsensusLogForcedChange[N]:
192192
return 2, mvdt.inner, nil
@@ -208,7 +208,7 @@ func (mvdt ConsensusLogVDT[N]) Value() (value any, err error) {
208208
func (mvdt ConsensusLogVDT[N]) ValueAt(index uint) (value any, err error) {
209209
switch index {
210210
case 1:
211-
return ConensusLogScheduledChange[N]{}, nil
211+
return ConsensusLogScheduledChange[N]{}, nil
212212
case 2:
213213
return ConsensusLogForcedChange[N]{}, nil
214214
case 3:

internal/primitives/runtime/runtime.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ func (j Justifications) Get(engineID ConsensusEngineID) *EncodedJustification {
4747
return nil
4848
}
4949

50-
// Return a copy of the encoded justification for the given consensus
51-
// engine, if it exists.
50+
// IntoJustification returns the encoded justification for the given consensus engine, if it exists.
5251
func (j Justifications) IntoJustification(enginedID ConsensusEngineID) *EncodedJustification {
5352
for _, justification := range j {
5453
if justification.ConsensusEngineID == enginedID {

0 commit comments

Comments
 (0)