From 5d3c6e5c29b045fbd896488b70fb46677b7446fa Mon Sep 17 00:00:00 2001 From: felix-zhaolei Date: Tue, 17 Dec 2024 03:18:37 +0800 Subject: [PATCH 1/3] fix: registerInfo. --- .../Guardian/GuardianAppService.cs | 68 +++++++++++++------ 1 file changed, 47 insertions(+), 21 deletions(-) diff --git a/src/CAServer.Application/Guardian/GuardianAppService.cs b/src/CAServer.Application/Guardian/GuardianAppService.cs index 479cf9189..44387f8d3 100644 --- a/src/CAServer.Application/Guardian/GuardianAppService.cs +++ b/src/CAServer.Application/Guardian/GuardianAppService.cs @@ -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; @@ -87,7 +87,8 @@ public async Task 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) { @@ -99,7 +100,8 @@ public async Task GetGuardianIdentifiersWrapperAsync(Guardian { await _guardiansCache.SetAsync(key, result, new DistributedCacheEntryOptions { - AbsoluteExpiration = DateTimeOffset.UtcNow.AddSeconds(_loginCacheOptions.GuardianIdentifiersCacheSeconds) + AbsoluteExpiration = + DateTimeOffset.UtcNow.AddSeconds(_loginCacheOptions.GuardianIdentifiersCacheSeconds) }); } @@ -122,17 +124,20 @@ public async Task 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); @@ -213,7 +218,7 @@ public async Task GetRegisterInfoWrapperAsync(RegisterInf return result; } - + private string GetRegisterInfoCacheKey(string guardianIdentifier) { return RegisterInfoCachePrefix + guardianIdentifier; @@ -225,6 +230,7 @@ public async Task GetRegisterInfoAsync(RegisterInfoDto re { throw new UserFriendlyException(_appleTransferOptions.ErrorMessage); } + var guardianIdentifierHash = GetHash(requestDto.LoginGuardianIdentifier); string originChainId; if (_loginCacheOptions.RegisterInfoParallelModeSwitch) @@ -239,6 +245,7 @@ public async Task GetRegisterInfoAsync(RegisterInfoDto re ? await GetOriginChainIdAsync(guardianIdentifierHash, requestDto.CaHash) : guardian.OriginChainId; } + return new RegisterInfoResultDto { OriginChainId = originChainId }; } @@ -273,6 +280,7 @@ private string GetHash(string guardianIdentifier) { throw new UserFriendlyException(_stopRegisterOptions.Message, GuardianMessageCode.StopRegister); } + throw new UserFriendlyException(guardianGrainDto.Message, GuardianMessageCode.NotExist); } @@ -289,12 +297,13 @@ private async Task 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; } @@ -303,12 +312,13 @@ private async Task 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; } @@ -323,11 +333,12 @@ private async Task GetOriginChainIdAsync(string guardianIdentifierHash, } } } - + if (_stopRegisterOptions.Open) { throw new UserFriendlyException(_stopRegisterOptions.Message, GuardianMessageCode.StopRegister); } + throw new UserFriendlyException("This address is not registered.", GuardianMessageCode.NotExist); } @@ -345,6 +356,7 @@ private async Task GetOriginalChainIdParallelMode(string guardianIdentif { throw new UserFriendlyException("This address is not registered.", GuardianMessageCode.NotExist); } + return originalChainIds.FirstOrDefault(item => !item.IsNullOrEmpty()); } @@ -360,13 +372,21 @@ private async Task 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.ChainId != 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) @@ -375,7 +395,7 @@ private async Task BuildTaskByContract(string guardianIdentifierHash, st return null; } } - + private async Task AddGuardianInfoAsync(List guardians, Dictionary hashDic, List userExtraInfos) { @@ -383,6 +403,7 @@ private async Task AddGuardianInfoAsync(List guardians, Dictionary< { return; } + foreach (var guardian in guardians) { guardian.GuardianIdentifier = hashDic.GetValueOrDefault(guardian.IdentifierHash); @@ -432,8 +453,9 @@ private async Task 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(holderInfo); _guardianProvider.AppendZkLoginInfo(holderInfo, guardianResultDto); return guardianResultDto; @@ -532,12 +554,16 @@ private async Task SetNameAsync(GuardianDto guardian) public async Task 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; } From 6b007b5b2fb3fd780d3aa117bc86c44e67e2ee0f Mon Sep 17 00:00:00 2001 From: felix-zhaolei Date: Tue, 17 Dec 2024 12:16:31 +0800 Subject: [PATCH 2/3] modify: register info. --- .../Guardian/GuardianAppService.cs | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/CAServer.Application/Guardian/GuardianAppService.cs b/src/CAServer.Application/Guardian/GuardianAppService.cs index 44387f8d3..ffbcebfba 100644 --- a/src/CAServer.Application/Guardian/GuardianAppService.cs +++ b/src/CAServer.Application/Guardian/GuardianAppService.cs @@ -240,10 +240,15 @@ public async Task 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 }; @@ -345,7 +350,6 @@ private async Task GetOriginChainIdAsync(string guardianIdentifierHash, private async Task GetOriginalChainIdParallelMode(string guardianIdentifierHash, string caHash) { var holderInfoTasks = new List>(); - holderInfoTasks.Add(BuildTaskByScan(guardianIdentifierHash, caHash)); foreach (var (chainId, chainInfo) in _chainOptions.ChainInfos) { holderInfoTasks.Add(BuildTaskByContract(guardianIdentifierHash, caHash, chainId)); @@ -360,12 +364,6 @@ private async Task GetOriginalChainIdParallelMode(string guardianIdentif return originalChainIds.FirstOrDefault(item => !item.IsNullOrEmpty()); } - private async Task BuildTaskByScan(string guardianIdentifierHash, string caHash) - { - var guardians = await _guardianProvider.GetGuardiansAsync(guardianIdentifierHash, caHash); - return guardians?.CaHolderInfo?.FirstOrDefault(t => !string.IsNullOrWhiteSpace(t.OriginChainId))?.OriginChainId; - } - private async Task BuildTaskByContract(string guardianIdentifierHash, string caHash, string chainId) { try From 2072ac343a440f8953a20aa69e21eded7e1144cc Mon Sep 17 00:00:00 2001 From: felix-zhaolei Date: Tue, 17 Dec 2024 12:56:32 +0800 Subject: [PATCH 3/3] modify: register info. --- src/CAServer.Application/Guardian/GuardianAppService.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/CAServer.Application/Guardian/GuardianAppService.cs b/src/CAServer.Application/Guardian/GuardianAppService.cs index ffbcebfba..5192a1ebd 100644 --- a/src/CAServer.Application/Guardian/GuardianAppService.cs +++ b/src/CAServer.Application/Guardian/GuardianAppService.cs @@ -373,7 +373,7 @@ private async Task BuildTaskByContract(string guardianIdentifierHash, st var guardianResultDto = await _guardianProvider.GetHolderInfoFromCacheAsync(guardianIdentifierHash: guardianIdentifierHash, chainId: chainId, needCache: true); - return guardianResultDto.CreateChainId.IsNullOrEmpty() || guardianResultDto.ChainId != chainId + return guardianResultDto.CreateChainId.IsNullOrEmpty() || guardianResultDto.CreateChainId != chainId ? null : guardianResultDto.CreateChainId; }