7 log "github.com/sirupsen/logrus"
9 "github.com/bytom/bytom/consensus"
10 "github.com/bytom/bytom/protocol/bc"
11 "github.com/bytom/bytom/protocol/state"
14 // AuthVerification verify whether the Verification is legal.
15 // the status of source checkpoint must justified, and an individual validator ν must not publish two distinct Verification
16 // ⟨ν,s1,t1,h(s1),h(t1)⟩ and ⟨ν,s2,t2,h(s2),h(t2)⟩, such that either:
17 // h(t1) = h(t2) OR h(s1) < h(s2) < h(t2) < h(t1)
18 func (c *Casper) AuthVerification(v *Verification) error {
19 if err := validate(v); err != nil {
26 // root of tree is the last finalized checkpoint
27 if v.TargetHeight < c.tree.checkpoint.Height {
28 // discard the verification message which height of target less than height of last finalized checkpoint
29 // is for simplify check the vote within the span of its other votes
33 targetNode, err := c.tree.nodeByHash(v.TargetHash)
35 c.verificationCache.Add(verificationCacheKey(v.TargetHash, v.PubKey), v)
39 validators, err := c.Validators(&v.TargetHash)
44 if _, ok := validators[v.PubKey]; !ok {
45 return errPubKeyIsNotValidator
48 if targetNode.checkpoint.ContainsVerification(validators[v.PubKey].Order, &v.SourceHash) {
52 oldBestHash := c.bestChain()
53 if err := c.authVerification(v, targetNode.checkpoint, validators); err != nil {
57 return c.tryRollback(oldBestHash)
60 func (c *Casper) authVerification(v *Verification, target *state.Checkpoint, validators map[string]*state.Validator) error {
61 validator := validators[v.PubKey]
62 if err := c.verifyVerification(v, validator.Order, true); err != nil {
66 checkpoints, err := c.addVerificationToCheckpoint(target, validators, v)
71 if err := c.store.SaveCheckpoints(checkpoints); err != nil {
75 return c.saveVerificationToHeader(v, validator.Order)
78 func (c *Casper) addVerificationToCheckpoint(target *state.Checkpoint, validators map[string]*state.Validator, verifications ...*Verification) ([]*state.Checkpoint, error) {
79 affectedCheckpoints := []*state.Checkpoint{target}
80 for _, v := range verifications {
81 source, err := c.store.GetCheckpoint(&v.SourceHash)
86 supLink := target.AddVerification(v.SourceHash, v.SourceHeight, validators[v.PubKey].Order, v.Signature)
87 if target.Status != state.Unjustified || !supLink.IsMajority(len(validators)) || source.Status == state.Finalized {
91 c.setJustified(source, target)
92 affectedCheckpoints = append(affectedCheckpoints, source)
94 return affectedCheckpoints, nil
97 func (c *Casper) saveVerificationToHeader(v *Verification, validatorOrder int) error {
98 blockHeader, err := c.store.GetBlockHeader(&v.TargetHash)
103 signature, err := hex.DecodeString(v.Signature)
108 blockHeader.SupLinks.AddSupLink(v.SourceHeight, v.SourceHash, signature, validatorOrder)
109 return c.store.SaveBlockHeader(blockHeader)
112 // source status is justified, and exist a super majority link from source to target
113 func (c *Casper) setJustified(source, target *state.Checkpoint) {
114 target.Status = state.Justified
116 if target.ParentHash == source.Hash {
117 c.setFinalized(source)
121 func (c *Casper) setFinalized(checkpoint *state.Checkpoint) {
122 checkpoint.Status = state.Finalized
123 newRoot, err := c.tree.nodeByHash(checkpoint.Hash)
125 log.WithField("err", err).Error("source checkpoint before the last finalized checkpoint")
129 // update the checkpoint state in memory
130 newRoot.checkpoint.Status = state.Finalized
134 func (c *Casper) tryRollback(oldBestHash bc.Hash) error {
135 if newBestHash := c.bestChain(); oldBestHash != newBestHash {
136 msg := &rollbackMsg{bestHash: newBestHash, reply: make(chan error)}
143 func (c *Casper) authVerificationLoop() {
144 for blockHash := range c.newEpochCh {
145 validators, err := c.Validators(&blockHash)
147 log.WithField("err", err).Error("get validators when auth verification")
151 for _, validator := range validators {
152 key := verificationCacheKey(blockHash, validator.PubKey)
153 verification, ok := c.verificationCache.Get(key)
158 v := verification.(*Verification)
159 target, err := c.store.GetCheckpoint(&v.TargetHash)
161 log.WithField("err", err).Error("get target checkpoint")
162 c.verificationCache.Remove(key)
167 if err := c.authVerification(v, target, validators); err != nil {
168 log.WithField("err", err).Error("auth verification in cache")
172 c.verificationCache.Remove(key)
177 func (c *Casper) verifyVerification(v *Verification, validatorOrder int, trackEvilValidator bool) error {
178 if err := c.verifySameHeight(v, validatorOrder, trackEvilValidator); err != nil {
182 return c.verifySpanHeight(v, validatorOrder, trackEvilValidator)
185 // a validator must not publish two distinct votes for the same target height
186 func (c *Casper) verifySameHeight(v *Verification, validatorOrder int, trackEvilValidator bool) error {
187 checkpoints, err := c.store.GetCheckpointsByHeight(v.TargetHeight)
192 for _, checkpoint := range checkpoints {
193 for _, supLink := range checkpoint.SupLinks {
194 if supLink.Signatures[validatorOrder] != "" && checkpoint.Hash != v.TargetHash {
195 if trackEvilValidator {
196 c.evilValidators[v.PubKey] = []*Verification{v, makeVerification(supLink, checkpoint, v.PubKey, validatorOrder)}
198 return errSameHeightInVerification
205 // a validator must not vote within the span of its other votes.
206 func (c *Casper) verifySpanHeight(v *Verification, validatorOrder int, trackEvilValidator bool) error {
207 if c.tree.findOnlyOne(func(checkpoint *state.Checkpoint) bool {
208 if checkpoint.Height == v.TargetHeight {
212 for _, supLink := range checkpoint.SupLinks {
213 if supLink.Signatures[validatorOrder] != "" {
214 if (checkpoint.Height < v.TargetHeight && supLink.SourceHeight > v.SourceHeight) ||
215 (checkpoint.Height > v.TargetHeight && supLink.SourceHeight < v.SourceHeight) {
216 if trackEvilValidator {
217 c.evilValidators[v.PubKey] = []*Verification{v, makeVerification(supLink, checkpoint, v.PubKey, validatorOrder)}
225 return errSpanHeightInVerification
230 func makeVerification(supLink *state.SupLink, checkpoint *state.Checkpoint, pubKey string, validatorOrder int) *Verification {
231 return &Verification{
232 SourceHash: supLink.SourceHash,
233 TargetHash: checkpoint.Hash,
234 SourceHeight: supLink.SourceHeight,
235 TargetHeight: checkpoint.Height,
236 Signature: supLink.Signatures[validatorOrder],
241 func validate(v *Verification) error {
242 blocksOfEpoch := consensus.ActiveNetParams.BlocksOfEpoch
243 if v.SourceHeight%blocksOfEpoch != 0 || v.TargetHeight%blocksOfEpoch != 0 {
244 return errVoteToGrowingCheckpoint
247 if v.SourceHeight == v.TargetHeight {
248 return errVoteToSameCheckpoint
251 return v.VerifySignature()
254 func verificationCacheKey(blockHash bc.Hash, pubKey string) string {
255 return fmt.Sprintf("%s:%s", blockHash.String(), pubKey)