From 601705b36412910fad36dc6348e18e16d4d2c94e Mon Sep 17 00:00:00 2001 From: area363 Date: Thu, 23 Nov 2023 20:34:14 +0900 Subject: [PATCH 1/3] do not call acs when iterating chain --- 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..c7d4a21790 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.TryAdd(transaction.Signer, acsTxQuota); + if (acsTxQuota == 0) + { + return false; + } } var deniedTxs = new[] From e41494cdffaa9958ad3b3a07881fb71f2bb2c1c0 Mon Sep 17 00:00:00 2001 From: area363 Date: Thu, 23 Nov 2023 22:18:23 +0900 Subject: [PATCH 2/3] dynamically update quota --- Lib9c.Policy/NCStagePolicy.cs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/Lib9c.Policy/NCStagePolicy.cs b/Lib9c.Policy/NCStagePolicy.cs index c7d4a21790..d14b9eefee 100644 --- a/Lib9c.Policy/NCStagePolicy.cs +++ b/Lib9c.Policy/NCStagePolicy.cs @@ -90,7 +90,15 @@ public bool Stage(BlockChain blockChain, Transaction transaction) { if (_accessControlService?.GetTxQuota(transaction.Signer) is { } acsTxQuota) { - _quotaPerSignerList.TryAdd(transaction.Signer, acsTxQuota); + if (_quotaPerSignerList.ContainsKey(transaction.Signer)) + { + _quotaPerSignerList[transaction.Signer] = acsTxQuota; + } + else + { + _quotaPerSignerList.TryAdd(transaction.Signer, acsTxQuota); + } + if (acsTxQuota == 0) { return false; From 96c2baddaa19bd1467cd5006eccf9280710c9527 Mon Sep 17 00:00:00 2001 From: area363 Date: Thu, 23 Nov 2023 23:32:12 +0900 Subject: [PATCH 3/3] add suggested change --- Lib9c.Policy/NCStagePolicy.cs | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/Lib9c.Policy/NCStagePolicy.cs b/Lib9c.Policy/NCStagePolicy.cs index d14b9eefee..13b77bdf60 100644 --- a/Lib9c.Policy/NCStagePolicy.cs +++ b/Lib9c.Policy/NCStagePolicy.cs @@ -90,14 +90,7 @@ public bool Stage(BlockChain blockChain, Transaction transaction) { if (_accessControlService?.GetTxQuota(transaction.Signer) is { } acsTxQuota) { - if (_quotaPerSignerList.ContainsKey(transaction.Signer)) - { - _quotaPerSignerList[transaction.Signer] = acsTxQuota; - } - else - { - _quotaPerSignerList.TryAdd(transaction.Signer, acsTxQuota); - } + _quotaPerSignerList[transaction.Signer] = acsTxQuota; if (acsTxQuota == 0) {