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); }
/// <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(); } }
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(); } }
/// <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(); } }
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); } }
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); }
/// <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(); } }
/// <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(); } }
/// <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(); } } }
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); }
/// <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(); } } }
/// <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); }
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); }