From c0291c83060b59b0899cfbd9d44a5ce21e54fe2e Mon Sep 17 00:00:00 2001 From: moreal Date: Mon, 15 Jul 2024 09:39:34 +0900 Subject: [PATCH] Remove unused actions --- .Lib9c.Tests/Action/ActionEvaluationTest.cs | 4 - .Lib9c.Tests/Action/ActivateAccountTest.cs | 83 ------ .../Action/AddActivatedAccountTest.cs | 69 ----- .../Snapshot/TransferAsset0SnapshotTest.cs | 140 ---------- .../transfer_asset.PlainValue.verified.txt | 73 ----- ...er_asset.TransferCrystal.diff.verified.txt | 12 - ...r_asset.TransferWithMemo.diff.verified.txt | 12 - .Lib9c.Tests/Action/Stake0Test.cs | 258 ------------------ .Lib9c.Tests/Policy/BlockPolicySourceTest.cs | 13 +- .Lib9c.Tests/Policy/BlockPolicyTest.cs | 4 - Lib9c/Action/ActivateAccount.cs | 92 ------- Lib9c/Action/AddActivatedAccount.cs | 64 ----- Lib9c/Action/Stake0.cs | 134 --------- Lib9c/Action/TransferAsset0.cs | 138 ---------- Lib9c/Model/ActivationKey.cs | 5 - Lib9c/Model/State/PendingActivationState.cs | 5 - 16 files changed, 9 insertions(+), 1097 deletions(-) delete mode 100644 .Lib9c.Tests/Action/ActivateAccountTest.cs delete mode 100644 .Lib9c.Tests/Action/AddActivatedAccountTest.cs delete mode 100644 .Lib9c.Tests/Action/Snapshot/TransferAsset0SnapshotTest.cs delete mode 100644 .Lib9c.Tests/Action/Snapshot/transfer_asset.PlainValue.verified.txt delete mode 100644 .Lib9c.Tests/Action/Snapshot/transfer_asset.TransferCrystal.diff.verified.txt delete mode 100644 .Lib9c.Tests/Action/Snapshot/transfer_asset.TransferWithMemo.diff.verified.txt delete mode 100644 .Lib9c.Tests/Action/Stake0Test.cs delete mode 100644 Lib9c/Action/ActivateAccount.cs delete mode 100644 Lib9c/Action/AddActivatedAccount.cs delete mode 100644 Lib9c/Action/Stake0.cs delete mode 100644 Lib9c/Action/TransferAsset0.cs diff --git a/.Lib9c.Tests/Action/ActionEvaluationTest.cs b/.Lib9c.Tests/Action/ActionEvaluationTest.cs index 3b04609245..6f32dd9c29 100644 --- a/.Lib9c.Tests/Action/ActionEvaluationTest.cs +++ b/.Lib9c.Tests/Action/ActionEvaluationTest.cs @@ -51,8 +51,6 @@ public ActionEvaluationTest() [InlineData(typeof(TransferAsset))] [InlineData(typeof(CreateAvatar))] [InlineData(typeof(HackAndSlash))] - [InlineData(typeof(ActivateAccount))] - [InlineData(typeof(AddActivatedAccount))] [InlineData(typeof(AddRedeemCode))] [InlineData(typeof(Buy))] [InlineData(typeof(ChargeActionPoint))] @@ -155,8 +153,6 @@ private ActionBase GetAction(Type type) StageId = 0, AvatarAddress = new PrivateKey().Address, }, - ActivateAccount _ => new ActivateAccount(new PrivateKey().Address, new byte[] { 0x0 }), - AddActivatedAccount _ => new AddActivatedAccount(), AddRedeemCode _ => new AddRedeemCode { redeemCsv = "csv", diff --git a/.Lib9c.Tests/Action/ActivateAccountTest.cs b/.Lib9c.Tests/Action/ActivateAccountTest.cs deleted file mode 100644 index 0aa531f1bf..0000000000 --- a/.Lib9c.Tests/Action/ActivateAccountTest.cs +++ /dev/null @@ -1,83 +0,0 @@ -namespace Lib9c.Tests.Action -{ - using System; - using Bencodex.Types; - using Libplanet.Action.State; - using Libplanet.Crypto; - using Libplanet.Mocks; - using Nekoyume.Action; - using Nekoyume.Model; - using Nekoyume.Model.State; - using Nekoyume.Module; - using Xunit; - - public class ActivateAccountTest - { - [Theory] - [InlineData(false, true, false, null)] - [InlineData(true, true, false, typeof(InvalidSignatureException))] - [InlineData(false, false, false, typeof(PendingActivationDoesNotExistsException))] - [InlineData(false, true, true, typeof(AlreadyActivatedException))] - public void Execute(bool invalid, bool pendingExist, bool alreadyActivated, Type exc) - { - var nonce = new byte[] { 0x00, 0x01, 0x02, 0x03 }; - var privateKey = new PrivateKey(); - (ActivationKey activationKey, PendingActivationState pendingActivation) = - ActivationKey.Create(privateKey, nonce); - - Address activatedAddress = default(Address).Derive(ActivationKey.DeriveKey); - IWorld state = new World(MockUtil.MockModernWorldState); - - if (pendingExist) - { - state = state.SetLegacyState(pendingActivation.address, pendingActivation.Serialize()); - } - - if (alreadyActivated) - { - state = state.SetLegacyState(activatedAddress, true.Serialize()); - } - - ActivateAccount action = activationKey.CreateActivateAccount(invalid ? new byte[] { 0x00 } : nonce); - - if (exc is null) - { - IWorld nextState = action.Execute(new ActionContext() - { - PreviousState = state, - Signer = default, - BlockIndex = 1, - }); - - Assert.Equal(Null.Value, nextState.GetLegacyState(pendingActivation.address)); - Assert.True(nextState.GetLegacyState(activatedAddress).ToBoolean()); - } - else - { - Assert.Throws(exc, () => action.Execute(new ActionContext() - { - PreviousState = state, - Signer = default, - BlockIndex = 1, - })); - } - } - - [Fact] - public void PlainValue() - { - var nonce = new byte[] { 0x00, 0x01, 0x02, 0x03 }; - var privateKey = new PrivateKey(); - (ActivationKey activationKey, PendingActivationState pendingActivation) = - ActivationKey.Create(privateKey, nonce); - - ActivateAccount action = activationKey.CreateActivateAccount(nonce); - - var action2 = new ActivateAccount(); - action2.LoadPlainValue(action.PlainValue); - - Assert.Equal(action.Signature, action2.Signature); - Assert.Equal(action.PendingAddress, action2.PendingAddress); - } - } -} diff --git a/.Lib9c.Tests/Action/AddActivatedAccountTest.cs b/.Lib9c.Tests/Action/AddActivatedAccountTest.cs deleted file mode 100644 index 9a4240fc54..0000000000 --- a/.Lib9c.Tests/Action/AddActivatedAccountTest.cs +++ /dev/null @@ -1,69 +0,0 @@ -namespace Lib9c.Tests.Action -{ - using System; - using Libplanet.Action.State; - using Libplanet.Crypto; - using Libplanet.Mocks; - using Nekoyume.Action; - using Nekoyume.Model; - using Nekoyume.Model.State; - using Nekoyume.Module; - using Xunit; - - public class AddActivatedAccountTest - { - [Theory] - [InlineData(true, 1, false, null)] - [InlineData(true, 101, false, typeof(PolicyExpiredException))] - [InlineData(false, 1, false, typeof(PermissionDeniedException))] - [InlineData(true, 1, true, typeof(AlreadyActivatedException))] - public void Execute(bool isAdmin, long blockIndex, bool alreadyActivated, Type exc) - { - var admin = new Address("8d9f76aF8Dc5A812aCeA15d8bf56E2F790F47fd7"); - IWorld state = new World(MockUtil.MockModernWorldState) - .SetLegacyState(AdminState.Address, new AdminState(admin, 100).Serialize()); - var newComer = new Address("399bddF9F7B6d902ea27037B907B2486C9910730"); - var activatedAddress = newComer.Derive(ActivationKey.DeriveKey); - if (alreadyActivated) - { - state = state.SetLegacyState(activatedAddress, true.Serialize()); - } - - var action = new AddActivatedAccount(newComer); - var signer = isAdmin ? admin : default; - - if (exc is null) - { - IWorld nextState = action.Execute(new ActionContext() - { - BlockIndex = blockIndex, - Miner = default, - PreviousState = state, - Signer = signer, - }); - Assert.True(nextState.GetLegacyState(activatedAddress).ToBoolean()); - } - else - { - Assert.Throws(exc, () => action.Execute(new ActionContext() - { - BlockIndex = blockIndex, - Miner = default, - PreviousState = state, - Signer = signer, - })); - } - } - - [Fact] - public void PlainValue() - { - var newComer = new Address("399bddF9F7B6d902ea27037B907B2486C9910730"); - var action = new AddActivatedAccount(newComer); - var action2 = new AddActivatedAccount(); - action2.LoadPlainValue(action.PlainValue); - - Assert.Equal(action.Address, action2.Address); - } - } -} diff --git a/.Lib9c.Tests/Action/Snapshot/TransferAsset0SnapshotTest.cs b/.Lib9c.Tests/Action/Snapshot/TransferAsset0SnapshotTest.cs deleted file mode 100644 index 0bc6bfcd1a..0000000000 --- a/.Lib9c.Tests/Action/Snapshot/TransferAsset0SnapshotTest.cs +++ /dev/null @@ -1,140 +0,0 @@ -namespace Lib9c.Tests.Action.Snapshot -{ - using System.Linq; - using System.Threading.Tasks; - using Bencodex.Types; - using Libplanet.Action.State; - using Libplanet.Common; - using Libplanet.Crypto; - using Libplanet.Store; - using Libplanet.Store.Trie; - using Libplanet.Types.Assets; - using Nekoyume.Action; - using Nekoyume.Helper; - using Nekoyume.Module; - using VerifyTests; - using VerifyXunit; - using Xunit; - using static ActionUtils; - - [UsesVerify] - public class TransferAsset0SnapshotTest - { - public TransferAsset0SnapshotTest() - { - VerifierSettings.SortPropertiesAlphabetically(); - } - - [Fact] - public Task PlainValue() - { - var action = new TransferAsset0( - default(Address), - default(Address), - Currency.Legacy("NNN", 2, null) * 100); - - return Verifier - .Verify(action.PlainValue) - .UseTypeName((Text)GetActionTypeId()); - } - - [Fact] - public Task TransferCrystal() - { - var senderPrivateKey = - new PrivateKey(ByteUtil.ParseHex( - "810234bc093e2b66406b06dd0c2d2d3320bc5f19caef7acd3f800424bd46cb60")); - var recipientPrivateKey = - new PrivateKey(ByteUtil.ParseHex( - "f8960846e9ae4ad1c23686f74c8e5f80f22336b6f2175be21db82afa8823c92d")); - var senderAddress = senderPrivateKey.Address; - var recipientAddress = recipientPrivateKey.Address; - var crystal = CrystalCalculator.CRYSTAL; - var context = new ActionContext(); - - var stateStore = new TrieStateStore(new MemoryKeyValueStore()); - IWorld state = new World(new WorldBaseState(stateStore.GetStateRoot(null), stateStore)) - .MintAsset(context, senderAddress, crystal * 100); - var inputLegacyTrie = stateStore.Commit(state.GetAccount(ReservedAddresses.LegacyAccount).Trie); - - var actionContext = new ActionContext - { - Signer = senderAddress, - PreviousState = state, - }; - var action = new TransferAsset0( - senderAddress, - recipientAddress, - crystal * 20); - - var outputState = action.Execute(actionContext); - var outputLegacyTrie = stateStore.Commit(outputState.GetAccount(ReservedAddresses.LegacyAccount).Trie); - - var trieDiff = outputLegacyTrie.Diff(inputLegacyTrie) - .Select(elem => new object[] - { - ByteUtil.Hex(elem.Path.ByteArray), - elem.TargetValue?.ToString(), - elem.SourceValue.ToString(), - }) - .ToArray(); - var accountDiff = AccountDiff.Create(inputLegacyTrie, outputLegacyTrie); - - // Verifier does not handle tuples well when nested. - var diff = Verifier - .Verify(trieDiff) - .UseTypeName((Text)GetActionTypeId()) - .UseMethodName($"{nameof(TransferCrystal)}.diff"); - return diff; - } - - [Fact] - public Task TransferWithMemo() - { - var senderPrivateKey = - new PrivateKey(ByteUtil.ParseHex( - "810234bc093e2b66406b06dd0c2d2d3320bc5f19caef7acd3f800424bd46cb60")); - var recipientPrivateKey = - new PrivateKey(ByteUtil.ParseHex( - "f8960846e9ae4ad1c23686f74c8e5f80f22336b6f2175be21db82afa8823c92d")); - var senderAddress = senderPrivateKey.Address; - var recipientAddress = recipientPrivateKey.Address; - var crystal = CrystalCalculator.CRYSTAL; - var context = new ActionContext(); - - var stateStore = new TrieStateStore(new MemoryKeyValueStore()); - IWorld state = new World(new WorldBaseState(stateStore.GetStateRoot(null), stateStore)) - .MintAsset(context, senderAddress, crystal * 100); - var inputLegacyTrie = stateStore.Commit(state.GetAccount(ReservedAddresses.LegacyAccount).Trie); - - var actionContext = new ActionContext - { - Signer = senderAddress, - PreviousState = state, - }; - var action = new TransferAsset0( - senderAddress, - recipientAddress, - crystal * 20, - "MEMO"); - var outputState = action.Execute(actionContext); - var outputLegacyTrie = stateStore.Commit(outputState.GetAccount(ReservedAddresses.LegacyAccount).Trie); - - var trieDiff = outputLegacyTrie.Diff(inputLegacyTrie) - .Select(elem => new object[] - { - ByteUtil.Hex(elem.Path.ByteArray), - elem.TargetValue?.ToString(), - elem.SourceValue.ToString(), - }) - .ToArray(); - - // Verifier does not handle tuples well when nested. - var diff = Verifier - .Verify(trieDiff) - .UseTypeName((Text)GetActionTypeId()) - .UseMethodName($"{nameof(TransferWithMemo)}.diff"); - return diff; - } - } -} diff --git a/.Lib9c.Tests/Action/Snapshot/transfer_asset.PlainValue.verified.txt b/.Lib9c.Tests/Action/Snapshot/transfer_asset.PlainValue.verified.txt deleted file mode 100644 index 9aee256842..0000000000 --- a/.Lib9c.Tests/Action/Snapshot/transfer_asset.PlainValue.verified.txt +++ /dev/null @@ -1,73 +0,0 @@ -{ - Bencodex.Types.Text "type_id": { - EncodingLength: 18, - Kind: Text, - Value: transfer_asset - }, - Bencodex.Types.Text "values": { - Bencodex.Types.Text "amount": [ - { - Bencodex.Types.Text "decimalPlaces": [ - 2 - ], - Bencodex.Types.Text "minters": { - EncodingLength: 1 - }, - Bencodex.Types.Text "ticker": { - EncodingLength: 6, - Kind: Text, - Value: NNN - } - }, - { - EncodingLength: 7, - Kind: Integer, - Value: 10000 - } - ], - Bencodex.Types.Text "recipient": [ - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0 - ], - Bencodex.Types.Text "sender": [ - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0 - ] - } -} \ No newline at end of file diff --git a/.Lib9c.Tests/Action/Snapshot/transfer_asset.TransferCrystal.diff.verified.txt b/.Lib9c.Tests/Action/Snapshot/transfer_asset.TransferCrystal.diff.verified.txt deleted file mode 100644 index 35aff764ea..0000000000 --- a/.Lib9c.Tests/Action/Snapshot/transfer_asset.TransferCrystal.diff.verified.txt +++ /dev/null @@ -1,12 +0,0 @@ -[ - [ - 5f393961303663326264373166306561613831393666653435626535636134323465653830393733335f64656564663762343337363963303635656263396563376162653636613838336466303234303762, - null, - Bencodex.Types.Integer 20000000000000000000 - ], - [ - 5f633631633337363639336536623236373137633965653961636135613234353330323866366666615f64656564663762343337363963303635656263396563376162653636613838336466303234303762, - Bencodex.Types.Integer 100000000000000000000, - Bencodex.Types.Integer 80000000000000000000 - ] -] \ No newline at end of file diff --git a/.Lib9c.Tests/Action/Snapshot/transfer_asset.TransferWithMemo.diff.verified.txt b/.Lib9c.Tests/Action/Snapshot/transfer_asset.TransferWithMemo.diff.verified.txt deleted file mode 100644 index 35aff764ea..0000000000 --- a/.Lib9c.Tests/Action/Snapshot/transfer_asset.TransferWithMemo.diff.verified.txt +++ /dev/null @@ -1,12 +0,0 @@ -[ - [ - 5f393961303663326264373166306561613831393666653435626535636134323465653830393733335f64656564663762343337363963303635656263396563376162653636613838336466303234303762, - null, - Bencodex.Types.Integer 20000000000000000000 - ], - [ - 5f633631633337363639336536623236373137633965653961636135613234353330323866366666615f64656564663762343337363963303635656263396563376162653636613838336466303234303762, - Bencodex.Types.Integer 100000000000000000000, - Bencodex.Types.Integer 80000000000000000000 - ] -] \ No newline at end of file diff --git a/.Lib9c.Tests/Action/Stake0Test.cs b/.Lib9c.Tests/Action/Stake0Test.cs deleted file mode 100644 index d47d89be41..0000000000 --- a/.Lib9c.Tests/Action/Stake0Test.cs +++ /dev/null @@ -1,258 +0,0 @@ -namespace Lib9c.Tests.Action -{ - using Bencodex.Types; - using Libplanet.Action.State; - using Libplanet.Crypto; - using Libplanet.Mocks; - using Libplanet.Types.Assets; - using Nekoyume; - using Nekoyume.Action; - using Nekoyume.Model.State; - using Nekoyume.Module; - using Serilog; - using Xunit; - using Xunit.Abstractions; - - public class Stake0Test - { - private readonly IWorld _initialState; - private readonly Currency _currency; - private readonly GoldCurrencyState _goldCurrencyState; - private readonly TableSheets _tableSheets; - private readonly Address _signerAddress; - - public Stake0Test(ITestOutputHelper outputHelper) - { - Log.Logger = new LoggerConfiguration() - .MinimumLevel.Verbose() - .WriteTo.TestOutput(outputHelper) - .CreateLogger(); - - var context = new ActionContext(); - _initialState = new World(MockUtil.MockModernWorldState); - - var sheets = TableSheetsImporter.ImportSheets(); - foreach (var (key, value) in sheets) - { - _initialState = _initialState - .SetLegacyState(Addresses.TableSheet.Derive(key), value.Serialize()); - } - - _tableSheets = new TableSheets(sheets); - -#pragma warning disable CS0618 - // Use of obsolete method Currency.Legacy(): https://github.com/planetarium/lib9c/discussions/1319 - _currency = Currency.Legacy("NCG", 2, null); -#pragma warning restore CS0618 - _goldCurrencyState = new GoldCurrencyState(_currency); - - _signerAddress = new PrivateKey().Address; - _initialState = _initialState - .SetLegacyState(GoldCurrencyState.Address, _goldCurrencyState.Serialize()) - .MintAsset(context, _signerAddress, _currency * 100); - } - - [Fact] - public void Execute_Throws_WhenNotEnoughBalance() - { - var action = new Stake0(200); - Assert.Throws(() => - action.Execute(new ActionContext - { - PreviousState = _initialState, - Signer = _signerAddress, - BlockIndex = 100, - })); - } - - [Fact] - public void Execute_Throws_WhenThereIsMonsterCollection() - { - Address monsterCollectionAddress = - MonsterCollectionState.DeriveAddress(_signerAddress, 0); - var agentState = new AgentState(_signerAddress) - { - avatarAddresses = { [0] = new PrivateKey().Address, }, - }; - var states = _initialState - .SetAgentState(_signerAddress, agentState) - .SetLegacyState( - monsterCollectionAddress, - new MonsterCollectionState(monsterCollectionAddress, 1, 0).SerializeV2()); - var action = new Stake0(200); - Assert.Throws(() => - action.Execute(new ActionContext - { - PreviousState = states, - Signer = _signerAddress, - BlockIndex = 100, - })); - } - - [Fact] - public void Execute_Throws_WhenClaimableExisting() - { - Address stakeStateAddress = StakeState.DeriveAddress(_signerAddress); - var context = new ActionContext(); - var states = _initialState - .SetLegacyState(stakeStateAddress, new StakeState(stakeStateAddress, 0).Serialize()) - .MintAsset(context, stakeStateAddress, _currency * 50); - var action = new Stake0(100); - Assert.Throws(() => - action.Execute(new ActionContext - { - PreviousState = states, - Signer = _signerAddress, - BlockIndex = StakeState.RewardInterval, - })); - } - - [Fact] - public void Execute_Throws_WhenCancelOrUpdateWhileLockup() - { - var action = new Stake0(51); - var states = action.Execute(new ActionContext - { - PreviousState = _initialState, - Signer = _signerAddress, - BlockIndex = 0, - }); - - // Cancel - var updateAction = new Stake0(0); - Assert.Throws(() => updateAction.Execute(new ActionContext - { - PreviousState = states, - Signer = _signerAddress, - BlockIndex = 1, - })); - - // Less - updateAction = new Stake0(50); - Assert.Throws(() => updateAction.Execute(new ActionContext - { - PreviousState = states, - Signer = _signerAddress, - BlockIndex = 1, - })); - - // Same (since 4611070) - if (states.TryGetStakeState(_signerAddress, out StakeState stakeState)) - { - states = states.SetLegacyState( - stakeState.address, - new StakeState(stakeState.address, 4611070 - 100).Serialize()); - } - - updateAction = new Stake0(51); - Assert.Throws(() => updateAction.Execute(new ActionContext - { - PreviousState = states, - Signer = _signerAddress, - BlockIndex = 4611070, - })); - - // At 4611070 - 99, it should be updated. - Assert.True(updateAction.Execute(new ActionContext - { - PreviousState = states, - Signer = _signerAddress, - BlockIndex = 4611070 - 99, - }).TryGetStakeState(_signerAddress, out stakeState)); - Assert.Equal(4611070 - 99, stakeState.StartedBlockIndex); - } - - [Fact] - public void Execute() - { - var action = new Stake0(100); - var states = action.Execute(new ActionContext - { - PreviousState = _initialState, - Signer = _signerAddress, - BlockIndex = 0, - }); - - Assert.Equal(_currency * 0, states.GetBalance(_signerAddress, _currency)); - Assert.Equal( - _currency * 100, - states.GetBalance(StakeState.DeriveAddress(_signerAddress), _currency)); - - states.TryGetStakeState(_signerAddress, out StakeState stakeState); - Assert.Equal(0, stakeState.StartedBlockIndex); - Assert.Equal(0 + StakeState.LockupInterval, stakeState.CancellableBlockIndex); - Assert.Equal(0, stakeState.ReceivedBlockIndex); - Assert.Equal(_currency * 100, states.GetBalance(stakeState.address, _currency)); - Assert.Equal(_currency * 0, states.GetBalance(_signerAddress, _currency)); - - var achievements = stakeState.Achievements; - Assert.False(achievements.Check(0, 0)); - Assert.False(achievements.Check(0, 1)); - Assert.False(achievements.Check(1, 0)); - - StakeState producedStakeState = new StakeState( - stakeState.address, - stakeState.StartedBlockIndex, - // Produce a situation that it already received rewards. - StakeState.LockupInterval - 1, - stakeState.CancellableBlockIndex, - stakeState.Achievements); - states = states.SetLegacyState(stakeState.address, producedStakeState.SerializeV2()); - var cancelAction = new Stake0(0); - states = cancelAction.Execute(new ActionContext - { - PreviousState = states, - Signer = _signerAddress, - BlockIndex = StakeState.LockupInterval, - }); - - Assert.Equal(Null.Value, states.GetLegacyState(stakeState.address)); - Assert.Equal(_currency * 0, states.GetBalance(stakeState.address, _currency)); - Assert.Equal(_currency * 100, states.GetBalance(_signerAddress, _currency)); - } - - [Fact] - public void Update() - { - var action = new Stake0(50); - var states = action.Execute(new ActionContext - { - PreviousState = _initialState, - Signer = _signerAddress, - BlockIndex = 0, - }); - - states.TryGetStakeState(_signerAddress, out StakeState stakeState); - Assert.Equal(0, stakeState.StartedBlockIndex); - Assert.Equal(0 + StakeState.LockupInterval, stakeState.CancellableBlockIndex); - Assert.Equal(0, stakeState.ReceivedBlockIndex); - Assert.Equal(_currency * 50, states.GetBalance(stakeState.address, _currency)); - Assert.Equal(_currency * 50, states.GetBalance(_signerAddress, _currency)); - - var updateAction = new Stake0(100); - states = updateAction.Execute(new ActionContext - { - PreviousState = states, - Signer = _signerAddress, - BlockIndex = 1, - }); - - states.TryGetStakeState(_signerAddress, out stakeState); - Assert.Equal(1, stakeState.StartedBlockIndex); - Assert.Equal(1 + StakeState.LockupInterval, stakeState.CancellableBlockIndex); - Assert.Equal(0, stakeState.ReceivedBlockIndex); - Assert.Equal(_currency * 100, states.GetBalance(stakeState.address, _currency)); - Assert.Equal(_currency * 0, states.GetBalance(_signerAddress, _currency)); - } - - [Fact] - public void Serialization() - { - var action = new Stake0(100); - var deserialized = new Stake0(); - deserialized.LoadPlainValue(action.PlainValue); - - Assert.Equal(action.Amount, deserialized.Amount); - } - } -} diff --git a/.Lib9c.Tests/Policy/BlockPolicySourceTest.cs b/.Lib9c.Tests/Policy/BlockPolicySourceTest.cs index 410f582f5e..1b4bd538c0 100644 --- a/.Lib9c.Tests/Policy/BlockPolicySourceTest.cs +++ b/.Lib9c.Tests/Policy/BlockPolicySourceTest.cs @@ -1,5 +1,6 @@ namespace Lib9c.Tests.Policy { + using System; using Bencodex.Types; using Libplanet.Action; using Libplanet.Action.Loader; @@ -26,23 +27,27 @@ public class BlockPolicySourceTest [Fact] public void IsObsolete() { - var transferAsset0 = new TransferAsset0(default, default, Currencies.Crystal * 0); + var buy = new Buy + { + buyerAvatarAddress = new PrivateKey().Address, + purchaseInfos = Array.Empty(), + }; var odinTx = Transaction.Create( 0, new PrivateKey(), OdinGenesisHash, - new[] { transferAsset0.PlainValue } + new[] { buy.PlainValue } ); var heimdallTx = Transaction.Create( 0, new PrivateKey(), HeimdallGenesisHash, - new[] { transferAsset0.PlainValue } + new[] { buy.PlainValue } ); var ncActionLoader = new NCActionLoader(); Assert.False(BlockPolicySource.IsObsolete(odinTx, ncActionLoader, 1)); - Assert.True(BlockPolicySource.IsObsolete(odinTx, ncActionLoader, 7_000_700)); + Assert.True(BlockPolicySource.IsObsolete(odinTx, ncActionLoader, 8_324_911)); Assert.True(BlockPolicySource.IsObsolete(heimdallTx, ncActionLoader, 1)); var odinTx2 = Transaction.Create( diff --git a/.Lib9c.Tests/Policy/BlockPolicyTest.cs b/.Lib9c.Tests/Policy/BlockPolicyTest.cs index 7c5fa87ea1..d3d8dc67ef 100644 --- a/.Lib9c.Tests/Policy/BlockPolicyTest.cs +++ b/.Lib9c.Tests/Policy/BlockPolicyTest.cs @@ -182,10 +182,6 @@ public void BlockCommitFromNonValidator() ), renderers: new[] { new BlockRenderer() } ); - blockChain.MakeTransaction( - adminPrivateKey, - new ActionBase[] { new AddActivatedAccount(adminPrivateKey.Address) } - ); Block block1 = blockChain.ProposeBlock(adminPrivateKey); Assert.Throws( () => blockChain.Append(block1, GenerateBlockCommit(block1, nonValidator))); diff --git a/Lib9c/Action/ActivateAccount.cs b/Lib9c/Action/ActivateAccount.cs deleted file mode 100644 index e6d00b7d49..0000000000 --- a/Lib9c/Action/ActivateAccount.cs +++ /dev/null @@ -1,92 +0,0 @@ -using System; -using System.Collections.Generic; -using Bencodex.Types; -using Lib9c.Abstractions; -using Libplanet.Action; -using Libplanet.Action.State; -using Libplanet.Crypto; -using Nekoyume.Model; -using Nekoyume.Model.State; -using Nekoyume.Module; -using Serilog; - -namespace Nekoyume.Action -{ - [Serializable] - [ActionType("activate_account2")] - [ActionObsolete(ActionObsoleteConfig.V200030ObsoleteIndex)] - public class ActivateAccount : ActionBase, IActivateAccount - { - public Address PendingAddress { get; private set; } - - public byte[] Signature { get; private set; } - - Address IActivateAccount.PendingAddress => PendingAddress; - byte[] IActivateAccount.Signature => Signature; - - public override IValue PlainValue => Dictionary.Empty - .Add("type_id", "activate_account2") - .Add("values", new Dictionary( - new[] - { - new KeyValuePair((Text)"pa", PendingAddress.Serialize()), - new KeyValuePair((Text)"s", (Binary) Signature), - } - )); - - public ActivateAccount() - { - } - - public ActivateAccount(Address pendingAddress, byte[] signature) - { - PendingAddress = pendingAddress; - Signature = signature; - } - - public override IWorld Execute(IActionContext context) - { - context.UseGas(1); - IWorld state = context.PreviousState; - Address activatedAddress = context.Signer.Derive(ActivationKey.DeriveKey); - - CheckObsolete(ActionObsoleteConfig.V200030ObsoleteIndex, context); - - if (!(state.GetLegacyState(activatedAddress) is null)) - { - throw new AlreadyActivatedException($"{context.Signer} already activated."); - } - if (!state.TryGetLegacyState(PendingAddress, out Dictionary pendingAsDict)) - { - throw new PendingActivationDoesNotExistsException(PendingAddress); - } - - var pending = new PendingActivationState(pendingAsDict); - - if (pending.Verify(this)) - { - // We left this log message to track activation history. - // Please delete it if we have an API for evaluation results on the Libplanet side. - Log.Information("{pendingAddress} is activated by {signer} now.", pending.address, context.Signer); - return state - .SetLegacyState(activatedAddress, true.Serialize()) - .SetLegacyState(pending.address, new Bencodex.Types.Null()); - } - else - { - throw new InvalidSignatureException(pending, Signature); - } - } - - public override void LoadPlainValue(IValue plainValue) - { - var asDict = (Dictionary)((Dictionary)plainValue)["values"]; - PendingAddress = asDict["pa"].ToAddress(); - Signature = ((Binary)asDict["s"]).ToByteArray(); - } - - public Address GetPendingAddress() => PendingAddress; - - public byte[] GetSignature() => Signature; - } -} diff --git a/Lib9c/Action/AddActivatedAccount.cs b/Lib9c/Action/AddActivatedAccount.cs deleted file mode 100644 index ccdd420e21..0000000000 --- a/Lib9c/Action/AddActivatedAccount.cs +++ /dev/null @@ -1,64 +0,0 @@ -using System; -using System.Collections.Generic; -using Bencodex.Types; -using Lib9c.Abstractions; -using Libplanet.Action; -using Libplanet.Action.State; -using Libplanet.Crypto; -using Nekoyume.Model; -using Nekoyume.Model.State; -using Nekoyume.Module; - -namespace Nekoyume.Action -{ - [Serializable] - [ActionType("add_activated_account2")] - [ActionObsolete(ActionObsoleteConfig.V200030ObsoleteIndex)] - public class AddActivatedAccount : ActionBase, IAddActivatedAccountV1 - { - public AddActivatedAccount(Address address) - { - Address = address; - } - - public AddActivatedAccount() - { - } - - public Address Address { get; private set; } - - Address IAddActivatedAccountV1.Address => Address; - - public override IValue PlainValue => Dictionary.Empty - .Add("type_id", "add_activated_account2") - .Add("values", new Dictionary( - new[] - { - new KeyValuePair((Text)"a", Address.Serialize()), - } - )); - - public override IWorld Execute(IActionContext context) - { - context.UseGas(1); - IWorld state = context.PreviousState; - var address = Address.Derive(ActivationKey.DeriveKey); - - CheckObsolete(ActionObsoleteConfig.V200030ObsoleteIndex, context); - if (!(state.GetLegacyState(address) is null)) - { - throw new AlreadyActivatedException($"{address} is already activated."); - } - - CheckPermission(context); - - return state.SetLegacyState(address, true.Serialize()); - } - - public override void LoadPlainValue(IValue plainValue) - { - var asDict = (Dictionary)((Dictionary)plainValue)["values"]; - Address = asDict["a"].ToAddress(); - } - } -} diff --git a/Lib9c/Action/Stake0.cs b/Lib9c/Action/Stake0.cs deleted file mode 100644 index 985d88957c..0000000000 --- a/Lib9c/Action/Stake0.cs +++ /dev/null @@ -1,134 +0,0 @@ -using System; -using System.Linq; -using System.Numerics; -using Bencodex.Types; -using Lib9c.Abstractions; -using Libplanet.Action; -using Libplanet.Action.State; -using Nekoyume.Model.State; -using Nekoyume.Module; -using Nekoyume.TableData; -using static Lib9c.SerializeKeys; - -namespace Nekoyume.Action -{ - [ActionType("stake")] - [ActionObsolete(ObsoleteIndex)] - public class Stake0 : ActionBase, IStakeV1 - { - public const long ObsoleteIndex = ActionObsoleteConfig.V200030ObsoleteIndex; - - internal BigInteger Amount { get; set; } - - BigInteger IStakeV1.Amount => Amount; - - public Stake0(BigInteger amount) - { - Amount = amount >= 0 - ? amount - : throw new ArgumentOutOfRangeException(nameof(amount)); - } - - public Stake0() - { - } - - public override IValue PlainValue => Dictionary.Empty - .Add("type_id", "stake") - .Add("values", Dictionary.Empty.Add(AmountKey, Amount)); - - public override void LoadPlainValue(IValue plainValue) - { - var dictionary = (Dictionary)((Dictionary)plainValue)["values"]; - Amount = dictionary[AmountKey].ToBigInteger(); - } - - public override IWorld Execute(IActionContext context) - { - context.UseGas(1); - CheckObsolete(ActionObsoleteConfig.V200030ObsoleteIndex, context); - IWorld states = context.PreviousState; - - // Restrict staking if there is a monster collection until now. - if (states.GetAgentState(context.Signer) is { } agentState && - states.TryGetLegacyState(MonsterCollectionState.DeriveAddress( - context.Signer, - agentState.MonsterCollectionRound), out Dictionary _)) - { - throw new MonsterCollectionExistingException(); - } - - CheckObsolete(ObsoleteIndex, context); - - if (Amount < 0) - { - throw new ArgumentOutOfRangeException(nameof(Amount)); - } - - var stakeRegularRewardSheet = states.GetSheet(); - var minimumRequiredGold = stakeRegularRewardSheet.OrderedRows.Min(x => x.RequiredGold); - if (Amount != 0 && Amount < minimumRequiredGold) - { - throw new ArgumentOutOfRangeException(nameof(Amount)); - } - - var stakeStateAddress = StakeState.DeriveAddress(context.Signer); - var currency = states.GetGoldCurrency(); - var currentBalance = states.GetBalance(context.Signer, currency); - var stakedBalance = states.GetBalance(stakeStateAddress, currency); - var targetStakeBalance = currency * Amount; - if (currentBalance + stakedBalance < targetStakeBalance) - { - throw new NotEnoughFungibleAssetValueException( - context.Signer.ToHex(), - Amount, - currentBalance); - } - - // Stake if it doesn't exist yet. - if (!states.TryGetStakeState(context.Signer, out StakeState stakeState)) - { - var stakeAchievementRewardSheet = states.GetSheet(); - - stakeState = new StakeState(stakeStateAddress, context.BlockIndex); - return states - .SetLegacyState( - stakeStateAddress, - stakeState.SerializeV2()) - .TransferAsset(context, context.Signer, stakeStateAddress, targetStakeBalance); - } - - if (stakeState.IsClaimable(context.BlockIndex)) - { - throw new StakeExistingClaimableException(); - } - - if (!stakeState.IsCancellable(context.BlockIndex) && - (context.BlockIndex >= 4611070 - ? targetStakeBalance <= stakedBalance - : targetStakeBalance < stakedBalance)) - { - throw new RequiredBlockIndexException(); - } - - // Cancel - if (Amount == 0) - { - if (stakeState.IsCancellable(context.BlockIndex)) - { - return states - .SetLegacyState(stakeState.address, Null.Value) - .TransferAsset(context, stakeState.address, context.Signer, stakedBalance); - } - } - - // Stake with more or less amount. - return states - .TransferAsset(context, stakeState.address, context.Signer, stakedBalance) - .TransferAsset(context, context.Signer, stakeState.address, targetStakeBalance) - .SetLegacyState( - stakeState.address, - new StakeState(stakeState.address, context.BlockIndex).SerializeV2()); - } - } -} diff --git a/Lib9c/Action/TransferAsset0.cs b/Lib9c/Action/TransferAsset0.cs deleted file mode 100644 index ba19fd9ff4..0000000000 --- a/Lib9c/Action/TransferAsset0.cs +++ /dev/null @@ -1,138 +0,0 @@ -using Bencodex; -using Bencodex.Types; -using Libplanet.Action; -using Libplanet.Action.State; -using Libplanet.Crypto; -using Libplanet.Types.Assets; -using Nekoyume.Model.State; -using Nekoyume.Module; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Runtime.Serialization; -using Lib9c.Abstractions; - -namespace Nekoyume.Action -{ - [Serializable] - [ActionObsolete(ActionObsoleteConfig.V200020AccidentObsoleteIndex)] - [ActionType("transfer_asset")] - public class TransferAsset0 : ActionBase, ISerializable, ITransferAsset, ITransferAssetV1 - { - private const int MemoMaxLength = 80; - - public TransferAsset0() - { - } - - public TransferAsset0(Address sender, Address recipient, FungibleAssetValue amount, string memo = null) - { - Sender = sender; - Recipient = recipient; - Amount = amount; - - CheckMemoLength(memo); - Memo = memo; - } - - protected TransferAsset0(SerializationInfo info, StreamingContext context) - { - var rawBytes = (byte[])info.GetValue("serialized", typeof(byte[])); - Dictionary pv = (Dictionary) new Codec().Decode(rawBytes); - - LoadPlainValue(pv); - } - - public Address Sender { get; private set; } - public Address Recipient { get; private set; } - public FungibleAssetValue Amount { get; private set; } - public string Memo { get; private set; } - - Address ITransferAssetV1.Sender => Sender; - Address ITransferAssetV1.Recipient => Recipient; - FungibleAssetValue ITransferAssetV1.Amount => Amount; - string ITransferAssetV1.Memo => Memo; - - public override IValue PlainValue - { - get - { - IEnumerable> pairs = new[] - { - new KeyValuePair((Text) "sender", Sender.Serialize()), - new KeyValuePair((Text) "recipient", Recipient.Serialize()), - new KeyValuePair((Text) "amount", Amount.Serialize()), - }; - - if (!(Memo is null)) - { - pairs = pairs.Append(new KeyValuePair((Text) "memo", Memo.Serialize())); - } - - return Dictionary.Empty - .Add("type_id", "transfer_asset") - .Add("values", new Dictionary(pairs)); - } - } - - public override IWorld Execute(IActionContext context) - { - context.UseGas(4); - var state = context.PreviousState; - - CheckObsolete(ActionObsoleteConfig.V100080ObsoleteIndex, context); - - if (Sender != context.Signer) - { - throw new InvalidTransferSignerException(context.Signer, Sender, Recipient); - } - - // This works for block after 380000. Please take a look at - // https://github.com/planetarium/libplanet/pull/1133 - if (context.BlockIndex > 380000 && Sender == Recipient) - { - throw new InvalidTransferRecipientException(Sender, Recipient); - } - - Currency currency = Amount.Currency; - if (!(currency.Minters is null) && - (currency.Minters.Contains(Sender) || currency.Minters.Contains(Recipient))) - { - throw new InvalidTransferMinterException( - currency.Minters, - Sender, - Recipient - ); - } - - return state.TransferAsset(context, Sender, Recipient, Amount); - } - - public override void LoadPlainValue(IValue plainValue) - { - var asDict = (Dictionary)((Dictionary)plainValue)["values"]; - - Sender = asDict["sender"].ToAddress(); - Recipient = asDict["recipient"].ToAddress(); - Amount = asDict["amount"].ToFungibleAssetValue(); - Memo = asDict.TryGetValue((Text) "memo", out IValue memo) ? memo.ToDotnetString() : null; - - CheckMemoLength(Memo); - } - - public void GetObjectData(SerializationInfo info, StreamingContext context) - { - info.AddValue("serialized", new Codec().Encode(PlainValue)); - } - - private void CheckMemoLength(string memo) - { - if (memo?.Length > MemoMaxLength) - { - string msg = $"The length of the memo, {memo.Length}, " + - $"is overflowed than the max length, {MemoMaxLength}."; - throw new MemoLengthOverflowException(msg); - } - } - } -} diff --git a/Lib9c/Model/ActivationKey.cs b/Lib9c/Model/ActivationKey.cs index 487af19302..69fb2960d4 100644 --- a/Lib9c/Model/ActivationKey.cs +++ b/Lib9c/Model/ActivationKey.cs @@ -59,10 +59,5 @@ public string Encode() { return $"{ByteUtil.Hex(PrivateKey.ByteArray)}/{ByteUtil.Hex(PendingAddress.ByteArray)}"; } - - public ActivateAccount CreateActivateAccount(byte[] nonce) - { - return new ActivateAccount(PendingAddress, PrivateKey.Sign(nonce)); - } } } diff --git a/Lib9c/Model/State/PendingActivationState.cs b/Lib9c/Model/State/PendingActivationState.cs index ce1e601284..4545a37043 100644 --- a/Lib9c/Model/State/PendingActivationState.cs +++ b/Lib9c/Model/State/PendingActivationState.cs @@ -60,11 +60,6 @@ public void GetObjectData(SerializationInfo info, StreamingContext context) info.AddValue("serialized", new Codec().Encode(Serialize())); } - public bool Verify(ActivateAccount action) - { - return Verify(action.Signature); - } - public bool Verify(byte[] signature) { return PublicKey.Verify(Nonce, signature);