-
Notifications
You must be signed in to change notification settings - Fork 179
/
follower.go
80 lines (70 loc) · 2.75 KB
/
follower.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package follower
import (
"errors"
"fmt"
"github.com/rs/zerolog"
"github.com/onflow/flow-go/consensus/hotstuff"
"github.com/onflow/flow-go/consensus/hotstuff/forks"
"github.com/onflow/flow-go/consensus/hotstuff/model"
"github.com/onflow/flow-go/utils/logging"
)
// FollowerLogic runs in non-consensus nodes. It informs other components within the node
// about finalization of blocks. The consensus Follower consumes all block proposals
// broadcasts by the consensus node, verifies the block header and locally evaluates
// the finalization rules.
//
// CAUTION: Follower is NOT CONCURRENCY safe
type FollowerLogic struct {
log zerolog.Logger
validator hotstuff.Validator
finalizationLogic forks.Finalizer
}
// New creates a new FollowerLogic instance
func New(
log zerolog.Logger,
validator hotstuff.Validator,
finalizationLogic forks.Finalizer,
) (*FollowerLogic, error) {
return &FollowerLogic{
log: log.With().Str("hotstuff", "follower").Logger(),
validator: validator,
finalizationLogic: finalizationLogic,
}, nil
}
// FinalizedBlock returns the latest finalized block
func (f *FollowerLogic) FinalizedBlock() *model.Block {
return f.finalizationLogic.FinalizedBlock()
}
// AddBlock processes the given block proposal
func (f *FollowerLogic) AddBlock(blockProposal *model.Proposal) error {
// validate the block. skip if the proposal is invalid
err := f.validator.ValidateProposal(blockProposal)
if model.IsInvalidBlockError(err) {
f.log.Warn().Err(err).Hex("block_id", logging.ID(blockProposal.Block.BlockID)).
Msg("invalid proposal")
return nil
}
if errors.Is(err, model.ErrUnverifiableBlock) {
f.log.Warn().
Hex("block_id", logging.ID(blockProposal.Block.BlockID)).
Hex("qc_block_id", logging.ID(blockProposal.Block.QC.BlockID)).
Msg("unverifiable proposal")
// even if the block is unverifiable because the QC has been
// pruned, it still needs to be added to the forks, otherwise,
// a new block with a QC to this block will fail to be added
// to forks and crash the event loop.
} else if err != nil {
return fmt.Errorf("cannot validate block proposal %x: %w", blockProposal.Block.BlockID, err)
}
// as a sanity check, we run the finalization logic's internal validation on the block
if err := f.finalizationLogic.VerifyBlock(blockProposal.Block); err != nil {
// this should never happen: the block was found to be valid by the validator
// if the finalization logic's internal validation errors, we have a bug
return fmt.Errorf("invaid block passed validation: %w", err)
}
err = f.finalizationLogic.AddBlock(blockProposal.Block)
if err != nil {
return fmt.Errorf("finalization logic cannot process block proposal %x: %w", blockProposal.Block.BlockID, err)
}
return nil
}