Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Morse->Shannon Migration] feat: implement claim Morse pokt #1048

Draft
wants to merge 1 commit into
base: scaffold/migration/claim
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 59 additions & 26 deletions testutil/integration/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ import (
gatewaykeeper "github.com/pokt-network/poktroll/x/gateway/keeper"
gateway "github.com/pokt-network/poktroll/x/gateway/module"
gatewaytypes "github.com/pokt-network/poktroll/x/gateway/types"
migrationkeeper "github.com/pokt-network/poktroll/x/migration/keeper"
migration "github.com/pokt-network/poktroll/x/migration/module"
migrationtypes "github.com/pokt-network/poktroll/x/migration/types"
proofkeeper "github.com/pokt-network/poktroll/x/proof/keeper"
proof "github.com/pokt-network/poktroll/x/proof/module"
prooftypes "github.com/pokt-network/poktroll/x/proof/types"
Expand Down Expand Up @@ -273,6 +276,7 @@ func NewCompleteIntegrationApp(t *testing.T, opts ...IntegrationAppOptionFn) *Ap
cosmostypes.RegisterInterfaces(registry)
cryptocodec.RegisterInterfaces(registry)
banktypes.RegisterInterfaces(registry)
migrationtypes.RegisterInterfaces(registry)

// Prepare all the store keys
storeKeys := storetypes.NewKVStoreKeys(
Expand All @@ -287,6 +291,7 @@ func NewCompleteIntegrationApp(t *testing.T, opts ...IntegrationAppOptionFn) *Ap
prooftypes.StoreKey,
servicetypes.StoreKey,
authtypes.StoreKey,
migrationtypes.StoreKey,
)

// Prepare the codec
Expand Down Expand Up @@ -512,6 +517,22 @@ func NewCompleteIntegrationApp(t *testing.T, opts ...IntegrationAppOptionFn) *Ap
supplierKeeper,
)

// Prepare the migration keeper and module
migrationKeeper := migrationkeeper.NewKeeper(
cdc,
runtime.NewKVStoreService(storeKeys[migrationtypes.StoreKey]),
logger,
authority.String(),
accountKeeper,
bankKeeper,
)
migrationModule := migration.NewAppModule(
cdc,
migrationKeeper,
accountKeeper,
bankKeeper,
)

// Prepare the message & query routers
msgRouter := baseapp.NewMsgServiceRouter()
queryHelper := baseapp.NewQueryServerTestHelper(sdkCtx, registry)
Expand Down Expand Up @@ -544,6 +565,7 @@ func NewCompleteIntegrationApp(t *testing.T, opts ...IntegrationAppOptionFn) *Ap
prooftypes.ModuleName: proofModule,
authtypes.ModuleName: authModule,
sessiontypes.ModuleName: sessionModule,
migrationtypes.ModuleName: migrationModule,
}

// Initialize the integration integrationApp
Expand All @@ -563,32 +585,39 @@ func NewCompleteIntegrationApp(t *testing.T, opts ...IntegrationAppOptionFn) *Ap
opts...,
)

// Register the message servers
banktypes.RegisterMsgServer(msgRouter, bankkeeper.NewMsgServerImpl(bankKeeper))
tokenomicstypes.RegisterMsgServer(msgRouter, tokenomicskeeper.NewMsgServerImpl(tokenomicsKeeper))
servicetypes.RegisterMsgServer(msgRouter, servicekeeper.NewMsgServerImpl(serviceKeeper))
sharedtypes.RegisterMsgServer(msgRouter, sharedkeeper.NewMsgServerImpl(sharedKeeper))
gatewaytypes.RegisterMsgServer(msgRouter, gatewaykeeper.NewMsgServerImpl(gatewayKeeper))
apptypes.RegisterMsgServer(msgRouter, appkeeper.NewMsgServerImpl(applicationKeeper))
suppliertypes.RegisterMsgServer(msgRouter, supplierkeeper.NewMsgServerImpl(supplierKeeper))
prooftypes.RegisterMsgServer(msgRouter, proofkeeper.NewMsgServerImpl(proofKeeper))
authtypes.RegisterMsgServer(msgRouter, authkeeper.NewMsgServerImpl(accountKeeper))
sessiontypes.RegisterMsgServer(msgRouter, sessionkeeper.NewMsgServerImpl(sessionKeeper))
authz.RegisterMsgServer(msgRouter, authzKeeper)

// Register query servers
banktypes.RegisterQueryServer(queryHelper, bankKeeper)
authz.RegisterQueryServer(queryHelper, authzKeeper)
tokenomicstypes.RegisterQueryServer(queryHelper, tokenomicsKeeper)
servicetypes.RegisterQueryServer(queryHelper, serviceKeeper)
sharedtypes.RegisterQueryServer(queryHelper, sharedKeeper)
gatewaytypes.RegisterQueryServer(queryHelper, gatewayKeeper)
apptypes.RegisterQueryServer(queryHelper, applicationKeeper)
suppliertypes.RegisterQueryServer(queryHelper, supplierKeeper)
prooftypes.RegisterQueryServer(queryHelper, proofKeeper)
// TODO_TECHDEBT: What is the query server for authtypes?
// authtypes.RegisterQueryServer(queryHelper, accountKeeper)
sessiontypes.RegisterQueryServer(queryHelper, sessionKeeper)
// Register the message & query servers.
configurator := module.NewConfigurator(cdc, msgRouter, queryHelper)
for _, mod := range integrationApp.GetModuleManager().Modules {
mod.(module.HasServices).RegisterServices(configurator)
}

//banktypes.RegisterMsgServer(msgRouter, bankkeeper.NewMsgServerImpl(bankKeeper))
//tokenomicstypes.RegisterMsgServer(msgRouter, tokenomicskeeper.NewMsgServerImpl(tokenomicsKeeper))
//servicetypes.RegisterMsgServer(msgRouter, servicekeeper.NewMsgServerImpl(serviceKeeper))
//sharedtypes.RegisterMsgServer(msgRouter, sharedkeeper.NewMsgServerImpl(sharedKeeper))
//gatewaytypes.RegisterMsgServer(msgRouter, gatewaykeeper.NewMsgServerImpl(gatewayKeeper))
//apptypes.RegisterMsgServer(msgRouter, appkeeper.NewMsgServerImpl(applicationKeeper))
//suppliertypes.RegisterMsgServer(msgRouter, supplierkeeper.NewMsgServerImpl(supplierKeeper))
//prooftypes.RegisterMsgServer(msgRouter, proofkeeper.NewMsgServerImpl(proofKeeper))
//authtypes.RegisterMsgServer(msgRouter, authkeeper.NewMsgServerImpl(accountKeeper))
//sessiontypes.RegisterMsgServer(msgRouter, sessionkeeper.NewMsgServerImpl(sessionKeeper))
//authz.RegisterMsgServer(msgRouter, authzKeeper)
//migrationtypes.RegisterMsgServer(msgRouter, migrationkeeper.NewMsgServerImpl(migrationKeeper))
//
//// Register query servers
//banktypes.RegisterQueryServer(queryHelper, bankKeeper)
//authz.RegisterQueryServer(queryHelper, authzKeeper)
//tokenomicstypes.RegisterQueryServer(queryHelper, tokenomicsKeeper)
//servicetypes.RegisterQueryServer(queryHelper, serviceKeeper)
//sharedtypes.RegisterQueryServer(queryHelper, sharedKeeper)
//gatewaytypes.RegisterQueryServer(queryHelper, gatewayKeeper)
//apptypes.RegisterQueryServer(queryHelper, applicationKeeper)
//suppliertypes.RegisterQueryServer(queryHelper, supplierKeeper)
//prooftypes.RegisterQueryServer(queryHelper, proofKeeper)
//// TODO_TECHDEBT: What is the query server for authtypes?
//// authtypes.RegisterQueryServer(queryHelper, accountKeeper)
//sessiontypes.RegisterQueryServer(queryHelper, sessionKeeper)
//migrationtypes.RegisterQueryServer(queryHelper, migrationKeeper)

// Need to go to the next block to finalize the genesis and setup.
// This has to be after the params are set, as the params are stored in the
Expand Down Expand Up @@ -963,6 +992,10 @@ func (app *App) setupDefaultActorsState(
app.NextBlock(t)
}

func (app *App) GetModuleManager() module.Manager {
return app.moduleManager
}

// fundAccount mints and sends amountUpokt tokens to the given recipientAddr.
//
// TODO_IMPROVE: Eliminate usage of and remove this function in favor of
Expand Down
21 changes: 21 additions & 0 deletions testutil/keeper/migration.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package keeper

import (
"context"
"testing"

"cosmossdk.io/log"
Expand All @@ -15,8 +16,10 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"

"github.com/pokt-network/poktroll/testutil/proof/mocks"
"github.com/pokt-network/poktroll/x/migration/keeper"
"github.com/pokt-network/poktroll/x/migration/types"
)
Expand All @@ -33,11 +36,29 @@ func MigrationKeeper(t testing.TB) (keeper.Keeper, sdk.Context) {
cdc := codec.NewProtoCodec(registry)
authority := authtypes.NewModuleAddress(govtypes.ModuleName)

ctrl := gomock.NewController(t)
mockBankKeeper := mocks.NewMockBankKeeper(ctrl)
mockBankKeeper.EXPECT().
SpendableCoins(gomock.Any(), gomock.Any()).
DoAndReturn(
func(ctx context.Context, addr sdk.AccAddress) sdk.Coins {
mapMu.RLock()
defer mapMu.RUnlock()
if coins, ok := mapAccAddrCoins[addr.String()]; ok {
return coins
}
return sdk.Coins{}
},
).AnyTimes()
mockAccountKeeper := mocks.NewMockAccountKeeper(ctrl)

k := keeper.NewKeeper(
cdc,
runtime.NewKVStoreService(storeKey),
log.NewNopLogger(),
authority.String(),
mockAccountKeeper,
mockBankKeeper,
)

ctx := sdk.NewContext(stateStore, cmtproto.Header{}, false, log.NewNopLogger())
Expand Down
21 changes: 21 additions & 0 deletions testutil/keeper/tokenomics.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ import (
apptypes "github.com/pokt-network/poktroll/x/application/types"
gatewaykeeper "github.com/pokt-network/poktroll/x/gateway/keeper"
gatewaytypes "github.com/pokt-network/poktroll/x/gateway/types"
migrationkeeper "github.com/pokt-network/poktroll/x/migration/keeper"
migrationtypes "github.com/pokt-network/poktroll/x/migration/types"
proofkeeper "github.com/pokt-network/poktroll/x/proof/keeper"
prooftypes "github.com/pokt-network/poktroll/x/proof/types"
servicekeeper "github.com/pokt-network/poktroll/x/service/keeper"
Expand Down Expand Up @@ -67,6 +69,7 @@ type TokenomicsModuleKeepers struct {
tokenomicstypes.SharedKeeper
tokenomicstypes.SessionKeeper
tokenomicstypes.ServiceKeeper
tokenomicstypes.MigrationKeeper

Codec *codec.ProtoCodec
}
Expand Down Expand Up @@ -324,6 +327,7 @@ func NewTokenomicsModuleKeepers(
prooftypes.StoreKey,
sharedtypes.StoreKey,
servicetypes.StoreKey,
migrationtypes.StoreKey,
)

// Construct a multistore & mount store keys for each keeper that will interact with the state store.
Expand Down Expand Up @@ -523,6 +527,22 @@ func NewTokenomicsModuleKeepers(
require.NoError(t, err)
}

migrationKeeper := migrationkeeper.NewKeeper(
cdc,
runtime.NewKVStoreService(keys[migrationtypes.StoreKey]),
logger,
authority.String(),
accountKeeper,
bankKeeper,
)

require.NoError(t, migrationKeeper.SetParams(sdkCtx, migrationtypes.DefaultParams()))

if params, ok := cfg.moduleParams[migrationtypes.ModuleName]; ok {
err := migrationKeeper.SetParams(ctx, *params.(*migrationtypes.Params))
require.NoError(t, err)
}

keepers := TokenomicsModuleKeepers{
Keeper: &tokenomicsKeeper,
AccountKeeper: &accountKeeper,
Expand All @@ -533,6 +553,7 @@ func NewTokenomicsModuleKeepers(
SharedKeeper: &sharedKeeper,
SessionKeeper: &sessionKeeper,
ServiceKeeper: &serviceKeeper,
MigrationKeeper: &migrationKeeper,

Codec: cdc,
}
Expand Down
4 changes: 1 addition & 3 deletions testutil/testmigration/fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package testmigration
import (
"encoding/binary"
"fmt"
"math/rand"

cometcrypto "github.com/cometbft/cometbft/crypto/ed25519"
cmtjson "github.com/cometbft/cometbft/libs/json"
Expand Down Expand Up @@ -59,9 +58,8 @@ func NewMorseStateExportAndAccountState(
}

for i := 1; i < numAccounts+1; i++ {
seedUint := rand.Uint64()
seedBz := make([]byte, 8)
binary.LittleEndian.PutUint64(seedBz, seedUint)
binary.LittleEndian.PutUint64(seedBz, uint64(i))
privKey := cometcrypto.GenPrivKeyFromSecret(seedBz)
pubKey := privKey.PubKey()
balanceAmount := int64(1e6*i + i) // i_000_00i
Expand Down
15 changes: 11 additions & 4 deletions x/migration/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ type (
// the address capable of executing a MsgUpdateParams message. Typically, this
// should be the x/gov module account.
authority string

accountKeeper types.AccountKeeper
bankKeeper types.BankKeeper
}
)

Expand All @@ -28,17 +31,21 @@ func NewKeeper(
storeService store.KVStoreService,
logger log.Logger,
authority string,
accountKeeper types.AccountKeeper,
bankKeeper types.BankKeeper,

) Keeper {
if _, err := sdk.AccAddressFromBech32(authority); err != nil {
panic(fmt.Sprintf("invalid authority address: %s", authority))
}

return Keeper{
cdc: cdc,
storeService: storeService,
authority: authority,
logger: logger,
cdc: cdc,
storeService: storeService,
authority: authority,
logger: logger,
accountKeeper: accountKeeper,
bankKeeper: bankKeeper,
}
}

Expand Down
80 changes: 80 additions & 0 deletions x/migration/keeper/msg_server_claim_morse_upokt_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package keeper_test

import (
"encoding/binary"
"testing"

"cosmossdk.io/depinject"
cometcrypto "github.com/cometbft/cometbft/crypto/ed25519"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
"github.com/cosmos/gogoproto/proto"
"github.com/stretchr/testify/require"

"github.com/pokt-network/poktroll/app/volatile"
"github.com/pokt-network/poktroll/pkg/client/query"
"github.com/pokt-network/poktroll/testutil/integration"
"github.com/pokt-network/poktroll/testutil/sample"
"github.com/pokt-network/poktroll/testutil/testmigration"
"github.com/pokt-network/poktroll/x/migration/types"
)

func TestMsgServer_ClaimMorsePokt(t *testing.T) {
app := integration.NewCompleteIntegrationApp(t)

numAccounts := 10
_, accountState := testmigration.NewMorseStateExportAndAccountState(t, numAccounts)

// Ensure that the MorseAccountState is set initially.
resAny, err := app.RunMsg(t, &types.MsgCreateMorseAccountState{
Authority: authtypes.NewModuleAddress(govtypes.ModuleName).String(),
MorseAccountState: *accountState,
})
require.NoError(t, err)

createStateRes, ok := resAny.(*types.MsgCreateMorseAccountStateResponse)
require.True(t, ok)
t.Logf("createStateRes: %+v", createStateRes)

shannonDestAddr := sample.AccAddress()

deps := depinject.Supply(app.QueryHelper())
bankClient, err := query.NewBankQuerier(deps)
require.NoError(t, err)

balance, err := bankClient.GetBalance(app.GetSdkCtx(), shannonDestAddr)
require.NoError(t, err)
require.Equal(t, int64(0), balance.Amount.Int64())

// TODO_IN_THIS_COMMIT: comment or refactor testutil...
seedBz := make([]byte, 8)
binary.LittleEndian.PutUint64(seedBz, uint64(1))
privKey := cometcrypto.GenPrivKeyFromSecret(seedBz)
morseDestAddr := privKey.PubKey().Address().String()
require.Equal(t, morseDestAddr, accountState.Accounts[0].Address.String())

morseClaimMsg := types.NewMsgClaimMorsePokt(shannonDestAddr, morseDestAddr, nil)
morseClaimMsgUnsignedBz, err := proto.Marshal(morseClaimMsg)
require.NoError(t, err)

signature, err := privKey.Sign(morseClaimMsgUnsignedBz)
require.NoError(t, err)

morseClaimMsg.MorseSignature = signature
resAny, err = app.RunMsg(t, morseClaimMsg)
require.NoError(t, err)

expectedBalance := sdk.NewInt64Coin(volatile.DenomuPOKT, 1110111)
expectedStateHash, err := accountState.GetHash()
require.NoError(t, err)

claimAccountRes, ok := resAny.(*types.MsgClaimMorsePoktResponse)
require.True(t, ok)
require.Equal(t, expectedStateHash, claimAccountRes.GetStateHash())
require.Equal(t, expectedBalance, claimAccountRes.GetBalance())

balance, err = bankClient.GetBalance(app.GetSdkCtx(), shannonDestAddr)
require.NoError(t, err)
require.Equal(t, expectedBalance, balance)
}
2 changes: 2 additions & 0 deletions x/migration/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@ func ProvideModule(in ModuleInputs) ModuleOutputs {
in.StoreService,
in.Logger,
authority.String(),
in.AccountKeeper,
in.BankKeeper,
)
m := NewAppModule(
in.Cdc,
Expand Down
Loading
Loading