OSDN Git Service

fix next leader time (#98)
[bytom/vapor.git] / protocol / bbft.go
1 package protocol
2
3 import (
4         "encoding/hex"
5         "fmt"
6
7         "github.com/golang/groupcache/lru"
8         log "github.com/sirupsen/logrus"
9
10         "github.com/vapor/config"
11         "github.com/vapor/crypto/ed25519"
12         "github.com/vapor/errors"
13         "github.com/vapor/event"
14         "github.com/vapor/protocol/bc"
15         "github.com/vapor/protocol/bc/types"
16         "github.com/vapor/protocol/state"
17         "github.com/vapor/crypto/ed25519/chainkd"
18 )
19
20 const (
21         maxSignatureCacheSize = 10000
22 )
23
24 var (
25         errVotingOperationOverFlow = errors.New("voting operation result overflow")
26         errDoubleSignBlock         = errors.New("the consensus is double sign in same height of different block")
27         errInvalidSignature        = errors.New("the signature of block is invalid")
28 )
29
30 type bbft struct {
31         consensusNodeManager *consensusNodeManager
32         orphanManage         *OrphanManage
33         signatureCache       *lru.Cache
34         eventDispatcher      *event.Dispatcher
35 }
36
37 func newBbft(store Store, blockIndex *state.BlockIndex, orphanManage *OrphanManage, eventDispatcher *event.Dispatcher) *bbft {
38         return &bbft{
39                 orphanManage:         orphanManage,
40                 consensusNodeManager: newConsensusNodeManager(store, blockIndex),
41                 signatureCache:       lru.New(maxSignatureCacheSize),
42                 eventDispatcher:      eventDispatcher,
43         }
44 }
45
46 func (b *bbft) isIrreversible(block *types.Block) bool {
47         signNum, err := b.validateSign(block)
48         if err != nil {
49                 return false
50         }
51
52         return signNum > (NumOfConsensusNode * 2 / 3)
53 }
54
55 // NextLeaderTime returns the start time of the specified public key as the next leader node
56 func (b *bbft) NextLeaderTimeRange(pubkey []byte, bestBlockHash *bc.Hash) (uint64, uint64, error) {
57         return b.consensusNodeManager.nextLeaderTimeRange(pubkey, bestBlockHash)
58 }
59
60 func (b *bbft) ApplyBlock(voteResultMap map[uint64]*state.VoteResult, block *types.Block) (err error) {
61         return b.consensusNodeManager.applyBlock(voteResultMap, block)
62 }
63
64 func (b *bbft) DetachBlock(voteResultMap map[uint64]*state.VoteResult, block *types.Block) error {
65         return b.consensusNodeManager.detachBlock(voteResultMap, block)
66 }
67
68 // ProcessBlockSignature process the received block signature messages
69 // return whether a block become irreversible, if so, the chain module must update status
70 func (b *bbft) ProcessBlockSignature(signature []byte, xPub [64]byte, blockHeight uint64, blockHash *bc.Hash) (bool, error) {
71         block, err := b.consensusNodeManager.store.GetBlock(blockHash)
72         if err != nil {
73                 // block is not exist, save the signature
74                 key := fmt.Sprintf("%s:%s", blockHash.String(), hex.EncodeToString(xPub[:]))
75                 b.signatureCache.Add(key, signature)
76                 return false, err
77         }
78
79         consensusNode, err := b.consensusNodeManager.getConsensusNode(&block.PreviousBlockHash, hex.EncodeToString(xPub[:]))
80         if err != nil {
81                 return false, err
82         }
83
84
85         if chainkd.XPub(xPub).Verify(blockHash.Bytes(), signature) {
86                 return false, errInvalidSignature
87         }
88
89         isDoubleSign, err := b.checkDoubleSign(consensusNode.order, blockHeight, *blockHash)
90         if err != nil {
91                 return false, err
92         }
93
94         if isDoubleSign {
95                 log.WithFields(log.Fields{"module": logModule, "blockHash": blockHash.String(), "xPub": hex.EncodeToString(xPub[:])}).Warn("the consensus node double sign the same height of different block")
96                 return false, errDoubleSignBlock
97         }
98
99         orphanBlock, ok := b.orphanManage.Get(blockHash)
100         if ok {
101                 orphanBlock.Witness[consensusNode.order] = signature
102                 return false, nil
103         }
104
105         if err := b.updateBlockSignature(block, consensusNode.order, signature); err != nil {
106                 return false, err
107         }
108
109         return b.isIrreversible(block), nil
110 }
111
112 // ValidateBlock verify whether the block is valid
113 func (b *bbft) ValidateBlock(block *types.Block) error {
114         signNum, err := b.validateSign(block)
115         if err != nil {
116                 return err
117         }
118
119         if signNum == 0 {
120                 return errors.New("no valid signature")
121         }
122         return nil
123 }
124
125 // validateSign verify the signatures of block, and return the number of correct signature
126 // if some signature is invalid, they will be reset to nil
127 // if the block has not the signature of blocker, it will return error
128 func (b *bbft) validateSign(block *types.Block) (uint64, error) {
129         var correctSignNum uint64
130         consensusNodeMap, err := b.consensusNodeManager.getConsensusNodesByVoteResult(&block.PreviousBlockHash)
131         if err != nil {
132                 return 0, err
133         }
134
135         hasBlockerSign := false
136         for pubKey, node := range consensusNodeMap {
137                 if len(block.Witness) <= int(node.order) {
138                         continue
139                 }
140
141                 blockHash := block.Hash()
142                 if block.Witness[node.order] == nil {
143                         key := fmt.Sprintf("%s:%s", blockHash.String(), pubKey)
144                         signature, ok := b.signatureCache.Get(key)
145                         if ok {
146                                 block.Witness[node.order] = signature.([]byte)
147                         }
148                 }
149
150                 pubKeyBytes, err := hex.DecodeString(pubKey)
151                 if err != nil {
152                         return 0, err
153                 }
154
155                 if ed25519.Verify(ed25519.PublicKey(pubKeyBytes[:32]), blockHash.Bytes(), block.Witness[node.order]) {
156                         isDoubleSign, err := b.checkDoubleSign(node.order, block.Height, block.Hash())
157                         if err != nil {
158                                 return 0, err
159                         }
160
161                         if isDoubleSign {
162                                 log.WithFields(log.Fields{"module": logModule, "blockHash": blockHash.String(), "pubKey": pubKey}).Warn("the consensus node double sign the same height of different block")
163                                 // Consensus node is signed twice with the same block height, discard the signature
164                                 block.Witness[node.order] = nil
165                         } else {
166                                 correctSignNum++
167                                 isBlocker, err := b.consensusNodeManager.isBlocker(block, pubKey)
168                                 if err != nil {
169                                         return 0, err
170                                 }
171                                 if isBlocker {
172                                         hasBlockerSign = true
173                                 }
174                         }
175                 } else {
176                         // discard the invalid signature
177                         block.Witness[node.order] = nil
178                 }
179         }
180         if !hasBlockerSign {
181                 return 0, errors.New("the block has no signature of the blocker")
182         }
183         return correctSignNum, nil
184 }
185
186 func (b *bbft) checkDoubleSign(nodeOrder, blockHeight uint64, blockHash bc.Hash) (bool, error) {
187         blockNodes := b.consensusNodeManager.blockIndex.NodesByHeight(blockHeight)
188         for _, blockNode := range blockNodes {
189                 if blockNode.Hash == blockHash {
190                         continue
191                 }
192                 if ok, err := blockNode.BlockWitness.Test(uint32(nodeOrder)); err != nil && ok {
193                         block, err := b.consensusNodeManager.store.GetBlock(&blockHash)
194                         if err != nil {
195                                 return false, err
196                         }
197
198                         // reset nil to discard signature
199                         if err := b.updateBlockSignature(block, nodeOrder, nil); err != nil {
200                                 return false, err
201                         }
202
203                         return true, nil
204                 }
205         }
206         return false, nil
207 }
208
209 // SignBlock signing the block if current node is consensus node
210 func (b *bbft) SignBlock(block *types.Block) ([]byte, error) {
211         xprv := config.CommonConfig.PrivateKey()
212         xpub := [64]byte(xprv.XPub())
213         node, err := b.consensusNodeManager.getConsensusNode(&block.PreviousBlockHash, hex.EncodeToString(xpub[:]))
214         if err != nil && err != errNotFoundConsensusNode {
215                 return nil, err
216         }
217
218         if node == nil {
219                 return nil, nil
220         }
221
222         signature := block.Witness[node.order]
223         if len(signature) == 0 {
224                 signature = xprv.Sign(block.Hash().Bytes())
225                 block.Witness[node.order] = signature
226         }
227         return signature, nil
228 }
229
230 func (b *bbft) updateBlockSignature(block *types.Block, nodeOrder uint64, signature []byte) error {
231         blockHash := block.Hash()
232         blockNode := b.consensusNodeManager.blockIndex.GetNode(&blockHash)
233
234         if len(signature) != 0 {
235                 if err := blockNode.BlockWitness.Set(uint32(nodeOrder)); err != nil {
236                         return err
237                 }
238         } else {
239                 if err := blockNode.BlockWitness.Clean(uint32(nodeOrder)); err != nil {
240                         return err
241                 }
242         }
243
244         block.Witness[nodeOrder] = signature
245         txStatus, err := b.consensusNodeManager.store.GetTransactionStatus(&blockHash)
246         if err != nil {
247                 return err
248         }
249
250         return b.consensusNodeManager.store.SaveBlock(block, txStatus)
251 }