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

Commit 2578687

Browse files
committed
[FAB-6382] - Add TxValidationCode to ExecuteTxResponse
Added TxValidationCode to ExecuteTxResponse so that the client knows the reason that the transaction failed. Change-Id: I5873e282dc447cd043273d294099dbe54e118872 Signed-off-by: Bob Stasyszyn <bob.stasyszyn@securekey.com>
1 parent e89a861 commit 2578687

File tree

6 files changed

+51
-39
lines changed

6 files changed

+51
-39
lines changed

api/apitxn/txn.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ SPDX-License-Identifier: Apache-2.0
66

77
package apitxn
88

9-
import "time"
9+
import (
10+
"time"
11+
12+
pb "github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/peer"
13+
)
1014

1115
// QueryRequest contains the parameters for query
1216
type QueryRequest struct {
@@ -30,8 +34,9 @@ type QueryResponse struct {
3034

3135
// ExecuteTxResponse contains result of asynchronous call
3236
type ExecuteTxResponse struct {
33-
Response TransactionID
34-
Error error
37+
Response TransactionID
38+
Error error
39+
TxValidationCode pb.TxValidationCode
3540
}
3641

3742
// ExecuteTxRequest contains the parameters to execute transaction

pkg/fabric-txn/admin/transactionconfig.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
internal "github.com/hyperledger/fabric-sdk-go/pkg/fabric-txn/internal"
1919
"github.com/hyperledger/fabric-sdk-go/pkg/logging"
2020
"github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/common"
21+
"github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/peer"
2122
)
2223

2324
var logger = logging.NewLogger("fabric_sdk_go")
@@ -61,20 +62,21 @@ func SendInstantiateCC(channel fab.Channel, chainCodeID string, args []string,
6162
}
6263

6364
// Register for commit event
64-
done, fail := internal.RegisterTxEvent(txID, eventHub)
65+
chcode := internal.RegisterTxEvent(txID, eventHub)
6566

6667
if _, err = internal.CreateAndSendTransaction(channel, transactionProposalResponse); err != nil {
6768
return fmt.Errorf("CreateTransaction returned error: %v", err)
6869
}
6970

7071
select {
71-
case <-done:
72-
case <-fail:
73-
return fmt.Errorf("instantiateCC Error received from eventhub for txid(%s) error(%v)", txID, fail)
72+
case code := <-chcode:
73+
if code == peer.TxValidationCode_VALID {
74+
return nil
75+
}
76+
return fmt.Errorf("instantiateCC Error received from eventhub for txid(%s), code(%s)", txID, code)
7477
case <-time.After(time.Second * 30):
7578
return fmt.Errorf("instantiateCC Didn't receive block event for txid(%s)", txID)
7679
}
77-
return nil
7880
}
7981

8082
// SendUpgradeCC Sends upgrade CC proposal to one or more endorsing peers
@@ -95,20 +97,21 @@ func SendUpgradeCC(channel fab.Channel, chainCodeID string, args []string,
9597
}
9698

9799
// Register for commit event
98-
done, fail := internal.RegisterTxEvent(txID, eventHub)
100+
chcode := internal.RegisterTxEvent(txID, eventHub)
99101

100102
if _, err = internal.CreateAndSendTransaction(channel, transactionProposalResponse); err != nil {
101103
return fmt.Errorf("CreateTransaction returned error: %v", err)
102104
}
103105

104106
select {
105-
case <-done:
106-
case <-fail:
107-
return fmt.Errorf("upgradeCC Error received from eventhub for txid(%s) error(%v)", txID, fail)
107+
case code := <-chcode:
108+
if code == peer.TxValidationCode_VALID {
109+
return nil
110+
}
111+
return fmt.Errorf("upgradeCC Error received from eventhub for txid(%s) code(%s)", txID, code)
108112
case <-time.After(time.Second * 30):
109113
return fmt.Errorf("upgradeCC Didn't receive block event for txid(%s)", txID)
110114
}
111-
return nil
112115
}
113116

114117
// CreateOrUpdateChannel creates a channel if it does not exist or updates a channel

pkg/fabric-txn/chclient/chclient.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/hyperledger/fabric-sdk-go/api/apitxn"
1717
"github.com/hyperledger/fabric-sdk-go/pkg/fabric-client/peer"
1818
"github.com/hyperledger/fabric-sdk-go/pkg/fabric-txn/internal"
19+
pb "github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/peer"
1920
)
2021

2122
const (
@@ -170,18 +171,20 @@ func sendTransaction(channel fab.Channel, txID apitxn.TransactionID, txProposalR
170171
}
171172
}
172173

173-
done, fail := internal.RegisterTxEvent(txID, eventHub)
174+
chcode := internal.RegisterTxEvent(txID, eventHub)
174175
_, err := internal.CreateAndSendTransaction(channel, txProposalResponses)
175176
if err != nil {
176177
notifier <- apitxn.ExecuteTxResponse{Response: apitxn.TransactionID{}, Error: fmt.Errorf("CreateAndSendTransaction returned error: %v", err)}
177178
return
178179
}
179180

180181
select {
181-
case <-done:
182-
notifier <- apitxn.ExecuteTxResponse{Response: txID, Error: nil}
183-
case err := <-fail:
184-
notifier <- apitxn.ExecuteTxResponse{Response: txID, Error: fmt.Errorf("ExecuteTx received an error from eventhub for txid(%s), error(%v)", txID, err)}
182+
case code := <-chcode:
183+
if code == pb.TxValidationCode_VALID {
184+
notifier <- apitxn.ExecuteTxResponse{Response: txID, TxValidationCode: code}
185+
} else {
186+
notifier <- apitxn.ExecuteTxResponse{Response: txID, TxValidationCode: code, Error: fmt.Errorf("ExecuteTx received a failed transaction response from eventhub for txid(%s), code(%s)", txID, code)}
187+
}
185188
case <-time.After(timeout):
186189
notifier <- apitxn.ExecuteTxResponse{Response: txID, Error: fmt.Errorf("ExecuteTx didn't receive block event for txid(%s)", txID)}
187190
}

pkg/fabric-txn/internal/common.go

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -64,21 +64,17 @@ func CreateAndSendTransaction(sender apitxn.Sender, resps []*apitxn.TransactionP
6464
}
6565

6666
// RegisterTxEvent registers on the given eventhub for the given transaction id
67-
// returns a boolean channel which receives true when the event is complete and
68-
// an error channel for errors
69-
func RegisterTxEvent(txID apitxn.TransactionID, eventHub fab.EventHub) (chan bool, chan error) {
70-
done := make(chan bool)
71-
fail := make(chan error)
72-
73-
eventHub.RegisterTxEvent(txID, func(txId string, errorCode pb.TxValidationCode, err error) {
74-
if err != nil {
75-
logger.Debugf("Received error event for txid(%s)\n", txId)
76-
fail <- err
77-
} else {
78-
logger.Debugf("Received success event for txid(%s)\n", txId)
79-
done <- true
80-
}
67+
// returns a TxValidationCode channel which receives the validation code when the
68+
// transaction completes. If the code is TxValidationCode_VALID then
69+
// the transaction committed successfully, otherwise the code indicates the error
70+
// that occurred.
71+
func RegisterTxEvent(txID apitxn.TransactionID, eventHub fab.EventHub) chan pb.TxValidationCode {
72+
chcode := make(chan pb.TxValidationCode)
73+
74+
eventHub.RegisterTxEvent(txID, func(txId string, code pb.TxValidationCode, err error) {
75+
logger.Debugf("Received code(%s) for txid(%s)\n", code, txId)
76+
chcode <- code
8177
})
8278

83-
return done, fail
79+
return chcode
8480
}

pkg/fabric-txn/transaction.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
fab "github.com/hyperledger/fabric-sdk-go/api/apifabclient"
1414
"github.com/hyperledger/fabric-sdk-go/api/apitxn"
1515
internal "github.com/hyperledger/fabric-sdk-go/pkg/fabric-txn/internal"
16+
"github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/peer"
1617

1718
"github.com/hyperledger/fabric-sdk-go/pkg/logging"
1819
)
@@ -68,22 +69,22 @@ func InvokeChaincode(client fab.FabricClient, channel fab.Channel, targets []api
6869
return apitxn.TransactionID{}, fmt.Errorf("CreateAndSendTransactionProposal returned error: %v", err)
6970
}
7071

71-
done, fail := internal.RegisterTxEvent(txID, eventHub)
72+
chcode := internal.RegisterTxEvent(txID, eventHub)
7273

7374
_, err = internal.CreateAndSendTransaction(channel, transactionProposalResponses)
7475
if err != nil {
7576
return txID, fmt.Errorf("CreateAndSendTransaction returned error: %v", err)
7677
}
7778

7879
select {
79-
case <-done:
80-
case err := <-fail:
81-
return txID, fmt.Errorf("invoke Error received from eventhub for txid(%s), error(%v)", txID, err)
80+
case code := <-chcode:
81+
if code == peer.TxValidationCode_VALID {
82+
return txID, nil
83+
}
84+
return txID, fmt.Errorf("invoke Error received from eventhub for txid(%s), code(%s)", txID, code)
8285
case <-time.After(time.Second * 30):
8386
return txID, fmt.Errorf("invoke Didn't receive block event for txid(%s)", txID)
8487
}
85-
86-
return txID, nil
8788
}
8889

8990
// checkCommonArgs ...

test/integration/channel_client_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/hyperledger/fabric-sdk-go/api/apitxn"
1515
"github.com/hyperledger/fabric-sdk-go/def/fabapi"
1616
"github.com/hyperledger/fabric-sdk-go/def/fabapi/opt"
17+
pb "github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/peer"
1718
)
1819

1920
var queryArgs = [][]byte{[]byte("query"), []byte("b")}
@@ -149,6 +150,9 @@ func testAsyncTransaction(ccID string, chClient apitxn.ChannelClient, t *testing
149150
if response.Error != nil {
150151
t.Fatalf("ExecuteTx returned error: %s", response.Error)
151152
}
153+
if response.TxValidationCode != pb.TxValidationCode_VALID {
154+
t.Fatalf("Expecting TxValidationCode to be TxValidationCode_VALID but received: %s", response.TxValidationCode)
155+
}
152156
case <-time.After(time.Second * 20):
153157
t.Fatalf("ExecuteTx timed out")
154158
}

0 commit comments

Comments
 (0)