0g-chain/x/issuance/legacy/v0_16/migrate_test.go
0xsatoshi 9b4a1e6b32 fix
2024-05-04 14:26:54 +08:00

178 lines
4.1 KiB
Go

package v0_16
import (
"testing"
"time"
sdkmath "cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/suite"
app "github.com/0glabs/0g-chain/app"
"github.com/0glabs/0g-chain/chaincfg"
v015issuance "github.com/0glabs/0g-chain/x/issuance/legacy/v0_15"
v016issuance "github.com/0glabs/0g-chain/x/issuance/types"
)
type migrateTestSuite struct {
suite.Suite
addresses []sdk.AccAddress
v15genstate v015issuance.GenesisState
cdc codec.Codec
legacyCdc *codec.LegacyAmino
}
func (s *migrateTestSuite) SetupTest() {
chaincfg.SetSDKConfig()
s.v15genstate = v015issuance.GenesisState{
Params: v015issuance.Params{},
Supplies: v015issuance.AssetSupplies{},
}
config := app.MakeEncodingConfig()
s.cdc = config.Marshaler
legacyCodec := codec.NewLegacyAmino()
s.legacyCdc = legacyCodec
_, accAddresses := app.GeneratePrivKeyAddressPairs(10)
s.addresses = accAddresses
}
func (s *migrateTestSuite) TestMigrate_JSON() {
// Migrate v15 issuance to v16
data := `{
"params": {
"assets": [
{
"blockable": true,
"blocked_addresses": null,
"denom": "hbtc",
"owner": "0g1ffv7nhd3z6sych2qpqkk03ec6hzkmufyhp5hf8",
"paused": false,
"rate_limit": {
"active": false,
"limit": "0",
"time_period": "0"
}
}
]
},
"supplies": [
{
"current_supply": { "denom": "ua0gi", "amount": "100" },
"time_elapsed": "3600000000000"
},
{
"current_supply": { "denom": "bnb", "amount": "300" },
"time_elapsed": "300000000000"
}
]
}`
err := s.legacyCdc.UnmarshalJSON([]byte(data), &s.v15genstate)
s.Require().NoError(err)
genstate := Migrate(s.v15genstate)
// Compare expect v16 issuance json with migrated json
expected := `{
"params": {
"assets": [
{
"blockable": true,
"blocked_addresses": [],
"denom": "hbtc",
"owner": "0g1ffv7nhd3z6sych2qpqkk03ec6hzkmufyhp5hf8",
"paused": false,
"rate_limit": {
"active": false,
"limit": "0",
"time_period": "0s"
}
}
]
},
"supplies": [
{
"current_supply": { "denom": "ua0gi", "amount": "100" },
"time_elapsed": "3600s"
},
{
"current_supply": { "denom": "bnb", "amount": "300" },
"time_elapsed": "300s"
}
]
}`
actual := s.cdc.MustMarshalJSON(genstate)
s.Require().NoError(err)
s.Require().JSONEq(expected, string(actual))
}
func (s *migrateTestSuite) TestMigrate_Params() {
s.v15genstate.Params = v015issuance.Params{
Assets: v015issuance.Assets{
{
Owner: s.addresses[0],
Denom: "ua0gi",
BlockedAddresses: s.addresses[1:2],
Paused: true,
Blockable: true,
RateLimit: v015issuance.RateLimit{
Active: true,
Limit: sdkmath.NewInt(10),
TimePeriod: 1 * time.Hour,
},
},
},
}
expectedParams := v016issuance.Params{
Assets: []v016issuance.Asset{
{
Owner: s.addresses[0].String(),
Denom: "ua0gi",
BlockedAddresses: []string{s.addresses[1].String()},
Paused: true,
Blockable: true,
RateLimit: v016issuance.RateLimit{
Active: true,
Limit: sdkmath.NewInt(10),
TimePeriod: 1 * time.Hour,
},
},
},
}
genState := Migrate(s.v15genstate)
s.Require().Equal(expectedParams, genState.Params)
}
func (s *migrateTestSuite) TestMigrate_Supplies() {
s.v15genstate.Supplies = v015issuance.AssetSupplies{
{
CurrentSupply: sdk.NewCoin("ua0gi", sdkmath.NewInt(100)),
TimeElapsed: time.Duration(1 * time.Hour),
},
{
CurrentSupply: sdk.NewCoin("bnb", sdkmath.NewInt(300)),
TimeElapsed: time.Duration(5 * time.Minute),
},
}
expected := []v016issuance.AssetSupply{
{
CurrentSupply: sdk.NewCoin("ua0gi", sdkmath.NewInt(100)),
TimeElapsed: time.Duration(1 * time.Hour),
},
{
CurrentSupply: sdk.NewCoin("bnb", sdkmath.NewInt(300)),
TimeElapsed: time.Duration(5 * time.Minute),
},
}
genState := Migrate(s.v15genstate)
s.Require().Equal(expected, genState.Supplies)
}
func TestIssuanceMigrateTestSuite(t *testing.T) {
suite.Run(t, new(migrateTestSuite))
}