Skip to content
This repository was archived by the owner on Feb 9, 2021. It is now read-only.

Commit 62f24c2

Browse files
committed
Remove base64 from protocol
This was a leftover from when the fabric transport was string based. Since we can now send raw bytes, we might as well use them since its more space and time efficient to avoid the base64 encode/decode cycle. Change-Id: If1ad0aa0ac0d85a37e6409e8a7ea319a8dbe669f Signed-off-by: Greg Haskins <gregory.haskins@gmail.com>
1 parent d2ceb4e commit 62f24c2

File tree

3 files changed

+24
-36
lines changed

3 files changed

+24
-36
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ message ChaincodeInput {
310310
311311
}
312312
```
313-
Chaintool deterministically maps functions declared within a CCI to an [encoded function name](#function-encoding), and expects the corresponding input parameter to be a base64 encoded protobuf message as the first and only arg string.
313+
Chaintool deterministically maps functions declared within a CCI to an [encoded function name](#function-encoding), and expects the corresponding input parameter to be a serialized protobuf message as the first and only arg string.
314314

315315
Example:
316316
```
@@ -323,7 +323,7 @@ Function naming follows the convention *interface-name/method-type/method-index*
323323

324324
### Output Protocol
325325

326-
Standard chaincode protocol allows chaincode applications to return a string to a caller. Chaintool managed applications will encode this string with a base64 encoded protobuf structure (when applicable).
326+
Standard chaincode protocol allows chaincode applications to return a byte-array payload to a caller. Chaintool managed applications will encode this payload with a serialized protobuf structure (when applicable).
327327

328328
### Protobuf "hints"
329329

examples/example02/client/nodejs/client.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ function createRequest(fcn, args) {
3131
chainId: 'testchainid',
3232
chaincodeId: 'mycc',
3333
fcn: fcn,
34-
args: [args.toBase64()],
34+
args: [args.toBuffer()],
3535
txId: tx_id,
3636
nonce: nonce
3737
};
@@ -93,7 +93,7 @@ function checkBalance(args) {
9393
return sendQuery('org.hyperledger.chaincode.example02/fcn/3',
9494
new app.Entity(args))
9595
.then(function(results) {
96-
return app.BalanceResult.decode64(results[0].toString('utf-8'));
96+
return app.BalanceResult.decode(results[0]);
9797
});
9898
}
9999

resources/generators/golang.stg

Lines changed: 20 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,14 @@ var functionspec = regexp.MustCompile("([a-zA-Z0-9.]*)/fcn/([0-9]*)")
3737
// Initialization function, called only once
3838
func (self *stubHandler) Init(stub shim.ChaincodeStubInterface) pb.Response {
3939

40-
function, args := stub.GetFunctionAndParameters()
40+
args := stub.GetArgs()
4141

42-
if len(args) != 1 {
43-
return shim.Error("Expected exactly one argument")
42+
if len(args) != 2 {
43+
return shim.Error("Expected exactly two arguments")
4444
}
4545

46+
function := string(args[0])
47+
4648
if function != "init" {
4749
return shim.Error("Function must be \"init\"")
4850
}
@@ -52,21 +54,21 @@ func (self *stubHandler) Init(stub shim.ChaincodeStubInterface) pb.Response {
5254
return shim.Error("Interface not found")
5355
}
5456

55-
return dispatcher.Dispatch(stub, 1, args[0])
57+
return dispatcher.Dispatch(stub, 1, args[1])
5658
}
5759

5860
// Callback representing the invocation of a chaincode
5961
func (self *stubHandler) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
6062

61-
function, args := stub.GetFunctionAndParameters()
63+
args := stub.GetArgs()
6264

63-
var params string;
65+
var params []byte
6466

65-
if len(args) > 0 {
66-
params = args[0]
67+
if len(args) > 1 {
68+
params = args[1]
6769
}
6870

69-
dispatcher, index, err := self.decodeFunction(function)
71+
dispatcher, index, err := self.decodeFunction(string(args[0]))
7072
if err != nil {
7173
return shim.Error(err.Error())
7274
}
@@ -162,7 +164,7 @@ import (
162164
)
163165

164166
type Dispatcher interface {
165-
Dispatch(stub shim.ChaincodeStubInterface, function int, params string) pb.Response
167+
Dispatch(stub shim.ChaincodeStubInterface, function int, params []byte) pb.Response
166168
}
167169

168170
type Factory interface {
@@ -273,7 +275,6 @@ import (
273275
"github.com/hyperledger/fabric/core/chaincode/shim"
274276
pb "github.com/hyperledger/fabric/protos/peer"
275277
"<base>/ccs/api"
276-
"encoding/base64"
277278
"fmt"
278279
)
279280

@@ -299,7 +300,7 @@ func (self *factoryImpl) Create(intf interface{}) (api.Dispatcher, error) {
299300
return &stubImpl{intf: intf.(CCInterface)}, nil
300301
}
301302

302-
func (self *stubImpl) Dispatch(stub shim.ChaincodeStubInterface, function int, params string) pb.Response {
303+
func (self *stubImpl) Dispatch(stub shim.ChaincodeStubInterface, function int, params []byte) pb.Response {
303304
// Handle different functions
304305
switch {
305306
<dispatchfunctions(intf, intf.functions)>
@@ -336,7 +337,6 @@ package <intf.package>
336337
import (
337338
"github.com/golang/protobuf/proto"
338339
"github.com/hyperledger/fabric/core/chaincode/shim"
339-
"encoding/base64"
340340
"fmt"
341341
)
342342

@@ -369,17 +369,13 @@ case function == <func.index>:
369369
implementserver(intf, func) ::=
370370
<<
371371

372-
func (self *stubImpl) proxy<func.name>(stub shim.ChaincodeStubInterface, _params string) pb.Response {
372+
func (self *stubImpl) proxy<func.name>(stub shim.ChaincodeStubInterface, _params []byte) pb.Response {
373373

374374
var err error;
375375

376376
<if(func.param)>
377377
params := &<func.param>{}
378-
_pbinput, err := base64.StdEncoding.DecodeString(_params)
379-
if (err != nil) {
380-
return shim.Error(fmt.Sprintf("base64 decode error: %s", err))
381-
}
382-
err = proto.Unmarshal(_pbinput, params)
378+
err = proto.Unmarshal(_params, params)
383379
if (err != nil) {
384380
return shim.Error(fmt.Sprintf("protobuf unmarshal error: %s", err))
385381
}
@@ -391,12 +387,11 @@ func (self *stubImpl) proxy<func.name>(stub shim.ChaincodeStubInterface, _params
391387
}
392388

393389
<if(func.rettype)>
394-
_pboutput, err := proto.Marshal(result)
390+
_result, err := proto.Marshal(result)
395391
if (err != nil) {
396392
return shim.Error(fmt.Sprintf("protobuf marshal error: %s", err))
397393
}
398-
_result := base64.StdEncoding.EncodeToString(_pboutput)
399-
return shim.Success([]byte(_result))
394+
return shim.Success(_result)
400395
<else>
401396
return shim.Success(nil)
402397
<endif>
@@ -420,24 +415,17 @@ func <func.name>(stub shim.ChaincodeStubInterface, chaincodeName string<if(func.
420415
args[0] = []byte(<compositename(intf, func)>)
421416

422417
<if(func.param)>
423-
_pboutput, err := proto.Marshal(params)
418+
args[1], err = proto.Marshal(params)
424419
if (err != nil) {
425420
return <if(func.rettype)>nil, <endif>err
426421
}
427-
428-
args[1] = make([]byte, base64.StdEncoding.EncodedLen(len(_pboutput)))
429-
base64.StdEncoding.Encode(args[1], _pboutput)
430422
<endif>
431423

432-
resp := stub.InvokeChaincode(chaincodeName, args)
424+
resp := stub.InvokeChaincode(chaincodeName, args, "")
433425
if resp.Status \< shim.ERROR {
434426
<if(func.rettype)>
435427
result := &<func.rettype>{}
436-
_pbinput, err := base64.StdEncoding.DecodeString(string(resp.Payload))
437-
if (err != nil) {
438-
return nil, fmt.Errorf("Error decoding base64 return value: %s", err)
439-
}
440-
err = proto.Unmarshal(_pbinput, result)
428+
err = proto.Unmarshal(resp.Payload, result)
441429
if (err != nil) {
442430
return nil, fmt.Errorf("Error unmarshalling return value: %s", err)
443431
}

0 commit comments

Comments
 (0)