@@ -21,45 +21,135 @@ import (
21
21
"github.com/op/go-logging"
22
22
"golang.org/x/net/context"
23
23
24
+ "github.com/hyperledger/fabric/core/chaincode"
24
25
"github.com/hyperledger/fabric/core/peer"
25
26
"github.com/hyperledger/fabric/core/util"
26
27
pb "github.com/hyperledger/fabric/protos"
27
28
)
28
29
29
30
var devopsLogger = logging .MustGetLogger ("devops" )
30
31
32
+ // The Jira issue that documents Endorser flow along with its relationship to
33
+ // the lifecycle chaincode - https://jira.hyperledger.org/browse/FAB-181
34
+
35
+ // Endorser provides the Endorser service ProcessProposal
31
36
type Endorser struct {
32
37
coord peer.MessageHandlerCoordinator
33
38
}
34
39
35
- // NewDevopsServer creates and returns a new Devops server instance.
40
+ // NewEndorserServer creates and returns a new Endorser server instance.
36
41
func NewEndorserServer (coord peer.MessageHandlerCoordinator ) pb.EndorserServer {
37
42
e := new (Endorser )
38
43
e .coord = coord
39
44
return e
40
45
}
41
46
42
- // ProcessProposal process the Proposal
43
- func (e * Endorser ) ProcessProposal (ctx context.Context , proposal * pb.Proposal ) (* pb.ProposalResponse , error ) {
44
- // if err := crypto.RegisterClient(secret.EnrollId, nil, secret.EnrollId, secret.EnrollSecret); nil != err {
45
- // return &pb.Response{Status: pb.Response_FAILURE, Msg: []byte(err.Error())}, nil
46
- // }
47
+ //get the ChaincodeInvocationSpec from the proposal
48
+ func (* Endorser ) getChaincodeInvocationSpec (prop * pb.Proposal ) (* pb.ChaincodeInvocationSpec , error ) {
49
+ cis := & pb.ChaincodeInvocationSpec {}
50
+ err := proto .Unmarshal (prop .Payload , cis )
51
+ if err != nil {
52
+ return nil , err
53
+ }
54
+ return cis , nil
55
+ }
47
56
48
- // Create a dummy action
49
- action := & pb.Action {ProposalHash : util .ComputeCryptoHash (proposal .Payload ), SimulationResult : []byte ("TODO: Simulated Result" )}
57
+ //TODO - what would Endorser's ACL be ?
58
+ func (* Endorser ) checkACL (prop * pb.Proposal ) error {
59
+ return nil
60
+ }
50
61
51
- actionBytes , err := proto .Marshal (action )
62
+ //TODO - check for escc and vscc
63
+ func (* Endorser ) checkEsccAndVscc (prop * pb.Proposal ) error {
64
+ return nil
65
+ }
66
+
67
+ //call specified chaincode (system or user)
68
+ func (* Endorser ) callChaincode (cis * pb.ChaincodeInvocationSpec ) ([]byte , error ) {
69
+ //TODO - get chainname from cis when defined
70
+ chainName := string (chaincode .DefaultChain )
71
+ b , err := chaincode .ExecuteChaincode (pb .Transaction_CHAINCODE_INVOKE , chainName , cis .ChaincodeSpec .ChaincodeID .Name , cis .ChaincodeSpec .CtorMsg .Args )
72
+ return b , err
73
+ }
74
+
75
+ //simulate the proposal by calling the chaincode
76
+ func (e * Endorser ) simulateProposal (prop * pb.Proposal ) ([]byte , []byte , error ) {
77
+ //we do expect the payload to be a ChaincodeInvocationSpec
78
+ //if we are supporting other payloads in future, this be glaringly point
79
+ //as something that should change
80
+ cis , err := e .getChaincodeInvocationSpec (prop )
52
81
if err != nil {
53
- return nil , err
82
+ return nil , nil , err
83
+ }
84
+ //---1. check ACL
85
+ if err = e .checkACL (prop ); err != nil {
86
+ return nil , nil , err
87
+ }
88
+
89
+ //---2. check ESCC and VSCC for the chaincode
90
+ if err = e .checkEsccAndVscc (prop ); err != nil {
91
+ return nil , nil , err
54
92
}
55
93
56
- sig , err := e .coord .GetSecHelper ().Sign (actionBytes )
94
+ //---3. execute the proposal
95
+ var resp []byte
96
+ resp , err = e .callChaincode (cis )
57
97
if err != nil {
58
- return nil , err
98
+ return nil , nil , err
59
99
}
60
100
61
- endorsement := & pb.Endorsement {Signature : sig }
101
+ //---4. get simulation results
102
+
103
+ simulationResult := []byte ("TODO: sim results" )
104
+ return resp , simulationResult , nil
105
+ }
106
+
107
+ //endorse the proposal by calling the ESCC
108
+ func (e * Endorser ) endorseProposal (proposal * pb.Proposal ) (* pb.Endorsement , error ) {
109
+ /************ TODO
110
+ //---4. call ESCC
111
+ args := util.ToChaincodeArgs("", "serialized_action", "serialized_proposal", "any", "other", "args")
112
+ ecccis := &pb.ChaincodeInvocationSpec{ ChaincodeSpec: &pb.ChaincodeSpec{ Type: pb.ChaincodeSpec_GOLANG, ChaincodeID: &pb.ChaincodeID{ Name: "escc" }, CtorMsg: &pb.ChaincodeInput{ Args: args }}}
62
113
114
+ var sig []byte
115
+ sig, err = e.callChaincode(ecccis)
116
+ if err != nil {
117
+ return err
118
+ }
119
+ ************/
120
+
121
+ endorsement := & pb.Endorsement {Signature : []byte ("TODO Signature" )}
122
+ return endorsement , nil
123
+ }
124
+
125
+ // ProcessProposal process the Proposal
126
+ func (e * Endorser ) ProcessProposal (ctx context.Context , prop * pb.Proposal ) (* pb.ProposalResponse , error ) {
127
+ //1 -- simulate
128
+ //TODO what do we do with response ? We need it for Invoke responses for sure
129
+ //Which field in PayloadResponse will carry return value ?
130
+ _ , simulationResult , err := e .simulateProposal (prop )
131
+ if err != nil {
132
+ return & pb.ProposalResponse {Response : & pb.Response2 {Status : 500 , Message : err .Error ()}}, err
133
+ }
134
+
135
+ //2 -- endorse
136
+ //TODO what do we do with response ? We need it for Invoke responses for sure
137
+ endorsement , err := e .endorseProposal (prop )
138
+ if err != nil {
139
+ return & pb.ProposalResponse {Response : & pb.Response2 {Status : 500 , Message : err .Error ()}}, err
140
+ }
141
+
142
+ //3 -- respond
143
+ // Create action
144
+ action := & pb.Action {ProposalHash : util .ComputeCryptoHash (prop .Payload ), SimulationResult : simulationResult }
145
+
146
+ actionBytes , err := proto .Marshal (action )
147
+ if err != nil {
148
+ return nil , err
149
+ }
150
+
151
+ //TODO when we have additional field in response, use "resp" bytes from the simulation
63
152
resp := & pb.Response2 {Status : 200 , Message : "Proposal accepted" }
153
+
64
154
return & pb.ProposalResponse {Response : resp , ActionBytes : actionBytes , Endorsement : endorsement }, nil
65
155
}
0 commit comments