Skip to content

Commit

Permalink
FABG-933 Gateway package for Go SDK (#51)
Browse files Browse the repository at this point in the history
* FABG-933 Gateway package for Go SDK

First user story for Gateway SDK
Basic framework
Unit tests
Integration test

Signed-off-by: andrew-coleman <andrew_coleman@uk.ibm.com>

* FABG-933 Gateway package for Go SDK

Incorporate review feedback

Signed-off-by: andrew-coleman <andrew_coleman@uk.ibm.com>
  • Loading branch information
andrew-coleman authored Feb 14, 2020
1 parent 745ca5b commit be7b275
Show file tree
Hide file tree
Showing 20 changed files with 1,655 additions and 3 deletions.
63 changes: 63 additions & 0 deletions pkg/gateway/contract.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
Copyright 2020 IBM All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package gateway

import "github.com/hyperledger/fabric-sdk-go/pkg/client/channel"

// A Contract object represents a smart contract instance in a network.
// Applications should get a Contract instance from a Network using the GetContract method
type Contract struct {
chaincodeID string
name string
network *Network
client *channel.Client
}

func newContract(network *Network, chaincodeID string, name string) *Contract {
return &Contract{network: network, client: network.client, chaincodeID: chaincodeID, name: name}
}

// Name returns the name of the smart contract
func (c *Contract) Name() string {
return c.chaincodeID
}

// EvaluateTransaction will evaluate a transaction function and return its results.
// The transaction function 'name'
// will be evaluated on the endorsing peers but the responses will not be sent to
// the ordering service and hence will not be committed to the ledger.
// This can be used for querying the world state.
func (c *Contract) EvaluateTransaction(name string, args ...string) ([]byte, error) {
txn, err := c.CreateTransaction(name)

if err != nil {
return nil, err
}

return txn.Evaluate(args...)
}

// SubmitTransaction will submit a transaction to the ledger. The transaction function 'name'
// will be evaluated on the endorsing peers and then submitted to the ordering service
// for committing to the ledger.
func (c *Contract) SubmitTransaction(name string, args ...string) ([]byte, error) {
txn, err := c.CreateTransaction(name)

if err != nil {
return nil, err
}

return txn.Submit(args...)
}

// CreateTransaction creates an object representing a specific invocation of a transaction
// function implemented by this contract, and provides more control over
// the transaction invocation using the optional arguments. A new transaction object must
// be created for each transaction invocation.
func (c *Contract) CreateTransaction(name string, args ...TransactionOption) (*Transaction, error) {
return newTransaction(name, c, args...)
}
84 changes: 84 additions & 0 deletions pkg/gateway/contract_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
Copyright 2020 IBM All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package gateway

import (
"testing"
)

func TestCreateTransaction(t *testing.T) {
c := mockChannelProvider("mychannel")

gw := &Gateway{}

nw, err := newNetwork(gw, c)

if err != nil {
t.Fatalf("Failed to create network: %s", err)
}

contr := nw.GetContract("contract1")

txn, err := contr.CreateTransaction("txn1")

if err != nil {
t.Fatalf("Failed to create transaction: %s", err)
}

name := txn.name
if name != "txn1" {
t.Fatalf("Incorrect transaction name: %s", name)
}
}

func TestSubmitTransaction(t *testing.T) {
c := mockChannelProvider("mychannel")

gw := &Gateway{}

nw, err := newNetwork(gw, c)

if err != nil {
t.Fatalf("Failed to create network: %s", err)
}

contr := nw.GetContract("contract1")

result, err := contr.SubmitTransaction("txn1")

if err != nil {
t.Fatalf("Failed to submit transaction: %s", err)
}

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

func TestEvaluateTransaction(t *testing.T) {
c := mockChannelProvider("mychannel")

gw := &Gateway{}

nw, err := newNetwork(gw, c)

if err != nil {
t.Fatalf("Failed to create network: %s", err)
}

contr := nw.GetContract("contract1")

result, err := contr.EvaluateTransaction("txn1")

if err != nil {
t.Fatalf("Failed to evaluate transaction: %s", err)
}

if string(result) != "abc" {
t.Fatalf("Incorrect transaction result: %s", result)
}
}
53 changes: 53 additions & 0 deletions pkg/gateway/defaultcommithandlers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
Copyright 2020 IBM All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package gateway

type list struct {
None CommitHandlerFactory
OrgAll CommitHandlerFactory
OrgAny CommitHandlerFactory
NetworkAll CommitHandlerFactory
NetworkAny CommitHandlerFactory
}

// DefaultCommitHandlers provides the built-in commit handler implementations.
var DefaultCommitHandlers = &list{
None: nil,
OrgAll: orgAll,
OrgAny: orgAny,
NetworkAll: networkAll,
NetworkAny: networkAny,
}

type commithandler struct {
transactionID string
network Network
}

func (ch *commithandler) StartListening() {
}

func (ch *commithandler) WaitForEvents(timeout int64) {
}

func (ch *commithandler) CancelListening() {
}

type commithandlerfactory struct {
}

func (chf *commithandlerfactory) Create(txid string, network Network) CommitHandler {
return &commithandler{
transactionID: txid,
network: network,
}
}

var orgAll = &commithandlerfactory{}
var orgAny = &commithandlerfactory{}
var networkAll = &commithandlerfactory{}
var networkAny = &commithandlerfactory{}
Loading

0 comments on commit be7b275

Please sign in to comment.