OSDN Git Service

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