Skip to content

Commit 79739ca

Browse files
Upgrade chaincode (#401)
Signed-off-by: sapthasurendran <saptha.surendran@ibm.com> Co-authored-by: Angelo De Caro <adc@zurich.ibm.com>
1 parent 0a04f4b commit 79739ca

File tree

31 files changed

+403
-179
lines changed

31 files changed

+403
-179
lines changed

Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ include $(TOP)/checks.mk
3131

3232
.PHONY: unit-tests
3333
unit-tests:
34-
34+
3535
@export FAB_BINS=$(FAB_BINS); go test -cover $(shell go list ./... | grep -v '/integration/')
3636
cd integration/nwo/; go test -cover ./...
3737

@@ -81,15 +81,15 @@ orion-server-images:
8181
docker image tag orionbcdb/orion-server:$(ORION_VERSION) orionbcdb/orion-server:latest
8282

8383
INTEGRATION_TARGETS = integration-tests-iou
84-
INTEGRATION_TARGETS += integration-tests-atsacc
84+
INTEGRATION_TARGETS += integration-tests-atsacc
8585
INTEGRATION_TARGETS += integration-tests-chaincode-events
8686
INTEGRATION_TARGETS += integration-tests-atsafsc
8787
INTEGRATION_TARGETS += integration-tests-twonets
8888
INTEGRATION_TARGETS += integration-tests-pingpong
8989
INTEGRATION_TARGETS += integration-tests-stoprestart
9090

9191
.PHONY: integration-tests
92-
integration-tests: $(INTEGRATION_TARGETS)
92+
integration-tests: $(INTEGRATION_TARGETS)
9393

9494
.PHONY: integration-tests-iou
9595
integration-tests-iou:

integration/fabric/events/chaincode/chaincode/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ import (
1414
func main() {
1515
eventsChaincode, err := contractapi.NewChaincode(&chaincode.SmartContract{})
1616
if err != nil {
17-
log.Panicf("Error creating asset-transfer-events chaincode: %v", err)
17+
log.Panicf("Error creating events chaincode: %v", err)
1818
}
1919

2020
if err := eventsChaincode.Start(); err != nil {
21-
log.Panicf("Error starting asset-transfer-events chaincode: %v", err)
21+
log.Panicf("Error starting events chaincode: %v", err)
2222
}
2323
}

integration/fabric/events/chaincode/events_test.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/hyperledger-labs/fabric-smart-client/integration"
1313
"github.com/hyperledger-labs/fabric-smart-client/integration/fabric/events/chaincode"
1414
"github.com/hyperledger-labs/fabric-smart-client/integration/fabric/events/chaincode/views"
15+
"github.com/hyperledger-labs/fabric-smart-client/integration/nwo/fabric"
1516
. "github.com/onsi/ginkgo/v2"
1617
. "github.com/onsi/gomega"
1718
)
@@ -26,7 +27,7 @@ var _ = Describe("EndToEnd", func() {
2627
ii.Stop()
2728
})
2829

29-
Describe("Asset Transfer Events (With Chaincode)", func() {
30+
Describe("Events (With Chaincode)", func() {
3031
var (
3132
alice *chaincode.Client
3233
bob *chaincode.Client
@@ -80,5 +81,26 @@ var _ = Describe("EndToEnd", func() {
8081
Expect(payloadsReceived).To(Equal(expectedEventPayloads))
8182

8283
})
84+
85+
It("Upgrade Chaincode", func() {
86+
// Old chaincode
87+
event, err := alice.EventsView("CreateAsset", "CreateAsset")
88+
Expect(err).ToNot(HaveOccurred())
89+
eventReceived := &views.EventReceived{}
90+
json.Unmarshal(event.([]byte), eventReceived)
91+
Expect(string(eventReceived.Event.Payload)).To(Equal("Invoked Create Asset Successfully"))
92+
93+
// Update
94+
fabricNetwork := fabric.Network(ii.Ctx, "default")
95+
Expect(fabricNetwork).ToNot(BeNil(), "failed to find fabric network 'default'")
96+
fabricNetwork.UpdateChaincode("events", "Version-1.0", "github.com/hyperledger-labs/fabric-smart-client/integration/fabric/events/chaincode/newChaincode", "")
97+
98+
// New chaincode
99+
event, err = alice.EventsView("CreateAsset", "CreateAsset")
100+
Expect(err).ToNot(HaveOccurred())
101+
eventReceived = &views.EventReceived{}
102+
json.Unmarshal(event.([]byte), eventReceived)
103+
Expect(string(eventReceived.Event.Payload)).To(Equal("Invoked Create Asset Successfully From Upgraded Chaincode"))
104+
})
83105
})
84106
})
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
SPDX-License-Identifier: Apache-2.0
3+
*/
4+
5+
package chaincode
6+
7+
import (
8+
"fmt"
9+
10+
"github.com/hyperledger/fabric-contract-api-go/contractapi"
11+
)
12+
13+
// SmartContract provides functions for Event Listening
14+
type SmartContract struct {
15+
contractapi.Contract
16+
}
17+
18+
func (s *SmartContract) InitLedger(ctx contractapi.TransactionContextInterface) {
19+
fmt.Println("Init Function Invoked")
20+
}
21+
22+
func (s *SmartContract) CreateAsset(ctx contractapi.TransactionContextInterface) {
23+
ctx.GetStub().SetEvent("CreateAsset", []byte("Invoked Create Asset Successfully From Upgraded Chaincode"))
24+
25+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
SPDX-License-Identifier: Apache-2.0
3+
*/
4+
5+
package main
6+
7+
import (
8+
"log"
9+
10+
chaincode "github.com/hyperledger-labs/fabric-smart-client/integration/fabric/events/chaincode/newChaincode/events"
11+
"github.com/hyperledger/fabric-contract-api-go/contractapi"
12+
)
13+
14+
func main() {
15+
eventsChaincode, err := contractapi.NewChaincode(&chaincode.SmartContract{})
16+
if err != nil {
17+
log.Panicf("Error creating events chaincode: %v", err)
18+
}
19+
20+
if err := eventsChaincode.Start(); err != nil {
21+
log.Panicf("Error starting events chaincode: %v", err)
22+
}
23+
}

integration/fabric/iou/commands.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ import (
1313
. "github.com/onsi/gomega"
1414
)
1515

16-
func CreateIOU(ii *integration.Infrastructure, identityLabel string, amount uint) string {
16+
func CreateIOU(ii *integration.Infrastructure, identityLabel string, amount uint, approver string) string {
1717
res, err := ii.Client("borrower").CallView(
1818
"create", common.JSONMarshall(&views.Create{
1919
Amount: amount,
2020
Identity: identityLabel,
2121
Lender: ii.Identity("lender"),
22-
Approver: ii.Identity("approver"),
22+
Approver: ii.Identity(approver),
2323
}),
2424
)
2525
Expect(err).NotTo(HaveOccurred())
@@ -33,15 +33,20 @@ func CheckState(ii *integration.Infrastructure, partyID, iouStateID string, expe
3333
Expect(common.JSONUnmarshalInt(res)).To(BeEquivalentTo(expected))
3434
}
3535

36-
func UpdateIOU(ii *integration.Infrastructure, iouStateID string, amount uint) {
36+
func UpdateIOU(ii *integration.Infrastructure, iouStateID string, amount uint, approver string) {
3737
txIDBoxed, err := ii.Client("borrower").CallView("update",
3838
common.JSONMarshall(&views.Update{
3939
LinearID: iouStateID,
4040
Amount: amount,
41-
Approver: ii.Identity("approver"),
41+
Approver: ii.Identity(approver),
4242
}),
4343
)
4444
Expect(err).NotTo(HaveOccurred())
4545
txID := common.JSONUnmarshalString(txIDBoxed)
4646
Expect(ii.Client("lender").IsTxFinal(txID)).NotTo(HaveOccurred())
4747
}
48+
49+
func InitApprover(ii *integration.Infrastructure, approver string) {
50+
_, err := ii.Client(approver).CallView("init", nil)
51+
Expect(err).NotTo(HaveOccurred())
52+
}

integration/fabric/iou/iou_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,12 @@ var _ = Describe("EndToEnd", func() {
3939
})
4040

4141
It("succeeded", func() {
42-
iouState := iou.CreateIOU(ii, "", 10)
42+
iou.InitApprover(ii, "approver1")
43+
iou.InitApprover(ii, "approver2")
44+
iouState := iou.CreateIOU(ii, "", 10, "approver1")
4345
iou.CheckState(ii, "borrower", iouState, 10)
4446
iou.CheckState(ii, "lender", iouState, 10)
45-
iou.UpdateIOU(ii, iouState, 5)
47+
iou.UpdateIOU(ii, iouState, 5, "approver2")
4648
iou.CheckState(ii, "borrower", iouState, 5)
4749
iou.CheckState(ii, "lender", iouState, 5)
4850
})

integration/fabric/iou/topology.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,26 @@ func Topology() []api.Topology {
2727
fscTopology := fsc.NewTopology()
2828

2929
// Add the approver FSC node.
30-
approver := fscTopology.AddNodeByName("approver")
30+
approver1 := fscTopology.AddNodeByName("approver1")
3131
// This option equips the approver's FSC node with an identity belonging to Org1.
3232
// Therefore, the approver is an endorser of the Fabric namespace we defined above.
33-
approver.AddOptions(
33+
approver1.AddOptions(
3434
fabric.WithOrganization("Org1"),
3535
)
36-
approver.RegisterResponder(&views.ApproverView{}, &views.CreateIOUView{})
37-
approver.RegisterResponder(&views.ApproverView{}, &views.UpdateIOUView{})
36+
approver1.RegisterResponder(&views.ApproverView{}, &views.CreateIOUView{})
37+
approver1.RegisterResponder(&views.ApproverView{}, &views.UpdateIOUView{})
38+
approver1.RegisterViewFactory("init", &views.ApproverInitViewFactory{})
39+
40+
// Add another approver as well
41+
approver2 := fscTopology.AddNodeByName("approver2")
42+
// This option equips the approver's FSC node with an identity belonging to Org1.
43+
// Therefore, the approver is an endorser of the Fabric namespace we defined above.
44+
approver2.AddOptions(
45+
fabric.WithOrganization("Org1"),
46+
)
47+
approver2.RegisterResponder(&views.ApproverView{}, &views.CreateIOUView{})
48+
approver2.RegisterResponder(&views.ApproverView{}, &views.UpdateIOUView{})
49+
approver2.RegisterViewFactory("init", &views.ApproverInitViewFactory{})
3850

3951
// Add the borrower's FSC node
4052
borrower := fscTopology.AddNodeByName("borrower")

integration/fabric/iou/views/approver.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,17 @@ func (i *ApproverView) Call(context view.Context) (interface{}, error) {
8888
wg.Wait()
8989
return nil, nil
9090
}
91+
92+
type ApproverInitView struct{}
93+
94+
func (a *ApproverInitView) Call(context view.Context) (interface{}, error) {
95+
assert.NoError(fabric.GetDefaultChannel(context).Committer().ProcessNamespace("iou"), "failed to setup namespace to process")
96+
return nil, nil
97+
}
98+
99+
type ApproverInitViewFactory struct{}
100+
101+
func (c *ApproverInitViewFactory) NewView(in []byte) (view.View, error) {
102+
f := &ApproverInitView{}
103+
return f, nil
104+
}

integration/fabric/iouhsm/iou_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,17 @@ var _ = Describe("EndToEnd", func() {
3939
})
4040

4141
It("succeeded", func() {
42-
iouState := iou.CreateIOU(ii, "", 10)
42+
iouState := iou.CreateIOU(ii, "", 10, "approver")
4343
iou.CheckState(ii, "borrower", iouState, 10)
4444
iou.CheckState(ii, "lender", iouState, 10)
45-
iou.UpdateIOU(ii, iouState, 5)
45+
iou.UpdateIOU(ii, iouState, 5, "approver")
4646
iou.CheckState(ii, "borrower", iouState, 5)
4747
iou.CheckState(ii, "lender", iouState, 5)
4848

49-
iouState = iou.CreateIOU(ii, "borrower-hsm-2", 10)
49+
iouState = iou.CreateIOU(ii, "borrower-hsm-2", 10, "approver")
5050
iou.CheckState(ii, "borrower", iouState, 10)
5151
iou.CheckState(ii, "lender", iouState, 10)
52-
iou.UpdateIOU(ii, iouState, 5)
52+
iou.UpdateIOU(ii, iouState, 5, "approver")
5353
iou.CheckState(ii, "borrower", iouState, 5)
5454
iou.CheckState(ii, "lender", iouState, 5)
5555
})

0 commit comments

Comments
 (0)