Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Register info #1028

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 54 additions & 30 deletions src/CAServer.Application/Guardian/GuardianAppService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public GuardianAppService(
_clusterClient = clusterClient;
_appleUserProvider = appleUserProvider;
_appleTransferOptions = appleTransferOptions.Value;
_stopRegisterOptions = stopRegisterOptions.Value;
_stopRegisterOptions = stopRegisterOptions.Value;
_nicknameProvider = nicknameProvider;
_zkLoginProvider = zkLoginProvider;
_guardiansCache = guardiansCache;
Expand All @@ -87,7 +87,8 @@ public async Task<GuardianResultDto> GetGuardianIdentifiersWrapperAsync(Guardian
return await GetGuardianIdentifiersAsync(guardianIdentifierDto);
}

var key = GetGuardianIdentifiersCacheKey(guardianIdentifierDto.GuardianIdentifier, guardianIdentifierDto.ChainId);
var key = GetGuardianIdentifiersCacheKey(guardianIdentifierDto.GuardianIdentifier,
guardianIdentifierDto.ChainId);
var result = await _guardiansCache.GetAsync(key);
if (result != null)
{
Expand All @@ -99,7 +100,8 @@ public async Task<GuardianResultDto> GetGuardianIdentifiersWrapperAsync(Guardian
{
await _guardiansCache.SetAsync(key, result, new DistributedCacheEntryOptions
{
AbsoluteExpiration = DateTimeOffset.UtcNow.AddSeconds(_loginCacheOptions.GuardianIdentifiersCacheSeconds)
AbsoluteExpiration =
DateTimeOffset.UtcNow.AddSeconds(_loginCacheOptions.GuardianIdentifiersCacheSeconds)
});
}

Expand All @@ -122,17 +124,20 @@ public async Task<GuardianResultDto> GetGuardianIdentifiersAsync(GuardianIdentif
sw.Start();
guardianIdentifierHash = await GetHashFromIdentifierAsync(guardianIdentifierDto.GuardianIdentifier);
sw.Stop();
_logger.LogInformation("GetGuardianIdentifiersAsync:GetHashFromIdentifierAsync=>cost:{0}ms", sw.ElapsedMilliseconds);
_logger.LogInformation("GetGuardianIdentifiersAsync:GetHashFromIdentifierAsync=>cost:{0}ms",
sw.ElapsedMilliseconds);
if (string.IsNullOrWhiteSpace(guardianIdentifierHash))
{
throw new UserFriendlyException($"{guardianIdentifierDto.GuardianIdentifier} not exist.",
GuardianMessageCode.NotExist);
}
}

//get holderInfo from contract
//100-1600ms, Key optimization
sw.Restart();
var guardianResult = await GetHolderInfosAsync(guardianIdentifierHash, guardianIdentifierDto.ChainId, guardianIdentifierDto.CaHash,
var guardianResult = await GetHolderInfosAsync(guardianIdentifierHash, guardianIdentifierDto.ChainId,
guardianIdentifierDto.CaHash,
guardianIdentifierDto.GuardianIdentifier);
sw.Stop();
_logger.LogInformation("GetGuardianIdentifiersAsync:GetHolderInfosAsync=>cost:{0}ms", sw.ElapsedMilliseconds);
Expand Down Expand Up @@ -213,7 +218,7 @@ public async Task<RegisterInfoResultDto> GetRegisterInfoWrapperAsync(RegisterInf

return result;
}

private string GetRegisterInfoCacheKey(string guardianIdentifier)
{
return RegisterInfoCachePrefix + guardianIdentifier;
Expand All @@ -225,6 +230,7 @@ public async Task<RegisterInfoResultDto> GetRegisterInfoAsync(RegisterInfoDto re
{
throw new UserFriendlyException(_appleTransferOptions.ErrorMessage);
}

var guardianIdentifierHash = GetHash(requestDto.LoginGuardianIdentifier);
string originChainId;
if (_loginCacheOptions.RegisterInfoParallelModeSwitch)
Expand All @@ -234,11 +240,17 @@ public async Task<RegisterInfoResultDto> GetRegisterInfoAsync(RegisterInfoDto re
else
{
var guardians = await _guardianProvider.GetGuardiansAsync(guardianIdentifierHash, requestDto.CaHash);
var guardian = guardians?.CaHolderInfo?.FirstOrDefault(t => !string.IsNullOrWhiteSpace(t.OriginChainId));
var holderInfo = guardians?.CaHolderInfo?.FirstOrDefault(t =>
t.GuardianList != null && !t.GuardianList.Guardians.IsNullOrEmpty() &&
!string.IsNullOrWhiteSpace(t.OriginChainId));
var guardian = holderInfo?.GuardianList?.Guardians?.FirstOrDefault(t =>
t.IdentifierHash == guardianIdentifierHash && t.IsLoginGuardian == true);

originChainId = guardian == null
? await GetOriginChainIdAsync(guardianIdentifierHash, requestDto.CaHash)
: guardian.OriginChainId;
: holderInfo.OriginChainId;
}

return new RegisterInfoResultDto { OriginChainId = originChainId };
}

Expand Down Expand Up @@ -273,6 +285,7 @@ private string GetHash(string guardianIdentifier)
{
throw new UserFriendlyException(_stopRegisterOptions.Message, GuardianMessageCode.StopRegister);
}

throw new UserFriendlyException(guardianGrainDto.Message, GuardianMessageCode.NotExist);
}

Expand All @@ -289,12 +302,13 @@ private async Task<string> GetOriginChainIdAsync(string guardianIdentifierHash,
{
var holderInfo =
await _guardianProvider.GetHolderInfoFromContractAsync(guardianIdentifierHash, caHash, chainId);
if (holderInfo.CreateChainId > 0)
if (holderInfo.CreateChainId > 0 &&
ChainHelper.ConvertChainIdToBase58(holderInfo.CreateChainId) == chainId)
{
return ChainHelper.ConvertChainIdToBase58(holderInfo.CreateChainId);
}

if (holderInfo?.GuardianList?.Guardians?.Count > 0)
if (holderInfo.CreateChainId == 0 && holderInfo?.GuardianList?.Guardians?.Count > 0)
{
return chainId;
}
Expand All @@ -303,12 +317,13 @@ private async Task<string> GetOriginChainIdAsync(string guardianIdentifierHash,
{
var guardianResult =
await _guardianProvider.GetHolderInfoFromCacheAsync(guardianIdentifierHash, chainId, true);
if (!guardianResult.CreateChainId.IsNullOrEmpty())
if (!guardianResult.CreateChainId.IsNullOrEmpty() && guardianResult.CreateChainId == chainId)
{
return guardianResult.CreateChainId;
}

if (guardianResult?.GuardianList?.Guardians?.Count > 0)
if (guardianResult.CreateChainId.IsNullOrEmpty() &&
guardianResult?.GuardianList?.Guardians?.Count > 0)
{
return chainId;
}
Expand All @@ -323,18 +338,18 @@ private async Task<string> GetOriginChainIdAsync(string guardianIdentifierHash,
}
}
}

if (_stopRegisterOptions.Open)
{
throw new UserFriendlyException(_stopRegisterOptions.Message, GuardianMessageCode.StopRegister);
}

throw new UserFriendlyException("This address is not registered.", GuardianMessageCode.NotExist);
}

private async Task<string> GetOriginalChainIdParallelMode(string guardianIdentifierHash, string caHash)
{
var holderInfoTasks = new List<Task<string>>();
holderInfoTasks.Add(BuildTaskByScan(guardianIdentifierHash, caHash));
foreach (var (chainId, chainInfo) in _chainOptions.ChainInfos)
{
holderInfoTasks.Add(BuildTaskByContract(guardianIdentifierHash, caHash, chainId));
Expand All @@ -345,13 +360,8 @@ private async Task<string> GetOriginalChainIdParallelMode(string guardianIdentif
{
throw new UserFriendlyException("This address is not registered.", GuardianMessageCode.NotExist);
}
return originalChainIds.FirstOrDefault(item => !item.IsNullOrEmpty());
}

private async Task<string> BuildTaskByScan(string guardianIdentifierHash, string caHash)
{
var guardians = await _guardianProvider.GetGuardiansAsync(guardianIdentifierHash, caHash);
return guardians?.CaHolderInfo?.FirstOrDefault(t => !string.IsNullOrWhiteSpace(t.OriginChainId))?.OriginChainId;
return originalChainIds.FirstOrDefault(item => !item.IsNullOrEmpty());
}

private async Task<string> BuildTaskByContract(string guardianIdentifierHash, string caHash, string chainId)
Expand All @@ -360,13 +370,21 @@ private async Task<string> BuildTaskByContract(string guardianIdentifierHash, st
{
if (!guardianIdentifierHash.IsNullOrEmpty() && !chainId.IsNullOrEmpty())
{
var guardianResultDto = await _guardianProvider.GetHolderInfoFromCacheAsync(guardianIdentifierHash:guardianIdentifierHash, chainId:chainId, needCache:true);
return guardianResultDto.CreateChainId.IsNullOrEmpty() ? null : guardianResultDto.CreateChainId;
var guardianResultDto =
await _guardianProvider.GetHolderInfoFromCacheAsync(guardianIdentifierHash: guardianIdentifierHash,
chainId: chainId, needCache: true);
return guardianResultDto.CreateChainId.IsNullOrEmpty() || guardianResultDto.CreateChainId != chainId
? null
: guardianResultDto.CreateChainId;
}
else
{
var holderInfo = await _guardianProvider.GetHolderInfoFromContractAsync(guardianIdentifierHash, caHash, chainId);
return holderInfo.CreateChainId > 0 ? ChainHelper.ConvertChainIdToBase58(holderInfo.CreateChainId) : null;
var holderInfo =
await _guardianProvider.GetHolderInfoFromContractAsync(guardianIdentifierHash, caHash, chainId);
return holderInfo.CreateChainId > 0 &&
ChainHelper.ConvertChainIdToBase58(holderInfo.CreateChainId) == chainId
? ChainHelper.ConvertChainIdToBase58(holderInfo.CreateChainId)
: null;
}
}
catch (Exception e)
Expand All @@ -375,14 +393,15 @@ private async Task<string> BuildTaskByContract(string guardianIdentifierHash, st
return null;
}
}

private async Task AddGuardianInfoAsync(List<GuardianDto> guardians, Dictionary<string, string> hashDic,
List<UserExtraInfoIndex> userExtraInfos)
{
if (guardians == null || guardians.Count == 0)
{
return;
}

foreach (var guardian in guardians)
{
guardian.GuardianIdentifier = hashDic.GetValueOrDefault(guardian.IdentifierHash);
Expand Down Expand Up @@ -432,8 +451,9 @@ private async Task<GuardianResultDto> GetHolderInfosAsync(string guardianIdentif
{
return await _guardianProvider.GetHolderInfoFromCacheAsync(guardianIdentifierHash, chainId, true);
}

var holderInfo = await _guardianProvider.GetHolderInfoFromContractAsync(guardianIdentifierHash, caHash, chainId);

var holderInfo =
await _guardianProvider.GetHolderInfoFromContractAsync(guardianIdentifierHash, caHash, chainId);
var guardianResultDto = ObjectMapper.Map<GetHolderInfoOutput, GuardianResultDto>(holderInfo);
_guardianProvider.AppendZkLoginInfo(holderInfo, guardianResultDto);
return guardianResultDto;
Expand Down Expand Up @@ -532,12 +552,16 @@ private async Task SetNameAsync(GuardianDto guardian)
public async Task<bool> UpdateUnsetGuardianIdentifierAsync(UpdateGuardianIdentifierDto updateGuardianIdentifierDto)
{
GuardianResultDto guardianResultDto = await GetGuardianIdentifiersAsync(updateGuardianIdentifierDto);
if (guardianResultDto == null || guardianResultDto.GuardianList == null || guardianResultDto.GuardianList.Guardians.IsNullOrEmpty())
if (guardianResultDto == null || guardianResultDto.GuardianList == null ||
guardianResultDto.GuardianList.Guardians.IsNullOrEmpty())
{
return false;
}
var result = await ModifyNicknameHandler(guardianResultDto, updateGuardianIdentifierDto.UserId, updateGuardianIdentifierDto.UnsetGuardianIdentifierHash);
_logger.LogInformation("UpdateUnsetGuardianIdentifierAsync result is={0}, caHash={1}", result, updateGuardianIdentifierDto.CaHash);

var result = await ModifyNicknameHandler(guardianResultDto, updateGuardianIdentifierDto.UserId,
updateGuardianIdentifierDto.UnsetGuardianIdentifierHash);
_logger.LogInformation("UpdateUnsetGuardianIdentifierAsync result is={0}, caHash={1}", result,
updateGuardianIdentifierDto.CaHash);
return result;
}

Expand Down
Loading