diff --git a/Lib9c.Policy/NCStagePolicy.cs b/Lib9c.Policy/NCStagePolicy.cs index d8467e0643..d64e0ac66a 100644 --- a/Lib9c.Policy/NCStagePolicy.cs +++ b/Lib9c.Policy/NCStagePolicy.cs @@ -50,73 +50,96 @@ public bool Ignores(BlockChain blockChain, TxId id) public IEnumerable Iterate(BlockChain blockChain, bool filtered = true) { - if (filtered) + try { - var txsPerSigner = new Dictionary>(); - foreach (Transaction tx in _impl.Iterate(blockChain, filtered)) - { - if (!txsPerSigner.TryGetValue(tx.Signer, out var s)) - { - txsPerSigner[tx.Signer] = s = new SortedSet(new TxComparer()); - } - - s.Add(tx); - int txQuotaPerSigner = _quotaPerSigner; - - // update txQuotaPerSigner if signer is in the list - if (_quotaPerSignerList.TryGetValue(tx.Signer, out int signerQuota)) - { - Console.WriteLine("[TEST-QUOTA] ITERATE1 {0} quota: {1}", tx.Signer, signerQuota); - txQuotaPerSigner = signerQuota; - } - if (_quotaPerSignerList.ContainsKey(tx.Signer)) + if (filtered) + { + var txsPerSigner = new Dictionary>(); + foreach (Transaction tx in _impl.Iterate(blockChain, filtered)) { - Console.WriteLine("[TEST-QUOTA] ITERATE2 {0} quota: {1}", tx.Signer, _quotaPerSignerList[tx.Signer]); - txQuotaPerSigner = _quotaPerSignerList[tx.Signer]; + if (!txsPerSigner.TryGetValue(tx.Signer, out var s)) + { + txsPerSigner[tx.Signer] = + s = new SortedSet(new TxComparer()); + } + + s.Add(tx); + int txQuotaPerSigner = _quotaPerSigner; + + // update txQuotaPerSigner if signer is in the list + if (_quotaPerSignerList.TryGetValue(tx.Signer, out int signerQuota)) + { + Console.WriteLine("[TEST-QUOTA] ITERATE1 {0} quota: {1}", tx.Signer, + signerQuota); + txQuotaPerSigner = signerQuota; + } + + if (_quotaPerSignerList.ContainsKey(tx.Signer)) + { + Console.WriteLine("[TEST-QUOTA] ITERATE2 {0} quota: {1}", tx.Signer, + _quotaPerSignerList[tx.Signer]); + txQuotaPerSigner = _quotaPerSignerList[tx.Signer]; + } + + if (s.Count > txQuotaPerSigner) + { + s.Remove(s.Max); + } } - if (s.Count > txQuotaPerSigner) - { - s.Remove(s.Max); - } - } - #pragma warning disable LAA1002 // DictionariesOrSetsShouldBeOrderedToEnumerate - return txsPerSigner.Values.SelectMany(i => i); + return txsPerSigner.Values.SelectMany(i => i); #pragma warning restore LAA1002 // DictionariesOrSetsShouldBeOrderedToEnumerate + } + else + { + return _impl.Iterate(blockChain, filtered); + } } - else + catch (Exception ex) { + Console.WriteLine("[TEST-QUOTA] ITERATE: {0}", ex.Message); + Console.WriteLine("[TEST-QUOTA] ITERATE: {0}", ex.StackTrace); return _impl.Iterate(blockChain, filtered); } } public bool Stage(BlockChain blockChain, Transaction transaction) { - if (_accessControlService?.GetTxQuota(transaction.Signer) is { } acsTxQuota) + try { - Console.WriteLine("[TEST-QUOTA] STAGE {0} quota: {1}", transaction.Signer, acsTxQuota); - _quotaPerSignerList.Add(transaction.Signer, acsTxQuota); - if (acsTxQuota == 0) + + if (_accessControlService?.GetTxQuota(transaction.Signer) is { } acsTxQuota) + { + Console.WriteLine("[TEST-QUOTA] STAGE {0} quota: {1}", transaction.Signer, acsTxQuota); + _quotaPerSignerList.Add(transaction.Signer, acsTxQuota); + if (acsTxQuota == 0) + { + return false; + } + } + + var deniedTxs = new[] + { + // CreatePledge Transaction with 50000 addresses + TxId.FromHex("300826da62b595d8cd663dadf04995a7411534d1cdc17dac75ce88754472f774"), + // CreatePledge Transaction with 5000 addresses + TxId.FromHex("210d1374d8f068de657de6b991e63888da9cadbc68e505ac917b35568b5340f8"), + }; + if (deniedTxs.Contains(transaction.Id)) { return false; } - } - var deniedTxs = new[] - { - // CreatePledge Transaction with 50000 addresses - TxId.FromHex("300826da62b595d8cd663dadf04995a7411534d1cdc17dac75ce88754472f774"), - // CreatePledge Transaction with 5000 addresses - TxId.FromHex("210d1374d8f068de657de6b991e63888da9cadbc68e505ac917b35568b5340f8"), - }; - if (deniedTxs.Contains(transaction.Id)) + return _impl.Stage(blockChain, transaction); + } + catch (Exception ex) { - return false; + Console.WriteLine("[TEST-QUOTA] STAGE: {0}", ex.Message); + Console.WriteLine("[TEST-QUOTA] STAGE: {0}", ex.StackTrace); + return _impl.Stage(blockChain, transaction); } - - return _impl.Stage(blockChain, transaction); } public bool Unstage(BlockChain blockChain, TxId id)