mirror of
https://github.com/0glabs/0g-chain.git
synced 2024-11-10 10:05:18 +00:00
Savings genesis state (#1198)
* add deposits to genesis state * import/export genesis with deposits * add helper keeper method + update tests
This commit is contained in:
parent
70d431b5c8
commit
003b040458
@ -10,4 +10,6 @@ option go_package = "github.com/kava-labs/kava/x/savings/types";
|
|||||||
message GenesisState {
|
message GenesisState {
|
||||||
// params defines all the parameters of the module.
|
// params defines all the parameters of the module.
|
||||||
Params params = 1 [(gogoproto.nullable) = false];
|
Params params = 1 [(gogoproto.nullable) = false];
|
||||||
|
|
||||||
|
repeated Deposit deposits = 2 [(gogoproto.castrepeated) = "Deposits", (gogoproto.nullable) = false];
|
||||||
}
|
}
|
||||||
|
@ -11,8 +11,16 @@ import (
|
|||||||
|
|
||||||
// InitGenesis initializes genesis state
|
// InitGenesis initializes genesis state
|
||||||
func InitGenesis(ctx sdk.Context, k keeper.Keeper, ak types.AccountKeeper, gs types.GenesisState) {
|
func InitGenesis(ctx sdk.Context, k keeper.Keeper, ak types.AccountKeeper, gs types.GenesisState) {
|
||||||
|
if err := gs.Validate(); err != nil {
|
||||||
|
panic(fmt.Sprintf("failed to validate %s genesis state: %s", types.ModuleName, err))
|
||||||
|
}
|
||||||
|
|
||||||
k.SetParams(ctx, gs.Params)
|
k.SetParams(ctx, gs.Params)
|
||||||
|
|
||||||
|
for _, deposit := range gs.Deposits {
|
||||||
|
k.SetDeposit(ctx, deposit)
|
||||||
|
}
|
||||||
|
|
||||||
// check if the module account exists
|
// check if the module account exists
|
||||||
SavingsModuleAccount := ak.GetModuleAccount(ctx, types.ModuleAccountName)
|
SavingsModuleAccount := ak.GetModuleAccount(ctx, types.ModuleAccountName)
|
||||||
if SavingsModuleAccount == nil {
|
if SavingsModuleAccount == nil {
|
||||||
@ -23,5 +31,6 @@ func InitGenesis(ctx sdk.Context, k keeper.Keeper, ak types.AccountKeeper, gs ty
|
|||||||
// ExportGenesis returns a GenesisState for a given context and keeper
|
// ExportGenesis returns a GenesisState for a given context and keeper
|
||||||
func ExportGenesis(ctx sdk.Context, k keeper.Keeper) types.GenesisState {
|
func ExportGenesis(ctx sdk.Context, k keeper.Keeper) types.GenesisState {
|
||||||
params := k.GetParams(ctx)
|
params := k.GetParams(ctx)
|
||||||
return types.NewGenesisState(params)
|
deposits := k.GetAllDeposits(ctx)
|
||||||
|
return types.NewGenesisState(params, deposits)
|
||||||
}
|
}
|
||||||
|
@ -11,6 +11,7 @@ import (
|
|||||||
tmtime "github.com/tendermint/tendermint/types/time"
|
tmtime "github.com/tendermint/tendermint/types/time"
|
||||||
|
|
||||||
"github.com/kava-labs/kava/app"
|
"github.com/kava-labs/kava/app"
|
||||||
|
"github.com/kava-labs/kava/x/savings"
|
||||||
"github.com/kava-labs/kava/x/savings/keeper"
|
"github.com/kava-labs/kava/x/savings/keeper"
|
||||||
"github.com/kava-labs/kava/x/savings/types"
|
"github.com/kava-labs/kava/x/savings/types"
|
||||||
)
|
)
|
||||||
@ -36,11 +37,19 @@ func (suite *GenesisTestSuite) SetupTest() {
|
|||||||
suite.addrs = addrs
|
suite.addrs = addrs
|
||||||
}
|
}
|
||||||
|
|
||||||
func (suite *GenesisTestSuite) TestInitGenesis() {
|
func (suite *GenesisTestSuite) TestInitExportGenesis() {
|
||||||
params := types.NewParams(
|
params := types.NewParams(
|
||||||
[]string{"btc", "ukava", "bnb"},
|
[]string{"btc", "ukava", "bnb"},
|
||||||
)
|
)
|
||||||
savingsGenesis := types.NewGenesisState(params)
|
|
||||||
|
deposits := types.Deposits{
|
||||||
|
types.NewDeposit(
|
||||||
|
suite.addrs[0],
|
||||||
|
sdk.NewCoins(sdk.NewCoin("ukava", sdk.NewInt(1e8))), // 100 ukava
|
||||||
|
),
|
||||||
|
}
|
||||||
|
|
||||||
|
savingsGenesis := types.NewGenesisState(params, deposits)
|
||||||
|
|
||||||
suite.NotPanics(
|
suite.NotPanics(
|
||||||
func() {
|
func() {
|
||||||
@ -50,6 +59,12 @@ func (suite *GenesisTestSuite) TestInitGenesis() {
|
|||||||
)
|
)
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
expectedDeposits := suite.keeper.GetAllDeposits(suite.ctx)
|
||||||
|
expectedGenesis := savingsGenesis
|
||||||
|
expectedGenesis.Deposits = expectedDeposits
|
||||||
|
exportedGenesis := savings.ExportGenesis(suite.ctx, suite.keeper)
|
||||||
|
suite.Equal(expectedGenesis, exportedGenesis)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGenesisTestSuite(t *testing.T) {
|
func TestGenesisTestSuite(t *testing.T) {
|
||||||
|
@ -114,7 +114,10 @@ func (suite *KeeperTestSuite) TestDeposit() {
|
|||||||
[]sdk.Coins{tc.args.initialDepositorBalance},
|
[]sdk.Coins{tc.args.initialDepositorBalance},
|
||||||
[]sdk.AccAddress{tc.args.depositor},
|
[]sdk.AccAddress{tc.args.depositor},
|
||||||
)
|
)
|
||||||
savingsGS := types.NewGenesisState(types.NewParams(tc.args.allowedDenoms))
|
savingsGS := types.NewGenesisState(
|
||||||
|
types.NewParams(tc.args.allowedDenoms),
|
||||||
|
types.Deposits{},
|
||||||
|
)
|
||||||
|
|
||||||
tApp.InitializeFromGenesisStates(authGS,
|
tApp.InitializeFromGenesisStates(authGS,
|
||||||
app.GenesisState{types.ModuleName: tApp.AppCodec().MustMarshalJSON(&savingsGS)},
|
app.GenesisState{types.ModuleName: tApp.AppCodec().MustMarshalJSON(&savingsGS)},
|
||||||
|
@ -78,6 +78,15 @@ func (k Keeper) IterateDeposits(ctx sdk.Context, cb func(deposit types.Deposit)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetAllDeposits returns all Deposits from the store
|
||||||
|
func (k Keeper) GetAllDeposits(ctx sdk.Context) (deposits types.Deposits) {
|
||||||
|
k.IterateDeposits(ctx, func(deposit types.Deposit) bool {
|
||||||
|
deposits = append(deposits, deposit)
|
||||||
|
return false
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// Logger returns a module-specific logger.
|
// Logger returns a module-specific logger.
|
||||||
func (k Keeper) Logger(ctx sdk.Context) log.Logger {
|
func (k Keeper) Logger(ctx sdk.Context) log.Logger {
|
||||||
return ctx.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName))
|
return ctx.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName))
|
||||||
|
@ -117,7 +117,10 @@ func (suite *KeeperTestSuite) TestWithdraw() {
|
|||||||
[]sdk.Coins{tc.args.initialDepositorBalance},
|
[]sdk.Coins{tc.args.initialDepositorBalance},
|
||||||
[]sdk.AccAddress{tc.args.depositor},
|
[]sdk.AccAddress{tc.args.depositor},
|
||||||
)
|
)
|
||||||
savingsGS := types.NewGenesisState(types.NewParams(tc.args.allowedDenoms))
|
savingsGS := types.NewGenesisState(
|
||||||
|
types.NewParams(tc.args.allowedDenoms),
|
||||||
|
types.Deposits{},
|
||||||
|
)
|
||||||
|
|
||||||
tApp.InitializeFromGenesisStates(authGS,
|
tApp.InitializeFromGenesisStates(authGS,
|
||||||
app.GenesisState{types.ModuleName: tApp.AppCodec().MustMarshalJSON(&savingsGS)},
|
app.GenesisState{types.ModuleName: tApp.AppCodec().MustMarshalJSON(&savingsGS)},
|
||||||
|
@ -1,9 +1,10 @@
|
|||||||
package types
|
package types
|
||||||
|
|
||||||
// NewGenesisState creates a new genesis state for the savings module
|
// NewGenesisState creates a new genesis state for the savings module
|
||||||
func NewGenesisState(p Params) GenesisState {
|
func NewGenesisState(p Params, deposits Deposits) GenesisState {
|
||||||
return GenesisState{
|
return GenesisState{
|
||||||
Params: p,
|
Params: p,
|
||||||
|
Deposits: deposits,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -11,11 +12,17 @@ func NewGenesisState(p Params) GenesisState {
|
|||||||
func DefaultGenesisState() GenesisState {
|
func DefaultGenesisState() GenesisState {
|
||||||
return NewGenesisState(
|
return NewGenesisState(
|
||||||
DefaultParams(),
|
DefaultParams(),
|
||||||
|
Deposits{},
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate performs basic validation of genesis data returning an
|
// Validate performs basic validation of genesis data returning an
|
||||||
// error for any failed validation criteria.
|
// error for any failed validation criteria.
|
||||||
func (gs GenesisState) Validate() error {
|
func (gs GenesisState) Validate() error {
|
||||||
return gs.Params.Validate()
|
|
||||||
|
if err := gs.Params.Validate(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return gs.Deposits.Validate()
|
||||||
}
|
}
|
||||||
|
@ -26,7 +26,8 @@ const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package
|
|||||||
// GenesisState defines the savings module's genesis state.
|
// GenesisState defines the savings module's genesis state.
|
||||||
type GenesisState struct {
|
type GenesisState struct {
|
||||||
// params defines all the parameters of the module.
|
// params defines all the parameters of the module.
|
||||||
Params Params `protobuf:"bytes,1,opt,name=params,proto3" json:"params"`
|
Params Params `protobuf:"bytes,1,opt,name=params,proto3" json:"params"`
|
||||||
|
Deposits Deposits `protobuf:"bytes,2,rep,name=deposits,proto3,castrepeated=Deposits" json:"deposits"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *GenesisState) Reset() { *m = GenesisState{} }
|
func (m *GenesisState) Reset() { *m = GenesisState{} }
|
||||||
@ -69,6 +70,13 @@ func (m *GenesisState) GetParams() Params {
|
|||||||
return Params{}
|
return Params{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m *GenesisState) GetDeposits() Deposits {
|
||||||
|
if m != nil {
|
||||||
|
return m.Deposits
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
proto.RegisterType((*GenesisState)(nil), "kava.savings.v1beta1.GenesisState")
|
proto.RegisterType((*GenesisState)(nil), "kava.savings.v1beta1.GenesisState")
|
||||||
}
|
}
|
||||||
@ -78,20 +86,23 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var fileDescriptor_f5dcde4d417fcec8 = []byte{
|
var fileDescriptor_f5dcde4d417fcec8 = []byte{
|
||||||
// 204 bytes of a gzipped FileDescriptorProto
|
// 245 bytes of a gzipped FileDescriptorProto
|
||||||
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x52, 0xca, 0x4e, 0x2c, 0x4b,
|
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x52, 0xca, 0x4e, 0x2c, 0x4b,
|
||||||
0xd4, 0x2f, 0x4e, 0x2c, 0xcb, 0xcc, 0x4b, 0x2f, 0xd6, 0x2f, 0x33, 0x4c, 0x4a, 0x2d, 0x49, 0x34,
|
0xd4, 0x2f, 0x4e, 0x2c, 0xcb, 0xcc, 0x4b, 0x2f, 0xd6, 0x2f, 0x33, 0x4c, 0x4a, 0x2d, 0x49, 0x34,
|
||||||
0xd4, 0x4f, 0x4f, 0xcd, 0x4b, 0x2d, 0xce, 0x2c, 0xd6, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x12,
|
0xd4, 0x4f, 0x4f, 0xcd, 0x4b, 0x2d, 0xce, 0x2c, 0xd6, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x12,
|
||||||
0x01, 0xa9, 0xd1, 0x83, 0xaa, 0xd1, 0x83, 0xaa, 0x91, 0x52, 0xc0, 0xaa, 0xb3, 0xb8, 0x24, 0xbf,
|
0x01, 0xa9, 0xd1, 0x83, 0xaa, 0xd1, 0x83, 0xaa, 0x91, 0x52, 0xc0, 0xaa, 0xb3, 0xb8, 0x24, 0xbf,
|
||||||
0x28, 0x15, 0xa2, 0x4f, 0x4a, 0x24, 0x3d, 0x3f, 0x3d, 0x1f, 0xcc, 0xd4, 0x07, 0xb1, 0x20, 0xa2,
|
0x28, 0x15, 0xa2, 0x4f, 0x4a, 0x24, 0x3d, 0x3f, 0x3d, 0x1f, 0xcc, 0xd4, 0x07, 0xb1, 0x20, 0xa2,
|
||||||
0x4a, 0x5e, 0x5c, 0x3c, 0xee, 0x10, 0xe3, 0x83, 0x4b, 0x12, 0x4b, 0x52, 0x85, 0xac, 0xb8, 0xd8,
|
0x4a, 0xd3, 0x19, 0xb9, 0x78, 0xdc, 0x21, 0xe6, 0x07, 0x97, 0x24, 0x96, 0xa4, 0x0a, 0x59, 0x71,
|
||||||
0x0a, 0x12, 0x8b, 0x12, 0x73, 0x8b, 0x25, 0x18, 0x15, 0x18, 0x35, 0xb8, 0x8d, 0x64, 0xf4, 0xb0,
|
0xb1, 0x15, 0x24, 0x16, 0x25, 0xe6, 0x16, 0x4b, 0x30, 0x2a, 0x30, 0x6a, 0x70, 0x1b, 0xc9, 0xe8,
|
||||||
0x59, 0xa7, 0x17, 0x00, 0x56, 0xe3, 0xc4, 0x72, 0xe2, 0x9e, 0x3c, 0x43, 0x10, 0x54, 0x87, 0x93,
|
0x61, 0xb3, 0x4f, 0x2f, 0x00, 0xac, 0xc6, 0x89, 0xe5, 0xc4, 0x3d, 0x79, 0x86, 0x20, 0xa8, 0x0e,
|
||||||
0xf3, 0x89, 0x47, 0x72, 0x8c, 0x17, 0x1e, 0xc9, 0x31, 0x3e, 0x78, 0x24, 0xc7, 0x38, 0xe1, 0xb1,
|
0x21, 0x6f, 0x2e, 0x8e, 0x94, 0xd4, 0x82, 0xfc, 0xe2, 0xcc, 0x92, 0x62, 0x09, 0x26, 0x05, 0x66,
|
||||||
0x1c, 0xc3, 0x85, 0xc7, 0x72, 0x0c, 0x37, 0x1e, 0xcb, 0x31, 0x44, 0x69, 0xa6, 0x67, 0x96, 0x64,
|
0x0d, 0x6e, 0x23, 0x59, 0xec, 0xba, 0x5d, 0x20, 0xaa, 0x9c, 0x04, 0x40, 0xda, 0x57, 0xdd, 0x97,
|
||||||
0x94, 0x26, 0xe9, 0x25, 0xe7, 0xe7, 0xea, 0x83, 0xcc, 0xd3, 0xcd, 0x49, 0x4c, 0x2a, 0x06, 0xb3,
|
0xe7, 0x80, 0x0a, 0x14, 0x07, 0xc1, 0x0d, 0x70, 0x72, 0x3e, 0xf1, 0x48, 0x8e, 0xf1, 0xc2, 0x23,
|
||||||
0xf4, 0x2b, 0xe0, 0x8e, 0x2e, 0xa9, 0x2c, 0x48, 0x2d, 0x4e, 0x62, 0x03, 0xbb, 0xcb, 0x18, 0x10,
|
0x39, 0xc6, 0x07, 0x8f, 0xe4, 0x18, 0x27, 0x3c, 0x96, 0x63, 0xb8, 0xf0, 0x58, 0x8e, 0xe1, 0xc6,
|
||||||
0x00, 0x00, 0xff, 0xff, 0x5d, 0xc8, 0xba, 0x5d, 0x0b, 0x01, 0x00, 0x00,
|
0x63, 0x39, 0x86, 0x28, 0xcd, 0xf4, 0xcc, 0x92, 0x8c, 0xd2, 0x24, 0xbd, 0xe4, 0xfc, 0x5c, 0x7d,
|
||||||
|
0x90, 0xf1, 0xba, 0x39, 0x89, 0x49, 0xc5, 0x60, 0x96, 0x7e, 0x05, 0x3c, 0x08, 0x4a, 0x2a, 0x0b,
|
||||||
|
0x52, 0x8b, 0x93, 0xd8, 0xc0, 0xbe, 0x34, 0x06, 0x04, 0x00, 0x00, 0xff, 0xff, 0xef, 0xd9, 0x22,
|
||||||
|
0xbc, 0x59, 0x01, 0x00, 0x00,
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *GenesisState) Marshal() (dAtA []byte, err error) {
|
func (m *GenesisState) Marshal() (dAtA []byte, err error) {
|
||||||
@ -114,6 +125,20 @@ func (m *GenesisState) MarshalToSizedBuffer(dAtA []byte) (int, error) {
|
|||||||
_ = i
|
_ = i
|
||||||
var l int
|
var l int
|
||||||
_ = l
|
_ = l
|
||||||
|
if len(m.Deposits) > 0 {
|
||||||
|
for iNdEx := len(m.Deposits) - 1; iNdEx >= 0; iNdEx-- {
|
||||||
|
{
|
||||||
|
size, err := m.Deposits[iNdEx].MarshalToSizedBuffer(dAtA[:i])
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
i -= size
|
||||||
|
i = encodeVarintGenesis(dAtA, i, uint64(size))
|
||||||
|
}
|
||||||
|
i--
|
||||||
|
dAtA[i] = 0x12
|
||||||
|
}
|
||||||
|
}
|
||||||
{
|
{
|
||||||
size, err := m.Params.MarshalToSizedBuffer(dAtA[:i])
|
size, err := m.Params.MarshalToSizedBuffer(dAtA[:i])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -146,6 +171,12 @@ func (m *GenesisState) Size() (n int) {
|
|||||||
_ = l
|
_ = l
|
||||||
l = m.Params.Size()
|
l = m.Params.Size()
|
||||||
n += 1 + l + sovGenesis(uint64(l))
|
n += 1 + l + sovGenesis(uint64(l))
|
||||||
|
if len(m.Deposits) > 0 {
|
||||||
|
for _, e := range m.Deposits {
|
||||||
|
l = e.Size()
|
||||||
|
n += 1 + l + sovGenesis(uint64(l))
|
||||||
|
}
|
||||||
|
}
|
||||||
return n
|
return n
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -217,6 +248,40 @@ func (m *GenesisState) Unmarshal(dAtA []byte) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
iNdEx = postIndex
|
iNdEx = postIndex
|
||||||
|
case 2:
|
||||||
|
if wireType != 2 {
|
||||||
|
return fmt.Errorf("proto: wrong wireType = %d for field Deposits", wireType)
|
||||||
|
}
|
||||||
|
var msglen int
|
||||||
|
for shift := uint(0); ; shift += 7 {
|
||||||
|
if shift >= 64 {
|
||||||
|
return ErrIntOverflowGenesis
|
||||||
|
}
|
||||||
|
if iNdEx >= l {
|
||||||
|
return io.ErrUnexpectedEOF
|
||||||
|
}
|
||||||
|
b := dAtA[iNdEx]
|
||||||
|
iNdEx++
|
||||||
|
msglen |= int(b&0x7F) << shift
|
||||||
|
if b < 0x80 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if msglen < 0 {
|
||||||
|
return ErrInvalidLengthGenesis
|
||||||
|
}
|
||||||
|
postIndex := iNdEx + msglen
|
||||||
|
if postIndex < 0 {
|
||||||
|
return ErrInvalidLengthGenesis
|
||||||
|
}
|
||||||
|
if postIndex > l {
|
||||||
|
return io.ErrUnexpectedEOF
|
||||||
|
}
|
||||||
|
m.Deposits = append(m.Deposits, Deposit{})
|
||||||
|
if err := m.Deposits[len(m.Deposits)-1].Unmarshal(dAtA[iNdEx:postIndex]); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
iNdEx = postIndex
|
||||||
default:
|
default:
|
||||||
iNdEx = preIndex
|
iNdEx = preIndex
|
||||||
skippy, err := skipGenesis(dAtA[iNdEx:])
|
skippy, err := skipGenesis(dAtA[iNdEx:])
|
||||||
|
Loading…
Reference in New Issue
Block a user