Skip to content

Commit

Permalink
FABG-938 Transient Data (#58)
Browse files Browse the repository at this point in the history
gateway package to support transient data
plug options for endorsing peers and timeout in line with node/java
unit and integration tests

Signed-off-by: andrew-coleman <andrew_coleman@uk.ibm.com>
  • Loading branch information
andrew-coleman authored Mar 17, 2020
1 parent 3e8998e commit e71412f
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 5 deletions.
16 changes: 14 additions & 2 deletions pkg/gateway/contract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,13 @@ func TestCreateTransaction(t *testing.T) {
func TestSubmitTransaction(t *testing.T) {
c := mockChannelProvider("mychannel")

gw := &Gateway{}
gw := &Gateway{
options: &gatewayOptions{
CommitHandler: DefaultCommitHandlers.OrgAll,
Discovery: defaultDiscovery,
Timeout: defaultTimeout,
},
}

nw, err := newNetwork(gw, c)

Expand All @@ -62,7 +68,13 @@ func TestSubmitTransaction(t *testing.T) {
func TestEvaluateTransaction(t *testing.T) {
c := mockChannelProvider("mychannel")

gw := &Gateway{}
gw := &Gateway{
options: &gatewayOptions{
CommitHandler: DefaultCommitHandlers.OrgAll,
Discovery: defaultDiscovery,
Timeout: defaultTimeout,
},
}

nw, err := newNetwork(gw, c)

Expand Down
19 changes: 18 additions & 1 deletion pkg/gateway/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package gateway

import (
"fmt"
"time"

"github.com/hyperledger/fabric-sdk-go/pkg/client/msp"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/context"
Expand All @@ -17,6 +18,11 @@ import (
"github.com/pkg/errors"
)

const (
defaultTimeout = 5 * time.Minute
defaultDiscovery = true
)

// Gateway is the entry point to a Fabric network
type Gateway struct {
sdk *fabsdk.FabricSDK
Expand All @@ -30,6 +36,7 @@ type gatewayOptions struct {
User string
CommitHandler CommitHandlerFactory
Discovery bool
Timeout time.Duration
}

// Option functional arguments can be supplied when connecting to the gateway.
Expand All @@ -48,7 +55,8 @@ func Connect(config ConfigOption, identity IdentityOption, options ...Option) (*
g := &Gateway{
options: &gatewayOptions{
CommitHandler: DefaultCommitHandlers.OrgAll,
Discovery: true,
Discovery: defaultDiscovery,
Timeout: defaultTimeout,
},
}

Expand Down Expand Up @@ -185,6 +193,15 @@ func WithDiscovery(discovery bool) Option {
}
}

// WithTimeout is an optional argument to the Connect method which
// defines the commit timeout for all transaction submissions for this gateway.
func WithTimeout(timeout time.Duration) Option {
return func(gw *Gateway) error {
gw.options.Timeout = timeout
return nil
}
}

func (gw *Gateway) getSDK() *fabsdk.FabricSDK {
return gw.sdk
}
Expand Down
26 changes: 26 additions & 0 deletions pkg/gateway/gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package gateway
import (
"reflect"
"testing"
"time"

"github.com/hyperledger/fabric-sdk-go/pkg/core/config"
"github.com/hyperledger/fabric-sdk-go/pkg/fabsdk"
Expand Down Expand Up @@ -71,6 +72,10 @@ func TestConnectNoOptions(t *testing.T) {
if options.Discovery != true {
t.Fatal("Discovery not correctly initialized")
}

if options.Timeout != defaultTimeout {
t.Fatal("Timeout not correctly initialized")
}
}

func TestConnectWithSDK(t *testing.T) {
Expand Down Expand Up @@ -98,6 +103,10 @@ func TestConnectWithSDK(t *testing.T) {
if options.Discovery != true {
t.Fatal("Discovery not correctly initialized")
}

if options.Timeout != defaultTimeout {
t.Fatal("Timeout not correctly initialized")
}
}

func TestConnectWithIdentity(t *testing.T) {
Expand Down Expand Up @@ -158,6 +167,23 @@ func TestConnectWithDiscovery(t *testing.T) {
}
}

func TestConnectWithTimout(t *testing.T) {
gw, err := Connect(
WithConfig(config.FromFile("testdata/connection-tls.json")),
WithUser("user1"),
WithTimeout(20*time.Second),
)
if err != nil {
t.Fatalf("Failed to create gateway: %s", err)
}

options := gw.options

if options.Timeout != 20*time.Second {
t.Fatal("Timeout not set correctly")
}
}

func TestConnectWithMultipleOptions(t *testing.T) {
gw, err := Connect(
WithConfig(config.FromFile("testdata/connection-tls.json")),
Expand Down
20 changes: 18 additions & 2 deletions pkg/gateway/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package gateway

import (
"github.com/hyperledger/fabric-sdk-go/pkg/client/channel"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/fab"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -76,7 +77,13 @@ func (txn *Transaction) Evaluate(args ...string) ([]byte, error) {
}
txn.request.Args = bytes

response, err := txn.contract.client.Query(*txn.request, channel.WithTargets(txn.contract.network.peers[0]))
var options []channel.RequestOption
options = append(options, channel.WithTimeout(fab.Query, txn.contract.network.gateway.options.Timeout))

response, err := txn.contract.client.Query(
*txn.request,
options...,
)
if err != nil {
return nil, errors.Wrap(err, "Failed to evaluate")
}
Expand All @@ -94,7 +101,16 @@ func (txn *Transaction) Submit(args ...string) ([]byte, error) {
}
txn.request.Args = bytes

response, err := txn.contract.client.Execute(*txn.request)
var options []channel.RequestOption
if txn.endorsingPeers != nil {
options = append(options, channel.WithTargetEndpoints(txn.endorsingPeers...))
}
options = append(options, channel.WithTimeout(fab.Execute, txn.contract.network.gateway.options.Timeout))

response, err := txn.contract.client.Execute(
*txn.request,
options...,
)
if err != nil {
return nil, errors.Wrap(err, "Failed to submit")
}
Expand Down
41 changes: 41 additions & 0 deletions test/integration/pkg/gateway/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,28 @@ func RunWithWallet(t *testing.T) {
testGateway(gw, t)
}

// RunWithTransient tests sending transient data
func RunWithTransient(t *testing.T) {
configPath := integration.GetConfigPath("config_e2e.yaml")

gw, err := gateway.Connect(
gateway.WithConfig(config.FromFile(configPath)),
gateway.WithUser("User1"),
)
if err != nil {
t.Fatalf("Failed to create new Gateway: %s", err)
}
defer gw.Close()

nw, err := gw.GetNetwork(channelID)
if err != nil {
t.Fatalf("Failed to get network: %s", err)
}

contract := nw.GetContract(ccID)
testTransientData(contract, t)
}

func testGateway(gw *gateway.Gateway, t *testing.T) {
nw, err := gw.GetNetwork(channelID)
if err != nil {
Expand Down Expand Up @@ -149,6 +171,25 @@ func runContract(contract *gateway.Contract, t *testing.T) {
}
}

func testTransientData(contract *gateway.Contract, t *testing.T) {
transient := make(map[string][]byte)
transient["result"] = []byte("8500")

txn, err := contract.CreateTransaction("invoke", gateway.WithTransient(transient))
if err != nil {
t.Fatalf("Failed to create transaction: %s", err)
}

result, err := txn.Submit("move", "a", "b", "1")
if err != nil {
t.Fatalf("Failed to submit transaction: %s", err)
}

if string(result) != "8500" {
t.Fatalf("Incorrect result: %s", string(result))
}
}

func populateWallet(wallet *gateway.Wallet) error {
credPath := filepath.Join(
metadata.GetProjectPath(),
Expand Down
6 changes: 6 additions & 0 deletions test/integration/pkg/gateway/gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,9 @@ func TestGatewayWithWallet(t *testing.T) {
RunWithWallet(t)
})
}

func TestTransientData(t *testing.T) {
t.Run("Base", func(t *testing.T) {
RunWithTransient(t)
})
}

0 comments on commit e71412f

Please sign in to comment.