Skip to content

Commit 44c341c

Browse files
committed
[FAB-4590] Auto-vendor all deps properly
It was noted that the auto-vendoring mechanism introduced in CR 6991 was failing to vendor dependencies properly. Investigating, it was root-caused that we were only auto- vendoring transitive dependencies, not direct imports. This patch corrects this by merging the direct imports into our dependency tree. Fix was verified by adding the unit-test, verifying that it fails, and subsequently verifying that it passes after the fix was applied. Credit to Rahul Hegde <[email protected]> for finding the issue and proposing the proper fix that inspired this patch. Fixes FAB-4590 Change-Id: Ie0631ac1033c3f427ea42c43eccf76a0f7390320 Signed-off-by: Greg Haskins <[email protected]>
1 parent f5dbbaf commit 44c341c

File tree

5 files changed

+90
-4
lines changed

5 files changed

+90
-4
lines changed

core/chaincode/platforms/golang/platform.go

+15-4
Original file line numberDiff line numberDiff line change
@@ -239,18 +239,29 @@ func (goPlatform *Platform) GetDeploymentPayload(spec *pb.ChaincodeSpec) ([]byte
239239
})
240240

241241
// --------------------------------------------------------------------------------------
242-
// Assemble the fully resolved list of transitive dependencies from the imports that remain
242+
// Assemble the fully resolved list of direct and transitive dependencies based on the
243+
// imports that remain after filtering
243244
// --------------------------------------------------------------------------------------
244245
deps := make(map[string]bool)
245246

246247
for _, pkg := range imports {
247-
_deps, err := listDeps(env, pkg)
248+
// ------------------------------------------------------------------------------
249+
// Resolve direct import's transitives
250+
// ------------------------------------------------------------------------------
251+
transitives, err := listDeps(env, pkg)
248252
if err != nil {
249253
return nil, fmt.Errorf("Error obtaining dependencies for %s: %s", pkg, err)
250254
}
251255

252-
// Merge with our top list
253-
for _, dep := range _deps {
256+
// ------------------------------------------------------------------------------
257+
// Merge all results with our top list
258+
// ------------------------------------------------------------------------------
259+
260+
// Merge direct dependency...
261+
deps[pkg] = true
262+
263+
// .. and then all transitives
264+
for _, dep := range transitives {
254265
deps[dep] = true
255266
}
256267
}

core/chaincode/platforms/golang/platform_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ func TestGenerateDockerBuild(t *testing.T) {
260260
specs = append(specs, spec{CCName: "NoCode", Path: "path/to/nowhere", File: "/bin/warez", Mode: 0100400, SuccessExpected: false})
261261
specs = append(specs, spec{CCName: "invalidhttp", Path: "https://not/a/valid/path", File: "/src/github.com/hyperledger/fabric/examples/chaincode/go/map/map.go", Mode: 0100400, SuccessExpected: false, RealGen: true})
262262
specs = append(specs, spec{CCName: "map", Path: "github.com/hyperledger/fabric/examples/chaincode/go/map", File: "/src/github.com/hyperledger/fabric/examples/chaincode/go/map/map.go", Mode: 0100400, SuccessExpected: true, RealGen: true})
263+
specs = append(specs, spec{CCName: "AutoVendor", Path: "github.com/hyperledger/fabric/test/chaincodes/AutoVendor/chaincode", File: "/src/github.com/hyperledger/fabric/test/chaincodes/AutoVendor/chaincode/main.go", Mode: 0100400, SuccessExpected: true, RealGen: true})
263264
specs = append(specs, spec{CCName: "mapBadPath", Path: "github.com/hyperledger/fabric/examples/chaincode/go/map", File: "/src/github.com/hyperledger/fabric/examples/bad/path/to/map.go", Mode: 0100400, SuccessExpected: false})
264265
specs = append(specs, spec{CCName: "mapBadMode", Path: "github.com/hyperledger/fabric/examples/chaincode/go/map", File: "/src/github.com/hyperledger/fabric/examples/chaincode/go/map/map.go", Mode: 0100555, SuccessExpected: false})
265266

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright Greg Haskins All Rights Reserved
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* The purpose of this test code is to prove that the system properly packages
7+
* up dependencies. We therefore synthesize the scenario where a chaincode
8+
* imports non-standard dependencies both directly and indirectly and then
9+
* expect a unit-test to verify that the package includes everything needed
10+
* and ultimately builds properly.
11+
*
12+
*/
13+
14+
package main
15+
16+
import (
17+
"fmt"
18+
19+
"github.com/hyperledger/fabric/core/chaincode/shim"
20+
pb "github.com/hyperledger/fabric/protos/peer"
21+
"github.com/hyperledger/fabric/test/chaincodes/AutoVendor/directdep"
22+
)
23+
24+
// SimpleChaincode example simple Chaincode implementation
25+
type SimpleChaincode struct {
26+
}
27+
28+
func (t *SimpleChaincode) Init(stub shim.ChaincodeStubInterface) pb.Response {
29+
return shim.Error("NOT IMPL")
30+
}
31+
32+
func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
33+
return shim.Error("NOT IMPL")
34+
}
35+
36+
func main() {
37+
directdep.PointlessFunction()
38+
39+
err := shim.Start(new(SimpleChaincode))
40+
if err != nil {
41+
fmt.Printf("Error starting Simple chaincode: %s", err)
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* Copyright Greg Haskins All Rights Reserved
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* See github.com/hyperledger/fabric/test/chaincodes/AutoVendor/chaincode/main.go for details
7+
*/
8+
package directdep
9+
10+
import (
11+
"github.com/hyperledger/fabric/test/chaincodes/AutoVendor/indirectdep"
12+
)
13+
14+
func PointlessFunction() {
15+
// delegate to our indirect dependency
16+
indirectdep.PointlessFunction()
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
* Copyright Greg Haskins All Rights Reserved
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* See github.com/hyperledger/fabric/test/chaincodes/AutoVendor/chaincode/main.go for details
7+
*/
8+
package indirectdep
9+
10+
import "fmt"
11+
12+
func PointlessFunction() {
13+
fmt.Printf("Successfully invoked pointless function\n")
14+
}

0 commit comments

Comments
 (0)