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");
        }
        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);
        }
Ejemplo n.º 4
0
        public virtual async Task <Response> InvokeChaincodeAsync(string channelId, string txId, string chaincodeName, List <byte[]> args, CancellationToken token = default(CancellationToken))
        {
            try
            {
                // create invocation specification of the chaincode to invoke
                ChaincodeSpec invocationSpec = new ChaincodeSpec {
                    ChaincodeId = new ChaincodeID {
                        Name = chaincodeName
                    }, Input = new ChaincodeInput {
                        Args = { args.Select(ByteString.CopyFrom) }
                    }
                };
                // invoke other chaincode
                ByteString payload = await InvokeChaincodeSupportAsync(NewInvokeChaincodeMessage(channelId, txId, invocationSpec.ToByteString()), token).ConfigureAwait(false);

                // response message payload should be yet another chaincode
                // message (the actual response message)
                ChaincodeMessage responseMessage = ChaincodeMessage.Parser.ParseFrom(payload);
                // the actual response message must be of type COMPLETED
                logger.Debug($"[{txId},-8]{responseMessage.Type} response received from other chaincode.");
                if (responseMessage.Type == ChaincodeMessage.Types.Type.Completed)
                {
                    // success
                    return(ToChaincodeResponse(Protos.Peer.ProposalResponsePackage.Response.Parser.ParseFrom(responseMessage.Payload)));
                }

                // error
                return(NewErrorChaincodeResponse(responseMessage.Payload.ToStringUtf8()));
            }
            catch (Exception e)
            {
                throw new InvalidOperationException(e.Message, e);
            }
        }
Ejemplo n.º 5
0
        private Task HandleReadyAsync(ChaincodeMessage message, CancellationToken token)
        {
            switch (message.Type)
            {
            case ChaincodeMessage.Types.Type.Response:
                logger.Debug($"[{message.Txid,-8}s] Received RESPONSE: publishing to channel");
                return(SendChannelAsync(message, token));

            case ChaincodeMessage.Types.Type.Error:
                logger.Debug($"[{message.Txid,-8}s] Received ERROR: publishing to channel");
                return(SendChannelAsync(message, token));

            case ChaincodeMessage.Types.Type.Init:
                logger.Debug($"[{message.Txid,-8}s] Received INIT: invoking chaincode init");
                return(HandleInitAsync(message, token));

            case ChaincodeMessage.Types.Type.Transaction:
                logger.Debug($"[{message.Txid,-8}s] Received TRANSACTION: invoking chaincode");
                return(HandleTransactionAsync(message, token));

            default:
                logger.Warning($"[{message.Txid,-8}s] Received {message.Type}: cannot handle");
                return(Task.FromResult(0));
            }
        }
        Chat_sends_an_error_to_the_peer_when_it_is_in_state_established_but_it_does_not_receive_a_ready_response()
        {
            var responseMessage = new ChaincodeMessage
            {
                Type = ChaincodeMessage.Types.Type.Completed
            };

            var requestStreamMock = new Mock <IClientStreamWriter <ChaincodeMessage> >();

            requestStreamMock.Setup(m => m.WriteAsync(null)).Returns(Task.CompletedTask);
            requestStreamMock.Setup(m => m.WriteAsync(It.Is(
                                                          (ChaincodeMessage message) => message != null && message.Type == ChaincodeMessage.Types.Type.Error
                                                          ))).Returns(Task.CompletedTask);

            var(responseStreamMock, chaincodeSupportClientMock, chaincodeSupportClientFactoryMock, handler) =
                CreateHandlerMockWithStreamExpectation(responseMessage, requestStreamMock.Object);

            handler.State = States.Established;
            await handler.Chat(null);

            chaincodeSupportClientFactoryMock.VerifyAll();
            chaincodeSupportClientMock.VerifyAll();
            responseStreamMock.VerifyAll();
            requestStreamMock.VerifyAll();
            handler.State.Should().Be(States.Established);
        }
Ejemplo n.º 7
0
        private async Task HandleChaincodeMessageAsync(ChaincodeMessage message, CancellationToken token)
        {
            logger.Debug($"[{message.Txid,-8}s] Handling ChaincodeMessage of type: {message.Type}, handler state {State}");
            if (message.Type == ChaincodeMessage.Types.Type.Keepalive)
            {
                logger.Debug($"[{message.Txid,-8}s] Received KEEPALIVE: nothing to do");
                return;
            }

            switch (State)
            {
            case CCState.CREATED:
                HandleCreated(message);
                break;

            case CCState.ESTABLISHED:
                HandleEstablished(message);
                break;

            case CCState.READY:
                await HandleReadyAsync(message, token).ConfigureAwait(false);

                break;

            default:
                logger.Warning($"[{message.Txid,-8}s] Received {message.Type}: cannot handle");
                break;
            }
        }
        Chat_handles_the_message_with_message_queue_when_state_is_ready_and_response_is_of_type_response()
        {
            var responseMessage = new ChaincodeMessage
            {
                Type = ChaincodeMessage.Types.Type.Response
            };

            var messageQueueMock = new Mock <IMessageQueue>();

            messageQueueMock.Setup(m => m.HandleMessageResponse(responseMessage));

            var messageQueueFactoryMock = new Mock <IMessageQueueFactory>();

            messageQueueFactoryMock.Setup(m => m.Create(It.IsAny <global::Thinktecture.HyperledgerFabric.Chaincode.NET.Handler.Handler>()))
            .Returns(messageQueueMock.Object);

            var(responseStreamMock, chaincodeSupportClientMock, chaincodeSupportClientFactoryMock, handler) =
                CreateHandlerMockWithStreamExpectation(responseMessage, null, messageQueueFactoryMock.Object);

            handler.State = States.Ready;

            await handler.Chat(null);

            chaincodeSupportClientFactoryMock.VerifyAll();
            chaincodeSupportClientMock.VerifyAll();
            responseStreamMock.VerifyAll();
            messageQueueFactoryMock.VerifyAll();
            messageQueueMock.VerifyAll();
        }
        Chat_sends_an_error_to_peer_when_it_should_handle_a_transaction_but_input_parameter_is_incorrect()
        {
            var responseMessage = new ChaincodeMessage
            {
                Type      = ChaincodeMessage.Types.Type.Transaction,
                Payload   = "unittest".ToByteString(),
                ChannelId = "ChannelId",
                Txid      = "TxId"
            };

            var conversationStartMessage = new ChaincodeMessage();
            var requestStreamMock        = new Mock <IClientStreamWriter <ChaincodeMessage> >();

            requestStreamMock.Setup(m => m.WriteAsync(conversationStartMessage)).Returns(Task.CompletedTask);
            requestStreamMock.Setup(m => m.WriteAsync(It.Is(
                                                          (ChaincodeMessage message) => message.Type == ChaincodeMessage.Types.Type.Error &&
                                                          message.Txid == "TxId" &&
                                                          message.ChannelId == "ChannelId" &&
                                                          message.Payload == responseMessage.Payload
                                                          ))).Returns(Task.CompletedTask);

            var(responseStreamMock, chaincodeSupportClientMock, chaincodeSupportClientFactoryMock, handler) =
                CreateHandlerMockWithStreamExpectation(responseMessage, requestStreamMock.Object);

            handler.State = States.Ready;

            await handler.Chat(conversationStartMessage);

            chaincodeSupportClientFactoryMock.VerifyAll();
            chaincodeSupportClientMock.VerifyAll();
            responseStreamMock.VerifyAll();
            requestStreamMock.VerifyAll();
        }
        public async void Start_calls_the_handlers_chat_method()
        {
            var options = Options.Create(new ChaincodeSettings
            {
                CORE_PEER_ADDRESS      = "example.test:9999",
                CORE_CHAINCODE_ID_NAME = "unittest"
            });

            var message = new ChaincodeMessage
            {
                Type    = ChaincodeMessage.Types.Type.Register,
                Payload = new ChaincodeID {
                    Name = "unittest"
                }.ToByteString()
            };

            var handlerMock = new Mock <IHandler>();

            handlerMock.Setup(m => m.Chat(message)).Returns(Task.CompletedTask);

            var handlerFactoryMock = new Mock <IHandlerFactory>();

            handlerFactoryMock.Setup(m => m.Create("example.test", 9999))
            .Returns(handlerMock.Object);

            var shim   = new Shim(options, new Mock <ILogger <Shim> >().Object, handlerFactoryMock.Object);
            var result = await shim.Start();

            result.Should().BeSameAs(handlerMock.Object);

            handlerFactoryMock.VerifyAll();
            handlerMock.VerifyAll();
        }
Ejemplo n.º 11
0
        public void HandleMessageResponse(ChaincodeMessage response)
        {
            var messageTxContextId = response.ChannelId + response.Txid;

            var message = GetCurrentMessage(messageTxContextId) as dynamic; // TODO: This needs to be done better

            HandleResponseMessage(message, messageTxContextId, response);
        }
        private Task <T> AskPeerAndListen <T>(ChaincodeMessage message, MessageMethod method)
        {
            var taskCompletionSource = new TaskCompletionSource <T>();

            var queueMessage = new QueueMessage <T>(message, method, taskCompletionSource);

            _messageQueue.QueueMessage(queueMessage);

            return(taskCompletionSource.Task);
        }
Ejemplo n.º 13
0
 public static string ToJsonString(this ChaincodeMessage message)
 {
     try
     {
         return(JsonConvert.SerializeObject(message, Formatting.None));
     }
     catch (InvalidProtocolBufferException)
     {
         return($"{{ Type: {message.Type}, TxId: {message.Txid} }}");
     }
 }
        Chat_sends_an_error_to_peer_when_it_should_handle_a_transaction_but_chaincode_invocation_responded_with_unknown_response_code()
        {
            var responseMessage = new ChaincodeMessage
            {
                Type      = ChaincodeMessage.Types.Type.Transaction,
                Payload   = new ChaincodeInput().ToByteString(),
                ChannelId = "ChannelId",
                Txid      = "TxId"
            };

            var chaincodeStub = new Mock <IChaincodeStub>().Object;

            var chaincodeStubFactoryMock = new Mock <IChaincodeStubFactory>();

            chaincodeStubFactoryMock.Setup <IChaincodeStub>(m => m.Create(It.IsAny <global::Thinktecture.HyperledgerFabric.Chaincode.NET.Handler.Handler>(), "ChannelId", "TxId",
                                                                          It.IsAny <ChaincodeInput>(), It.IsAny <SignedProposal>()))
            .Returns(chaincodeStub);

            var response = new Response {
                Status = 0
            };

            var chaincodeMock = new Mock <IChaincode>();

            chaincodeMock.Setup(m => m.Invoke(chaincodeStub))
            .ReturnsAsync(response);

            var conversationStartMessage = new ChaincodeMessage();
            var requestStreamMock        = new Mock <IClientStreamWriter <ChaincodeMessage> >();

            requestStreamMock.Setup(m => m.WriteAsync(conversationStartMessage)).Returns(Task.CompletedTask);
            requestStreamMock.Setup(m => m.WriteAsync(It.Is(
                                                          (ChaincodeMessage message) => message.Type == ChaincodeMessage.Types.Type.Error &&
                                                          message.Txid == "TxId" &&
                                                          message.ChannelId == "ChannelId" &&
                                                          message.Payload.ToStringUtf8().Contains("has not called success or error")
                                                          ))).Returns(Task.CompletedTask);

            var(responseStreamMock, chaincodeSupportClientMock, chaincodeSupportClientFactoryMock, handler) =
                CreateHandlerMockWithStreamExpectation(responseMessage, requestStreamMock.Object, null,
                                                       chaincodeStubFactoryMock.Object, chaincodeMock.Object);

            handler.State = States.Ready;

            await handler.Chat(conversationStartMessage);

            chaincodeSupportClientFactoryMock.VerifyAll();
            chaincodeSupportClientMock.VerifyAll();
            responseStreamMock.VerifyAll();
            chaincodeStubFactoryMock.VerifyAll();
            chaincodeMock.VerifyAll();
            requestStreamMock.VerifyAll();
        }
        public async void Chat_invokes_the_chaincode_when_it_receives_a_transaction_message()
        {
            var responseMessage = new ChaincodeMessage
            {
                Type      = ChaincodeMessage.Types.Type.Transaction,
                Payload   = new ChaincodeInput().ToByteString(),
                ChannelId = "ChannelId",
                Txid      = "TxId"
            };

            var chaincodeStub = new Mock <IChaincodeStub>().Object;

            var chaincodeStubFactoryMock = new Mock <IChaincodeStubFactory>();

            chaincodeStubFactoryMock.Setup <IChaincodeStub>(m => m.Create(It.IsAny <global::Thinktecture.HyperledgerFabric.Chaincode.NET.Handler.Handler>(), "ChannelId", "TxId",
                                                                          It.IsAny <ChaincodeInput>(), It.IsAny <SignedProposal>()))
            .Returns(chaincodeStub);

            var response = new Response {
                Status = (int)ResponseCodes.Ok
            };

            var chaincodeMock = new Mock <IChaincode>();

            chaincodeMock.Setup(m => m.Invoke(chaincodeStub))
            .ReturnsAsync(response);

            var conversationStartMessage = new ChaincodeMessage();
            var requestStreamMock        = new Mock <IClientStreamWriter <ChaincodeMessage> >();

            requestStreamMock.Setup(m => m.WriteAsync(conversationStartMessage)).Returns(Task.CompletedTask);
            requestStreamMock.Setup(m => m.WriteAsync(It.Is(
                                                          (ChaincodeMessage message) => message.Type == ChaincodeMessage.Types.Type.Completed &&
                                                          message.Txid == "TxId" &&
                                                          message.ChannelId == "ChannelId" &&
                                                          message.Payload == response.ToByteString()
                                                          ))).Returns(Task.CompletedTask);

            var(responseStreamMock, chaincodeSupportClientMock, chaincodeSupportClientFactoryMock, handler) =
                CreateHandlerMockWithStreamExpectation(responseMessage, requestStreamMock.Object, null,
                                                       chaincodeStubFactoryMock.Object, chaincodeMock.Object);

            handler.State = States.Ready;

            await handler.Chat(conversationStartMessage);

            chaincodeSupportClientFactoryMock.VerifyAll();
            chaincodeSupportClientMock.VerifyAll();
            responseStreamMock.VerifyAll();
            chaincodeStubFactoryMock.VerifyAll();
            chaincodeMock.VerifyAll();
            requestStreamMock.VerifyAll();
        }
Ejemplo n.º 16
0
 private void HandleCreated(ChaincodeMessage message)
 {
     if (message.Type == ChaincodeMessage.Types.Type.Registered)
     {
         State = CCState.ESTABLISHED;
         logger.Debug($"[{message.Txid,-8}s] Received REGISTERED: moving to established state");
     }
     else
     {
         logger.Warning($"[{message.Txid,-8}s] Received {message.Type}: cannot handle");
     }
 }
Ejemplo n.º 17
0
        private static ChaincodeMessage NewEventMessage(ChaincodeMessage.Types.Type type, string channelId, string txId, ByteString payload, ChaincodeEvent evnt)
        {
            ChaincodeMessage msg = new ChaincodeMessage {
                Type = type, ChannelId = channelId, Txid = txId, Payload = payload
            };

            if (evnt != null)
            {
                msg.ChaincodeEvent = evnt;
            }
            return(msg);
        }
Ejemplo n.º 18
0
 private void HandleEstablished(ChaincodeMessage message)
 {
     if (message.Type == ChaincodeMessage.Types.Type.Ready)
     {
         State = CCState.READY;
         logger.Debug($"[{message.Txid,-8}s] Received READY: ready for invocations");
     }
     else
     {
         logger.Warning($"[{message.Txid,-8}s] Received {message.Type}: cannot handle");
     }
 }
Ejemplo n.º 19
0
 private async Task SendChannelAsync(ChaincodeMessage message, CancellationToken token)
 {
     using (await _channelLock.LockAsync(token).ConfigureAwait(false))
     {
         string key = GetTxKey(message.ChannelId, message.Txid);
         if (!responseChannel.ContainsKey(key))
         {
             throw new InvalidOperationException($"[{message.Txid},-8]sendChannel does not exist");
         }
         logger.Debug($"[{message.Txid},-8.8]Before send");
         await responseChannel[key].EnqueueAsync(message, token).ConfigureAwait(false);
         logger.Debug($"[{message.Txid},-8]After send");
     }
 }
        private ChaincodeMessage NewErrorMessage(ChaincodeMessage message, States state)
        {
            var errorString = $"[{message.ChannelId}-{message.Txid}] Chaincode Handler FSM cannot " +
                              $"handle message ({message.Type}, with payload size {message.Payload.Length} " +
                              $"while in state {state}";

            return(new ChaincodeMessage
            {
                Type = ChaincodeMessage.Types.Type.Error,
                Payload = errorString.ToByteString(),
                Txid = message.Txid,
                ChannelId = message.ChannelId
            });
        }
Ejemplo n.º 21
0
        /**
         *
         * @return Chaincode response packed as payload inside COMPLETE message packed as payload inside RESPONSE message
         */

        public List <ChaincodeMessage> Next()
        {
            ByteString chaincodeResponse = new Protos.Peer.ProposalResponsePackage.Response {
                Status = (int)Protos.Common.Status.Success, Message = "OK"
            }.ToByteString();
            ByteString completePayload = new ChaincodeMessage {
                Type = ChaincodeMessage.Types.Type.Completed, ChannelId = orgMsg.ChannelId, Txid = orgMsg.Txid, Payload = chaincodeResponse
            }.ToByteString();
            List <ChaincodeMessage> list = new List <ChaincodeMessage>();

            list.Add(new ChaincodeMessage {
                Type = ChaincodeMessage.Types.Type.Response, ChannelId = orgMsg.ChannelId, Txid = orgMsg.Txid, Payload = completePayload
            });
            return(list);
        }
Ejemplo n.º 22
0
        /**
         * Check incoming message
         * If message type is PUT_STATE and payload equal to passed in constructor
         * @param msg message from chaincode
         * @return
         */
        public bool Expected(ChaincodeMessage msg)
        {
            orgMsg = msg;
            PutState putMsg;

            try
            {
                putMsg = PutState.Parser.ParseFrom(msg.Payload);
            }
            catch (InvalidProtocolBufferException)
            {
                return(false);
            }

            return(val.Equals(putMsg.Value.ToStringUtf8()) && msg.Type == ChaincodeMessage.Types.Type.PutState);
        }
        public async void Chat_switches_state_from_created_to_established_when_it_receives_a_registered_response()
        {
            var responseMessage = new ChaincodeMessage
            {
                Type = ChaincodeMessage.Types.Type.Registered
            };

            var(responseStreamMock, chaincodeSupportClientMock, chaincodeSupportClientFactoryMock, handler) =
                CreateHandlerMockWithStreamExpectation(responseMessage);

            await handler.Chat(null);

            chaincodeSupportClientFactoryMock.VerifyAll();
            chaincodeSupportClientMock.VerifyAll();
            responseStreamMock.VerifyAll();
            handler.State.Should().Be(States.Established);
        }
Ejemplo n.º 24
0
        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");
        }
Ejemplo n.º 25
0
        private void HandleResponseMessage <T>(
            QueueMessage <T> message,
            string messageTxContextId,
            ChaincodeMessage response
            )
        {
            try
            {
                var parsedResponse = _handler.ParseResponse(response, message.Method);
                message.Success((T)parsedResponse);
            }
            catch (Exception ex)
            {
                message.Fail(ex);
            }

            RemoveCurrentAndSendNextMessage(messageTxContextId);
        }
Ejemplo n.º 26
0
        /**
         * Check incoming message
         * If message type is PUT_STATE_METADATA and payload match to passed in constructor
         * @param msg message from chaincode
         * @return
         */
        public bool Expected(ChaincodeMessage msg)
        {
            orgMsg = msg;
            PutStateMetadata psm;

            try
            {
                psm = PutStateMetadata.Parser.ParseFrom(msg.Payload);
            }
            catch (InvalidProtocolBufferException)
            {
                return(false);
            }

            StateBasedEndorsement msgSbe = new StateBasedEndorsement(psm.Metadata.Value.ToByteArray());

            return(msg.Type == ChaincodeMessage.Types.Type.PutStateMetadata && ChaincodeStub.VALIDATION_PARAMETER.Equals(psm.Metadata.Metakey) && msgSbe.ListOrgs().Count == val.ListOrgs().Count);
        }
        public object ParseResponse(ChaincodeMessage response, MessageMethod messageMethod)
        {
            if (response.Type == ChaincodeMessage.Types.Type.Response)
            {
                _logger.LogInformation(
                    $"[{response.ChannelId}-{response.Txid}] Received {messageMethod} successful response");

                switch (messageMethod)
                {
                case MessageMethod.GetStateByRange:
                case MessageMethod.GetQueryResult:
                    return(new StateQueryIterator(this, response.ChannelId, response.Txid,
                                                  QueryResponse.Parser.ParseFrom(response.Payload)));

                case MessageMethod.GetHistoryForKey:
                    return(new HistoryQueryIterator(this, response.ChannelId, response.Txid,
                                                    QueryResponse.Parser.ParseFrom(response.Payload)));

                case MessageMethod.QueryStateNext:
                case MessageMethod.QueryStateClose:
                    return(QueryResponse.Parser.ParseFrom(response.Payload));

                case MessageMethod.InvokeChaincode:
                    return(ChaincodeMessage.Parser.ParseFrom(response.Payload));

                default:
                    return(response.Payload);
                }
            }

            if (response.Type == ChaincodeMessage.Types.Type.Error)
            {
                _logger.LogInformation(
                    $"[{response.ChannelId}-{response.Txid}] Received {messageMethod} error response");
                throw new Exception(response.Payload.ToStringUtf8());
            }

            var errorMessage = $"[{response.ChannelId}-{response.Txid}] Received incorrect chaincode " +
                               $"in response to the {messageMethod} call: " +
                               $"type={response.Type}, expecting \"RESPONSE\"";

            _logger.LogInformation(errorMessage);
            throw new Exception(errorMessage);
        }
Ejemplo n.º 28
0
        public async void Directly_sends_a_single_message()
        {
            var handlerMock = new Mock <IHandler>();
            var streamMock  = new Mock <IClientStreamWriter <ChaincodeMessage> >();

            handlerMock.SetupGet(m => m.WriteStream).Returns(streamMock.Object);

            var sut = new MessageQueue(handlerMock.Object, new Mock <ILogger <MessageQueue> >().Object);

            var chaincodeMessage = new ChaincodeMessage
            {
                Txid      = "bar",
                ChannelId = "foo"
            };

            await sut.QueueMessage(new QueueMessage(chaincodeMessage, 0));

            streamMock.Verify(m => m.WriteAsync(chaincodeMessage), Times.Once);
        }
Ejemplo n.º 29
0
        private async Task <ByteString> InvokeChaincodeSupportAsync(ChaincodeMessage message, CancellationToken token)
        {
            string channelId = message.ChannelId;
            string txId      = message.Txid;

            try
            {
                // create a new response channel
                AsyncProducerConsumerQueue <ChaincodeMessage> respChannel = AquireResponseChannelForTx(channelId, txId);

                // send the message
                await QueueOutboundChaincodeMessageAsync(message, token).ConfigureAwait(false);

                // wait for response
                ChaincodeMessage response = await ReceiveChannelAsync(respChannel, token).ConfigureAwait(false);

                logger.Debug($"[{txId},-8]{response.Type} response received.");

                // handle response
                switch (response.Type)
                {
                case ChaincodeMessage.Types.Type.Response:
                    logger.Debug($"[{txId},-8]Successful response received.");
                    return(response.Payload);

                case ChaincodeMessage.Types.Type.Error:
                    string error = $"[{txId},-8]Unsuccessful response received.";
                    logger.Error(error);
                    throw new InvalidOperationException(error);

                default:
                    string error2 = $"[{txId},-8]Unexpected {response.Type} response received. Expected {ChaincodeMessage.Types.Type.Response} or {ChaincodeMessage.Types.Type.Error}.";
                    logger.Error(error2);
                    throw new InvalidOperationException(error2);
                }
            }
            finally
            {
                ReleaseResponseChannelForTx(channelId, txId);
            }
        }
Ejemplo n.º 30
0
 // handleTransaction Handles request to execute a transaction.
 private void InternalHandleTransaction(ChaincodeMessage message, CancellationToken token, Func <IChaincodeStub, CancellationToken, Task <Response> > action)
 {
     Task.Run(async() =>
     {
         try
         {
             // Get the function and args from Payload
             ChaincodeInput input = ChaincodeInput.Parser.ParseFrom(message.Payload);
             // Mark as a transaction (allow put/del state)
             MarkIsTransaction(message.ChannelId, message.Txid, true);
             // Create the ChaincodeStub which the chaincode can use to
             // callback
             IChaincodeStub stub = new ChaincodeStub(message.ChannelId, message.Txid, this, input.Args.ToList(), message.Proposal);
             // Call chaincode's init
             Response result = await action(stub, token).ConfigureAwait(false);
             if (result.Status >= Status.INTERNAL_SERVER_ERROR)
             {
                 // Send ERROR with entire result.Message as payload
                 logger.Error($"[{message.Txid},-8]Init failed. Sending {ChaincodeMessage.Types.Type.Error}");
                 await QueueOutboundChaincodeMessageAsync(NewErrorEventMessage(message.ChannelId, message.Txid, result.Message, stub.Event), token).ConfigureAwait(false);
             }
             else
             {
                 // Send COMPLETED with entire result as payload
                 logger.Debug($"[{message.Txid},-8]Init succeeded. Sending {ChaincodeMessage.Types.Type.Completed}");
                 await QueueOutboundChaincodeMessageAsync(NewCompletedEventMessage(message.ChannelId, message.Txid, result, stub.Event), token).ConfigureAwait(false);
             }
         }
         catch (Exception e)
         {
             logger.Error(e, $"[{message.Txid},-8]Init failed. Sending {ChaincodeMessage.Types.Type.Error}");
             await QueueOutboundChaincodeMessageAsync(NewErrorEventMessage(message.ChannelId, message.Txid, e), token).ConfigureAwait(false);
         }
         finally
         {
             // delete isTransaction entry
             DeleteIsTransaction(message.ChannelId, message.Txid);
         }
     }, token);
 }