Skip to content

Commit 5e821ff

Browse files
committed
[FAB-12399] e2e test: list tokens
- write test for e2e list tokens Change-Id: I98e67b4cc551cc3dd7e2abea69be55be7b357f68 Signed-off-by: Angelo De Caro <adc@zurich.ibm.com> Signed-off-by: Kaoutar Elkhiyaoui <kao@zurich.ibm.com>
1 parent 953ffd8 commit 5e821ff

File tree

3 files changed

+121
-17
lines changed

3 files changed

+121
-17
lines changed

integration/token/token_test.go

Lines changed: 97 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@ import (
1414
"syscall"
1515
"time"
1616

17+
"github.com/hyperledger/fabric/bccsp/sw"
18+
"github.com/pkg/errors"
19+
1720
docker "github.com/fsouza/go-dockerclient"
1821
"github.com/golang/protobuf/proto"
1922
"github.com/hyperledger/fabric/integration/nwo"
20-
mspmgmt "github.com/hyperledger/fabric/msp/mgmt"
21-
peercommon "github.com/hyperledger/fabric/peer/common"
23+
"github.com/hyperledger/fabric/msp"
2224
"github.com/hyperledger/fabric/protos/common"
2325
"github.com/hyperledger/fabric/protos/token"
2426
tk "github.com/hyperledger/fabric/token"
@@ -37,6 +39,7 @@ var _ = Describe("Token EndToEnd", func() {
3739

3840
tokensToIssue []*token.TokenToIssue
3941
expectedTokenTransaction *token.TokenTransaction
42+
expectedUnspentTokens *token.UnspentTokens
4043
)
4144

4245
BeforeEach(func() {
@@ -60,6 +63,16 @@ var _ = Describe("Token EndToEnd", func() {
6063
},
6164
}
6265

66+
expectedUnspentTokens = &token.UnspentTokens{
67+
Tokens: []*token.TokenOutput{
68+
{
69+
Quantity: 119,
70+
Type: "ABC123",
71+
Id: []byte("ledger-id"),
72+
},
73+
},
74+
}
75+
6376
var err error
6477
testDir, err = ioutil.TempDir("", "token-e2e")
6578
Expect(err).NotTo(HaveOccurred())
@@ -94,6 +107,13 @@ var _ = Describe("Token EndToEnd", func() {
94107
client, err = docker.NewClientFromEnv()
95108
Expect(err).NotTo(HaveOccurred())
96109

110+
peer := network.Peer("Org1", "peer1")
111+
// Get recipient's identity
112+
recipient, err := getIdentity(network, peer, "User2", "Org1MSP")
113+
Expect(err).NotTo(HaveOccurred())
114+
tokensToIssue[0].Recipient = recipient
115+
expectedTokenTransaction.GetPlainAction().GetPlainImport().Outputs[0].Owner = recipient
116+
97117
networkRunner := network.NetworkGroupRunner()
98118
process = ifrit.Invoke(networkRunner)
99119
Eventually(process.Ready()).Should(BeClosed())
@@ -118,6 +138,14 @@ var _ = Describe("Token EndToEnd", func() {
118138

119139
By("issuing tokens")
120140
RunIssueRequest(tClient, tokensToIssue, expectedTokenTransaction)
141+
142+
By("list tokens")
143+
config = getClientConfig(network, peer, orderer, "testchannel", "User2", "Org1MSP")
144+
signingIdentity, err = getSigningIdentity(config.MSPInfo.MSPConfigPath, config.MSPInfo.MSPID, config.MSPInfo.MSPType)
145+
Expect(err).NotTo(HaveOccurred())
146+
tClient, err = tokenclient.NewClient(*config, signingIdentity)
147+
Expect(err).NotTo(HaveOccurred())
148+
RunListTokens(tClient, expectedUnspentTokens)
121149
})
122150
})
123151
})
@@ -140,6 +168,19 @@ func RunIssueRequest(c *tokenclient.Client, tokens []*token.TokenToIssue, expect
140168
Expect(tokenTxid).To(Equal(txid))
141169
}
142170

171+
func RunListTokens(c *tokenclient.Client, expectedUnspentTokens *token.UnspentTokens) []*token.TokenOutput {
172+
tokenOutputs, err := c.ListTokens()
173+
Expect(err).NotTo(HaveOccurred())
174+
175+
// unmarshall CommandResponse and verify the result
176+
Expect(len(tokenOutputs)).To(Equal(len(expectedUnspentTokens.Tokens)))
177+
for i, token := range expectedUnspentTokens.Tokens {
178+
Expect(tokenOutputs[i].Type).To(Equal(token.Type))
179+
Expect(tokenOutputs[i].Quantity).To(Equal(token.Quantity))
180+
}
181+
return tokenOutputs
182+
}
183+
143184
func getClientConfig(n *nwo.Network, peer *nwo.Peer, orderer *nwo.Orderer, channelId, user, mspID string) *tokenclient.ClientConfig {
144185
mspDir := n.PeerUserMSPDir(peer, user)
145186
peerAddr := n.PeerAddress(peer, nwo.ListenPort)
@@ -174,12 +215,39 @@ func getClientConfig(n *nwo.Network, peer *nwo.Peer, orderer *nwo.Orderer, chann
174215
return &config
175216
}
176217

218+
func getIdentity(n *nwo.Network, peer *nwo.Peer, user, mspId string) ([]byte, error) {
219+
orderer := n.Orderer("orderer")
220+
config := getClientConfig(n, peer, orderer, "testchannel", user, mspId)
221+
222+
localMSP, err := LoadLocalMSPAt(config.MSPInfo.MSPConfigPath, config.MSPInfo.MSPID, config.MSPInfo.MSPType)
223+
if err != nil {
224+
return nil, err
225+
}
226+
227+
signer, err := localMSP.GetDefaultSigningIdentity()
228+
if err != nil {
229+
return nil, err
230+
}
231+
232+
creator, err := signer.Serialize()
233+
if err != nil {
234+
return nil, err
235+
}
236+
237+
return creator, nil
238+
}
239+
177240
func getSigningIdentity(mspConfigPath, mspID, mspType string) (tk.SigningIdentity, error) {
178-
peercommon.InitCrypto(mspConfigPath, mspID, mspType)
179-
signingIdentity, err := mspmgmt.GetLocalMSP().GetDefaultSigningIdentity()
241+
mspInstance, err := LoadLocalMSPAt(mspConfigPath, mspID, mspType)
180242
if err != nil {
181243
return nil, err
182244
}
245+
246+
signingIdentity, err := mspInstance.GetDefaultSigningIdentity()
247+
if err != nil {
248+
return nil, err
249+
}
250+
183251
return signingIdentity, nil
184252
}
185253

@@ -195,3 +263,28 @@ func updateConfigtx(network *nwo.Network) error {
195263
output := bytes.Replace(input, []byte("CAPABILITY_PLACEHOLDER: false"), []byte("V1_4_FABTOKEN_EXPERIMENTAL: true"), -1)
196264
return ioutil.WriteFile(filepath, output, 0644)
197265
}
266+
267+
// LoadLocalMSPAt loads an MSP whose configuration is stored at 'dir', and whose
268+
// id and type are the passed as arguments.
269+
func LoadLocalMSPAt(dir, id, mspType string) (msp.MSP, error) {
270+
if mspType != "bccsp" {
271+
return nil, errors.Errorf("invalid msp type, expected 'bccsp', got %s", mspType)
272+
}
273+
conf, err := msp.GetLocalMspConfig(dir, nil, id)
274+
if err != nil {
275+
return nil, err
276+
}
277+
ks, err := sw.NewFileBasedKeyStore(nil, filepath.Join(dir, "keystore"), true)
278+
if err != nil {
279+
return nil, err
280+
}
281+
thisMSP, err := msp.NewBccspMspWithKeyStore(msp.MSPv1_0, ks)
282+
if err != nil {
283+
return nil, err
284+
}
285+
err = thisMSP.Setup(conf)
286+
if err != nil {
287+
return nil, err
288+
}
289+
return thisMSP, nil
290+
}

msp/msp_test.go

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1196,13 +1196,10 @@ func getLocalMSPWithVersionAndError(t *testing.T, dir string, version MSPVersion
11961196
conf, err := GetLocalMspConfig(dir, nil, "SampleOrg")
11971197
assert.NoError(t, err)
11981198

1199-
thisMSP, err := newBccspMsp(version)
1200-
assert.NoError(t, err)
12011199
ks, err := sw.NewFileBasedKeyStore(nil, filepath.Join(dir, "keystore"), true)
12021200
assert.NoError(t, err)
1203-
csp, err := sw.NewWithParams(256, "SHA2", ks)
1201+
thisMSP, err := NewBccspMspWithKeyStore(version, ks)
12041202
assert.NoError(t, err)
1205-
thisMSP.(*bccspmsp).bccsp = csp
12061203

12071204
return thisMSP, thisMSP.Setup(conf)
12081205
}
@@ -1211,14 +1208,10 @@ func getLocalMSP(t *testing.T, dir string) MSP {
12111208
conf, err := GetLocalMspConfig(dir, nil, "SampleOrg")
12121209
assert.NoError(t, err)
12131210

1214-
thisMSP, err := newBccspMsp(MSPv1_0)
1215-
assert.NoError(t, err)
12161211
ks, err := sw.NewFileBasedKeyStore(nil, filepath.Join(dir, "keystore"), true)
12171212
assert.NoError(t, err)
1218-
csp, err := sw.NewWithParams(256, "SHA2", ks)
1219-
assert.NoError(t, err)
1220-
thisMSP.(*bccspmsp).bccsp = csp
12211213

1214+
thisMSP, err := NewBccspMspWithKeyStore(MSPv1_0, ks)
12221215
err = thisMSP.Setup(conf)
12231216
assert.NoError(t, err)
12241217

@@ -1229,13 +1222,10 @@ func getLocalMSPWithVersion(t *testing.T, dir string, version MSPVersion) MSP {
12291222
conf, err := GetLocalMspConfig(dir, nil, "SampleOrg")
12301223
assert.NoError(t, err)
12311224

1232-
thisMSP, err := newBccspMsp(version)
1233-
assert.NoError(t, err)
12341225
ks, err := sw.NewFileBasedKeyStore(nil, filepath.Join(dir, "keystore"), true)
12351226
assert.NoError(t, err)
1236-
csp, err := sw.NewWithParams(256, "SHA2", ks)
1227+
thisMSP, err := NewBccspMspWithKeyStore(version, ks)
12371228
assert.NoError(t, err)
1238-
thisMSP.(*bccspmsp).bccsp = csp
12391229

12401230
err = thisMSP.Setup(conf)
12411231
assert.NoError(t, err)

msp/mspimpl.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/hyperledger/fabric/bccsp"
1919
"github.com/hyperledger/fabric/bccsp/factory"
2020
"github.com/hyperledger/fabric/bccsp/signer"
21+
"github.com/hyperledger/fabric/bccsp/sw"
2122
m "github.com/hyperledger/fabric/protos/msp"
2223
"github.com/pkg/errors"
2324
)
@@ -127,6 +128,26 @@ func newBccspMsp(version MSPVersion) (MSP, error) {
127128
return theMsp, nil
128129
}
129130

131+
// NewBccspMspWithKeyStore allows to create a BCCSP-based MSP whose underlying
132+
// crypto material is available through the passed keystore
133+
func NewBccspMspWithKeyStore(version MSPVersion, keyStore bccsp.KeyStore) (MSP, error) {
134+
thisMSP, err := newBccspMsp(version)
135+
if err != nil {
136+
return nil, err
137+
}
138+
139+
csp, err := sw.NewWithParams(
140+
factory.GetDefaultOpts().SwOpts.SecLevel,
141+
factory.GetDefaultOpts().SwOpts.HashFamily,
142+
keyStore)
143+
if err != nil {
144+
return nil, err
145+
}
146+
thisMSP.(*bccspmsp).bccsp = csp
147+
148+
return thisMSP, nil
149+
}
150+
130151
func (msp *bccspmsp) getCertFromPem(idBytes []byte) (*x509.Certificate, error) {
131152
if idBytes == nil {
132153
return nil, errors.New("getCertFromPem error: nil idBytes")

0 commit comments

Comments
 (0)