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

Commit

Permalink
broadcast sdk.api.DoOpts with context to chaincode.Service (#46)
Browse files Browse the repository at this point in the history
* broadcast sdk.api.DoOpts with context to chaincode.Service
  • Loading branch information
inotnako authored May 14, 2020
1 parent 1fb092e commit 1d59268
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 3 deletions.
10 changes: 10 additions & 0 deletions gateway/opt.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (

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

"github.com/s7techlab/cckit/extensions/encryption"
"github.com/s7techlab/cckit/gateway/service"
)
Expand All @@ -24,6 +26,14 @@ 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
2 changes: 1 addition & 1 deletion gateway/service/chaincode.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (cs *ChaincodeService) Invoke(ctx context.Context, in *ChaincodeInput) (*pe
WithIdentity(signer).
ArgBytes(in.Args[1:]).
Transient(in.Transient).
Do(ctx)
Do(ctx, DoOptionFromContext(ctx)...)

if err != nil {
return nil, err
Expand Down
26 changes: 25 additions & 1 deletion gateway/service/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ import (
"context"

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

const CtxSignerKey = `SigningIdentity`
const (
CtxSignerKey = `SigningIdentity`
CtxDoOptionKey = `SdkDoOption`
)

func ContextWithDefaultSigner(ctx context.Context, defaultSigner msp.SigningIdentity) context.Context {
if _, err := SignerFromContext(ctx); err != nil {
Expand All @@ -27,3 +31,23 @@ 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 := ctx.Value(CtxDoOptionKey).([]api.DoOption)
if doOpts == nil {
doOpts = []api.DoOption{}
}
return doOpts
}
16 changes: 15 additions & 1 deletion gateway/service/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import (
. "github.com/onsi/gomega"

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

"github.com/s7techlab/cckit/examples/cpaper_asservice"
cpservice "github.com/s7techlab/cckit/examples/cpaper_asservice/service"
"github.com/s7techlab/cckit/gateway/service"
Expand All @@ -32,7 +35,8 @@ var (

ctx = service.ContextWithSigner(
context.Background(),
idtestdata.Certificates[0].MustIdentity(idtestdata.DefaultMSP))
idtestdata.Certificates[0].MustIdentity(idtestdata.DefaultMSP),
)
)

var _ = Describe(`Service`, func() {
Expand All @@ -56,6 +60,16 @@ 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: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ github.com/hashicorp/go-version v1.1.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09
github.com/hashicorp/go-version v1.2.0 h1:3vNe/fWF5CBgRIguda1meWhsZHy3m8gCJ5wx+dIzX/E=
github.com/hashicorp/go-version v1.2.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.1 h1:0hERBMJE1eitiLkihrMvRVBYAkpHzc/J3QdDN+dAcgU=
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/hcl v0.0.0-20180404174102-ef8a98b0bbce/go.mod h1:oZtUIOe8dh44I2q6ScRibXws4Ajl+d+nod3AaR9vL5w=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
Expand Down Expand Up @@ -434,6 +435,7 @@ go.opencensus.io v0.19.0/go.mod h1:AYeH0+ZxYyghG8diqaaIq/9P3VgCCt5GF2ldCY4dkFg=
go.opencensus.io v0.19.1/go.mod h1:gug0GbSHa8Pafr0d2urOSgoXHZ6x/RUlaiT0d9pqb4A=
go.opencensus.io v0.20.0/go.mod h1:NO/8qkisMZLZ1FCsKNqtJPwc8/TaclWyY0B6wcYNg9M=
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
go.opencensus.io v0.22.0 h1:C9hSCOW830chIVkdja34wa6Ky+IzWllkUinR+BtRZd4=
go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8=
go.uber.org/atomic v1.3.2/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
Expand Down

0 comments on commit 1d59268

Please sign in to comment.