예제 #1
0
 public int GetProfileCountByName(string name)
 {
     using (X42DbContext dbContext = new X42DbContext(databaseSettings.ConnectionString))
     {
         return(dbContext.Profiles.Where(p => p.Name == name).Count());
     }
 }
예제 #2
0
 public int GetProfileReservationCountSearch(string name, string keyAddress)
 {
     using (X42DbContext dbContext = new X42DbContext(databaseSettings.ConnectionString))
     {
         return(dbContext.ProfileReservations.Where(p => p.Name == name || p.KeyAddress == keyAddress).Count());
     }
 }
예제 #3
0
 public int GetProfileCountByKeyAddress(string keyAddress)
 {
     using (X42DbContext dbContext = new X42DbContext(databaseSettings.ConnectionString))
     {
         return(dbContext.Profiles.Where(p => p.KeyAddress == keyAddress).Count());
     }
 }
예제 #4
0
 public ProfileReservationData GetProfileReservationByName(string name)
 {
     using (X42DbContext dbContext = new X42DbContext(databaseSettings.ConnectionString))
     {
         return(dbContext.ProfileReservations.Where(n => n.Name == name).FirstOrDefault());
     }
 }
예제 #5
0
        public TopResult GetTopXServers(int top)
        {
            TopResult result = new TopResult();

            using (X42DbContext dbContext = new X42DbContext(ConnectionString))
            {
                IQueryable <ServerNodeData> servers = dbContext.ServerNodes.Where(n => n.Active).OrderByDescending(u => u.Priority);
                if (top > 0)
                {
                    servers = servers.Take(top);
                }

                if (servers.Count() > 0)
                {
                    servers.ToList().ForEach(
                        server => result.XServers.Add(
                            new XServerConnectionInfo()
                    {
                        Name            = server.ProfileName,
                        NetworkProtocol = server.NetworkProtocol,
                        NetworkAddress  = server.NetworkAddress,
                        NetworkPort     = server.NetworkPort,
                        Priotiry        = server.Priority,
                        Tier            = server.Tier
                    }
                            ));
                }
            }

            return(result);
        }
예제 #6
0
        private async Task CheckPaidPriceLocks(CancellationToken cancellationToken)
        {
            using (X42DbContext dbContext = new X42DbContext(databaseSettings.ConnectionString))
            {
                var priceLocks = dbContext.PriceLocks.Where(p => p.Status > (int)Status.New && p.Status < (int)Status.Mature);
                foreach (var priceLock in priceLocks)
                {
                    try
                    {
                        var plTransaction = await networkFeatures.GetRawTransaction(priceLock.TransactionId, true);

                        if (plTransaction.Confirmations >= 500)
                        {
                            priceLock.Status = (int)Status.Mature;
                        }
                        else if (plTransaction.Confirmations >= 1)
                        {
                            priceLock.Status = (int)Status.Confirmed;
                        }
                    }
                    catch (Exception) { }
                    if (cancellationToken.IsCancellationRequested)
                    {
                        break;
                    }
                }
                dbContext.SaveChanges();
            }
        }
예제 #7
0
 public ProfileReservationData GetProfileReservationByKeyAddress(string keyAddress)
 {
     using (X42DbContext dbContext = new X42DbContext(databaseSettings.ConnectionString))
     {
         return(dbContext.ProfileReservations.Where(n => n.KeyAddress == keyAddress).FirstOrDefault());
     }
 }
예제 #8
0
 public List <ProfileData> GetFirstProfilesFromBlock(int fromBlock, int take)
 {
     using (X42DbContext dbContext = new X42DbContext(databaseSettings.ConnectionString))
     {
         return(dbContext.Profiles.Where(p => p.Status == (int)Profile.Status.Created && p.BlockConfirmed > fromBlock).OrderBy(p => p.BlockConfirmed).Take(take).ToList());
     }
 }
예제 #9
0
        /// <summary>
        ///     Return active xServers available
        /// </summary>
        /// <param name="fromId">The Id to resume from</param>
        /// <returns>Will return active servers from the Id specified.</returns>
        public List <ServerRegisterResult> GetActiveXServers(int fromId)
        {
            List <ServerRegisterResult> result = new List <ServerRegisterResult>();

            using (X42DbContext dbContext = new X42DbContext(ConnectionString))
            {
                IQueryable <ServerNodeData> servers = dbContext.ServerNodes.Where(n => n.Active && n.Id > fromId).Take(xServerTake);
                if (servers.Count() > 0)
                {
                    servers.ToList().ForEach(
                        server => result.Add(
                            new ServerRegisterResult()
                    {
                        Id              = server.Id,
                        ProfileName     = server.ProfileName,
                        NetworkProtocol = server.NetworkProtocol,
                        NetworkAddress  = server.NetworkAddress,
                        NetworkPort     = server.NetworkPort,
                        Signature       = server.Signature,
                        Tier            = server.Tier,
                        FeeAddress      = server.FeeAddress,
                        KeyAddress      = server.KeyAddress,
                        SignAddress     = server.SignAddress
                    }
                            ));
                }
            }

            return(result);
        }
예제 #10
0
        /// <summary>
        ///     Only check pricelocks that are not past the expiration block with grace period.
        /// </summary>
        private async Task CheckUnPaidPriceLocks(CancellationToken cancellationToken)
        {
            using (X42DbContext dbContext = new X42DbContext(databaseSettings.ConnectionString))
            {
                var priceLocks = dbContext.PriceLocks.Where(p => p.Status == (int)Status.New && p.ExpireBlock <= (networkFeatures.BestBlockHeight - network.BlockGracePeriod));
                foreach (var priceLock in priceLocks)
                {
                    try
                    {
                        var confirmedPL = await networkFeatures.GetPriceLockFromT3(cancellationToken, priceLock.PriceLockId.ToString(), true);

                        if (confirmedPL != null)
                        {
                            networkFeatures.AddCompletePriceLock(confirmedPL);
                        }
                    }
                    catch (Exception) { }
                    if (cancellationToken.IsCancellationRequested)
                    {
                        break;
                    }
                }
                dbContext.SaveChanges();
            }
        }
예제 #11
0
        public bool Set(string key, object value)
        {
            bool result = false;

            using (X42DbContext dbContext = new X42DbContext(databaseSettings.ConnectionString))
            {
                var item = dbContext.DictionaryItems.Where(d => d.Key == key).FirstOrDefault();
                if (item == null)
                {
                    var newDictionaryItem = new DictionaryData()
                    {
                        Key   = key,
                        Value = ConvertType <string>(value)
                    };
                    var addedItem = dbContext.Add(newDictionaryItem);
                    if (addedItem.State == EntityState.Added)
                    {
                        dbContext.SaveChanges();
                        result = true;
                    }
                }
                else
                {
                    item.Value = ConvertType <string>(value);
                    dbContext.SaveChanges();
                    result = true;
                }
            }
            return(result);
        }
예제 #12
0
        public PriceLockResult GetPriceLock(Guid priceLockId)
        {
            var result = new PriceLockResult();

            using (X42DbContext dbContext = new X42DbContext(databaseSettings.ConnectionString))
            {
                var priceLock = dbContext.PriceLocks.Where(p => p.PriceLockId == priceLockId).FirstOrDefault();
                if (priceLock != null)
                {
                    result.DestinationAddress = priceLock.DestinationAddress;
                    result.DestinationAmount  = priceLock.DestinationAmount;
                    result.FeeAddress         = priceLock.FeeAddress;
                    result.FeeAmount          = priceLock.FeeAmount;
                    result.RequestAmount      = priceLock.RequestAmount;
                    result.RequestAmountPair  = priceLock.RequestAmountPair;
                    result.PriceLockId        = priceLock.PriceLockId.ToString();
                    result.PriceLockSignature = priceLock.PriceLockSignature;
                    result.Status             = priceLock.Status;
                    result.PayeeSignature     = priceLock.PayeeSignature;
                    result.TransactionId      = priceLock.TransactionId;
                    result.ExpireBlock        = priceLock.ExpireBlock;
                    result.SignAddress        = priceLock.SignAddress;
                    result.Success            = true;
                }
                else
                {
                    result.ResultMessage = "Pricelock not found.";
                    result.Success       = false;
                }
            }
            return(result);
        }
예제 #13
0
        /// <summary>
        ///     Check the reserved profiles, and process the paid ones, and remove the expired ones.
        /// </summary>
        private async Task CheckReservedProfiles(CancellationToken cancellationToken)
        {
            using (X42DbContext dbContext = new X42DbContext(databaseSettings.ConnectionString))
            {
                var profileReservations = dbContext.ProfileReservations;
                foreach (var profileReservation in profileReservations)
                {
                    if (!networkFeatures.ProfileExists(profileReservation.Name, profileReservation.KeyAddress, true))
                    {
                        await RegisterProfile(cancellationToken, profileReservation);
                    }
                    if (Convert.ToInt64(networkFeatures.BestBlockHeight) > profileReservation.ReservationExpirationBlock)
                    {
                        var priceLock = await networkFeatures.GetPriceLockFromT3(cancellationToken, profileReservation.PriceLockId);

                        if (priceLock.Status <= (int)PriceLock.Status.New)
                        {
                            dbContext.ProfileReservations.Remove(profileReservation);
                        }
                        else if (networkFeatures.ProfileExists(profileReservation.Name, profileReservation.KeyAddress, true))
                        {
                            dbContext.ProfileReservations.Remove(profileReservation);
                        }
                    }
                }
                dbContext.SaveChanges();
            }
        }
예제 #14
0
        public dynamic Get <T>(string key)
        {
            var result = default(T);

            using (X42DbContext dbContext = new X42DbContext(databaseSettings.ConnectionString))
            {
                string value = dbContext.DictionaryItems.Where(d => d.Key == key).Select(d => d.Value).FirstOrDefault();
                result = ConvertType <T>(value);
            }
            return(result);
        }
예제 #15
0
        public int GetActiveServerCount()
        {
            int result = 0;

            using (X42DbContext dbContext = new X42DbContext(ConnectionString))
            {
                result = dbContext.ServerNodes.Where(s => s.Active).Count();
            }

            return(result);
        }
예제 #16
0
 /// <summary>
 ///     Once a new block is staked, this method is used to verify that it
 /// </summary>
 /// <param name="block">The new block.</param>
 /// <param name="chainTip">Block that was considered as a chain tip when the block staking started.</param>
 private async Task CheckActiveServersAsync()
 {
     using (X42DbContext dbContext = new X42DbContext(databaseSettings.ConnectionString))
     {
         IQueryable <MasterNodeData> masterNodes = dbContext.MasterNodes.Where(s => s.Active);
         foreach (MasterNodeData masterNode in masterNodes)
         {
             if (await ValidateServer(masterNode))
             {
                 // TODO: Test server.
             }
         }
     }
 }
예제 #17
0
        private async Task RegisterProfile(CancellationToken cancellationToken, ProfileReservationData profileReservationData)
        {
            try
            {
                var priceLock = await networkFeatures.GetPriceLockFromT3(cancellationToken, profileReservationData.PriceLockId, true);

                if (priceLock != null && priceLock.Status == (int)PriceLock.Status.Confirmed)
                {
                    var transaction = await networkFeatures.GetRawTransaction(priceLock.TransactionId, true);

                    if (transaction != null && transaction.BlockHeight > 0)
                    {
                        int blockConfirmed = (int)transaction.BlockHeight;
                        using (X42DbContext dbContext = new X42DbContext(databaseSettings.ConnectionString))
                        {
                            var profileCount = dbContext.Profiles.Where(p => p.Name == profileReservationData.Name || p.KeyAddress == profileReservationData.KeyAddress).Count();
                            if (profileCount == 0)
                            {
                                var newProfile = new ProfileData()
                                {
                                    KeyAddress     = profileReservationData.KeyAddress,
                                    Name           = profileReservationData.Name,
                                    PriceLockId    = profileReservationData.PriceLockId,
                                    ReturnAddress  = profileReservationData.ReturnAddress,
                                    Signature      = profileReservationData.Signature,
                                    Relayed        = false,
                                    BlockConfirmed = blockConfirmed,
                                    Status         = (int)Status.Created
                                };
                                var newRecord = dbContext.Profiles.Add(newProfile);
                                if (newRecord.State == EntityState.Added)
                                {
                                    dbContext.SaveChanges();
                                    var profileHeight = database.dataStore.GetIntFromDictionary("ProfileHeight");
                                    if (blockConfirmed > profileHeight)
                                    {
                                        networkFeatures.SetProfileHeightOnSelf(blockConfirmed);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                logger.LogError("Error During Profile Registration", ex);
            }
        }
예제 #18
0
        public ProfileData GetProfileByKeyAddress(string keyAddress)
        {
            ProfileData result = null;

            using (X42DbContext dbContext = new X42DbContext(ConnectionString))
            {
                var profile = dbContext.Profiles.Where(p => p.KeyAddress == keyAddress).FirstOrDefault();
                if (profile != null)
                {
                    result = profile;
                }
            }

            return(result);
        }
예제 #19
0
        public async Task <SubmitPaymentResult> SubmitPayment(SubmitPaymentRequest submitPaymentRequest)
        {
            var result = new SubmitPaymentResult();

            if (Guid.TryParse(submitPaymentRequest.PriceLockId, out Guid validPriceLockId))
            {
                var payeeValidationResult = await ValidateNewPriceLockPayee(submitPaymentRequest);

                if (payeeValidationResult == PaymentErrorCodes.None)
                {
                    using (X42DbContext dbContext = new X42DbContext(databaseSettings.ConnectionString))
                    {
                        var priceLock = dbContext.PriceLocks.Where(p => p.PriceLockId == validPriceLockId).FirstOrDefault();
                        if (priceLock != null)
                        {
                            if (priceLock.Status == (int)Status.New)
                            {
                                priceLock.PayeeSignature = submitPaymentRequest.PayeeSignature;
                                priceLock.TransactionId  = submitPaymentRequest.TransactionId;
                                priceLock.Status         = (int)Status.WaitingForConfirmation;
                                priceLock.Relayed        = false;
                                dbContext.SaveChanges();
                                if (!string.IsNullOrEmpty(submitPaymentRequest.TransactionHex))
                                {
                                    await networkFeatures.SendTransaction(submitPaymentRequest.TransactionHex);
                                }
                                result.Success = true;
                            }
                            else
                            {
                                result.ErrorCode = (int)PaymentErrorCodes.NotNew;
                            }
                        }
                        else
                        {
                            result.ErrorCode = (int)PaymentErrorCodes.PriceLockNotFound;
                        }
                    }
                }
                else
                {
                    result.ErrorCode = (int)payeeValidationResult;
                }
            }
            return(result);
        }
예제 #20
0
        /// <summary>
        ///     Relay profiles and reservations that have not been processed.
        /// </summary>
        private async Task RelayProfiles(CancellationToken cancellationToken)
        {
            using (X42DbContext dbContext = new X42DbContext(databaseSettings.ConnectionString))
            {
                var t2Servers = networkFeatures.GetAllTier2ConnectionInfo();
                var profileReservationsToRelay = dbContext.ProfileReservations.Where(pr => !pr.Relayed);
                foreach (var profileReservation in profileReservationsToRelay)
                {
                    foreach (var server in t2Servers)
                    {
                        await networkFeatures.RelayProfileReservation(cancellationToken, profileReservation, server);
                    }
                    profileReservation.Relayed = true;
                }
                dbContext.SaveChanges();
            }

            using (X42DbContext dbContext = new X42DbContext(databaseSettings.ConnectionString))
            {
                var t2Servers = networkFeatures.GetAllTier2ConnectionInfo();
                var profiles  = dbContext.Profiles.Where(pr => !pr.Relayed);
                foreach (var profile in profiles)
                {
                    foreach (var server in t2Servers)
                    {
                        var profileReservation = new ProfileReservationData()
                        {
                            KeyAddress  = profile.KeyAddress,
                            Name        = profile.Name,
                            PriceLockId = profile.PriceLockId,
                            Relayed     = profile.Relayed,
                            ReservationExpirationBlock = Convert.ToInt32(networkFeatures.BestBlockHeight + network.BlockGracePeriod),
                            ReturnAddress = profile.ReturnAddress,
                            Signature     = profile.Signature,
                            Status        = profile.Status
                        };
                        await networkFeatures.RelayProfileReservation(cancellationToken, profileReservation, server);
                    }
                    profile.Relayed = true;
                }
                dbContext.SaveChanges();
            }
        }
예제 #21
0
        /// <summary>
        ///     Check for active servers, and remove any inactive servers.
        /// </summary>
        private async Task CheckActiveServersAsync()
        {
            using (X42DbContext dbContext = new X42DbContext(databaseSettings.ConnectionString))
            {
                IQueryable <ServerNodeData> allServerNodes = dbContext.ServerNodes;

                List <Task <ServerNodeData> > nodeTasks = new List <Task <ServerNodeData> >();

                foreach (ServerNodeData serverNode in allServerNodes)
                {
                    nodeTasks.Add(ServerCheck(serverNode));
                }
                await Task.WhenAll(nodeTasks);

                dbContext.SaveChanges();
            }

            // Remove any servers that have been unavailable past the grace period.
            using (X42DbContext dbContext = new X42DbContext(databaseSettings.ConnectionString))
            {
                IQueryable <ServerNodeData> inactiveServers = dbContext.ServerNodes.Where(n => !n.Active);
                foreach (ServerNodeData serverNode in inactiveServers)
                {
                    var lastSeen = serverNode.LastSeen.AddMinutes(network.DowntimeGracePeriod);
                    if (DateTime.UtcNow > lastSeen)
                    {
                        dbContext.ServerNodes.Remove(serverNode);
                    }
                    else
                    {
                        var serverTier = await networkFeatures.GetServerTier(serverNode, network.BlockGracePeriod);

                        if (serverTier == null)
                        {
                            dbContext.ServerNodes.Remove(serverNode);
                        }
                    }
                }
                dbContext.SaveChanges();
            }
        }
예제 #22
0
        /// <summary>
        ///     Check for new paylocks and relay the information.
        /// </summary>
        private async Task RelayNewPayLocks(CancellationToken cancellationToken)
        {
            using (X42DbContext dbContext = new X42DbContext(databaseSettings.ConnectionString))
            {
                IQueryable <PriceLockData> newPriceLocks = dbContext.PriceLocks.Where(p => !p.Relayed);
                if (newPriceLocks.Count() > 0)
                {
                    List <ServerNodeData> tier3Nodes = dbContext.ServerNodes.Where(s => s.Active && s.Tier == (int)TierLevel.Three).ToList();
                    foreach (var newPriceLock in newPriceLocks)
                    {
                        try
                        {
                            await networkFeatures.RelayPriceLock(newPriceLock, tier3Nodes, cancellationToken);

                            newPriceLock.Relayed = true;
                        }
                        catch (Exception) { }
                    }
                    dbContext.SaveChanges();
                }
            }
        }
예제 #23
0
        public async Task <bool> ReceiveProfileReservation(ReceiveProfileReserveRequest profileReserveSyncRequest)
        {
            bool result = false;

            using (X42DbContext dbContext = new X42DbContext(databaseSettings.ConnectionString))
            {
                var profileCount = dbContext.ProfileReservations.Where(p => p.Name == profileReserveSyncRequest.Name || p.KeyAddress == profileReserveSyncRequest.KeyAddress).Count();
                if (profileCount == 0)
                {
                    if (!networkFeatures.ProfileExists(profileReserveSyncRequest.Name, profileReserveSyncRequest.KeyAddress))
                    {
                        bool isProfileKeyValid = await networkFeatures.IsProfileKeyValid(profileReserveSyncRequest.Name, profileReserveSyncRequest.KeyAddress, profileReserveSyncRequest.ReturnAddress, profileReserveSyncRequest.Signature);

                        if (isProfileKeyValid)
                        {
                            var newProfile = new ProfileReservationData()
                            {
                                KeyAddress    = profileReserveSyncRequest.KeyAddress,
                                Name          = profileReserveSyncRequest.Name,
                                PriceLockId   = profileReserveSyncRequest.PriceLockId,
                                ReturnAddress = profileReserveSyncRequest.ReturnAddress,
                                Signature     = profileReserveSyncRequest.Signature,
                                Relayed       = false,
                                Status        = (int)Status.Reserved,
                                ReservationExpirationBlock = profileReserveSyncRequest.ReservationExpirationBlock
                            };
                            var newRecord = dbContext.ProfileReservations.Add(newProfile);
                            if (newRecord.State == EntityState.Added)
                            {
                                dbContext.SaveChanges();
                                result = true;
                            }
                        }
                    }
                }
            }
            return(result);
        }
예제 #24
0
        /// <inheritdoc />
        public override Task InitializeAsync()
        {
            try
            {
                using (X42DbContext dbContext = new X42DbContext(databaseSettings.ConnectionString))
                {
                    logger.LogInformation("Connecting to database");

                    dbContext.Database.Migrate();

                    DatabaseConnected = true;

                    logger.LogInformation("Database Feature Initialized");
                }
            }
            catch (Exception ex)
            {
                logger.LogCritical("Database failed to Initialize.", ex);
                logger.LogTrace("(-)[INITIALIZE_EXCEPTION]");
            }

            return(Task.CompletedTask);
        }
예제 #25
0
        /// <summary>
        ///     Check for new xServers to relay to active xServers.
        /// </summary>
        private async Task RelayNewxServerAsync(CancellationToken cancellationToken)
        {
            using (X42DbContext dbContext = new X42DbContext(databaseSettings.ConnectionString))
            {
                IQueryable <ServerNodeData> newServerNodes = dbContext.ServerNodes.Where(s => !s.Relayed);
                if (newServerNodes.Count() > 0)
                {
                    List <ServerNodeData> serverNodes = dbContext.ServerNodes.Where(s => s.Active).ToList();
                    var xServerStats = await networkFeatures.GetXServerStats();

                    foreach (var connectedXServer in xServerStats.Nodes)
                    {
                        var xServer = serverNodes.Where(x => x.NetworkAddress == connectedXServer.NetworkAddress);
                        if (xServer.Count() == 0)
                        {
                            serverNodes.Add(new ServerNodeData()
                            {
                                NetworkAddress  = connectedXServer.NetworkAddress,
                                NetworkPort     = connectedXServer.NetworkPort,
                                NetworkProtocol = connectedXServer.NetworkProtocol
                            });
                        }
                    }
                    foreach (ServerNodeData newServer in newServerNodes)
                    {
                        try
                        {
                            await RelayXServerAsync(newServer, serverNodes, cancellationToken);

                            newServer.Relayed = true;
                        }
                        catch (Exception) { }
                    }
                    dbContext.SaveChanges();
                }
            }
        }
예제 #26
0
        public SetupStatusResult GetServerSetupStatus()
        {
            SetupStatusResult result = new SetupStatusResult()
            {
                ServerStatus = Status.NotStarted
            };

            using (X42DbContext dbContext = new X42DbContext(ConnectionString))
            {
                var profileName = database.dataStore.GetStringFromDictionary("ProfileName");
                if (!string.IsNullOrEmpty(profileName))
                {
                    result.ServerStatus = Status.Started;

                    var signAddress = database.dataStore.GetStringFromDictionary("SignAddress");
                    if (!string.IsNullOrEmpty(signAddress))
                    {
                        IQueryable <ServerNodeData> serverNode = dbContext.ServerNodes.Where(s => s.ProfileName == profileName && s.Active);
                        if (serverNode.Count() > 0)
                        {
                            if (serverNode.FirstOrDefault().SignAddress == signAddress)
                            {
                                result.SignAddress  = signAddress;
                                result.ServerStatus = Status.Complete;
                                result.TierLevel    = (Tier.TierLevel)serverNode.First().Tier;
                            }
                            else
                            {
                                result.ServerStatus = Status.InvalidSignAddress;
                            }
                        }
                    }
                }
            }
            return(result);
        }
예제 #27
0
        /// <summary>
        ///     Return xServer from profile name or sign address.
        /// </summary>
        /// <param name="profileName">The profile name to search for the xserver</param>
        /// <param name="signAddress">The sign address to search for the xserver</param>
        /// <returns>xServer found by the search.</returns>
        public ServerRegisterResult SearchForXServer(string profileName = "", string signAddress = "")
        {
            ServerRegisterResult result = new ServerRegisterResult();

            using (X42DbContext dbContext = new X42DbContext(ConnectionString))
            {
                ServerNodeData serverNode = null;
                if (!string.IsNullOrEmpty(profileName))
                {
                    serverNode = dbContext.ServerNodes.Where(sn => sn.ProfileName == profileName).FirstOrDefault();
                }
                else if (!string.IsNullOrEmpty(profileName))
                {
                    serverNode = dbContext.ServerNodes.Where(sn => sn.SignAddress == signAddress).FirstOrDefault();
                }

                if (serverNode != null)
                {
                    result = new ServerRegisterResult()
                    {
                        Id              = serverNode.Id,
                        ProfileName     = serverNode.ProfileName,
                        NetworkProtocol = serverNode.NetworkProtocol,
                        NetworkAddress  = serverNode.NetworkAddress,
                        NetworkPort     = serverNode.NetworkPort,
                        Signature       = serverNode.Signature,
                        Tier            = serverNode.Tier,
                        FeeAddress      = serverNode.FeeAddress,
                        KeyAddress      = serverNode.KeyAddress,
                        SignAddress     = serverNode.SignAddress
                    };
                }
            }

            return(result);
        }
예제 #28
0
        /// <summary>
        ///     Register a new profile.
        /// </summary>
        public async Task <ReserveProfileResult> ReserveProfile(ProfileReserveRequest profileRegisterRequest)
        {
            ReserveProfileResult result = new ReserveProfileResult();

            if (!networkFeatures.ProfileExists(profileRegisterRequest.Name, profileRegisterRequest.KeyAddress))
            {
                bool isProfileKeyValid = await networkFeatures.IsProfileKeyValid(profileRegisterRequest.Name, profileRegisterRequest.KeyAddress, profileRegisterRequest.ReturnAddress, profileRegisterRequest.Signature);

                if (!isProfileKeyValid)
                {
                    result.Success       = false;
                    result.ResultMessage = "Profile validation failed.";
                    return(result);
                }

                // Price Lock ID does not exist, this is a new request, so let's create a price lock ID for it, and reserve the name.
                var profilePriceLockRequest = new CreatePriceLockRequest()
                {
                    DestinationAddress = networkFeatures.GetMyFeeAddress(),
                    RequestAmount      = 5, // $5
                    RequestAmountPair  = 1, // USD
                    ExpireBlock        = 15
                };
                var newPriceLock = await networkFeatures.CreateNewPriceLock(profilePriceLockRequest);

                if (newPriceLock == null || string.IsNullOrEmpty(newPriceLock?.PriceLockId) || newPriceLock?.ExpireBlock <= 0)
                {
                    result.Success       = false;
                    result.ResultMessage = "Failed to acquire a price lock";
                    return(result);
                }
                int status     = (int)Status.Reserved;
                var newProfile = new ProfileReservationData()
                {
                    Name          = profileRegisterRequest.Name,
                    KeyAddress    = profileRegisterRequest.KeyAddress,
                    ReturnAddress = profileRegisterRequest.ReturnAddress,
                    PriceLockId   = newPriceLock.PriceLockId,
                    Signature     = profileRegisterRequest.Signature,
                    Status        = status,
                    ReservationExpirationBlock = newPriceLock.ExpireBlock,
                    Relayed = false
                };
                using (X42DbContext dbContext = new X42DbContext(databaseSettings.ConnectionString))
                {
                    var newRecord = dbContext.ProfileReservations.Add(newProfile);
                    if (newRecord.State == EntityState.Added)
                    {
                        dbContext.SaveChanges();
                        result.PriceLockId = newPriceLock.PriceLockId;
                        result.Status      = status;
                        result.Success     = true;
                    }
                    else
                    {
                        result.Status        = (int)Status.Rejected;
                        result.ResultMessage = "Failed to add profile.";
                        result.Success       = false;
                    }
                }
            }
            else
            {
                result.Status        = (int)Status.Rejected;
                result.Success       = false;
                result.ResultMessage = "Profile already exists.";
            }

            return(result);
        }
예제 #29
0
        private async Task <List <string> > LocateAndAddNewServers(CancellationToken cancellationToken)
        {
            List <string> result = new List <string>();

            using (X42DbContext dbContext = new X42DbContext(databaseSettings.ConnectionString))
            {
                List <ServerNodeData> serverNodes = dbContext.ServerNodes.Where(s => s.Active).ToList();
                string selfProfile      = networkFeatures.GetServerProfile();
                int    localActiveCount = serverNodes.Count();
                var    xServerStats     = await networkFeatures.GetXServerStats();

                if (xServerStats == null)
                {
                    return(result);
                }
                foreach (var connectedXServer in xServerStats.Nodes)
                {
                    var xServer = serverNodes.Where(x => x.NetworkAddress == connectedXServer.NetworkAddress);
                    if (xServer.Count() == 0)
                    {
                        serverNodes.Add(new ServerNodeData()
                        {
                            NetworkAddress  = connectedXServer.NetworkAddress,
                            NetworkPort     = connectedXServer.NetworkPort,
                            NetworkProtocol = connectedXServer.NetworkProtocol
                        });
                    }
                }

                if (serverNodes.Count() > 0)
                {
                    List <ServerNodeData> newserverNodes = new List <ServerNodeData>();
                    foreach (ServerNodeData server in serverNodes)
                    {
                        bool foundSelf           = false;
                        bool xServerSyncComplete = false;
                        long lastId = 0;
                        try
                        {
                            string xServerURL = networkFeatures.GetServerUrl(server.NetworkProtocol, server.NetworkAddress, server.NetworkPort);
                            while (!xServerSyncComplete)
                            {
                                try
                                {
                                    var client = new RestClient(xServerURL);
                                    var allActiveXServersRequest = new RestRequest("/getactivexservers/", Method.GET);
                                    allActiveXServersRequest.AddParameter("fromId", lastId);
                                    var allActiveXServersResult = await client.ExecuteAsync <List <ServerRegisterResult> >(allActiveXServersRequest, cancellationToken).ConfigureAwait(false);

                                    if (allActiveXServersResult.StatusCode == HttpStatusCode.OK)
                                    {
                                        var activeXServersList = allActiveXServersResult.Data;
                                        if (activeXServersList.Count() == 0)
                                        {
                                            xServerSyncComplete = true;
                                        }
                                        else
                                        {
                                            foreach (var serverResult in activeXServersList)
                                            {
                                                if (serverResult.Id > lastId)
                                                {
                                                    lastId = serverResult.Id;
                                                }
                                                if (serverResult.ProfileName == selfProfile)
                                                {
                                                    foundSelf = true;
                                                }
                                                var registeredServer = serverNodes.Where(s => s.Signature == serverResult.Signature).FirstOrDefault();
                                                if (registeredServer == null)
                                                {
                                                    var newServer = new ServerNodeData()
                                                    {
                                                        ProfileName     = serverResult.ProfileName,
                                                        NetworkAddress  = serverResult.NetworkAddress,
                                                        NetworkPort     = serverResult.NetworkPort,
                                                        NetworkProtocol = serverResult.NetworkProtocol,
                                                        Signature       = serverResult.Signature,
                                                        Tier            = serverResult.Tier,
                                                        FeeAddress      = serverResult.FeeAddress,
                                                        SignAddress     = serverResult.SignAddress,
                                                        KeyAddress      = serverResult.KeyAddress,
                                                        Active          = true,
                                                        DateAdded       = DateTime.UtcNow,
                                                        LastSeen        = DateTime.UtcNow,
                                                        Priority        = 0,
                                                        Relayed         = true
                                                    };

                                                    // Local Registration of new nodes we don't know about.
                                                    await networkFeatures.Register(newServer);
                                                }
                                            }
                                        }
                                    }
                                    else
                                    {
                                        xServerSyncComplete = true;
                                    }
                                }
                                catch (Exception ex)
                                {
                                    logger.LogDebug($"Server failed to request during reco", ex);
                                }
                            }
                            if (!foundSelf)
                            {
                                result.Add(xServerURL);
                            }
                        }
                        catch (Exception ex)
                        {
                            logger.LogDebug($"Error in Reconciliation Service", ex);
                        }
                    }
                }
            }
            return(result);
        }
예제 #30
0
        public async Task <PriceLockResult> CreatePriceLock(CreatePriceLockRequest priceLockRequest, string validPriceLockId = "")
        {
            var result   = new PriceLockResult();
            var fiatPair = FiatPairs.Where(f => (int)f.Currency == priceLockRequest.RequestAmountPair).FirstOrDefault();

            if (fiatPair != null)
            {
                var averagePrice = fiatPair.GetPrice();
                if (averagePrice != -1)
                {
                    var price = Math.Round(priceLockRequest.RequestAmount / averagePrice, 8).Normalize();
                    var fee   = Math.Round(price * priceLockFeePercent / 100, 8).Normalize();

                    var feeAddress  = networkFeatures.GetMyFeeAddress();
                    var signAddress = networkFeatures.GetMySignAddress();

                    if (price < 42000000)
                    {
                        var expirationBlock = Convert.ToInt32(networkFeatures.BestBlockHeight + Convert.ToInt32(priceLockRequest.ExpireBlock));
                        if (!string.IsNullOrEmpty(validPriceLockId))
                        {
                            expirationBlock = Convert.ToInt32(priceLockRequest.ExpireBlock);
                        }

                        using (X42DbContext dbContext = new X42DbContext(databaseSettings.ConnectionString))
                        {
                            var newPriceLock = new PriceLockData()
                            {
                                DestinationAddress = priceLockRequest.DestinationAddress,
                                DestinationAmount  = price,
                                FeeAmount          = fee,
                                SignAddress        = signAddress,
                                FeeAddress         = feeAddress,
                                ExpireBlock        = expirationBlock,
                                RequestAmount      = priceLockRequest.RequestAmount,
                                RequestAmountPair  = priceLockRequest.RequestAmountPair,
                                Status             = (int)Status.New
                            };
                            if (!string.IsNullOrEmpty(validPriceLockId))
                            {
                                newPriceLock.PriceLockId = new Guid(validPriceLockId);
                            }
                            var newPriceLockRecord = dbContext.Add(newPriceLock);
                            if (newPriceLockRecord.State == EntityState.Added)
                            {
                                string signature = await networkFeatures.SignPriceLock($"{newPriceLock.PriceLockId}{newPriceLock.DestinationAddress}{newPriceLock.DestinationAmount}{newPriceLock.FeeAddress}{newPriceLock.FeeAmount}");

                                if (!string.IsNullOrEmpty(signature))
                                {
                                    newPriceLock.PriceLockSignature = signature;
                                    dbContext.SaveChanges();

                                    result.DestinationAddress = newPriceLock.DestinationAddress;
                                    result.DestinationAmount  = newPriceLock.DestinationAmount;
                                    result.FeeAddress         = newPriceLock.FeeAddress;
                                    result.SignAddress        = newPriceLock.SignAddress;
                                    result.FeeAmount          = newPriceLock.FeeAmount;
                                    result.RequestAmount      = newPriceLock.RequestAmount;
                                    result.RequestAmountPair  = newPriceLock.RequestAmountPair;
                                    result.PriceLockId        = newPriceLock.PriceLockId.ToString();
                                    result.PriceLockSignature = newPriceLock.PriceLockSignature;
                                    result.ExpireBlock        = newPriceLock.ExpireBlock;
                                    result.Status             = (int)Status.New;
                                    result.Success            = true;

                                    List <ServerNodeData> tier3Nodes = dbContext.ServerNodes.Where(s => s.Active && s.Tier == (int)TierLevel.Three).ToList();
                                    await networkFeatures.RelayPriceLock(newPriceLock, tier3Nodes, networkCancellationTokenSource.Token);
                                }
                                else
                                {
                                    result.ResultMessage = "Problem with node, Failed to sign price lock.";
                                    result.Success       = false;
                                }
                            }
                        }
                    }
                    else
                    {
                        result.ResultMessage = "Price not valid, max cap exceeded.";
                        result.Success       = false;
                    }
                }
                else
                {
                    result.ResultMessage = "Node could not create price lock because insufficient price data.";
                    result.Success       = false;
                }
            }
            else
            {
                result.ResultMessage = "The supplied pair does not exist.";
                result.Success       = false;
            }
            return(result);
        }