forked from hyperledger/fabric-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FABG-933 Gateway package for Go SDK (hyperledger#51)
* FABG-933 Gateway package for Go SDK First user story for Gateway SDK Basic framework Unit tests Integration test Signed-off-by: andrew-coleman <[email protected]> * FABG-933 Gateway package for Go SDK Incorporate review feedback Signed-off-by: andrew-coleman <[email protected]>
- Loading branch information
1 parent
745ca5b
commit be7b275
Showing
20 changed files
with
1,655 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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{} |
Oops, something went wrong.