This repository has been archived by the owner on Mar 21, 2024. It is now read-only.
/
auth_verification.go
230 lines (191 loc) · 6.19 KB
/
auth_verification.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package casper
import (
"fmt"
log "github.com/sirupsen/logrus"
"github.com/bytom/bytom/errors"
"github.com/bytom/bytom/protocol/bc"
"github.com/bytom/bytom/protocol/state"
)
// AuthVerification verify whether the Verification is legal.
// the status of source checkpoint must justified, and an individual validator ν must not publish two distinct Verification
// ⟨ν,s1,t1,h(s1),h(t1)⟩ and ⟨ν,s2,t2,h(s2),h(t2)⟩, such that either:
// h(t1) = h(t2) OR h(s1) < h(s2) < h(t2) < h(t1)
func (c *Casper) AuthVerification(msg *ValidCasperSignMsg) error {
c.mu.Lock()
defer c.mu.Unlock()
targetNode := c.tree.nodeByHash(msg.TargetHash)
if targetNode == nil {
c.verificationCache.Add(verificationCacheKey(msg.TargetHash, msg.PubKey), msg)
return nil
}
source, err := c.store.GetCheckpoint(&msg.SourceHash)
if err != nil {
return err
}
v, err := convertVerification(source, targetNode.Checkpoint, msg)
if err != nil {
return err
}
if targetNode.ContainsVerification(v.order, &v.SourceHash) {
return nil
}
oldBestHash := c.bestChain()
if err := c.authVerification(v, targetNode.Checkpoint); err != nil {
return err
}
return c.tryRollback(oldBestHash)
}
func (c *Casper) authVerification(v *verification, target *state.Checkpoint) error {
if err := c.verifyVerification(v); err != nil {
return err
}
checkpoints, err := c.addVerificationToCheckpoint(target, v)
if err != nil {
return err
}
if err := c.msgQueue.Post(v.toValidCasperSignMsg()); err != nil {
return err
}
if err := c.store.SaveCheckpoints(checkpoints); err != nil {
return err
}
return c.saveVerificationToHeader(v)
}
func (c *Casper) addVerificationToCheckpoint(target *state.Checkpoint, verifications ...*verification) ([]*state.Checkpoint, error) {
validatorSize := len(target.Parent.EffectiveValidators())
affectedCheckpoints := []*state.Checkpoint{target}
for _, v := range verifications {
source, err := c.store.GetCheckpoint(&v.SourceHash)
if err != nil {
return nil, err
}
supLink := target.AddVerification(v.SourceHash, v.SourceHeight, v.order, v.Signature)
if target.Status != state.Unjustified || !supLink.IsMajority(validatorSize) || source.Status == state.Finalized {
continue
}
c.setJustified(source, target)
affectedCheckpoints = append(affectedCheckpoints, source)
}
return affectedCheckpoints, nil
}
func (c *Casper) saveVerificationToHeader(v *verification) error {
blockHeader, err := c.store.GetBlockHeader(&v.TargetHash)
if err != nil {
return err
}
blockHeader.SupLinks.AddSupLink(v.SourceHeight, v.SourceHash, v.Signature, v.order)
return c.store.SaveBlockHeader(blockHeader)
}
// source status is justified, and exist a super majority link from source to target
func (c *Casper) setJustified(source, target *state.Checkpoint) {
target.Status = state.Justified
// must direct child
if target.ParentHash == source.Hash {
c.setFinalized(source)
}
}
func (c *Casper) setFinalized(checkpoint *state.Checkpoint) {
checkpoint.Status = state.Finalized
newRoot := c.tree.nodeByHash(checkpoint.Hash)
if newRoot == nil {
log.WithFields(log.Fields{"module": logModule, "hash": checkpoint.Hash}).Warn("source checkpoint before the last finalized checkpoint")
return
}
// update the checkpoint state in memory
newRoot.Status = state.Finalized
newRoot.Parent = nil
c.tree = newRoot
}
func (c *Casper) tryRollback(oldBestHash bc.Hash) error {
if newBestHash := c.bestChain(); oldBestHash != newBestHash {
msg := &RollbackMsg{BestHash: newBestHash, Reply: make(chan error)}
c.rollbackCh <- msg
return <-msg.Reply
}
return nil
}
func (c *Casper) authVerificationLoop() {
for blockHash := range c.newEpochCh {
validators, err := c.validators(&blockHash)
if err != nil {
log.WithFields(log.Fields{"err": err, "module": logModule}).Error("get validators when auth verification")
continue
}
for _, validator := range validators {
key := verificationCacheKey(blockHash, validator.PubKey)
data, ok := c.verificationCache.Get(key)
if !ok {
continue
}
if err := c.authCachedMsg(data.(*ValidCasperSignMsg), key); err != nil {
log.WithFields(log.Fields{"err": err, "module": logModule}).Error("auth cached message")
}
}
}
}
func (c *Casper) authCachedMsg(msg *ValidCasperSignMsg, msgKey string) error {
defer c.verificationCache.Remove(msgKey)
source, err := c.store.GetCheckpoint(&msg.SourceHash)
if err != nil {
return errors.Wrap(err, "get source checkpoint")
}
targetNode := c.tree.nodeByHash(msg.TargetHash)
if targetNode == nil {
return errors.New("get target checkpoint")
}
target := targetNode.Checkpoint
v, err := convertVerification(source, target, msg)
if err != nil {
return errors.Wrap(err, "authVerificationLoop fail on newVerification")
}
c.mu.Lock()
defer c.mu.Unlock()
return c.authVerification(v, target)
}
func (c *Casper) verifyVerification(v *verification) error {
if err := v.valid(); err != nil {
return err
}
if err := c.verifySameHeight(v); err != nil {
return err
}
return c.verifySpanHeight(v)
}
// a validator must not publish two distinct votes for the same target height
func (c *Casper) verifySameHeight(v *verification) error {
checkpoints, err := c.store.GetCheckpointsByHeight(v.TargetHeight)
if err != nil {
return err
}
for _, checkpoint := range checkpoints {
for _, supLink := range checkpoint.SupLinks {
if len(supLink.Signatures[v.order]) != 0 && checkpoint.Hash != v.TargetHash {
return errSameHeightInVerification
}
}
}
return nil
}
// a validator must not vote within the span of its other votes.
func (c *Casper) verifySpanHeight(v *verification) error {
if c.tree.findOnlyOne(func(checkpoint *state.Checkpoint) bool {
if checkpoint.Height == v.TargetHeight {
return false
}
for _, supLink := range checkpoint.SupLinks {
if len(supLink.Signatures[v.order]) != 0 {
if (checkpoint.Height < v.TargetHeight && supLink.SourceHeight > v.SourceHeight) ||
(checkpoint.Height > v.TargetHeight && supLink.SourceHeight < v.SourceHeight) {
return true
}
}
}
return false
}) != nil {
return errSpanHeightInVerification
}
return nil
}
func verificationCacheKey(blockHash bc.Hash, pubKey string) string {
return fmt.Sprintf("%s:%s", blockHash.String(), pubKey)
}