-
Notifications
You must be signed in to change notification settings - Fork 0
/
block_serialization.go
218 lines (197 loc) · 5.74 KB
/
block_serialization.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
Copyright IBM Corp. 2016 All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package fsblkstorage
import (
"github.com/golang/protobuf/proto"
ledgerutil "github.com/hyperledger/fabric/common/ledger/util"
"github.com/hyperledger/fabric/protos/common"
"github.com/hyperledger/fabric/protos/utils"
)
type serializedBlockInfo struct {
blockHeader *common.BlockHeader
txOffsets []*txindexInfo
metadata *common.BlockMetadata
}
//The order of the transactions must be maintained for history
type txindexInfo struct {
txID string
loc *locPointer
}
func serializeBlock(block *common.Block) ([]byte, *serializedBlockInfo, error) {
buf := proto.NewBuffer(nil)
var err error
info := &serializedBlockInfo{}
info.blockHeader = block.Header
info.metadata = block.Metadata
if err = addHeaderBytes(block.Header, buf); err != nil {
return nil, nil, err
}
if info.txOffsets, err = addDataBytes(block.Data, buf); err != nil {
return nil, nil, err
}
if err = addMetadataBytes(block.Metadata, buf); err != nil {
return nil, nil, err
}
return buf.Bytes(), info, nil
}
func deserializeBlock(serializedBlockBytes []byte) (*common.Block, error) {
block := &common.Block{}
var err error
b := ledgerutil.NewBuffer(serializedBlockBytes)
if block.Header, err = extractHeader(b); err != nil {
return nil, err
}
if block.Data, _, err = extractData(b); err != nil {
return nil, err
}
if block.Metadata, err = extractMetadata(b); err != nil {
return nil, err
}
return block, nil
}
func extractSerializedBlockInfo(serializedBlockBytes []byte) (*serializedBlockInfo, error) {
info := &serializedBlockInfo{}
var err error
b := ledgerutil.NewBuffer(serializedBlockBytes)
info.blockHeader, err = extractHeader(b)
if err != nil {
return nil, err
}
_, info.txOffsets, err = extractData(b)
if err != nil {
return nil, err
}
info.metadata, err = extractMetadata(b)
if err != nil {
return nil, err
}
return info, nil
}
func addHeaderBytes(blockHeader *common.BlockHeader, buf *proto.Buffer) error {
if err := buf.EncodeVarint(blockHeader.Number); err != nil {
return err
}
if err := buf.EncodeRawBytes(blockHeader.DataHash); err != nil {
return err
}
if err := buf.EncodeRawBytes(blockHeader.PreviousHash); err != nil {
return err
}
return nil
}
func addDataBytes(blockData *common.BlockData, buf *proto.Buffer) ([]*txindexInfo, error) {
var txOffsets []*txindexInfo
if err := buf.EncodeVarint(uint64(len(blockData.Data))); err != nil {
return nil, err
}
for _, txEnvelopeBytes := range blockData.Data {
offset := len(buf.Bytes())
txid, err := extractTxID(txEnvelopeBytes)
if err != nil {
return nil, err
}
if err := buf.EncodeRawBytes(txEnvelopeBytes); err != nil {
return nil, err
}
idxInfo := &txindexInfo{txid, &locPointer{offset, len(buf.Bytes()) - offset}}
txOffsets = append(txOffsets, idxInfo)
}
return txOffsets, nil
}
func addMetadataBytes(blockMetadata *common.BlockMetadata, buf *proto.Buffer) error {
numItems := uint64(0)
if blockMetadata != nil {
numItems = uint64(len(blockMetadata.Metadata))
}
if err := buf.EncodeVarint(numItems); err != nil {
return err
}
for _, b := range blockMetadata.Metadata {
if err := buf.EncodeRawBytes(b); err != nil {
return err
}
}
return nil
}
func extractHeader(buf *ledgerutil.Buffer) (*common.BlockHeader, error) {
header := &common.BlockHeader{}
var err error
if header.Number, err = buf.DecodeVarint(); err != nil {
return nil, err
}
if header.DataHash, err = buf.DecodeRawBytes(false); err != nil {
return nil, err
}
if header.PreviousHash, err = buf.DecodeRawBytes(false); err != nil {
return nil, err
}
if len(header.PreviousHash) == 0 {
header.PreviousHash = nil
}
return header, nil
}
func extractData(buf *ledgerutil.Buffer) (*common.BlockData, []*txindexInfo, error) {
data := &common.BlockData{}
var txOffsets []*txindexInfo
var numItems uint64
var err error
if numItems, err = buf.DecodeVarint(); err != nil {
return nil, nil, err
}
for i := uint64(0); i < numItems; i++ {
var txEnvBytes []byte
var txid string
txOffset := buf.GetBytesConsumed()
if txEnvBytes, err = buf.DecodeRawBytes(false); err != nil {
return nil, nil, err
}
if txid, err = extractTxID(txEnvBytes); err != nil {
return nil, nil, err
}
data.Data = append(data.Data, txEnvBytes)
idxInfo := &txindexInfo{txid, &locPointer{txOffset, buf.GetBytesConsumed() - txOffset}}
txOffsets = append(txOffsets, idxInfo)
}
return data, txOffsets, nil
}
func extractMetadata(buf *ledgerutil.Buffer) (*common.BlockMetadata, error) {
metadata := &common.BlockMetadata{}
var numItems uint64
var metadataEntry []byte
var err error
if numItems, err = buf.DecodeVarint(); err != nil {
return nil, err
}
for i := uint64(0); i < numItems; i++ {
if metadataEntry, err = buf.DecodeRawBytes(false); err != nil {
return nil, err
}
metadata.Metadata = append(metadata.Metadata, metadataEntry)
}
return metadata, nil
}
func extractTxID(txEnvelopBytes []byte) (string, error) {
txEnvelope, err := utils.GetEnvelopeFromBlock(txEnvelopBytes)
if err != nil {
return "", err
}
txPayload, err := utils.GetPayload(txEnvelope)
if err != nil {
return "", nil
}
chdr, err := utils.UnmarshalChannelHeader(txPayload.Header.ChannelHeader)
if err != nil {
return "", err
}
return chdr.TxId, nil
}