Skip to content

Commit

Permalink
Merge pull request #91 from kaleido-io/is-init
Browse files Browse the repository at this point in the history
Update to use WithInit() based on the changes in the dependent PR in fabric-sdk-go
  • Loading branch information
jimthematrix authored Nov 4, 2022
2 parents 4476bd8 + e12d594 commit 1df7cbb
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 29 deletions.
4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ require (
github.com/hashicorp/golang-lru v0.5.4
github.com/hyperledger/fabric-config v0.0.7 // indirect
github.com/hyperledger/fabric-protos-go v0.0.0-20211118165945-23d738fc3553
github.com/hyperledger/fabric-sdk-go v1.0.1-0.20220510182741-7a94fbc3efed
github.com/hyperledger/fabric-sdk-go v1.0.1-0.20220617091732-e170b98fa821
github.com/julienschmidt/httprouter v1.3.0
github.com/klauspost/compress v1.13.5 // indirect
github.com/mattn/go-runewidth v0.0.13 // indirect
Expand Down Expand Up @@ -51,5 +51,3 @@ require (
)

replace google.golang.org/grpc => google.golang.org/grpc v1.29.0

replace github.com/hyperledger/fabric-sdk-go => github.com/kaleido-io/fabric-sdk-go v1.0.1-0.20220610141558-4cdb9535ce4b
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,8 @@ github.com/hyperledger/fabric-lib-go v1.0.0/go.mod h1:H362nMlunurmHwkYqR5uHL2UDW
github.com/hyperledger/fabric-protos-go v0.0.0-20200424173316-dd554ba3746e/go.mod h1:xVYTjK4DtZRBxZ2D9aE4y6AbLaPwue2o/criQyQbVD0=
github.com/hyperledger/fabric-protos-go v0.0.0-20211118165945-23d738fc3553 h1:E9f0v1q4EDfrE+0LdkxVtdYKAZ7PGCaj1bBx45R9yEQ=
github.com/hyperledger/fabric-protos-go v0.0.0-20211118165945-23d738fc3553/go.mod h1:xVYTjK4DtZRBxZ2D9aE4y6AbLaPwue2o/criQyQbVD0=
github.com/hyperledger/fabric-sdk-go v1.0.1-0.20220617091732-e170b98fa821 h1:0VfkWCv8MPDjm+6xoD6t3yN075AQ2x2KTcLY1m5BJno=
github.com/hyperledger/fabric-sdk-go v1.0.1-0.20220617091732-e170b98fa821/go.mod h1:JRplpKBeAvXjsBhOCCM/KvMRUbdDyhsAh80qbXzKc10=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/imdario/mergo v0.3.4/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
Expand Down Expand Up @@ -429,8 +431,6 @@ github.com/juju/ratelimit v1.0.1/go.mod h1:qapgC/Gy+xNh9UxzV13HGGl/6UXNN+ct+vwSg
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U=
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
github.com/kaleido-io/fabric-sdk-go v1.0.1-0.20220610141558-4cdb9535ce4b h1:PUn8zRTSokSIVOJaB7MaaylcetcvE+o4ms3AFFtc7gc=
github.com/kaleido-io/fabric-sdk-go v1.0.1-0.20220610141558-4cdb9535ce4b/go.mod h1:JRplpKBeAvXjsBhOCCM/KvMRUbdDyhsAh80qbXzKc10=
github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=
github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQLJ+jE2L00=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
Expand Down
34 changes: 14 additions & 20 deletions internal/fabric/client/client_gateway_clientside.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,15 @@ import (
// defined to allow mocking in tests
type gatewayCreator func(core.ConfigProvider, string, int) (*gateway.Gateway, error)
type networkCreator func(*gateway.Gateway, string) (*gateway.Network, error)
type txPreparer func(*gwRPCWrapper, string, string, string, string) (*gateway.Transaction, <-chan *fab.TxStatusEvent, error)
type txPreparer func(*gwRPCWrapper, string, string, string, string, bool) (*gateway.Transaction, <-chan *fab.TxStatusEvent, error)
type txSubmitter func(*gateway.Transaction, ...string) ([]byte, error)

type gwRPCWrapper struct {
*commonRPCWrapper
gatewayCreator gatewayCreator
networkCreator networkCreator
txPreparer txPreparer
txSubmitter txSubmitter
txInitSubmitter txSubmitter
gatewayCreator gatewayCreator
networkCreator networkCreator
txPreparer txPreparer
txSubmitter txSubmitter
// networkCreator networkC
// one gateway client per signer
gwClients map[string]*gateway.Gateway
Expand All @@ -68,7 +67,6 @@ func newRPCClientWithClientSideGateway(configProvider core.ConfigProvider, txTim
networkCreator: getNetwork,
txPreparer: prepareTx,
txSubmitter: submitTx,
txInitSubmitter: submitInitTx,
gwClients: make(map[string]*gateway.Gateway),
gwGatewayClients: make(map[string]map[string]*gateway.Network),
gwChannelClients: make(map[string]map[string]*channel.Client),
Expand Down Expand Up @@ -156,16 +154,11 @@ func (w *gwRPCWrapper) Close() error {
}

func (w *gwRPCWrapper) sendTransaction(signer, channelId, chaincodeName, method string, args []string, isInit bool) ([]byte, *fab.TxStatusEvent, error) {
tx, notifier, err := w.txPreparer(w, signer, channelId, chaincodeName, method)
tx, notifier, err := w.txPreparer(w, signer, channelId, chaincodeName, method, isInit)
if err != nil {
return nil, nil, err
}
var result []byte
if isInit {
result, err = w.txInitSubmitter(tx, args...)
} else {
result, err = w.txSubmitter(tx, args...)
}
result, err := w.txSubmitter(tx, args...)
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -249,13 +242,18 @@ func getNetwork(gateway *gateway.Gateway, channelId string) (*gateway.Network, e
return gateway.GetNetwork(channelId)
}

func prepareTx(w *gwRPCWrapper, signer, channelId, chaincodeName, method string) (*gateway.Transaction, <-chan *fab.TxStatusEvent, error) {
func prepareTx(w *gwRPCWrapper, signer, channelId, chaincodeName, method string, isInit bool) (*gateway.Transaction, <-chan *fab.TxStatusEvent, error) {
channelClient, err := w.getGatewayClient(signer, channelId)
if err != nil {
return nil, nil, err
}
contractClient := channelClient.GetContract(chaincodeName)
tx, err := contractClient.CreateTransaction(method)
var tx *gateway.Transaction
if isInit {
tx, err = contractClient.CreateTransaction(method, gateway.WithInit())
} else {
tx, err = contractClient.CreateTransaction(method)
}
if err != nil {
return nil, nil, err
}
Expand All @@ -266,7 +264,3 @@ func prepareTx(w *gwRPCWrapper, signer, channelId, chaincodeName, method string)
func submitTx(tx *gateway.Transaction, args ...string) ([]byte, error) {
return tx.Submit(args...)
}

func submitInitTx(tx *gateway.Transaction, args ...string) ([]byte, error) {
return tx.SubmitInit(args...)
}
8 changes: 4 additions & 4 deletions internal/fabric/client/rpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ func TestGatewayClientSendTx(t *testing.T) {
wrapper.gatewayCreator = createMockGateway
wrapper.networkCreator = createMockNetwork

mockPrepareTx := func(w *gwRPCWrapper, signer, channelId, chaincodeName, method string) (*gateway.Transaction, <-chan *fab.TxStatusEvent, error) {
mockPrepareTx := func(w *gwRPCWrapper, signer, channelId, chaincodeName, method string, isInit bool) (*gateway.Transaction, <-chan *fab.TxStatusEvent, error) {
notifier := make(chan *fab.TxStatusEvent)
go func() {
notifier <- &fab.TxStatusEvent{}
Expand Down Expand Up @@ -268,18 +268,18 @@ func TestGatewayClientSendInitTx(t *testing.T) {
wrapper.gatewayCreator = createMockGateway
wrapper.networkCreator = createMockNetwork

mockPrepareTx := func(w *gwRPCWrapper, signer, channelId, chaincodeName, method string) (*gateway.Transaction, <-chan *fab.TxStatusEvent, error) {
mockPrepareTx := func(w *gwRPCWrapper, signer, channelId, chaincodeName, method string, isInit bool) (*gateway.Transaction, <-chan *fab.TxStatusEvent, error) {
notifier := make(chan *fab.TxStatusEvent)
go func() {
notifier <- &fab.TxStatusEvent{}
}()
return nil, notifier, nil
}
mockSubmitInitTx := func(tx *gateway.Transaction, args ...string) ([]byte, error) {
mockSubmitTx := func(tx *gateway.Transaction, args ...string) ([]byte, error) {
return []byte(""), nil
}
wrapper.txPreparer = mockPrepareTx
wrapper.txInitSubmitter = mockSubmitInitTx
wrapper.txSubmitter = mockSubmitTx

_, _, err = wrapper.sendTransaction("signer1", "channel-1", "chaincode-1", "method-1", []string{"args-1"}, true)
assert.NoError(err)
Expand Down

0 comments on commit 1df7cbb

Please sign in to comment.