private static bool CheckInstalledChaincode(HFClient client, Peer peer, string ccName, string ccPath, string ccVersion)
        {
            Util.COut("Checking installed chaincode: {0}, at version: {1}, on peer: {2}", ccName, ccVersion, peer.Name);
            List <ChaincodeInfo> ccinfoList = client.QueryInstalledChaincodes(peer);

            bool found = false;

            foreach (ChaincodeInfo ccifo in ccinfoList)
            {
                if (ccPath != null)
                {
                    found = ccName.Equals(ccifo.Name) && ccPath.Equals(ccifo.Path) && ccVersion.Equals(ccifo.Version);
                    if (found)
                    {
                        break;
                    }
                }

                found = ccName.Equals(ccifo.Name) && ccVersion.Equals(ccifo.Version);
                if (found)
                {
                    break;
                }
            }

            return(found);
        }
Exemplo n.º 2
0
        public static HFClient Create()
        {
            HFClient hfclient = HFClient.Create();

            SetupClient(hfclient);
            return(hfclient);
        }
Exemplo n.º 3
0
        public void TestLoadFromConfigFileYamlNOOverrides()
        {
            // Should be able to instantiate a new instance of "Client" with a valid path to the YAML configuration
            string        f      = TestUtils.TestUtils.RelocateFilePathsYAML("fixture/sdkintegration/network_configs/network-config.yaml".Locate());
            NetworkConfig config = NetworkConfig.FromYamlFile(f);

            //HFClient client = HFClient.loadFromConfig(f);
            Assert.IsNotNull(config);
            HFClient client = HFClient.Create();

            client.CryptoSuite = Factory.Instance.GetCryptoSuite();
            client.UserContext = TestUtils.TestUtils.GetMockUser(USER_NAME, USER_MSP_ID);

            Channel channel = client.LoadChannelFromConfig("foo", config);

            Assert.IsNotNull(channel);

            Assert.IsTrue(channel.Peers.Count != 0);

            foreach (Peer peer in channel.Peers)
            {
                Properties properties = peer.Properties;

                Assert.IsNotNull(properties);
                //Assert.IsNull(properties.Get("grpc.keepalive_time_ms"));
                Assert.IsNull(properties.Get("grpc.keepalive_timeout_ms"));
                //Assert.IsNull(properties.Get("grpc.NettyChannelBuilderOption.keepAliveWithoutCalls"));
            }
        }
        private void QueryChaincodeForExpectedValue(HFClient client, Channel channel, string expect, ChaincodeID chaincdeID)
        {
            Util.COut("Now query chaincode {0} on channel {1} for the value of b expecting to see: {2}", chaincdeID, channel.Name, expect);
            QueryByChaincodeRequest queryByChaincodeRequest = client.NewQueryProposalRequest();

            queryByChaincodeRequest.SetArgs("b".ToBytes()); // test using bytes as args. End2end uses Strings.
            queryByChaincodeRequest.SetFcn("query");
            queryByChaincodeRequest.ChaincodeID = chaincdeID;

            List <ProposalResponse> queryProposals;

            queryProposals = channel.QueryByChaincode(queryByChaincodeRequest);

            foreach (ProposalResponse proposalResponse in queryProposals)
            {
                if (!proposalResponse.IsVerified || proposalResponse.Status != ChaincodeResponse.ChaincodeResponseStatus.SUCCESS)
                {
                    Assert.Fail($"Failed query proposal from peer {proposalResponse.Peer.Name} status: {proposalResponse.Status}. Messages: {proposalResponse.Message}. Was verified : {proposalResponse.IsVerified}");
                }
                else
                {
                    string payload = proposalResponse.ProtoProposalResponse.Response.Payload.ToStringUtf8();
                    Util.COut("Query payload of b from peer {0} returned {1}", proposalResponse.Peer.Name, payload);
                    Assert.AreEqual(expect, payload, $"Failed compare on channel {channel.Name} chaincode id {chaincdeID} expected value:'{expect}', but got:'{payload}'");
                }
            }
        }
Exemplo n.º 5
0
        public void TestLoadFromConfigFileYamlNOOverridesButSet()
        {
            // Should be able to instantiate a new instance of "Client" with a valid path to the YAML configuration
            string        f      = TestUtils.TestUtils.RelocateFilePathsYAML("fixture/sdkintegration/network_configs/network-config.yaml".Locate());
            NetworkConfig config = NetworkConfig.FromYamlFile(f);

            //HFClient client = HFClient.loadFromConfig(f);
            Assert.IsNotNull(config);

            HFClient client = HFClient.Create();

            client.CryptoSuite = Factory.Instance.GetCryptoSuite();
            client.UserContext = TestUtils.TestUtils.GetMockUser(USER_NAME, USER_MSP_ID);


            Channel channel = client.LoadChannelFromConfig("foo", config);

            Assert.IsNotNull(channel);

            Assert.IsTrue(channel.Orderers.Count != 0);

            foreach (Orderer orderer in channel.Orderers)
            {
                Properties properties = orderer.Properties;
                string     str        = properties.Get("grpc.keepalive_time_ms");
                Assert.AreEqual(long.Parse(str), 360000L);

                str = properties.Get("grpc.keepalive_timeout_ms");
                Assert.AreEqual(long.Parse(str), 180000L);
            }
        }
Exemplo n.º 6
0
        public UserManager()
        {
            keyStorePath  = @"D:\Projetos\Hyperledger\fabcar\hfc-key-store\";
            admin         = SampleUser.Load("admin", keyStorePath);
            fabric_client = HFClient.Create();
            var crypto = new Hyperledger.Fabric.SDK.Security.CryptoPrimitives();

            crypto.Init();
            crypto.Store.AddCertificate(admin.Enrollment.Cert);

            fabric_client.CryptoSuite = crypto;

            fabric_client.UserContext = admin;

            channel = fabric_client.NewChannel("mychannel");
            var peer = fabric_client.NewPeer("p1", $"grpc://{Startup.HyperleaderServer}:7051");

            channel.AddPeer(peer);

            var ordered = fabric_client.NewOrderer("o1", $"grpc://{Startup.HyperleaderServer}:7050");

            channel.AddOrderer(ordered);

            channel.Initialize();

            fabric_ca_client             = new Hyperledger.Fabric_CA.SDK.HFCAClient("", $"http://{Startup.HyperleaderServer}:7054", new Hyperledger.Fabric.SDK.Helper.Properties());
            fabric_ca_client.CryptoSuite = crypto;
        }
Exemplo n.º 7
0
        /**
         * Just for testing remove all peers and orderers and add them back.
         *
         * @param client
         * @param channel
         */
        public static void TestRemovingAddingPeersOrderers(HFClient client, Channel channel)
        {
            Dictionary <Peer, PeerOptions> perm = new Dictionary <Peer, PeerOptions>();

            Assert.IsTrue(channel.IsInitialized);
            Assert.IsFalse(channel.IsShutdown);
            Thread.Sleep(1500); // time needed let channel get config block

            channel.Peers.ToList().ForEach(peer =>
            {
                perm[peer] = channel.GetPeersOptions(peer);
                channel.RemovePeer(peer);
            });

            perm.Keys.ToList().ForEach(peer =>
            {
                PeerOptions value = perm[peer];
                Peer newPeer      = client.NewPeer(peer.Name, peer.Url, peer.Properties);
                channel.AddPeer(newPeer, value);
            });

            List <Orderer> removedOrders = new List <Orderer>();

            foreach (Orderer orderer in channel.Orderers.ToList())
            {
                channel.RemoveOrderer(orderer);
                removedOrders.Add(orderer);
            }

            removedOrders.ForEach(orderer =>
            {
                Orderer newOrderer = client.NewOrderer(orderer.Name, orderer.Url, orderer.Properties);
                channel.AddOrderer(newOrderer);
            });
        }
        public void RunFabricTest(SampleStore sStore)
        {
            ////////////////////////////
            // Setup client

            //Create instance of client.
            HFClient client = HFClient.Create();

            client.CryptoSuite = Factory.Instance.GetCryptoSuite();

            ////////////////////////////
            //Reconstruct and run the channels
            SampleOrg sampleOrg  = testConfig.GetIntegrationTestsSampleOrg("peerOrg1");
            Channel   fooChannel = ReconstructChannel(FOO_CHANNEL_NAME, client, sampleOrg);

            RunChannel(client, fooChannel, sampleOrg, 0);
            Assert.IsFalse(fooChannel.IsShutdown);
            Assert.IsTrue(fooChannel.IsInitialized);
            fooChannel.Shutdown(true); //clean up resources no longer needed.
            Assert.IsTrue(fooChannel.IsShutdown);
            Util.COut("\n");

            sampleOrg = testConfig.GetIntegrationTestsSampleOrg("peerOrg2");
            Channel barChannel = ReconstructChannel(BAR_CHANNEL_NAME, client, sampleOrg);

            RunChannel(client, barChannel, sampleOrg, 100); //run a newly constructed foo channel with different b value!
            Assert.IsFalse(barChannel.IsShutdown);
            Assert.IsTrue(barChannel.IsInitialized);

            if (!testConfig.IsRunningAgainstFabric10())
            {
                //Peer eventing service support started with v1.1

                // Now test replay feature of V1.1 peer eventing services.
                string json = barChannel.Serialize();
                barChannel.Shutdown(true);

                Channel replayChannel = client.DeSerializeChannel(json);

                Util.COut("doing testPeerServiceEventingReplay,0,-1,false");
                TestPeerServiceEventingReplay(client, replayChannel, 0L, -1L, false);

                replayChannel = client.DeSerializeChannel(json);
                Util.COut("doing testPeerServiceEventingReplay,0,-1,true"); // block 0 is import to test
                TestPeerServiceEventingReplay(client, replayChannel, 0L, -1L, true);

                //Now do it again starting at block 1
                replayChannel = client.DeSerializeChannel(json);
                Util.COut("doing testPeerServiceEventingReplay,1,-1,false");
                TestPeerServiceEventingReplay(client, replayChannel, 1L, -1L, false);

                //Now do it again starting at block 2 to 3
                replayChannel = client.DeSerializeChannel(json);
                Util.COut("doing testPeerServiceEventingReplay,2,3,false");
                TestPeerServiceEventingReplay(client, replayChannel, 2L, 3L, false);
            }

            Util.COut("That's all folks!");
        }
Exemplo n.º 9
0
        internal override Channel ConstructChannel(string name, HFClient client, SampleOrg sampleOrg)
        {
            // override this method since we don't want to construct the channel that's been done.
            // Just get it out of the samplestore!

            client.UserContext = sampleOrg.PeerAdmin;
            return(sampleStore.GetChannel(client, name).Initialize());
        }
Exemplo n.º 10
0
        private static void QueryChaincodeForExpectedValue(HFClient client, Channel channel, string expect, ChaincodeID chaincodeID)
        {
            Util.COut("Now query chaincode on channel {0} for the value of b expecting to see: {1}", channel.Name, expect);

            string value = QueryChaincodeForCurrentValue(client, channel, chaincodeID);

            Assert.AreEqual(expect, value);
        }
Exemplo n.º 11
0
        public void TestBadUserContextNull()
        {
            HFClient client = HFClient.Create();

            client.CryptoSuite = Factory.Instance.GetCryptoSuite();

            client.UserContext = null;
        }
Exemplo n.º 12
0
        public void TestBadUserNameNull()
        {
            HFClient client = HFClient.Create();

            client.CryptoSuite = Factory.Instance.GetCryptoSuite();

            TestUtils.TestUtils.MockUser mockUser = TestUtils.TestUtils.GetMockUser(null, USER_MSP_ID);

            client.UserContext = mockUser;
        }
Exemplo n.º 13
0
        public void TestGoodMockUser()
        {
            HFClient client = HFClient.Create();

            client.CryptoSuite = Factory.Instance.GetCryptoSuite();
            client.UserContext = TestUtils.TestUtils.GetMockUser(USER_NAME, USER_MSP_ID);
            Orderer orderer = hfclient.NewOrderer("justMockme", "grpc://localhost:99"); // test mock should work.

            Assert.IsNotNull(orderer);
        }
Exemplo n.º 14
0
        public void TestBadEnrollmentNull()
        {
            HFClient client = HFClient.Create();

            client.CryptoSuite = Factory.Instance.GetCryptoSuite();


            TestUtils.TestUtils.MockUser mockUser = TestUtils.TestUtils.GetMockUser(USER_NAME, USER_MSP_ID);
            mockUser.Enrollment = null;
            client.UserContext  = mockUser;
        }
Exemplo n.º 15
0
        public void TestBadUserMSPIDNull()
        {
            HFClient client = HFClient.Create();

            client.CryptoSuite = Factory.Instance.GetCryptoSuite();


            TestUtils.TestUtils.MockUser mockUser = TestUtils.TestUtils.GetMockUser(USER_NAME, null);

            client.UserContext = mockUser;
        }
Exemplo n.º 16
0
 public static void SetupClient(TestContext context)
 {
     try
     {
         hfclient = TestHFClient.Create();
     }
     catch (System.Exception e)
     {
         Assert.Fail($"Unexpected Exception {e.Message}");
     }
 }
Exemplo n.º 17
0
        // Returns a new client instance
        private static HFClient GetTheClient()
        {
            HFClient client = HFClient.Create();

            client.CryptoSuite = Factory.Instance.GetCryptoSuite();

            IUser peerAdmin = GetAdminUser(TEST_ORG);

            client.UserContext = peerAdmin;

            return(client);
        }
Exemplo n.º 18
0
        private static Channel ConstructChannel(HFClient client, string channelName)
        {
            //Channel newChannel = client.GetChannel(channelName);
            Channel newChannel = client.LoadChannelFromConfig(channelName, networkConfig);

            if (newChannel == null)
            {
                throw new System.Exception("Channel " + channelName + " is not defined in the config file!");
            }

            return(newChannel.Initialize());
        }
Exemplo n.º 19
0
 public static void SetupClient(TestContext context)
 {
     try
     {
         hfclient = TestHFClient.Create();
         peer     = hfclient.NewPeer(PEER_NAME, "grpc://localhost:7051");
     }
     catch (System.Exception e)
     {
         Assert.Fail($"Unexpected Exception {e.Message}");
     }
 }
Exemplo n.º 20
0
        public Channel GetChannel(HFClient client, string name)
        {
            Channel ret = null;

            string channelHex = GetValue("channel." + name);

            if (channelHex != null)
            {
                ret = client.DeSerializeChannel(channelHex.FromHexString().ToUTF8String());
            }

            return(ret);
        }
Exemplo n.º 21
0
        public void TestBadEnrollmentBadKey()
        {
            HFClient client = HFClient.Create();

            client.CryptoSuite = Factory.Instance.GetCryptoSuite();



            TestUtils.TestUtils.MockUser mockUser = TestUtils.TestUtils.GetMockUser(USER_NAME, USER_MSP_ID);

            IEnrollment mockEnrollment = TestUtils.TestUtils.GetMockEnrollment(null, "mockCert");

            mockUser.Enrollment = mockEnrollment;
            client.UserContext  = mockUser;
        }
Exemplo n.º 22
0
        public void TestUpdate1()
        {
            // Setup client and channel instances
            HFClient client  = GetTheClient();
            Channel  channel = ConstructChannel(client, FOO_CHANNEL_NAME);

            ChaincodeID chaincodeID = new ChaincodeID().SetName(CHAIN_CODE_NAME).SetVersion(CHAIN_CODE_VERSION).SetPath(CHAIN_CODE_PATH);

            string channelName = channel.Name;

            Util.COut("Running testUpdate1 - Channel %s", channelName);

            int    moveAmount  = 5;
            string originalVal = QueryChaincodeForCurrentValue(client, channel, chaincodeID);
            string newVal      = "" + (int.Parse(originalVal) + moveAmount);

            Util.COut("Original value = {0}", originalVal);

            //user registered user
            client.UserContext = orgRegisteredUsers.GetOrNull("Org1"); // only using org1

            // Move some assets
            try
            {
                MoveAmount(client, channel, chaincodeID, "a", "b", "" + moveAmount, null);
                QueryChaincodeForExpectedValue(client, channel, newVal, chaincodeID);
                MoveAmount(client, channel, chaincodeID, "b", "a", "" + moveAmount, null);
                QueryChaincodeForExpectedValue(client, channel, originalVal, chaincodeID);
            }
            catch (TransactionEventException t)
            {
                TransactionEvent te = t.TransactionEvent;
                if (te != null)
                {
                    Assert.Fail($"Transaction with txid {te.TransactionID} failed. {t.Message}");
                }
                Assert.Fail($"Transaction failed with exception message {t.Message}");
            }
            catch (System.Exception e)
            {
                Assert.Fail($"Test failed with {e.GetType().Name} exception {e.Message}");
            }

            channel.Shutdown(true); // Force channel to shutdown clean up resources.

            Util.COut("testUpdate1 - done");
            Util.COut("That's all folks!");
        }
Exemplo n.º 23
0
        public void TestNewChannel()
        {
            // Should be able to instantiate a new instance of "Channel" with the definition in the network configuration'
            JObject jsonConfig = GetJsonConfig(1, 0, 1);

            NetworkConfig config = NetworkConfig.FromJsonObject(jsonConfig);

            HFClient client = HFClient.Create();

            TestHFClient.SetupClient(client);

            Channel channel = client.LoadChannelFromConfig(CHANNEL_NAME, config);

            Assert.IsNotNull(channel);
            Assert.AreEqual(CHANNEL_NAME, channel.Name);
            Assert.AreEqual(channel.GetPeers(PeerRole.SERVICE_DISCOVERY).Count, 1);
        }
Exemplo n.º 24
0
        // Determines whether or not the chaincode has been deployed and deploys it if necessary
        private static void DeployChaincodeIfRequired()
        {
            ////////////////////////////
            // Setup client
            HFClient client = GetTheClient();

            Channel channel = ConstructChannel(client, FOO_CHANNEL_NAME);

            // Use any old peer...
            Peer peer = channel.Peers.First();

            if (!CheckInstantiatedChaincode(channel, peer, CHAIN_CODE_NAME, CHAIN_CODE_PATH, CHAIN_CODE_VERSION))
            {
                // The chaincode we require does not exist, so deploy it...
                DeployChaincode(client, channel, CHAIN_CODE_NAME, CHAIN_CODE_PATH, CHAIN_CODE_VERSION);
            }
        }
Exemplo n.º 25
0
        public void TestGetChannelNotExists()
        {
            // Should be able to instantiate a new instance of "Client" with a valid path to the YAML configuration
            string        f      = TestUtils.TestUtils.RelocateFilePathsYAML("fixture/sdkintegration/network_configs/network-config.yaml".Locate());
            NetworkConfig config = NetworkConfig.FromYamlFile(f);

            //HFClient client = HFClient.loadFromConfig(f);
            Assert.IsNotNull(config);


            HFClient client = HFClient.Create();

            client.CryptoSuite = Factory.Instance.GetCryptoSuite();
            client.UserContext = TestUtils.TestUtils.GetMockUser(USER_NAME, USER_MSP_ID);

            client.LoadChannelFromConfig("MissingChannel", config);
        }
Exemplo n.º 26
0
        public void TestCryptoFactory()
        {
            TestUtils.TestUtils.ResetConfig();
            Assert.IsNotNull(Config.Instance.GetDefaultCryptoSuiteFactory());

            HFClient client = HFClient.Create();

            client.CryptoSuite = Factory.Instance.GetCryptoSuite();


            TestUtils.TestUtils.MockUser mockUser = TestUtils.TestUtils.GetMockUser(USER_NAME, USER_MSP_ID);

            IEnrollment mockEnrollment = TestUtils.TestUtils.GetMockEnrollment(null, "mockCert");

            mockUser.Enrollment = mockEnrollment;
            client.UserContext  = mockUser;
        }
Exemplo n.º 27
0
        public static void SetupClient(TestContext context)
        {
            try
            {
                hfclient = TestHFClient.Create();

                shutdownChannel = new Channel("shutdown", hfclient);
                shutdownChannel.AddOrderer(hfclient.NewOrderer("shutdow_orderer", "grpc://localhost:99"));
                shutdownChannel.IsShutdown = true;
                throwOrderer = new ThrowOrderer("foo", "grpc://localhost:8", null);
                throwChannel = new Channel("throw", hfclient);
                throwChannel.AddOrderer(throwOrderer);
            }
            catch (System.Exception e)
            {
                Assert.Fail($"Unexpected Exception {e.Message}");
            }
        }
Exemplo n.º 28
0
        public void TestGetChannelNoPeers()
        {
            // Should not be able to instantiate a new instance of "Channel" with no peers configured
            JObject jsonConfig = GetJsonConfig(1, 1, 0);

            NetworkConfig config = NetworkConfig.FromJsonObject(jsonConfig);

            HFClient client = HFClient.Create();

            TestHFClient.SetupClient(client);

            client.LoadChannelFromConfig(CHANNEL_NAME, config);

            //HFClient client = HFClient.loadFromConfig(jsonConfig);
            //TestHFClient.setupClient(client);

            //client.getChannel(CHANNEL_NAME);
        }
Exemplo n.º 29
0
        public void TestLoadFromConfigFileJson()
        {
            // Should be able to instantiate a new instance of "Client" with a valid path to the JSON configuration
            string        f      = TestUtils.TestUtils.RelocateFilePathsJSON("fixture/sdkintegration/network_configs/network-config.json".Locate());
            NetworkConfig config = NetworkConfig.FromJsonFile(f);

            Assert.IsNotNull(config);

            //HFClient client = HFClient.loadFromConfig(f);
            //Assert.Assert.IsNotNull(client);

            HFClient client = HFClient.Create();

            client.CryptoSuite = Factory.Instance.GetCryptoSuite();
            client.UserContext = TestUtils.TestUtils.GetMockUser(USER_NAME, USER_MSP_ID);

            Channel channel = client.LoadChannelFromConfig("mychannel", config);

            Assert.IsNotNull(channel);
        }
Exemplo n.º 30
0
        public static void SetupClient(HFClient hfclient)
        {
            ICryptoSuite cryptoSuite = Factory.GetCryptoSuite();

            string props = Path.Combine(GetHomePath(), "test.properties");

            if (File.Exists(props))
            {
                File.Delete(props);
            }
            SampleStore sampleStore = new SampleStore(props);

            //src/test/fixture/sdkintegration/e2e-2Orgs/channel/crypto-config/peerOrganizations/org1.example.com/users/[email protected]/msp/keystore/

            //SampleUser someTestUSER = sampleStore.getMember("someTestUSER", "someTestORG");
            SampleUser someTestUSER = sampleStore.GetMember("someTestUSER", "someTestORG", "mspid", FindFileSk("fixture/sdkintegration/e2e-2Orgs/" + TestConfig.Instance.FAB_CONFIG_GEN_VERS + "/crypto-config/peerOrganizations/org1.example.com/users/[email protected]/msp/keystore"), ("fixture/sdkintegration/e2e-2Orgs/" + TestConfig.Instance.FAB_CONFIG_GEN_VERS + "/crypto-config/peerOrganizations/org1.example.com/users/[email protected]/msp/signcerts/[email protected]").Locate());

            someTestUSER.MspId = "testMSPID?";

            hfclient.CryptoSuite = Factory.Instance.GetCryptoSuite();
            hfclient.UserContext = someTestUSER;
        }