Skip to content

Commit 59a04ef

Browse files
committed
[FAB-13171] Address comments: TxSubmitter
- Address comments for tx_submitter.go in https://gerrit.hyperledger.org/r/#/c/27227/ - Remove SubmitTransactionWithChan to simplify token client Change-Id: I1604f4475d7b604f3ba547fc2a5a6432a973c7af Signed-off-by: Wenjian Qiao <wenjianq@gmail.com>
1 parent 80cf017 commit 59a04ef

15 files changed

+784
-496
lines changed

integration/token/token_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
docker "github.com/fsouza/go-dockerclient"
1818
"github.com/golang/protobuf/proto"
1919
"github.com/hyperledger/fabric/integration/nwo"
20+
"github.com/hyperledger/fabric/protos/common"
2021
"github.com/hyperledger/fabric/protos/token"
2122
tokenclient "github.com/hyperledger/fabric/token/client"
2223
. "github.com/onsi/ginkgo"
@@ -139,11 +140,12 @@ func RunTokenTransactionSubmit(n *nwo.Network, orderer *nwo.Orderer, peer *nwo.P
139140
mockTokenTxBytes, err := proto.Marshal(mockTokenTx)
140141
Expect(err).NotTo(HaveOccurred())
141142

142-
_, txEnvelope, err := txSubmitter.CreateTxEnvelope(mockTokenTxBytes)
143+
txEnvelope, _, err := txSubmitter.CreateTxEnvelope(mockTokenTxBytes)
143144
Expect(err).NotTo(HaveOccurred())
144-
committed, _, err := txSubmitter.SubmitTransaction(txEnvelope, 60)
145+
ordererStatus, committed, err := txSubmitter.Submit(txEnvelope, 60*time.Second)
145146
Expect(err).NotTo(HaveOccurred())
146147
Expect(committed).To(Equal(true))
148+
Expect(*ordererStatus).To(Equal(common.Status_SUCCESS))
147149
}
148150

149151
// update configtx.yaml with V1_4_FABTOKEN_EXPERIMENTAL: true

token/client/client_suite_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"testing"
1010

1111
"github.com/golang/protobuf/proto"
12+
pb "github.com/hyperledger/fabric/protos/peer"
1213
. "github.com/onsi/ginkgo"
1314
. "github.com/onsi/gomega"
1415
)
@@ -24,3 +25,20 @@ func ProtoMarshal(m proto.Message) []byte {
2425

2526
return bytes
2627
}
28+
29+
func createFilteredBlock(channelId string, validationCode pb.TxValidationCode, txIDs ...string) *pb.FilteredBlock {
30+
var filteredTransactions []*pb.FilteredTransaction
31+
for _, txID := range txIDs {
32+
ft := &pb.FilteredTransaction{
33+
Txid: txID,
34+
TxValidationCode: validationCode,
35+
}
36+
filteredTransactions = append(filteredTransactions, ft)
37+
}
38+
fb := &pb.FilteredBlock{
39+
Number: 0,
40+
ChannelId: channelId,
41+
FilteredTransactions: filteredTransactions,
42+
}
43+
return fb
44+
}

token/client/deliver_client.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/hyperledger/fabric/protos/common"
1919
ab "github.com/hyperledger/fabric/protos/orderer"
2020
pb "github.com/hyperledger/fabric/protos/peer"
21+
tk "github.com/hyperledger/fabric/token"
2122
"github.com/pkg/errors"
2223
"google.golang.org/grpc"
2324
"google.golang.org/grpc/status"
@@ -100,7 +101,7 @@ func (d *deliverClient) Certificate() *tls.Certificate {
100101
}
101102

102103
// create a signed envelope with SeekPosition_Newest for block
103-
func CreateDeliverEnvelope(channelId string, creator []byte, signer SignerIdentity, cert *tls.Certificate) (*common.Envelope, error) {
104+
func CreateDeliverEnvelope(channelId string, creator []byte, signingIdentity tk.SigningIdentity, cert *tls.Certificate) (*common.Envelope, error) {
104105
var tlsCertHash []byte
105106
var err error
106107
// check for client certificate and compute SHA2-256 on certificate if present
@@ -143,7 +144,7 @@ func CreateDeliverEnvelope(channelId string, creator []byte, signer SignerIdenti
143144
return nil, errors.Wrap(err, "error marshaling SeekInfo")
144145
}
145146

146-
envelope, err := CreateEnvelope(raw, header, signer)
147+
envelope, err := CreateEnvelope(raw, header, signingIdentity)
147148
if err != nil {
148149
return nil, err
149150
}
@@ -160,7 +161,7 @@ func DeliverSend(df DeliverFiltered, address string, envelope *common.Envelope)
160161
return nil
161162
}
162163

163-
func DeliverReceive(df DeliverFiltered, address string, txid string, eventCh chan TxEvent) error {
164+
func DeliverReceive(df DeliverFiltered, address string, txid string, eventCh chan<- TxEvent) error {
164165
event := TxEvent{
165166
Txid: txid,
166167
Committed: false,
@@ -213,7 +214,7 @@ read:
213214
// DeliverWaitForResponse waits for either eventChan has value (i.e., response has been received) or ctx is timed out
214215
// This function assumes that the eventCh is only for the specified txid
215216
// If an eventCh is shared by multiple transactions, a loop should be used to listen to events from multiple transactions
216-
func DeliverWaitForResponse(ctx context.Context, eventCh chan TxEvent, txid string) (bool, error) {
217+
func DeliverWaitForResponse(ctx context.Context, eventCh <-chan TxEvent, txid string) (bool, error) {
217218
select {
218219
case event, _ := <-eventCh:
219220
if txid == event.Txid {

token/client/deliver_client_test.go

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,13 @@ import (
2121
"github.com/pkg/errors"
2222
)
2323

24-
var _ = Describe("OrdererClient", func() {
24+
//go:generate counterfeiter -o mock/deliver_server.go -fake-name DeliverServer . deliverServer
25+
26+
type deliverServer interface {
27+
pb.DeliverServer
28+
}
29+
30+
var _ = Describe("DeliverClient", func() {
2531
var (
2632
channelId string
2733
creator []byte
@@ -31,7 +37,7 @@ var _ = Describe("OrdererClient", func() {
3137
deliverResp *pb.DeliverResponse
3238
fakeTxid string
3339

34-
fakeSigner *mock.SignerIdentity
40+
fakeSigningIdentity *mock.SigningIdentity
3541
fakeDeliverFiltered *mock.DeliverFiltered
3642
)
3743

@@ -64,13 +70,13 @@ var _ = Describe("OrdererClient", func() {
6470
}
6571
expectedPayloadData = ProtoMarshal(seekInfo)
6672

67-
fakeSigner = &mock.SignerIdentity{}
68-
fakeSigner.SignReturns([]byte("envelop-signature"), nil)
73+
fakeSigningIdentity = &mock.SigningIdentity{}
74+
fakeSigningIdentity.SignReturns([]byte("envelop-signature"), nil)
6975

7076
fakeTxid = "test_txid_123"
7177
deliverResp = &pb.DeliverResponse{
7278
Type: &pb.DeliverResponse_FilteredBlock{
73-
FilteredBlock: createFilteredBlock(channelId, fakeTxid),
79+
FilteredBlock: createFilteredBlock(channelId, pb.TxValidationCode_VALID, fakeTxid),
7480
},
7581
}
7682
fakeDeliverFiltered = &mock.DeliverFiltered{}
@@ -81,7 +87,7 @@ var _ = Describe("OrdererClient", func() {
8187

8288
Describe("CreateDeliverEnvelope", func() {
8389
It("returns expected envelope", func() {
84-
envelope, err := client.CreateDeliverEnvelope(channelId, creator, fakeSigner, nil)
90+
envelope, err := client.CreateDeliverEnvelope(channelId, creator, fakeSigningIdentity, nil)
8591
Expect(err).NotTo(HaveOccurred())
8692

8793
payload := common.Payload{}
@@ -105,18 +111,18 @@ var _ = Describe("OrdererClient", func() {
105111
Expect(err).NotTo(HaveOccurred())
106112
Expect(signatureHeader.Creator).To(Equal(creator))
107113

108-
Expect(fakeSigner.SignCallCount()).To(Equal(1))
109-
raw := fakeSigner.SignArgsForCall(0)
114+
Expect(fakeSigningIdentity.SignCallCount()).To(Equal(1))
115+
raw := fakeSigningIdentity.SignArgsForCall(0)
110116
Expect(raw).To(Equal(envelope.Payload))
111117
})
112118

113-
Context("when SignerIdentity returns error", func() {
119+
Context("when SigningIdentity returns error", func() {
114120
BeforeEach(func() {
115-
fakeSigner.SignReturns(nil, errors.New("flying-pineapple"))
121+
fakeSigningIdentity.SignReturns(nil, errors.New("flying-pineapple"))
116122
})
117123

118124
It("returns an error", func() {
119-
_, err := client.CreateDeliverEnvelope(channelId, creator, fakeSigner, nil)
125+
_, err := client.CreateDeliverEnvelope(channelId, creator, fakeSigningIdentity, nil)
120126
Expect(err).To(MatchError("flying-pineapple"))
121127
})
122128
})

token/client/mock/ab_server.go

Lines changed: 181 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)