From 6a1dd91f0a8d00ee6512e954a0d474a68249461a Mon Sep 17 00:00:00 2001 From: area363 Date: Thu, 23 Nov 2023 15:07:04 +0900 Subject: [PATCH] minimize overuse of acs --- Lib9c.Policy/NCStagePolicy.cs | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/Lib9c.Policy/NCStagePolicy.cs b/Lib9c.Policy/NCStagePolicy.cs index 0dbd00c1f5..38610c303a 100644 --- a/Lib9c.Policy/NCStagePolicy.cs +++ b/Lib9c.Policy/NCStagePolicy.cs @@ -15,6 +15,7 @@ public class NCStagePolicy : IStagePolicy private readonly VolatileStagePolicy _impl; private readonly ConcurrentDictionary> _txs; private readonly int _quotaPerSigner; + private readonly Dictionary _quotaPerSignerList; private IAccessControlService? _accessControlService; public NCStagePolicy(TimeSpan txLifeTime, int quotaPerSigner, IAccessControlService? accessControlService = null) @@ -31,6 +32,7 @@ public NCStagePolicy(TimeSpan txLifeTime, int quotaPerSigner, IAccessControlServ ? new VolatileStagePolicy() : new VolatileStagePolicy(txLifeTime); + _quotaPerSignerList = new Dictionary(); _accessControlService = accessControlService; } @@ -61,10 +63,10 @@ public IEnumerable Iterate(BlockChain blockChain, bool filtered = t s.Add(tx); int txQuotaPerSigner = _quotaPerSigner; - // update txQuotaPerSigner if ACS returns a value for the signer. - if (_accessControlService?.GetTxQuota(tx.Signer) is { } acsTxQuota) + // update txQuotaPerSigner if signer is in the list + if (_quotaPerSignerList.TryGetValue(tx.Signer, out int signerQuota)) { - txQuotaPerSigner = acsTxQuota; + txQuotaPerSigner = signerQuota; } @@ -86,10 +88,13 @@ public IEnumerable Iterate(BlockChain blockChain, bool filtered = t public bool Stage(BlockChain blockChain, Transaction transaction) { - if (_accessControlService?.GetTxQuota(transaction.Signer) is { } acsTxQuota - && acsTxQuota == 0) + if (_accessControlService?.GetTxQuota(transaction.Signer) is { } acsTxQuota) { - return false; + _quotaPerSignerList.Add(transaction.Signer, acsTxQuota); + if (acsTxQuota == 0) + { + return false; + } } var deniedTxs = new[]