|
| 1 | +package migration |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/filecoin-project/go-address" |
| 9 | + "github.com/filecoin-project/go-state-types/abi" |
| 10 | + "github.com/filecoin-project/go-state-types/big" |
| 11 | + reward18 "github.com/filecoin-project/go-state-types/builtin/v18/reward" |
| 12 | + smoothing18 "github.com/filecoin-project/go-state-types/builtin/v18/util/smoothing" |
| 13 | + reward19 "github.com/filecoin-project/go-state-types/builtin/v19/reward" |
| 14 | + "github.com/filecoin-project/go-state-types/migration" |
| 15 | + "github.com/ipfs/go-cid" |
| 16 | + cbor "github.com/ipfs/go-ipld-cbor" |
| 17 | + "github.com/stretchr/testify/require" |
| 18 | +) |
| 19 | + |
| 20 | +func migrationIDAddress(t *testing.T, id uint64) address.Address { |
| 21 | + t.Helper() |
| 22 | + addr, err := address.NewIDAddress(id) |
| 23 | + require.NoError(t, err) |
| 24 | + return addr |
| 25 | +} |
| 26 | + |
| 27 | +func validRewardMigrationConfig(t *testing.T, activationEpoch abi.ChainEpoch) RewardMigrationConfig { |
| 28 | + t.Helper() |
| 29 | + pct := reward19.Denom / 100 |
| 30 | + return RewardMigrationConfig{ |
| 31 | + SWATimelockEpochs: 20_160, |
| 32 | + SWAActor: migrationIDAddress(t, 100), |
| 33 | + Streams: []reward19.RegisterStreamParams{ |
| 34 | + { |
| 35 | + ID: 1, |
| 36 | + Weight: reward19.WeightRecord{VStart: 95 * pct, Slope: -1, TStart: activationEpoch, Floor: 50 * pct, Cap: 95 * pct}, |
| 37 | + ActivationEpoch: activationEpoch, |
| 38 | + }, |
| 39 | + { |
| 40 | + ID: 2, |
| 41 | + Weight: reward19.WeightRecord{VStart: 5 * pct, Slope: 1, TStart: activationEpoch, Floor: 5 * pct, Cap: 10 * pct}, |
| 42 | + Distribution: &reward19.DistributionInit{ |
| 43 | + Writer: migrationIDAddress(t, 101), |
| 44 | + Shares: []reward19.RecipientShare{{Recipient: migrationIDAddress(t, 102), Share: reward19.Denom}}, |
| 45 | + }, |
| 46 | + ActivationEpoch: activationEpoch, |
| 47 | + }, |
| 48 | + }, |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +func TestRewardMigration(t *testing.T) { |
| 53 | + ctx, cancel := context.WithTimeout(context.Background(), time.Second) |
| 54 | + defer cancel() |
| 55 | + req := require.New(t) |
| 56 | + store := cbor.NewMemCborStore() |
| 57 | + |
| 58 | + inState := reward18.State{ |
| 59 | + CumsumBaseline: big.NewInt(1), |
| 60 | + CumsumRealized: big.NewInt(2), |
| 61 | + EffectiveNetworkTime: 3, |
| 62 | + EffectiveBaselinePower: big.NewInt(4), |
| 63 | + ThisEpochReward: abi.NewTokenAmount(5), |
| 64 | + ThisEpochRewardSmoothed: smoothing18.NewEstimate(big.NewInt(6), big.NewInt(7)), |
| 65 | + ThisEpochBaselinePower: big.NewInt(8), |
| 66 | + Epoch: 9, |
| 67 | + TotalStoragePowerReward: abi.NewTokenAmount(10), |
| 68 | + SimpleTotal: abi.NewTokenAmount(11), |
| 69 | + BaselineTotal: abi.NewTokenAmount(12), |
| 70 | + } |
| 71 | + inHead, err := store.Put(ctx, &inState) |
| 72 | + req.NoError(err) |
| 73 | + |
| 74 | + activationEpoch := abi.ChainEpoch(100) |
| 75 | + config := validRewardMigrationConfig(t, activationEpoch) |
| 76 | + outCodeCID := cid.MustParse("bafy2bzaca4aaaaaaaaaqk") |
| 77 | + migrator, err := newRewardMigrator(config, activationEpoch, outCodeCID) |
| 78 | + req.NoError(err) |
| 79 | + result, err := migrator.MigrateState(ctx, store, migration.ActorMigrationInput{Address: address.TestAddress, Head: inHead}) |
| 80 | + req.NoError(err) |
| 81 | + req.Equal(outCodeCID, result.NewCodeCID) |
| 82 | + |
| 83 | + var outState reward19.State |
| 84 | + req.NoError(store.Get(ctx, result.NewHead, &outState)) |
| 85 | + req.Equal(inState.CumsumBaseline, outState.CumsumBaseline) |
| 86 | + req.Equal(inState.CumsumRealized, outState.CumsumRealized) |
| 87 | + req.Equal(inState.EffectiveNetworkTime, outState.EffectiveNetworkTime) |
| 88 | + req.Equal(inState.EffectiveBaselinePower, outState.EffectiveBaselinePower) |
| 89 | + req.Equal(inState.ThisEpochReward, outState.ThisEpochReward) |
| 90 | + req.Equal(inState.ThisEpochRewardSmoothed.PositionEstimate, outState.ThisEpochRewardSmoothed.PositionEstimate) |
| 91 | + req.Equal(inState.ThisEpochRewardSmoothed.VelocityEstimate, outState.ThisEpochRewardSmoothed.VelocityEstimate) |
| 92 | + req.Equal(inState.ThisEpochBaselinePower, outState.ThisEpochBaselinePower) |
| 93 | + req.Equal(inState.Epoch, outState.Epoch) |
| 94 | + req.Equal(inState.TotalStoragePowerReward, outState.TotalMintedReward) |
| 95 | + req.Equal(big.Zero(), outState.TotalBurnMinted) |
| 96 | + req.Equal(big.Zero(), outState.TotalExplicitMinted) |
| 97 | + req.Equal([]reward19.StreamAccrual{{ID: 2, Amount: big.Zero()}}, outState.Accrued) |
| 98 | + req.Equal(config.SWATimelockEpochs, outState.SWATimelockEpochs) |
| 99 | + req.Equal(config.SWAActor, outState.SWAActor) |
| 100 | + |
| 101 | + var streams reward19.StreamsState |
| 102 | + req.NoError(store.Get(ctx, outState.StreamsRoot, &streams)) |
| 103 | + req.Len(streams.Streams, 2) |
| 104 | + req.Equal(reward19.StreamID(1), streams.Streams[0].ID) |
| 105 | + req.Nil(streams.Streams[0].Distribution) |
| 106 | + req.Equal(reward19.StreamID(2), streams.Streams[1].ID) |
| 107 | + req.Equal(config.Streams[1].Distribution.Writer, streams.Streams[1].Distribution.Writer) |
| 108 | + req.Equal(config.Streams[1].Distribution.Shares, streams.Streams[1].Distribution.Shares) |
| 109 | + req.Empty(streams.Tombstones) |
| 110 | + req.Empty(streams.PendingWrites) |
| 111 | +} |
| 112 | + |
| 113 | +func TestNewRewardMigratorRejectsInvalidConfig(t *testing.T) { |
| 114 | + activationEpoch := abi.ChainEpoch(100) |
| 115 | + outCodeCID := cid.MustParse("bafy2bzaca4aaaaaaaaaqk") |
| 116 | + testCases := []struct { |
| 117 | + name string |
| 118 | + mutate func(*RewardMigrationConfig) |
| 119 | + expected string |
| 120 | + }{ |
| 121 | + { |
| 122 | + name: "activation epoch mismatch", |
| 123 | + mutate: func(config *RewardMigrationConfig) { |
| 124 | + config.Streams[0].ActivationEpoch++ |
| 125 | + }, |
| 126 | + expected: "activation epoch 101 does not match upgrade epoch 100", |
| 127 | + }, |
| 128 | + { |
| 129 | + name: "weight start mismatch", |
| 130 | + mutate: func(config *RewardMigrationConfig) { |
| 131 | + config.Streams[1].Weight.TStart++ |
| 132 | + }, |
| 133 | + expected: "weight start 101 does not match upgrade epoch 100", |
| 134 | + }, |
| 135 | + { |
| 136 | + name: "wrong stream count", |
| 137 | + mutate: func(config *RewardMigrationConfig) { |
| 138 | + config.Streams = config.Streams[:1] |
| 139 | + }, |
| 140 | + expected: "requires exactly two streams", |
| 141 | + }, |
| 142 | + { |
| 143 | + name: "wrong bootstrap weight", |
| 144 | + mutate: func(config *RewardMigrationConfig) { |
| 145 | + config.Streams[0].Weight.VStart-- |
| 146 | + }, |
| 147 | + expected: "consensus bootstrap weight is invalid", |
| 148 | + }, |
| 149 | + { |
| 150 | + name: "unequal slopes", |
| 151 | + mutate: func(config *RewardMigrationConfig) { |
| 152 | + config.Streams[1].Weight.Slope++ |
| 153 | + }, |
| 154 | + expected: "bootstrap weight slopes are invalid", |
| 155 | + }, |
| 156 | + { |
| 157 | + name: "invalid initial distribution", |
| 158 | + mutate: func(config *RewardMigrationConfig) { |
| 159 | + config.Streams[1].Distribution.Shares[0].Share-- |
| 160 | + }, |
| 161 | + expected: "one full-share recipient", |
| 162 | + }, |
| 163 | + { |
| 164 | + name: "negative timelock", |
| 165 | + mutate: func(config *RewardMigrationConfig) { |
| 166 | + config.SWATimelockEpochs = -1 |
| 167 | + }, |
| 168 | + expected: "SWA timelock is negative", |
| 169 | + }, |
| 170 | + { |
| 171 | + name: "non-ID SWA actor", |
| 172 | + mutate: func(config *RewardMigrationConfig) { |
| 173 | + addr, err := address.NewDelegatedAddress(10, []byte{1}) |
| 174 | + require.NoError(t, err) |
| 175 | + config.SWAActor = addr |
| 176 | + }, |
| 177 | + expected: "SWA actor is not an ID address", |
| 178 | + }, |
| 179 | + } |
| 180 | + |
| 181 | + for _, tc := range testCases { |
| 182 | + t.Run(tc.name, func(t *testing.T) { |
| 183 | + config := validRewardMigrationConfig(t, activationEpoch) |
| 184 | + tc.mutate(&config) |
| 185 | + _, err := newRewardMigrator(config, activationEpoch, outCodeCID) |
| 186 | + require.ErrorContains(t, err, tc.expected) |
| 187 | + }) |
| 188 | + } |
| 189 | +} |
0 commit comments