-
Notifications
You must be signed in to change notification settings - Fork 4
/
parseutil.go
337 lines (304 loc) · 9.04 KB
/
parseutil.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
package client
import (
"bytes"
"fmt"
"encoding/pem"
"crypto/x509"
"encoding/hex"
"github.com/jonluo94/cool/json"
"github.com/hyperledger/fabric/protos/utils"
"github.com/hyperledger/fabric/common/configtx"
"github.com/hyperledger/fabric/protos/common"
"github.com/hyperledger/fabric/protos/peer"
"github.com/hyperledger/fabric/core/ledger/util"
"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/rwsetutil"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/fab"
cm "github.com/hyperledger/fabric-protos-go/common"
"github.com/hyperledger/fabric-protos-go/msp"
)
type FabricBlockchainInfo struct {
Number uint64 `json:"number"`
CurrentBlockHash string `json:"currentBlockHash"`
PreviousBlockHash string `json:"previousBlockHash"`
}
type FabricBlock struct {
FabricBlockchainInfo
Transactions []FabricTransaction `json:"transactions"`
}
type FabricTransaction struct {
No int64 `json:"no"`
Status string `json:"status"`
Txid string `json:"txid"`
Channel string `json:"channel"`
Type string `json:"type"`
Subject string `json:"subject"`
Config string `json:"config"`
Timestamp int64 `json:"timestamp"`
Actions []FabricTransactionAction `json:"actions"`
}
type FabricChannelConfig struct {
Id string `json:"id"`
BlockNumber uint64 `json:"blockNumber"`
MSPs []*msp.MSPConfig `json:"msps"`
AnchorPeers []*fab.OrgAnchorPeer `json:"anchorPeers"`
Orderers []string `json:"orderers"`
}
type FabricTransactionAction struct {
Endorsers []string `json:"endorsers"`
RWSet []FabricTransactionActionRWSet `json:"rwSet"`
}
type FabricTransactionActionRWSet struct {
Cc string `json:"cc"`
RSet []string `json:"rSet"`
WSet []string `json:"wSet"`
}
func parseFabricBlockchainInfo(info *fab.BlockchainInfoResponse) *FabricBlockchainInfo {
return &FabricBlockchainInfo{
Number: info.BCI.Height,
CurrentBlockHash: hex.EncodeToString(info.BCI.CurrentBlockHash),
PreviousBlockHash: hex.EncodeToString(info.BCI.PreviousBlockHash),
}
}
func blockParse(block *cm.Block) *common.Block {
if block == nil {
return nil
}
cmBlock := new(common.Block)
cmBlock.Data = &common.BlockData{
Data: block.Data.Data,
}
cmBlock.Header = &common.BlockHeader{
Number: block.Header.Number,
PreviousHash: block.Header.PreviousHash,
DataHash: block.Header.DataHash,
}
cmBlock.Metadata = &common.BlockMetadata{
Metadata: block.Metadata.Metadata,
}
return cmBlock
}
func decodeSerializedIdentity(creator []byte) (string, error) {
certStart := bytes.Index(creator, []byte("-----BEGIN"))
if certStart == -1 {
return "", fmt.Errorf("No certificate found")
}
certText := creator[certStart:]
bl, _ := pem.Decode(certText)
if bl == nil {
return "", fmt.Errorf("Could not decode the PEM structure")
}
cert, err := x509.ParseCertificate(bl.Bytes)
if err != nil {
return "", err
}
uname := cert.Subject.CommonName
return uname, nil
}
func parseFabricBlock(block *common.Block) (*FabricBlock, error) {
if block == nil {
return nil, nil
}
var err error
faBlock := new(FabricBlock)
trans := make([]FabricTransaction, 0)
// Handle header
faBlock.Number = block.GetHeader().Number
faBlock.CurrentBlockHash = hex.EncodeToString(block.GetHeader().Hash())
faBlock.PreviousBlockHash = hex.EncodeToString(block.GetHeader().PreviousHash)
// Handle transaction
var tranNo int64 = -1
txsFilter := util.TxValidationFlags(block.Metadata.Metadata[common.BlockMetadataIndex_TRANSACTIONS_FILTER])
if len(txsFilter) == 0 {
txsFilter = util.NewTxValidationFlags(len(block.Data.Data))
block.Metadata.Metadata[common.BlockMetadataIndex_TRANSACTIONS_FILTER] = txsFilter
}
for _, envBytes := range block.Data.Data {
tran := FabricTransaction{}
tranNo++
tran.No = tranNo
if txsFilter.IsInvalid(int(tranNo)) {
tran.Status = "INVALID"
continue
} else {
tran.Status = "VALID"
}
var env *common.Envelope
if env, err = utils.GetEnvelopeFromBlock(envBytes); err != nil {
return nil, err
}
var payload *common.Payload
if payload, err = utils.GetPayload(env); err != nil {
return nil, err
}
var chdr *common.ChannelHeader
chdr, err = utils.UnmarshalChannelHeader(payload.Header.ChannelHeader)
if err != nil {
return nil, err
}
tran.Txid = chdr.TxId
tran.Channel = chdr.ChannelId
tran.Timestamp = chdr.Timestamp.GetSeconds()
var shdr *common.SignatureHeader
shdr, err = utils.GetSignatureHeader(payload.Header.SignatureHeader)
if err != nil {
return nil, err
}
var subject string
subject, err = decodeSerializedIdentity(shdr.Creator)
if err != nil {
return nil, err
}
tran.Subject = subject
if common.HeaderType(chdr.Type) == common.HeaderType_CONFIG {
tran.Type = "CONFIG"
conf, err := parseConfig(payload)
if err != nil {
return nil, err
}
tran.Config = string(conf)
} else if common.HeaderType(chdr.Type) == common.HeaderType_ENDORSER_TRANSACTION {
tran.Type = "ENDORSER_TRANSACTION"
actions, err := parseFabricTransactionAction(payload)
if err != nil {
return nil, err
}
tran.Actions = actions
} else {
tran.Type = "UNKNOWN"
}
trans = append(trans, tran)
}
faBlock.Transactions = trans
return faBlock, nil
}
func parseFabricTransactionAction(payload *common.Payload) ([]FabricTransactionAction, error) {
var err error
var tx *peer.Transaction
if tx, err = utils.GetTransaction(payload.Data); err != nil {
return nil, err
}
actions := make([]FabricTransactionAction, len(tx.Actions))
for i, action := range tx.Actions {
act := FabricTransactionAction{}
var ca *peer.ChaincodeAction
var capayload *peer.ChaincodeActionPayload
capayload, ca, err = utils.GetPayloads(action)
if err != nil {
return nil, err
}
endorsers := make([]string, len(capayload.Action.Endorsements))
for j, endorser := range capayload.Action.Endorsements {
var subject string
subject, err = decodeSerializedIdentity(endorser.Endorser)
if err != nil {
return nil, err
}
endorsers[j] = subject
}
act.Endorsers = endorsers
txRWSet := &rwsetutil.TxRwSet{}
err = txRWSet.FromProtoBytes(ca.Results)
if err != nil {
return nil, err
}
rwSets := make([]FabricTransactionActionRWSet, 0)
for _, nsRWSet := range txRWSet.NsRwSets {
ns := nsRWSet.NameSpace
if ns != "lscc" { // skip system chaincode
rwSet := FabricTransactionActionRWSet{}
rwSet.Cc = ns
rset := make([]string, len(nsRWSet.KvRwSet.Reads))
for i, kvRead := range nsRWSet.KvRwSet.Reads {
rset[i] = fmt.Sprintf("key=%v,version=%v", kvRead.Key, kvRead.Version)
}
rwSet.RSet = rset
wset := make([]string, len(nsRWSet.KvRwSet.Writes))
for i, kvWrite := range nsRWSet.KvRwSet.Writes {
wset[i] = fmt.Sprintf("key=%v,isDelete=%v,value=%v", kvWrite.Key, kvWrite.IsDelete, string(kvWrite.Value))
}
rwSet.WSet = wset
rwSets = append(rwSets, rwSet)
}
}
act.RWSet = rwSets
actions[i] = act
}
return actions, nil
}
func parseConfig(payload *common.Payload) ([]byte, error) {
var err error
var configEnvelope *common.ConfigEnvelope
configEnvelope, err = configtx.UnmarshalConfigEnvelope(payload.Data)
if err != nil {
return nil, err
}
config := configEnvelope.Config
conf, err := json.Marshal(config)
if err != nil {
return nil, err
}
return conf, nil
}
func parseChannelConfig(cfg fab.ChannelCfg) *FabricChannelConfig {
return &FabricChannelConfig{
Id: cfg.ID(),
BlockNumber: cfg.BlockNumber(),
MSPs: cfg.MSPs(),
AnchorPeers: cfg.AnchorPeers(),
Orderers: cfg.Orderers(),
}
}
func parseFabricTransaction(code int32, envPayload, envSignature []byte) (*FabricTransaction, error) {
tran := new(FabricTransaction)
if code == 0 {
tran.Status = "VALID"
} else {
tran.Status = "INVALID"
}
env := &common.Envelope{
Payload: envPayload,
Signature: envSignature,
}
var err error
pay, err := utils.GetPayload(env)
if err != nil {
return nil, err
}
var chdr *common.ChannelHeader
chdr, err = utils.UnmarshalChannelHeader(pay.Header.ChannelHeader)
if err != nil {
return nil, err
}
tran.Txid = chdr.TxId
tran.Channel = chdr.ChannelId
tran.Timestamp = chdr.Timestamp.GetSeconds()
var shdr *common.SignatureHeader
shdr, err = utils.GetSignatureHeader(pay.Header.SignatureHeader)
if err != nil {
return nil, err
}
var subject string
subject, err = decodeSerializedIdentity(shdr.Creator)
if err != nil {
return nil, err
}
tran.Subject = subject
if common.HeaderType(chdr.Type) == common.HeaderType_CONFIG {
tran.Type = "CONFIG"
conf, err := parseConfig(pay)
if err != nil {
return nil, err
}
tran.Config = string(conf)
} else if common.HeaderType(chdr.Type) == common.HeaderType_ENDORSER_TRANSACTION {
tran.Type = "ENDORSER_TRANSACTION"
actions, err := parseFabricTransactionAction(pay)
if err != nil {
return nil, err
}
tran.Actions = actions
} else {
tran.Type = "UNKNOWN"
}
return tran, nil
}