Skip to content

Commit

Permalink
Merge pull request #173 from terra-money/fix/amino/0.1.1
Browse files Browse the repository at this point in the history
fix amino interfaces
  • Loading branch information
emidev98 authored May 2, 2023
2 parents d112845 + 721086a commit c97b7c9
Show file tree
Hide file tree
Showing 16 changed files with 160 additions and 55 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,14 @@ format-tools:
go install mvdan.cc/[email protected]
go install github.com/client9/misspell/cmd/[email protected]
go install golang.org/x/tools/cmd/goimports@latest
go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.50.1
go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.52.2

lint: format-tools
golangci-lint run
find . -name '*.go' -type f -not -path "./vendor*" -not -path "*.git*" -not -path "*.pb.go" -not -path "*pb.gw.go" | xargs gofumpt -d

lint-docker:
docker run --rm -v $(PWD):/app -w /app golangci/golangci-lint:v1.49.0-alpine golangci-lint run
docker run --rm -v $(PWD):/app -w /app golangci/golangci-lint:v1.52.2-alpine golangci-lint run

format: format-tools
find . -name '*.go' -type f -not -path "./vendor*" -not -path "*.git*" -not -path "*.pb.go" -not -path "*pb.gw.go" | xargs gofumpt -w
Expand Down
2 changes: 1 addition & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -812,7 +812,7 @@ func (app *App) GetSubspace(moduleName string) paramstypes.Subspace {

// RegisterAPIRoutes registers all application module routes with the provided
// API server.
func (app *App) RegisterAPIRoutes(apiSvr *api.Server, apiConfig config.APIConfig) {
func (app *App) RegisterAPIRoutes(apiSvr *api.Server, _ config.APIConfig) {
clientCtx := apiSvr.ClientCtx
// Register new tx routes from grpc-gateway.
authtx.RegisterGRPCGatewayRoutes(clientCtx, apiSvr.GRPCGatewayRouter)
Expand Down
2 changes: 1 addition & 1 deletion app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

func TestAppExportAndBlockedAddrs(t *testing.T) {
app := Setup(t, false)
app := Setup(t)
_, err := app.ExportAppStateAndValidators(true, []string{})
require.NoError(t, err, "ExportAppStateAndValidators should not have an error")

Expand Down
4 changes: 2 additions & 2 deletions app/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import (
)

// Setup initializes a new SimApp. A Nop logger is set in SimApp.
func Setup(t *testing.T, isCheckTx bool) *App {
func Setup(t *testing.T) *App {
t.Helper()

privVal := mock.NewPV()
Expand Down Expand Up @@ -194,7 +194,7 @@ func MakeTestEncodingConfig() params.EncodingConfig {
type EmptyAppOptions struct{}

// Get implements AppOptions
func (ao EmptyAppOptions) Get(o string) interface{} {
func (ao EmptyAppOptions) Get(_ string) interface{} {
return nil
}

Expand Down
18 changes: 9 additions & 9 deletions x/alliance/keeper/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,42 +11,42 @@ type Hooks struct {

var _ stakingtypes.StakingHooks = Hooks{}

func (h Hooks) AfterValidatorCreated(ctx sdk.Context, valAddr sdk.ValAddress) error {
func (h Hooks) AfterValidatorCreated(_ sdk.Context, _ sdk.ValAddress) error {
return nil
}

func (h Hooks) BeforeValidatorModified(ctx sdk.Context, valAddr sdk.ValAddress) error {
func (h Hooks) BeforeValidatorModified(_ sdk.Context, _ sdk.ValAddress) error {
return nil
}

func (h Hooks) AfterValidatorRemoved(ctx sdk.Context, consAddr sdk.ConsAddress, valAddr sdk.ValAddress) error {
func (h Hooks) AfterValidatorRemoved(ctx sdk.Context, _ sdk.ConsAddress, _ sdk.ValAddress) error {
h.k.QueueAssetRebalanceEvent(ctx)
return nil
}

func (h Hooks) AfterValidatorBonded(ctx sdk.Context, consAddr sdk.ConsAddress, valAddr sdk.ValAddress) error {
func (h Hooks) AfterValidatorBonded(ctx sdk.Context, _ sdk.ConsAddress, _ sdk.ValAddress) error {
h.k.QueueAssetRebalanceEvent(ctx)
return nil
}

func (h Hooks) AfterValidatorBeginUnbonding(ctx sdk.Context, consAddr sdk.ConsAddress, valAddr sdk.ValAddress) error {
func (h Hooks) AfterValidatorBeginUnbonding(ctx sdk.Context, _ sdk.ConsAddress, _ sdk.ValAddress) error {
h.k.QueueAssetRebalanceEvent(ctx)
return nil
}

func (h Hooks) BeforeDelegationCreated(ctx sdk.Context, delAddr sdk.AccAddress, valAddr sdk.ValAddress) error {
func (h Hooks) BeforeDelegationCreated(_ sdk.Context, _ sdk.AccAddress, _ sdk.ValAddress) error {
return nil
}

func (h Hooks) BeforeDelegationSharesModified(ctx sdk.Context, delAddr sdk.AccAddress, valAddr sdk.ValAddress) error {
func (h Hooks) BeforeDelegationSharesModified(_ sdk.Context, _ sdk.AccAddress, _ sdk.ValAddress) error {
return nil
}

func (h Hooks) BeforeDelegationRemoved(ctx sdk.Context, delAddr sdk.AccAddress, valAddr sdk.ValAddress) error {
func (h Hooks) BeforeDelegationRemoved(_ sdk.Context, _ sdk.AccAddress, _ sdk.ValAddress) error {
return nil
}

func (h Hooks) AfterDelegationModified(ctx sdk.Context, delAddr sdk.AccAddress, valAddr sdk.ValAddress) error {
func (h Hooks) AfterDelegationModified(ctx sdk.Context, _ sdk.AccAddress, _ sdk.ValAddress) error {
h.k.QueueAssetRebalanceEvent(ctx)
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion x/alliance/keeper/tests/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

func createTestContext(t *testing.T) (*app.App, sdk.Context) {
app := app.Setup(t, false)
app := app.Setup(t)
ctx := app.BaseApp.NewContext(false, tmproto.Header{})
return app, ctx
}
14 changes: 7 additions & 7 deletions x/alliance/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (a AppModuleBasic) DefaultGenesis(jsonCodec codec.JSONCodec) json.RawMessag
return jsonCodec.MustMarshalJSON(DefaultGenesisState())
}

func (a AppModuleBasic) ValidateGenesis(jsonCodec codec.JSONCodec, config client.TxEncodingConfig, message json.RawMessage) error {
func (a AppModuleBasic) ValidateGenesis(jsonCodec codec.JSONCodec, _ client.TxEncodingConfig, message json.RawMessage) error {
var genesis types.GenesisState
if err := jsonCodec.UnmarshalJSON(message, &genesis); err != nil {
return fmt.Errorf("failed to unmarshal %s genesis state: %w", types.ModuleName, err)
Expand Down Expand Up @@ -109,7 +109,7 @@ func (a AppModule) InitGenesis(ctx sdk.Context, jsonCodec codec.JSONCodec, messa
return a.keeper.InitGenesis(ctx, &genesis)
}

func (a AppModule) ExportGenesis(ctx sdk.Context, jsonCodec codec.JSONCodec) json.RawMessage {
func (a AppModule) ExportGenesis(ctx sdk.Context, _ codec.JSONCodec) json.RawMessage {
genesis := a.keeper.ExportGenesis(ctx)
return a.cdc.MustMarshalJSON(genesis)
}
Expand Down Expand Up @@ -147,18 +147,18 @@ func (a AppModule) GenerateGenesisState(simState *module.SimulationState) {
simulation2.RandomizedGenesisState(simState)
}

func (a AppModule) ProposalContents(simState module.SimulationState) []simtypes.WeightedProposalContent {
func (a AppModule) ProposalContents(_ module.SimulationState) []simtypes.WeightedProposalContent {
return nil
}

func (a AppModule) RandomizedParams(r *rand.Rand) []simtypes.ParamChange {
return simulation2.ParamChanges(r)
func (a AppModule) RandomizedParams(_ *rand.Rand) []simtypes.ParamChange {
return simulation2.ParamChanges()
}

func (a AppModule) RegisterStoreDecoder(registry sdk.StoreDecoderRegistry) {
registry[types.StoreKey] = simulation2.NewDecodeStore(a.cdc)
}

func (a AppModule) WeightedOperations(simState module.SimulationState) []simtypes.WeightedOperation {
return simulation2.WeightedOperations(simState.AppParams, a.pcdc, a.accountKeeper, a.bankKeeper, a.stakingKeeper, a.keeper)
func (a AppModule) WeightedOperations(_ module.SimulationState) []simtypes.WeightedOperation {
return simulation2.WeightedOperations(a.pcdc, a.accountKeeper, a.bankKeeper, a.stakingKeeper, a.keeper)
}
1 change: 1 addition & 0 deletions x/alliance/tests/benchmark/benchmark_genesis.json

Large diffs are not rendered by default.

24 changes: 19 additions & 5 deletions x/alliance/tests/benchmark/benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func TestRunBenchmarks(t *testing.T) {
delegateOperation(ctx, app, r, assets, vals, dels)
operations["delegate"]++
case 1:
redelegateOperation(ctx, app, r, assets, vals, dels)
redelegateOperation(ctx, app, r, vals)
operations["redelegate"]++
case 2:
undelegateOperation(ctx, app, r)
Expand Down Expand Up @@ -151,7 +151,7 @@ func delegateOperation(ctx sdk.Context, app *test_helpers.App, r *rand.Rand, ass
createdDelegations = append(createdDelegations, types.NewDelegation(ctx, delAddr, valAddr, asset.Denom, sdk.ZeroDec(), []types.RewardHistory{}))
}

func redelegateOperation(ctx sdk.Context, app *test_helpers.App, r *rand.Rand, assets []types.AllianceAsset, vals []sdk.AccAddress, dels []sdk.AccAddress) { //nolint:unparam // assets is unused
func redelegateOperation(ctx sdk.Context, app *test_helpers.App, r *rand.Rand, vals []sdk.AccAddress) {
var delegation types.Delegation
if len(createdDelegations) == 0 {
return
Expand All @@ -171,9 +171,7 @@ func redelegateOperation(ctx sdk.Context, app *test_helpers.App, r *rand.Rand, a
return
}

dstValAddr := sdk.ValAddress(vals[r.Intn(len(vals)-1)])
for ; dstValAddr.Equals(srcValAddr); dstValAddr = sdk.ValAddress(vals[r.Intn(len(vals)-1)]) {
}
dstValAddr := getRandomValAddress(r, vals, srcValAddr)
dstValidator, _ := app.AllianceKeeper.GetAllianceValidator(ctx, dstValAddr)

delegation, found := app.AllianceKeeper.GetDelegation(ctx, delAddr, srcValidator.GetOperator(), asset.Denom)
Expand All @@ -190,6 +188,22 @@ func redelegateOperation(ctx sdk.Context, app *test_helpers.App, r *rand.Rand, a
}
}

func getRandomValAddress(r *rand.Rand, vals []sdk.AccAddress, srcValAddr sdk.ValAddress) sdk.ValAddress {
var dstValAddr sdk.ValAddress

for {
// Get a random destination validator address
dstValAddr = sdk.ValAddress(vals[r.Intn(len(vals)-1)])

// Break the loop if the destination validator address is different from the source validator address
if !dstValAddr.Equals(srcValAddr) {
break
}
}

return dstValAddr
}

func undelegateOperation(ctx sdk.Context, app *test_helpers.App, r *rand.Rand) {
if len(createdDelegations) == 0 {
return
Expand Down
2 changes: 1 addition & 1 deletion x/alliance/tests/benchmark/test_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
)

func SetupApp(t *testing.T, r *rand.Rand, numAssets int, numValidators int, numDelegators int) (app *test_helpers.App, ctx sdk.Context, assets []types.AllianceAsset, valAddrs []sdk.AccAddress, delAddrs []sdk.AccAddress) {
app = test_helpers.Setup(t, false)
app = test_helpers.Setup(t)
ctx = app.BaseApp.NewContext(false, tmproto.Header{})
startTime := time.Now()
ctx = ctx.WithBlockTime(startTime)
Expand Down
2 changes: 1 addition & 1 deletion x/alliance/tests/e2e/test_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

func setupApp(t *testing.T, numValidators int, numDelegators int, initBalance sdk.Coins) (app *test_helpers.App, ctx sdk.Context, valAddrs []sdk.ValAddress, delAddrs []sdk.AccAddress) {
app = test_helpers.Setup(t, false)
app = test_helpers.Setup(t)
ctx = app.BaseApp.NewContext(false, tmproto.Header{})
startTime := time.Now()
ctx = ctx.WithBlockTime(startTime)
Expand Down
8 changes: 4 additions & 4 deletions x/alliance/tests/simulation/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (

// WeightedOperations returns all the operations from the module with their respective weights
func WeightedOperations(
appParams simtypes.AppParams, cdc *codec.ProtoCodec,
cdc *codec.ProtoCodec,
ak types.AccountKeeper, bk types.BankKeeper,
sk types.StakingKeeper, k keeper.Keeper,
) simulation.WeightedOperations {
Expand Down Expand Up @@ -48,7 +48,7 @@ func WeightedOperations(
),
simulation.NewWeightedOperation(
weightMsgClaimRewards,
SimulateMsgClaimRewards(cdc, ak, bk, sk, k),
SimulateMsgClaimRewards(cdc, ak, bk, k),
),
}
}
Expand Down Expand Up @@ -169,7 +169,7 @@ func SimulateMsgRedelegate(cdc *codec.ProtoCodec, ak types.AccountKeeper, bk typ
}
}

func SimulateMsgUndelegate(cdc *codec.ProtoCodec, ak types.AccountKeeper, bk types.BankKeeper, sk types.StakingKeeper, k keeper.Keeper) simtypes.Operation {
func SimulateMsgUndelegate(cdc *codec.ProtoCodec, ak types.AccountKeeper, bk types.BankKeeper, _ types.StakingKeeper, k keeper.Keeper) simtypes.Operation {
return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainId string) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
var delegations []types.Delegation
k.IterateDelegations(ctx, func(d types.Delegation) bool {
Expand Down Expand Up @@ -230,7 +230,7 @@ func SimulateMsgUndelegate(cdc *codec.ProtoCodec, ak types.AccountKeeper, bk typ
}
}

func SimulateMsgClaimRewards(cdc *codec.ProtoCodec, ak types.AccountKeeper, bk types.BankKeeper, sk types.StakingKeeper, k keeper.Keeper) simtypes.Operation {
func SimulateMsgClaimRewards(cdc *codec.ProtoCodec, ak types.AccountKeeper, bk types.BankKeeper, k keeper.Keeper) simtypes.Operation {
return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainId string) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
var delegations []types.Delegation
k.IterateDelegations(ctx, func(d types.Delegation) bool {
Expand Down
2 changes: 1 addition & 1 deletion x/alliance/tests/simulation/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/terra-money/alliance/x/alliance/types"
)

func ParamChanges(r *rand.Rand) []simtypes.ParamChange {
func ParamChanges() []simtypes.ParamChange {
return []simtypes.ParamChange{
simulation.NewSimParamChange(types.ModuleName, string(types.RewardDelayTime),
func(r *rand.Rand) string {
Expand Down
17 changes: 17 additions & 0 deletions x/alliance/types/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package types
import (
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/types"
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/msgservice"
authzcodec "github.com/cosmos/cosmos-sdk/x/authz/codec"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
)

Expand Down Expand Up @@ -34,3 +36,18 @@ func RegisterInterfaces(registry types.InterfaceRegistry) {
)
msgservice.RegisterMsgServiceDesc(registry, &_Msg_serviceDesc)
}

var (
amino = codec.NewLegacyAmino()
ModuleCdc = codec.NewAminoCodec(amino)
)

func init() {
RegisterLegacyAminoCodec(amino)
cryptocodec.RegisterCrypto(amino)
sdk.RegisterLegacyAminoCodec(amino)

// Register all Amino interfaces and concrete types on the authz Amino codec so that this can later be
// used to properly serialize MsgGrant and MsgExec instances
RegisterLegacyAminoCodec(authzcodec.Amino)
}
Loading

0 comments on commit c97b7c9

Please sign in to comment.