Esempio n. 1
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!");
        }
Esempio n. 2
0
        // ReSharper disable once UnusedMethodReturnValue.Local
        private static ChaincodeID DeployChaincode(HFClient client, Channel channel, string ccName, string ccPath, string ccVersion)
        {
            Util.COut("deployChaincode - enter");
            ChaincodeID chaincodeID = null;

            try
            {
                string channelName = channel.Name;
                Util.COut("deployChaincode - channelName = " + channelName);

                IReadOnlyList <Orderer> orderers = channel.Orderers;
                List <ProposalResponse> responses;
                List <ProposalResponse> successful = new List <ProposalResponse>();
                List <ProposalResponse> failed     = new List <ProposalResponse>();

                chaincodeID = new ChaincodeID().SetName(ccName).SetVersion(ccVersion).SetPath(ccPath);

                ////////////////////////////
                // Install Proposal Request
                //
                Util.COut("Creating install proposal");

                InstallProposalRequest installProposalRequest = client.NewInstallProposalRequest();
                installProposalRequest.SetChaincodeID(chaincodeID);

                ////For GO language and serving just a single user, chaincodeSource is mostly likely the users GOPATH
                installProposalRequest.SetChaincodeSourceLocation(Path.Combine(TEST_FIXTURES_PATH, "sdkintegration/gocc/sample1").Locate());

                installProposalRequest.SetChaincodeVersion(ccVersion);

                Util.COut("Sending install proposal");

                ////////////////////////////
                // only a client from the same org as the peer can issue an install request
                int numInstallProposal = 0;

                IReadOnlyList <Peer> peersFromOrg = channel.Peers;
                numInstallProposal = numInstallProposal + peersFromOrg.Count;
                responses          = client.SendInstallProposal(installProposalRequest, peersFromOrg);

                foreach (ProposalResponse response in responses)
                {
                    if (response.Status == ChaincodeResponse.ChaincodeResponseStatus.SUCCESS)
                    {
                        Util.COut("Successful install proposal response Txid: {0} from peer {1}", response.TransactionID, response.Peer.Name);
                        successful.Add(response);
                    }
                    else
                    {
                        failed.Add(response);
                    }
                }

                Util.COut("Received {0} install proposal responses. Successful+verified: {1} . Failed: {2}", numInstallProposal, successful.Count, failed.Count);

                if (failed.Count > 0)
                {
                    ProposalResponse first = failed.First();
                    Assert.Fail($"Not enough endorsers for install : {successful.Count}.  {first.Message}");
                }

                ///////////////
                //// Instantiate chaincode.
                //
                // From the docs:
                // The instantiate transaction invokes the lifecycle System Chaincode (LSCC) to create and initialize a chaincode on a channel
                // After being successfully instantiated, the chaincode enters the active state on the channel and is ready to process any transaction proposals of type ENDORSER_TRANSACTION

                InstantiateProposalRequest instantiateProposalRequest = client.NewInstantiationProposalRequest();
                instantiateProposalRequest.SetProposalWaitTime(testConfig.GetProposalWaitTime());
                instantiateProposalRequest.SetChaincodeID(chaincodeID);
                instantiateProposalRequest.SetFcn("init");
                instantiateProposalRequest.SetArgs("a", "500", "b", "999");

                Dictionary <string, byte[]> tm = new Dictionary <string, byte[]>();
                tm.Add("HyperLedgerFabric", "InstantiateProposalRequest:JavaSDK".ToBytes());
                tm.Add("method", "InstantiateProposalRequest".ToBytes());
                instantiateProposalRequest.SetTransientMap(tm);

                /*
                 * policy OR(Org1MSP.member, Org2MSP.member) meaning 1 signature from someone in either Org1 or Org2
                 * See README.md Chaincode endorsement policies section for more details.
                 */
                ChaincodeEndorsementPolicy chaincodeEndorsementPolicy = new ChaincodeEndorsementPolicy();
                chaincodeEndorsementPolicy.FromYamlFile(Path.Combine(TEST_FIXTURES_PATH, "sdkintegration/chaincodeendorsementpolicy.yaml").Locate());
                instantiateProposalRequest.SetChaincodeEndorsementPolicy(chaincodeEndorsementPolicy);

                Util.COut("Sending instantiateProposalRequest to all peers...");
                successful.Clear();
                failed.Clear();

                responses = channel.SendInstantiationProposal(instantiateProposalRequest);

                foreach (ProposalResponse response in responses)
                {
                    if (response.IsVerified && response.Status == ChaincodeResponse.ChaincodeResponseStatus.SUCCESS)
                    {
                        successful.Add(response);
                        Util.COut("Succesful instantiate proposal response Txid:{0} from peer {1}", response.TransactionID, response.Peer.Name);
                    }
                    else
                    {
                        failed.Add(response);
                    }
                }

                Util.COut("Received {0} instantiate proposal responses. Successful+verified: {1} . Failed: {2}", responses.Count, successful.Count, failed.Count);
                if (failed.Count > 0)
                {
                    ProposalResponse first = failed.First();
                    Assert.Fail($"Not enough endorsers for instantiate :{successful.Count} endorser failed with {first.Message}. Was verified:{first.IsVerified}");
                }

                ///////////////
                // Send instantiate transaction to orderer
                Util.COut("Sending instantiateTransaction to orderer...");
                Util.COut("calling get...");
                TransactionEvent evnt = channel.SendTransaction(successful, orderers, 30 * 1000);
                Util.COut("get done...");

                Assert.IsTrue(evnt.IsValid); // must be valid to be here.
                Util.COut("Finished instantiate transaction with transaction id {0}", evnt.TransactionID);
            }
            catch (System.Exception e)
            {
                Util.COut("Caught an exception running channel {0}", channel.Name);
                Assert.Fail($"Test failed with error : {e.Message}");
            }

            return(chaincodeID);
        }
Esempio n. 3
0
        // ReSharper disable once UnusedMethodReturnValue.Local
        private static TransactionEvent MoveAmount(HFClient client, Channel channel, ChaincodeID chaincodeID, string from, string to, string moveAmount, IUser user)
        {
            List <ProposalResponse> successful = new List <ProposalResponse>();
            List <ProposalResponse> failed     = new List <ProposalResponse>();

            ///////////////
            // Send transaction proposal to all peers
            TransactionProposalRequest transactionProposalRequest = client.NewTransactionProposalRequest();

            transactionProposalRequest.SetChaincodeID(chaincodeID);
            transactionProposalRequest.SetFcn("move");
            transactionProposalRequest.SetArgs(from, to, moveAmount);
            transactionProposalRequest.SetProposalWaitTime(testConfig.GetProposalWaitTime());
            if (user != null)
            {
                // specific user use that
                transactionProposalRequest.SetUserContext(user);
            }

            Util.COut("sending transaction proposal to all peers with arguments: move(%s,%s,%s)", from, to, moveAmount);

            List <ProposalResponse> invokePropResp = channel.SendTransactionProposal(transactionProposalRequest);

            foreach (ProposalResponse response in invokePropResp)
            {
                if (response.Status == ChaincodeResponse.ChaincodeResponseStatus.SUCCESS)
                {
                    Util.COut("Successful transaction proposal response Txid: {0} from peer {1}", response.TransactionID, response.Peer.Name);
                    successful.Add(response);
                }
                else
                {
                    failed.Add(response);
                }
            }

            // Check that all the proposals are consistent with each other. We should have only one set
            // where all the proposals above are consistent.
            List <HashSet <ProposalResponse> > proposalConsistencySets = SDKUtils.GetProposalConsistencySets(invokePropResp);

            if (proposalConsistencySets.Count != 1)
            {
                Assert.Fail($"Expected only one set of consistent move proposal responses but got {proposalConsistencySets.Count}");
            }

            Util.COut("Received {0} transaction proposal responses. Successful+verified: {1} . Failed: {2}", invokePropResp.Count, successful.Count, failed.Count);
            if (failed.Count > 0)
            {
                ProposalResponse firstTransactionProposalResponse = failed.First();

                throw new ProposalException($"Not enough endorsers for invoke(move {from},{to},{moveAmount}):{firstTransactionProposalResponse.Status} endorser error:{firstTransactionProposalResponse.Message}. Was verified:{firstTransactionProposalResponse.IsVerified}");
            }

            Util.COut("Successfully received transaction proposal responses.");

            ////////////////////////////
            // Send transaction to orderer
            Util.COut("Sending chaincode transaction(move %s,%s,%s) to orderer.", from, to, moveAmount);
            if (user != null)
            {
                return(channel.SendTransaction(successful, user, testConfig.GetTransactionWaitTime() * 1000));
            }

            return(channel.SendTransaction(successful, testConfig.GetTransactionWaitTime() * 1000));
        }
Esempio n. 4
0
        // Returns the current value of b's assets
        private static string QueryChaincodeForCurrentValue(HFClient client, Channel channel, ChaincodeID chaincodeID)
        {
            Util.COut("Now query chaincode on channel {0} for the current value of b", channel.Name);

            QueryByChaincodeRequest queryByChaincodeRequest = client.NewQueryProposalRequest();

            queryByChaincodeRequest.SetArgs("b");
            queryByChaincodeRequest.SetFcn("query");
            queryByChaincodeRequest.SetChaincodeID(chaincodeID);

            List <ProposalResponse> queryProposals;

            queryProposals = channel.QueryByChaincode(queryByChaincodeRequest);
            string expect = null;

            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);
                    if (expect != null)
                    {
                        Assert.AreEqual(expect, payload);
                    }
                    else
                    {
                        expect = payload;
                    }
                }
            }

            return(expect);
        }
Esempio n. 5
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);
        }
        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}'");
                }
            }
        }
        private TransactionEvent MoveAmount(HFClient client, Channel channel, ChaincodeID chaincdeID, string moveAmount, IUser user)
        {
            try
            {
                List <ProposalResponse> successful = new List <ProposalResponse>();
                List <ProposalResponse> failed     = new List <ProposalResponse>();

                ///////////////
                // Send transaction proposal to all peers
                TransactionProposalRequest transactionProposalRequest = client.NewTransactionProposalRequest();
                transactionProposalRequest.ChaincodeID = chaincdeID;
                transactionProposalRequest.Fcn         = "move";
                transactionProposalRequest.SetArgs( //test using bytes .. end2end uses Strings.
                    "a".ToBytes(), "b".ToBytes(), moveAmount.ToBytes());
                transactionProposalRequest.ProposalWaitTime = testConfig.GetProposalWaitTime();
                if (user != null)
                {
                    // specific user use that
                    transactionProposalRequest.UserContext = user;
                }

                Util.COut("sending transaction proposal to all peers with arguments: move(a,b,{0})", moveAmount);

                List <ProposalResponse> invokePropResp = channel.SendTransactionProposal(transactionProposalRequest);
                foreach (ProposalResponse response in invokePropResp)
                {
                    if (response.Status == ChaincodeResponse.ChaincodeResponseStatus.SUCCESS)
                    {
                        Util.COut("Successful transaction proposal response Txid: {0} from peer {1}", response.TransactionID, response.Peer.Name);
                        successful.Add(response);
                    }
                    else
                    {
                        failed.Add(response);
                    }
                }

                Util.COut("Received {0} transaction proposal responses. Successful+verified: {1} . Failed: {2}", invokePropResp.Count, successful.Count, failed.Count);
                if (failed.Count > 0)
                {
                    ProposalResponse firstTransactionProposalResponse = failed.First();

                    throw new ProposalException($"Not enough endorsers for invoke(move a,b,{moveAmount}):{firstTransactionProposalResponse.Status} endorser error:{firstTransactionProposalResponse.Message}. Was verified:{firstTransactionProposalResponse.IsVerified}x");
                }

                Util.COut("Successfully received transaction proposal responses.");

                ////////////////////////////
                // Send transaction to orderer
                Util.COut("Sending chaincode transaction(move a,b,{0}) to orderer.", moveAmount);
                if (user != null)
                {
                    return(channel.SendTransaction(successful, user, testConfig.GetTransactionWaitTime() * 1000));
                }
                return(channel.SendTransaction(successful, testConfig.GetTransactionWaitTime() * 1000));
            }
            catch (System.Exception)
            {
                throw new TaskCanceledException();
            }
        }