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);
        }
        [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);
        }
Exemplo n.º 4
0
        public void TestHandlerStates()
        {
            CancellationToken token = default(CancellationToken);

            ChaincodeBaseAsync cb = new EmptyChaincode();

            ChaincodeID chaincodeId = new ChaincodeID {
                Name = "mycc"
            };
            Handler handler = Handler.CreateAsync(chaincodeId, cb).RunAndUnwrap();

            ChaincodeMessage msgReg = new ChaincodeMessage {
                Type = ChaincodeMessage.Types.Type.Registered
            };

            // Correct message
            handler.OnChaincodeMessageAsync(msgReg, token).RunAndUnwrap();
            Assert.AreEqual(CCState.ESTABLISHED, handler.State, "Not correct handler state");

            ChaincodeMessage msgReady = new ChaincodeMessage {
                Type = ChaincodeMessage.Types.Type.Ready
            };

            // Correct message
            handler.OnChaincodeMessageAsync(msgReady, token).RunAndUnwrap();
            Assert.AreEqual(CCState.READY, handler.State, "Not correct handler state");

            handler = Handler.CreateAsync(chaincodeId, cb).RunAndUnwrap();
            // Incorrect message
            handler.OnChaincodeMessageAsync(msgReady, token).RunAndUnwrap();
            Assert.AreEqual(CCState.CREATED, handler.State, "Not correct handler state");
            // Correct message
            handler.OnChaincodeMessageAsync(msgReg, token).RunAndUnwrap();
            Assert.AreEqual(CCState.ESTABLISHED, handler.State, "Not correct handler state");
            // Incorrect message
            handler.OnChaincodeMessageAsync(msgReg, token).RunAndUnwrap();
            Assert.AreEqual(CCState.ESTABLISHED, handler.State, "Not correct handler state");
            handler.OnChaincodeMessageAsync(msgReady, token).RunAndUnwrap();
            Assert.AreEqual(CCState.READY, handler.State, "Not correct handler state");

            // Unrelated message, do nothing
            ChaincodeMessage unkonwnMessage = new ChaincodeMessage {
                Type = ChaincodeMessage.Types.Type.PutState, ChannelId = "mychannel", Txid = "q", Payload = ByteString.CopyFromUtf8("")
            };

            handler.OnChaincodeMessageAsync(unkonwnMessage, token).RunAndUnwrap();
            Assert.AreEqual(CCState.READY, handler.State, "Not correct handler state");

            // KEEPALIVE message, do nothing
            ChaincodeMessage keepAliveMessage = new ChaincodeMessage {
                Type = ChaincodeMessage.Types.Type.Keepalive, ChannelId = "mychannel", Txid = "q", Payload = ByteString.CopyFromUtf8("")
            };

            handler.OnChaincodeMessageAsync(keepAliveMessage, token).RunAndUnwrap();
            Assert.AreEqual(CCState.READY, handler.State, "Not correct handler state");

            ChaincodeMessage errorMsg = new ChaincodeMessage {
                Type = ChaincodeMessage.Types.Type.Error, ChannelId = "mychannel", Txid = "q", Payload = ByteString.CopyFromUtf8("")
            };

            // Error message, except exception, no open communication
            try
            {
                handler.OnChaincodeMessageAsync(errorMsg, token).RunAndUnwrap();
                Assert.Fail("Expecting InvalidOperationException");
            }
            catch (InvalidOperationException)
            {
                //Ignore
            }

            Assert.AreEqual(CCState.READY, handler.State, "Not correct handler state");
        }