public void TestInvokeChaincode()
        {
            ChaincodeBase  cb = new Cb5();
            ChaincodeInput ci = new ChaincodeInput();

            ci.Args.AddRange(new[] { ByteString.CopyFromUtf8("") });
            ByteString       initPayload = ci.ToByteString();
            ChaincodeMessage initMsg     = MessageUtil.NewEventMessage(ChaincodeMessage.Types.Type.Init, "testChannel", "0", initPayload, null);

            ci = new ChaincodeInput();
            ci.Args.AddRange(new[] { ByteString.CopyFromUtf8("invoke") });
            ByteString       invokePayload = ci.ToByteString();
            ChaincodeMessage invokeMsg     = MessageUtil.NewEventMessage(ChaincodeMessage.Types.Type.Transaction, "testChannel", "0", invokePayload, null);

            List <IScenarioStep> scenario = new List <IScenarioStep>();

            scenario.Add(new RegisterStep());
            scenario.Add(new CompleteStep());
            scenario.Add(new InvokeChaincodeStep());
            scenario.Add(new CompleteStep());

            server = ChaincodeMockPeer.StartServer(scenario);

            cb.Start(new string[] { "-a", "127.0.0.1:7052", "-i", "testId" });
            CheckScenarioStepEnded(server, 1, Timeout);
            server.Send(initMsg);
            CheckScenarioStepEnded(server, 2, Timeout);
            server.Send(invokeMsg);
            CheckScenarioStepEnded(server, 4, Timeout);
            Assert.AreEqual(server.LastMessageSend.Type, ChaincodeMessage.Types.Type.Response);
            Assert.AreEqual(server.LastMessageRcvd.Type, ChaincodeMessage.Types.Type.Completed);
        }
        public void TestRegisterAndEmptyInit()
        {
            ChaincodeBaseAsync cb  = new EmptyChaincode();
            ChaincodeInput     inp = new ChaincodeInput();

            inp.Args.Add(ByteString.CopyFromUtf8(""));
            ByteString       payload = inp.ToByteString();
            ChaincodeMessage initMsg = MessageUtil.NewEventMessage(ChaincodeMessage.Types.Type.Init, "testChannel", "0", payload, null);

            List <IScenarioStep> scenario = new List <IScenarioStep>();

            scenario.Add(new RegisterStep());
            scenario.Add(new CompleteStep());

            server = ChaincodeMockPeer.StartServer(scenario);

            cb.Start(new string[] { "-a", "127.0.0.1:7052", "-i", "testId" });
            CheckScenarioStepEnded(server, 1, Timeout);

            server.Send(initMsg);
            CheckScenarioStepEnded(server, 2, Timeout);

            Assert.AreEqual(server.LastMessageSend.Type, ChaincodeMessage.Types.Type.Init);
            Assert.AreEqual(server.LastMessageRcvd.Type, ChaincodeMessage.Types.Type.Completed);
        }
        public void TestGetHistoryForKey()
        {
            ChaincodeBase  cb = new Cb4();
            ChaincodeInput ci = new ChaincodeInput();

            ci.Args.AddRange(new[] { ByteString.CopyFromUtf8("") });
            ByteString       initPayload = ci.ToByteString();
            ChaincodeMessage initMsg     = MessageUtil.NewEventMessage(ChaincodeMessage.Types.Type.Init, "testChannel", "0", initPayload, null);

            ci = new ChaincodeInput();
            ci.Args.AddRange(new[] { ByteString.CopyFromUtf8("invoke"), ByteString.CopyFromUtf8("key1") });
            ByteString       invokePayload = ci.ToByteString();
            ChaincodeMessage invokeMsg     = MessageUtil.NewEventMessage(ChaincodeMessage.Types.Type.Transaction, "testChannel", "0", invokePayload, null);

            List <IScenarioStep> scenario = new List <IScenarioStep>();

            scenario.Add(new RegisterStep());
            scenario.Add(new CompleteStep());
            scenario.Add(new GetHistoryForKeyStep(false, "1", "2"));
            scenario.Add(new QueryCloseStep());
            scenario.Add(new CompleteStep());
            server = ChaincodeMockPeer.StartServer(scenario);

            cb.Start(new string[] { "-a", "127.0.0.1:7052", "-i", "testId" });
            CheckScenarioStepEnded(server, 1, Timeout);
            server.Send(initMsg);
            CheckScenarioStepEnded(server, 2, Timeout);
            server.Send(invokeMsg);
            CheckScenarioStepEnded(server, 5, Timeout);
            Assert.AreEqual(server.LastMessageSend.Type, ChaincodeMessage.Types.Type.Response);
            Assert.AreEqual(server.LastMessageRcvd.Type, ChaincodeMessage.Types.Type.Completed);
            Assert.AreEqual(Protos.Peer.ProposalResponsePackage.Response.Parser.ParseFrom(server.LastMessageRcvd.Payload).Message, "OK response2");
        }
        [Ignore] //Not Supported yet
        public void TestChaincodeLogLevel()
        {
            ChaincodeBase cb = new EmptyChaincode();

            List <IScenarioStep> scenario = new List <IScenarioStep>();

            scenario.Add(new RegisterStep());
            scenario.Add(new CompleteStep());
            server = ChaincodeMockPeer.StartServer(scenario);

            cb.Start(new string[] { "-a", "127.0.0.1:7052", "-i", "testId" });

            //assertEquals("Wrong debug level for " + cb.getClass().getPackage().getName(), Level.FINEST, Logger.getLogger(cb.getClass().getPackage().getName()).getLevel());
        }
        public void TestRegister()
        {
            ChaincodeBaseAsync cb = new EmptyChaincode();

            List <IScenarioStep> scenario = new List <IScenarioStep>();

            scenario.Add(new RegisterStep());

            server = ChaincodeMockPeer.StartServer(scenario);

            cb.Start(new string[] { "-a", "127.0.0.1:7052", "-i", "testId" });

            CheckScenarioStepEnded(server, 1, Timeout);
            Assert.AreEqual(server.LastMessageSend.Type, ChaincodeMessage.Types.Type.Ready);
            Assert.AreEqual(server.LastMessageRcvd.Type, ChaincodeMessage.Types.Type.Register);
        }
        public void TestStateValidationParameter()
        {
            ChaincodeBase  cb = new Cb8();
            ChaincodeInput ci = new ChaincodeInput();

            ci.Args.AddRange(new[] { ByteString.CopyFromUtf8("init") });
            ByteString            initPayload = ci.ToByteString();
            ChaincodeMessage      initMsg     = MessageUtil.NewEventMessage(ChaincodeMessage.Types.Type.Init, "testChannel", "0", initPayload, null);
            StateBasedEndorsement sbe         = new StateBasedEndorsement(null);

            sbe.AddOrgs(RoleType.RoleTypePeer, "Org1");
            sbe.AddOrgs(RoleType.RoleTypeMember, "Org2");

            List <IScenarioStep> scenario = new List <IScenarioStep>();


            scenario.Add(new RegisterStep());
            scenario.Add(new CompleteStep());

            scenario.Add(new GetStateMetadataStep(sbe));
            scenario.Add(new PutStateMetadataStep(sbe));
            scenario.Add(new CompleteStep());
            server = ChaincodeMockPeer.StartServer(scenario);

            cb.Start(new string[] { "-a", "127.0.0.1:7052", "-i", "testId" });
            CheckScenarioStepEnded(server, 1, Timeout);

            server.Send(initMsg);
            CheckScenarioStepEnded(server, 2, Timeout);
            Assert.AreEqual(server.LastMessageSend.Type, ChaincodeMessage.Types.Type.Init);
            Assert.AreEqual(server.LastMessageRcvd.Type, ChaincodeMessage.Types.Type.Completed);
            Assert.AreEqual(Protos.Peer.ProposalResponsePackage.Response.Parser.ParseFrom(server.LastMessageRcvd.Payload).Message, "OK response1");


            ci = new ChaincodeInput();
            ci.Args.AddRange(new[] { ByteString.CopyFromUtf8("invoke"), ByteString.CopyFromUtf8("a") });
            ByteString       invokePayload = ci.ToByteString();
            ChaincodeMessage invokeMsg     = MessageUtil.NewEventMessage(ChaincodeMessage.Types.Type.Transaction, "testChannel", "0", invokePayload, null);

            server.Send(invokeMsg);
            CheckScenarioStepEnded(server, 5, Timeout);
            Assert.AreEqual(server.LastMessageSend.Type, ChaincodeMessage.Types.Type.Response);
            Assert.AreEqual(server.LastMessageRcvd.Type, ChaincodeMessage.Types.Type.Completed);
            Assert.AreEqual(Protos.Peer.ProposalResponsePackage.Response.Parser.ParseFrom(server.LastMessageRcvd.Payload).Message, "OK response2");
        }
        public void TestStreamShutdown()
        {
            ChaincodeBase  cb = new Cb7();
            ChaincodeInput ci = new ChaincodeInput();

            ci.Args.AddRange(new[] { ByteString.CopyFromUtf8("") });
            ByteString       initPayload = ci.ToByteString();
            ChaincodeMessage initMsg     = MessageUtil.NewEventMessage(ChaincodeMessage.Types.Type.Init, "testChannel", "0", initPayload, null);

            List <IScenarioStep> scenario = new List <IScenarioStep>();

            scenario.Add(new RegisterStep());
            scenario.Add(new CompleteStep());
            server = ChaincodeMockPeer.StartServer(scenario);

            cb.Start(new string[] { "-a", "127.0.0.1:7052", "-i", "testId" });
            CheckScenarioStepEnded(server, 1, Timeout);

            server.Send(initMsg);
            server.Stop();
            server = null;
        }