Skip to content

Commit

Permalink
bug fix #76 #77
Browse files Browse the repository at this point in the history
- [x] network/channel support for IsTxFinal #77
- [x] nwo: fabric platform's Name() does not return the topology name #76

Signed-off-by: Angelo De Caro <adc@zurich.ibm.com>
  • Loading branch information
adecaro committed Jul 16, 2021
1 parent 05be169 commit f57c564
Show file tree
Hide file tree
Showing 22 changed files with 143 additions and 79 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ require (
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad // indirect
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect
golang.org/x/text v0.3.5 // indirect
golang.org/x/tools v0.1.4 // indirect
golang.org/x/tools v0.1.5 // indirect
google.golang.org/genproto v0.0.0-20210201184850-646a494a81ea // indirect
google.golang.org/grpc v1.35.0
google.golang.org/protobuf v1.25.0
Expand Down
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -862,10 +862,8 @@ golang.org/x/tools v0.0.0-20200131233409-575de47986ce/go.mod h1:b+2E5dAYhXwXZwtn
golang.org/x/tools v0.0.0-20201105001634-bc3cf281b174/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/tools v0.0.0-20201224043029-2b0845dc783e/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0=
golang.org/x/tools v0.1.4 h1:cVngSRcfgyZCzys3KYOpCFa+4dqX/Oub9tAq00ttGVs=
golang.org/x/tools v0.1.4 h1:cVngSRcfgyZCzys3KYOpCFa+4dqX/Oub9tAq00ttGVs=
golang.org/x/tools v0.1.4/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.4/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.5 h1:ouewzE6p+/VEB31YYnTbEJdi8pFqKp4P4n85vwo3DHA=
golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
3 changes: 2 additions & 1 deletion integration/fabric/atsa/chaincode/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ package chaincode
import (
"github.com/hyperledger-labs/fabric-smart-client/integration/fabric/atsa/chaincode/views"
"github.com/hyperledger-labs/fabric-smart-client/integration/nwo/common"
"github.com/hyperledger-labs/fabric-smart-client/pkg/api"
"github.com/hyperledger-labs/fabric-smart-client/platform/view/view"
)

type ViewClient interface {
CallView(fid string, in []byte) (interface{}, error)
IsTxFinal(txid string) error
IsTxFinal(txid string, opts ...api.ServiceOption) error
}

type Client struct {
Expand Down
7 changes: 4 additions & 3 deletions integration/fabric/atsa/fsc/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ import (
"github.com/hyperledger-labs/fabric-smart-client/integration/fabric/atsa/fsc/states"
"github.com/hyperledger-labs/fabric-smart-client/integration/fabric/atsa/fsc/views"
"github.com/hyperledger-labs/fabric-smart-client/integration/nwo/common"
"github.com/hyperledger-labs/fabric-smart-client/pkg/api"
"github.com/hyperledger-labs/fabric-smart-client/platform/view/view"
)

type ViewClient interface {
CallView(fid string, in []byte) (interface{}, error)
IsTxFinal(txid string) error
IsTxFinal(txid string, opts ...api.ServiceOption) error
}

type Client struct {
Expand Down Expand Up @@ -79,6 +80,6 @@ func (c *Client) Transfer(assetID string, agreementID string, recipient view.Ide
return nil
}

func (c *Client) IsTxFinal(id string) error {
return c.c.IsTxFinal(id)
func (c *Client) IsTxFinal(id string, opts ...api.ServiceOption) error {
return c.c.IsTxFinal(id, opts...)
}
3 changes: 2 additions & 1 deletion integration/nwo/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"gopkg.in/yaml.v2"

"github.com/hyperledger-labs/fabric-smart-client/integration/nwo/fsc/identity"
"github.com/hyperledger-labs/fabric-smart-client/pkg/api"
"github.com/hyperledger-labs/fabric-smart-client/platform/view/services/client/view"
"github.com/hyperledger-labs/fabric-smart-client/platform/view/services/grpc"
)
Expand Down Expand Up @@ -73,7 +74,7 @@ type Builder interface {

type ViewClient interface {
CallView(fid string, in []byte) (interface{}, error)
IsTxFinal(txid string) error
IsTxFinal(txid string, opts ...api.ServiceOption) error
}

type Platform interface {
Expand Down
2 changes: 1 addition & 1 deletion integration/nwo/fabric/fabric.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func NewPlatform(context api.Context, t api.Topology, components BuilderClient)
}

func (p *platform) Name() string {
return "default"
return p.Topology().Name()
}

func (p *platform) Type() string {
Expand Down
33 changes: 32 additions & 1 deletion pkg/api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,37 @@ SPDX-License-Identifier: Apache-2.0

package api

type ServiceOptions struct {
Network string
Channel string
}

func CompileServiceOptions(opts ...ServiceOption) (*ServiceOptions, error) {
txOptions := &ServiceOptions{}
for _, opt := range opts {
if err := opt(txOptions); err != nil {
return nil, err
}
}
return txOptions, nil
}

type ServiceOption func(*ServiceOptions) error

func WithNetwork(network string) ServiceOption {
return func(o *ServiceOptions) error {
o.Network = network
return nil
}
}

func WithChannel(channel string) ServiceOption {
return func(o *ServiceOptions) error {
o.Channel = channel
return nil
}
}

type ViewClient interface {
// CallView takes in input a view factory identifier, fid, and an input, in, and invokes the
// factory f bound to fid on input in. The view returned by the factory is invoked on
Expand All @@ -25,5 +56,5 @@ type ViewClient interface {

// IsTxFinal takes in input a transaction id and return nil if the transaction has been committed,
// an error otherwise.
IsTxFinal(txid string) error
IsTxFinal(txid string, opts ...ServiceOption) error
}
8 changes: 6 additions & 2 deletions pkg/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,12 @@ func (n *node) ResolveIdentities(endpoints ...string) ([]view.Identity, error) {
return ids, nil
}

func (n *node) IsTxFinal(txid string) error {
return fabric.GetDefaultChannel(n.registry).Finality().IsFinal(txid)
func (n *node) IsTxFinal(txid string, opts ...api.ServiceOption) error {
options, err := api.CompileServiceOptions(opts...)
if err != nil {
return err
}
return fabric.GetChannel(n.registry, options.Network, options.Channel).Finality().IsFinal(txid)
}

func (n *node) CallView(fid string, in []byte) (interface{}, error) {
Expand Down
2 changes: 1 addition & 1 deletion platform/fabric/core/generic/chaincode/invoke.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func (i *Invoke) Call() (interface{}, error) {
// collect responses
responses, err := i.collectResponses(endorserClients, signedProp)
if err != nil {
return nil, errors.WithMessagef(err, "error endorsing")
return nil, errors.Wrapf(err, "failed collecting proposal responses")
}

if len(responses) == 0 {
Expand Down
31 changes: 19 additions & 12 deletions platform/fabric/core/generic/finality/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/pkg/errors"

"github.com/hyperledger-labs/fabric-smart-client/platform/fabric/driver"
view2 "github.com/hyperledger-labs/fabric-smart-client/platform/view/services/server/view"
protos2 "github.com/hyperledger-labs/fabric-smart-client/platform/view/services/server/view/protos"
)
Expand All @@ -20,33 +21,39 @@ type Server interface {
RegisterProcessor(typ reflect.Type, p view2.Processor)
}

type NetworkProvider interface {
FabricNetworkService(network string) (driver.FabricNetworkService, error)
}

type finalityHandler struct {
network Network
networkProvider NetworkProvider
}

func InstallHandler(server Server, network Network) {
fh := &finalityHandler{network: network}
func InstallHandler(server Server, networkProvider NetworkProvider) {
fh := &finalityHandler{networkProvider: networkProvider}
server.RegisterProcessor(reflect.TypeOf(&protos2.Command_IsTxFinal{}), fh.isTxFinal)
}

func (s *finalityHandler) isTxFinal(ctx context.Context, command *protos2.Command) (interface{}, error) {
isTxFinalCommand := command.Payload.(*protos2.Command_IsTxFinal).IsTxFinal
c := command.Payload.(*protos2.Command_IsTxFinal).IsTxFinal

logger.Debugf("Answering: Is [%s] final?", isTxFinalCommand.Txid)
logger.Debugf("Answering: Is [%s] final on [%s:%s]?", c.Txid, c.Network, c.Channel)

ch, err := s.network.Channel(isTxFinalCommand.Channel)
network, err := s.networkProvider.FabricNetworkService(c.Network)
if err != nil {
return nil, errors.Errorf("failed getting finality service for channel [%s], err [%s]", isTxFinalCommand.Channel, err)
return nil, errors.Errorf("failed getting network [%s], err [%s]", c.Network, err)
}

err = ch.IsFinal(isTxFinalCommand.Txid)
ch, err := network.Channel(c.Channel)
if err != nil {
logger.Debugf("Answering: Is [%s] final? No", isTxFinalCommand.Txid)
return nil, errors.Errorf("failed getting finality service for channel [%s], err [%s]", c.Channel, err)
}
err = ch.IsFinal(c.Txid)
if err != nil {
logger.Debugf("Answering: Is [%s] final on [%s:%s]? No", c.Txid, c.Network, c.Channel)
return &protos2.CommandResponse_IsTxFinalResponse{IsTxFinalResponse: &protos2.IsTxFinalResponse{
Payload: []byte(err.Error()),
}}, nil
}

logger.Debugf("Answering: Is [%s] final? Yes", isTxFinalCommand.Txid)
logger.Debugf("Answering: Is [%s] final on [%s:%s]? Yes", c.Txid, c.Network, c.Channel)
return &protos2.CommandResponse_IsTxFinalResponse{IsTxFinalResponse: &protos2.IsTxFinalResponse{}}, nil
}
10 changes: 6 additions & 4 deletions platform/fabric/core/generic/rwset/envelope.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
)

type UnpackedEnvelope struct {
NetworkID string
TxID string
Ch string
ChaincodeName string
Expand All @@ -31,15 +32,15 @@ type UnpackedEnvelope struct {
ProposalResponses []*peer.ProposalResponse
}

func UnpackEnvelopeFromBytes(raw []byte) (*UnpackedEnvelope, error) {
func UnpackEnvelopeFromBytes(networkID string, raw []byte) (*UnpackedEnvelope, error) {
env := &common.Envelope{}
if err := proto.Unmarshal(raw, env); err != nil {
return nil, err
}
return UnpackEnvelope(env)
return UnpackEnvelope(networkID, env)
}

func UnpackEnvelope(env *common.Envelope) (*UnpackedEnvelope, error) {
func UnpackEnvelope(networkID string, env *common.Envelope) (*UnpackedEnvelope, error) {
payl, err := protoutil.UnmarshalPayload(env.Payload)
if err != nil {
logger.Errorf("VSCC error: GetPayload failed, err %s", err)
Expand Down Expand Up @@ -115,6 +116,7 @@ func UnpackEnvelope(env *common.Envelope) (*UnpackedEnvelope, error) {
}

return &UnpackedEnvelope{
NetworkID: networkID,
TxID: chdr.TxId,
Ch: chdr.ChannelId,
ChaincodeName: cis.ChaincodeSpec.ChaincodeId.Name,
Expand All @@ -136,7 +138,7 @@ func (u *UnpackedEnvelope) ID() string {
}

func (u *UnpackedEnvelope) Network() string {
return ""
return u.NetworkID
}

func (u *UnpackedEnvelope) Channel() string {
Expand Down
3 changes: 2 additions & 1 deletion platform/fabric/core/generic/rwset/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var logger = flogging.MustGetLogger("fabric-sdk.rwset")
type Network interface {
Channel(name string) (driver.Channel, error)
TransactionManager() driver.TransactionManager
Name() string
}

type RWSExtractor interface {
Expand Down Expand Up @@ -132,7 +133,7 @@ func (r *processorManager) getTxFromEvn(ch driver.Channel, txid string) (driver.
return nil, nil, errors.Wrapf(err, "failed unmarshalling envelope [%s]", txid)
}
logger.Debugf("unpack envelope [%s,%s]", ch.Name(), txid)
upe, err := UnpackEnvelope(env)
upe, err := UnpackEnvelope(r.network.Name(), env)
if err != nil {
return nil, nil, errors.Wrapf(err, "failed unpacking envelope [%s]", txid)
}
Expand Down
5 changes: 3 additions & 2 deletions platform/fabric/core/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ func (p *fnsProvider) Start(ctx context.Context) error {
}
}
}

finality.InstallHandler(view3.GetService(p.sp), p)

return nil
}

Expand Down Expand Up @@ -142,8 +145,6 @@ func (p *fnsProvider) newFNS(network string) (driver.FabricNetworkService, error
return nil, errors.Wrap(err, "failed instantiating fabric service provider")
}

finality.InstallHandler(view3.GetService(p.sp), net)

return net, nil
}

Expand Down
1 change: 1 addition & 0 deletions platform/fabric/sdk/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ func (p *p) Install() error {
p.fnsProvider, err = core.NewFabricNetworkServiceProvider(p.registry, fnspConfig)
assert.NoError(err, "failed instantiating fabric network service provider")
assert.NoError(p.registry.RegisterService(p.fnsProvider))

assert.NoError(fabric2.GetDefaultFNS(p.registry).ProcessorManager().SetDefaultProcessor(
state.NewRWSetProcessor(fabric2.GetDefaultFNS(p.registry)),
))
Expand Down
Loading

0 comments on commit f57c564

Please sign in to comment.