OSDN Git Service

add block witness update function (#176)
authorChengcheng Zhang <943420582@qq.com>
Thu, 13 Jun 2019 13:53:32 +0000 (21:53 +0800)
committerPaladz <yzhu101@uottawa.ca>
Thu, 13 Jun 2019 13:53:32 +0000 (21:53 +0800)
* add block witness update function

* add Delete and test function

* update

* update

* update

* update

protocol/bbft.go
protocol/bc/types/block_witness.go
protocol/bc/types/block_witness_test.go

index f42e5f1..c6fc3b7 100644 (file)
@@ -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
index e4bb2ac..52f3e0e 100644 (file)
@@ -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
+}
index 048a629..9dc53b9 100644 (file)
@@ -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)
+               }
+       }
+}