7 log "github.com/sirupsen/logrus"
9 "github.com/bytom/bytom/protocol/bc"
10 "github.com/bytom/bytom/protocol/state"
13 // AuthVerification verify whether the Verification is legal.
14 // the status of source checkpoint must justified, and an individual validator ν must not publish two distinct Verification
15 // ⟨ν,s1,t1,h(s1),h(t1)⟩ and ⟨ν,s2,t2,h(s2),h(t2)⟩, such that either:
16 // h(t1) = h(t2) OR h(s1) < h(s2) < h(t2) < h(t1)
17 func (c *Casper) AuthVerification(v *Verification) error {
18 if err := validate(v); err != nil {
22 target, err := c.store.GetCheckpoint(&v.TargetHash)
24 c.verificationCache.Add(verificationCacheKey(v.TargetHash, v.PubKey), v)
28 validators, err := c.Validators(&v.TargetHash)
33 if _, ok := validators[v.PubKey]; !ok {
34 return errPubKeyIsNotValidator
37 if target.ContainsVerification(v.SourceHash, validators[v.PubKey].Order) {
44 // root of tree is the last finalized checkpoint
45 if v.TargetHeight < c.tree.checkpoint.Height {
46 // discard the verification message which height of target less than height of last finalized checkpoint
47 // is for simplify check the vote within the span of its other votes
51 return c.authVerification(v, target, validators)
54 func (c *Casper) authVerification(v *Verification, target *state.Checkpoint, validators map[string]*state.Validator) error {
55 validator := validators[v.PubKey]
56 if err := c.verifyVerification(v, validator.Order, true); err != nil {
60 if err := c.addVerificationToCheckpoint(target, validators, v); err != nil {
64 return c.saveVerificationToHeader(v, validator.Order)
67 func (c *Casper) addVerificationToCheckpoint(target *state.Checkpoint, validators map[string]*state.Validator, verifications ...*Verification) error {
68 _, oldBestHash := c.bestChain()
69 var affectedCheckpoints []*state.Checkpoint
70 for _, v := range verifications {
71 source, err := c.store.GetCheckpoint(&v.SourceHash)
76 supLink := target.AddVerification(v.SourceHash, v.SourceHeight, validators[v.PubKey].Order, v.Signature)
77 if target.Status != state.Unjustified || !supLink.IsMajority(len(validators)) || source.Status == state.Finalized {
81 if source.Status == state.Unjustified {
82 c.justifyingCheckpoints[source.Hash] = append(c.justifyingCheckpoints[source.Hash], target)
86 affectedCheckpoints = append(affectedCheckpoints, c.setJustified(source, target)...)
89 _, newBestHash := c.bestChain()
90 if oldBestHash != newBestHash {
91 c.rollbackNotifyCh <- nil
94 return c.store.SaveCheckpoints(affectedCheckpoints...)
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) []*state.Checkpoint {
114 var affectedCheckpoint []*state.Checkpoint
115 target.Status = state.Justified
117 if target.Parent.Hash == source.Hash {
118 c.setFinalized(source)
121 for _, checkpoint := range c.justifyingCheckpoints[target.Hash] {
122 affectedCheckpoint = append(affectedCheckpoint, c.setJustified(target, checkpoint)...)
125 delete(c.justifyingCheckpoints, target.Hash)
126 affectedCheckpoint = append(affectedCheckpoint, source, target)
127 return affectedCheckpoint
130 func (c *Casper) setFinalized(checkpoint *state.Checkpoint) {
131 checkpoint.Status = state.Finalized
132 newRoot, err := c.tree.nodeByHash(checkpoint.Hash)
134 log.WithField("err", err).Panic("fail to set checkpoint finalized")
140 func (c *Casper) authVerificationLoop() {
141 for blockHash := range c.newEpochCh {
142 validators, err := c.Validators(&blockHash)
144 log.WithField("err", err).Error("get validators when auth verification")
148 for _, validator := range validators {
149 key := verificationCacheKey(blockHash, validator.PubKey)
150 verification, ok := c.verificationCache.Get(key)
155 v := verification.(*Verification)
156 target, err := c.store.GetCheckpoint(&v.TargetHash)
158 log.WithField("err", err).Error("get target checkpoint")
159 c.verificationCache.Remove(key)
164 if err := c.authVerification(v, target, validators); err != nil {
165 log.WithField("err", err).Error("auth verification in cache")
169 c.verificationCache.Remove(key)
174 func (c *Casper) verifyVerification(v *Verification, validatorOrder int, trackEvilValidator bool) error {
175 if err := c.verifySameHeight(v, validatorOrder, trackEvilValidator); err != nil {
179 return c.verifySpanHeight(v, validatorOrder, trackEvilValidator)
182 // a validator must not publish two distinct votes for the same target height
183 func (c *Casper) verifySameHeight(v *Verification, validatorOrder int, trackEvilValidator bool) error {
184 checkpoints, err := c.store.GetCheckpointsByHeight(v.TargetHeight)
189 for _, checkpoint := range checkpoints {
190 for _, supLink := range checkpoint.SupLinks {
191 if supLink.Signatures[validatorOrder] != "" && checkpoint.Hash != v.TargetHash {
192 if trackEvilValidator {
193 c.evilValidators[v.PubKey] = []*Verification{v, makeVerification(supLink, checkpoint, v.PubKey, validatorOrder)}
195 return errSameHeightInVerification
202 // a validator must not vote within the span of its other votes.
203 func (c *Casper) verifySpanHeight(v *Verification, validatorOrder int, trackEvilValidator bool) error {
204 if c.tree.findOnlyOne(func(checkpoint *state.Checkpoint) bool {
205 if checkpoint.Height == v.TargetHeight {
209 for _, supLink := range checkpoint.SupLinks {
210 if supLink.Signatures[validatorOrder] != "" {
211 if (checkpoint.Height < v.TargetHeight && supLink.SourceHeight > v.SourceHeight) ||
212 (checkpoint.Height > v.TargetHeight && supLink.SourceHeight < v.SourceHeight) {
213 if trackEvilValidator {
214 c.evilValidators[v.PubKey] = []*Verification{v, makeVerification(supLink, checkpoint, v.PubKey, validatorOrder)}
222 return errSpanHeightInVerification
227 func makeVerification(supLink *state.SupLink, checkpoint *state.Checkpoint, pubKey string, validatorOrder int) *Verification {
228 return &Verification{
229 SourceHash: supLink.SourceHash,
230 TargetHash: checkpoint.Hash,
231 SourceHeight: supLink.SourceHeight,
232 TargetHeight: checkpoint.Height,
233 Signature: supLink.Signatures[validatorOrder],
238 func validate(v *Verification) error {
239 if v.SourceHeight%state.BlocksOfEpoch != 0 || v.TargetHeight%state.BlocksOfEpoch != 0 {
240 return errVoteToGrowingCheckpoint
243 if v.SourceHeight == v.TargetHeight {
244 return errVoteToSameCheckpoint
247 return v.VerifySignature()
250 func verificationCacheKey(blockHash bc.Hash, pubKey string) string {
251 return fmt.Sprintf("%s:%s", blockHash.String(), pubKey)