github.com/leonlxy/hyperledger@v1.0.0-alpha.0.20170427033203-34922035d248/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 //use a different address than what we usually use for "peer" 55 //we override the peerAddress set in chaincode_support.go 56 // FIXME: Use peer.GetLocalAddress() 57 peerAddress := "0.0.0.0:21726" 58 lis, err := net.Listen("tcp", peerAddress) 59 if err != nil { 60 return nil, nil, err 61 } 62 63 getPeerEndpoint := func() (*pb.PeerEndpoint, error) { 64 return &pb.PeerEndpoint{Id: &pb.PeerID{Name: "testpeer"}, Address: peerAddress}, nil 65 } 66 67 ccStartupTimeout := time.Duration(5000) * time.Millisecond 68 pb.RegisterChaincodeSupportServer(grpcServer, NewChaincodeSupport(getPeerEndpoint, false, ccStartupTimeout)) 69 70 go grpcServer.Serve(lis) 71 72 //set systemChaincodes to sample 73 sysccs := []*scc.SystemChaincode{ 74 { 75 Enabled: true, 76 Name: "sample_syscc", 77 Path: "github.com/hyperledger/fabric/core/scc/samplesyscc", 78 InitArgs: [][]byte{}, 79 Chaincode: &samplesyscc.SampleSysCC{}, 80 }, 81 } 82 83 sysccinfo := &oldSysCCInfo{origSysCCWhitelist: viper.GetStringMapString("chaincode.system")} 84 85 // System chaincode has to be enabled 86 viper.Set("chaincode.system", map[string]string{"sample_syscc": "true"}) 87 88 sysccinfo.origSystemCC = scc.MockRegisterSysCCs(sysccs) 89 90 /////^^^ system initialization completed ^^^ 91 return sysccinfo, lis, nil 92 } 93 94 func deploySampleSysCC(t *testing.T, ctxt context.Context, chainID string) error { 95 scc.DeploySysCCs(chainID) 96 97 defer scc.DeDeploySysCCs(chainID) 98 99 url := "github.com/hyperledger/fabric/core/scc/sample_syscc" 100 101 sysCCVers := util.GetSysCCVersion() 102 103 f := "putval" 104 args := util.ToChaincodeArgs(f, "greeting", "hey there") 105 106 spec := &pb.ChaincodeSpec{Type: 1, ChaincodeId: &pb.ChaincodeID{Name: "sample_syscc", Path: url, Version: sysCCVers}, Input: &pb.ChaincodeInput{Args: args}} 107 // the ledger is created with genesis block. Start block number 1 onwards 108 var nextBlockNumber uint64 = 1 109 _, _, _, err := invokeWithVersion(ctxt, chainID, sysCCVers, spec, nextBlockNumber, nil) 110 nextBlockNumber++ 111 112 cccid := ccprovider.NewCCContext(chainID, "sample_syscc", sysCCVers, "", true, nil, nil) 113 cdsforStop := &pb.ChaincodeDeploymentSpec{ExecEnv: 1, ChaincodeSpec: spec} 114 if err != nil { 115 theChaincodeSupport.Stop(ctxt, cccid, cdsforStop) 116 t.Logf("Error invoking sample_syscc: %s", err) 117 return err 118 } 119 120 f = "getval" 121 args = util.ToChaincodeArgs(f, "greeting") 122 spec = &pb.ChaincodeSpec{Type: 1, ChaincodeId: &pb.ChaincodeID{Name: "sample_syscc", Path: url, Version: sysCCVers}, Input: &pb.ChaincodeInput{Args: args}} 123 _, _, _, err = invokeWithVersion(ctxt, chainID, sysCCVers, spec, nextBlockNumber, nil) 124 if err != nil { 125 theChaincodeSupport.Stop(ctxt, cccid, cdsforStop) 126 t.Logf("Error invoking sample_syscc: %s", err) 127 return err 128 } 129 130 theChaincodeSupport.Stop(ctxt, cccid, cdsforStop) 131 132 return nil 133 } 134 135 // Test deploy of a transaction. 136 func TestExecuteDeploySysChaincode(t *testing.T) { 137 sysccinfo, lis, err := initSysCCTests() 138 if err != nil { 139 t.Fail() 140 return 141 } 142 143 defer func() { 144 sysccinfo.reset() 145 }() 146 147 chainID := util.GetTestChainID() 148 149 if err = peer.MockCreateChain(chainID); err != nil { 150 closeListenerAndSleep(lis) 151 return 152 } 153 154 var ctxt = context.Background() 155 156 err = deploySampleSysCC(t, ctxt, chainID) 157 if err != nil { 158 closeListenerAndSleep(lis) 159 t.Fail() 160 return 161 } 162 163 closeListenerAndSleep(lis) 164 } 165 166 // Test multichains 167 func TestMultichains(t *testing.T) { 168 sysccinfo, lis, err := initSysCCTests() 169 if err != nil { 170 t.Fail() 171 return 172 } 173 174 defer func() { 175 sysccinfo.reset() 176 }() 177 178 chainID := "chain1" 179 180 if err = peer.MockCreateChain(chainID); err != nil { 181 closeListenerAndSleep(lis) 182 return 183 } 184 185 var ctxt = context.Background() 186 187 err = deploySampleSysCC(t, ctxt, chainID) 188 if err != nil { 189 closeListenerAndSleep(lis) 190 t.Fail() 191 return 192 } 193 194 chainID = "chain2" 195 196 if err = peer.MockCreateChain(chainID); err != nil { 197 closeListenerAndSleep(lis) 198 return 199 } 200 201 err = deploySampleSysCC(t, ctxt, chainID) 202 if err != nil { 203 closeListenerAndSleep(lis) 204 t.Fail() 205 return 206 } 207 208 closeListenerAndSleep(lis) 209 }