OSDN Git Service

add block witness update function (#176)
[bytom/vapor.git] / protocol / bc / types / block_witness.go
1 package types
2
3 import (
4         "io"
5
6         "github.com/vapor/encoding/blockchain"
7 )
8
9 type BlockWitness struct {
10         // Witness is a vector of arguments  for validating this block.
11         Witness [][]byte
12 }
13
14 func (bw *BlockWitness) writeTo(w io.Writer) error {
15         _, err := blockchain.WriteVarstrList(w, bw.Witness)
16         return err
17 }
18
19 func (bw *BlockWitness) readFrom(r *blockchain.Reader) (err error) {
20         bw.Witness, err = blockchain.ReadVarstrList(r)
21         return err
22 }
23
24 func (bw *BlockWitness) Set(index uint64, data []byte) {
25         if uint64(len(bw.Witness)) <= index {
26                 newWitness := make([][]byte, index+1, index+1)
27                 copy(newWitness, bw.Witness)
28                 bw.Witness = newWitness
29         }
30         bw.Witness[index] = data
31 }
32
33 func (bw *BlockWitness) Delete(index uint64) {
34         if uint64(len(bw.Witness)) > index {
35                 bw.Witness[index] = nil
36         }
37 }
38
39 func (bw *BlockWitness) Get(index uint64) []byte {
40         if uint64(len(bw.Witness)) > index {
41                 return bw.Witness[index]
42         }
43         return nil
44 }