Skip to content

Commit

Permalink
test2
Browse files Browse the repository at this point in the history
  • Loading branch information
area363 committed Nov 23, 2023
1 parent bf1fe65 commit 405562b
Showing 1 changed file with 68 additions and 45 deletions.
113 changes: 68 additions & 45 deletions Lib9c.Policy/NCStagePolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,73 +50,96 @@ public bool Ignores(BlockChain blockChain, TxId id)

public IEnumerable<Transaction> Iterate(BlockChain blockChain, bool filtered = true)
{
if (filtered)
try
{
var txsPerSigner = new Dictionary<Address, SortedSet<Transaction>>();
foreach (Transaction tx in _impl.Iterate(blockChain, filtered))
{
if (!txsPerSigner.TryGetValue(tx.Signer, out var s))
{
txsPerSigner[tx.Signer] = s = new SortedSet<Transaction>(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<Address, SortedSet<Transaction>>();
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<Transaction>(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)
Expand Down

0 comments on commit 405562b

Please sign in to comment.