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

[PROTO-2211] Ensure all structs always have all field members present #536

Merged
merged 8 commits into from
Oct 11, 2024
Merged
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
3 changes: 2 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ linters:
- errcheck
- errname
- copyloopvar
- exhaustruct
- forcetypeassert
- goconst
- gofmt
Expand Down Expand Up @@ -103,4 +104,4 @@ issues:
- ".*\\.pb\\.go"
- ".*\\.pb\\.gw\\.go"
- ".*\\.pulsar\\.go"
- ".*_mocks\\.go"
- ".*_mocks\\.go"
2 changes: 1 addition & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func NewAlloraApp(
baseAppOptions ...func(*baseapp.BaseApp),
) (*AlloraApp, error) {
var (
app = &AlloraApp{}
app = &AlloraApp{} //nolint:exhaustruct
appBuilder *runtime.AppBuilder
)

Expand Down
2 changes: 1 addition & 1 deletion app/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (app *AlloraApp) ExportAppStateAndValidators(
modulesToExport []string,
) (servertypes.ExportedApp, error) {
// as if they could withdraw from the start of the next block
ctx := app.NewContextLegacy(true, tmproto.Header{Height: app.LastBlockHeight()})
ctx := app.NewContextLegacy(true, tmproto.Header{Height: app.LastBlockHeight()}) //nolint:exhaustruct

// We export at last height + 1, because that's the height at which
// CometBFT will start InitChain.
Expand Down
16 changes: 8 additions & 8 deletions app/ibc.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (app *AlloraApp) registerIBCModules() {

// register the key tables for legacy param subspaces
keyTable := ibcclienttypes.ParamKeyTable()
keyTable.RegisterParamSet(&ibcconnectiontypes.Params{})
keyTable.RegisterParamSet(&ibcconnectiontypes.Params{MaxExpectedTimePerBlock: 25000000000})
app.ParamsKeeper.Subspace(ibcexported.ModuleName).WithKeyTable(keyTable)
app.ParamsKeeper.Subspace(ibctransfertypes.ModuleName).WithKeyTable(ibctransfertypes.ParamKeyTable())
app.ParamsKeeper.Subspace(icacontrollertypes.SubModuleName).WithKeyTable(icacontrollertypes.ParamKeyTable())
Expand Down Expand Up @@ -177,7 +177,7 @@ func (app *AlloraApp) registerIBCModules() {
ibcfee.NewAppModule(app.IBCFeeKeeper),
icamodule.NewAppModule(&app.ICAControllerKeeper, &app.ICAHostKeeper),
capability.NewAppModule(app.appCodec, *app.CapabilityKeeper, false),
ibctm.AppModule{},
ibctm.AppModule{AppModuleBasic: ibctm.AppModuleBasic{}},
); err != nil {
panic(err)
}
Expand All @@ -188,12 +188,12 @@ func (app *AlloraApp) registerIBCModules() {
// This needs to be removed after IBC supports App Wiring.
func RegisterIBC(registry cdctypes.InterfaceRegistry) map[string]appmodule.AppModule {
modules := map[string]appmodule.AppModule{
ibcexported.ModuleName: ibc.AppModule{},
ibctransfertypes.ModuleName: ibctransfer.AppModule{},
ibcfeetypes.ModuleName: ibcfee.AppModule{},
icatypes.ModuleName: icamodule.AppModule{},
capabilitytypes.ModuleName: capability.AppModule{},
ibctm.ModuleName: ibctm.AppModule{},
ibcexported.ModuleName: ibc.AppModule{AppModuleBasic: ibc.AppModuleBasic{}},
ibctransfertypes.ModuleName: ibctransfer.AppModule{AppModuleBasic: ibctransfer.AppModuleBasic{}},
ibcfeetypes.ModuleName: ibcfee.AppModule{AppModuleBasic: ibcfee.AppModuleBasic{}},
icatypes.ModuleName: icamodule.AppModule{AppModuleBasic: icamodule.AppModuleBasic{}},
capabilitytypes.ModuleName: capability.AppModule{AppModuleBasic: capability.AppModuleBasic{}},
ibctm.ModuleName: ibctm.AppModule{AppModuleBasic: ibctm.AppModuleBasic{}},
}

sortedModuleKeys := alloraMath.GetSortedKeys(modules)
Expand Down
4 changes: 2 additions & 2 deletions cmd/allorad/cmd/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func initRootCmd(rootCmd *cobra.Command, txConfig client.TxConfig, basicManager
}

func queryCommand() *cobra.Command {
cmd := &cobra.Command{
cmd := &cobra.Command{ // nolint: exhaustruct // dependency code don't want to change the way it works
Use: "query",
Aliases: []string{"q"},
Short: "Querying subcommands",
Expand All @@ -71,7 +71,7 @@ func queryCommand() *cobra.Command {
}

func txCommand() *cobra.Command {
cmd := &cobra.Command{
cmd := &cobra.Command{ // nolint: exhaustruct // dependency code don't want to change the way it works
Use: "tx",
Short: "Transactions subcommands",
DisableFlagParsing: false,
Expand Down
20 changes: 10 additions & 10 deletions cmd/allorad/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func NewRootCmd() *cobra.Command {
panic(err)
}

rootCmd := &cobra.Command{
rootCmd := &cobra.Command{ // nolint: exhaustruct // dependency code don't want to change the way it works
Use: "allorad",
Short: "allorad - the Allora chain",
PersistentPreRunE: func(cmd *cobra.Command, _ []string) error {
Expand Down Expand Up @@ -136,15 +136,15 @@ func ProvideClientContext(
txConfig client.TxConfig,
legacyAmino *codec.LegacyAmino,
) client.Context {
clientCtx := client.Context{}.
WithCodec(appCodec).
WithInterfaceRegistry(interfaceRegistry).
WithTxConfig(txConfig).
WithLegacyAmino(legacyAmino).
WithInput(os.Stdin).
WithAccountRetriever(types.AccountRetriever{}).
WithHomeDir(app.DefaultNodeHome).
WithViper("ALLORA") // env variable prefix
clientCtx := client.Context{}. // nolint: exhaustruct // dependency code don't want to change the way it works
WithCodec(appCodec).
WithInterfaceRegistry(interfaceRegistry).
WithTxConfig(txConfig).
WithLegacyAmino(legacyAmino).
WithInput(os.Stdin).
WithAccountRetriever(types.AccountRetriever{}).
WithHomeDir(app.DefaultNodeHome).
WithViper("ALLORA") // env variable prefix

// Read the config again to overwrite the default values with the values from the config file
clientCtx, _ = config.ReadFromClientConfig(clientCtx)
Expand Down
13 changes: 10 additions & 3 deletions linter/maprange.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"flag"
"go/ast"
"go/types"

Expand All @@ -12,9 +13,15 @@ var AnalyzerPlugin = map[string]*analysis.Analyzer{
}

var Analyzer = &analysis.Analyzer{
Name: "maprange",
Doc: "check for range loops over maps",
Run: run,
Name: "maprange",
Doc: "check for range loops over maps",
Run: run,
URL: "",
Flags: flag.FlagSet{Usage: nil},
RunDespiteErrors: false,
Requires: nil,
ResultType: nil,
FactTypes: nil,
}

func New(conf any) ([]*analysis.Analyzer, error) {
Expand Down
13 changes: 11 additions & 2 deletions math/dec.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,20 @@ var dec128Context = apd.Context{
MaxExponent: apd.MaxExponent,
MinExponent: apd.MinExponent,
Traps: apd.DefaultTraps,
Rounding: apd.RoundDown,
}

// create a new Dec that represents NaN
func NewNaN() Dec {
return Dec{apd.Decimal{}, true}
return Dec{
apd.Decimal{
Form: apd.Finite,
Negative: false,
Exponent: 0,
Coeff: *apd.NewBigInt(0),
},
true,
}
}

// NewDecFromString returns a new Dec from a given string. It returns an error if the string
Expand Down Expand Up @@ -754,7 +763,7 @@ func (x Dec) NumDecimalPlaces() uint32 {
// Reduce returns a copy of x with all trailing zeros removed and the number
// of trailing zeros removed.
func (x Dec) Reduce() (Dec, int) {
y := Dec{}
y := ZeroDec()
_, n := y.dec.Reduce(&x.dec)
return y, n
}
65 changes: 38 additions & 27 deletions test/common/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,25 +58,21 @@ func NewClient(
seed int64,
) Client {
t.Helper()
client := Client{}
ctx := context.Background()
client.RpcConnectionType = rpcConnectionType
client.Clients = make([]cosmosclient.Client, len(nodeRpcAddresses))
client.QueryAuths = make([]authtypes.QueryClient, len(nodeRpcAddresses))
client.QueryBanks = make([]banktypes.QueryClient, len(nodeRpcAddresses))
client.QueryDistributions = make([]distributiontypes.QueryClient, len(nodeRpcAddresses))
client.QueryEmissionses = make([]emissionstypes.QueryServiceClient, len(nodeRpcAddresses))
client.QueryMints = make([]minttypes.QueryClient, len(nodeRpcAddresses))
client.QueryGovs = make([]govtypesv1.QueryClient, len(nodeRpcAddresses))
client.QueryUpgrades = make([]upgradetypes.QueryClient, len(nodeRpcAddresses))
client.RpcCounterMutex = &sync.Mutex{}
client.accountRegistryMutex = &sync.Mutex{}
client.RpcCounterSeed = 0

clients := make([]cosmosclient.Client, len(nodeRpcAddresses))
queryAuths := make([]authtypes.QueryClient, len(nodeRpcAddresses))
queryBanks := make([]banktypes.QueryClient, len(nodeRpcAddresses))
queryDistributions := make([]distributiontypes.QueryClient, len(nodeRpcAddresses))
queryEmissionses := make([]emissionstypes.QueryServiceClient, len(nodeRpcAddresses))
queryMints := make([]minttypes.QueryClient, len(nodeRpcAddresses))
queryGovs := make([]govtypesv1.QueryClient, len(nodeRpcAddresses))
queryUpgrades := make([]upgradetypes.QueryClient, len(nodeRpcAddresses))

rpcCounterSeed := int64(0)
if rpcConnectionType == RandomBasedOnDeterministicSeed {
client.RpcCounterSeed = seed
rpcCounterSeed = seed
}
client.Rand = rand.New(rand.NewSource(seed)) //nolint:gosec // G404: Use of weak random number generator (math/rand or math/rand/v2 instead of crypto/rand)

for i, rpcAddress := range nodeRpcAddresses {
cosmosClient, err := cosmosclient.New(
ctx,
Expand All @@ -89,27 +85,42 @@ func NewClient(
require.NoError(t, err)
ccCtx := cosmosClient.Context()

client.Clients[i] = cosmosClient
client.QueryAuths[i] = authtypes.NewQueryClient(ccCtx)
client.QueryBanks[i] = banktypes.NewQueryClient(ccCtx)
client.QueryDistributions[i] = distributiontypes.NewQueryClient(ccCtx)
client.QueryEmissionses[i] = emissionstypes.NewQueryServiceClient(ccCtx)
client.QueryMints[i] = minttypes.NewQueryClient(ccCtx)
client.QueryGovs[i] = govtypesv1.NewQueryClient(ccCtx)
client.QueryUpgrades[i] = upgradetypes.NewQueryClient(ccCtx)
clients[i] = cosmosClient
queryAuths[i] = authtypes.NewQueryClient(ccCtx)
queryBanks[i] = banktypes.NewQueryClient(ccCtx)
queryDistributions[i] = distributiontypes.NewQueryClient(ccCtx)
queryEmissionses[i] = emissionstypes.NewQueryServiceClient(ccCtx)
queryMints[i] = minttypes.NewQueryClient(ccCtx)
queryGovs[i] = govtypesv1.NewQueryClient(ccCtx)
queryUpgrades[i] = upgradetypes.NewQueryClient(ccCtx)

// this is terrible, no isConnected as part of this code path
require.NotEqual(t, "", ccCtx.ChainID)
}

var err error
client.accountRegistry, err = cosmosaccount.New(
accountRegistry, err := cosmosaccount.New(
cosmosaccount.WithKeyringServiceName(sdktypes.KeyringServiceName()),
cosmosaccount.WithKeyringBackend(cosmosaccount.KeyringTest),
cosmosaccount.WithHome(alloraHomeDir),
)
require.NoError(t, err)
return client

return Client{
RpcConnectionType: rpcConnectionType,
Clients: clients,
QueryAuths: queryAuths,
QueryBanks: queryBanks,
QueryDistributions: queryDistributions,
QueryEmissionses: queryEmissionses,
QueryMints: queryMints,
QueryGovs: queryGovs,
QueryUpgrades: queryUpgrades,
RpcCounterMutex: &sync.Mutex{},
RpcCounterSeed: rpcCounterSeed,
accountRegistry: accountRegistry,
accountRegistryMutex: &sync.Mutex{},
Rand: rand.New(rand.NewSource(seed)), //nolint:gosec // G404: Use of weak random number generator (math/rand or math/rand/v2 instead of crypto/rand)
}
}

func (c *Client) getNextClientNumber() int64 {
Expand Down
70 changes: 42 additions & 28 deletions test/common/node_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,7 @@ func NewTestConfig(
seed int,
) TestConfig {
t.Helper()
nodeConfig := TestConfig{}
var err error
nodeConfig.T = t
nodeConfig.AlloraHomeDir = alloraHomeDir
nodeConfig.Seed = seed
if rpcConnectionType == SingleRpc {
require.Len(t, nodeRpcAddresses, 1, "must have exactly one rpc address")
} else { // RoundRobin or RandomBasedOnDeterministicSeed
Expand All @@ -66,47 +62,65 @@ func NewTestConfig(
alloraHomeDir,
int64(seed),
)
nodeConfig.Client = client
//// restore from mnemonic
nodeConfig.FaucetAcc, err = client.Clients[0].AccountRegistry.GetByName("faucet")
faucetAcc, err := client.Clients[0].AccountRegistry.GetByName("faucet")
require.NoError(t, err)
nodeConfig.UpshotAcc, err = client.Clients[0].AccountRegistry.GetByName("upshot")
upshotAcc, err := client.Clients[0].AccountRegistry.GetByName("upshot")
require.NoError(t, err)
nodeConfig.AliceAcc, err = client.Clients[0].AccountRegistry.GetByName("faucet")
aliceAcc, err := client.Clients[0].AccountRegistry.GetByName("faucet")
require.NoError(t, err)
nodeConfig.BobAcc, err = client.Clients[0].AccountRegistry.GetByName("upshot")
bobAcc, err := client.Clients[0].AccountRegistry.GetByName("upshot")
require.NoError(t, err)
nodeConfig.Validator0Acc, err = client.Clients[0].AccountRegistry.GetByName("validator0")
validator0Acc, err := client.Clients[0].AccountRegistry.GetByName("validator0")
require.NoError(t, err)
nodeConfig.Validator1Acc, err = client.Clients[0].AccountRegistry.GetByName("validator1")
validator1Acc, err := client.Clients[0].AccountRegistry.GetByName("validator1")
require.NoError(t, err)
nodeConfig.Validator2Acc, err = client.Clients[0].AccountRegistry.GetByName("validator2")
validator2Acc, err := client.Clients[0].AccountRegistry.GetByName("validator2")
require.NoError(t, err)
nodeConfig.FaucetAddr, err = nodeConfig.FaucetAcc.Address(params.HumanCoinUnit)
faucetAddr, err := faucetAcc.Address(params.HumanCoinUnit)
require.NoError(t, err)
nodeConfig.UpshotAddr, err = nodeConfig.UpshotAcc.Address(params.HumanCoinUnit)
upshotAddr, err := upshotAcc.Address(params.HumanCoinUnit)
require.NoError(t, err)
nodeConfig.AliceAddr, err = nodeConfig.AliceAcc.Address(params.HumanCoinUnit)
aliceAddr, err := aliceAcc.Address(params.HumanCoinUnit)
require.NoError(t, err)
nodeConfig.BobAddr, err = nodeConfig.BobAcc.Address(params.HumanCoinUnit)
bobAddr, err := bobAcc.Address(params.HumanCoinUnit)
require.NoError(t, err)
nodeConfig.Validator0Addr, err = nodeConfig.Validator0Acc.Address(params.HumanCoinUnit)
validator0Addr, err := validator0Acc.Address(params.HumanCoinUnit)
require.NoError(t, err)
nodeConfig.Validator1Addr, err = nodeConfig.Validator1Acc.Address(params.HumanCoinUnit)
validator1Addr, err := validator1Acc.Address(params.HumanCoinUnit)
require.NoError(t, err)
nodeConfig.Validator2Addr, err = nodeConfig.Validator2Acc.Address(params.HumanCoinUnit)
validator2Addr, err := validator2Acc.Address(params.HumanCoinUnit)
require.NoError(t, err)

encCfg := moduletestutil.MakeTestEncodingConfig(
mint.AppModuleBasic{},
emissions.AppModule{},
auth.AppModule{},
bank.AppModule{},
distribution.AppModule{},
gov.AppModule{},
upgrade.AppModule{},
mint.AppModuleBasic{}, //nolint:exhaustruct
emissions.AppModule{}, //nolint:exhaustruct
auth.AppModule{}, //nolint:exhaustruct
bank.AppModule{}, //nolint:exhaustruct
distribution.AppModule{}, //nolint:exhaustruct
gov.AppModule{}, //nolint:exhaustruct
upgrade.AppModule{}, //nolint:exhaustruct
)
nodeConfig.Cdc = codec.NewProtoCodec(encCfg.InterfaceRegistry)

return nodeConfig
return TestConfig{
T: t,
AlloraHomeDir: alloraHomeDir,
Seed: seed,
Client: client,
Cdc: codec.NewProtoCodec(encCfg.InterfaceRegistry),
FaucetAcc: faucetAcc,
FaucetAddr: faucetAddr,
UpshotAcc: upshotAcc,
UpshotAddr: upshotAddr,
AliceAcc: aliceAcc,
AliceAddr: aliceAddr,
BobAcc: bobAcc,
BobAddr: bobAddr,
Validator0Acc: validator0Acc,
Validator0Addr: validator0Addr,
Validator1Acc: validator1Acc,
Validator1Addr: validator1Addr,
Validator2Acc: validator2Acc,
Validator2Addr: validator2Addr,
}
}
2 changes: 1 addition & 1 deletion test/integration/create_topic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func CreateTopic(m testCommon.TestConfig) (topicId uint64) {
require.NoError(m.T, err)
_, err = m.Client.WaitForTx(ctx, txResp.TxHash)
require.NoError(m.T, err)
createTopicResponse := &emissionstypes.CreateNewTopicResponse{}
createTopicResponse := &emissionstypes.CreateNewTopicResponse{} //nolint:exhaustruct // the fields are populated by decode
err = txResp.Decode(createTopicResponse)
require.NoError(m.T, err)
topicId = createTopicResponse.TopicId
Expand Down
Loading