From ec0afbb67d60846ef20d1149e2a8dc4946332fee Mon Sep 17 00:00:00 2001 From: "zecong.nian" Date: Wed, 22 Nov 2023 20:35:04 +0800 Subject: [PATCH 1/7] feat: save fail tx to storage --- .../Application/ITransactionResultService.cs | 31 ++++++++++++++++- .../Domain/ITransactionResultManager.cs | 29 +++++++++++++++- ...tionValidationStatusChangedEventHandler.cs | 22 ++++++++++++- .../Services/TransactionResultAppService.cs | 33 ++++++++++++------- 4 files changed, 101 insertions(+), 14 deletions(-) diff --git a/src/AElf.Kernel.Core/Blockchain/Application/ITransactionResultService.cs b/src/AElf.Kernel.Core/Blockchain/Application/ITransactionResultService.cs index c2d6e0b2de..f660f3b7ec 100644 --- a/src/AElf.Kernel.Core/Blockchain/Application/ITransactionResultService.cs +++ b/src/AElf.Kernel.Core/Blockchain/Application/ITransactionResultService.cs @@ -1,3 +1,4 @@ +using System.Linq; using AElf.Kernel.Blockchain.Domain; namespace AElf.Kernel.Blockchain.Application; @@ -6,17 +7,23 @@ public interface ITransactionResultQueryService { Task GetTransactionResultAsync(Hash transactionId); Task GetTransactionResultAsync(Hash transactionId, Hash blockHash); + Task GetFailedTransactionResultAsync(Hash transactionId); } public interface ITransactionResultService : ITransactionResultQueryService { Task AddTransactionResultsAsync(IList transactionResult, BlockHeader blockHeader); - Task ProcessTransactionResultAfterExecutionAsync(BlockHeader blockHeader, List transactionIds); + Task AddFailedTransactionResultsAsync(TransactionResult transactionResult); } public class TransactionResultService : ITransactionResultService, ITransientDependency { + private static readonly IEnumerable FailStatus = new List + { + TransactionResultStatus.Failed, TransactionResultStatus.NodeValidationFailed, TransactionResultStatus.Conflict + }; + private readonly ITransactionBlockIndexService _transactionBlockIndexService; private readonly ITransactionResultManager _transactionResultManager; @@ -27,6 +34,28 @@ public TransactionResultService(ITransactionResultManager transactionResultManag _transactionBlockIndexService = transactionBlockIndexService; } + public async Task AddFailedTransactionResultsAsync(TransactionResult transactionResult) + { + if (!FailStatus.Contains(transactionResult.Status)) return; + await _transactionResultManager.AddFailedTransactionResultAsync(transactionResult); + } + + public async Task AddFailedTransactionResultsAsync(IList transactionResults) + { + var failTransactionResult = transactionResults + .Where(r => FailStatus.Contains(r.Status)).ToList(); + if (failTransactionResult.IsNullOrEmpty()) return; + + await _transactionResultManager.AddFailedTransactionResultsAsync(failTransactionResult); + } + + public async Task GetFailedTransactionResultAsync(Hash transactionId) + { + var transactionResult = await _transactionResultManager.GetFailedTransactionResultAsync(transactionId); + transactionResult.TransactionId = transactionId; + return transactionResult; + } + public async Task AddTransactionResultsAsync(IList transactionResults, BlockHeader blockHeader) { diff --git a/src/AElf.Kernel.Core/Blockchain/Domain/ITransactionResultManager.cs b/src/AElf.Kernel.Core/Blockchain/Domain/ITransactionResultManager.cs index e2f2176886..964841541c 100644 --- a/src/AElf.Kernel.Core/Blockchain/Domain/ITransactionResultManager.cs +++ b/src/AElf.Kernel.Core/Blockchain/Domain/ITransactionResultManager.cs @@ -7,15 +7,19 @@ namespace AElf.Kernel.Blockchain.Domain; public interface ITransactionResultManager { Task AddTransactionResultAsync(TransactionResult transactionResult, Hash disambiguationHash); - Task AddTransactionResultsAsync(IList transactionResults, Hash disambiguationHash); Task GetTransactionResultAsync(Hash txId, Hash disambiguationHash); Task> GetTransactionResultsAsync(IList txIds, Hash disambiguationHash); Task HasTransactionResultAsync(Hash transactionId, Hash disambiguationHash); + Task AddFailedTransactionResultAsync(TransactionResult transactionResult); + Task AddFailedTransactionResultsAsync(IList transactionResults); + Task GetFailedTransactionResultAsync(Hash transactionId); + Task> GetFailedTransactionResultsAsync(IList txIds); } public class TransactionResultManager : ITransactionResultManager { + private const string FailStorageKeyPrefix = "FAIL:"; private readonly IBlockchainStore _transactionResultStore; public TransactionResultManager(IBlockchainStore transactionResultStore) @@ -23,6 +27,29 @@ public TransactionResultManager(IBlockchainStore transactionR _transactionResultStore = transactionResultStore; } + public async Task AddFailedTransactionResultAsync(TransactionResult transactionResult) + { + await _transactionResultStore.SetAsync(FailStorageKeyPrefix + transactionResult.TransactionId.ToStorageKey(), transactionResult); + } + + public async Task AddFailedTransactionResultsAsync(IList transactionResults) + { + await _transactionResultStore.SetAllAsync( + transactionResults.ToDictionary(tx => FailStorageKeyPrefix + tx.TransactionId.ToStorageKey(), t => t) + ); + } + + public async Task GetFailedTransactionResultAsync(Hash transactionId) + { + return await _transactionResultStore.GetAsync(FailStorageKeyPrefix + transactionId.ToStorageKey()); + } + + public async Task> GetFailedTransactionResultsAsync(IList txIds) + { + return await _transactionResultStore.GetAllAsync(txIds.Select(id => FailStorageKeyPrefix + id.ToStorageKey()).ToList()); + } + + public async Task AddTransactionResultAsync(TransactionResult transactionResult, Hash disambiguationHash) { await _transactionResultStore.SetAsync( diff --git a/src/AElf.WebApp.Application.Chain/Application/TransactionValidationStatusChangedEventHandler.cs b/src/AElf.WebApp.Application.Chain/Application/TransactionValidationStatusChangedEventHandler.cs index 890740441c..ef9edb620f 100644 --- a/src/AElf.WebApp.Application.Chain/Application/TransactionValidationStatusChangedEventHandler.cs +++ b/src/AElf.WebApp.Application.Chain/Application/TransactionValidationStatusChangedEventHandler.cs @@ -1,6 +1,9 @@ using System.Threading.Tasks; using AElf.Kernel; +using AElf.Kernel.Blockchain.Application; +using AElf.Types; using AElf.WebApp.Application.Chain.Infrastructure; +using Microsoft.Extensions.Options; using Volo.Abp.DependencyInjection; using Volo.Abp.EventBus; @@ -11,21 +14,38 @@ public class TransactionValidationStatusChangedEventHandler : ITransientDependency { private readonly ITransactionResultStatusCacheProvider _transactionResultStatusCacheProvider; + private readonly ITransactionResultService _transactionResultService; + private readonly WebAppOptions _webAppOptions; public TransactionValidationStatusChangedEventHandler( - ITransactionResultStatusCacheProvider transactionResultStatusCacheProvider) + ITransactionResultStatusCacheProvider transactionResultStatusCacheProvider, + ITransactionResultService transactionResultService, + IOptionsMonitor optionsSnapshot) { _transactionResultStatusCacheProvider = transactionResultStatusCacheProvider; + _transactionResultService = transactionResultService; + _webAppOptions = optionsSnapshot.CurrentValue; } public Task HandleEventAsync(TransactionValidationStatusChangedEvent eventData) { + + // save to local cache _transactionResultStatusCacheProvider.ChangeTransactionResultStatus(eventData.TransactionId, new TransactionValidateStatus { TransactionResultStatus = eventData.TransactionResultStatus, Error = eventData.Error }); + + // save to storage + _transactionResultService.AddFailedTransactionResultsAsync(new TransactionResult + { + TransactionId = eventData.TransactionId, + Status = eventData.TransactionResultStatus, + Error = TransactionErrorResolver.TakeErrorMessage(eventData.Error, _webAppOptions.IsDebugMode) + }); + return Task.CompletedTask; } } \ No newline at end of file diff --git a/src/AElf.WebApp.Application.Chain/Services/TransactionResultAppService.cs b/src/AElf.WebApp.Application.Chain/Services/TransactionResultAppService.cs index 08552f42e5..c52fae025b 100644 --- a/src/AElf.WebApp.Application.Chain/Services/TransactionResultAppService.cs +++ b/src/AElf.WebApp.Application.Chain/Services/TransactionResultAppService.cs @@ -88,23 +88,34 @@ public async Task GetTransactionResultAsync(string transac output.Transaction = _objectMapper.Map(transaction); output.TransactionSize = transaction?.CalculateSize() ?? 0; - if (transactionResult.Status == TransactionResultStatus.NotExisted) + if (transactionResult.Status != TransactionResultStatus.NotExisted) { - var validationStatus = - _transactionResultStatusCacheProvider.GetTransactionResultStatus(transactionIdHash); - if (validationStatus != null) - { - output.Status = validationStatus.TransactionResultStatus.ToString().ToUpper(); - output.Error = - TransactionErrorResolver.TakeErrorMessage(validationStatus.Error, _webAppOptions.IsDebugMode); - } + await FormatTransactionParamsAsync(output.Transaction, transaction.Params); + return output; + } + var validationStatus = _transactionResultStatusCacheProvider.GetTransactionResultStatus(transactionIdHash); + if (validationStatus != null) + { + output.Status = validationStatus.TransactionResultStatus.ToString().ToUpper(); + output.Error = + TransactionErrorResolver.TakeErrorMessage(validationStatus.Error, _webAppOptions.IsDebugMode); return output; } - await FormatTransactionParamsAsync(output.Transaction, transaction.Params); - + var failedTransactionResult = + await _transactionResultProxyService.TransactionResultQueryService.GetFailedTransactionResultAsync( + transactionIdHash); + if (failedTransactionResult != null) + { + output.Status = failedTransactionResult.Status.ToString().ToUpper(); + output.Error = + TransactionErrorResolver.TakeErrorMessage(failedTransactionResult.Error, _webAppOptions.IsDebugMode); + return output; + } + return output; + } /// From 3a05484ab0f6110b1754a4543d196fb5a4336971 Mon Sep 17 00:00:00 2001 From: "zecong.nian" Date: Fri, 1 Dec 2023 19:56:02 +0800 Subject: [PATCH 2/7] feat: save failed tx result to tf storage --- protobuf/aelf/core.proto | 9 +++++ .../Application/ITransactionResultService.cs | 31 +-------------- .../Domain/ITransactionFailedResultManager.cs | 38 +++++++++++++++++++ .../TransactionOptions.cs | 7 ++++ ...tionValidationStatusChangedEventHandler.cs | 29 +++++++++----- .../ChainApplicationWebAppAElfModule.cs | 18 +++++++++ .../TransactionFailedResultService.cs | 34 +++++++++++++++++ .../Services/TransactionResultAppService.cs | 2 +- .../Services/TransactionResultProxyService.cs | 7 +++- 9 files changed, 134 insertions(+), 41 deletions(-) create mode 100644 src/AElf.Kernel.Core/Blockchain/Domain/ITransactionFailedResultManager.cs create mode 100644 src/AElf.WebApp.Application.Chain/Services/TransactionFailedResultService.cs diff --git a/protobuf/aelf/core.proto b/protobuf/aelf/core.proto index c990aa9df6..cbf739892c 100644 --- a/protobuf/aelf/core.proto +++ b/protobuf/aelf/core.proto @@ -72,6 +72,15 @@ message TransactionResult { string error = 10; } +message TransactionFailedResult { + // The transaction id. + Hash transaction_id = 1; + // The transaction result status. + TransactionResultStatus status = 2; + // Failed execution error message. + string error = 3; +} + message LogEvent { // The contract address. Address address = 1; diff --git a/src/AElf.Kernel.Core/Blockchain/Application/ITransactionResultService.cs b/src/AElf.Kernel.Core/Blockchain/Application/ITransactionResultService.cs index f660f3b7ec..c2d6e0b2de 100644 --- a/src/AElf.Kernel.Core/Blockchain/Application/ITransactionResultService.cs +++ b/src/AElf.Kernel.Core/Blockchain/Application/ITransactionResultService.cs @@ -1,4 +1,3 @@ -using System.Linq; using AElf.Kernel.Blockchain.Domain; namespace AElf.Kernel.Blockchain.Application; @@ -7,23 +6,17 @@ public interface ITransactionResultQueryService { Task GetTransactionResultAsync(Hash transactionId); Task GetTransactionResultAsync(Hash transactionId, Hash blockHash); - Task GetFailedTransactionResultAsync(Hash transactionId); } public interface ITransactionResultService : ITransactionResultQueryService { Task AddTransactionResultsAsync(IList transactionResult, BlockHeader blockHeader); + Task ProcessTransactionResultAfterExecutionAsync(BlockHeader blockHeader, List transactionIds); - Task AddFailedTransactionResultsAsync(TransactionResult transactionResult); } public class TransactionResultService : ITransactionResultService, ITransientDependency { - private static readonly IEnumerable FailStatus = new List - { - TransactionResultStatus.Failed, TransactionResultStatus.NodeValidationFailed, TransactionResultStatus.Conflict - }; - private readonly ITransactionBlockIndexService _transactionBlockIndexService; private readonly ITransactionResultManager _transactionResultManager; @@ -34,28 +27,6 @@ public TransactionResultService(ITransactionResultManager transactionResultManag _transactionBlockIndexService = transactionBlockIndexService; } - public async Task AddFailedTransactionResultsAsync(TransactionResult transactionResult) - { - if (!FailStatus.Contains(transactionResult.Status)) return; - await _transactionResultManager.AddFailedTransactionResultAsync(transactionResult); - } - - public async Task AddFailedTransactionResultsAsync(IList transactionResults) - { - var failTransactionResult = transactionResults - .Where(r => FailStatus.Contains(r.Status)).ToList(); - if (failTransactionResult.IsNullOrEmpty()) return; - - await _transactionResultManager.AddFailedTransactionResultsAsync(failTransactionResult); - } - - public async Task GetFailedTransactionResultAsync(Hash transactionId) - { - var transactionResult = await _transactionResultManager.GetFailedTransactionResultAsync(transactionId); - transactionResult.TransactionId = transactionId; - return transactionResult; - } - public async Task AddTransactionResultsAsync(IList transactionResults, BlockHeader blockHeader) { diff --git a/src/AElf.Kernel.Core/Blockchain/Domain/ITransactionFailedResultManager.cs b/src/AElf.Kernel.Core/Blockchain/Domain/ITransactionFailedResultManager.cs new file mode 100644 index 0000000000..1d9b05c13f --- /dev/null +++ b/src/AElf.Kernel.Core/Blockchain/Domain/ITransactionFailedResultManager.cs @@ -0,0 +1,38 @@ +using System.Linq; +using AElf.Kernel.Blockchain.Infrastructure; +using AElf.Kernel.Infrastructure; + +namespace AElf.Kernel.Blockchain.Domain; + +public interface ITransactionFailedResultManager +{ + Task AddFailedTransactionResultAsync(Hash transactionId, TransactionFailedResult transactionResult); + Task GetFailedTransactionResultAsync(Hash transactionId); +} + +public class TransactionFailedResultManager : ITransactionFailedResultManager +{ + private readonly IBlockchainStore _transactionFailedResultStore; + + public TransactionFailedResultManager(IBlockchainStore transactionFailedResultStore) + { + _transactionFailedResultStore = transactionFailedResultStore; + } + + public async Task AddFailedTransactionResultAsync(Hash transactionId, TransactionFailedResult transactionResult) + { + await _transactionFailedResultStore.SetAsync(transactionId.ToStorageKey(), transactionResult); + } + + + public async Task GetFailedTransactionResultAsync(Hash transactionId) + { + var failedResult = await _transactionFailedResultStore.GetAsync(transactionId.ToStorageKey()); + if (failedResult != null) + { + failedResult.TransactionId = transactionId; + } + return failedResult; + } + +} \ No newline at end of file diff --git a/src/AElf.Kernel.TransactionPool/TransactionOptions.cs b/src/AElf.Kernel.TransactionPool/TransactionOptions.cs index 7847da5094..b99b43d10c 100644 --- a/src/AElf.Kernel.TransactionPool/TransactionOptions.cs +++ b/src/AElf.Kernel.TransactionPool/TransactionOptions.cs @@ -17,4 +17,11 @@ public class TransactionOptions /// But common node needs to enable it to prevent transaction flood attack /// public bool EnableTransactionExecutionValidation { get; set; } = true; + + + /// + /// Configuration whether to save failed transaction results + /// + public bool SaveFailedResult { get; set; } + } \ No newline at end of file diff --git a/src/AElf.WebApp.Application.Chain/Application/TransactionValidationStatusChangedEventHandler.cs b/src/AElf.WebApp.Application.Chain/Application/TransactionValidationStatusChangedEventHandler.cs index ef9edb620f..a5ca046188 100644 --- a/src/AElf.WebApp.Application.Chain/Application/TransactionValidationStatusChangedEventHandler.cs +++ b/src/AElf.WebApp.Application.Chain/Application/TransactionValidationStatusChangedEventHandler.cs @@ -1,8 +1,10 @@ using System.Threading.Tasks; using AElf.Kernel; using AElf.Kernel.Blockchain.Application; +using AElf.Kernel.TransactionPool; using AElf.Types; using AElf.WebApp.Application.Chain.Infrastructure; +using AElf.WebApp.Application.Chain.Services; using Microsoft.Extensions.Options; using Volo.Abp.DependencyInjection; using Volo.Abp.EventBus; @@ -15,16 +17,22 @@ public class TransactionValidationStatusChangedEventHandler : { private readonly ITransactionResultStatusCacheProvider _transactionResultStatusCacheProvider; private readonly ITransactionResultService _transactionResultService; + private readonly ITransactionFailedResultService _transactionFailedResultService; private readonly WebAppOptions _webAppOptions; + private readonly TransactionOptions _transactionOptions; public TransactionValidationStatusChangedEventHandler( ITransactionResultStatusCacheProvider transactionResultStatusCacheProvider, ITransactionResultService transactionResultService, - IOptionsMonitor optionsSnapshot) + IOptionsMonitor optionsSnapshot, + IOptionsMonitor transactionOptions, + ITransactionFailedResultService transactionFailedResultService) { _transactionResultStatusCacheProvider = transactionResultStatusCacheProvider; _transactionResultService = transactionResultService; + _transactionFailedResultService = transactionFailedResultService; _webAppOptions = optionsSnapshot.CurrentValue; + _transactionOptions = transactionOptions.CurrentValue; } public Task HandleEventAsync(TransactionValidationStatusChangedEvent eventData) @@ -37,15 +45,18 @@ public Task HandleEventAsync(TransactionValidationStatusChangedEvent eventData) TransactionResultStatus = eventData.TransactionResultStatus, Error = eventData.Error }); - - // save to storage - _transactionResultService.AddFailedTransactionResultsAsync(new TransactionResult + + if (_transactionOptions.SaveFailedResult) { - TransactionId = eventData.TransactionId, - Status = eventData.TransactionResultStatus, - Error = TransactionErrorResolver.TakeErrorMessage(eventData.Error, _webAppOptions.IsDebugMode) - }); - + // save to storage + _transactionFailedResultService.AddFailedTransactionResultsAsync(eventData.TransactionId, + new TransactionFailedResult + { + Status = eventData.TransactionResultStatus, + Error = TransactionErrorResolver.TakeErrorMessage(eventData.Error, _webAppOptions.IsDebugMode) + }); + } + return Task.CompletedTask; } } \ No newline at end of file diff --git a/src/AElf.WebApp.Application.Chain/ChainApplicationWebAppAElfModule.cs b/src/AElf.WebApp.Application.Chain/ChainApplicationWebAppAElfModule.cs index d5820e1828..b21fc539ee 100644 --- a/src/AElf.WebApp.Application.Chain/ChainApplicationWebAppAElfModule.cs +++ b/src/AElf.WebApp.Application.Chain/ChainApplicationWebAppAElfModule.cs @@ -1,6 +1,9 @@ using AElf.Kernel; +using AElf.Kernel.Infrastructure; using AElf.Modularity; +using AElf.Types; using AElf.WebApp.Application.Chain.Infrastructure; +using Google.Protobuf; using Microsoft.Extensions.DependencyInjection; using Volo.Abp.AutoMapper; using Volo.Abp.Modularity; @@ -22,5 +25,20 @@ public override void ConfigureServices(ServiceConfigurationContext context) context.Services .AddSingleton(); + + context.Services.AddStoreKeyPrefixProvide("tf"); + } +} + +public static class StoreKeyPrefixProviderServiceCollectionExtensions +{ + public static IServiceCollection AddStoreKeyPrefixProvide( + this IServiceCollection serviceCollection, string prefix) + where T : IMessage, new() + { + serviceCollection.AddTransient>(c => + new FastStoreKeyPrefixProvider(prefix)); + + return serviceCollection; } } \ No newline at end of file diff --git a/src/AElf.WebApp.Application.Chain/Services/TransactionFailedResultService.cs b/src/AElf.WebApp.Application.Chain/Services/TransactionFailedResultService.cs new file mode 100644 index 0000000000..66be6301ed --- /dev/null +++ b/src/AElf.WebApp.Application.Chain/Services/TransactionFailedResultService.cs @@ -0,0 +1,34 @@ +using System.Threading.Tasks; +using AElf.Kernel.Blockchain.Domain; +using AElf.Types; +using Volo.Abp.DependencyInjection; + +namespace AElf.WebApp.Application.Chain.Services; + + +public interface ITransactionFailedResultService +{ + Task AddFailedTransactionResultsAsync(Hash transactionId, TransactionFailedResult transactionResult); + Task GetFailedTransactionResultAsync(Hash transactionId); +} + +public class TransactionFailedResultService : ITransactionFailedResultService, ITransientDependency +{ + private readonly ITransactionFailedResultManager _transactionFailedResultManager; + + public TransactionFailedResultService(ITransactionFailedResultManager transactionFailedResultManager) + { + _transactionFailedResultManager = transactionFailedResultManager; + } + + + public async Task AddFailedTransactionResultsAsync(Hash transactionId, TransactionFailedResult transactionResult) + { + await _transactionFailedResultManager.AddFailedTransactionResultAsync(transactionId, transactionResult); + } + + public async Task GetFailedTransactionResultAsync(Hash transactionId) + { + return await _transactionFailedResultManager.GetFailedTransactionResultAsync(transactionId); + } +} \ No newline at end of file diff --git a/src/AElf.WebApp.Application.Chain/Services/TransactionResultAppService.cs b/src/AElf.WebApp.Application.Chain/Services/TransactionResultAppService.cs index c52fae025b..fc690541ba 100644 --- a/src/AElf.WebApp.Application.Chain/Services/TransactionResultAppService.cs +++ b/src/AElf.WebApp.Application.Chain/Services/TransactionResultAppService.cs @@ -104,7 +104,7 @@ public async Task GetTransactionResultAsync(string transac } var failedTransactionResult = - await _transactionResultProxyService.TransactionResultQueryService.GetFailedTransactionResultAsync( + await _transactionResultProxyService.TransactionFailedResultService.GetFailedTransactionResultAsync( transactionIdHash); if (failedTransactionResult != null) { diff --git a/src/AElf.WebApp.Application.Chain/Services/TransactionResultProxyService.cs b/src/AElf.WebApp.Application.Chain/Services/TransactionResultProxyService.cs index 5ebfcc8598..99f088818e 100644 --- a/src/AElf.WebApp.Application.Chain/Services/TransactionResultProxyService.cs +++ b/src/AElf.WebApp.Application.Chain/Services/TransactionResultProxyService.cs @@ -1,5 +1,6 @@ using AElf.Kernel.Blockchain.Application; using AElf.Kernel.TransactionPool.Application; +using AElf.WebApp.Application.Chain.Services; namespace AElf.WebApp.Application.Chain; @@ -7,17 +8,21 @@ public interface ITransactionResultProxyService { ITransactionPoolService TransactionPoolService { get; } ITransactionResultQueryService TransactionResultQueryService { get; } + ITransactionFailedResultService TransactionFailedResultService { get; } } public class TransactionResultProxyService : ITransactionResultProxyService { public TransactionResultProxyService(ITransactionPoolService transactionPoolService, - ITransactionResultQueryService transactionResultQueryService) + ITransactionResultQueryService transactionResultQueryService, + ITransactionFailedResultService transactionFailedResultService) { TransactionPoolService = transactionPoolService; TransactionResultQueryService = transactionResultQueryService; + TransactionFailedResultService = transactionFailedResultService; } public ITransactionPoolService TransactionPoolService { get; set; } public ITransactionResultQueryService TransactionResultQueryService { get; set; } + public ITransactionFailedResultService TransactionFailedResultService { get; } } \ No newline at end of file From 15e5093484a78d27e284637ad09d314604d1e6f2 Mon Sep 17 00:00:00 2001 From: "zecong.nian" Date: Wed, 6 Dec 2023 23:14:30 +0800 Subject: [PATCH 3/7] feat: save invalid transaction result --- protobuf/aelf/core.proto | 2 +- .../Domain/ITransactionFailedResultManager.cs | 21 +++---- src/AElf.Kernel.Core/CoreKernelAElfModule.cs | 2 +- .../TransactionFailedResultService.cs | 15 +++-- ...ctionValidationStatusFailedEventHandler.cs | 58 +++++++++++++++++++ .../TransactionOptions.cs | 3 +- src/AElf.Launcher/appsettings.json | 1 + ...tionValidationStatusChangedEventHandler.cs | 33 +---------- .../ChainApplicationWebAppAElfModule.cs | 17 ------ .../Services/TransactionResultProxyService.cs | 1 - 10 files changed, 79 insertions(+), 74 deletions(-) rename src/{AElf.WebApp.Application.Chain/Services => AElf.Kernel.TransactionPool/Application}/TransactionFailedResultService.cs (59%) create mode 100644 src/AElf.Kernel.TransactionPool/Handler/TransactionValidationStatusFailedEventHandler.cs diff --git a/protobuf/aelf/core.proto b/protobuf/aelf/core.proto index cbf739892c..c0ca910f0d 100644 --- a/protobuf/aelf/core.proto +++ b/protobuf/aelf/core.proto @@ -72,7 +72,7 @@ message TransactionResult { string error = 10; } -message TransactionFailedResult { +message TransactionValidationFailure{ // The transaction id. Hash transaction_id = 1; // The transaction result status. diff --git a/src/AElf.Kernel.Core/Blockchain/Domain/ITransactionFailedResultManager.cs b/src/AElf.Kernel.Core/Blockchain/Domain/ITransactionFailedResultManager.cs index 1d9b05c13f..b2a96da421 100644 --- a/src/AElf.Kernel.Core/Blockchain/Domain/ITransactionFailedResultManager.cs +++ b/src/AElf.Kernel.Core/Blockchain/Domain/ITransactionFailedResultManager.cs @@ -6,33 +6,28 @@ namespace AElf.Kernel.Blockchain.Domain; public interface ITransactionFailedResultManager { - Task AddFailedTransactionResultAsync(Hash transactionId, TransactionFailedResult transactionResult); - Task GetFailedTransactionResultAsync(Hash transactionId); + Task AddFailedTransactionResultAsync(TransactionValidationFailure transactionResult); + Task GetFailedTransactionResultAsync(Hash transactionId); } public class TransactionFailedResultManager : ITransactionFailedResultManager { - private readonly IBlockchainStore _transactionFailedResultStore; + private readonly IBlockchainStore _transactionFailedResultStore; - public TransactionFailedResultManager(IBlockchainStore transactionFailedResultStore) + public TransactionFailedResultManager(IBlockchainStore transactionFailedResultStore) { _transactionFailedResultStore = transactionFailedResultStore; } - public async Task AddFailedTransactionResultAsync(Hash transactionId, TransactionFailedResult transactionResult) + public async Task AddFailedTransactionResultAsync(TransactionValidationFailure transactionResult) { - await _transactionFailedResultStore.SetAsync(transactionId.ToStorageKey(), transactionResult); + await _transactionFailedResultStore.SetAsync(transactionResult.TransactionId.ToStorageKey(), transactionResult); } - public async Task GetFailedTransactionResultAsync(Hash transactionId) + public async Task GetFailedTransactionResultAsync(Hash transactionId) { - var failedResult = await _transactionFailedResultStore.GetAsync(transactionId.ToStorageKey()); - if (failedResult != null) - { - failedResult.TransactionId = transactionId; - } - return failedResult; + return await _transactionFailedResultStore.GetAsync(transactionId.ToStorageKey()); } } \ No newline at end of file diff --git a/src/AElf.Kernel.Core/CoreKernelAElfModule.cs b/src/AElf.Kernel.Core/CoreKernelAElfModule.cs index 86df74b631..f40455f686 100644 --- a/src/AElf.Kernel.Core/CoreKernelAElfModule.cs +++ b/src/AElf.Kernel.Core/CoreKernelAElfModule.cs @@ -39,7 +39,7 @@ public override void ConfigureServices(ServiceConfigurationContext context) services.AddStoreKeyPrefixProvide("ti"); services.AddStoreKeyPrefixProvide("tr"); services.AddStoreKeyPrefixProvide("vs"); - + services.AddStoreKeyPrefixProvide("tf"); services.AddTransient(typeof(IStateStore<>), typeof(StateStore<>)); services.AddSingleton(typeof(INotModifiedCachedStateStore<>), typeof(NotModifiedCachedStateStore<>)); diff --git a/src/AElf.WebApp.Application.Chain/Services/TransactionFailedResultService.cs b/src/AElf.Kernel.TransactionPool/Application/TransactionFailedResultService.cs similarity index 59% rename from src/AElf.WebApp.Application.Chain/Services/TransactionFailedResultService.cs rename to src/AElf.Kernel.TransactionPool/Application/TransactionFailedResultService.cs index 66be6301ed..c9b6a23e3c 100644 --- a/src/AElf.WebApp.Application.Chain/Services/TransactionFailedResultService.cs +++ b/src/AElf.Kernel.TransactionPool/Application/TransactionFailedResultService.cs @@ -3,13 +3,13 @@ using AElf.Types; using Volo.Abp.DependencyInjection; -namespace AElf.WebApp.Application.Chain.Services; +namespace AElf.Kernel.TransactionPool.Application; public interface ITransactionFailedResultService { - Task AddFailedTransactionResultsAsync(Hash transactionId, TransactionFailedResult transactionResult); - Task GetFailedTransactionResultAsync(Hash transactionId); + Task AddFailedTransactionResultsAsync(TransactionValidationFailure transactionResult); + Task GetFailedTransactionResultAsync(Hash transactionId); } public class TransactionFailedResultService : ITransactionFailedResultService, ITransientDependency @@ -20,14 +20,13 @@ public TransactionFailedResultService(ITransactionFailedResultManager transactio { _transactionFailedResultManager = transactionFailedResultManager; } - - - public async Task AddFailedTransactionResultsAsync(Hash transactionId, TransactionFailedResult transactionResult) + + public async Task AddFailedTransactionResultsAsync(TransactionValidationFailure transactionResult) { - await _transactionFailedResultManager.AddFailedTransactionResultAsync(transactionId, transactionResult); + await _transactionFailedResultManager.AddFailedTransactionResultAsync(transactionResult); } - public async Task GetFailedTransactionResultAsync(Hash transactionId) + public async Task GetFailedTransactionResultAsync(Hash transactionId) { return await _transactionFailedResultManager.GetFailedTransactionResultAsync(transactionId); } diff --git a/src/AElf.Kernel.TransactionPool/Handler/TransactionValidationStatusFailedEventHandler.cs b/src/AElf.Kernel.TransactionPool/Handler/TransactionValidationStatusFailedEventHandler.cs new file mode 100644 index 0000000000..ec6b2ff26d --- /dev/null +++ b/src/AElf.Kernel.TransactionPool/Handler/TransactionValidationStatusFailedEventHandler.cs @@ -0,0 +1,58 @@ +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Threading.Tasks; +using AElf.Kernel.TransactionPool.Application; +using AElf.Types; +using Microsoft.Extensions.Options; +using Volo.Abp.DependencyInjection; +using Volo.Abp.EventBus; + +namespace AElf.Kernel.TransactionPool.Handler; + +public class TransactionValidationStatusFailedEventHandler : + ILocalEventHandler, + ITransientDependency +{ + private static readonly IEnumerable FailStatus = new List + { + TransactionResultStatus.Failed, TransactionResultStatus.NodeValidationFailed, TransactionResultStatus.Conflict + }; + + private readonly ITransactionFailedResultService _transactionFailedResultService; + private readonly TransactionOptions _transactionOptions; + + public TransactionValidationStatusFailedEventHandler( + IOptionsMonitor transactionOptionsMonitor, + ITransactionFailedResultService transactionFailedResultService) + { + _transactionFailedResultService = transactionFailedResultService; + _transactionOptions = transactionOptionsMonitor.CurrentValue; + } + + public Task HandleEventAsync(TransactionValidationStatusChangedEvent eventData) + { + if (!FailStatus.Contains(eventData.TransactionResultStatus)) return Task.CompletedTask; + if (!_transactionOptions.SaveInvalidTransactionResult) return Task.CompletedTask; + + // save to storage + _transactionFailedResultService.AddFailedTransactionResultsAsync( + new TransactionValidationFailure + { + TransactionId = eventData.TransactionId, + Status = eventData.TransactionResultStatus, + Error = TakeErrorMessage(eventData.Error) + }); + + return Task.CompletedTask; + } + + public static string TakeErrorMessage(string transactionResultError) + { + if (string.IsNullOrWhiteSpace(transactionResultError)) + return null; + using var stringReader = new StringReader(transactionResultError); + return stringReader.ReadLine(); + } + +} \ No newline at end of file diff --git a/src/AElf.Kernel.TransactionPool/TransactionOptions.cs b/src/AElf.Kernel.TransactionPool/TransactionOptions.cs index b99b43d10c..4170415511 100644 --- a/src/AElf.Kernel.TransactionPool/TransactionOptions.cs +++ b/src/AElf.Kernel.TransactionPool/TransactionOptions.cs @@ -22,6 +22,7 @@ public class TransactionOptions /// /// Configuration whether to save failed transaction results /// - public bool SaveFailedResult { get; set; } + public bool SaveInvalidTransactionResult { get; set; } + } \ No newline at end of file diff --git a/src/AElf.Launcher/appsettings.json b/src/AElf.Launcher/appsettings.json index 80b0e93f8f..a7a524721a 100644 --- a/src/AElf.Launcher/appsettings.json +++ b/src/AElf.Launcher/appsettings.json @@ -31,6 +31,7 @@ "MinerIncreaseInterval": 31536000 }, "Transaction": { + "SaveInvalidTransactionResult" : true, "PoolLimit": 10240 }, "BasicAuth": { diff --git a/src/AElf.WebApp.Application.Chain/Application/TransactionValidationStatusChangedEventHandler.cs b/src/AElf.WebApp.Application.Chain/Application/TransactionValidationStatusChangedEventHandler.cs index a5ca046188..890740441c 100644 --- a/src/AElf.WebApp.Application.Chain/Application/TransactionValidationStatusChangedEventHandler.cs +++ b/src/AElf.WebApp.Application.Chain/Application/TransactionValidationStatusChangedEventHandler.cs @@ -1,11 +1,6 @@ using System.Threading.Tasks; using AElf.Kernel; -using AElf.Kernel.Blockchain.Application; -using AElf.Kernel.TransactionPool; -using AElf.Types; using AElf.WebApp.Application.Chain.Infrastructure; -using AElf.WebApp.Application.Chain.Services; -using Microsoft.Extensions.Options; using Volo.Abp.DependencyInjection; using Volo.Abp.EventBus; @@ -16,47 +11,21 @@ public class TransactionValidationStatusChangedEventHandler : ITransientDependency { private readonly ITransactionResultStatusCacheProvider _transactionResultStatusCacheProvider; - private readonly ITransactionResultService _transactionResultService; - private readonly ITransactionFailedResultService _transactionFailedResultService; - private readonly WebAppOptions _webAppOptions; - private readonly TransactionOptions _transactionOptions; public TransactionValidationStatusChangedEventHandler( - ITransactionResultStatusCacheProvider transactionResultStatusCacheProvider, - ITransactionResultService transactionResultService, - IOptionsMonitor optionsSnapshot, - IOptionsMonitor transactionOptions, - ITransactionFailedResultService transactionFailedResultService) + ITransactionResultStatusCacheProvider transactionResultStatusCacheProvider) { _transactionResultStatusCacheProvider = transactionResultStatusCacheProvider; - _transactionResultService = transactionResultService; - _transactionFailedResultService = transactionFailedResultService; - _webAppOptions = optionsSnapshot.CurrentValue; - _transactionOptions = transactionOptions.CurrentValue; } public Task HandleEventAsync(TransactionValidationStatusChangedEvent eventData) { - - // save to local cache _transactionResultStatusCacheProvider.ChangeTransactionResultStatus(eventData.TransactionId, new TransactionValidateStatus { TransactionResultStatus = eventData.TransactionResultStatus, Error = eventData.Error }); - - if (_transactionOptions.SaveFailedResult) - { - // save to storage - _transactionFailedResultService.AddFailedTransactionResultsAsync(eventData.TransactionId, - new TransactionFailedResult - { - Status = eventData.TransactionResultStatus, - Error = TransactionErrorResolver.TakeErrorMessage(eventData.Error, _webAppOptions.IsDebugMode) - }); - } - return Task.CompletedTask; } } \ No newline at end of file diff --git a/src/AElf.WebApp.Application.Chain/ChainApplicationWebAppAElfModule.cs b/src/AElf.WebApp.Application.Chain/ChainApplicationWebAppAElfModule.cs index b21fc539ee..d6e09b36f8 100644 --- a/src/AElf.WebApp.Application.Chain/ChainApplicationWebAppAElfModule.cs +++ b/src/AElf.WebApp.Application.Chain/ChainApplicationWebAppAElfModule.cs @@ -1,9 +1,6 @@ using AElf.Kernel; -using AElf.Kernel.Infrastructure; using AElf.Modularity; -using AElf.Types; using AElf.WebApp.Application.Chain.Infrastructure; -using Google.Protobuf; using Microsoft.Extensions.DependencyInjection; using Volo.Abp.AutoMapper; using Volo.Abp.Modularity; @@ -26,19 +23,5 @@ public override void ConfigureServices(ServiceConfigurationContext context) context.Services .AddSingleton(); - context.Services.AddStoreKeyPrefixProvide("tf"); - } -} - -public static class StoreKeyPrefixProviderServiceCollectionExtensions -{ - public static IServiceCollection AddStoreKeyPrefixProvide( - this IServiceCollection serviceCollection, string prefix) - where T : IMessage, new() - { - serviceCollection.AddTransient>(c => - new FastStoreKeyPrefixProvider(prefix)); - - return serviceCollection; } } \ No newline at end of file diff --git a/src/AElf.WebApp.Application.Chain/Services/TransactionResultProxyService.cs b/src/AElf.WebApp.Application.Chain/Services/TransactionResultProxyService.cs index 99f088818e..cf02dcf037 100644 --- a/src/AElf.WebApp.Application.Chain/Services/TransactionResultProxyService.cs +++ b/src/AElf.WebApp.Application.Chain/Services/TransactionResultProxyService.cs @@ -1,6 +1,5 @@ using AElf.Kernel.Blockchain.Application; using AElf.Kernel.TransactionPool.Application; -using AElf.WebApp.Application.Chain.Services; namespace AElf.WebApp.Application.Chain; From 1a520c37196d9b90963df781ec6c3f8fc943dbc8 Mon Sep 17 00:00:00 2001 From: "zecong.nian" Date: Wed, 13 Dec 2023 11:36:04 +0800 Subject: [PATCH 4/7] feat: rename --- protobuf/aelf/core.proto | 2 +- .../Domain/ITransactionFailedResultManager.cs | 33 ------------------- .../ITransactionInvalidResultManager.cs | 33 +++++++++++++++++++ src/AElf.Kernel.Core/CoreKernelAElfModule.cs | 2 +- .../ITransactionInvalidResultService.cs | 33 +++++++++++++++++++ .../TransactionFailedResultService.cs | 33 ------------------- ...ctionValidationStatusFailedEventHandler.cs | 12 +++---- .../TransactionOptions.cs | 2 +- src/AElf.Launcher/appsettings.json | 2 +- .../Services/TransactionResultAppService.cs | 2 +- .../Services/TransactionResultProxyService.cs | 8 ++--- .../BlockChainAppServiceTest.cs | 33 +++++++++++++++++++ .../WebAppTestAElfModule.cs | 5 +++ 13 files changed, 119 insertions(+), 81 deletions(-) delete mode 100644 src/AElf.Kernel.Core/Blockchain/Domain/ITransactionFailedResultManager.cs create mode 100644 src/AElf.Kernel.Core/Blockchain/Domain/ITransactionInvalidResultManager.cs create mode 100644 src/AElf.Kernel.TransactionPool/Application/ITransactionInvalidResultService.cs delete mode 100644 src/AElf.Kernel.TransactionPool/Application/TransactionFailedResultService.cs diff --git a/protobuf/aelf/core.proto b/protobuf/aelf/core.proto index c0ca910f0d..e45d7e581d 100644 --- a/protobuf/aelf/core.proto +++ b/protobuf/aelf/core.proto @@ -72,7 +72,7 @@ message TransactionResult { string error = 10; } -message TransactionValidationFailure{ +message InvalidTransactionResult{ // The transaction id. Hash transaction_id = 1; // The transaction result status. diff --git a/src/AElf.Kernel.Core/Blockchain/Domain/ITransactionFailedResultManager.cs b/src/AElf.Kernel.Core/Blockchain/Domain/ITransactionFailedResultManager.cs deleted file mode 100644 index b2a96da421..0000000000 --- a/src/AElf.Kernel.Core/Blockchain/Domain/ITransactionFailedResultManager.cs +++ /dev/null @@ -1,33 +0,0 @@ -using System.Linq; -using AElf.Kernel.Blockchain.Infrastructure; -using AElf.Kernel.Infrastructure; - -namespace AElf.Kernel.Blockchain.Domain; - -public interface ITransactionFailedResultManager -{ - Task AddFailedTransactionResultAsync(TransactionValidationFailure transactionResult); - Task GetFailedTransactionResultAsync(Hash transactionId); -} - -public class TransactionFailedResultManager : ITransactionFailedResultManager -{ - private readonly IBlockchainStore _transactionFailedResultStore; - - public TransactionFailedResultManager(IBlockchainStore transactionFailedResultStore) - { - _transactionFailedResultStore = transactionFailedResultStore; - } - - public async Task AddFailedTransactionResultAsync(TransactionValidationFailure transactionResult) - { - await _transactionFailedResultStore.SetAsync(transactionResult.TransactionId.ToStorageKey(), transactionResult); - } - - - public async Task GetFailedTransactionResultAsync(Hash transactionId) - { - return await _transactionFailedResultStore.GetAsync(transactionId.ToStorageKey()); - } - -} \ No newline at end of file diff --git a/src/AElf.Kernel.Core/Blockchain/Domain/ITransactionInvalidResultManager.cs b/src/AElf.Kernel.Core/Blockchain/Domain/ITransactionInvalidResultManager.cs new file mode 100644 index 0000000000..bb98dbda40 --- /dev/null +++ b/src/AElf.Kernel.Core/Blockchain/Domain/ITransactionInvalidResultManager.cs @@ -0,0 +1,33 @@ +using System.Linq; +using AElf.Kernel.Blockchain.Infrastructure; +using AElf.Kernel.Infrastructure; + +namespace AElf.Kernel.Blockchain.Domain; + +public interface ITransactionInvalidResultManager +{ + Task AddFailedTransactionResultAsync(InvalidTransactionResult transactionResult); + Task GetFailedTransactionResultAsync(Hash transactionId); +} + +public class TransactionInvalidResultManager : ITransactionInvalidResultManager +{ + private readonly IBlockchainStore _transactionFailedResultStore; + + public TransactionInvalidResultManager(IBlockchainStore transactionFailedResultStore) + { + _transactionFailedResultStore = transactionFailedResultStore; + } + + public async Task AddFailedTransactionResultAsync(InvalidTransactionResult transactionResult) + { + await _transactionFailedResultStore.SetAsync(transactionResult.TransactionId.ToStorageKey(), transactionResult); + } + + + public async Task GetFailedTransactionResultAsync(Hash transactionId) + { + return await _transactionFailedResultStore.GetAsync(transactionId.ToStorageKey()); + } + +} \ No newline at end of file diff --git a/src/AElf.Kernel.Core/CoreKernelAElfModule.cs b/src/AElf.Kernel.Core/CoreKernelAElfModule.cs index f40455f686..0dcf8b87bb 100644 --- a/src/AElf.Kernel.Core/CoreKernelAElfModule.cs +++ b/src/AElf.Kernel.Core/CoreKernelAElfModule.cs @@ -39,7 +39,7 @@ public override void ConfigureServices(ServiceConfigurationContext context) services.AddStoreKeyPrefixProvide("ti"); services.AddStoreKeyPrefixProvide("tr"); services.AddStoreKeyPrefixProvide("vs"); - services.AddStoreKeyPrefixProvide("tf"); + services.AddStoreKeyPrefixProvide("ir"); services.AddTransient(typeof(IStateStore<>), typeof(StateStore<>)); services.AddSingleton(typeof(INotModifiedCachedStateStore<>), typeof(NotModifiedCachedStateStore<>)); diff --git a/src/AElf.Kernel.TransactionPool/Application/ITransactionInvalidResultService.cs b/src/AElf.Kernel.TransactionPool/Application/ITransactionInvalidResultService.cs new file mode 100644 index 0000000000..2490e8fad4 --- /dev/null +++ b/src/AElf.Kernel.TransactionPool/Application/ITransactionInvalidResultService.cs @@ -0,0 +1,33 @@ +using System.Threading.Tasks; +using AElf.Kernel.Blockchain.Domain; +using AElf.Types; +using Volo.Abp.DependencyInjection; + +namespace AElf.Kernel.TransactionPool.Application; + + +public interface ITransactionInvalidResultService +{ + Task AddFailedTransactionResultsAsync(InvalidTransactionResult transactionResult); + Task GetFailedTransactionResultAsync(Hash transactionId); +} + +public class TransactionInvalidResultService : ITransactionInvalidResultService, ITransientDependency +{ + private readonly ITransactionInvalidResultManager _transactionInvalidResultManager; + + public TransactionInvalidResultService(ITransactionInvalidResultManager transactionInvalidResultManager) + { + _transactionInvalidResultManager = transactionInvalidResultManager; + } + + public async Task AddFailedTransactionResultsAsync(InvalidTransactionResult transactionResult) + { + await _transactionInvalidResultManager.AddFailedTransactionResultAsync(transactionResult); + } + + public async Task GetFailedTransactionResultAsync(Hash transactionId) + { + return await _transactionInvalidResultManager.GetFailedTransactionResultAsync(transactionId); + } +} \ No newline at end of file diff --git a/src/AElf.Kernel.TransactionPool/Application/TransactionFailedResultService.cs b/src/AElf.Kernel.TransactionPool/Application/TransactionFailedResultService.cs deleted file mode 100644 index c9b6a23e3c..0000000000 --- a/src/AElf.Kernel.TransactionPool/Application/TransactionFailedResultService.cs +++ /dev/null @@ -1,33 +0,0 @@ -using System.Threading.Tasks; -using AElf.Kernel.Blockchain.Domain; -using AElf.Types; -using Volo.Abp.DependencyInjection; - -namespace AElf.Kernel.TransactionPool.Application; - - -public interface ITransactionFailedResultService -{ - Task AddFailedTransactionResultsAsync(TransactionValidationFailure transactionResult); - Task GetFailedTransactionResultAsync(Hash transactionId); -} - -public class TransactionFailedResultService : ITransactionFailedResultService, ITransientDependency -{ - private readonly ITransactionFailedResultManager _transactionFailedResultManager; - - public TransactionFailedResultService(ITransactionFailedResultManager transactionFailedResultManager) - { - _transactionFailedResultManager = transactionFailedResultManager; - } - - public async Task AddFailedTransactionResultsAsync(TransactionValidationFailure transactionResult) - { - await _transactionFailedResultManager.AddFailedTransactionResultAsync(transactionResult); - } - - public async Task GetFailedTransactionResultAsync(Hash transactionId) - { - return await _transactionFailedResultManager.GetFailedTransactionResultAsync(transactionId); - } -} \ No newline at end of file diff --git a/src/AElf.Kernel.TransactionPool/Handler/TransactionValidationStatusFailedEventHandler.cs b/src/AElf.Kernel.TransactionPool/Handler/TransactionValidationStatusFailedEventHandler.cs index ec6b2ff26d..efef84e542 100644 --- a/src/AElf.Kernel.TransactionPool/Handler/TransactionValidationStatusFailedEventHandler.cs +++ b/src/AElf.Kernel.TransactionPool/Handler/TransactionValidationStatusFailedEventHandler.cs @@ -19,25 +19,25 @@ public class TransactionValidationStatusFailedEventHandler : TransactionResultStatus.Failed, TransactionResultStatus.NodeValidationFailed, TransactionResultStatus.Conflict }; - private readonly ITransactionFailedResultService _transactionFailedResultService; + private readonly ITransactionInvalidResultService _transactionInvalidResultService; private readonly TransactionOptions _transactionOptions; public TransactionValidationStatusFailedEventHandler( IOptionsMonitor transactionOptionsMonitor, - ITransactionFailedResultService transactionFailedResultService) + ITransactionInvalidResultService transactionInvalidResultService) { - _transactionFailedResultService = transactionFailedResultService; + _transactionInvalidResultService = transactionInvalidResultService; _transactionOptions = transactionOptionsMonitor.CurrentValue; } public Task HandleEventAsync(TransactionValidationStatusChangedEvent eventData) { if (!FailStatus.Contains(eventData.TransactionResultStatus)) return Task.CompletedTask; - if (!_transactionOptions.SaveInvalidTransactionResult) return Task.CompletedTask; + if (!_transactionOptions.StoreInvalidTransactionResultEnabled) return Task.CompletedTask; // save to storage - _transactionFailedResultService.AddFailedTransactionResultsAsync( - new TransactionValidationFailure + _transactionInvalidResultService.AddFailedTransactionResultsAsync( + new InvalidTransactionResult { TransactionId = eventData.TransactionId, Status = eventData.TransactionResultStatus, diff --git a/src/AElf.Kernel.TransactionPool/TransactionOptions.cs b/src/AElf.Kernel.TransactionPool/TransactionOptions.cs index 4170415511..491ddd51be 100644 --- a/src/AElf.Kernel.TransactionPool/TransactionOptions.cs +++ b/src/AElf.Kernel.TransactionPool/TransactionOptions.cs @@ -22,7 +22,7 @@ public class TransactionOptions /// /// Configuration whether to save failed transaction results /// - public bool SaveInvalidTransactionResult { get; set; } + public bool StoreInvalidTransactionResultEnabled { get; set; } } \ No newline at end of file diff --git a/src/AElf.Launcher/appsettings.json b/src/AElf.Launcher/appsettings.json index a7a524721a..45c0923284 100644 --- a/src/AElf.Launcher/appsettings.json +++ b/src/AElf.Launcher/appsettings.json @@ -31,7 +31,7 @@ "MinerIncreaseInterval": 31536000 }, "Transaction": { - "SaveInvalidTransactionResult" : true, + "StoreInvalidTransactionResultEnabled" : false, "PoolLimit": 10240 }, "BasicAuth": { diff --git a/src/AElf.WebApp.Application.Chain/Services/TransactionResultAppService.cs b/src/AElf.WebApp.Application.Chain/Services/TransactionResultAppService.cs index fc690541ba..5143de62e3 100644 --- a/src/AElf.WebApp.Application.Chain/Services/TransactionResultAppService.cs +++ b/src/AElf.WebApp.Application.Chain/Services/TransactionResultAppService.cs @@ -104,7 +104,7 @@ public async Task GetTransactionResultAsync(string transac } var failedTransactionResult = - await _transactionResultProxyService.TransactionFailedResultService.GetFailedTransactionResultAsync( + await _transactionResultProxyService.TransactionInvalidResultService.GetFailedTransactionResultAsync( transactionIdHash); if (failedTransactionResult != null) { diff --git a/src/AElf.WebApp.Application.Chain/Services/TransactionResultProxyService.cs b/src/AElf.WebApp.Application.Chain/Services/TransactionResultProxyService.cs index cf02dcf037..8602bfa3ed 100644 --- a/src/AElf.WebApp.Application.Chain/Services/TransactionResultProxyService.cs +++ b/src/AElf.WebApp.Application.Chain/Services/TransactionResultProxyService.cs @@ -7,21 +7,21 @@ public interface ITransactionResultProxyService { ITransactionPoolService TransactionPoolService { get; } ITransactionResultQueryService TransactionResultQueryService { get; } - ITransactionFailedResultService TransactionFailedResultService { get; } + ITransactionInvalidResultService TransactionInvalidResultService { get; } } public class TransactionResultProxyService : ITransactionResultProxyService { public TransactionResultProxyService(ITransactionPoolService transactionPoolService, ITransactionResultQueryService transactionResultQueryService, - ITransactionFailedResultService transactionFailedResultService) + ITransactionInvalidResultService transactionInvalidResultService) { TransactionPoolService = transactionPoolService; TransactionResultQueryService = transactionResultQueryService; - TransactionFailedResultService = transactionFailedResultService; + TransactionInvalidResultService = transactionInvalidResultService; } public ITransactionPoolService TransactionPoolService { get; set; } public ITransactionResultQueryService TransactionResultQueryService { get; set; } - public ITransactionFailedResultService TransactionFailedResultService { get; } + public ITransactionInvalidResultService TransactionInvalidResultService { get; } } \ No newline at end of file diff --git a/test/AElf.WebApp.Application.Chain.Tests/BlockChainAppServiceTest.cs b/test/AElf.WebApp.Application.Chain.Tests/BlockChainAppServiceTest.cs index b898977dc3..fadfe062eb 100644 --- a/test/AElf.WebApp.Application.Chain.Tests/BlockChainAppServiceTest.cs +++ b/test/AElf.WebApp.Application.Chain.Tests/BlockChainAppServiceTest.cs @@ -15,6 +15,7 @@ using AElf.Kernel.SmartContract.Application; using AElf.Kernel.SmartContract.Domain; using AElf.Kernel.Token; +using AElf.Kernel.TransactionPool.Handler; using AElf.Kernel.TransactionPool.Infrastructure; using AElf.OS; using AElf.Runtime.CSharp; @@ -44,6 +45,8 @@ public sealed class BlockChainAppServiceTest : WebAppTestBase private readonly ISmartContractAddressService _smartContractAddressService; private readonly ITransactionResultStatusCacheProvider _transactionResultStatusCacheProvider; private readonly TransactionValidationStatusChangedEventHandler _transactionValidationStatusChangedEventHandler; + private readonly TransactionValidationStatusFailedEventHandler _transactionExecutionValidationFailedEventHandler; + private readonly ITransactionResultProxyService _transactionResultProxyService; private readonly ITxHub _txHub; private IReadOnlyDictionary _codes; @@ -58,6 +61,9 @@ public BlockChainAppServiceTest(ITestOutputHelper outputHelper) : base(outputHel _osTestHelper = GetRequiredService(); _accountService = GetRequiredService(); _blockStateSetManger = GetRequiredService(); + _transactionResultProxyService = GetRequiredService(); + _transactionExecutionValidationFailedEventHandler = + GetRequiredService(); _transactionValidationStatusChangedEventHandler = GetRequiredService(); } @@ -1822,4 +1828,31 @@ await PostResponseAsObjectAsync("/api/blockChain/sendTran response.Transaction.Params.ShouldNotBe( AddOptionInput.Parser.ParseFrom(transaction.Params).ToString()); } + + [Fact] + public async Task TransactionInvalidResultTest() + { + + var txId = HashHelper.ComputeFrom("TransactionInvalidResultTest"); + await _transactionExecutionValidationFailedEventHandler.HandleEventAsync(new TransactionValidationStatusChangedEvent + { + TransactionId = txId, + TransactionResultStatus = TransactionResultStatus.NodeValidationFailed, + Error = "tx error" + }); + + var invalidResult = await _transactionResultProxyService.TransactionInvalidResultService + .GetFailedTransactionResultAsync(txId); + invalidResult.ShouldNotBeNull(); + invalidResult.Status.ShouldBe(TransactionResultStatus.NodeValidationFailed); + invalidResult.Error.ShouldBe("tx error"); + + var response = await GetResponseAsObjectAsync( + $"/api/blockChain/transactionResult?transactionId={txId.ToHex()}"); + response.ShouldNotBeNull(); + response.Status.ShouldBe(TransactionResultStatus.NodeValidationFailed.ToString().ToUpper()); + response.Error.ShouldBe("tx error"); + + } + } \ No newline at end of file diff --git a/test/AElf.WebApp.Application.TestBase/WebAppTestAElfModule.cs b/test/AElf.WebApp.Application.TestBase/WebAppTestAElfModule.cs index c2d6783b8f..c9dcf809ec 100644 --- a/test/AElf.WebApp.Application.TestBase/WebAppTestAElfModule.cs +++ b/test/AElf.WebApp.Application.TestBase/WebAppTestAElfModule.cs @@ -6,6 +6,7 @@ using AElf.Kernel.FeeCalculation; using AElf.Kernel.SmartContract.Application; using AElf.Kernel.SmartContract.ExecutionPluginForMethodFee; +using AElf.Kernel.TransactionPool; using AElf.Modularity; using AElf.OS; using AElf.OS.Network.Application; @@ -88,5 +89,9 @@ public override void ConfigureServices(ServiceConfigurationContext context) options.UserName = BasicAuth.DefaultUserName; options.Password = BasicAuth.DefaultPassword; }); + Configure(o => { + o.PoolLimit = 20; + o.StoreInvalidTransactionResultEnabled = true; + }); } } \ No newline at end of file From 84f76db373279310e7ab2ae3f3a26eb314f74158 Mon Sep 17 00:00:00 2001 From: "zecong.nian" Date: Thu, 14 Dec 2023 10:35:21 +0800 Subject: [PATCH 5/7] feat: rename and clear --- .../Domain/ITransactionResultManager.cs | 29 +------------------ .../ITransactionInvalidResultService.cs | 4 +-- .../ChainApplicationWebAppAElfModule.cs | 1 - .../Services/TransactionResultAppService.cs | 2 +- .../BlockChainAppServiceTest.cs | 2 +- 5 files changed, 5 insertions(+), 33 deletions(-) diff --git a/src/AElf.Kernel.Core/Blockchain/Domain/ITransactionResultManager.cs b/src/AElf.Kernel.Core/Blockchain/Domain/ITransactionResultManager.cs index 964841541c..e2f2176886 100644 --- a/src/AElf.Kernel.Core/Blockchain/Domain/ITransactionResultManager.cs +++ b/src/AElf.Kernel.Core/Blockchain/Domain/ITransactionResultManager.cs @@ -7,19 +7,15 @@ namespace AElf.Kernel.Blockchain.Domain; public interface ITransactionResultManager { Task AddTransactionResultAsync(TransactionResult transactionResult, Hash disambiguationHash); + Task AddTransactionResultsAsync(IList transactionResults, Hash disambiguationHash); Task GetTransactionResultAsync(Hash txId, Hash disambiguationHash); Task> GetTransactionResultsAsync(IList txIds, Hash disambiguationHash); Task HasTransactionResultAsync(Hash transactionId, Hash disambiguationHash); - Task AddFailedTransactionResultAsync(TransactionResult transactionResult); - Task AddFailedTransactionResultsAsync(IList transactionResults); - Task GetFailedTransactionResultAsync(Hash transactionId); - Task> GetFailedTransactionResultsAsync(IList txIds); } public class TransactionResultManager : ITransactionResultManager { - private const string FailStorageKeyPrefix = "FAIL:"; private readonly IBlockchainStore _transactionResultStore; public TransactionResultManager(IBlockchainStore transactionResultStore) @@ -27,29 +23,6 @@ public TransactionResultManager(IBlockchainStore transactionR _transactionResultStore = transactionResultStore; } - public async Task AddFailedTransactionResultAsync(TransactionResult transactionResult) - { - await _transactionResultStore.SetAsync(FailStorageKeyPrefix + transactionResult.TransactionId.ToStorageKey(), transactionResult); - } - - public async Task AddFailedTransactionResultsAsync(IList transactionResults) - { - await _transactionResultStore.SetAllAsync( - transactionResults.ToDictionary(tx => FailStorageKeyPrefix + tx.TransactionId.ToStorageKey(), t => t) - ); - } - - public async Task GetFailedTransactionResultAsync(Hash transactionId) - { - return await _transactionResultStore.GetAsync(FailStorageKeyPrefix + transactionId.ToStorageKey()); - } - - public async Task> GetFailedTransactionResultsAsync(IList txIds) - { - return await _transactionResultStore.GetAllAsync(txIds.Select(id => FailStorageKeyPrefix + id.ToStorageKey()).ToList()); - } - - public async Task AddTransactionResultAsync(TransactionResult transactionResult, Hash disambiguationHash) { await _transactionResultStore.SetAsync( diff --git a/src/AElf.Kernel.TransactionPool/Application/ITransactionInvalidResultService.cs b/src/AElf.Kernel.TransactionPool/Application/ITransactionInvalidResultService.cs index 2490e8fad4..fa3f912dc4 100644 --- a/src/AElf.Kernel.TransactionPool/Application/ITransactionInvalidResultService.cs +++ b/src/AElf.Kernel.TransactionPool/Application/ITransactionInvalidResultService.cs @@ -9,7 +9,7 @@ namespace AElf.Kernel.TransactionPool.Application; public interface ITransactionInvalidResultService { Task AddFailedTransactionResultsAsync(InvalidTransactionResult transactionResult); - Task GetFailedTransactionResultAsync(Hash transactionId); + Task GetTransactionInvalidResultAsync(Hash transactionId); } public class TransactionInvalidResultService : ITransactionInvalidResultService, ITransientDependency @@ -26,7 +26,7 @@ public async Task AddFailedTransactionResultsAsync(InvalidTransactionResult tran await _transactionInvalidResultManager.AddFailedTransactionResultAsync(transactionResult); } - public async Task GetFailedTransactionResultAsync(Hash transactionId) + public async Task GetTransactionInvalidResultAsync(Hash transactionId) { return await _transactionInvalidResultManager.GetFailedTransactionResultAsync(transactionId); } diff --git a/src/AElf.WebApp.Application.Chain/ChainApplicationWebAppAElfModule.cs b/src/AElf.WebApp.Application.Chain/ChainApplicationWebAppAElfModule.cs index d6e09b36f8..d5820e1828 100644 --- a/src/AElf.WebApp.Application.Chain/ChainApplicationWebAppAElfModule.cs +++ b/src/AElf.WebApp.Application.Chain/ChainApplicationWebAppAElfModule.cs @@ -22,6 +22,5 @@ public override void ConfigureServices(ServiceConfigurationContext context) context.Services .AddSingleton(); - } } \ No newline at end of file diff --git a/src/AElf.WebApp.Application.Chain/Services/TransactionResultAppService.cs b/src/AElf.WebApp.Application.Chain/Services/TransactionResultAppService.cs index 5143de62e3..e187c47a22 100644 --- a/src/AElf.WebApp.Application.Chain/Services/TransactionResultAppService.cs +++ b/src/AElf.WebApp.Application.Chain/Services/TransactionResultAppService.cs @@ -104,7 +104,7 @@ public async Task GetTransactionResultAsync(string transac } var failedTransactionResult = - await _transactionResultProxyService.TransactionInvalidResultService.GetFailedTransactionResultAsync( + await _transactionResultProxyService.TransactionInvalidResultService.GetTransactionInvalidResultAsync( transactionIdHash); if (failedTransactionResult != null) { diff --git a/test/AElf.WebApp.Application.Chain.Tests/BlockChainAppServiceTest.cs b/test/AElf.WebApp.Application.Chain.Tests/BlockChainAppServiceTest.cs index fadfe062eb..ce435f91ab 100644 --- a/test/AElf.WebApp.Application.Chain.Tests/BlockChainAppServiceTest.cs +++ b/test/AElf.WebApp.Application.Chain.Tests/BlockChainAppServiceTest.cs @@ -1842,7 +1842,7 @@ await _transactionExecutionValidationFailedEventHandler.HandleEventAsync(new Tra }); var invalidResult = await _transactionResultProxyService.TransactionInvalidResultService - .GetFailedTransactionResultAsync(txId); + .GetTransactionInvalidResultAsync(txId); invalidResult.ShouldNotBeNull(); invalidResult.Status.ShouldBe(TransactionResultStatus.NodeValidationFailed); invalidResult.Error.ShouldBe("tx error"); From dddae354668ca9ee24883ad2e7739e019b3ad38e Mon Sep 17 00:00:00 2001 From: "zecong.nian" Date: Thu, 14 Dec 2023 10:59:52 +0800 Subject: [PATCH 6/7] feat: rename --- .../Domain/ITransactionInvalidResultManager.cs | 18 +++++++++--------- .../ITransactionInvalidResultService.cs | 4 ++-- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/AElf.Kernel.Core/Blockchain/Domain/ITransactionInvalidResultManager.cs b/src/AElf.Kernel.Core/Blockchain/Domain/ITransactionInvalidResultManager.cs index bb98dbda40..f06198bd6e 100644 --- a/src/AElf.Kernel.Core/Blockchain/Domain/ITransactionInvalidResultManager.cs +++ b/src/AElf.Kernel.Core/Blockchain/Domain/ITransactionInvalidResultManager.cs @@ -6,28 +6,28 @@ namespace AElf.Kernel.Blockchain.Domain; public interface ITransactionInvalidResultManager { - Task AddFailedTransactionResultAsync(InvalidTransactionResult transactionResult); - Task GetFailedTransactionResultAsync(Hash transactionId); + Task AddTransactionInvalidResultAsync(InvalidTransactionResult transactionResult); + Task GetTransactionInvalidResultAsync(Hash transactionId); } public class TransactionInvalidResultManager : ITransactionInvalidResultManager { - private readonly IBlockchainStore _transactionFailedResultStore; + private readonly IBlockchainStore _transactionInvalidResultStore; - public TransactionInvalidResultManager(IBlockchainStore transactionFailedResultStore) + public TransactionInvalidResultManager(IBlockchainStore transactionInvalidResultStore) { - _transactionFailedResultStore = transactionFailedResultStore; + _transactionInvalidResultStore = transactionInvalidResultStore; } - public async Task AddFailedTransactionResultAsync(InvalidTransactionResult transactionResult) + public async Task AddTransactionInvalidResultAsync(InvalidTransactionResult transactionResult) { - await _transactionFailedResultStore.SetAsync(transactionResult.TransactionId.ToStorageKey(), transactionResult); + await _transactionInvalidResultStore.SetAsync(transactionResult.TransactionId.ToStorageKey(), transactionResult); } - public async Task GetFailedTransactionResultAsync(Hash transactionId) + public async Task GetTransactionInvalidResultAsync(Hash transactionId) { - return await _transactionFailedResultStore.GetAsync(transactionId.ToStorageKey()); + return await _transactionInvalidResultStore.GetAsync(transactionId.ToStorageKey()); } } \ No newline at end of file diff --git a/src/AElf.Kernel.TransactionPool/Application/ITransactionInvalidResultService.cs b/src/AElf.Kernel.TransactionPool/Application/ITransactionInvalidResultService.cs index fa3f912dc4..63b51561af 100644 --- a/src/AElf.Kernel.TransactionPool/Application/ITransactionInvalidResultService.cs +++ b/src/AElf.Kernel.TransactionPool/Application/ITransactionInvalidResultService.cs @@ -23,11 +23,11 @@ public TransactionInvalidResultService(ITransactionInvalidResultManager transact public async Task AddFailedTransactionResultsAsync(InvalidTransactionResult transactionResult) { - await _transactionInvalidResultManager.AddFailedTransactionResultAsync(transactionResult); + await _transactionInvalidResultManager.AddTransactionInvalidResultAsync(transactionResult); } public async Task GetTransactionInvalidResultAsync(Hash transactionId) { - return await _transactionInvalidResultManager.GetFailedTransactionResultAsync(transactionId); + return await _transactionInvalidResultManager.GetTransactionInvalidResultAsync(transactionId); } } \ No newline at end of file From 4f6f2a55bf033007acf99b3074bf1d77b09aa837 Mon Sep 17 00:00:00 2001 From: "zecong.nian" Date: Thu, 14 Dec 2023 23:31:50 +0800 Subject: [PATCH 7/7] fix:cr issue --- .../IInvalidTransactionResultManager.cs | 33 +++++++++++++++++++ .../ITransactionInvalidResultManager.cs | 33 ------------------- .../IInvalidTransactionResultService.cs | 33 +++++++++++++++++++ .../ITransactionInvalidResultService.cs | 33 ------------------- ...ctionValidationStatusFailedEventHandler.cs | 20 +++++------ .../Services/TransactionResultAppService.cs | 25 ++++++++------ .../Services/TransactionResultProxyService.cs | 8 ++--- .../BlockChainAppServiceTest.cs | 8 ++--- 8 files changed, 98 insertions(+), 95 deletions(-) create mode 100644 src/AElf.Kernel.Core/Blockchain/Domain/IInvalidTransactionResultManager.cs delete mode 100644 src/AElf.Kernel.Core/Blockchain/Domain/ITransactionInvalidResultManager.cs create mode 100644 src/AElf.Kernel.TransactionPool/Application/IInvalidTransactionResultService.cs delete mode 100644 src/AElf.Kernel.TransactionPool/Application/ITransactionInvalidResultService.cs diff --git a/src/AElf.Kernel.Core/Blockchain/Domain/IInvalidTransactionResultManager.cs b/src/AElf.Kernel.Core/Blockchain/Domain/IInvalidTransactionResultManager.cs new file mode 100644 index 0000000000..9c29556ad1 --- /dev/null +++ b/src/AElf.Kernel.Core/Blockchain/Domain/IInvalidTransactionResultManager.cs @@ -0,0 +1,33 @@ +using System.Linq; +using AElf.Kernel.Blockchain.Infrastructure; +using AElf.Kernel.Infrastructure; + +namespace AElf.Kernel.Blockchain.Domain; + +public interface IInvalidTransactionResultManager +{ + Task AddInvalidTransactionResultAsync(InvalidTransactionResult transactionResult); + Task GetInvalidTransactionResultAsync(Hash transactionId); +} + +public class InvalidTransactionResultManager : IInvalidTransactionResultManager +{ + private readonly IBlockchainStore _invalidTransactionResultStore; + + public InvalidTransactionResultManager(IBlockchainStore invalidTransactionResultStore) + { + _invalidTransactionResultStore = invalidTransactionResultStore; + } + + public async Task AddInvalidTransactionResultAsync(InvalidTransactionResult transactionResult) + { + await _invalidTransactionResultStore.SetAsync(transactionResult.TransactionId.ToStorageKey(), transactionResult); + } + + + public async Task GetInvalidTransactionResultAsync(Hash transactionId) + { + return await _invalidTransactionResultStore.GetAsync(transactionId.ToStorageKey()); + } + +} \ No newline at end of file diff --git a/src/AElf.Kernel.Core/Blockchain/Domain/ITransactionInvalidResultManager.cs b/src/AElf.Kernel.Core/Blockchain/Domain/ITransactionInvalidResultManager.cs deleted file mode 100644 index f06198bd6e..0000000000 --- a/src/AElf.Kernel.Core/Blockchain/Domain/ITransactionInvalidResultManager.cs +++ /dev/null @@ -1,33 +0,0 @@ -using System.Linq; -using AElf.Kernel.Blockchain.Infrastructure; -using AElf.Kernel.Infrastructure; - -namespace AElf.Kernel.Blockchain.Domain; - -public interface ITransactionInvalidResultManager -{ - Task AddTransactionInvalidResultAsync(InvalidTransactionResult transactionResult); - Task GetTransactionInvalidResultAsync(Hash transactionId); -} - -public class TransactionInvalidResultManager : ITransactionInvalidResultManager -{ - private readonly IBlockchainStore _transactionInvalidResultStore; - - public TransactionInvalidResultManager(IBlockchainStore transactionInvalidResultStore) - { - _transactionInvalidResultStore = transactionInvalidResultStore; - } - - public async Task AddTransactionInvalidResultAsync(InvalidTransactionResult transactionResult) - { - await _transactionInvalidResultStore.SetAsync(transactionResult.TransactionId.ToStorageKey(), transactionResult); - } - - - public async Task GetTransactionInvalidResultAsync(Hash transactionId) - { - return await _transactionInvalidResultStore.GetAsync(transactionId.ToStorageKey()); - } - -} \ No newline at end of file diff --git a/src/AElf.Kernel.TransactionPool/Application/IInvalidTransactionResultService.cs b/src/AElf.Kernel.TransactionPool/Application/IInvalidTransactionResultService.cs new file mode 100644 index 0000000000..0a1d13c0bf --- /dev/null +++ b/src/AElf.Kernel.TransactionPool/Application/IInvalidTransactionResultService.cs @@ -0,0 +1,33 @@ +using System.Threading.Tasks; +using AElf.Kernel.Blockchain.Domain; +using AElf.Types; +using Volo.Abp.DependencyInjection; + +namespace AElf.Kernel.TransactionPool.Application; + + +public interface IInvalidTransactionResultService +{ + Task AddInvalidTransactionResultsAsync(InvalidTransactionResult transactionResult); + Task GetInvalidTransactionResultAsync(Hash transactionId); +} + +public class InvalidTransactionResultService : IInvalidTransactionResultService, ITransientDependency +{ + private readonly IInvalidTransactionResultManager _invalidTransactionResultManager; + + public InvalidTransactionResultService(IInvalidTransactionResultManager invalidTransactionResultManager) + { + _invalidTransactionResultManager = invalidTransactionResultManager; + } + + public async Task AddInvalidTransactionResultsAsync(InvalidTransactionResult transactionResult) + { + await _invalidTransactionResultManager.AddInvalidTransactionResultAsync(transactionResult); + } + + public async Task GetInvalidTransactionResultAsync(Hash transactionId) + { + return await _invalidTransactionResultManager.GetInvalidTransactionResultAsync(transactionId); + } +} \ No newline at end of file diff --git a/src/AElf.Kernel.TransactionPool/Application/ITransactionInvalidResultService.cs b/src/AElf.Kernel.TransactionPool/Application/ITransactionInvalidResultService.cs deleted file mode 100644 index 63b51561af..0000000000 --- a/src/AElf.Kernel.TransactionPool/Application/ITransactionInvalidResultService.cs +++ /dev/null @@ -1,33 +0,0 @@ -using System.Threading.Tasks; -using AElf.Kernel.Blockchain.Domain; -using AElf.Types; -using Volo.Abp.DependencyInjection; - -namespace AElf.Kernel.TransactionPool.Application; - - -public interface ITransactionInvalidResultService -{ - Task AddFailedTransactionResultsAsync(InvalidTransactionResult transactionResult); - Task GetTransactionInvalidResultAsync(Hash transactionId); -} - -public class TransactionInvalidResultService : ITransactionInvalidResultService, ITransientDependency -{ - private readonly ITransactionInvalidResultManager _transactionInvalidResultManager; - - public TransactionInvalidResultService(ITransactionInvalidResultManager transactionInvalidResultManager) - { - _transactionInvalidResultManager = transactionInvalidResultManager; - } - - public async Task AddFailedTransactionResultsAsync(InvalidTransactionResult transactionResult) - { - await _transactionInvalidResultManager.AddTransactionInvalidResultAsync(transactionResult); - } - - public async Task GetTransactionInvalidResultAsync(Hash transactionId) - { - return await _transactionInvalidResultManager.GetTransactionInvalidResultAsync(transactionId); - } -} \ No newline at end of file diff --git a/src/AElf.Kernel.TransactionPool/Handler/TransactionValidationStatusFailedEventHandler.cs b/src/AElf.Kernel.TransactionPool/Handler/TransactionValidationStatusFailedEventHandler.cs index efef84e542..414cc76036 100644 --- a/src/AElf.Kernel.TransactionPool/Handler/TransactionValidationStatusFailedEventHandler.cs +++ b/src/AElf.Kernel.TransactionPool/Handler/TransactionValidationStatusFailedEventHandler.cs @@ -14,40 +14,38 @@ public class TransactionValidationStatusFailedEventHandler : ILocalEventHandler, ITransientDependency { - private static readonly IEnumerable FailStatus = new List + private readonly IEnumerable _failStatus = new List { TransactionResultStatus.Failed, TransactionResultStatus.NodeValidationFailed, TransactionResultStatus.Conflict }; - private readonly ITransactionInvalidResultService _transactionInvalidResultService; + private readonly IInvalidTransactionResultService _invalidTransactionResultService; private readonly TransactionOptions _transactionOptions; public TransactionValidationStatusFailedEventHandler( IOptionsMonitor transactionOptionsMonitor, - ITransactionInvalidResultService transactionInvalidResultService) + IInvalidTransactionResultService invalidTransactionResultService) { - _transactionInvalidResultService = transactionInvalidResultService; + _invalidTransactionResultService = invalidTransactionResultService; _transactionOptions = transactionOptionsMonitor.CurrentValue; } - public Task HandleEventAsync(TransactionValidationStatusChangedEvent eventData) + public async Task HandleEventAsync(TransactionValidationStatusChangedEvent eventData) { - if (!FailStatus.Contains(eventData.TransactionResultStatus)) return Task.CompletedTask; - if (!_transactionOptions.StoreInvalidTransactionResultEnabled) return Task.CompletedTask; + if (!_failStatus.Contains(eventData.TransactionResultStatus)) return; + if (!_transactionOptions.StoreInvalidTransactionResultEnabled) return; // save to storage - _transactionInvalidResultService.AddFailedTransactionResultsAsync( + await _invalidTransactionResultService.AddInvalidTransactionResultsAsync( new InvalidTransactionResult { TransactionId = eventData.TransactionId, Status = eventData.TransactionResultStatus, Error = TakeErrorMessage(eventData.Error) }); - - return Task.CompletedTask; } - public static string TakeErrorMessage(string transactionResultError) + private string TakeErrorMessage(string transactionResultError) { if (string.IsNullOrWhiteSpace(transactionResultError)) return null; diff --git a/src/AElf.WebApp.Application.Chain/Services/TransactionResultAppService.cs b/src/AElf.WebApp.Application.Chain/Services/TransactionResultAppService.cs index e187c47a22..483dbb4366 100644 --- a/src/AElf.WebApp.Application.Chain/Services/TransactionResultAppService.cs +++ b/src/AElf.WebApp.Application.Chain/Services/TransactionResultAppService.cs @@ -7,6 +7,7 @@ using AElf.Kernel.Blockchain.Application; using AElf.Kernel.Blockchain.Domain; using AElf.Kernel.SmartContract.Application; +using AElf.Kernel.TransactionPool; using AElf.Types; using AElf.WebApp.Application.Chain.Dto; using AElf.WebApp.Application.Chain.Infrastructure; @@ -39,6 +40,7 @@ public class TransactionResultAppService : AElfAppService, ITransactionResultApp private readonly ITransactionResultProxyService _transactionResultProxyService; private readonly ITransactionResultStatusCacheProvider _transactionResultStatusCacheProvider; private readonly WebAppOptions _webAppOptions; + private readonly TransactionOptions _transactionOptions; public TransactionResultAppService(ITransactionResultProxyService transactionResultProxyService, ITransactionManager transactionManager, @@ -46,7 +48,7 @@ public TransactionResultAppService(ITransactionResultProxyService transactionRes ITransactionReadOnlyExecutionService transactionReadOnlyExecutionService, IObjectMapper objectMapper, ITransactionResultStatusCacheProvider transactionResultStatusCacheProvider, - IOptionsMonitor optionsSnapshot) + IOptionsMonitor optionsSnapshot, IOptionsMonitor transactionOptions) { _transactionResultProxyService = transactionResultProxyService; _transactionManager = transactionManager; @@ -54,6 +56,7 @@ public TransactionResultAppService(ITransactionResultProxyService transactionRes _transactionReadOnlyExecutionService = transactionReadOnlyExecutionService; _objectMapper = objectMapper; _transactionResultStatusCacheProvider = transactionResultStatusCacheProvider; + _transactionOptions = transactionOptions.CurrentValue; _webAppOptions = optionsSnapshot.CurrentValue; Logger = NullLogger.Instance; @@ -102,16 +105,18 @@ public async Task GetTransactionResultAsync(string transac TransactionErrorResolver.TakeErrorMessage(validationStatus.Error, _webAppOptions.IsDebugMode); return output; } - - var failedTransactionResult = - await _transactionResultProxyService.TransactionInvalidResultService.GetTransactionInvalidResultAsync( - transactionIdHash); - if (failedTransactionResult != null) + + if (_transactionOptions.StoreInvalidTransactionResultEnabled) { - output.Status = failedTransactionResult.Status.ToString().ToUpper(); - output.Error = - TransactionErrorResolver.TakeErrorMessage(failedTransactionResult.Error, _webAppOptions.IsDebugMode); - return output; + var failedTransactionResult = + await _transactionResultProxyService.InvalidTransactionResultService.GetInvalidTransactionResultAsync( + transactionIdHash); + if (failedTransactionResult != null) + { + output.Status = failedTransactionResult.Status.ToString().ToUpper(); + output.Error = failedTransactionResult.Error; + return output; + } } return output; diff --git a/src/AElf.WebApp.Application.Chain/Services/TransactionResultProxyService.cs b/src/AElf.WebApp.Application.Chain/Services/TransactionResultProxyService.cs index 8602bfa3ed..9d13296930 100644 --- a/src/AElf.WebApp.Application.Chain/Services/TransactionResultProxyService.cs +++ b/src/AElf.WebApp.Application.Chain/Services/TransactionResultProxyService.cs @@ -7,21 +7,21 @@ public interface ITransactionResultProxyService { ITransactionPoolService TransactionPoolService { get; } ITransactionResultQueryService TransactionResultQueryService { get; } - ITransactionInvalidResultService TransactionInvalidResultService { get; } + IInvalidTransactionResultService InvalidTransactionResultService { get; } } public class TransactionResultProxyService : ITransactionResultProxyService { public TransactionResultProxyService(ITransactionPoolService transactionPoolService, ITransactionResultQueryService transactionResultQueryService, - ITransactionInvalidResultService transactionInvalidResultService) + IInvalidTransactionResultService invalidTransactionResultService) { TransactionPoolService = transactionPoolService; TransactionResultQueryService = transactionResultQueryService; - TransactionInvalidResultService = transactionInvalidResultService; + InvalidTransactionResultService = invalidTransactionResultService; } public ITransactionPoolService TransactionPoolService { get; set; } public ITransactionResultQueryService TransactionResultQueryService { get; set; } - public ITransactionInvalidResultService TransactionInvalidResultService { get; } + public IInvalidTransactionResultService InvalidTransactionResultService { get; } } \ No newline at end of file diff --git a/test/AElf.WebApp.Application.Chain.Tests/BlockChainAppServiceTest.cs b/test/AElf.WebApp.Application.Chain.Tests/BlockChainAppServiceTest.cs index ce435f91ab..7aec5b5cfd 100644 --- a/test/AElf.WebApp.Application.Chain.Tests/BlockChainAppServiceTest.cs +++ b/test/AElf.WebApp.Application.Chain.Tests/BlockChainAppServiceTest.cs @@ -1830,10 +1830,10 @@ await PostResponseAsObjectAsync("/api/blockChain/sendTran } [Fact] - public async Task TransactionInvalidResultTest() + public async Task InvalidTransactionResultTest() { - var txId = HashHelper.ComputeFrom("TransactionInvalidResultTest"); + var txId = HashHelper.ComputeFrom("InvalidTransactionResultTest"); await _transactionExecutionValidationFailedEventHandler.HandleEventAsync(new TransactionValidationStatusChangedEvent { TransactionId = txId, @@ -1841,8 +1841,8 @@ await _transactionExecutionValidationFailedEventHandler.HandleEventAsync(new Tra Error = "tx error" }); - var invalidResult = await _transactionResultProxyService.TransactionInvalidResultService - .GetTransactionInvalidResultAsync(txId); + var invalidResult = await _transactionResultProxyService.InvalidTransactionResultService + .GetInvalidTransactionResultAsync(txId); invalidResult.ShouldNotBeNull(); invalidResult.Status.ShouldBe(TransactionResultStatus.NodeValidationFailed); invalidResult.Error.ShouldBe("tx error");