github.com/kchristidis/fabric@v1.0.4-0.20171028114726-837acd08cde1/core/chaincode/systemchaincode_test.go (about) 1 /* 2 Copyright IBM Corp. 2016 All Rights Reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package chaincode 18 19 import ( 20 "net" 21 "os" 22 "testing" 23 "time" 24 25 "github.com/hyperledger/fabric/common/util" 26 "github.com/hyperledger/fabric/core/common/ccprovider" 27 "github.com/hyperledger/fabric/core/peer" 28 "github.com/hyperledger/fabric/core/scc" 29 "github.com/hyperledger/fabric/core/scc/samplesyscc" 30 pb "github.com/hyperledger/fabric/protos/peer" 31 "github.com/spf13/viper" 32 "golang.org/x/net/context" 33 "google.golang.org/grpc" 34 ) 35 36 type oldSysCCInfo struct { 37 origSystemCC []*scc.SystemChaincode 38 origSysCCWhitelist map[string]string 39 } 40 41 func (osyscc *oldSysCCInfo) reset() { 42 scc.MockResetSysCCs(osyscc.origSystemCC) 43 viper.Set("chaincode.system", osyscc.origSysCCWhitelist) 44 } 45 46 func initSysCCTests() (*oldSysCCInfo, net.Listener, error) { 47 var opts []grpc.ServerOption 48 grpcServer := grpc.NewServer(opts...) 49 viper.Set("peer.fileSystemPath", "/tmp/hyperledger/test/tmpdb") 50 defer os.RemoveAll("/tmp/hyperledger/test/tmpdb") 51 52 peer.MockInitialize() 53 54 mspGetter := func(cid string) []string { 55 return []string{"DEFAULT"} 56 } 57 58 peer.MockSetMSPIDGetter(mspGetter) 59 60 //use a different address than what we usually use for "peer" 61 //we override the peerAddress set in chaincode_support.go 62 // FIXME: Use peer.GetLocalAddress() 63 peerAddress := "0.0.0.0:21726" 64 lis, err := net.Listen("tcp", peerAddress) 65 if err != nil { 66 return nil, nil, err 67 } 68 69 getPeerEndpoint := func() (*pb.PeerEndpoint, error) { 70 return &pb.PeerEndpoint{Id: &pb.PeerID{Name: "testpeer"}, Address: peerAddress}, nil 71 } 72 73 ccStartupTimeout := time.Duration(5000) * time.Millisecond 74 pb.RegisterChaincodeSupportServer(grpcServer, NewChaincodeSupport(getPeerEndpoint, false, ccStartupTimeout)) 75 76 go grpcServer.Serve(lis) 77 78 //set systemChaincodes to sample 79 sysccs := []*scc.SystemChaincode{ 80 { 81 Enabled: true, 82 Name: "sample_syscc", 83 Path: "github.com/hyperledger/fabric/core/scc/samplesyscc", 84 InitArgs: [][]byte{}, 85 Chaincode: &samplesyscc.SampleSysCC{}, 86 }, 87 } 88 89 sysccinfo := &oldSysCCInfo{origSysCCWhitelist: viper.GetStringMapString("chaincode.system")} 90 91 // System chaincode has to be enabled 92 viper.Set("chaincode.system", map[string]string{"sample_syscc": "true"}) 93 94 sysccinfo.origSystemCC = scc.MockRegisterSysCCs(sysccs) 95 96 /////^^^ system initialization completed ^^^ 97 return sysccinfo, lis, nil 98 } 99 100 func deploySampleSysCC(t *testing.T, ctxt context.Context, chainID string) error { 101 scc.DeploySysCCs(chainID) 102 103 defer scc.DeDeploySysCCs(chainID) 104 105 url := "github.com/hyperledger/fabric/core/scc/sample_syscc" 106 107 sysCCVers := util.GetSysCCVersion() 108 109 f := "putval" 110 args := util.ToChaincodeArgs(f, "greeting", "hey there") 111 112 spec := &pb.ChaincodeSpec{Type: 1, ChaincodeId: &pb.ChaincodeID{Name: "sample_syscc", Path: url, Version: sysCCVers}, Input: &pb.ChaincodeInput{Args: args}} 113 // the ledger is created with genesis block. Start block number 1 onwards 114 var nextBlockNumber uint64 = 1 115 _, _, _, err := invokeWithVersion(ctxt, chainID, sysCCVers, spec, nextBlockNumber, nil) 116 nextBlockNumber++ 117 118 cccid := ccprovider.NewCCContext(chainID, "sample_syscc", sysCCVers, "", true, nil, nil) 119 cdsforStop := &pb.ChaincodeDeploymentSpec{ExecEnv: 1, ChaincodeSpec: spec} 120 if err != nil { 121 theChaincodeSupport.Stop(ctxt, cccid, cdsforStop) 122 t.Logf("Error invoking sample_syscc: %s", err) 123 return err 124 } 125 126 f = "getval" 127 args = util.ToChaincodeArgs(f, "greeting") 128 spec = &pb.ChaincodeSpec{Type: 1, ChaincodeId: &pb.ChaincodeID{Name: "sample_syscc", Path: url, Version: sysCCVers}, Input: &pb.ChaincodeInput{Args: args}} 129 _, _, _, err = invokeWithVersion(ctxt, chainID, sysCCVers, spec, nextBlockNumber, nil) 130 if err != nil { 131 theChaincodeSupport.Stop(ctxt, cccid, cdsforStop) 132 t.Logf("Error invoking sample_syscc: %s", err) 133 return err 134 } 135 136 theChaincodeSupport.Stop(ctxt, cccid, cdsforStop) 137 138 return nil 139 } 140 141 // Test deploy of a transaction. 142 func TestExecuteDeploySysChaincode(t *testing.T) { 143 testForSkip(t) 144 sysccinfo, lis, err := initSysCCTests() 145 if err != nil { 146 t.Fail() 147 return 148 } 149 150 defer func() { 151 sysccinfo.reset() 152 }() 153 154 chainID := util.GetTestChainID() 155 156 if err = peer.MockCreateChain(chainID); err != nil { 157 closeListenerAndSleep(lis) 158 return 159 } 160 161 var ctxt = context.Background() 162 163 err = deploySampleSysCC(t, ctxt, chainID) 164 if err != nil { 165 closeListenerAndSleep(lis) 166 t.Fail() 167 return 168 } 169 170 closeListenerAndSleep(lis) 171 } 172 173 // Test multichains 174 func TestMultichains(t *testing.T) { 175 testForSkip(t) 176 sysccinfo, lis, err := initSysCCTests() 177 if err != nil { 178 t.Fail() 179 return 180 } 181 182 defer func() { 183 sysccinfo.reset() 184 }() 185 186 chainID := "chain1" 187 188 if err = peer.MockCreateChain(chainID); err != nil { 189 closeListenerAndSleep(lis) 190 return 191 } 192 193 var ctxt = context.Background() 194 195 err = deploySampleSysCC(t, ctxt, chainID) 196 if err != nil { 197 closeListenerAndSleep(lis) 198 t.Fail() 199 return 200 } 201 202 chainID = "chain2" 203 204 if err = peer.MockCreateChain(chainID); err != nil { 205 closeListenerAndSleep(lis) 206 return 207 } 208 209 err = deploySampleSysCC(t, ctxt, chainID) 210 if err != nil { 211 closeListenerAndSleep(lis) 212 t.Fail() 213 return 214 } 215 216 closeListenerAndSleep(lis) 217 }