Skip to content

Commit 79d165d

Browse files
mbrandenburgerAkramBitar
authored andcommitted
fix(tokengen): inject the public params into the chaincode package
GeneratePackage matched the suffix github.com/LFDT-Panurus/panurus/token/tcc/params.go but the file holding the public parameters lives at token/services/network/fabric/tcc/params.go (the token/tcc layout is a leftover of the token-sdk move). The packager passes the absolute path of every file it packages, so the replacer never fired and `tokengen ... --cc` produced a package still carrying the placeholder `var Params = ``', i.e. a token chaincode without public parameters. The integration harness got this right, so the two definitions had silently drifted apart. Move the chaincode import path, the params file suffix and the params template into a single package in the root module, shared by tokengen and by the harness, and use it on both sides. Cover the replacer with a test that fails on the old suffix. Signed-off-by: Marcus Brandenburger <bur@zurich.ibm.com>
1 parent 411fbf1 commit 79d165d

6 files changed

Lines changed: 81 additions & 20 deletions

File tree

cmd/tokengen/cobra/pp/cc/cc.go

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ import (
1414
"strings"
1515
"text/template"
1616

17+
"github.com/LFDT-Panurus/panurus/token/services/network/fabric/tcc/ccpackage"
1718
"github.com/hyperledger-labs/fabric-smart-client/integration/nwo/fabric/packager"
19+
"github.com/hyperledger-labs/fabric-smart-client/integration/nwo/fabric/packager/replacer"
1820
"github.com/hyperledger-labs/fabric-smart-client/pkg/utils/errors"
1921
)
2022

@@ -33,21 +35,28 @@ func GeneratePackage(raw []byte, outputDir string) error {
3335
}
3436

3537
err = packager.New().PackageChaincode(
36-
"github.com/LFDT-Panurus/panurus/token/services/network/fabric/tcc/main",
38+
ccpackage.ChaincodePath,
3739
"golang",
3840
"tcc",
3941
filepath.Join(outputDir, "tcc.tar"),
40-
func(s string, s2 string) (string, []byte) {
41-
if strings.HasSuffix(s, "github.com/LFDT-Panurus/panurus/token/tcc/params.go") {
42-
return "", paramsFile.Bytes()
43-
}
44-
45-
return "", nil
46-
},
42+
paramsReplacer(paramsFile.Bytes()),
4743
)
4844
if err != nil {
4945
return errors.Wrap(err, "failed creating chaincode package")
5046
}
5147

5248
return nil
5349
}
50+
51+
// paramsReplacer returns the replacer that swaps the public parameters file of
52+
// the token chaincode with params while it is packaged. The packager passes the
53+
// absolute path of each file it packages.
54+
func paramsReplacer(params []byte) replacer.Func {
55+
return func(filePath string, fileName string) (string, []byte) {
56+
if strings.HasSuffix(filePath, ccpackage.ParamsFileSuffix) {
57+
return "", params
58+
}
59+
60+
return "", nil
61+
}
62+
}

cmd/tokengen/cobra/pp/cc/cc_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,27 @@ func TestGeneratePackage(t *testing.T) {
2020
assert.Error(t, err)
2121
})
2222
}
23+
24+
// TestParamsReplacer checks that the replacer passed to the chaincode packager
25+
// swaps the public parameters file, and only that file. The packager hands out
26+
// the absolute path of each file it packages.
27+
func TestParamsReplacer(t *testing.T) {
28+
params := []byte("package tcc\n\nvar Params = \"cGFyYW1z\"\n")
29+
replace := paramsReplacer(params)
30+
31+
t.Run("replaces_the_params_file", func(t *testing.T) {
32+
_, raw := replace(
33+
"/home/user/panurus/token/services/network/fabric/tcc/params.go",
34+
"src/token/services/network/fabric/tcc/params.go",
35+
)
36+
assert.Equal(t, params, raw)
37+
})
38+
39+
t.Run("keeps_every_other_file", func(t *testing.T) {
40+
_, raw := replace(
41+
"/home/user/panurus/token/services/network/fabric/tcc/tcc.go",
42+
"src/token/services/network/fabric/tcc/tcc.go",
43+
)
44+
assert.Nil(t, raw)
45+
})
46+
}

cmd/tokengen/cobra/pp/cc/params.go

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

77
package cc
88

9+
import "github.com/LFDT-Panurus/panurus/token/services/network/fabric/tcc/ccpackage"
10+
911
// DefaultParams defines the template for the public parameters burned into
1012
// the token chaincode.
11-
const DefaultParams = `
12-
package tcc
13-
14-
var Params = "{{ Params }}"
15-
`
13+
const DefaultParams = ccpackage.ParamsTemplate

integration/nwo/token/fabric/cc/params.go

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

77
package cc
88

9+
import "github.com/LFDT-Panurus/panurus/token/services/network/fabric/tcc/ccpackage"
10+
911
// DefaultParams defines the template for the public parameters burned into
1012
// the token chaincode.
11-
const DefaultParams = `
12-
package tcc
13-
14-
var Params = "{{ Params }}"
15-
`
13+
const DefaultParams = ccpackage.ParamsTemplate

integration/nwo/token/fabric/cc/tcc.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/LFDT-Panurus/panurus/integration/nwo/token/fabric"
2222
topology3 "github.com/LFDT-Panurus/panurus/integration/nwo/token/topology"
2323
"github.com/LFDT-Panurus/panurus/token/services/logging"
24+
"github.com/LFDT-Panurus/panurus/token/services/network/fabric/tcc/ccpackage"
2425
"github.com/hyperledger-labs/fabric-smart-client/integration/nwo/fabric/packager"
2526
"github.com/hyperledger-labs/fabric-smart-client/integration/nwo/fabric/topology"
2627
"github.com/onsi/gomega"
@@ -29,8 +30,10 @@ import (
2930
const (
3031
// DefaultTokenChaincode is the default path to the token chaincode.
3132
// #nosec G101 no passwords here
32-
DefaultTokenChaincode = "github.com/LFDT-Panurus/panurus/token/services/network/fabric/tcc/main"
33-
DefaultTokenChaincodeParamsReplaceSuffix = "/token/services/network/fabric/tcc/params.go"
33+
DefaultTokenChaincode = ccpackage.ChaincodePath
34+
// DefaultTokenChaincodeParamsReplaceSuffix is the path suffix of the file
35+
// holding the public parameters of the token chaincode.
36+
DefaultTokenChaincodeParamsReplaceSuffix = ccpackage.ParamsFileSuffix
3437
)
3538

3639
var logger = logging.MustGetLogger()
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
Copyright IBM Corp. All Rights Reserved.
3+
4+
SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
// Package ccpackage describes how the public parameters are baked into the
8+
// token chaincode package. Everything that builds such a package (tokengen and
9+
// the integration test harness) shares these definitions, so that a move of the
10+
// chaincode sources cannot silently break one of them.
11+
package ccpackage
12+
13+
const (
14+
// ChaincodePath is the import path of the token chaincode.
15+
ChaincodePath = "github.com/LFDT-Panurus/panurus/token/services/network/fabric/tcc/main"
16+
17+
// ParamsFileSuffix is the path suffix of the source file holding the public
18+
// parameters. The packager passes the absolute path of every file it
19+
// packages, so this must match the tail of the path of tcc/params.go.
20+
ParamsFileSuffix = "/token/services/network/fabric/tcc/params.go"
21+
22+
// ParamsTemplate is the source of the file that replaces the file matched by
23+
// ParamsFileSuffix. It must stay in sync with tcc/params.go.
24+
ParamsTemplate = `
25+
package tcc
26+
27+
var Params = "{{ Params }}"
28+
`
29+
)

0 commit comments

Comments
 (0)