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

Commit e170b98

Browse files
authored
Add WithInit() to support chaincodes that require init (#236)
* Add SubmitInit() to support chaincodes that require init Signed-off-by: Jim Zhang <[email protected]> * Change to using options in CreateTransaction Signed-off-by: Jim Zhang <[email protected]>
1 parent 7a94fbc commit e170b98

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

pkg/gateway/transaction.go

Lines changed: 11 additions & 0 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
@@ -124,6 +134,7 @@ func (txn *Transaction) Submit(args ...string) ([]byte, error) {
124134
bytes[i] = []byte(v)
125135
}
126136
txn.request.Args = bytes
137+
txn.request.IsInit = txn.isInit
127138

128139
var options []channel.RequestOption
129140
if txn.endorsingPeers != nil {

pkg/gateway/transaction_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,57 @@ func TestTransactionOptions(t *testing.T) {
8080
txn.Submit("arg1", "arg2")
8181
}
8282

83+
func TestInitTransactionOptions(t *testing.T) {
84+
transient := make(map[string][]byte)
85+
transient["price"] = []byte("8500")
86+
87+
c := mockChannelProvider("mychannel")
88+
89+
gw := &Gateway{
90+
options: &gatewayOptions{
91+
Timeout: defaultTimeout,
92+
},
93+
}
94+
95+
nw, err := newNetwork(gw, c)
96+
97+
if err != nil {
98+
t.Fatalf("Failed to create network: %s", err)
99+
}
100+
101+
contr := nw.GetContract("contract1")
102+
103+
txn, err := contr.CreateTransaction(
104+
"txn1",
105+
WithTransient(transient),
106+
WithEndorsingPeers("peer1"),
107+
WithCollections("_implicit_org_org1"),
108+
WithInit(),
109+
)
110+
111+
if err != nil {
112+
t.Fatalf("Failed to create transaction: %s", err)
113+
}
114+
115+
data := txn.request.TransientMap["price"]
116+
if string(data) != "8500" {
117+
t.Fatalf("Incorrect transient data: %s", string(data))
118+
}
119+
120+
endorsers := txn.endorsingPeers
121+
if endorsers[0] != "peer1" {
122+
t.Fatalf("Incorrect endorsing peer: %s", endorsers[0])
123+
}
124+
125+
collections := txn.collections
126+
if collections[0] != "_implicit_org_org1" {
127+
t.Fatalf("Incorrect collection: %s", collections[0])
128+
}
129+
130+
txn.Evaluate("arg1", "arg2")
131+
txn.Submit("arg1", "arg2")
132+
}
133+
83134
func TestCommitEvent(t *testing.T) {
84135
c := mockChannelProvider("mychannel")
85136

0 commit comments

Comments
 (0)