return false, nil
}
-func (c *chain) ProcessBlockSignature(signature, pubkey []byte, blockHeight uint64, blockHash *bc.Hash) error {
+func (c *chain) ProcessBlockSignature(signature []byte, pubkey [64]byte, blockHeight uint64, blockHash *bc.Hash) error {
return nil
}
BlockHash [32]byte
Height uint64
Signature []byte
- PubKey []byte
+ PubKey [64]byte
}
//NewBlockSignatureMsg create new block signature msg.
-func NewBlockSignatureMsg(blockHash bc.Hash, height uint64, signature []byte, pubKey []byte) ConsensusMessage {
+func NewBlockSignatureMsg(blockHash bc.Hash, height uint64, signature []byte, pubKey [64]byte) ConsensusMessage {
hash := blockHash.Byte32()
return &BlockSignatureMsg{BlockHash: hash, Height: height, Signature: signature, PubKey: pubKey}
}
msg: &BlockSignatureMsg{
BlockHash: [32]byte{0x01},
Signature: []byte{0x00},
- PubKey: []byte{0x01},
+ PubKey: [64]byte{0x01},
},
msgType: blockSignatureByte,
},
BlockHash: [32]byte{0x01},
Height: 100,
Signature: []byte{0x00},
- PubKey: []byte{0x01},
+ PubKey: [64]byte{0x01},
}
signatureBroadcastMsg := NewBroadcastMsg(NewBlockSignatureMsg(bc.NewHash(blockSignMsg.BlockHash), blockSignMsg.Height, blockSignMsg.Signature, blockSignMsg.PubKey), consensusChannel)
BlockHash: [32]byte{0x01},
Height: 100,
Signature: []byte{0x00},
- PubKey: []byte{0x01},
+ PubKey: [64]byte{0x01},
}
gotMsg := NewBlockSignatureMsg(bc.NewHash(msg.BlockHash), msg.Height, msg.Signature, msg.PubKey)
if !reflect.DeepEqual(gotMsg, msg) {
t.Fatalf("test block signature message err. got:%s\n want:%s", spew.Sdump(gotMsg), spew.Sdump(msg))
}
- wantString := "{block_hash: 0100000000000000000000000000000000000000000000000000000000000000,block_height:100,signature:00,pubkey:01}"
+ wantString := "{block_hash: 0100000000000000000000000000000000000000000000000000000000000000,block_height:100,signature:00,pubkey:01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000}"
if gotMsg.String() != wantString {
t.Fatalf("test block signature message err. got string:%s\n want string:%s", gotMsg.String(), wantString)
}
BestBlockHeight() uint64
GetHeaderByHash(*bc.Hash) (*types.BlockHeader, error)
ProcessBlock(*types.Block) (bool, error)
- ProcessBlockSignature(signature, pubkey []byte, blockHeight uint64, blockHash *bc.Hash) error
+ ProcessBlockSignature(signature []byte, pubkey [64]byte, blockHeight uint64, blockHash *bc.Hash) error
}
type blockMsg struct {
return
}
- blockSignatureMsg := NewBroadcastMsg(NewBlockSignatureMsg(ev.BlockHash, blockHeader.Height, ev.Signature, ev.XPub[:]), consensusChannel)
+ blockSignatureMsg := NewBroadcastMsg(NewBlockSignatureMsg(ev.BlockHash, blockHeader.Height, ev.Signature, ev.XPub), consensusChannel)
if err := m.peers.BroadcastMsg(blockSignatureMsg); err != nil {
logrus.WithFields(logrus.Fields{"module": logModule, "err": err}).Error("failed on broadcast BlockSignBroadcastMsg.")
return
return false, nil
}
-func (c *Chain) ProcessBlockSignature(signature, pubKey []byte, blockHeight uint64, blockHash *bc.Hash) error {
- var xPub [64]byte
- for i := 0; i < 64; i++ {
- xPub[i] = pubKey[i]
- }
-
+func (c *Chain) ProcessBlockSignature(signature []byte, xPub [64]byte, blockHeight uint64, blockHash *bc.Hash) error {
isIrreversible, err := c.bbft.ProcessBlockSignature(signature, xPub, blockHeight, blockHash)
if err != nil {
return err
func (c *consensusNodeManager) applyBlock(voteResultMap map[uint64]*state.VoteResult, block *types.Block) (err error) {
voteSeq := block.Height / roundVoteBlockNums
- voteResult := voteResultMap[voteSeq]
-
- if voteResult == nil {
- voteResult, err = c.store.GetVoteResult(voteSeq)
- if err != nil && err != ErrNotFoundVoteResult {
- return err
- }
- }
-
- if voteResult == nil {
- voteResult = &state.VoteResult{
- Seq: voteSeq,
- NumOfVote: make(map[string]uint64),
- }
+ voteResult, err := c.getVoteResult(voteResultMap, voteSeq)
+ if err != nil {
+ return err
}
- voteResultMap[voteSeq] = voteResult
-
emptyHash := bc.Hash{}
if voteResult.LastBlockHash != emptyHash && voteResult.LastBlockHash != block.PreviousBlockHash {
return errors.New("bbft append block error, the block parent hash is not equals last block hash of vote result")
return nil
}
+func (c *consensusNodeManager) getVoteResult(voteResultMap map[uint64]*state.VoteResult, seq uint64) (*state.VoteResult, error) {
+ var err error
+ voteResult := voteResultMap[seq]
+ if voteResult == nil {
+ prevVoteResult := voteResultMap[seq - 1]
+ voteResult = &state.VoteResult {
+ Seq: seq,
+ NumOfVote: prevVoteResult.NumOfVote,
+ Finalized: false,
+ }
+ }
+
+ if voteResult == nil {
+ voteResult, err = c.store.GetVoteResult(seq)
+ if err != nil && err != ErrNotFoundVoteResult {
+ return nil, err
+ }
+ }
+
+ if voteResult == nil {
+ voteResult, err := c.store.GetVoteResult(seq - 1)
+ if err != nil && err != ErrNotFoundVoteResult {
+ return nil, err
+ }
+ // previous round voting must have finalized
+ if !voteResult.Finalized {
+ return nil, errors.New("previous round voting has not finalized")
+ }
+
+ voteResult.Finalized = false
+ voteResult.LastBlockHash = bc.Hash{}
+ }
+ voteResultMap[seq] = voteResult
+ return voteResult, nil
+}
+
func (c *consensusNodeManager) detachBlock(voteResultMap map[uint64]*state.VoteResult, block *types.Block) error {
voteSeq := block.Height / roundVoteBlockNums
voteResult := voteResultMap[voteSeq]