-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathblockreader.go
146 lines (114 loc) · 4.23 KB
/
blockreader.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
package main
import (
"fmt"
"github.com/hyperledger/fabric/protos/common"
"github.com/hyperledger/fabric/protos/peer"
"github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/ledger/rwset/kvrwset"
"github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/ledger/rwset"
"github.com/golang/protobuf/proto"
"github.com/pkg/errors"
)
func ReadBlock(block *common.Block) error{
fmt.Println("\n ######################################################################## ")
blockHeader := block.Header
blockData := block.Data.Data
fmt.Println(" Received Block Number = ", blockHeader.Number)
//First Get the Envelope from the BlockData
envelope, err := GetEnvelopeFromBlock(blockData[0])
if err != nil {
return errors.WithMessage(err,"unmarshaling Envelope error: ")
}
//Retrieve the Payload from the Envelope
payload := &common.Payload{}
err = proto.Unmarshal(envelope.Payload, payload)
if err != nil {
return errors.WithMessage(err,"unmarshaling Payload error: ")
}
transaction := &peer.Transaction{}
err = proto.Unmarshal(payload.Data, transaction)
if err != nil {
return errors.WithMessage(err,"unmarshaling Payload Transaction error: ")
}
chaincodeActionPayload := &peer.ChaincodeActionPayload{}
err = proto.Unmarshal(transaction.Actions[0].Payload, chaincodeActionPayload)
if err != nil {
return errors.WithMessage(err,"unmarshaling Chaincode Action Payload error: ")
}
chaincodeProposalPayload := &peer.ChaincodeProposalPayload{}
err = proto.Unmarshal(chaincodeActionPayload.ChaincodeProposalPayload, chaincodeProposalPayload)
if err != nil {
return errors.WithMessage(err,"unmarshaling Chaincode Proposal Payload error: ")
}
// The Input field is marshalled object of ChaincodeInvocationSpec
input := &peer.ChaincodeInvocationSpec{}
err = proto.Unmarshal(chaincodeProposalPayload.Input, input)
if err != nil {
return errors.WithMessage(err,"unmarshaling Chaincode Proposal Payload Input error: ")
}
chaincodeArgs := make([]string, len(input.ChaincodeSpec.Input.Args))
for i, c := range input.ChaincodeSpec.Input.Args {
args := CToGoString(c[:])
chaincodeArgs[i] = args
}
fmt.Println("\n ## Chaincode ")
fmt.Println(" Name : ", input.ChaincodeSpec.ChaincodeId.Name)
fmt.Println(" Version : ", input.ChaincodeSpec.ChaincodeId.Version)
fmt.Println(" Args : ", chaincodeArgs)
proposalResponsePayload := &peer.ProposalResponsePayload{}
err = proto.Unmarshal(chaincodeActionPayload.Action.ProposalResponsePayload, proposalResponsePayload)
if err != nil {
return errors.WithMessage(err,"unmarshaling Proposal Response Payload error: ")
}
chaincodeAction := &peer.ChaincodeAction{}
err = proto.Unmarshal(proposalResponsePayload.Extension, chaincodeAction)
if err != nil {
return errors.WithMessage(err,"unmarshaling Extension error: ")
}
txReadWriteSet := &rwset.TxReadWriteSet{}
err = proto.Unmarshal(chaincodeAction.Results, txReadWriteSet)
if err != nil {
return errors.WithMessage(err,"unmarshaling txReadWriteSet error: ")
}
RwSet := txReadWriteSet.NsRwset[0].Rwset
kvrwset := &kvrwset.KVRWSet{}
err = proto.Unmarshal(RwSet, kvrwset)
if err != nil {
return errors.WithMessage(err,"unmarshaling kvrwset error: ")
}
fmt.Println("\n Block Read Write Set ")
if len(kvrwset.Reads) != 0 {
fmt.Println("\n ## KVRead Set ")
fmt.Println(" BlockNum = ",kvrwset.Reads[0].Version.BlockNum)
fmt.Println(" TxNum = ",kvrwset.Reads[0].Version.TxNum)
fmt.Println(" Key = ",kvrwset.Reads[0].Key)
}
if len(kvrwset.Writes) != 0 {
values := CToGoString(kvrwset.Writes[0].Value[:])
fmt.Println("\n ## KVWrite Set")
fmt.Println(" Key = ",kvrwset.Writes[0].Key)
fmt.Println(" Value = ",values)
err = SaveToCouchDB(kvrwset.Writes[0].Value)
if err != nil {
return errors.WithMessage(err,"error while saving to CouchDB")
}
}
return nil
}
func GetEnvelopeFromBlock(data []byte) (*common.Envelope, error){
var err error
env := &common.Envelope{}
if err = proto.Unmarshal(data, env); err != nil {
return nil, errors.Wrap(err, "error unmarshaling Envelope")
}
return env, nil
}
func CToGoString(c []byte) string {
n := -1
for i, b := range c {
if b == 0 {
break
}
n = i
}
return string(c[:n+1])
}