This repository has been archived by the owner on Mar 6, 2024. It is now read-only.
forked from s7techlab/hlf-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 2
/
tx_action.go
189 lines (153 loc) · 5.97 KB
/
tx_action.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
package proto
import (
"fmt"
"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric-protos-go/ledger/rwset"
"github.com/hyperledger/fabric-protos-go/ledger/rwset/kvrwset"
"github.com/hyperledger/fabric-protos-go/msp"
"github.com/hyperledger/fabric-protos-go/peer"
"github.com/hyperledger/fabric/protoutil"
)
func ParseTxActions(txActions []*peer.TransactionAction) ([]*TransactionAction, error) {
var parsedTxActions []*TransactionAction
for _, action := range txActions {
txAction, err := ParseTxAction(action)
if err != nil {
return nil, fmt.Errorf(`tx action: %w`, err)
}
parsedTxActions = append(parsedTxActions, txAction)
}
return parsedTxActions, nil
}
func ParseTxAction(txAction *peer.TransactionAction) (*TransactionAction, error) {
sigHeader, err := protoutil.UnmarshalSignatureHeader(txAction.Header)
if err != nil {
return nil, fmt.Errorf("get signature header: %w", err)
}
creator, err := protoutil.UnmarshalSerializedIdentity(sigHeader.Creator)
if err != nil {
return nil, fmt.Errorf("parse transaction creator: %w", err)
}
ccAction, err := ParseChaincodeAction(txAction)
if err != nil {
return nil, fmt.Errorf("parse transaction chaincode action: %w", err)
}
ccEvent, err := ParseTransactionActionEvents(ccAction)
if err != nil {
return nil, fmt.Errorf("parse transaction events: %w", err)
}
endorsers, err := ParseTransactionActionEndorsers(txAction)
if err != nil {
return nil, fmt.Errorf("parse transaction endorsers: %w", err)
}
rwSets, err := ParseTransactionActionReadWriteSet(ccAction)
if err != nil {
return nil, fmt.Errorf("parse transaction read/write sets: %w", err)
}
chaincodeInvocationSpec, err := ParseTransactionActionChaincode(txAction)
if err != nil {
return nil, fmt.Errorf("parse transaction chaincode invocation spec: %w", err)
}
payload, err := ParseTxPayload(txAction)
if err != nil {
return nil, fmt.Errorf("parse transaction payload: %w", err)
}
// because there is no cc version in peer.ChaincodeInvocationSpec
if chaincodeInvocationSpec.ChaincodeSpec == nil {
chaincodeInvocationSpec.ChaincodeSpec = &peer.ChaincodeSpec{}
}
if chaincodeInvocationSpec.ChaincodeSpec.ChaincodeId == nil {
chaincodeInvocationSpec.ChaincodeSpec.ChaincodeId = &peer.ChaincodeID{}
}
if ccAction.ChaincodeId != nil {
chaincodeInvocationSpec.ChaincodeSpec.ChaincodeId.Version = ccAction.ChaincodeId.Version
}
parsedTxAction := &TransactionAction{
Event: ccEvent,
Endorsers: endorsers,
ReadWriteSets: rwSets,
ChaincodeInvocationSpec: chaincodeInvocationSpec,
CreatorIdentity: creator,
Payload: payload,
}
return parsedTxAction, nil
}
func ParseTxPayload(txAction *peer.TransactionAction) ([]byte, error) {
payload, _, err := protoutil.GetPayloads(txAction)
if err != nil {
return nil, fmt.Errorf(`get payload from tx`)
}
if payload.GetAction() == nil {
return nil, nil
}
return payload.GetAction().GetProposalResponsePayload(), nil
}
func ParseChaincodeAction(txAction *peer.TransactionAction) (*peer.ChaincodeAction, error) {
ccActionPayload, err := protoutil.UnmarshalChaincodeActionPayload(txAction.Payload)
if err != nil {
return nil, fmt.Errorf(`chaincode action payload: %w`, err)
}
proposalResponsePayload, err := protoutil.UnmarshalProposalResponsePayload(
ccActionPayload.Action.ProposalResponsePayload)
if err != nil {
return nil, fmt.Errorf(`proposal response payload: %w`, err)
}
chaincodeAction, err := protoutil.UnmarshalChaincodeAction(proposalResponsePayload.Extension)
if err != nil {
return nil, fmt.Errorf(`chaincode action from proposal response: %w`, err)
}
return chaincodeAction, nil
}
func ParseTransactionActionEvents(chaincodeAction *peer.ChaincodeAction) (*peer.ChaincodeEvent, error) {
ccEvent, err := protoutil.UnmarshalChaincodeEvents(chaincodeAction.Events)
if err != nil {
return nil, fmt.Errorf(`event from chaincode action: %w`, err)
}
return ccEvent, nil
}
func ParseTransactionActionEndorsers(txAction *peer.TransactionAction) ([]*msp.SerializedIdentity, error) {
ccActionPayload, err := protoutil.UnmarshalChaincodeActionPayload(txAction.Payload)
if err != nil {
return nil, fmt.Errorf(`chaincode action payload: %w`, err)
}
endorsers := make([]*msp.SerializedIdentity, 0)
for _, en := range ccActionPayload.Action.Endorsements {
endorser := &msp.SerializedIdentity{}
if err = proto.Unmarshal(en.Endorser, endorser); err != nil {
return nil, fmt.Errorf("get endorser: %w", err)
}
endorsers = append(endorsers, endorser)
}
return endorsers, nil
}
func ParseTransactionActionReadWriteSet(chaincodeAction *peer.ChaincodeAction) ([]*kvrwset.KVRWSet, error) {
txReadWriteSet := &rwset.TxReadWriteSet{}
if err := proto.Unmarshal(chaincodeAction.Results, txReadWriteSet); err != nil {
return nil, fmt.Errorf("get txReadWriteSet: %w", err)
}
kvReadWriteSets := make([]*kvrwset.KVRWSet, 0)
for _, rw := range txReadWriteSet.NsRwset {
kvReadWriteSet := &kvrwset.KVRWSet{}
if err := proto.Unmarshal(rw.Rwset, kvReadWriteSet); err != nil {
return nil, fmt.Errorf("get kvReadWriteSet: %w", err)
}
kvReadWriteSets = append(kvReadWriteSets, kvReadWriteSet)
}
return kvReadWriteSets, nil
}
func ParseTransactionActionChaincode(txAction *peer.TransactionAction) (*peer.ChaincodeInvocationSpec, error) {
actionPayload, err := protoutil.UnmarshalChaincodeActionPayload(txAction.Payload)
if err != nil {
return nil, fmt.Errorf("get chaincode action from action payload: %w", err)
}
chaincodeProposalPayload, err := protoutil.UnmarshalChaincodeProposalPayload(actionPayload.ChaincodeProposalPayload)
if err != nil {
return nil, fmt.Errorf("get chaincode proposal from action payload: %w", err)
}
// todo transient map could be fetched here
chaincodeInvocationSpec, err := protoutil.UnmarshalChaincodeInvocationSpec(chaincodeProposalPayload.Input)
if err != nil {
return nil, fmt.Errorf("get chaincode invocation spec from action payload: %w", err)
}
return chaincodeInvocationSpec, nil
}