From 92bb173f9c4852dfc841734a14fdeb43f872cb7c Mon Sep 17 00:00:00 2001 From: area363 Date: Tue, 7 Nov 2023 17:55:34 +0900 Subject: [PATCH 1/3] move NCStagePolicy and IAccessControlService to headless --- .../IAccessControlService.cs | 9 -- Lib9c.Policy/NCStagePolicy.cs | 140 ------------------ 2 files changed, 149 deletions(-) delete mode 100644 Lib9c.Policy/AccessControlService/IAccessControlService.cs delete mode 100644 Lib9c.Policy/NCStagePolicy.cs diff --git a/Lib9c.Policy/AccessControlService/IAccessControlService.cs b/Lib9c.Policy/AccessControlService/IAccessControlService.cs deleted file mode 100644 index 42e7dc1208..0000000000 --- a/Lib9c.Policy/AccessControlService/IAccessControlService.cs +++ /dev/null @@ -1,9 +0,0 @@ -using Libplanet.Crypto; - -namespace Nekoyume.Blockchain -{ - public interface IAccessControlService - { - public int? GetTxQuota(Address address); - } -} diff --git a/Lib9c.Policy/NCStagePolicy.cs b/Lib9c.Policy/NCStagePolicy.cs deleted file mode 100644 index 0dbd00c1f5..0000000000 --- a/Lib9c.Policy/NCStagePolicy.cs +++ /dev/null @@ -1,140 +0,0 @@ -namespace Nekoyume.Blockchain -{ - using System; - using System.Collections.Concurrent; - using System.Collections.Generic; - using System.Collections.Immutable; - using System.Linq; - using Libplanet.Blockchain; - using Libplanet.Blockchain.Policies; - using Libplanet.Crypto; - using Libplanet.Types.Tx; - - public class NCStagePolicy : IStagePolicy - { - private readonly VolatileStagePolicy _impl; - private readonly ConcurrentDictionary> _txs; - private readonly int _quotaPerSigner; - private IAccessControlService? _accessControlService; - - public NCStagePolicy(TimeSpan txLifeTime, int quotaPerSigner, IAccessControlService? accessControlService = null) - { - if (quotaPerSigner < 1) - { - throw new ArgumentOutOfRangeException( - $"{nameof(quotaPerSigner)} must be positive: ${quotaPerSigner}"); - } - - _txs = new ConcurrentDictionary>(); - _quotaPerSigner = quotaPerSigner; - _impl = (txLifeTime == default) - ? new VolatileStagePolicy() - : new VolatileStagePolicy(txLifeTime); - - _accessControlService = accessControlService; - } - - public Transaction Get(BlockChain blockChain, TxId id, bool filtered = true) - => _impl.Get(blockChain, id, filtered); - - public long GetNextTxNonce(BlockChain blockChain, Address address) - => _impl.GetNextTxNonce(blockChain, address); - - public void Ignore(BlockChain blockChain, TxId id) - => _impl.Ignore(blockChain, id); - - public bool Ignores(BlockChain blockChain, TxId id) - => _impl.Ignores(blockChain, id); - - public IEnumerable Iterate(BlockChain blockChain, bool filtered = true) - { - if (filtered) - { - 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 ACS returns a value for the signer. - if (_accessControlService?.GetTxQuota(tx.Signer) is { } acsTxQuota) - { - txQuotaPerSigner = acsTxQuota; - } - - - if (s.Count > txQuotaPerSigner) - { - s.Remove(s.Max); - } - } - -#pragma warning disable LAA1002 // DictionariesOrSetsShouldBeOrderedToEnumerate - return txsPerSigner.Values.SelectMany(i => i); -#pragma warning restore LAA1002 // DictionariesOrSetsShouldBeOrderedToEnumerate - } - else - { - return _impl.Iterate(blockChain, filtered); - } - } - - public bool Stage(BlockChain blockChain, Transaction transaction) - { - if (_accessControlService?.GetTxQuota(transaction.Signer) is { } acsTxQuota - && 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; - } - - return _impl.Stage(blockChain, transaction); - } - - public bool Unstage(BlockChain blockChain, TxId id) - => _impl.Unstage(blockChain, id); - - private class TxComparer : IComparer - { - public int Compare(Transaction x, Transaction y) - { - if (x.Nonce < y.Nonce) - { - return -1; - } - else if (x.Nonce > y.Nonce) - { - return 1; - } - else if (x.Timestamp < y.Timestamp) - { - return -1; - } - else if (x.Timestamp > y.Timestamp) - { - return 1; - } - else - { - return 0; - } - } - } - } -} From e5b1772eefa05947348b98a8eadfef5491e2bbb4 Mon Sep 17 00:00:00 2001 From: area363 Date: Tue, 7 Nov 2023 18:10:13 +0900 Subject: [PATCH 2/3] remove unused test --- .Lib9c.Tests/StagePolicyTest.cs | 257 -------------------------------- 1 file changed, 257 deletions(-) delete mode 100644 .Lib9c.Tests/StagePolicyTest.cs diff --git a/.Lib9c.Tests/StagePolicyTest.cs b/.Lib9c.Tests/StagePolicyTest.cs deleted file mode 100644 index 506b134d7a..0000000000 --- a/.Lib9c.Tests/StagePolicyTest.cs +++ /dev/null @@ -1,257 +0,0 @@ -namespace Lib9c.Tests -{ - using System; - using System.Collections.Generic; - using System.Linq; - using System.Threading.Tasks; - using Lib9c.Renderers; - using Lib9c.Tests.TestHelper; - using Libplanet.Action; - using Libplanet.Blockchain; - using Libplanet.Blockchain.Policies; - using Libplanet.Crypto; - using Libplanet.Types.Tx; - using Nekoyume.Action; - using Nekoyume.Blockchain; - using Nekoyume.Blockchain.Policy; - using Serilog.Core; - using Xunit; - - public class StagePolicyTest - { - private readonly PrivateKey[] _accounts; - - private readonly Dictionary _txs; - - public StagePolicyTest() - { - _accounts = new[] - { - new PrivateKey(), - new PrivateKey(), - new PrivateKey(), - new PrivateKey(), - }; - _txs = _accounts.ToDictionary( - acc => acc.ToAddress(), - acc => Enumerable - .Range(0, 10) - .Select( - n => Transaction.Create( - n, - acc, - default, - new ActionBase[0].ToPlainValues() - ) - ) - .ToArray() - ); - } - - [Fact] - public void Stage() - { - NCStagePolicy stagePolicy = new NCStagePolicy(TimeSpan.FromHours(1), 2); - BlockChain chain = MakeChainWithStagePolicy(stagePolicy); - - stagePolicy.Stage(chain, _txs[_accounts[0].ToAddress()][0]); - stagePolicy.Stage(chain, _txs[_accounts[0].ToAddress()][1]); - stagePolicy.Stage(chain, _txs[_accounts[1].ToAddress()][0]); - stagePolicy.Stage(chain, _txs[_accounts[2].ToAddress()][0]); - - AssertTxs( - chain, - stagePolicy, - _txs[_accounts[0].ToAddress()][0], - _txs[_accounts[0].ToAddress()][1], - _txs[_accounts[1].ToAddress()][0], - _txs[_accounts[2].ToAddress()][0] - ); - } - - [Fact] - public void StageOverQuota() - { - NCStagePolicy stagePolicy = new NCStagePolicy(TimeSpan.FromHours(1), 2); - BlockChain chain = MakeChainWithStagePolicy(stagePolicy); - - stagePolicy.Stage(chain, _txs[_accounts[0].ToAddress()][0]); - stagePolicy.Stage(chain, _txs[_accounts[0].ToAddress()][1]); - stagePolicy.Stage(chain, _txs[_accounts[0].ToAddress()][2]); - stagePolicy.Stage(chain, _txs[_accounts[0].ToAddress()][3]); - - AssertTxs( - chain, - stagePolicy, - _txs[_accounts[0].ToAddress()][0], - _txs[_accounts[0].ToAddress()][1] - ); - } - - [Fact] - public void StageOverQuotaInverseOrder() - { - NCStagePolicy stagePolicy = new NCStagePolicy(TimeSpan.FromHours(1), 2); - BlockChain chain = MakeChainWithStagePolicy(stagePolicy); - - stagePolicy.Stage(chain, _txs[_accounts[0].ToAddress()][3]); - stagePolicy.Stage(chain, _txs[_accounts[0].ToAddress()][2]); - stagePolicy.Stage(chain, _txs[_accounts[0].ToAddress()][1]); - stagePolicy.Stage(chain, _txs[_accounts[0].ToAddress()][0]); - - AssertTxs( - chain, - stagePolicy, - _txs[_accounts[0].ToAddress()][0], - _txs[_accounts[0].ToAddress()][1] - ); - } - - [Fact] - public void StageOverQuotaOutOfOrder() - { - NCStagePolicy stagePolicy = new NCStagePolicy(TimeSpan.FromHours(1), 2); - BlockChain chain = MakeChainWithStagePolicy(stagePolicy); - - stagePolicy.Stage(chain, _txs[_accounts[0].ToAddress()][2]); - stagePolicy.Stage(chain, _txs[_accounts[0].ToAddress()][1]); - stagePolicy.Stage(chain, _txs[_accounts[0].ToAddress()][3]); - stagePolicy.Stage(chain, _txs[_accounts[0].ToAddress()][0]); - - AssertTxs( - chain, - stagePolicy, - _txs[_accounts[0].ToAddress()][0], - _txs[_accounts[0].ToAddress()][1] - ); - } - - [Fact] - public void StageSameNonce() - { - NCStagePolicy stagePolicy = new NCStagePolicy(TimeSpan.FromHours(1), 2); - BlockChain chain = MakeChainWithStagePolicy(stagePolicy); - var txA = Transaction.Create(0, _accounts[0], default, new ActionBase[0].ToPlainValues()); - var txB = Transaction.Create(0, _accounts[0], default, new ActionBase[0].ToPlainValues()); - var txC = Transaction.Create(0, _accounts[0], default, new ActionBase[0].ToPlainValues()); - - stagePolicy.Stage(chain, txA); - stagePolicy.Stage(chain, txB); - stagePolicy.Stage(chain, txC); - - AssertTxs(chain, stagePolicy, txA, txB); - } - - [Fact] - public async Task StateFromMultiThread() - { - NCStagePolicy stagePolicy = new NCStagePolicy(TimeSpan.FromHours(1), 2); - BlockChain chain = MakeChainWithStagePolicy(stagePolicy); - - await Task.WhenAll( - Enumerable - .Range(0, 40) - .Select(i => Task.Run(() => - { - stagePolicy.Stage(chain, _txs[_accounts[i / 10].ToAddress()][i % 10]); - })) - ); - AssertTxs( - chain, - stagePolicy, - _txs[_accounts[0].ToAddress()][0], - _txs[_accounts[0].ToAddress()][1], - _txs[_accounts[1].ToAddress()][0], - _txs[_accounts[1].ToAddress()][1], - _txs[_accounts[2].ToAddress()][0], - _txs[_accounts[2].ToAddress()][1], - _txs[_accounts[3].ToAddress()][0], - _txs[_accounts[3].ToAddress()][1] - ); - } - - [Fact] - public void IterateAfterUnstage() - { - NCStagePolicy stagePolicy = new NCStagePolicy(TimeSpan.FromHours(1), 2); - BlockChain chain = MakeChainWithStagePolicy(stagePolicy); - - stagePolicy.Stage(chain, _txs[_accounts[0].ToAddress()][0]); - stagePolicy.Stage(chain, _txs[_accounts[0].ToAddress()][1]); - stagePolicy.Stage(chain, _txs[_accounts[0].ToAddress()][2]); - stagePolicy.Stage(chain, _txs[_accounts[0].ToAddress()][3]); - - AssertTxs( - chain, - stagePolicy, - _txs[_accounts[0].ToAddress()][0], - _txs[_accounts[0].ToAddress()][1] - ); - - stagePolicy.Unstage(chain, _txs[_accounts[0].ToAddress()][0].Id); - - AssertTxs( - chain, - stagePolicy, - _txs[_accounts[0].ToAddress()][1], - _txs[_accounts[0].ToAddress()][2] - ); - } - - [Fact] - public void CalculateNextTxNonceCorrectWhenTxOverQuota() - { - NCStagePolicy stagePolicy = new NCStagePolicy(TimeSpan.FromHours(1), 2); - BlockChain chain = MakeChainWithStagePolicy(stagePolicy); - - long nextTxNonce = chain.GetNextTxNonce(_accounts[0].ToAddress()); - Assert.Equal(0, nextTxNonce); - var txA = Transaction.Create(nextTxNonce, _accounts[0], default, new ActionBase[0].ToPlainValues()); - stagePolicy.Stage(chain, txA); - - nextTxNonce = chain.GetNextTxNonce(_accounts[0].ToAddress()); - Assert.Equal(1, nextTxNonce); - var txB = Transaction.Create(nextTxNonce, _accounts[0], default, new ActionBase[0].ToPlainValues()); - stagePolicy.Stage(chain, txB); - - nextTxNonce = chain.GetNextTxNonce(_accounts[0].ToAddress()); - Assert.Equal(2, nextTxNonce); - var txC = Transaction.Create(nextTxNonce, _accounts[0], default, new ActionBase[0].ToPlainValues()); - stagePolicy.Stage(chain, txC); - - nextTxNonce = chain.GetNextTxNonce(_accounts[0].ToAddress()); - Assert.Equal(3, nextTxNonce); - - AssertTxs( - chain, - stagePolicy, - txA, - txB); - } - - private void AssertTxs(BlockChain blockChain, NCStagePolicy policy, params Transaction[] txs) - { - foreach (Transaction tx in txs) - { - Assert.Equal(tx, policy.Get(blockChain, tx.Id, filtered: true)); - } - - Assert.Equal( - txs.ToHashSet(), - policy.Iterate(blockChain, filtered: true).ToHashSet() - ); - } - - private BlockChain MakeChainWithStagePolicy(NCStagePolicy stagePolicy) - { - BlockPolicySource blockPolicySource = new BlockPolicySource(); - IBlockPolicy policy = blockPolicySource.GetPolicy(); - BlockChain chain = - BlockChainHelper.MakeBlockChain( - blockRenderers: new[] { new BlockRenderer() }, - policy: policy, - stagePolicy: stagePolicy); - return chain; - } - } -} From fefa24749fe4815233da217b4f563c8d2f2befb9 Mon Sep 17 00:00:00 2001 From: area363 Date: Tue, 7 Nov 2023 18:38:29 +0900 Subject: [PATCH 3/3] fix RewardGoldTest --- .Lib9c.Tests/Action/RewardGoldTest.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.Lib9c.Tests/Action/RewardGoldTest.cs b/.Lib9c.Tests/Action/RewardGoldTest.cs index dd515ca590..9e1dc27504 100644 --- a/.Lib9c.Tests/Action/RewardGoldTest.cs +++ b/.Lib9c.Tests/Action/RewardGoldTest.cs @@ -482,7 +482,7 @@ void AssertMinerReward(int blockIndex, string expected) public async Task Genesis_StateRootHash(bool mainnet) { BlockPolicySource blockPolicySource = new BlockPolicySource(); - NCStagePolicy stagePolicy = new NCStagePolicy(default, 2); + IStagePolicy stagePolicy = new VolatileStagePolicy(); IBlockPolicy policy = blockPolicySource.GetPolicy(); Block genesis; if (mainnet)