示例#1
0
        public void Execute_Prepared_With_Param_Test()
        {
            using (var connection = CreateConnection())
            {
                connection.Open().Wait();

                var prepareRequest = new PrepareRequest(GetSerializer(), "SELECT * FROM system.local WHERE key = ?", null, null);
                var task           = connection.Send(prepareRequest);
                var prepareOutput  = ValidateResult <OutputPrepared>(task.Result);

                var options = new QueryProtocolOptions(ConsistencyLevel.One, new object[] { "local" }, false, 100, null, ConsistencyLevel.Any);

                var executeRequest = new ExecuteRequest(
                    GetSerializer(),
                    prepareOutput.QueryId,
                    null,
                    new ResultMetadata(prepareOutput.ResultMetadataId, prepareOutput.ResultRowsMetadata),
                    options,
                    false,
                    null);

                task = connection.Send(executeRequest);
                var output = ValidateResult <OutputRows>(task.Result);

                var rows = output.RowSet.ToList();
                Assert.Greater(rows.Count, 0);
                Assert.NotNull(rows[0].GetValue <string>("key"), "It should contain a key column value");
            }
        }
示例#2
0
        public void Execute_Prepared_Test()
        {
            using (var connection = CreateConnection())
            {
                connection.Open().Wait();

                //Prepare a query
                var prepareRequest = new PrepareRequest(GetSerializer(), BasicQuery, null, null);
                var task           = connection.Send(prepareRequest);
                var prepareOutput  = ValidateResult <OutputPrepared>(task.Result);

                //Execute the prepared query
                var executeRequest = new ExecuteRequest(
                    GetSerializer(),
                    prepareOutput.QueryId,
                    null,
                    new ResultMetadata(prepareOutput.ResultMetadataId, prepareOutput.ResultRowsMetadata),
                    QueryProtocolOptions.Default,
                    false,
                    null);
                task = connection.Send(executeRequest);
                var output = ValidateResult <OutputRows>(task.Result);
                var rs     = output.RowSet;
                var rows   = rs.ToList();
                Assert.Greater(rows.Count, 0);
                Assert.True(rows[0].GetValue <string>("key") != null, "It should contain a key column");
            }
        }
示例#3
0
        public async Task Acceptor_OlderPrepareRequest_Rejected()
        {
            var acceptor = new Acceptor <string>("sample-acceptor");

            var winningProposal = new Proposal <string>(100, "192.168.0.100");
            var winningRequest  = new PrepareRequest(winningProposal);
            var winningResponse = await acceptor.ReceivePrepareRequestAsync(winningRequest)
                                  .ConfigureAwait(false);

            var newProposal = new Proposal <string>(99, "192.168.0.99");
            var newRequest  = new PrepareRequest(newProposal);
            var newResponse = await acceptor.ReceivePrepareRequestAsync(newRequest)
                              .ConfigureAwait(false);



            Assert.NotNull(newResponse);
            Assert.False(newResponse.Promised);
            Assert.NotNull(newResponse.AcceptedProposal);
            Assert.IsType <Proposal <string> >(newResponse.AcceptedProposal);
            Assert.IsNotType <Proposal <object> >(newResponse.AcceptedProposal);
            Assert.IsNotType <Proposal <int> >(newResponse.AcceptedProposal);
            Assert.Equal(winningProposal, winningResponse.AcceptedProposal);


            Assert.Equal(100, acceptor.PromisedProposalNumber);
            Assert.NotNull(acceptor.PromisedProposal.As <Proposal <string> >());
            Assert.Equal("192.168.0.100", acceptor.PromisedProposal.As <Proposal <string> >() !.Value);

            Assert.Equal($"sample-acceptor: Proposal {{ Number = 100, Value = 192.168.0.100 }}", acceptor.ToString());
        }
示例#4
0
        public async Task InitiatePrepareForMoveResourcesAsync()
        {
            using (var context = MockContext.Start(this.GetType()))
            {
                this.TestHelper.Initialize(context);
                var client = this.TestHelper.ResourceMoverServiceClient;

                var prepareRequest = new PrepareRequest()
                {
                    MoveResources = new List <string>()
                    {
                        VirtualMachineId,
                        NetworkInterfaceId,
                        PublicIpId,
                        LoadBalancerId,
                        VirtualNetworkId,
                        NetworkSecurityGroupId
                    },
                    MoveResourceInputType = MoveResourceInputType.MoveResourceSourceId
                };

                var operationStatus =
                    (await client.MoveCollections.PrepareWithHttpMessagesAsync(
                         MoveCollectionResourceGroup,
                         MoveCollectionName,
                         prepareRequest)).Body;

                Assert.True(OperationStatusSucceeded.Equals(
                                operationStatus.Status,
                                StringComparison.OrdinalIgnoreCase),
                            "Prepare operation should have succeeded.");
            }
        }
示例#5
0
 public override Task <Empty> Prepare(PrepareRequest request, ServerCallContext context)
 {
     _bus.Publish(new ElectionMessage.Prepare(
                      Uuid.FromDto(request.ServerId).ToGuid(),
                      new IPEndPoint(IPAddress.Parse(request.ServerInternalHttp.Address), (int)request.ServerInternalHttp.Port),
                      request.View));
     return(EmptyResult);
 }
 private async Task SendPrepareAsync(Guid serverId, EndPoint serverHttpEndPoint, int view, DateTime deadline)
 {
     var request = new PrepareRequest {
         ServerId   = Uuid.FromGuid(serverId).ToDto(),
         ServerHttp = new GossipEndPoint(serverHttpEndPoint.GetHost(), (uint)serverHttpEndPoint.GetPort()),
         View       = view
     };
     await _electionsClient.PrepareAsync(request, deadline : deadline.ToUniversalTime());
 }
示例#7
0
        public void OnPrepareRequestReceived(ConsensusPayload payload, PrepareRequest request)
        {
            if (!_consensus.TryGetValue(ConsensusMessageType.PrepareRequest, out var state))
            {
                state = new ConsensusPhaseStat();
            }

            state.Add(payload);
        }
 private async Task SendPrepareAsync(Guid serverId, IPEndPoint serverInternalHttp, int view, DateTime deadline)
 {
     var request = new PrepareRequest {
         ServerId           = Uuid.FromGuid(serverId).ToDto(),
         ServerInternalHttp = new EndPoint(serverInternalHttp.Address.ToString(), (uint)serverInternalHttp.Port),
         View = view
     };
     await _electionsClient.PrepareAsync(request, deadline : deadline.ToUniversalTime());
 }
示例#9
0
        public virtual Task <PrepareResponse> ReceivePrepareRequestAsync(PrepareRequest request)
        {
            if (request.Proposal.Number > PromisedProposalNumber)
            {
                _promisedProposal.Value = request.Proposal;
                return(Task.FromResult(new PrepareResponse(true, PromisedProposal)));
            }

            return(Task.FromResult(new PrepareResponse(false, PromisedProposal)));
        }
        public async Task <ActionResult <StringResult> > Prepare([FromBody] PrepareRequest request)
        {
            if (!await _configRepository.IsNeedInstallAsync())
            {
                return(Unauthorized());
            }

            var(success, errorMessage) =
                await _administratorRepository.InsertValidateAsync(request.UserName, request.AdminPassword, request.Email, request.Mobile);

            if (!success)
            {
                return(this.Error(errorMessage));
            }

            if (_settingsManager.Containerized)
            {
                if (_settingsManager.DatabaseType == DatabaseType.SQLite)
                {
                    var filePath = PathUtils.Combine(_settingsManager.ContentRootPath,
                                                     Constants.LocalDbContainerVirtualPath.Substring(1));
                    if (!FileUtils.IsFileExists(filePath))
                    {
                        await FileUtils.WriteTextAsync(filePath, string.Empty);
                    }
                }
            }
            else
            {
                if (request.DatabaseType == DatabaseType.SQLite)
                {
                    var filePath = PathUtils.Combine(_settingsManager.ContentRootPath, Constants.LocalDbHostVirtualPath.Substring(1));
                    if (!FileUtils.IsFileExists(filePath))
                    {
                        await FileUtils.WriteTextAsync(filePath, string.Empty);
                    }
                }

                var databaseConnectionString = InstallUtils.GetDatabaseConnectionString(request.DatabaseType, request.DatabaseHost, request.IsDatabaseDefaultPort, TranslateUtils.ToInt(request.DatabasePort), request.DatabaseUserName, request.DatabasePassword, request.DatabaseName);
                var redisConnectionString    = string.Empty;
                if (request.IsRedis)
                {
                    redisConnectionString = InstallUtils.GetRedisConnectionString(request.RedisHost, request.IsRedisDefaultPort, request.RedisPort, request.IsSsl, request.RedisPassword);
                }

                _settingsManager.SaveSettings(request.IsProtectData, false, false, request.DatabaseType, databaseConnectionString,
                                              redisConnectionString, string.Empty, null, null);
            }

            return(new StringResult
            {
                Value = _settingsManager.SecurityKey
            });
        }
示例#11
0
 public void PrepareQuery()
 {
     using (var connection = CreateConnection())
     {
         connection.Init();
         var request = new PrepareRequest(connection.ProtocolVersion, "SELECT * FROM system.schema_keyspaces");
         var task    = connection.Send(request);
         task.Wait();
         Assert.AreEqual(TaskStatus.RanToCompletion, task.Status);
         var output = ValidateResult <OutputPrepared>(task.Result);
     }
 }
 public void Prepare_Query()
 {
     using (var connection = CreateConnection())
     {
         connection.Open().Wait();
         var request = new PrepareRequest(BasicQuery);
         var task    = connection.Send(request);
         task.Wait();
         Assert.AreEqual(TaskStatus.RanToCompletion, task.Status);
         ValidateResult <OutputPrepared>(task.Result);
     }
 }
示例#13
0
        /// <inheritdoc />
        public async Task <PreparedStatement> PrepareAsync(string query, IDictionary <string, byte[]> customPayload)
        {
            var request = new PrepareRequest(query)
            {
                Payload = customPayload
            };
            var handler = new RequestHandler <PreparedStatement>(this, _serializer, request);
            var ps      = await handler.Send().ConfigureAwait(false);

            await SetPrepareTableInfo(ps).ConfigureAwait(false);

            return(ps);
        }
示例#14
0
        public void Prepare_With_Keyspace_On_Lower_Protocol_Version_Should_Ignore_Keyspace()
        {
            const string query      = "SELECT col1, col2 FROM table1";
            var          serializer = new SerializerManager(ProtocolVersion.V2).GetCurrentSerializer();
            var          request    = new PrepareRequest(serializer, query, "my_keyspace", null);

            // The request only contains the query
            var buffer = GetBodyBuffer(request, serializer);

            var queryLength = BeConverter.ToInt32(buffer);

            Assert.AreEqual(query.Length, queryLength);
            Assert.AreEqual(4 + queryLength, buffer.Length);
        }
        public override async Task <Empty> Prepare(PrepareRequest request, ServerCallContext context)
        {
            var user = context.GetHttpContext().User;

            if (!await _authorizationProvider.CheckAccessAsync(user, PrepareOperation, context.CancellationToken).ConfigureAwait(false))
            {
                throw AccessDenied();
            }
            _bus.Publish(new ElectionMessage.Prepare(
                             Uuid.FromDto(request.ServerId).ToGuid(),
                             new IPEndPoint(IPAddress.Parse(request.ServerInternalHttp.Address), (int)request.ServerInternalHttp.Port),
                             request.View));
            return(EmptyResult);
        }
示例#16
0
 public void PrepareResponseErrorFaultsTask()
 {
     using (var connection = CreateConnection())
     {
         connection.Init();
         var request = new PrepareRequest(connection.ProtocolVersion, "SELECT WILL FAIL");
         var task    = connection.Send(request);
         task.ContinueWith(t =>
         {
             Assert.AreEqual(TaskStatus.Faulted, t.Status);
             Assert.IsInstanceOf <SyntaxError>(t.Exception.InnerException);
         }, TaskContinuationOptions.ExecuteSynchronously).Wait();
     }
 }
示例#17
0
 public void Prepare_ResponseError_Faults_Task()
 {
     using (var connection = CreateConnection())
     {
         connection.Open().Wait();
         var request = new PrepareRequest(GetSerializer(), "SELECT WILL FAIL", null, null);
         var task    = connection.Send(request);
         task.ContinueWith(t =>
         {
             Assert.AreEqual(TaskStatus.Faulted, t.Status);
             Assert.NotNull(t.Exception);
             Assert.IsInstanceOf <SyntaxError>(t.Exception.InnerException);
         }, TaskContinuationOptions.ExecuteSynchronously).Wait();
     }
 }
        public PrepareResponse Packet_Prepare(PrepareRequest req)
        {
            if (!_blender.TryPrepare(req.Version))
            {
                return new PrepareResponse()
                       {
                           Message = $"Failed to prepare version {req.Version}",
                           Success = false
                       }
            }
            ;

            return(new PrepareResponse()
            {
                Success = true
            });
        }
        /// <summary>
        /// Runs proxy forwarding requests to downstream server.
        /// </summary>
        /// <param name="app">
        ///     The application builder.
        /// </param>
        /// <param name="handleProxyRequest">
        ///     A delegate that can resolve the destination Uri.
        /// </param>
        /// <param name="prepareRequest">
        ///     A delegate to allow modification of the request prior to
        ///     forwarding.
        /// </param>
        public static void RunProxy(
            this IApplicationBuilder app,
            HandleProxyRequest handleProxyRequest,
            PrepareRequest prepareRequest = null)
        {
            if (app == null)
            {
                throw new ArgumentNullException(nameof(app));
            }

            var proxyOptions = new ProxyOptions
            {
                HandleProxyRequest = handleProxyRequest,
                PrepareRequest     = prepareRequest
            };

            app.UseMiddleware <ProxyMiddleware>(proxyOptions);
        }
示例#20
0
        public void Execute_Prepared_Test()
        {
            using (var connection = CreateConnection())
            {
                connection.Open().Wait();

                //Prepare a query
                var prepareRequest = new PrepareRequest("SELECT * FROM system.local");
                var task           = connection.Send(prepareRequest);
                var prepareOutput  = ValidateResult <OutputPrepared>(task.Result);

                //Execute the prepared query
                var executeRequest = new ExecuteRequest(GetLatestProtocolVersion(), prepareOutput.QueryId, null, false, QueryProtocolOptions.Default);
                task = connection.Send(executeRequest);
                var output = ValidateResult <OutputRows>(task.Result);
                var rs     = output.RowSet;
                var rows   = rs.ToList();
                Assert.Greater(rows.Count, 0);
                Assert.True(rows[0].GetValue <string>("key") != null, "It should contain a key column");
            }
        }
示例#21
0
        public void ExecutePreparedTest()
        {
            using (var connection = CreateConnection())
            {
                connection.Init();

                //Prepare a query
                var prepareRequest = new PrepareRequest(connection.ProtocolVersion, "SELECT * FROM system.schema_keyspaces");
                var task           = connection.Send(prepareRequest);
                var prepareOutput  = ValidateResult <OutputPrepared>(task.Result);

                //Execute the prepared query
                var executeRequest = new ExecuteRequest(connection.ProtocolVersion, prepareOutput.QueryId, null, false, QueryProtocolOptions.Default);
                task = connection.Send(executeRequest);
                var output = ValidateResult <OutputRows>(task.Result);
                var rs     = output.RowSet;
                var rows   = rs.ToList();
                Assert.Greater(rows.Count, 0);
                Assert.True(rows[0].GetValue <string>("keyspace_name") != null, "It should contain a keyspace name");
            }
        }
示例#22
0
        public void ExecutePreparedWithParamTest()
        {
            using (var connection = CreateConnection())
            {
                connection.Init();

                var prepareRequest = new PrepareRequest(connection.ProtocolVersion, "SELECT * FROM system.schema_columnfamilies WHERE keyspace_name = ?");
                var task           = connection.Send(prepareRequest);
                var prepareOutput  = ValidateResult <OutputPrepared>(task.Result);

                var options = new QueryProtocolOptions(ConsistencyLevel.One, new[] { "system" }, false, 100, null, ConsistencyLevel.Any);

                var executeRequest = new ExecuteRequest(connection.ProtocolVersion, prepareOutput.QueryId, null, false, options);
                task = connection.Send(executeRequest);
                var output = ValidateResult <OutputRows>(task.Result);

                var rows = output.RowSet.ToList();
                Assert.Greater(rows.Count, 0);
                Assert.True(rows[0].GetValue <string>("columnfamily_name") != null, "It should contain a column family name");
            }
        }
示例#23
0
        public void Reset()
        {
            ResetComponents();

            _onStarting  = null;
            _onCompleted = null;

            _responseStarted      = false;
            _keepAlive            = false;
            _autoChunk            = false;
            _applicationException = null;

            ResetFeatureCollection();

            Scheme       = null;
            Method       = null;
            RequestUri   = null;
            PathBase     = null;
            Path         = null;
            QueryString  = null;
            _httpVersion = HttpVersionType.Unknown;
            StatusCode   = 200;
            ReasonPhrase = null;

            var httpConnectionFeature = this as IHttpConnectionFeature;

            httpConnectionFeature.RemoteIpAddress = RemoteEndPoint?.Address;
            httpConnectionFeature.RemotePort      = RemoteEndPoint?.Port ?? 0;

            httpConnectionFeature.LocalIpAddress = LocalEndPoint?.Address;
            httpConnectionFeature.LocalPort      = LocalEndPoint?.Port ?? 0;

            httpConnectionFeature.ConnectionId = ConnectionId;

            PrepareRequest?.Invoke(this);

            _manuallySetRequestAbortToken = null;
            _abortedCts = null;
        }
示例#24
0
        public void Prepare_With_Keyspace_Should_Send_Keyspace_And_Flag()
        {
            const string query    = "QUERY1";
            const string keyspace = "ks1";
            var          request  = new PrepareRequest(RequestHandlerTests.Serializer, query, keyspace, null);

            // The request is composed by: <query><flags>[<keyspace>]
            var buffer = GetBodyBuffer(request);

            var queryLength = BeConverter.ToInt32(buffer);

            Assert.AreEqual(query.Length, queryLength);
            var offset = 4 + queryLength;
            var flags  = (PrepareFlags)BeConverter.ToInt32(buffer, offset);

            offset += 4;
            Assert.True(flags.HasFlag(PrepareFlags.WithKeyspace));
            var keyspaceLength = BeConverter.ToInt16(buffer, offset);

            offset += 2;
            Assert.AreEqual(keyspace.Length, keyspaceLength);
            Assert.AreEqual(keyspace, Encoding.UTF8.GetString(buffer.Skip(offset).Take(keyspaceLength).ToArray()));
        }
示例#25
0
        public async Task NewAcceptor_ReceivePrepareRequest_Promised()
        {
            var acceptor = new Acceptor <string>("sample-acceptor");

            var proposal = new Proposal <string>(1, "192.168.0.1");
            var request  = new PrepareRequest(proposal);
            var response = await acceptor.ReceivePrepareRequestAsync(request)
                           .ConfigureAwait(false);

            Assert.NotNull(response);
            Assert.True(response.Promised);
            Assert.NotNull(response.AcceptedProposal);
            Assert.IsType <Proposal <string> >(response.AcceptedProposal);
            Assert.IsNotType <Proposal <object> >(response.AcceptedProposal);
            Assert.IsNotType <Proposal <int> >(response.AcceptedProposal);
            Assert.Equal(proposal, response.AcceptedProposal);


            Assert.Equal(1, acceptor.PromisedProposalNumber);
            Assert.NotNull(acceptor.PromisedProposal.As <Proposal <string> >());
            Assert.Equal("192.168.0.1", acceptor.PromisedProposal.As <Proposal <string> >() !.Value);

            Assert.Equal($"sample-acceptor: Proposal {{ Number = 1, Value = 192.168.0.1 }}", acceptor.ToString());
        }
示例#26
0
        /// <summary>
        /// This request is used to create create a new Statement with the given query in the Phoenix query server.
        /// </summary>
        public async Task <PrepareResponse> PrepareRequestAsync(string connectionId, string sql, long maxRowsTotal, RequestOptions options)
        {
            PrepareRequest req = new PrepareRequest
            {
                ConnectionId = connectionId,
                Sql          = sql,
                MaxRowsTotal = maxRowsTotal
            };

            WireMessage msg = new WireMessage
            {
                Name           = Constants.WireMessagePrefix + "PrepareRequest",
                WrappedMessage = req.ToByteString()
            };

            using (Response webResponse = await PostRequestAsync(msg.ToByteArray(), options))
            {
                if (webResponse.WebResponse.StatusCode != HttpStatusCode.OK)
                {
                    WireMessage   output = WireMessage.Parser.ParseFrom(webResponse.WebResponse.GetResponseStream());
                    ErrorResponse res    = ErrorResponse.Parser.ParseFrom(output.WrappedMessage);
                    throw new WebException(
                              string.Format(
                                  "PrepareRequestAsync failed! connectionId: {0}, Response code was: {1}, Response body was: {2}",
                                  connectionId,
                                  webResponse.WebResponse.StatusCode,
                                  res.ToString()));
                }
                else
                {
                    WireMessage     output = WireMessage.Parser.ParseFrom(webResponse.WebResponse.GetResponseStream());
                    PrepareResponse res    = PrepareResponse.Parser.ParseFrom(output.WrappedMessage);
                    return(res);
                }
            }
        }
示例#27
0
        public void Reset()
        {
            FrameRequestHeaders?.Reset();
            FrameResponseHeaders?.Reset();

            _onStarting  = null;
            _onCompleted = null;

            _requestProcessingStatus = RequestProcessingStatus.RequestPending;
            _keepAlive            = false;
            _autoChunk            = false;
            _applicationException = null;

            ResetFeatureCollection();

            Scheme       = null;
            Method       = null;
            PathBase     = null;
            Path         = null;
            QueryString  = null;
            _httpVersion = HttpVersionType.Unset;
            StatusCode   = 200;
            ReasonPhrase = null;

            RemoteIpAddress = RemoteEndPoint?.Address;
            RemotePort      = RemoteEndPoint?.Port ?? 0;

            LocalIpAddress      = LocalEndPoint?.Address;
            LocalPort           = LocalEndPoint?.Port ?? 0;
            ConnectionIdFeature = ConnectionId;

            PrepareRequest?.Invoke(this);

            _manuallySetRequestAbortToken = null;
            _abortedCts = null;
        }
示例#28
0
        public void ConsensusService_Primary_Sends_PrepareRequest_After_OnStart()
        {
            TestProbe subscriber = CreateTestProbe();

            var mockConsensusContext = new Mock <IConsensusContext>();
            var mockStore            = new Mock <Store>();

            // context.Reset(): do nothing
            //mockConsensusContext.Setup(mr => mr.Reset()).Verifiable(); // void
            mockConsensusContext.SetupGet(mr => mr.MyIndex).Returns(2); // MyIndex == 2
            mockConsensusContext.SetupGet(mr => mr.BlockIndex).Returns(2);
            mockConsensusContext.SetupGet(mr => mr.PrimaryIndex).Returns(2);
            mockConsensusContext.SetupGet(mr => mr.ViewNumber).Returns(0);
            mockConsensusContext.SetupProperty(mr => mr.Nonce);
            mockConsensusContext.SetupProperty(mr => mr.NextConsensus);
            mockConsensusContext.Object.NextConsensus = UInt160.Zero;
            mockConsensusContext.SetupGet(mr => mr.PreparationPayloads).Returns(new ConsensusPayload[7]);
            mockConsensusContext.SetupGet(mr => mr.CommitPayloads).Returns(new ConsensusPayload[7]);

            int timeIndex  = 0;
            var timeValues = new[] {
                //new DateTime(1968, 06, 01, 0, 0, 15, DateTimeKind.Utc), // For tests here
                new DateTime(1968, 06, 01, 0, 0, 1, DateTimeKind.Utc),                               // For receiving block
                new DateTime(1968, 06, 01, 0, 0, (int)Blockchain.SecondsPerBlock, DateTimeKind.Utc), // For Initialize
                new DateTime(1968, 06, 01, 0, 0, 15, DateTimeKind.Utc),                              // unused
                new DateTime(1968, 06, 01, 0, 0, 15, DateTimeKind.Utc)                               // unused
            };

            //TimeProvider.Current.UtcNow.ToTimestamp().Should().Be(4244941711); //1968-06-01 00:00:15

            Console.WriteLine($"time 0: {timeValues[0].ToString()} 1: {timeValues[1].ToString()} 2: {timeValues[2].ToString()} 3: {timeValues[3].ToString()}");

            //mockConsensusContext.Object.block_received_time = new DateTime(1968, 06, 01, 0, 0, 1, DateTimeKind.Utc);
            //mockConsensusContext.Setup(mr => mr.GetUtcNow()).Returns(new DateTime(1968, 06, 01, 0, 0, 15, DateTimeKind.Utc));

            var timeMock = new Mock <TimeProvider>();

            timeMock.SetupGet(tp => tp.UtcNow).Returns(() => timeValues[timeIndex])
            .Callback(() => timeIndex++);
            //new DateTime(1968, 06, 01, 0, 0, 15, DateTimeKind.Utc));
            TimeProvider.Current = timeMock.Object;

            //public void Log(string message, LogLevel level)
            // TODO: create ILogPlugin for Tests

            /*
             * mockConsensusContext.Setup(mr => mr.Log(It.IsAny<string>(), It.IsAny<LogLevel>()))
             *           .Callback((string message, LogLevel level) => {
             *                           Console.WriteLine($"CONSENSUS LOG: {message}");
             *                                                     }
             *                    );
             */

            // Creating proposed block
            Header header = new Header();

            TestUtils.SetupHeaderWithValues(header, UInt256.Zero, out UInt256 merkRootVal, out UInt160 val160, out uint timestampVal, out uint indexVal, out ulong consensusDataVal, out Witness scriptVal);
            header.Size.Should().Be(109);

            Console.WriteLine($"header {header} hash {header.Hash} timstamp {timestampVal}");

            timestampVal.Should().Be(4244941696); //1968-06-01 00:00:00
                                                  // check basic ConsensusContext
            mockConsensusContext.Object.MyIndex.Should().Be(2);
            //mockConsensusContext.Object.block_received_time.ToTimestamp().Should().Be(4244941697); //1968-06-01 00:00:01

            MinerTransaction minerTx = new MinerTransaction
            {
                Attributes = new TransactionAttribute[0],
                Inputs     = new CoinReference[0],
                Outputs    = new TransactionOutput[0],
                Witnesses  = new Witness[0],
                Nonce      = 42
            };

            PrepareRequest prep = new PrepareRequest
            {
                Nonce             = mockConsensusContext.Object.Nonce,
                NextConsensus     = mockConsensusContext.Object.NextConsensus,
                TransactionHashes = new UInt256[0],
                MinerTransaction  = minerTx //(MinerTransaction)Transactions[TransactionHashes[0]],
            };

            ConsensusPayload prepPayload = new ConsensusPayload
            {
                Version          = 0,
                PrevHash         = mockConsensusContext.Object.PrevHash,
                BlockIndex       = mockConsensusContext.Object.BlockIndex,
                ValidatorIndex   = (ushort)mockConsensusContext.Object.MyIndex,
                ConsensusMessage = prep
            };

            mockConsensusContext.Setup(mr => mr.MakePrepareRequest()).Returns(prepPayload);

            // ============================================================================
            //                      creating ConsensusService actor
            // ============================================================================

            TestActorRef <ConsensusService> actorConsensus = ActorOfAsTestActorRef <ConsensusService>(
                Akka.Actor.Props.Create(() => new ConsensusService(subscriber, subscriber, mockConsensusContext.Object))
                );

            Console.WriteLine("will trigger OnPersistCompleted!");
            actorConsensus.Tell(new Blockchain.PersistCompleted
            {
                Block = new Block
                {
                    Version       = header.Version,
                    PrevHash      = header.PrevHash,
                    MerkleRoot    = header.MerkleRoot,
                    Timestamp     = header.Timestamp,
                    Index         = header.Index,
                    ConsensusData = header.ConsensusData,
                    NextConsensus = header.NextConsensus
                }
            });

            // OnPersist will not launch timer, we need OnStart

            Console.WriteLine("will start consensus!");
            actorConsensus.Tell(new ConsensusService.Start());

            Console.WriteLine("OnTimer should expire!");
            Console.WriteLine("Waiting for subscriber message!");
            // Timer should expire in one second (block_received_time at :01, initialized at :02)

            var answer = subscriber.ExpectMsg <LocalNode.SendDirectly>();

            Console.WriteLine($"MESSAGE 1: {answer}");
            //var answer2 = subscriber.ExpectMsg<LocalNode.SendDirectly>(); // expects to fail!

            // ============================================================================
            //                      finalize ConsensusService actor
            // ============================================================================

            //Thread.Sleep(4000);
            Sys.Stop(actorConsensus);
            TimeProvider.ResetToDefault();

            Assert.AreEqual(1, 1);
        }
示例#29
0
        public void TestSerializeAndDeserializeConsensusContext()
        {
            var consensusContext = new ConsensusContext(null, null)
            {
                Block = new Block
                {
                    PrevHash      = Blockchain.GenesisBlock.Hash,
                    Index         = 1,
                    Timestamp     = 4244941711,
                    NextConsensus = UInt160.Parse("5555AAAA5555AAAA5555AAAA5555AAAA5555AAAA"),
                    ConsensusData = new ConsensusData
                    {
                        PrimaryIndex = 6
                    }
                },
                ViewNumber = 2,
                Validators = new ECPoint[7]
                {
                    ECPoint.Parse("02486fd15702c4490a26703112a5cc1d0923fd697a33406bd5a1c00e0013b09a70", Cryptography.ECC.ECCurve.Secp256r1),
                    ECPoint.Parse("024c7b7fb6c310fccf1ba33b082519d82964ea93868d676662d4a59ad548df0e7d", Cryptography.ECC.ECCurve.Secp256r1),
                    ECPoint.Parse("02aaec38470f6aad0042c6e877cfd8087d2676b0f516fddd362801b9bd3936399e", Cryptography.ECC.ECCurve.Secp256r1),
                    ECPoint.Parse("02ca0e27697b9c248f6f16e085fd0061e26f44da85b58ee835c110caa5ec3ba554", Cryptography.ECC.ECCurve.Secp256r1),
                    ECPoint.Parse("02df48f60e8f3e01c48ff40b9b7f1310d7a8b2a193188befe1c2e3df740e895093", Cryptography.ECC.ECCurve.Secp256r1),
                    ECPoint.Parse("03b209fd4f53a7170ea4444e0cb0a6bb6a53c2bd016926989cf85f9b0fba17a70c", Cryptography.ECC.ECCurve.Secp256r1),
                    ECPoint.Parse("03b8d9d5771d8f513aa0869b9cc8d50986403b78c6da36890638c3d46a5adce04a", Cryptography.ECC.ECCurve.Secp256r1)
                },
                MyIndex = -1
            };
            var testTx1 = TestUtils.CreateRandomHashTransaction();
            var testTx2 = TestUtils.CreateRandomHashTransaction();

            int txCountToInlcude = 256;

            consensusContext.TransactionHashes = new UInt256[txCountToInlcude];

            Transaction[] txs = new Transaction[txCountToInlcude];
            for (int i = 0; i < txCountToInlcude; i++)
            {
                txs[i] = TestUtils.CreateRandomHashTransaction();
                consensusContext.TransactionHashes[i] = txs[i].Hash;
            }
            // consensusContext.TransactionHashes = new UInt256[2] {testTx1.Hash, testTx2.Hash};
            consensusContext.Transactions = txs.ToDictionary(p => p.Hash);

            consensusContext.PreparationPayloads = new ConsensusPayload[consensusContext.Validators.Length];
            var prepareRequestMessage = new PrepareRequest
            {
                TransactionHashes = consensusContext.TransactionHashes,
                Timestamp         = 23
            };

            consensusContext.PreparationPayloads[6] = MakeSignedPayload(consensusContext, prepareRequestMessage, 6, new[] { (byte)'3', (byte)'!' });
            consensusContext.PreparationPayloads[0] = MakeSignedPayload(consensusContext, new PrepareResponse {
                PreparationHash = consensusContext.PreparationPayloads[6].Hash
            }, 0, new[] { (byte)'t', (byte)'e' });
            consensusContext.PreparationPayloads[1] = MakeSignedPayload(consensusContext, new PrepareResponse {
                PreparationHash = consensusContext.PreparationPayloads[6].Hash
            }, 1, new[] { (byte)'s', (byte)'t' });
            consensusContext.PreparationPayloads[2] = null;
            consensusContext.PreparationPayloads[3] = MakeSignedPayload(consensusContext, new PrepareResponse {
                PreparationHash = consensusContext.PreparationPayloads[6].Hash
            }, 3, new[] { (byte)'1', (byte)'2' });
            consensusContext.PreparationPayloads[4] = null;
            consensusContext.PreparationPayloads[5] = null;

            consensusContext.CommitPayloads = new ConsensusPayload[consensusContext.Validators.Length];
            using (SHA256 sha256 = SHA256.Create())
            {
                consensusContext.CommitPayloads[3] = MakeSignedPayload(consensusContext, new Commit {
                    Signature = sha256.ComputeHash(testTx1.Hash.ToArray())
                }, 3, new[] { (byte)'3', (byte)'4' });
                consensusContext.CommitPayloads[6] = MakeSignedPayload(consensusContext, new Commit {
                    Signature = sha256.ComputeHash(testTx2.Hash.ToArray())
                }, 3, new[] { (byte)'6', (byte)'7' });
            }

            consensusContext.Block.Timestamp = TimeProvider.Current.UtcNow.ToTimestamp();

            consensusContext.ChangeViewPayloads    = new ConsensusPayload[consensusContext.Validators.Length];
            consensusContext.ChangeViewPayloads[0] = MakeSignedPayload(consensusContext, new ChangeView {
                ViewNumber = 1, Timestamp = 6
            }, 0, new[] { (byte)'A' });
            consensusContext.ChangeViewPayloads[1] = MakeSignedPayload(consensusContext, new ChangeView {
                ViewNumber = 1, Timestamp = 5
            }, 1, new[] { (byte)'B' });
            consensusContext.ChangeViewPayloads[2] = null;
            consensusContext.ChangeViewPayloads[3] = MakeSignedPayload(consensusContext, new ChangeView {
                ViewNumber = 1, Timestamp = uint.MaxValue
            }, 3, new[] { (byte)'C' });
            consensusContext.ChangeViewPayloads[4] = null;
            consensusContext.ChangeViewPayloads[5] = null;
            consensusContext.ChangeViewPayloads[6] = MakeSignedPayload(consensusContext, new ChangeView {
                ViewNumber = 1, Timestamp = 1
            }, 6, new[] { (byte)'D' });

            consensusContext.LastChangeViewPayloads = new ConsensusPayload[consensusContext.Validators.Length];

            var copiedContext = TestUtils.CopyMsgBySerialization(consensusContext, new ConsensusContext(null, null));

            copiedContext.Block.PrevHash.Should().Be(consensusContext.Block.PrevHash);
            copiedContext.Block.Index.Should().Be(consensusContext.Block.Index);
            copiedContext.ViewNumber.Should().Be(consensusContext.ViewNumber);
            copiedContext.Validators.ShouldAllBeEquivalentTo(consensusContext.Validators);
            copiedContext.MyIndex.Should().Be(consensusContext.MyIndex);
            copiedContext.Block.ConsensusData.PrimaryIndex.Should().Be(consensusContext.Block.ConsensusData.PrimaryIndex);
            copiedContext.Block.Timestamp.Should().Be(consensusContext.Block.Timestamp);
            copiedContext.Block.NextConsensus.Should().Be(consensusContext.Block.NextConsensus);
            copiedContext.TransactionHashes.ShouldAllBeEquivalentTo(consensusContext.TransactionHashes);
            copiedContext.Transactions.ShouldAllBeEquivalentTo(consensusContext.Transactions);
            copiedContext.Transactions.Values.ShouldAllBeEquivalentTo(consensusContext.Transactions.Values);
            copiedContext.PreparationPayloads.ShouldAllBeEquivalentTo(consensusContext.PreparationPayloads);
            copiedContext.CommitPayloads.ShouldAllBeEquivalentTo(consensusContext.CommitPayloads);
            copiedContext.ChangeViewPayloads.ShouldAllBeEquivalentTo(consensusContext.ChangeViewPayloads);
        }
示例#30
0
 /// <inheritdoc />
 public Task<PreparedStatement> PrepareAsync(string query, IDictionary<string, byte[]> customPayload)
 {
     var request = new PrepareRequest(query)
     {
         Payload = customPayload
     };
     return new RequestHandler<PreparedStatement>(this, _serializer, request)
         .Send()
         .Then(SetPrepareTableInfo);
 }
示例#31
0
 public Task<PreparedStatement> PrepareAsync(string query, IDictionary<string, byte[]> customPayload)
 {
     var request = new PrepareRequest(BinaryProtocolVersion, query)
     {
         Payload = customPayload
     };
     return new RequestHandler<PreparedStatement>(this, request)
         .Send()
         .Continue(SetPrepareTableInfo);
 }
示例#32
0
        private async Task <object> InvokeCore(HttpServiceRequest request, Type resultReturnType, JsonSerializerSettings jsonSettings)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }
            request.TracingData = new TracingData
            {
                HostName         = CurrentApplicationInfo.HostName?.ToUpperInvariant(),
                ServiceName      = CurrentApplicationInfo.Name,
                RequestID        = TracingContext.TryGetRequestID(),
                SpanID           = Guid.NewGuid().ToString("N"), //Each call is new span
                ParentSpanID     = TracingContext.TryGetSpanID(),
                SpanStartTime    = DateTimeOffset.UtcNow,
                AbandonRequestBy = TracingContext.AbandonRequestBy
            };
            PrepareRequest?.Invoke(request);

            while (true)
            {
                var config          = GetConfig();
                var clientCallEvent = EventPublisher.CreateEvent();
                clientCallEvent.TargetService = ServiceName;
                clientCallEvent.RequestId     = request.TracingData?.RequestID;
                clientCallEvent.TargetMethod  = request.Target.MethodName;
                clientCallEvent.SpanId        = request.TracingData?.SpanID;
                clientCallEvent.ParentSpanId  = request.TracingData?.ParentSpanID;

                string responseContent;
                HttpResponseMessage response;
                var nodeAndLoadBalancer = await ServiceDiscovery.GetNode().ConfigureAwait(false); // can throw

                int?effectivePort = GetEffectivePort(nodeAndLoadBalancer.Node, config);
                if (effectivePort == null)
                {
                    throw new ConfigurationException("Cannot access service. Service Port not configured. See tags to find missing configuration", unencrypted: new Tags {
                        { "ServiceName", ServiceName },
                        { "Required configuration key", $"Discovery.{ServiceName}.DefaultPort" }
                    });
                }

                // The URL is only for a nice experience in Fiddler, it's never parsed/used for anything.
                var uri = BuildUri(nodeAndLoadBalancer.Node.Hostname, effectivePort.Value, config) + ServiceName;
                if (request.Target.MethodName != null)
                {
                    uri += $".{request.Target.MethodName}";
                }
                if (request.Target.Endpoint != null)
                {
                    uri += $"/{request.Target.Endpoint}";
                }

                try
                {
                    Log.Debug(_ => _("ServiceProxy: Calling remote service. See tags for details.",
                                     unencryptedTags: new
                    {
                        remoteEndpoint    = nodeAndLoadBalancer.Node.Hostname,
                        remotePort        = effectivePort,
                        remoteServiceName = ServiceName,
                        remoteMethodName  = request.Target.MethodName
                    }));

                    clientCallEvent.TargetHostName = nodeAndLoadBalancer.Node.Hostname;
                    clientCallEvent.TargetPort     = effectivePort.Value;

                    request.Overrides = TracingContext.TryGetOverrides()?.ShallowCloneWithDifferentPreferredEnvironment(nodeAndLoadBalancer.PreferredEnvironment)
                                        ?? new RequestOverrides {
                        PreferredEnvironment = nodeAndLoadBalancer.PreferredEnvironment
                    };
                    string requestContent = _serializationTime.Time(() => JsonConvert.SerializeObject(request, jsonSettings));

                    var httpContent = new StringContent(requestContent, Encoding.UTF8, "application/json");
                    httpContent.Headers.Add(GigyaHttpHeaders.ProtocolVersion, HttpServiceRequest.ProtocolVersion);

                    clientCallEvent.RequestStartTimestamp = Stopwatch.GetTimestamp();
                    try
                    {
                        response = await GetHttpClient(config).PostAsync(uri, httpContent).ConfigureAwait(false);

                        responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
                    }
                    finally
                    {
                        clientCallEvent.ResponseEndTimestamp = Stopwatch.GetTimestamp();
                    }
                    if (response.Headers.TryGetValues(GigyaHttpHeaders.ExecutionTime, out IEnumerable <string> values))
                    {
                        var time = values.FirstOrDefault();
                        if (TimeSpan.TryParse(time, out TimeSpan executionTime))
                        {
                            clientCallEvent.ServerTimeMs = executionTime.TotalMilliseconds;
                        }
                    }
                }
                catch (HttpRequestException ex)
                {
                    Log.Error("The remote service failed to return a valid HTTP response. Continuing to next " +
                              "host. See tags for URL and exception for details.",
                              exception: ex,
                              unencryptedTags: new { uri });
                    _hostFailureCounter.Increment("RequestFailure");
                    clientCallEvent.Exception = ex;
                    EventPublisher.TryPublish(clientCallEvent); // fire and forget!

                    if (nodeAndLoadBalancer.LoadBalancer != null)
                    {
                        nodeAndLoadBalancer.LoadBalancer.ReportUnreachable(nodeAndLoadBalancer.Node, ex);
                        continue;
                    }

                    throw;
                }
                catch (TaskCanceledException ex)
                {
                    _failureCounter.Increment("RequestTimeout");

                    Exception rex = new RemoteServiceException("The request to the remote service exceeded the " +
                                                               "allotted timeout. See the 'RequestUri' property on this exception for the URL that was " +
                                                               "called and the tag 'requestTimeout' for the configured timeout.",
                                                               uri,
                                                               ex,
                                                               unencrypted: new Tags
                    {
                        { "requestTimeout", LastHttpClient?.Timeout.ToString() },
                        { "requestUri", uri }
                    });

                    clientCallEvent.Exception = rex;

                    EventPublisher.TryPublish(clientCallEvent); // fire and forget!
                    throw rex;
                }

                if (response.Headers.Contains(GigyaHttpHeaders.ServerHostname) || response.Headers.Contains(GigyaHttpHeaders.ProtocolVersion))
                {
                    try
                    {
                        if (response.IsSuccessStatusCode)
                        {
                            var returnObj = _deserializationTime.Time(() => JsonConvert.DeserializeObject(responseContent, resultReturnType, jsonSettings));

                            clientCallEvent.ErrCode = 0;
                            EventPublisher.TryPublish(clientCallEvent); // fire and forget!
                            _successCounter.Increment();

                            return(returnObj);
                        }
                        else
                        {
                            Exception remoteException;

                            try
                            {
                                remoteException = _deserializationTime.Time(() => ExceptionSerializer.Deserialize(responseContent));
                            }
                            catch (Exception ex)
                            {
                                _applicationExceptionCounter.Increment("ExceptionDeserializationFailure");

                                throw new RemoteServiceException("The remote service returned a failure response " +
                                                                 "that failed to deserialize.  See the 'RequestUri' property on this exception " +
                                                                 "for the URL that was called, the inner exception for the exact error and the " +
                                                                 "'responseContent' encrypted tag for the original response content.",
                                                                 uri,
                                                                 ex,
                                                                 unencrypted: new Tags {
                                    { "requestUri", uri }
                                },
                                                                 encrypted: new Tags {
                                    { "responseContent", responseContent }
                                });
                            }

                            _applicationExceptionCounter.Increment();

                            clientCallEvent.Exception = remoteException;
                            EventPublisher.TryPublish(clientCallEvent); // fire and forget!

                            if (remoteException is RequestException || remoteException is EnvironmentException)
                            {
                                ExceptionDispatchInfo.Capture(remoteException).Throw();
                            }

                            if (remoteException is UnhandledException)
                            {
                                remoteException = remoteException.InnerException;
                            }

                            throw new RemoteServiceException("The remote service returned a failure response. See " +
                                                             "the 'RequestUri' property on this exception for the URL that was called, and the " +
                                                             "inner exception for details.",
                                                             uri,
                                                             remoteException,
                                                             unencrypted: new Tags {
                                { "requestUri", uri }
                            });
                        }
                    }
                    catch (JsonException ex)
                    {
                        _failureCounter.Increment("Serialization");

                        Log.Error("The remote service returned a response with JSON that failed " +
                                  "deserialization. See the 'uri' tag for the URL that was called, the exception for the " +
                                  "exact error and the 'responseContent' encrypted tag for the original response content.",
                                  exception: ex,
                                  unencryptedTags: new { uri },
                                  encryptedTags: new { responseContent });

                        clientCallEvent.Exception = ex;
                        EventPublisher.TryPublish(clientCallEvent); // fire and forget!
                        throw new RemoteServiceException("The remote service returned a response with JSON that " +
                                                         "failed deserialization. See the 'RequestUri' property on this exception for the URL " +
                                                         "that was called, the inner exception for the exact error and the 'responseContent' " +
                                                         "encrypted tag for the original response content.",
                                                         uri,
                                                         ex,
                                                         new Tags {
                            { "responseContent", responseContent }
                        },
                                                         new Tags {
                            { "requestUri", uri }
                        });
                    }
                }
                else
                {
                    var exception = response.StatusCode == HttpStatusCode.ServiceUnavailable ?
                                    new Exception($"The remote service is unavailable (503) and is not recognized as a Gigya host at uri: {uri}") :
                                    new Exception($"The remote service returned a response but is not recognized as a Gigya host at uri: {uri}");

                    nodeAndLoadBalancer.LoadBalancer.ReportUnreachable(nodeAndLoadBalancer.Node, exception);
                    _hostFailureCounter.Increment("NotGigyaHost");

                    if (response.StatusCode == HttpStatusCode.ServiceUnavailable)
                    {
                        Log.Error("The remote service is unavailable (503) and is not recognized as a Gigya host. Continuing to next host.", unencryptedTags: new { uri });
                    }
                    else
                    {
                        Log.Error("The remote service returned a response but is not recognized as a Gigya host. Continuing to next host.", unencryptedTags: new { uri, statusCode = response.StatusCode }, encryptedTags: new { responseContent });
                    }

                    clientCallEvent.ErrCode = 500001;           //(int)GSErrors.General_Server_Error;
                    EventPublisher.TryPublish(clientCallEvent); // fire and forget!
                }
            }
        }
        /// <summary>
        /// This request is used to create create a new Statement with the given query in the Phoenix query server.
        /// </summary>
        public async Task<PrepareResponse> PrepareRequestAsync(string connectionId, string sql, long maxRowsTotal, RequestOptions options)
        {
            PrepareRequest req = new PrepareRequest
            {
                ConnectionId = connectionId,
                Sql = sql,
                MaxRowsTotal = maxRowsTotal
            };

            WireMessage msg = new WireMessage
            {
                Name = Constants.WireMessagePrefix + "PrepareRequest",
                WrappedMessage = req.ToByteString()
            };

            using (Response webResponse = await PostRequestAsync(msg.ToByteArray(), options))
            {
                if (webResponse.WebResponse.StatusCode != HttpStatusCode.OK)
                {
                    WireMessage output = WireMessage.Parser.ParseFrom(webResponse.WebResponse.GetResponseStream());
                    ErrorResponse res = ErrorResponse.Parser.ParseFrom(output.WrappedMessage);
                    throw new WebException(
                        string.Format(
                            "PrepareRequestAsync failed! connectionId: {0}, Response code was: {1}, Response body was: {2}",
                            connectionId,
                            webResponse.WebResponse.StatusCode,
                            res.ToString()));
                }
                else
                {
                    WireMessage output = WireMessage.Parser.ParseFrom(webResponse.WebResponse.GetResponseStream());
                    PrepareResponse res = PrepareResponse.Parser.ParseFrom(output.WrappedMessage);
                    return res;
                }
            }
        }