|
| 1 | +/* |
| 2 | +Copyright IBM Corp. All Rights Reserved. |
| 3 | +
|
| 4 | +SPDX-License-Identifier: Apache-2.0 |
| 5 | +*/ |
| 6 | + |
| 7 | +package chaincode_test |
| 8 | + |
| 9 | +import ( |
| 10 | + "fmt" |
| 11 | + |
| 12 | + "github.com/hyperledger/fabric/core/chaincode" |
| 13 | + "github.com/hyperledger/fabric/core/chaincode/fake" |
| 14 | + "github.com/hyperledger/fabric/core/chaincode/mock" |
| 15 | + "github.com/hyperledger/fabric/core/common/ccprovider" |
| 16 | + pb "github.com/hyperledger/fabric/protos/peer" |
| 17 | + |
| 18 | + . "github.com/onsi/ginkgo" |
| 19 | + . "github.com/onsi/gomega" |
| 20 | +) |
| 21 | + |
| 22 | +var _ = Describe("ChaincodeSupport", func() { |
| 23 | + var ( |
| 24 | + chaincodeSupport *chaincode.ChaincodeSupport |
| 25 | + |
| 26 | + fakeApplicationConfigRetriever *fake.ApplicationConfigRetriever |
| 27 | + fakeApplicationConfig *mock.ApplicationConfig |
| 28 | + fakeApplicationCapabilities *mock.ApplicationCapabilities |
| 29 | + ) |
| 30 | + |
| 31 | + BeforeEach(func() { |
| 32 | + fakeApplicationCapabilities = &mock.ApplicationCapabilities{} |
| 33 | + fakeApplicationCapabilities.LifecycleV20Returns(true) |
| 34 | + |
| 35 | + fakeApplicationConfig = &mock.ApplicationConfig{} |
| 36 | + fakeApplicationConfig.CapabilitiesReturns(fakeApplicationCapabilities) |
| 37 | + |
| 38 | + fakeApplicationConfigRetriever = &fake.ApplicationConfigRetriever{} |
| 39 | + fakeApplicationConfigRetriever.GetApplicationConfigReturns(fakeApplicationConfig, true) |
| 40 | + |
| 41 | + chaincodeSupport = &chaincode.ChaincodeSupport{ |
| 42 | + AppConfig: fakeApplicationConfigRetriever, |
| 43 | + } |
| 44 | + }) |
| 45 | + |
| 46 | + Describe("CheckInit", func() { |
| 47 | + var ( |
| 48 | + txParams *ccprovider.TransactionParams |
| 49 | + cccid *ccprovider.CCContext |
| 50 | + input *pb.ChaincodeInput |
| 51 | + |
| 52 | + fakeSimulator *mock.TxSimulator |
| 53 | + ) |
| 54 | + |
| 55 | + BeforeEach(func() { |
| 56 | + fakeSimulator = &mock.TxSimulator{} |
| 57 | + fakeSimulator.GetStateReturns([]byte("old-cc-version"), nil) |
| 58 | + |
| 59 | + txParams = &ccprovider.TransactionParams{ |
| 60 | + ChannelID: "channel-id", |
| 61 | + TXSimulator: fakeSimulator, |
| 62 | + } |
| 63 | + |
| 64 | + cccid = &ccprovider.CCContext{ |
| 65 | + Name: "cc-name", |
| 66 | + Version: "cc-version", |
| 67 | + InitRequired: true, |
| 68 | + } |
| 69 | + |
| 70 | + input = &pb.ChaincodeInput{ |
| 71 | + Args: [][]byte{[]byte("init")}, |
| 72 | + } |
| 73 | + }) |
| 74 | + |
| 75 | + It("indicates that it is init", func() { |
| 76 | + isInit, err := chaincodeSupport.CheckInit(txParams, cccid, input) |
| 77 | + Expect(err).NotTo(HaveOccurred()) |
| 78 | + Expect(isInit).To(BeTrue()) |
| 79 | + |
| 80 | + Expect(fakeSimulator.GetStateCallCount()).To(Equal(1)) |
| 81 | + namespace, key := fakeSimulator.GetStateArgsForCall(0) |
| 82 | + Expect(namespace).To(Equal("cc-name")) |
| 83 | + Expect(key).To(Equal("\x00\x00initialized")) |
| 84 | + |
| 85 | + Expect(fakeSimulator.SetStateCallCount()).To(Equal(1)) |
| 86 | + namespace, key, value := fakeSimulator.SetStateArgsForCall(0) |
| 87 | + Expect(namespace).To(Equal("cc-name")) |
| 88 | + Expect(key).To(Equal("\x00\x00initialized")) |
| 89 | + Expect(value).To(Equal([]byte("cc-version"))) |
| 90 | + }) |
| 91 | + |
| 92 | + Context("when the version is not changed", func() { |
| 93 | + BeforeEach(func() { |
| 94 | + cccid.Version = "old-cc-version" |
| 95 | + }) |
| 96 | + |
| 97 | + It("returns an error", func() { |
| 98 | + _, err := chaincodeSupport.CheckInit(txParams, cccid, input) |
| 99 | + Expect(err).To(MatchError("chaincode 'cc-name' is already initialized but 'init' called")) |
| 100 | + }) |
| 101 | + |
| 102 | + Context("when the invocation is not 'init'", func() { |
| 103 | + BeforeEach(func() { |
| 104 | + input.Args = [][]byte{[]byte("my-func")} |
| 105 | + }) |
| 106 | + |
| 107 | + It("returns that it is not an init", func() { |
| 108 | + isInit, err := chaincodeSupport.CheckInit(txParams, cccid, input) |
| 109 | + Expect(err).NotTo(HaveOccurred()) |
| 110 | + Expect(isInit).To(BeFalse()) |
| 111 | + Expect(fakeSimulator.GetStateCallCount()).To(Equal(1)) |
| 112 | + Expect(fakeSimulator.SetStateCallCount()).To(Equal(0)) |
| 113 | + }) |
| 114 | + |
| 115 | + Context("when the invocation contains no arguments", func() { |
| 116 | + BeforeEach(func() { |
| 117 | + input.Args = nil |
| 118 | + }) |
| 119 | + |
| 120 | + It("returns that it is an init", func() { |
| 121 | + isInit, err := chaincodeSupport.CheckInit(txParams, cccid, input) |
| 122 | + Expect(err).NotTo(HaveOccurred()) |
| 123 | + Expect(isInit).To(BeFalse()) |
| 124 | + }) |
| 125 | + }) |
| 126 | + |
| 127 | + }) |
| 128 | + }) |
| 129 | + |
| 130 | + Context("when init is not required", func() { |
| 131 | + BeforeEach(func() { |
| 132 | + cccid.InitRequired = false |
| 133 | + }) |
| 134 | + |
| 135 | + It("returns that it is not init", func() { |
| 136 | + isInit, err := chaincodeSupport.CheckInit(txParams, cccid, input) |
| 137 | + Expect(err).NotTo(HaveOccurred()) |
| 138 | + Expect(isInit).To(BeFalse()) |
| 139 | + Expect(fakeSimulator.GetStateCallCount()).To(Equal(0)) |
| 140 | + Expect(fakeSimulator.SetStateCallCount()).To(Equal(0)) |
| 141 | + }) |
| 142 | + }) |
| 143 | + |
| 144 | + Context("when the new lifecycle is not enabled", func() { |
| 145 | + BeforeEach(func() { |
| 146 | + fakeApplicationCapabilities.LifecycleV20Returns(false) |
| 147 | + }) |
| 148 | + |
| 149 | + It("returns that it is not init", func() { |
| 150 | + isInit, err := chaincodeSupport.CheckInit(txParams, cccid, input) |
| 151 | + Expect(err).NotTo(HaveOccurred()) |
| 152 | + Expect(isInit).To(BeFalse()) |
| 153 | + Expect(fakeSimulator.GetStateCallCount()).To(Equal(0)) |
| 154 | + Expect(fakeSimulator.SetStateCallCount()).To(Equal(0)) |
| 155 | + }) |
| 156 | + }) |
| 157 | + |
| 158 | + Context("when the invocation is channel-less", func() { |
| 159 | + BeforeEach(func() { |
| 160 | + txParams.ChannelID = "" |
| 161 | + }) |
| 162 | + |
| 163 | + It("returns it is not an init", func() { |
| 164 | + isInit, err := chaincodeSupport.CheckInit(txParams, cccid, input) |
| 165 | + Expect(err).NotTo(HaveOccurred()) |
| 166 | + Expect(isInit).To(BeFalse()) |
| 167 | + Expect(fakeSimulator.GetStateCallCount()).To(Equal(0)) |
| 168 | + Expect(fakeSimulator.SetStateCallCount()).To(Equal(0)) |
| 169 | + }) |
| 170 | + }) |
| 171 | + |
| 172 | + Context("when the application config cannot be retrieved", func() { |
| 173 | + BeforeEach(func() { |
| 174 | + fakeApplicationConfigRetriever.GetApplicationConfigReturns(nil, false) |
| 175 | + }) |
| 176 | + |
| 177 | + It("returns an error", func() { |
| 178 | + _, err := chaincodeSupport.CheckInit(txParams, cccid, input) |
| 179 | + Expect(err).To(MatchError("could not retrieve application config for channel 'channel-id'")) |
| 180 | + }) |
| 181 | + }) |
| 182 | + |
| 183 | + Context("when the invocation is not 'init'", func() { |
| 184 | + BeforeEach(func() { |
| 185 | + input.Args = [][]byte{[]byte("my-func")} |
| 186 | + }) |
| 187 | + |
| 188 | + It("returns an error", func() { |
| 189 | + _, err := chaincodeSupport.CheckInit(txParams, cccid, input) |
| 190 | + Expect(err).To(MatchError("chaincode 'cc-name' has not been initialized for this version, must call 'init' first")) |
| 191 | + }) |
| 192 | + }) |
| 193 | + |
| 194 | + Context("when the txsimulator cannot get state", func() { |
| 195 | + BeforeEach(func() { |
| 196 | + fakeSimulator.GetStateReturns(nil, fmt.Errorf("get-state-error")) |
| 197 | + }) |
| 198 | + |
| 199 | + It("wraps and returns the error", func() { |
| 200 | + _, err := chaincodeSupport.CheckInit(txParams, cccid, input) |
| 201 | + Expect(err).To(MatchError("could not get 'initialized' key: get-state-error")) |
| 202 | + }) |
| 203 | + }) |
| 204 | + |
| 205 | + Context("when the txsimulator cannot set state", func() { |
| 206 | + BeforeEach(func() { |
| 207 | + fakeSimulator.SetStateReturns(fmt.Errorf("set-state-error")) |
| 208 | + }) |
| 209 | + |
| 210 | + It("wraps and returns the error", func() { |
| 211 | + _, err := chaincodeSupport.CheckInit(txParams, cccid, input) |
| 212 | + Expect(err).To(MatchError("could not set 'initialized' key: set-state-error")) |
| 213 | + }) |
| 214 | + }) |
| 215 | + }) |
| 216 | +}) |
0 commit comments