-
Notifications
You must be signed in to change notification settings - Fork 261
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3557 from AElfProject/feature/invalid-tx-result
Store failed transaction result to database
- Loading branch information
Showing
11 changed files
with
213 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/AElf.Kernel.Core/Blockchain/Domain/IInvalidTransactionResultManager.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<InvalidTransactionResult> GetInvalidTransactionResultAsync(Hash transactionId); | ||
} | ||
|
||
public class InvalidTransactionResultManager : IInvalidTransactionResultManager | ||
{ | ||
private readonly IBlockchainStore<InvalidTransactionResult> _invalidTransactionResultStore; | ||
|
||
public InvalidTransactionResultManager(IBlockchainStore<InvalidTransactionResult> invalidTransactionResultStore) | ||
{ | ||
_invalidTransactionResultStore = invalidTransactionResultStore; | ||
} | ||
|
||
public async Task AddInvalidTransactionResultAsync(InvalidTransactionResult transactionResult) | ||
{ | ||
await _invalidTransactionResultStore.SetAsync(transactionResult.TransactionId.ToStorageKey(), transactionResult); | ||
} | ||
|
||
|
||
public async Task<InvalidTransactionResult> GetInvalidTransactionResultAsync(Hash transactionId) | ||
{ | ||
return await _invalidTransactionResultStore.GetAsync(transactionId.ToStorageKey()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/AElf.Kernel.TransactionPool/Application/IInvalidTransactionResultService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<InvalidTransactionResult> 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<InvalidTransactionResult> GetInvalidTransactionResultAsync(Hash transactionId) | ||
{ | ||
return await _invalidTransactionResultManager.GetInvalidTransactionResultAsync(transactionId); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
src/AElf.Kernel.TransactionPool/Handler/TransactionValidationStatusFailedEventHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
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<TransactionValidationStatusChangedEvent>, | ||
ITransientDependency | ||
{ | ||
private readonly IEnumerable<TransactionResultStatus> _failStatus = new List<TransactionResultStatus> | ||
{ | ||
TransactionResultStatus.Failed, TransactionResultStatus.NodeValidationFailed, TransactionResultStatus.Conflict | ||
}; | ||
|
||
private readonly IInvalidTransactionResultService _invalidTransactionResultService; | ||
private readonly TransactionOptions _transactionOptions; | ||
|
||
public TransactionValidationStatusFailedEventHandler( | ||
IOptionsMonitor<TransactionOptions> transactionOptionsMonitor, | ||
IInvalidTransactionResultService invalidTransactionResultService) | ||
{ | ||
_invalidTransactionResultService = invalidTransactionResultService; | ||
_transactionOptions = transactionOptionsMonitor.CurrentValue; | ||
} | ||
|
||
public async Task HandleEventAsync(TransactionValidationStatusChangedEvent eventData) | ||
{ | ||
if (!_failStatus.Contains(eventData.TransactionResultStatus)) return; | ||
if (!_transactionOptions.StoreInvalidTransactionResultEnabled) return; | ||
|
||
// save to storage | ||
await _invalidTransactionResultService.AddInvalidTransactionResultsAsync( | ||
new InvalidTransactionResult | ||
{ | ||
TransactionId = eventData.TransactionId, | ||
Status = eventData.TransactionResultStatus, | ||
Error = TakeErrorMessage(eventData.Error) | ||
}); | ||
} | ||
|
||
private string TakeErrorMessage(string transactionResultError) | ||
{ | ||
if (string.IsNullOrWhiteSpace(transactionResultError)) | ||
return null; | ||
using var stringReader = new StringReader(transactionResultError); | ||
return stringReader.ReadLine(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters