Skip to content

Commit

Permalink
minimize overuse of acs
Browse files Browse the repository at this point in the history
  • Loading branch information
area363 committed Nov 23, 2023
1 parent 62fe478 commit 6a1dd91
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions Lib9c.Policy/NCStagePolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class NCStagePolicy : IStagePolicy
private readonly VolatileStagePolicy _impl;
private readonly ConcurrentDictionary<Address, SortedList<Transaction, TxId>> _txs;
private readonly int _quotaPerSigner;
private readonly Dictionary<Address, int> _quotaPerSignerList;
private IAccessControlService? _accessControlService;

public NCStagePolicy(TimeSpan txLifeTime, int quotaPerSigner, IAccessControlService? accessControlService = null)
Expand All @@ -31,6 +32,7 @@ public NCStagePolicy(TimeSpan txLifeTime, int quotaPerSigner, IAccessControlServ
? new VolatileStagePolicy()
: new VolatileStagePolicy(txLifeTime);

_quotaPerSignerList = new Dictionary<Address, int>();
_accessControlService = accessControlService;
}

Expand Down Expand Up @@ -61,10 +63,10 @@ public IEnumerable<Transaction> 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;
}


Expand All @@ -86,10 +88,13 @@ public IEnumerable<Transaction> 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[]
Expand Down

0 comments on commit 6a1dd91

Please sign in to comment.