Skip to content

Commit

Permalink
Merge pull request #2167 from riemannulus/refactor/stakestateutils
Browse files Browse the repository at this point in the history
refactor: make `StakeStateUtils` more general
  • Loading branch information
riemannulus authored Oct 5, 2023
2 parents 545ab28 + e1f1e44 commit fa91d8e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 12 deletions.
9 changes: 8 additions & 1 deletion .Lib9c.Tests/Model/Stake/StakeStateUtilsTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace Lib9c.Tests.Model.Stake
{
using System;
using Bencodex.Types;
using Lib9c.Tests.Action;
using Lib9c.Tests.Fixtures.TableCSV;
using Lib9c.Tests.Fixtures.TableCSV.Stake;
Expand All @@ -18,7 +19,13 @@ public class StakeStateUtilsTest
public void TryMigrate_Throw_NullReferenceException_When_IAccountDelta_Null()
{
Assert.Throws<NullReferenceException>(() =>
StakeStateUtils.TryMigrate(null, default, out _));
StakeStateUtils.TryMigrate((IAccount)null, default, out _));
}

[Fact]
public void TryMigrate_Return_False_When_IValue_Null()
{
Assert.False(StakeStateUtils.TryMigrate((IValue)null, default, out _));
}

[Fact]
Expand Down
47 changes: 36 additions & 11 deletions Lib9c/Model/Stake/StakeStateUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,59 @@ public static bool TryMigrate(
Address stakeStateAddr,
out StakeStateV2 stakeStateV2)
{
var serialized = state.GetState(stakeStateAddr);
if (serialized is null or Null)
var nullableStateState =
Migrate(state.GetState(stakeStateAddr), state.GetGameConfigState());
if (nullableStateState is null)
{
stakeStateV2 = default;
return false;
}

stakeStateV2 = nullableStateState.Value;
return true;
}

public static bool TryMigrate(
IValue serialized,
GameConfigState gameConfigState,
out StakeStateV2 stakeStateV2)
{
var nullableStateState = Migrate(serialized, gameConfigState);
if (nullableStateState is null)
{
stakeStateV2 = default;
return false;
}

stakeStateV2 = nullableStateState.Value;
return true;
}

public static StakeStateV2? Migrate(
IValue serialized,
GameConfigState gameConfigState)
{
if (serialized is null or Null)
{
return null;
}

// NOTE: StakeStateV2 is serialized as Bencodex List.
if (serialized is List list)
{
stakeStateV2 = new StakeStateV2(list);
return true;
return new StakeStateV2(list);
}

// NOTE: StakeState is serialized as Bencodex Dictionary.
if (serialized is not Dictionary dict)
{
stakeStateV2 = default;
return false;
return null;
}

// NOTE: Migration needs GameConfigState.
var gameConfigState = state.GetGameConfigState();
if (gameConfigState is null)
{
stakeStateV2 = default;
return false;
return null;
}

// NOTE: Below is the migration logic from StakeState to StakeStateV2.
Expand Down Expand Up @@ -92,14 +118,13 @@ public static bool TryMigrate(
stakeRegularRewardSheetTableName = "StakeRegularRewardSheet_V5";
}

stakeStateV2 = new StakeStateV2(
return new StakeStateV2(
stakeStateV1,
new Contract(
stakeRegularFixedRewardSheetTableName: stakeRegularFixedRewardSheetTableName,
stakeRegularRewardSheetTableName: stakeRegularRewardSheetTableName,
rewardInterval: StakeState.RewardInterval,
lockupInterval: StakeState.LockupInterval));
return true;
}
}
}

0 comments on commit fa91d8e

Please sign in to comment.