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