Skip to content

Commit

Permalink
test stagepolicy
Browse files Browse the repository at this point in the history
  • Loading branch information
area363 committed Jul 24, 2023
1 parent 7f72ea1 commit ae04edd
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion Lib9c.Policy/NCStagePolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ namespace Nekoyume.Blockchain
using Libplanet.Blockchain;
using Libplanet.Blockchain.Policies;
using Libplanet.Tx;
using Serilog;

public class NCStagePolicy : IStagePolicy
{
private static Dictionary<Address, DateTimeOffset> _bannedAccountTracker = new();

Check failure on line 16 in Lib9c.Policy/NCStagePolicy.cs

View workflow job for this annotation

GitHub Actions / build-for-unity

Feature 'target-typed object creation' is not available in C# 8.0. Please use language version 9.0 or greater.

Check failure on line 16 in Lib9c.Policy/NCStagePolicy.cs

View workflow job for this annotation

GitHub Actions / build-for-unity

Feature 'target-typed object creation' is not available in C# 8.0. Please use language version 9.0 or greater.
private readonly VolatileStagePolicy _impl;
private readonly ConcurrentDictionary<Address, SortedList<Transaction, TxId>> _txs;
private readonly int _quotaPerSigner;

private static readonly ImmutableHashSet<Address> _bannedAccounts = new[]
private ImmutableHashSet<Address> _bannedAccounts = new[]
{
new Address("de96aa7702a7a1fd18ee0f84a5a0c7a2c28ec840"),
new Address("153281c93274bEB9726A03C33d3F19a8D78ad805"),
Expand Down Expand Up @@ -78,6 +80,16 @@ public IEnumerable<Transaction> Iterate(BlockChain blockChain, bool filtered = t
{
s.Remove(s.Max);
}

if (s.Count - _quotaPerSigner > -1)
{
if (!_bannedAccountTracker.ContainsKey(tx.Signer))
{
Log.Debug("Adding {signer} to banned accounts for 30 minutes", tx.Signer);
_bannedAccountTracker.Add(tx.Signer, DateTimeOffset.Now);
_bannedAccounts = _bannedAccounts.Add(tx.Signer);
}
}
}

#pragma warning disable LAA1002 // DictionariesOrSetsShouldBeOrderedToEnumerate
Expand All @@ -94,6 +106,16 @@ public bool Stage(BlockChain blockChain, Transaction transaction)
{
if (_bannedAccounts.Contains(transaction.Signer))
{
if (_bannedAccountTracker.ContainsKey(transaction.Signer))
{
if ((DateTimeOffset.Now - _bannedAccountTracker[transaction.Signer]).Minutes >= 30)
{
Log.Debug("Removing {signer} from banned accounts after 30 minutes", transaction.Signer);
_bannedAccountTracker.Remove(transaction.Signer);
_bannedAccounts = _bannedAccounts.Remove(transaction.Signer);
}
}

return false;
}

Expand Down

0 comments on commit ae04edd

Please sign in to comment.