Skip to content
This repository was archived by the owner on Apr 25, 2025. It is now read-only.

Commit 1c704ae

Browse files
committed
Change to using options in CreateTransaction
Signed-off-by: Jim Zhang <jim.zhang@kaleido.io>
1 parent 4cdb953 commit 1c704ae

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

pkg/gateway/transaction.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ type Transaction struct {
2929
request *channel.Request
3030
endorsingPeers []string
3131
collections []string
32+
isInit bool
3233
eventch chan *fab.TxStatusEvent
3334
}
3435

@@ -83,6 +84,15 @@ func WithCollections(collections ...string) TransactionOption {
8384
}
8485
}
8586

87+
// WithInit is an option that makes the transaction fulfill the --init-required condition before the chaincode
88+
// can be invoked to process regular transactions
89+
func WithInit() TransactionOption {
90+
return func(txn *Transaction) error {
91+
txn.isInit = true
92+
return nil
93+
}
94+
}
95+
8696
// Evaluate a transaction function and return its results.
8797
// The transaction function will be evaluated on the endorsing peers but
8898
// the responses will not be sent to the ordering service and hence will
@@ -119,21 +129,12 @@ func (txn *Transaction) Evaluate(args ...string) ([]byte, error) {
119129
// will be evaluated on the endorsing peers and then submitted to the ordering service
120130
// for committing to the ledger.
121131
func (txn *Transaction) Submit(args ...string) ([]byte, error) {
122-
return txn.submit(false, args...)
123-
}
124-
125-
// SubmitInit sends an initialization transaction, for chaincodes that requires init before processing regular transactions
126-
func (txn *Transaction) SubmitInit(args ...string) ([]byte, error) {
127-
return txn.submit(true, args...)
128-
}
129-
130-
func (txn *Transaction) submit(isInit bool, args ...string) ([]byte, error) {
131132
bytes := make([][]byte, len(args))
132133
for i, v := range args {
133134
bytes[i] = []byte(v)
134135
}
135136
txn.request.Args = bytes
136-
txn.request.IsInit = isInit
137+
txn.request.IsInit = txn.isInit
137138

138139
var options []channel.RequestOption
139140
if txn.endorsingPeers != nil {

pkg/gateway/transaction_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ func TestInitTransactionOptions(t *testing.T) {
105105
WithTransient(transient),
106106
WithEndorsingPeers("peer1"),
107107
WithCollections("_implicit_org_org1"),
108+
WithInit(),
108109
)
109110

110111
if err != nil {
@@ -127,7 +128,7 @@ func TestInitTransactionOptions(t *testing.T) {
127128
}
128129

129130
txn.Evaluate("arg1", "arg2")
130-
txn.SubmitInit("arg1", "arg2")
131+
txn.Submit("arg1", "arg2")
131132
}
132133

133134
func TestCommitEvent(t *testing.T) {

0 commit comments

Comments
 (0)