Skip to content

Commit

Permalink
Apply forced auth changes before executing block
Browse files Browse the repository at this point in the history
  • Loading branch information
dimartiro committed Dec 4, 2024
1 parent 6f5b4d8 commit 8a83ac2
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 10 deletions.
1 change: 1 addition & 0 deletions dot/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,7 @@ func (nodeBuilder) newSyncService(config *cfg.Config, st *state.Service, fg sync
BlockImportHandler: cs,
Telemetry: telemetryMailer,
BadBlocks: genesisData.BadBlocks,
GrandpaState: st.Grandpa,
RequestMaker: requestMaker,
}
fullSync := sync.NewFullSyncStrategy(syncCfg)
Expand Down
41 changes: 31 additions & 10 deletions dot/sync/block_importer.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ type (
VerifyBlockJustification(common.Hash, uint, []byte) (round uint64, setID uint64, err error)
}

// GrandpaState is the interface for the state.GrandpaState
GrandpaState interface {
ApplyForcedChanges(importedHeader *types.Header) error
}

// BlockImportHandler is the interface for the handler of newly imported blocks
BlockImportHandler interface {
HandleBlockImport(block *types.Block, state *rtstorage.TrieState, announce bool) error
Expand All @@ -64,6 +69,7 @@ type blockImporter struct {
transactionState TransactionState
babeVerifier BabeVerifier
finalityGadget FinalityGadget
grandpaState GrandpaState
blockImportHandler BlockImportHandler
telemetry Telemetry
}
Expand All @@ -75,6 +81,7 @@ func newBlockImporter(cfg *FullSyncConfig) *blockImporter {
transactionState: cfg.TransactionState,
babeVerifier: cfg.BabeVerifier,
finalityGadget: cfg.FinalityGadget,
grandpaState: cfg.GrandpaState,
blockImportHandler: cfg.BlockImportHandler,
telemetry: cfg.Telemetry,
}
Expand Down Expand Up @@ -104,31 +111,45 @@ func (b *blockImporter) importBlock(bd *types.BlockData, origin BlockOrigin) (im
// or the index of the block data that errored on failure.
func (b *blockImporter) processBlockData(blockData types.BlockData, origin BlockOrigin) error {
if blockData.Header != nil {
header := blockData.Header
var (
hasJustification = blockData.Justification != nil && len(*blockData.Justification) > 0
round uint64
setID uint64
)

if blockData.Body != nil {
err := b.processBlockDataWithHeaderAndBody(blockData, origin)
err := b.grandpaState.ApplyForcedChanges(blockData.Header)
if err != nil {
return fmt.Errorf("applying forced changes: %w", err)
}

if hasJustification {
var err error
round, setID, err = b.finalityGadget.VerifyBlockJustification(
blockData.Header.Hash(), blockData.Header.Number, *blockData.Justification)
if err != nil {
return fmt.Errorf("processing block data with header and body: %w", err)
return fmt.Errorf("verifying justification: %w", err)
}
}

if blockData.Justification != nil && len(*blockData.Justification) > 0 {
round, setID, err := b.finalityGadget.VerifyBlockJustification(
header.Hash(), header.Number, *blockData.Justification)
if blockData.Body != nil {
err := b.processBlockDataWithHeaderAndBody(blockData, origin)
if err != nil {
return fmt.Errorf("verifying justification for block %s: %w", header.Hash().String(), err)
return fmt.Errorf("processing block data with header and body: %w", err)
}
}

err = b.blockState.SetFinalisedHash(header.Hash(), round, setID)
if hasJustification {
header := blockData.Header
err := b.blockState.SetFinalisedHash(header.Hash(), round, setID)
if err != nil {
return fmt.Errorf("setting finalised hash: %w", err)
}

err = b.blockState.SetJustification(header.Hash(), *blockData.Justification)
if err != nil {
return fmt.Errorf("setting justification for block number %d: %w", header.Number, err)
}

return nil
}
}
err := b.blockState.CompareAndSetBlockData(&blockData)
Expand Down
1 change: 1 addition & 0 deletions dot/sync/fullsync.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type FullSyncConfig struct {
TransactionState TransactionState
BabeVerifier BabeVerifier
FinalityGadget FinalityGadget
GrandpaState GrandpaState
BlockImportHandler BlockImportHandler
Telemetry Telemetry
BlockState BlockState
Expand Down

0 comments on commit 8a83ac2

Please sign in to comment.