Skip to content

Commit

Permalink
add updte keygen message
Browse files Browse the repository at this point in the history
  • Loading branch information
kingpinXD committed Dec 9, 2024
1 parent 863b4cd commit ede755b
Show file tree
Hide file tree
Showing 8 changed files with 296 additions and 72 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,8 @@ test-sim-after-import-long: runsim
@echo "Running application simulation-after-import. This may take several minute"
@$(BINDIR)/runsim -Jobs=4 -SimAppPkg=$(SIMAPP) -ExitOnFail 500 50 TestAppSimulationAfterImport

test-sim-quick: test-sim-nondeterminism test-sim-fullappsimulation test-sim-import-export test-sim-after-import

.PHONY: \
test-sim-nondeterminism \
test-sim-fullappsimulation \
Expand Down
8 changes: 0 additions & 8 deletions simulation/simulation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,14 +425,6 @@ func TestAppImportExport(t *testing.T) {
storeA := ctxSimApp.KVStore(skp.A)
storeB := ctxNewSimApp.KVStore(skp.B)

a, b := CountKVStores(storeA, storeB, skp.SkipPrefixes)
if a != b {
t.Logf("storeA: %d, storeB: %d", a, b)
t.Log("key prefix: ", skp.A.Name())
//FindDiffKeys(storeA, storeB)
//t.Fail()
}

failedKVAs, failedKVBs := sdk.DiffKVStores(storeA, storeB, skp.SkipPrefixes)
require.Equal(t, len(failedKVAs), len(failedKVBs), "unequal sets of key-values to compare")

Expand Down
9 changes: 9 additions & 0 deletions testutil/sample/observer.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,15 @@ func Keygen(t *testing.T) *types.Keygen {
}
}

func KeygenFromRand(r *rand.Rand) types.Keygen {
pubkey := PubKey(r)
return types.Keygen{
Status: types.KeygenStatus_KeyGenSuccess,
GranteePubkeys: []string{pubkey.String()},
BlockNumber: r.Int63(),
}
}

func LastObserverCount(lastChangeHeight int64) *types.LastObserverCount {
r := newRandFromSeed(lastChangeHeight)

Expand Down
8 changes: 1 addition & 7 deletions x/observer/genesis.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package observer

import (
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/zeta-chain/node/pkg/chains"
Expand All @@ -13,17 +11,13 @@ import (
// InitGenesis initializes the observer module's state from a provided genesis
// state.
func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState types.GenesisState) {

fmt.Println("Observer Set Len : ", genState.Observers.Len())

observerCount := uint64(genState.Observers.Len())

if genState.Observers.Len() > 0 {
k.SetObserverSet(ctx, genState.Observers)
} else {
k.SetObserverSet(ctx, types.ObserverSet{})
}

observerCount := uint64(genState.Observers.Len())
if genState.LastObserverCount != nil {
k.SetLastObserverCount(ctx, genState.LastObserverCount)
} else {
Expand Down
54 changes: 54 additions & 0 deletions x/observer/simulation/operation_disable_cctx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package simulation

import (
"math/rand"

"github.com/cosmos/cosmos-sdk/baseapp"
sdk "github.com/cosmos/cosmos-sdk/types"
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
"github.com/cosmos/cosmos-sdk/x/simulation"
"github.com/zeta-chain/node/x/observer/keeper"
"github.com/zeta-chain/node/x/observer/types"
)

// SimulateMsgDisableCCTX generates a MsgDisableCCTX and delivers it.
func SimulateMsgDisableCCTX(k keeper.Keeper) simtypes.Operation {
return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accounts []simtypes.Account, _ string,
) (OperationMsg simtypes.OperationMsg, futureOps []simtypes.FutureOperation, err error) {
policyAccount, err := GetPolicyAccount(ctx, k.GetAuthorityKeeper(), accounts)
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgUpdateKeygen, err.Error()), nil, nil
}
authAccount := k.GetAuthKeeper().GetAccount(ctx, policyAccount.Address)
spendable := k.GetBankKeeper().SpendableCoins(ctx, authAccount.GetAddress())

msg := types.MsgDisableCCTX{
Creator: policyAccount.Address.String(),
DisableInbound: false,
DisableOutbound: true,
}

err = msg.ValidateBasic()
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), err.Error()), nil, err
}

txCtx := simulation.OperationInput{
R: r,
App: app,
TxGen: moduletestutil.MakeTestEncodingConfig().TxConfig,
Cdc: nil,
Msg: &msg,
MsgType: msg.Type(),
Context: ctx,
SimAccount: policyAccount,
AccountKeeper: k.GetAuthKeeper(),
Bankkeeper: k.GetBankKeeper(),
ModuleName: types.ModuleName,
CoinsSpentInMsg: spendable,
}

return simulation.GenAndDeliverTxWithRandFees(txCtx)
}
}
54 changes: 54 additions & 0 deletions x/observer/simulation/operation_enable_cctx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package simulation

import (
"math/rand"

"github.com/cosmos/cosmos-sdk/baseapp"
sdk "github.com/cosmos/cosmos-sdk/types"
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
"github.com/cosmos/cosmos-sdk/x/simulation"
"github.com/zeta-chain/node/x/observer/keeper"
"github.com/zeta-chain/node/x/observer/types"
)

// SimulateMsgEnableCCTX generates a MsgEnableCCTX and delivers it.
func SimulateMsgEnableCCTX(k keeper.Keeper) simtypes.Operation {
return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accounts []simtypes.Account, _ string,
) (OperationMsg simtypes.OperationMsg, futureOps []simtypes.FutureOperation, err error) {
policyAccount, err := GetPolicyAccount(ctx, k.GetAuthorityKeeper(), accounts)
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgUpdateKeygen, err.Error()), nil, nil
}
authAccount := k.GetAuthKeeper().GetAccount(ctx, policyAccount.Address)
spendable := k.GetBankKeeper().SpendableCoins(ctx, authAccount.GetAddress())

msg := types.MsgEnableCCTX{
Creator: policyAccount.Address.String(),
EnableInbound: true,
EnableOutbound: false,
}

err = msg.ValidateBasic()
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), err.Error()), nil, err
}

txCtx := simulation.OperationInput{
R: r,
App: app,
TxGen: moduletestutil.MakeTestEncodingConfig().TxConfig,
Cdc: nil,
Msg: &msg,
MsgType: msg.Type(),
Context: ctx,
SimAccount: policyAccount,
AccountKeeper: k.GetAuthKeeper(),
Bankkeeper: k.GetBankKeeper(),
ModuleName: types.ModuleName,
CoinsSpentInMsg: spendable,
}

return simulation.GenAndDeliverTxWithRandFees(txCtx)
}
}
65 changes: 65 additions & 0 deletions x/observer/simulation/operation_update_keygen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package simulation

import (
"math/rand"

"github.com/cosmos/cosmos-sdk/baseapp"
sdk "github.com/cosmos/cosmos-sdk/types"
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
"github.com/cosmos/cosmos-sdk/x/simulation"
"github.com/zeta-chain/node/testutil/sample"
"github.com/zeta-chain/node/x/observer/keeper"
"github.com/zeta-chain/node/x/observer/types"
)

// SimulateMsgUpdateKeygen generates a MsgUpdateKeygen and delivers it.
func SimulateMsgUpdateKeygen(k keeper.Keeper) simtypes.Operation {
return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accounts []simtypes.Account, _ string,
) (OperationMsg simtypes.OperationMsg, futureOps []simtypes.FutureOperation, err error) {
policyAccount, err := GetPolicyAccount(ctx, k.GetAuthorityKeeper(), accounts)
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, types.TypeMsgUpdateKeygen, err.Error()), nil, nil
}

authAccount := k.GetAuthKeeper().GetAccount(ctx, policyAccount.Address)
spendable := k.GetBankKeeper().SpendableCoins(ctx, authAccount.GetAddress())

_, found := k.GetKeygen(ctx)
if !found {
kg := sample.KeygenFromRand(r)
k.SetKeygen(ctx, kg)
}

blockHeighMin := ctx.BlockHeight() + 11
blockHeighMax := ctx.BlockHeight() + 1000
keygenBlockHeight := int64(r.Intn(int(blockHeighMax-blockHeighMin))) + blockHeighMin

msg := types.MsgUpdateKeygen{
Creator: policyAccount.Address.String(),
Block: keygenBlockHeight,
}

err = msg.ValidateBasic()
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), err.Error()), nil, err
}

txCtx := simulation.OperationInput{
R: r,
App: app,
TxGen: moduletestutil.MakeTestEncodingConfig().TxConfig,
Cdc: nil,
Msg: &msg,
MsgType: msg.Type(),
Context: ctx,
SimAccount: policyAccount,
AccountKeeper: k.GetAuthKeeper(),
Bankkeeper: k.GetBankKeeper(),
ModuleName: types.ModuleName,
CoinsSpentInMsg: spendable,
}

return simulation.GenAndDeliverTxWithRandFees(txCtx)
}
}
Loading

0 comments on commit ede755b

Please sign in to comment.