• R/O
  • HTTP
  • SSH
  • HTTPS

vapor: 提交

Golang implemented sidechain for Bytom


Commit MetaInfo

修订版0d2d815af7de0672914ff2baec752f75e07a5520 (tree)
时间2019-11-27 20:05:31
作者Paladz <yzhu101@uott...>
CommiterGitHub

Log Message

Merge branch 'mov' into fix_core_sync

更改概述

差异

--- a/netsync/chainmgr/fast_sync.go
+++ b/netsync/chainmgr/fast_sync.go
@@ -152,7 +152,7 @@ func (fs *fastSync) process() error {
152152 // sync length cannot be greater than maxFastSyncBlocksNum.
153153 func (fs *fastSync) findSyncRange() (*types.Block, error) {
154154 bestHeight := fs.chain.BestBlockHeight()
155- length := fs.mainSyncPeer.IrreversibleHeight() - fastSyncPivotGap - bestHeight
155+ length := fs.mainSyncPeer.Height() - fastSyncPivotGap - bestHeight
156156 if length > maxNumOfBlocksPerSync {
157157 length = maxNumOfBlocksPerSync
158158 }
--- a/proposal/proposal.go
+++ b/proposal/proposal.go
@@ -182,8 +182,11 @@ func (b *blockBuilder) build() (*types.Block, error) {
182182 return nil, err
183183 }
184184
185- _, err := b.chain.SignBlock(b.block)
186- return b.block, err
185+ if err := b.chain.SignBlockHeader(&b.block.BlockHeader); err != nil {
186+ return nil, err
187+ }
188+
189+ return b.block, nil
187190 }
188191
189192 func (b *blockBuilder) calcBlockCommitment() (err error) {
--- a/protocol/bbft.go
+++ b/protocol/bbft.go
@@ -199,27 +199,51 @@ func (c *Chain) ProcessBlockSignature(signature, xPub []byte, blockHash *bc.Hash
199199 return c.eventDispatcher.Post(event.BlockSignatureEvent{BlockHash: *blockHash, Signature: signature, XPub: xPub})
200200 }
201201
202-// SignBlock signing the block if current node is consensus node
203-func (c *Chain) SignBlock(block *types.Block) ([]byte, error) {
202+// SignBlockHeader signing the block if current node is consensus node
203+func (c *Chain) SignBlockHeader(blockHeader *types.BlockHeader) error {
204+ _, err := c.signBlockHeader(blockHeader)
205+ return err
206+}
207+
208+func (c *Chain) applyBlockSign(blockHeader *types.BlockHeader) error {
209+ signature, err := c.signBlockHeader(blockHeader)
210+ if err != nil {
211+ return err
212+ }
213+
214+ if len(signature) == 0 {
215+ return nil
216+ }
217+
218+ if err := c.store.SaveBlockHeader(blockHeader); err != nil {
219+ return err
220+ }
221+
222+ xpub := config.CommonConfig.PrivateKey().XPub()
223+ return c.eventDispatcher.Post(event.BlockSignatureEvent{BlockHash: blockHeader.Hash(), Signature: signature, XPub: xpub[:]})
224+}
225+
226+func (c *Chain) signBlockHeader(blockHeader *types.BlockHeader) ([]byte, error) {
204227 xprv := config.CommonConfig.PrivateKey()
205- xpubStr := xprv.XPub().String()
206- node, err := c.getConsensusNode(&block.PreviousBlockHash, xpubStr)
228+ xpub := xprv.XPub()
229+ node, err := c.getConsensusNode(&blockHeader.PreviousBlockHash, xpub.String())
207230 if err == errNotFoundConsensusNode {
208231 return nil, nil
209232 } else if err != nil {
210233 return nil, err
211234 }
212235
213- if err := c.checkDoubleSign(&block.BlockHeader, node.XPub.String()); err == errDoubleSignBlock {
236+ if err := c.checkDoubleSign(blockHeader, node.XPub.String()); err == errDoubleSignBlock {
214237 return nil, nil
215238 } else if err != nil {
216239 return nil, err
217240 }
218241
219- signature := block.Get(node.Order)
220- if len(signature) == 0 {
221- signature = xprv.Sign(block.Hash().Bytes())
222- block.Set(node.Order, signature)
242+ if signature := blockHeader.Get(node.Order); len(signature) != 0 {
243+ return nil, nil
223244 }
245+
246+ signature := xprv.Sign(blockHeader.Hash().Bytes())
247+ blockHeader.Set(node.Order, signature)
224248 return signature, nil
225249 }
--- a/protocol/block.go
+++ b/protocol/block.go
@@ -3,9 +3,7 @@ package protocol
33 import (
44 log "github.com/sirupsen/logrus"
55
6- "github.com/vapor/config"
76 "github.com/vapor/errors"
8- "github.com/vapor/event"
97 "github.com/vapor/protocol/bc"
108 "github.com/vapor/protocol/bc/types"
119 "github.com/vapor/protocol/state"
@@ -120,6 +118,10 @@ func (c *Chain) connectBlock(block *types.Block) (err error) {
120118 }
121119 }
122120
121+ if err := c.applyBlockSign(&block.BlockHeader); err != nil {
122+ return err
123+ }
124+
123125 irrBlockHeader := c.lastIrrBlockHeader
124126 if c.isIrreversible(&block.BlockHeader) && block.Height > irrBlockHeader.Height {
125127 irrBlockHeader = &block.BlockHeader
@@ -231,6 +233,10 @@ func (c *Chain) reorganizeChain(blockHeader *types.BlockHeader) error {
231233 consensusResults = append(consensusResults, consensusResult.Fork())
232234 }
233235
236+ if err := c.applyBlockSign(attachBlockHeader); err != nil {
237+ return err
238+ }
239+
234240 if c.isIrreversible(attachBlockHeader) && attachBlockHeader.Height > irrBlockHeader.Height {
235241 irrBlockHeader = attachBlockHeader
236242 }
@@ -309,22 +315,11 @@ func (c *Chain) saveBlock(block *types.Block) error {
309315 }
310316 }
311317
312- signature, err := c.SignBlock(block)
313- if err != nil {
314- return errors.Sub(ErrBadBlock, err)
315- }
316-
317318 if err := c.store.SaveBlock(block, bcBlock.TransactionStatus); err != nil {
318319 return err
319320 }
320- c.orphanManage.Delete(&bcBlock.ID)
321321
322- if len(signature) != 0 {
323- xPub := config.CommonConfig.PrivateKey().XPub()
324- if err := c.eventDispatcher.Post(event.BlockSignatureEvent{BlockHash: block.Hash(), Signature: signature, XPub: xPub[:]}); err != nil {
325- return err
326- }
327- }
322+ c.orphanManage.Delete(&bcBlock.ID)
328323 return nil
329324 }
330325
Show on old repository browser