Skip to content

Commit

Permalink
Add lifecycle interface to system cc (#60)
Browse files Browse the repository at this point in the history
* 1

* add Lifecycle interface to core SystemCC and implement it

* add Lifecycle interface to core SystemCC and implement it

* add Lifecycle interface to core SystemCC and implement it

* add Lifecycle interface to core SystemCC and implement it

* add Lifecycle interface to core SystemCC and implement it

* add Lifecycle interface to core SystemCC and implement it

* add Lifecycle interface to core SystemCC and implement it

Co-authored-by: n.neznaemov <n.neznaemov@s7.ru>
  • Loading branch information
criro1 and n.neznaemov authored Feb 19, 2021
1 parent 87af68e commit d77df12
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 3 deletions.
11 changes: 11 additions & 0 deletions api/chaincode_lifecycle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package api

import (
"context"

lb "github.com/hyperledger/fabric-protos-go/peer/lifecycle"
)

type Lifecycle interface {
QueryInstalledChaincodes(ctx context.Context) (*lb.QueryInstalledChaincodesResult, error)
}
1 change: 1 addition & 0 deletions api/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ type SystemCC interface {
CSCC() CSCC
QSCC() QSCC
LSCC() LSCC
Lifecycle() Lifecycle
}
49 changes: 49 additions & 0 deletions client/chaincode/system/lifecycle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package system

import (
"context"

"github.com/golang/protobuf/proto"
lb "github.com/hyperledger/fabric-protos-go/peer/lifecycle"
"github.com/hyperledger/fabric/common/util"
"github.com/hyperledger/fabric/core/chaincode/lifecycle"
"github.com/hyperledger/fabric/msp"
"github.com/pkg/errors"
"github.com/s7techlab/hlf-sdk-go/api"
peerSDK "github.com/s7techlab/hlf-sdk-go/peer"
)

type lifecycleCC struct {
peerPool api.PeerPool
identity msp.SigningIdentity
processor api.PeerProcessor
}

func (c *lifecycleCC) QueryInstalledChaincodes(ctx context.Context) (*lb.QueryInstalledChaincodesResult, error) {
resp, err := c.endorse(ctx, lifecycle.QueryInstalledChaincodesFuncName, ``)
if err != nil {
return nil, err
}
ccData := new(lb.QueryInstalledChaincodesResult)
if err = proto.Unmarshal(resp, ccData); err != nil {
return nil, errors.Wrap(err, `failed to unmarshal protobuf`)
}
return ccData, nil
}

func (c *lifecycleCC) endorse(ctx context.Context, fn string, args ...string) ([]byte, error) {
prop, _, err := c.processor.CreateProposal(&api.DiscoveryChaincode{Name: lifecycleName, Type: api.CCTypeGoLang}, c.identity, fn, util.ToChaincodeArgs(args...), nil)
if err != nil {
return nil, errors.Wrap(err, `failed to create proposal`)
}

resp, err := c.peerPool.Process(ctx, c.identity.GetMSPIdentifier(), prop)
if err != nil {
return nil, errors.Wrap(err, `failed to endorse proposal`)
}
return resp.Response.Payload, nil
}

func NewLifecycle(peerPool api.PeerPool, identity msp.SigningIdentity) api.Lifecycle {
return &lifecycleCC{peerPool: peerPool, identity: identity, processor: peerSDK.NewProcessor(``)}
}
11 changes: 8 additions & 3 deletions client/chaincode/system/scc.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import (
)

const (
csccName = `cscc`
qsccName = `qscc`
lsccName = `lscc`
csccName = `cscc`
qsccName = `qscc`
lsccName = `lscc`
lifecycleName = `_lifecycle`
)

type scc struct {
Expand All @@ -28,6 +29,10 @@ func (c *scc) LSCC() api.LSCC {
return NewLSCC(c.peerPool, c.identity)
}

func (c *scc) Lifecycle() api.Lifecycle {
return NewLifecycle(c.peerPool, c.identity)
}

func NewSCC(peer api.PeerPool, identity msp.SigningIdentity) api.SystemCC {
return &scc{peerPool: peer, identity: identity}
}

0 comments on commit d77df12

Please sign in to comment.