|
| 1 | +package retiredvesting_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/sha256" |
| 5 | + "encoding/hex" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | + "google.golang.org/protobuf/encoding/protowire" |
| 11 | + |
| 12 | + seiapp "github.com/sei-protocol/sei-chain/app" |
| 13 | + appparams "github.com/sei-protocol/sei-chain/app/params" |
| 14 | + "github.com/sei-protocol/sei-chain/app/retiredvesting" |
| 15 | + codectypes "github.com/sei-protocol/sei-chain/sei-cosmos/codec/types" |
| 16 | + sdk "github.com/sei-protocol/sei-chain/sei-cosmos/types" |
| 17 | + sdkerrors "github.com/sei-protocol/sei-chain/sei-cosmos/types/errors" |
| 18 | + txtypes "github.com/sei-protocol/sei-chain/sei-cosmos/types/tx" |
| 19 | + authtestutil "github.com/sei-protocol/sei-chain/sei-cosmos/x/auth/testutil" |
| 20 | + authtypes "github.com/sei-protocol/sei-chain/sei-cosmos/x/auth/types" |
| 21 | +) |
| 22 | + |
| 23 | +const ( |
| 24 | + msgCreateVestingAccountTypeURL = "/cosmos.vesting.v1beta1.MsgCreateVestingAccount" |
| 25 | + // vestingEndTime is 3000-01-01, the end time of every vesting account on pacific-1. |
| 26 | + vestingEndTime int64 = 32_503_680_000 |
| 27 | + |
| 28 | + // pacific1VestingTxHex is the pacific-1 transaction pacific1VestingTxHash, |
| 29 | + // included at height 228,422,853, which created a delayed vesting account. |
| 30 | + pacific1VestingTxHash = "2838170E42486FE03E4765DDD0C8BC3559AADB1F09B2882C66C45B5906940709" |
| 31 | + pacific1VestingTxHex = "0aa6010aa3010a2f2f636f736d6f732e76657374696e672e763162657461312e4d736743726561746556657374696e674163636f756e7412700a2a736569316d366d30323634303930653761666e7030716b32616d766565306339363434637437756c6836122a7365693167613833336d6a366a633664326c7965366a3834737475673968326d7078637875797735616b1a0e0a04757365691206313030303030208098fb8a79280112650a4e0a460a1f2f636f736d6f732e63727970746f2e736563703235366b312e5075624b657912230a2102b4f18f4ea5bc446a46d82d42d0abfe9b1fa4397b037cf7da5ca07dd1c967ace612040a02080112130a0d0a04757365691205333030303010e0a7121a409963a065d4c35e70639703a1d086dfe399ebe8df345fd583ad388055c4d5dba22af9a57c29e026e014d436a67e097ec7aff741cfe9c5ccf7d7fc91a0774187ec" |
| 32 | +) |
| 33 | + |
| 34 | +var appEncodings = []struct { |
| 35 | + name string |
| 36 | + config func() appparams.EncodingConfig |
| 37 | +}{ |
| 38 | + {"app", seiapp.MakeEncodingConfig}, |
| 39 | + {"legacy app", seiapp.MakeLegacyEncodingConfig}, |
| 40 | +} |
| 41 | + |
| 42 | +// msgCreateVestingAccountWithEveryField returns a MsgCreateVestingAccount with |
| 43 | +// every field set, including the admin the pacific-1 fixture leaves unset, and |
| 44 | +// its encoding under cosmos/vesting/v1beta1/tx.proto written field by field |
| 45 | +// rather than by the retired type. |
| 46 | +func msgCreateVestingAccountWithEveryField(t *testing.T) (*retiredvesting.MsgCreateVestingAccount, []byte) { |
| 47 | + t.Helper() |
| 48 | + msg := &retiredvesting.MsgCreateVestingAccount{ |
| 49 | + FromAddress: sdk.AccAddress("vesting-from-address").String(), |
| 50 | + ToAddress: sdk.AccAddress("vesting-to---address").String(), |
| 51 | + Amount: sdk.NewCoins(sdk.NewInt64Coin("usei", 100_000)), |
| 52 | + EndTime: vestingEndTime, |
| 53 | + Delayed: true, |
| 54 | + Admin: sdk.AccAddress("vesting-admin-addres").String(), |
| 55 | + } |
| 56 | + coin, err := msg.Amount[0].Marshal() |
| 57 | + require.NoError(t, err) |
| 58 | + encoded := protowire.AppendString(protowire.AppendTag(nil, 1, protowire.BytesType), msg.FromAddress) |
| 59 | + encoded = protowire.AppendString(protowire.AppendTag(encoded, 2, protowire.BytesType), msg.ToAddress) |
| 60 | + encoded = protowire.AppendBytes(protowire.AppendTag(encoded, 3, protowire.BytesType), coin) |
| 61 | + encoded = protowire.AppendVarint(protowire.AppendTag(encoded, 4, protowire.VarintType), uint64(vestingEndTime)) |
| 62 | + encoded = protowire.AppendVarint(protowire.AppendTag(encoded, 5, protowire.VarintType), 1) |
| 63 | + encoded = protowire.AppendString(protowire.AppendTag(encoded, 6, protowire.BytesType), msg.Admin) |
| 64 | + return msg, encoded |
| 65 | +} |
| 66 | + |
| 67 | +// txCarrying encodes a transaction whose only message is the |
| 68 | +// MsgCreateVestingAccount encoding msg, with a placeholder signature. |
| 69 | +func txCarrying(t *testing.T, msg []byte) []byte { |
| 70 | + t.Helper() |
| 71 | + body, err := (&txtypes.TxBody{ |
| 72 | + Messages: []*codectypes.Any{{TypeUrl: msgCreateVestingAccountTypeURL, Value: msg}}, |
| 73 | + }).Marshal() |
| 74 | + require.NoError(t, err) |
| 75 | + authInfo, err := (&txtypes.AuthInfo{ |
| 76 | + Fee: &txtypes.Fee{Amount: sdk.NewCoins(sdk.NewInt64Coin("usei", 200_000)), GasLimit: 1_000_000}, |
| 77 | + }).Marshal() |
| 78 | + require.NoError(t, err) |
| 79 | + txBytes, err := (&txtypes.TxRaw{ |
| 80 | + BodyBytes: body, |
| 81 | + AuthInfoBytes: authInfo, |
| 82 | + Signatures: [][]byte{make([]byte, 64)}, |
| 83 | + }).Marshal() |
| 84 | + require.NoError(t, err) |
| 85 | + return txBytes |
| 86 | +} |
| 87 | + |
| 88 | +// decodeMsgCreateVestingAccount decodes txBytes with the transaction config of |
| 89 | +// config and returns its only message, which must be a MsgCreateVestingAccount. |
| 90 | +func decodeMsgCreateVestingAccount(t *testing.T, config func() appparams.EncodingConfig, txBytes []byte) *retiredvesting.MsgCreateVestingAccount { |
| 91 | + t.Helper() |
| 92 | + tx, err := config().TxConfig.TxDecoder()(txBytes) |
| 93 | + require.NoError(t, err) |
| 94 | + require.Len(t, tx.GetMsgs(), 1) |
| 95 | + msg, ok := tx.GetMsgs()[0].(*retiredvesting.MsgCreateVestingAccount) |
| 96 | + require.True(t, ok, "decoded %T", tx.GetMsgs()[0]) |
| 97 | + return msg |
| 98 | +} |
| 99 | + |
| 100 | +// TestPacific1MsgCreateVestingAccountDecodes decodes a MsgCreateVestingAccount |
| 101 | +// transaction pacific-1 included through both app transaction configs, and |
| 102 | +// requires the retired type to carry the fields it set and encode the message |
| 103 | +// back to the bytes the chain included. |
| 104 | +func TestPacific1MsgCreateVestingAccountDecodes(t *testing.T) { |
| 105 | + txBytes, err := hex.DecodeString(pacific1VestingTxHex) |
| 106 | + require.NoError(t, err) |
| 107 | + digest := sha256.Sum256(txBytes) |
| 108 | + require.Equal(t, pacific1VestingTxHash, strings.ToUpper(hex.EncodeToString(digest[:]))) |
| 109 | + var raw txtypes.TxRaw |
| 110 | + require.NoError(t, raw.Unmarshal(txBytes)) |
| 111 | + var body txtypes.TxBody |
| 112 | + require.NoError(t, body.Unmarshal(raw.BodyBytes)) |
| 113 | + require.Len(t, body.Messages, 1) |
| 114 | + |
| 115 | + want := &retiredvesting.MsgCreateVestingAccount{ |
| 116 | + FromAddress: "sei1m6m0264090e7afnp0qk2amvee0c9644ct7ulh6", |
| 117 | + ToAddress: "sei1ga833mj6jc6d2lye6j84stug9h2mpxcxuyw5ak", |
| 118 | + Amount: sdk.NewCoins(sdk.NewInt64Coin("usei", 100_000)), |
| 119 | + EndTime: vestingEndTime, |
| 120 | + Delayed: true, |
| 121 | + } |
| 122 | + for _, encoding := range appEncodings { |
| 123 | + t.Run(encoding.name, func(t *testing.T) { |
| 124 | + got := decodeMsgCreateVestingAccount(t, encoding.config, txBytes) |
| 125 | + require.True(t, want.Equal(got), "decoded %v", got) |
| 126 | + reencoded, err := got.Marshal() |
| 127 | + require.NoError(t, err) |
| 128 | + require.Equal(t, body.Messages[0].Value, reencoded) |
| 129 | + }) |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +// TestMsgCreateVestingAccountWithEveryFieldDecodes decodes a transaction |
| 134 | +// carrying a MsgCreateVestingAccount with every field set through both app |
| 135 | +// transaction configs, and requires the retired type to carry every field and |
| 136 | +// encode it back byte for byte. |
| 137 | +func TestMsgCreateVestingAccountWithEveryFieldDecodes(t *testing.T) { |
| 138 | + want, encoded := msgCreateVestingAccountWithEveryField(t) |
| 139 | + txBytes := txCarrying(t, encoded) |
| 140 | + for _, encoding := range appEncodings { |
| 141 | + t.Run(encoding.name, func(t *testing.T) { |
| 142 | + got := decodeMsgCreateVestingAccount(t, encoding.config, txBytes) |
| 143 | + require.True(t, want.Equal(got), "decoded %v", got) |
| 144 | + reencoded, err := got.Marshal() |
| 145 | + require.NoError(t, err) |
| 146 | + require.Equal(t, encoded, reencoded) |
| 147 | + }) |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +// TestRetiredMsgCreateVestingAccountIsRefused requires the retired message to |
| 152 | +// keep its type URL and signer, and to fail validation in codespace vesting |
| 153 | +// with code 2. |
| 154 | +func TestRetiredMsgCreateVestingAccountIsRefused(t *testing.T) { |
| 155 | + msg, _ := msgCreateVestingAccountWithEveryField(t) |
| 156 | + require.Equal(t, msgCreateVestingAccountTypeURL, sdk.MsgTypeURL(msg)) |
| 157 | + require.Equal(t, []sdk.AccAddress{sdk.AccAddress("vesting-from-address")}, msg.GetSigners()) |
| 158 | + |
| 159 | + err := msg.ValidateBasic() |
| 160 | + require.ErrorIs(t, err, retiredvesting.ErrDeprecated) |
| 161 | + codespace, code, _ := sdkerrors.ABCIInfo(err, false) |
| 162 | + require.Equal(t, "vesting", codespace) |
| 163 | + require.EqualValues(t, 2, code) |
| 164 | +} |
| 165 | + |
| 166 | +// TestRetiredVestingDoesNotRestoreAccountsOrModule requires the app codec to |
| 167 | +// keep refusing every vesting account type, and the vesting module to stay |
| 168 | +// unregistered. |
| 169 | +func TestRetiredVestingDoesNotRestoreAccountsOrModule(t *testing.T) { |
| 170 | + registry := seiapp.MakeEncodingConfig().InterfaceRegistry |
| 171 | + for _, typeURL := range authtestutil.LegacyVestingAccountTypeURLs { |
| 172 | + var account authtypes.AccountI |
| 173 | + err := registry.UnpackAny(&codectypes.Any{TypeUrl: typeURL}, &account) |
| 174 | + require.ErrorContains(t, err, "no concrete type registered", typeURL) |
| 175 | + } |
| 176 | + _, registered := seiapp.ModuleBasics[retiredvesting.ModuleName] |
| 177 | + require.False(t, registered) |
| 178 | +} |
0 commit comments