Skip to content
This repository has been archived by the owner on Nov 8, 2023. It is now read-only.

Commit

Permalink
Merge pull request #74 from s7techlab/v2
Browse files Browse the repository at this point in the history
remove hlf
  • Loading branch information
bogatyr285 authored Oct 25, 2021
2 parents 871b0c3 + 3c159cb commit 15167b3
Show file tree
Hide file tree
Showing 12 changed files with 77 additions and 450 deletions.
2 changes: 1 addition & 1 deletion examples/cpaper_asservice/service/service.pb.cc.go

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

2 changes: 1 addition & 1 deletion extensions/debug/debug_state.pb.cc.go

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

4 changes: 2 additions & 2 deletions gateway/chaincode.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type ChaincodeEventSub interface {
}

type chaincode struct {
Service service.Chaincode
Service service.ChaincodeServer
Channel string
Chaincode string

Expand All @@ -40,7 +40,7 @@ type chaincode struct {
EventOpts []EventOpt
}

func NewChaincode(service service.Chaincode, channelName, chaincodeName string, opts ...Opt) *chaincode {
func NewChaincode(service service.ChaincodeServer, channelName, chaincodeName string, opts ...Opt) *chaincode {
c := &chaincode{
Service: service,
Channel: channelName,
Expand Down
9 changes: 0 additions & 9 deletions gateway/opt.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/hyperledger/fabric-protos-go/peer"
"github.com/hyperledger/fabric/msp"
"github.com/s7techlab/hlf-sdk-go/v2/api"

"github.com/s7techlab/cckit/extensions/encryption"
"github.com/s7techlab/cckit/gateway/service"
Expand All @@ -26,14 +25,6 @@ func WithDefaultSigner(defaultSigner msp.SigningIdentity) Opt {
}
}

func WithDefaultDoOpts(defaultDoOpts ...api.DoOption) Opt {
return func(c *chaincode) {
c.ContextOpts = append(c.ContextOpts, func(ctx context.Context) context.Context {
return service.ContextWithDefaultDoOption(ctx, defaultDoOpts...)
})
}
}

func WithTransientValue(key string, value []byte) Opt {
return func(c *chaincode) {
c.ContextOpts = append(c.ContextOpts, func(ctx context.Context) context.Context {
Expand Down
86 changes: 59 additions & 27 deletions gateway/service/chaincode.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,72 @@ package service
import (
"context"

"github.com/hyperledger/fabric-protos-go/orderer"
"github.com/hyperledger/fabric-protos-go/peer"
"github.com/hyperledger/fabric/msp"
"github.com/pkg/errors"
"github.com/s7techlab/hlf-sdk-go/v2/api"
)

type ChaincodeService struct {
peerSDK Peer
}

// gateway/chaincode.go needds access to grpc stream
type (
// Chaincode service interface
Chaincode = ChaincodeServer
//Chaincode = ChaincodeServer
ChaincodeEventsServer = chaincodeEventsServer
)

// ChaincodeService implementation based of hlf-sdk-go
type ChaincodeService struct {
sdk api.Core
type Peer interface {
ChannelChaincode(ctx context.Context, chanName string, ccName string) (Chaincode, error)
Events(
ctx context.Context,
channelName string,
ccName string,
eventCCSeekOption ...func() (*orderer.SeekPosition, *orderer.SeekPosition),
) (chan *peer.ChaincodeEvent, error)
CurrentIdentity() msp.SigningIdentity
}

type Chaincode interface {
// Invoke returns invoke builder for presented chaincode function
Invoke(fn string) ChaincodeInvokeBuilder
// Query returns query builder for presented function and arguments
Query(fn string, args ...string) ChaincodeQueryBuilder
}

// ChaincodeQueryBuilder describe possibilities how to get query results
type ChaincodeQueryBuilder interface {
// WithIdentity allows to invoke chaincode from custom identity
WithIdentity(identity msp.SigningIdentity) ChaincodeQueryBuilder
// Transient allows to pass arguments to transient map
Transient(args map[string][]byte) ChaincodeQueryBuilder
// AsProposalResponse allows to get raw peer response
AsProposalResponse(ctx context.Context) (*peer.ProposalResponse, error)
}

func New(sdk api.Core) *ChaincodeService {
return &ChaincodeService{sdk: sdk}
type ChaincodeInvokeBuilder interface {
// WithIdentity allows to invoke chaincode from custom identity
WithIdentity(identity msp.SigningIdentity) ChaincodeInvokeBuilder
// Transient allows to pass arguments to transient map
Transient(args map[string][]byte) ChaincodeInvokeBuilder
// ArgBytes set slice of bytes as argument
ArgBytes([][]byte) ChaincodeInvokeBuilder
// Do makes invoke with built arguments
Do(ctx context.Context) (res *peer.Response, chaincodeTx string, err error)
}

func New(peerSDK Peer) *ChaincodeService {
return &ChaincodeService{peerSDK: peerSDK}
}

func (cs *ChaincodeService) Exec(ctx context.Context, in *ChaincodeExec) (*peer.ProposalResponse, error) {
if in.Type == InvocationType_QUERY {
switch in.Type {
case InvocationType_QUERY:
return cs.Query(ctx, in.Input)
} else if in.Type == InvocationType_INVOKE {
case InvocationType_INVOKE:
return cs.Invoke(ctx, in.Input)
} else {
default:
return nil, ErrUnknownInvocationType
}
}
Expand All @@ -39,24 +79,22 @@ func (cs *ChaincodeService) Invoke(ctx context.Context, in *ChaincodeInput) (*pe
return nil, err
}

ccApi, err := cs.sdk.
Channel(in.Channel).
Chaincode(ctx, in.Chaincode)
ccAPI, err := cs.peerSDK.ChannelChaincode(ctx, in.Channel, in.Chaincode)
if err != nil {
return nil, err
}

response, _, err := ccApi.Invoke(string(in.Args[0])).
response, _, err := ccAPI.Invoke(string(in.Args[0])).
WithIdentity(signer).
ArgBytes(in.Args[1:]).
Transient(in.Transient).
Do(ctx, DoOptionFromContext(ctx)...)
Do(ctx)

if err != nil {
return nil, err
}

// todo: add to hlf-sdk-go method returning ProposalResponse
// todo: add to hlf-peerSDK-go method returning ProposalResponse
proposalResponse := &peer.ProposalResponse{
Response: response,
}
Expand All @@ -74,12 +112,12 @@ func (cs *ChaincodeService) Query(ctx context.Context, in *ChaincodeInput) (*pee
return nil, err
}

ccApi, err := cs.sdk.Channel(in.Channel).Chaincode(ctx, in.Chaincode)
ccAPI, err := cs.peerSDK.ChannelChaincode(ctx, in.Channel, in.Chaincode)
if err != nil {
return nil, err
}

resp, err := ccApi.Query(argSs[0], argSs[1:]...).
resp, err := ccAPI.Query(argSs[0], argSs[1:]...).
WithIdentity(signer).
Transient(in.Transient).
AsProposalResponse(ctx)
Expand All @@ -91,19 +129,13 @@ func (cs *ChaincodeService) Query(ctx context.Context, in *ChaincodeInput) (*pee
}

func (cs *ChaincodeService) Events(in *ChaincodeLocator, stream Chaincode_EventsServer) error {

deliver, err := cs.sdk.PeerPool().DeliverClient(cs.sdk.CurrentIdentity().GetMSPIdentifier(), cs.sdk.CurrentIdentity())
if err != nil {
return err
}

events, err := deliver.SubscribeCC(stream.Context(), in.Channel, in.Chaincode)
events, err := cs.peerSDK.Events(stream.Context(), in.Channel, in.Chaincode)
if err != nil {
return err
}

for {
e, ok := <-events.Events()
e, ok := <-events
if !ok {
return nil
}
Expand Down
21 changes: 0 additions & 21 deletions gateway/service/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"

"github.com/hyperledger/fabric/msp"
"github.com/s7techlab/hlf-sdk-go/v2/api"
)

type contextKey string
Expand Down Expand Up @@ -37,23 +36,3 @@ func SignerFromContext(ctx context.Context) (msp.SigningIdentity, error) {
return signer, nil
}
}

func ContextWithDefaultDoOption(ctx context.Context, defaultDoOpts ...api.DoOption) context.Context {
if opts := DoOptionFromContext(ctx); len(opts) == 0 {
return ContextWithDoOption(ctx, defaultDoOpts...)
} else {
return ctx
}
}

func ContextWithDoOption(ctx context.Context, doOpts ...api.DoOption) context.Context {
return context.WithValue(ctx, CtxDoOptionKey, doOpts)
}

func DoOptionFromContext(ctx context.Context) []api.DoOption {
doOpts, ok := ctx.Value(CtxDoOptionKey).([]api.DoOption)
if !ok {
doOpts = []api.DoOption{}
}
return doOpts
}
73 changes: 0 additions & 73 deletions gateway/service/context_test.go

This file was deleted.

12 changes: 0 additions & 12 deletions gateway/service/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
. "github.com/onsi/gomega"

"github.com/golang/protobuf/ptypes/empty"
"github.com/s7techlab/hlf-sdk-go/v2/client/chaincode"
"github.com/s7techlab/hlf-sdk-go/v2/client/chaincode/txwaiter"

"github.com/s7techlab/cckit/examples/cpaper_asservice"
cpservice "github.com/s7techlab/cckit/examples/cpaper_asservice/service"
Expand Down Expand Up @@ -60,16 +58,6 @@ var _ = Describe(`Service`, func() {
Expect(pp.Items).To(HaveLen(0))
})

It("Allow to propagate api.DoOption", func() {
ctxWithOpts := service.ContextWithDoOption(
ctx,
chaincode.WithTxWaiter(txwaiter.All),
)
pp, err := cPaperGateway.List(ctxWithOpts, &empty.Empty{})
Expect(err).NotTo(HaveOccurred())
Expect(pp.Items).To(HaveLen(0))
})

It("Allow to imitate error while access to peer", func() {
cPaperService.Invoker = mock.FailChaincode(ChaincodeName)

Expand Down
10 changes: 7 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/s7techlab/cckit

go 1.13
go 1.16

require (
github.com/gogo/protobuf v1.3.2 // indirect
Expand All @@ -13,13 +13,17 @@ require (
github.com/mwitkow/go-proto-validators v0.3.2
github.com/onsi/ginkgo v1.8.0
github.com/onsi/gomega v1.9.0
github.com/op/go-logging v0.0.0-20160315200505-970db520ece7
github.com/pelletier/go-toml v1.4.0 // indirect
github.com/pkg/errors v0.8.1
github.com/s7techlab/hlf-sdk-go/v2 v2.0.0-20211021114534-b3f0602d2193 // indirect
github.com/spf13/afero v1.2.2 // indirect
github.com/spf13/viper v1.4.0 // indirect
github.com/sykesm/zap-logfmt v0.0.3 // indirect
go.uber.org/zap v1.14.1
golang.org/x/net v0.0.0-20210119194325-5f4716e94777 // indirect
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c // indirect
golang.org/x/text v0.3.5 // indirect
google.golang.org/genproto v0.0.0-20210122163508-8081c04a3579
google.golang.org/grpc v1.29.1
google.golang.org/protobuf v1.25.0
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
)
Loading

0 comments on commit 15167b3

Please sign in to comment.