From f47413c46121216dae3b1a905fbe53ae559adee2 Mon Sep 17 00:00:00 2001 From: Chengcheng Zhang <943420582@qq.com> Date: Thu, 13 Jun 2019 21:53:32 +0800 Subject: [PATCH] add block witness update function (#176) * add block witness update function * add Delete and test function * update * update * update * update --- protocol/bbft.go | 15 +++---- protocol/bc/types/block_witness.go | 22 ++++++++++ protocol/bc/types/block_witness_test.go | 73 +++++++++++++++++++++++++++++++++ 3 files changed, 103 insertions(+), 7 deletions(-) diff --git a/protocol/bbft.go b/protocol/bbft.go index f42e5f1a..c6fc3b70 100644 --- a/protocol/bbft.go +++ b/protocol/bbft.go @@ -114,18 +114,18 @@ func (c *Chain) validateSign(block *types.Block) error { continue } - if block.Witness[node.Order] == nil { + if block.Get(node.Order) == nil { cachekey := signCacheKey(blockHash.String(), pubKey) if signature, ok := c.signatureCache.Get(cachekey); ok { - block.Witness[node.Order] = signature.([]byte) + block.Set(node.Order, signature.([]byte)) } else { continue } } - if err := c.checkNodeSign(&block.BlockHeader, node, block.Witness[node.Order]); err == errDoubleSignBlock { + if err := c.checkNodeSign(&block.BlockHeader, node, block.Get(node.Order)); err == errDoubleSignBlock { log.WithFields(log.Fields{"module": logModule, "blockHash": blockHash.String(), "pubKey": pubKey}).Warn("the consensus node double sign the same height of different block") - block.Witness[node.Order] = nil + block.Delete(node.Order) continue } else if err != nil { return err @@ -203,10 +203,10 @@ func (c *Chain) SignBlock(block *types.Block) ([]byte, error) { } } - signature := block.Witness[node.Order] + signature := block.Get(node.Order) if len(signature) == 0 { signature = xprv.Sign(block.Hash().Bytes()) - block.Witness[node.Order] = signature + block.Set(node.Order, signature) } return signature, nil } @@ -221,7 +221,8 @@ func (c *Chain) updateBlockSignature(blockNode *state.BlockNode, nodeOrder uint6 return err } - block.Witness[nodeOrder] = signature + block.Set(nodeOrder, signature) + txStatus, err := c.store.GetTransactionStatus(&blockNode.Hash) if err != nil { return err diff --git a/protocol/bc/types/block_witness.go b/protocol/bc/types/block_witness.go index e4bb2ac7..52f3e0e4 100644 --- a/protocol/bc/types/block_witness.go +++ b/protocol/bc/types/block_witness.go @@ -20,3 +20,25 @@ func (bw *BlockWitness) readFrom(r *blockchain.Reader) (err error) { bw.Witness, err = blockchain.ReadVarstrList(r) return err } + +func (bw *BlockWitness) Set(index uint64, data []byte) { + if uint64(len(bw.Witness)) <= index { + newWitness := make([][]byte, index+1, index+1) + copy(newWitness, bw.Witness) + bw.Witness = newWitness + } + bw.Witness[index] = data +} + +func (bw *BlockWitness) Delete(index uint64) { + if uint64(len(bw.Witness)) > index { + bw.Witness[index] = nil + } +} + +func (bw *BlockWitness) Get(index uint64) []byte { + if uint64(len(bw.Witness)) > index { + return bw.Witness[index] + } + return nil +} diff --git a/protocol/bc/types/block_witness_test.go b/protocol/bc/types/block_witness_test.go index 048a629c..9dc53b94 100644 --- a/protocol/bc/types/block_witness_test.go +++ b/protocol/bc/types/block_witness_test.go @@ -58,3 +58,76 @@ func TestReadWriteBlockWitness(t *testing.T) { } } } + +func TestBlockWitnessSet(t *testing.T) { + cases := []struct { + bw BlockWitness + index uint64 + data []byte + want BlockWitness + }{ + { + bw: BlockWitness{Witness: [][]byte{}}, + index: uint64(0), + data: []byte{0x01, 0x02, 0x03, 0x04}, + want: BlockWitness{Witness: [][]byte{[]byte{0x01, 0x02, 0x03, 0x04}}}, + }, + { + bw: BlockWitness{Witness: [][]byte{[]byte{0x01, 0x02, 0x03, 0x04}}}, + index: uint64(1), + data: []byte{0x01, 0x01, 0x01, 0x01}, + want: BlockWitness{Witness: [][]byte{[]byte{0x01, 0x02, 0x03, 0x04}, []byte{0x01, 0x01, 0x01, 0x01}}}, + }, + { + bw: BlockWitness{Witness: [][]byte{[]byte{0x01, 0x02, 0x03, 0x04}, []byte{0x01, 0x02, 0x03, 0x04}}}, + index: uint64(4), + data: []byte{0x04, 0x04, 0x04, 0x04}, + want: BlockWitness{Witness: [][]byte{[]byte{0x01, 0x02, 0x03, 0x04}, []byte{0x01, 0x02, 0x03, 0x04}, []byte{}, []byte{}, []byte{0x04, 0x04, 0x04, 0x04}}}, + }, + } + + for i, c := range cases { + newbw := c.bw + newbw.Set(c.index, c.data) + if !testutil.DeepEqual(c.want, newbw) { + t.Errorf("update result mismatch: %v, got:%v, want:%v", i, newbw, c.want) + } + } +} + +func TestBlockWitnessDelete(t *testing.T) { + cases := []struct { + bw BlockWitness + index uint64 + want BlockWitness + }{ + { + bw: BlockWitness{Witness: [][]byte{}}, + index: uint64(0), + want: BlockWitness{Witness: [][]byte{}}, + }, + { + bw: BlockWitness{Witness: [][]byte{[]byte{0x01, 0x02, 0x03, 0x04}}}, + index: uint64(0), + want: BlockWitness{Witness: [][]byte{[]byte{}}}, + }, + { + bw: BlockWitness{Witness: [][]byte{[]byte{0x01, 0x02, 0x03, 0x04}, []byte{0x01, 0x02, 0x03, 0x04}}}, + index: uint64(1), + want: BlockWitness{Witness: [][]byte{[]byte{0x01, 0x02, 0x03, 0x04}, []byte{}}}, + }, + { + bw: BlockWitness{Witness: [][]byte{[]byte{0x01, 0x02, 0x03, 0x04}, []byte{0x01, 0x02, 0x03, 0x04}}}, + index: uint64(100), + want: BlockWitness{Witness: [][]byte{[]byte{0x01, 0x02, 0x03, 0x04}, []byte{0x01, 0x02, 0x03, 0x04}}}, + }, + } + + for i, c := range cases { + newbw := c.bw + newbw.Delete(c.index) + if !testutil.DeepEqual(c.want, newbw) { + t.Errorf("update result mismatch: %v, got:%v, want:%v", i, newbw, c.want) + } + } +} -- 2.11.0