OSDN Git Service

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