|
| 1 | +package power |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/hex" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/filecoin-project/go-state-types/abi" |
| 9 | + "github.com/filecoin-project/go-state-types/big" |
| 10 | + "github.com/filecoin-project/go-state-types/builtin/v15/util/smoothing" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | +) |
| 13 | + |
| 14 | +// Test to match with Rust fil_actor_power::serialization |
| 15 | +func TestSerializationCurrentTotalPowerReturn(t *testing.T) { |
| 16 | + testCases := []struct { |
| 17 | + params CurrentTotalPowerReturn |
| 18 | + hex string |
| 19 | + }{ |
| 20 | + { |
| 21 | + params: CurrentTotalPowerReturn{ |
| 22 | + RawBytePower: abi.NewStoragePower(0), |
| 23 | + QualityAdjPower: abi.NewStoragePower(0), |
| 24 | + PledgeCollateral: abi.NewTokenAmount(0), |
| 25 | + QualityAdjPowerSmoothed: smoothing.NewEstimate(big.Zero(), big.Zero()), |
| 26 | + RampStartEpoch: 0, |
| 27 | + RampDurationEpochs: 0, |
| 28 | + }, |
| 29 | + // [byte[],byte[],byte[],[byte[],byte[]],0,0] |
| 30 | + hex: "864040408240400000", |
| 31 | + }, |
| 32 | + { |
| 33 | + params: CurrentTotalPowerReturn{ |
| 34 | + RawBytePower: abi.NewStoragePower(1 << 20), |
| 35 | + QualityAdjPower: abi.NewStoragePower(1 << 21), |
| 36 | + PledgeCollateral: abi.NewTokenAmount(1 << 22), |
| 37 | + QualityAdjPowerSmoothed: smoothing.NewEstimate(big.NewInt(1<<23), big.NewInt(1<<24)), |
| 38 | + RampStartEpoch: 25, |
| 39 | + RampDurationEpochs: 26, |
| 40 | + }, |
| 41 | + // FilterEstimate BigInts have a precision shift of 128, so they end up larger than the others. |
| 42 | + // [byte[00100000],byte[00200000],byte[00400000],[byte[0080000000000000000000000000000000000000],byte[000100000000000000000000000000000000000000]],25,26] |
| 43 | + hex: "8644001000004400200000440040000082540080000000000000000000000000000000000000550001000000000000000000000000000000000000001819181a", |
| 44 | + }, |
| 45 | + } |
| 46 | + |
| 47 | + for _, tc := range testCases { |
| 48 | + t.Run("", func(t *testing.T) { |
| 49 | + req := require.New(t) |
| 50 | + |
| 51 | + var buf bytes.Buffer |
| 52 | + req.NoError(tc.params.MarshalCBOR(&buf)) |
| 53 | + req.Equal(tc.hex, hex.EncodeToString(buf.Bytes())) |
| 54 | + var rt CurrentTotalPowerReturn |
| 55 | + req.NoError(rt.UnmarshalCBOR(&buf)) |
| 56 | + req.Equal(tc.params, rt) |
| 57 | + }) |
| 58 | + } |
| 59 | +} |
0 commit comments