Skip to content

Commit

Permalink
[FAB-6230] Add chaincode ID to ChaincodeInfo
Browse files Browse the repository at this point in the history
This change set just adds the ID to the chaincodeInfo message
so that LSCC.GETINSTALLEDCHAINCODES will return the ID.
This is needed to include the ID in instantiate.

Change-Id: I119964b675286969c5861b4c030598e218585cc8
Signed-off-by: yacovm <yacovm@il.ibm.com>
  • Loading branch information
yacovm committed Dec 8, 2017
1 parent ff0b4fa commit 7b452c7
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 23 deletions.
2 changes: 1 addition & 1 deletion core/common/ccprovider/ccprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ func GetInstalledChaincodes() (*pb.ChaincodeQueryResponse, error) {
// since this is just an installed chaincode these should be blank
input, escc, vscc := "", "", ""

ccInfo := &pb.ChaincodeInfo{Name: name, Version: version, Path: path, Input: input, Escc: escc, Vscc: vscc}
ccInfo := &pb.ChaincodeInfo{Name: name, Version: version, Path: path, Input: input, Escc: escc, Vscc: vscc, Id: ccpack.GetId()}

// add this specific chaincode's metadata to the array of all chaincodes
ccInfoArray = append(ccInfoArray, ccInfo)
Expand Down
35 changes: 32 additions & 3 deletions peer/chaincode/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@ SPDX-License-Identifier: Apache-2.0
package chaincode

import (
"bytes"
"encoding/hex"
"errors"
"fmt"
"reflect"

"github.com/golang/protobuf/proto"
"golang.org/x/net/context"

pb "github.com/hyperledger/fabric/protos/peer"
"github.com/hyperledger/fabric/protos/utils"
"github.com/spf13/cobra"
"golang.org/x/net/context"
)

var getInstalledChaincodes bool
Expand Down Expand Up @@ -98,7 +100,34 @@ func getChaincodes(cmd *cobra.Command, cf *ChaincodeCmdFactory) error {
fmt.Printf("Get instantiated chaincodes on channel %s:\n", channelID)
}
for _, chaincode := range cqr.Chaincodes {
fmt.Printf("%v\n", chaincode)
fmt.Printf("%v\n", ccInfo{chaincode}.String())
}
return nil
}

type ccInfo struct {
*pb.ChaincodeInfo
}

func (cci ccInfo) String() string {
b := bytes.Buffer{}
md := reflect.ValueOf(*cci.ChaincodeInfo)
md2 := reflect.Indirect(reflect.ValueOf(*cci.ChaincodeInfo)).Type()
for i := 0; i < md.NumField(); i++ {
f := md.Field(i)
val := f.String()
if isBytes(f) {
val = hex.EncodeToString(f.Bytes())
}
if len(val) == 0 {
continue
}
b.WriteString(fmt.Sprintf("%s: %s, ", md2.Field(i).Name, val))
}
return b.String()[:len(b.String())-2]

}

func isBytes(v reflect.Value) bool {
return v.Kind() == reflect.Slice && v.Type().Elem().Kind() == reflect.Uint8
}
21 changes: 20 additions & 1 deletion peer/chaincode/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"fmt"
"testing"

"encoding/hex"

"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric/peer/common"
pb "github.com/hyperledger/fabric/protos/peer"
Expand All @@ -26,7 +28,7 @@ func TestChaincodeListCmd(t *testing.T) {

installedCqr := &pb.ChaincodeQueryResponse{
Chaincodes: []*pb.ChaincodeInfo{
{Name: "mycc1", Version: "1.0", Path: "codePath1", Input: "input", Escc: "escc", Vscc: "vscc"},
{Name: "mycc1", Version: "1.0", Path: "codePath1", Input: "input", Escc: "escc", Vscc: "vscc", Id: []byte{1, 2, 3}},
{Name: "mycc2", Version: "1.0", Path: "codePath2", Input: "input", Escc: "escc", Vscc: "vscc"},
},
}
Expand Down Expand Up @@ -110,3 +112,20 @@ func TestChaincodeListCmd(t *testing.T) {
t.Errorf("Expect error: %s", expectErr)
}
}

func TestString(t *testing.T) {
id := []byte{1, 2, 3, 4, 5}
idBytes := hex.EncodeToString(id)
b, _ := hex.DecodeString(idBytes)
ccInf := &ccInfo{
ChaincodeInfo: &pb.ChaincodeInfo{
Name: "ccName",
Id: b,
Version: "1.0",
Escc: "escc",
Input: "input",
Vscc: "vscc",
},
}
assert.Equal(t, "Name: ccName, Version: 1.0, Input: input, Escc: escc, Vscc: vscc, Id: 0102030405", ccInf.String())
}
50 changes: 32 additions & 18 deletions protos/peer/query.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions protos/peer/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ message ChaincodeInfo {
// the name of the VSCC for this chaincode. This will be
// blank if the query is returning information about installed chaincodes.
string vscc = 6;
// the chaincode unique id.
// computed as: H(
// H(name || version) ||
// H(CodePackage)
// )
bytes id = 7;
}

// ChannelQueryResponse returns information about each channel that pertains
Expand Down

0 comments on commit 7b452c7

Please sign in to comment.