/
block_meta.go
138 lines (123 loc) · 3.4 KB
/
block_meta.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package types
import (
"bytes"
"fmt"
"github.com/tendermint/go-amino"
"github.com/pkg/errors"
)
// BlockMeta contains meta information.
type BlockMeta struct {
BlockID BlockID `json:"block_id"`
BlockSize int `json:"block_size"`
Header Header `json:"header"`
NumTxs int `json:"num_txs"`
}
// NewBlockMeta returns a new BlockMeta.
func NewBlockMeta(block *Block, blockParts *PartSet) *BlockMeta {
return &BlockMeta{
BlockID: BlockID{block.Hash(), blockParts.Header()},
BlockSize: block.Size(),
Header: block.Header,
NumTxs: len(block.Data.Txs),
}
}
func (bm *BlockMeta) UnmarshalFromAmino(cdc *amino.Codec, data []byte) error {
const fieldCount = 4
var currentField int
var currentType amino.Typ3
var err error
for cur := 1; cur <= fieldCount; cur++ {
if len(data) != 0 && (currentField == 0 || currentField < cur) {
var nextField int
if nextField, currentType, err = amino.ParseProtoPosAndTypeMustOneByte(data[0]); err != nil {
return err
}
if nextField < currentField {
return errors.Errorf("next field should greater than %d, got %d", currentField, nextField)
} else {
currentField = nextField
}
}
if len(data) == 0 || currentField != cur {
switch cur {
case 1:
bm.BlockID = BlockID{}
case 2:
bm.BlockSize = 0
case 3:
bm.Header = Header{}
case 4:
bm.NumTxs = 0
default:
return fmt.Errorf("unexpect feild num %d", cur)
}
} else {
pbk := data[0]
data = data[1:]
var subData []byte
if currentType == amino.Typ3_ByteLength {
if subData, err = amino.DecodeByteSliceWithoutCopy(&data); err != nil {
return err
}
}
switch pbk {
case 1<<3 | byte(amino.Typ3_ByteLength):
if err = bm.BlockID.UnmarshalFromAmino(cdc, subData); err != nil {
return err
}
case 2<<3 | byte(amino.Typ3_Varint):
if bm.BlockSize, err = amino.DecodeIntUpdateBytes(&data); err != nil {
return err
}
case 3<<3 | byte(amino.Typ3_ByteLength):
if err = bm.Header.UnmarshalFromAmino(cdc, subData); err != nil {
return err
}
case 4<<3 | byte(amino.Typ3_Varint):
if bm.NumTxs, err = amino.DecodeIntUpdateBytes(&data); err != nil {
return err
}
default:
return fmt.Errorf("unexpect pb key %d", pbk)
}
}
}
if len(data) != 0 {
return errors.Errorf("unexpect data remain %X", data)
}
return nil
}
//-----------------------------------------------------------
// These methods are for Protobuf Compatibility
// Size returns the size of the amino encoding, in bytes.
func (bm *BlockMeta) Size() int {
bs, _ := bm.Marshal()
return len(bs)
}
// Marshal returns the amino encoding.
func (bm *BlockMeta) Marshal() ([]byte, error) {
return cdc.MarshalBinaryBare(bm)
}
// MarshalTo calls Marshal and copies to the given buffer.
func (bm *BlockMeta) MarshalTo(data []byte) (int, error) {
bs, err := bm.Marshal()
if err != nil {
return -1, err
}
return copy(data, bs), nil
}
// Unmarshal deserializes from amino encoded form.
func (bm *BlockMeta) Unmarshal(bs []byte) error {
return cdc.UnmarshalBinaryBare(bs, bm)
}
// ValidateBasic performs basic validation.
func (bm *BlockMeta) ValidateBasic() error {
if err := bm.BlockID.ValidateBasic(); err != nil {
return err
}
if !bytes.Equal(bm.BlockID.Hash, bm.Header.Hash()) {
return errors.Errorf("expected BlockID#Hash and Header#Hash to be the same, got %X != %X",
bm.BlockID.Hash, bm.Header.Hash())
}
return nil
}