@@ -22,13 +22,13 @@ limitations under the License.
22
22
package chaincode
23
23
24
24
import (
25
- "errors"
26
25
"fmt"
27
26
28
27
"github.com/op/go-logging"
29
28
30
29
"github.com/hyperledger/fabric/core/chaincode/shim"
31
30
"github.com/hyperledger/fabric/core/peer"
31
+ pb "github.com/hyperledger/fabric/protos/peer"
32
32
"github.com/hyperledger/fabric/protos/utils"
33
33
)
34
34
@@ -50,10 +50,9 @@ const (
50
50
// Init is called once per chain when the chain is created.
51
51
// This allows the chaincode to initialize any variables on the ledger prior
52
52
// to any transaction execution on the chain.
53
- func (e * PeerConfiger ) Init (stub shim.ChaincodeStubInterface ) ([]byte , error ) {
54
- cnflogger .Info ("Init CSCC" )
55
-
56
- return nil , nil
53
+ func (e * PeerConfiger ) Init (stub shim.ChaincodeStubInterface ) pb.Response {
54
+ logger .Info ("Init CSCC" )
55
+ return shim .Success (nil )
57
56
}
58
57
59
58
// Invoke is called for the following:
@@ -66,11 +65,11 @@ func (e *PeerConfiger) Init(stub shim.ChaincodeStubInterface) ([]byte, error) {
66
65
// # args[1] is a configuration Block if args[0] is JoinChain or
67
66
// UpdateConfigBlock; otherwise it is the chain id
68
67
// TODO: Improve the scc interface to avoid marshal/unmarshal args
69
- func (e * PeerConfiger ) Invoke (stub shim.ChaincodeStubInterface ) ([] byte , error ) {
68
+ func (e * PeerConfiger ) Invoke (stub shim.ChaincodeStubInterface ) pb. Response {
70
69
args := stub .GetArgs ()
71
70
72
71
if len (args ) < 2 {
73
- return nil , fmt .Errorf ("Incorrect number of arguments, %d" , len (args ))
72
+ return shim . Error ( fmt .Sprintf ("Incorrect number of arguments, %d" , len (args ) ))
74
73
}
75
74
fname := string (args [0 ])
76
75
@@ -86,72 +85,72 @@ func (e *PeerConfiger) Invoke(stub shim.ChaincodeStubInterface) ([]byte, error)
86
85
return updateConfigBlock (args [1 ])
87
86
}
88
87
89
- return nil , fmt .Errorf ("Requested function %s not found." , fname )
88
+ return shim . Error ( fmt .Sprintf ("Requested function %s not found." , fname ) )
90
89
}
91
90
92
91
// joinChain will join the specified chain in the configuration block.
93
92
// Since it is the first block, it is the genesis block containing configuration
94
93
// for this chain, so we want to update the Chain object with this info
95
- func joinChain (blockBytes []byte ) ([] byte , error ) {
94
+ func joinChain (blockBytes []byte ) pb. Response {
96
95
if blockBytes == nil {
97
- return nil , fmt . Errorf ("Genesis block must not be nil." )
96
+ return shim . Error ("Genesis block must not be nil." )
98
97
}
99
98
100
99
block , err := utils .GetBlockFromBlockBytes (blockBytes )
101
100
if err != nil {
102
- return nil , fmt .Errorf ("Failed to reconstruct the genesis block, %s" , err )
101
+ return shim . Error ( fmt .Sprintf ("Failed to reconstruct the genesis block, %s" , err ) )
103
102
}
104
103
105
104
if err = peer .CreateChainFromBlock (block ); err != nil {
106
- return nil , err
105
+ return shim . Error ( err . Error ())
107
106
}
108
107
109
108
chainID , err := utils .GetChainIDFromBlock (block )
110
109
if err != nil {
111
- return nil , fmt .Errorf ("Failed to get the chain ID from the configuration block, %s" , err )
110
+ return shim . Error ( fmt .Sprintf ("Failed to get the chain ID from the configuration block, %s" , err ) )
112
111
}
113
112
114
113
if err = peer .CreateDeliveryService (chainID ); err != nil {
115
- return nil , err
114
+ return shim . Error ( err . Error ())
116
115
}
117
116
118
- return [] byte ( "200" ), nil
117
+ return shim . Success ( nil )
119
118
}
120
119
121
- func updateConfigBlock (blockBytes []byte ) ([] byte , error ) {
120
+ func updateConfigBlock (blockBytes []byte ) pb. Response {
122
121
if blockBytes == nil {
123
- return nil , errors . New ("Configuration block must not be nil." )
122
+ return shim . Error ("Configuration block must not be nil." )
124
123
}
125
124
block , err := utils .GetBlockFromBlockBytes (blockBytes )
126
125
if err != nil {
127
- return nil , fmt .Errorf ("Failed to reconstruct the configuration block, %s" , err )
126
+ return shim . Error ( fmt .Sprintf ("Failed to reconstruct the configuration block, %s" , err ) )
128
127
}
129
128
chainID , err := utils .GetChainIDFromBlock (block )
130
129
if err != nil {
131
- return nil , fmt .Errorf ("Failed to get the chain ID from the configuration block, %s" , err )
130
+ return shim . Error ( fmt .Sprintf ("Failed to get the chain ID from the configuration block, %s" , err ) )
132
131
}
133
132
134
133
if err := peer .SetCurrConfigBlock (block , chainID ); err != nil {
135
- return nil , err
134
+ return shim . Error ( err . Error ())
136
135
}
137
136
138
- return [] byte ( "200" ), nil
137
+ return shim . Success ( nil )
139
138
}
140
139
141
140
// Return the current configuration block for the specified chainID. If the
142
141
// peer doesn't belong to the chain, return error
143
- func getConfigBlock (chainID []byte ) ([] byte , error ) {
142
+ func getConfigBlock (chainID []byte ) pb. Response {
144
143
if chainID == nil {
145
- return nil , errors . New ("ChainID must not be nil." )
144
+ return shim . Error ("ChainID must not be nil." )
146
145
}
147
146
block := peer .GetCurrConfigBlock (string (chainID ))
148
147
if block == nil {
149
- return nil , fmt .Errorf ("Unknown chain ID, %s" , string (chainID ))
148
+ return shim . Error ( fmt .Sprintf ("Unknown chain ID, %s" , string (chainID ) ))
150
149
}
151
150
blockBytes , err := utils .Marshal (block )
152
151
if err != nil {
153
- return nil , err
152
+ return shim . Error ( err . Error ())
154
153
}
155
154
156
- return blockBytes , nil
155
+ return shim . Success ( blockBytes )
157
156
}
0 commit comments