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

Commit

Permalink
prepare surrounding for removing hlf
Browse files Browse the repository at this point in the history
  • Loading branch information
bogatyr285 committed Oct 22, 2021
1 parent 871b0c3 commit 7edecbd
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 133 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
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
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
83 changes: 70 additions & 13 deletions gateway/service/chaincode.go
Expand Up @@ -3,32 +3,90 @@ 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 {
sdk FabricAPI
}

// 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 FabricAPI interface {
// Channel returns channel instance by channel name
Channel(name string) ChannelAPI
DeliverClient(mspId string, identity msp.SigningIdentity) (DeliverClientAPI, error)
CurrentIdentity() msp.SigningIdentity
}

func New(sdk api.Core) *ChaincodeService {
/* channel api interfaces section*/
type ChannelAPI interface {
// Chaincode returns chaincode instance by chaincode name
Chaincode(ctx context.Context, name string) (ChaincodeAPI, error)
}

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

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

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

/* deliver client interfaces section */

type DeliverClientAPI interface {
// SubscribeCC allows to subscribe on chaincode events using name of channel, chaincode and block offset
SubscribeCC(
ctx context.Context,
channelName string,
ccName string,
eventCCSeekOption ...func() (*orderer.SeekPosition, *orderer.SeekPosition),
) (EventCCSubscriptionAPI, error)
}
type EventCCSubscriptionAPI interface {
// Events initiates internal GRPC stream and returns channel on chaincode events
Events() chan *peer.ChaincodeEvent
}

func New(sdk FabricAPI) *ChaincodeService {
return &ChaincodeService{sdk: sdk}
}

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 @@ -50,7 +108,7 @@ func (cs *ChaincodeService) Invoke(ctx context.Context, in *ChaincodeInput) (*pe
WithIdentity(signer).
ArgBytes(in.Args[1:]).
Transient(in.Transient).
Do(ctx, DoOptionFromContext(ctx)...)
Do(ctx)

if err != nil {
return nil, err
Expand Down Expand Up @@ -91,8 +149,7 @@ 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())
deliver, err := cs.sdk.DeliverClient(cs.sdk.CurrentIdentity().GetMSPIdentifier(), cs.sdk.CurrentIdentity())
if err != nil {
return err
}
Expand Down
21 changes: 0 additions & 21 deletions gateway/service/context.go
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
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
2 changes: 1 addition & 1 deletion go.mod
@@ -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 Down

0 comments on commit 7edecbd

Please sign in to comment.