From 076aa3e56a0ac46d6f65f46ab949e5dd0089de8b Mon Sep 17 00:00:00 2001 From: s2quake Date: Thu, 26 Dec 2024 10:21:58 +0900 Subject: [PATCH 1/2] chore: Remove warnings from policy-related code --- Lib9c.Policy/NCStagePolicy.cs | 23 +++++++++++++++---- .../Policy/BlockPolicySource.Utils.cs | 6 ++--- Lib9c.Policy/Policy/DebugPolicy.cs | 4 ++-- Lib9c.Policy/Policy/SpannedSubPolicy.cs | 2 +- Lib9c.Policy/Policy/VariableSubPolicy.cs | 4 ++-- 5 files changed, 27 insertions(+), 12 deletions(-) diff --git a/Lib9c.Policy/NCStagePolicy.cs b/Lib9c.Policy/NCStagePolicy.cs index 121f6a53ca..2f02e7231d 100644 --- a/Lib9c.Policy/NCStagePolicy.cs +++ b/Lib9c.Policy/NCStagePolicy.cs @@ -38,7 +38,7 @@ public NCStagePolicy(TimeSpan txLifeTime, int quotaPerSigner, IAccessControlServ _accessControlService = accessControlService; } - public Transaction Get(BlockChain blockChain, TxId id, bool filtered = true) + public Transaction? Get(BlockChain blockChain, TxId id, bool filtered = true) => _impl.Get(blockChain, id, filtered); public long GetNextTxNonce(BlockChain blockChain, Address address) @@ -79,9 +79,9 @@ public IEnumerable Iterate(BlockChain blockChain, bool filtered = t txQuotaPerSigner = _quotaPerSigner; } - if (s.Count > txQuotaPerSigner) + if (s.Count > txQuotaPerSigner && s.Max is { } max) { - s.Remove(s.Max); + s.Remove(max); } } @@ -141,8 +141,23 @@ public bool Unstage(BlockChain blockChain, TxId id) private class TxComparer : IComparer { - public int Compare(Transaction x, Transaction y) + public int Compare(Transaction? x, Transaction? y) { + if (x == null && y == null) + { + return 0; + } + + if (x == null) + { + return -1; + } + + if (y == null) + { + return 1; + } + if (x.Nonce < y.Nonce) { return -1; diff --git a/Lib9c.Policy/Policy/BlockPolicySource.Utils.cs b/Lib9c.Policy/Policy/BlockPolicySource.Utils.cs index 19b7362310..ee08521aed 100644 --- a/Lib9c.Policy/Policy/BlockPolicySource.Utils.cs +++ b/Lib9c.Policy/Policy/BlockPolicySource.Utils.cs @@ -89,7 +89,7 @@ internal static bool IsAdminTransaction(BlockChain blockChain, Transaction trans : null; } - private static InvalidBlockBytesLengthException ValidateTransactionsBytesRaw( + private static InvalidBlockBytesLengthException? ValidateTransactionsBytesRaw( Block block, IVariableSubPolicy maxTransactionsBytesPolicy) { @@ -108,7 +108,7 @@ private static InvalidBlockBytesLengthException ValidateTransactionsBytesRaw( return null; } - private static BlockPolicyViolationException ValidateTxCountPerBlockRaw( + private static BlockPolicyViolationException? ValidateTxCountPerBlockRaw( Block block, IVariableSubPolicy minTransactionsPerBlockPolicy, IVariableSubPolicy maxTransactionsPerBlockPolicy) @@ -138,7 +138,7 @@ private static BlockPolicyViolationException ValidateTxCountPerBlockRaw( return null; } - private static BlockPolicyViolationException ValidateTxCountPerSignerPerBlockRaw( + private static BlockPolicyViolationException? ValidateTxCountPerSignerPerBlockRaw( Block block, IVariableSubPolicy maxTransactionsPerSignerPerBlockPolicy) { diff --git a/Lib9c.Policy/Policy/DebugPolicy.cs b/Lib9c.Policy/Policy/DebugPolicy.cs index 8a1f5d1fbd..34daaf98e5 100644 --- a/Lib9c.Policy/Policy/DebugPolicy.cs +++ b/Lib9c.Policy/Policy/DebugPolicy.cs @@ -35,13 +35,13 @@ public DebugPolicy() new Reward(), new Refund(), }.ToImmutableArray()); - public TxPolicyViolationException ValidateNextBlockTx( + public TxPolicyViolationException? ValidateNextBlockTx( BlockChain blockChain, Transaction transaction) { return null; } - public BlockPolicyViolationException ValidateNextBlock( + public BlockPolicyViolationException? ValidateNextBlock( BlockChain blockChain, Block nextBlock) { return null; diff --git a/Lib9c.Policy/Policy/SpannedSubPolicy.cs b/Lib9c.Policy/Policy/SpannedSubPolicy.cs index bda1b8921f..a683c8612a 100644 --- a/Lib9c.Policy/Policy/SpannedSubPolicy.cs +++ b/Lib9c.Policy/Policy/SpannedSubPolicy.cs @@ -18,7 +18,7 @@ public class SpannedSubPolicy /// If an invalid value is given for either /// or . public SpannedSubPolicy( - long startIndex, long? endIndex, Predicate filter, T value) + long startIndex, long? endIndex, Predicate? filter, T value) { if (startIndex < 0) { diff --git a/Lib9c.Policy/Policy/VariableSubPolicy.cs b/Lib9c.Policy/Policy/VariableSubPolicy.cs index 54898e4599..527cad0e58 100644 --- a/Lib9c.Policy/Policy/VariableSubPolicy.cs +++ b/Lib9c.Policy/Policy/VariableSubPolicy.cs @@ -82,7 +82,7 @@ public IVariableSubPolicy Add(SpannedSubPolicy spannedSubPolicy) BindingFlags.Instance | BindingFlags.NonPublic, null, new object[] { this, spannedSubPolicy }, - null); + null)!; } catch (TargetInvocationException tie) { @@ -135,7 +135,7 @@ private Func ToGetter() /// private void Validate() { - SpannedSubPolicy prev = null; + SpannedSubPolicy? prev = null; foreach (SpannedSubPolicy next in SpannedSubPolicies) { if (prev is SpannedSubPolicy _prev) From 29727bcfebd903cb0f618d57624fdf0273c714dc Mon Sep 17 00:00:00 2001 From: s2quake Date: Thu, 26 Dec 2024 10:22:05 +0900 Subject: [PATCH 2/2] chore: Remove warnings from test code --- .../Action/CreateOrReplaceAvatarTest.cs | 2 +- .../Action/ManipulateStateTest.cs | 6 ++--- ...ustomActionsDeserializableValidatorTest.cs | 27 +++++++++++++++---- 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/.Lib9c.DevExtensions.Tests/Action/CreateOrReplaceAvatarTest.cs b/.Lib9c.DevExtensions.Tests/Action/CreateOrReplaceAvatarTest.cs index dabab140b0..34cadeec75 100644 --- a/.Lib9c.DevExtensions.Tests/Action/CreateOrReplaceAvatarTest.cs +++ b/.Lib9c.DevExtensions.Tests/Action/CreateOrReplaceAvatarTest.cs @@ -466,7 +466,7 @@ private static void Execute( RandomSeed = 0, BlockIndex = blockIndex, }); - var agent = nextStates.GetAgentState(agentAddr); + var agent = nextStates.GetAgentState(agentAddr)!; Assert.Single(agent.avatarAddresses); Assert.True(agent.avatarAddresses.ContainsKey(action.AvatarIndex)); avatarAddr ??= agent.avatarAddresses[action.AvatarIndex]; diff --git a/.Lib9c.DevExtensions.Tests/Action/ManipulateStateTest.cs b/.Lib9c.DevExtensions.Tests/Action/ManipulateStateTest.cs index 3887982e34..1aa650a5a4 100644 --- a/.Lib9c.DevExtensions.Tests/Action/ManipulateStateTest.cs +++ b/.Lib9c.DevExtensions.Tests/Action/ManipulateStateTest.cs @@ -29,7 +29,7 @@ public class ManipulateStateTest private static readonly Address AdminAddr = new PrivateKey().Address; // See also InitializeUtil.cs - private static readonly Currency Ncg = Currency.Legacy( + private static readonly Currency Ncg = Currency.Uncapped( "NCG", 2, null @@ -187,7 +187,7 @@ public static IEnumerable FetchWorldInfo() yield return new object[] { - tableSheets.WorldSheet.OrderedList.Last(world => world.Id < 100).StageEnd, + tableSheets.WorldSheet.OrderedList!.Last(world => world.Id < 100).StageEnd, new WorldInformation(0L, worldSheet, true), }; } @@ -421,7 +421,7 @@ private void TestAvatarState( private void TestInventoryState(IWorld state, Inventory targetInventory) { - var inventoryState = new Inventory((List)state.GetAccount(Addresses.Inventory).GetState(_avatarAddress)); + var inventoryState = new Inventory((List)state.GetAccount(Addresses.Inventory).GetState(_avatarAddress)!); Assert.Equal(targetInventory.Items.Count, inventoryState.Items.Count); foreach (var item in targetInventory.Items) { diff --git a/.Lib9c.Miner.Tests/CustomActionsDeserializableValidatorTest.cs b/.Lib9c.Miner.Tests/CustomActionsDeserializableValidatorTest.cs index cf58ed01d1..5731873a69 100644 --- a/.Lib9c.Miner.Tests/CustomActionsDeserializableValidatorTest.cs +++ b/.Lib9c.Miner.Tests/CustomActionsDeserializableValidatorTest.cs @@ -56,11 +56,24 @@ public IWorld Execute(IActionContext context) private class MockTransaction : ITransaction { + private IImmutableSet
? _updatedAddresses; + private PublicKey? _publicKey; + private byte[]? _signature; + public long Nonce { get; init; } public Address Signer { get; init; } - public IImmutableSet
UpdatedAddresses { get; init; } + public IImmutableSet
UpdatedAddresses + { + get => _updatedAddresses + ?? throw new InvalidOperationException("UpdatedAddresses is not set."); + init => _updatedAddresses = value; + } public DateTimeOffset Timestamp { get; init; } - public PublicKey PublicKey { get; init; } + public PublicKey PublicKey + { + get => _publicKey ?? throw new InvalidOperationException("PublicKey is not set."); + init => _publicKey = value; + } public BlockHash? GenesisHash { get; init; } public TxActionList Actions => new(SystemAction is { } sa ? new IValue[]{ sa } : CustomActions!); @@ -70,12 +83,16 @@ private class MockTransaction : ITransaction public long? GasLimit => null; public TxId Id { get; init; } - public byte[] Signature { get; init; } + public byte[] Signature + { + get => _signature ?? throw new InvalidOperationException("Signature is not set."); + init => _signature = value; + } public IValue? SystemAction { get; init; } public IImmutableList? CustomActions { get; init; } public bool Equals(ITxInvoice? other) { - return UpdatedAddresses.Equals(other.UpdatedAddresses) && + return UpdatedAddresses.Equals(other?.UpdatedAddresses) && Timestamp.Equals(other.Timestamp) && Nullable.Equals(GenesisHash, other.GenesisHash) && Actions.Equals(other.Actions); @@ -83,7 +100,7 @@ public bool Equals(ITxInvoice? other) public bool Equals(ITxSigningMetadata? other) { - return Nonce == other.Nonce && + return Nonce == other?.Nonce && Signer.Equals(other.Signer) && PublicKey.Equals(other.PublicKey); }