Skip to content

Commit

Permalink
add: genesis (#468)
Browse files Browse the repository at this point in the history
Co-authored-by: Walter White <[email protected]>
  • Loading branch information
testinginprod and NibiruHeisenberg authored May 23, 2022
1 parent 8d3640d commit 0e5b534
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 74 deletions.
3 changes: 1 addition & 2 deletions proto/lockup/v1/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,5 @@ option go_package = "github.com/NibiruChain/nibiru/x/lockup/types";

// GenesisState defines the lockup module's genesis state.
message GenesisState {
uint64 last_lock_id = 1;
repeated Lock locks = 2 [(gogoproto.nullable) = false];
repeated Lock locks = 1;
}
19 changes: 0 additions & 19 deletions x/lockup/genesis.go

This file was deleted.

13 changes: 13 additions & 0 deletions x/lockup/keeper/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,3 +341,16 @@ func (s LockState) keyDenomTime(denom string, t time.Time, pk []byte) []byte {
// TODO(mercilex): maybe more efficient
return append(append([]byte(denom), 0xFF), s.keyTime(t, pk)...)
}

func (s LockState) IterateLocks(do func(lock *types.Lock) (stop bool)) {
iter := s.locks.Iterator(nil, nil)
defer iter.Close()

for ; iter.Valid(); iter.Next() {
lock := new(types.Lock)
s.cdc.MustUnmarshal(iter.Value(), lock)
if do(lock) {
break
}
}
}
20 changes: 17 additions & 3 deletions x/lockup/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,15 +160,29 @@ func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, gs json.Ra
// Initialize global index to index in genesis state
cdc.MustUnmarshalJSON(gs, &genState)

// InitGenesis(ctx, am.keeper, genState)
for _, lockup := range genState.Locks {
addr, err := sdk.AccAddressFromBech32(lockup.Owner)
if err != nil {
panic(err)
}
_, err = am.keeper.LockTokens(ctx, addr, lockup.Coins, lockup.Duration)
if err != nil {
panic(err)
}
}

return []abci.ValidatorUpdate{}
}

// ExportGenesis returns the capability module's exported genesis state as raw JSON bytes.
func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.RawMessage {
// genState := ExportGenesis(ctx, am.keeper)
return cdc.MustMarshalJSON(types.DefaultGenesis())
state := new(types.GenesisState)
am.keeper.LocksState(ctx).IterateLocks(func(lock *types.Lock) (stop bool) {
state.Locks = append(state.Locks, lock)
return false
})

return am.cdc.MustMarshalJSON(state)
}

// BeginBlock executes all ABCI BeginBlock logic respective to the capability module.
Expand Down
60 changes: 60 additions & 0 deletions x/lockup/module_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package lockup_test

import (
"testing"
"time"

"github.com/cosmos/cosmos-sdk/simapp"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"

"github.com/NibiruChain/nibiru/x/lockup"
"github.com/NibiruChain/nibiru/x/lockup/types"
"github.com/NibiruChain/nibiru/x/testutil"
"github.com/NibiruChain/nibiru/x/testutil/sample"
)

func TestAppModule_ExportGenesis_ImportGenesis(t *testing.T) {
app := testutil.NewTestApp(false)
am := lockup.NewAppModule(app.AppCodec(), app.LockupKeeper, app.AccountKeeper, app.BankKeeper)

ctxUncached := app.NewContext(false, tmproto.Header{Time: time.Now()})
// create locks
ctx, _ := ctxUncached.CacheContext()
var locks []*types.Lock
for i := 0; i < 100; i++ {
addr := sample.AccAddress()
coins := sdk.NewCoins(
sdk.NewInt64Coin("test", 1+int64(i)*100),
)
err := simapp.FundAccount(app.BankKeeper, ctx, addr, coins)
require.NoError(t, err)

lock, err := app.LockupKeeper.LockTokens(ctx, addr, coins, time.Duration(i)*time.Hour*24)
require.NoError(t, err)

locks = append(locks, lock)
}

// export genesis
genesisRaw := am.ExportGenesis(ctx, app.AppCodec())
genesis := new(types.GenesisState)
app.AppCodec().MustUnmarshalJSON(genesisRaw, genesis)

require.Equal(t, locks, genesis.Locks)

// test import
ctx, _ = ctxUncached.CacheContext()
// we need to refund accounts
for _, lock := range genesis.Locks {
owner, err := sdk.AccAddressFromBech32(lock.Owner)
require.NoError(t, err)
err = simapp.FundAccount(app.BankKeeper, ctx, owner, lock.Coins)
require.NoError(t, err)
}

am.InitGenesis(ctx, app.AppCodec(), genesisRaw)
exportedAfterImport := am.ExportGenesis(ctx, app.AppCodec())
require.Equal(t, genesisRaw, exportedAfterImport)
}
63 changes: 13 additions & 50 deletions x/lockup/types/genesis.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0e5b534

Please sign in to comment.