app.go
# app.go
package app
import (
“io”
“os”
“github.com/cosmos/cosmos-sdk/baseapp” “github.com/cosmos/cosmos-sdk/codec” “github.com/cosmos/cosmos-sdk/codec/types” “github.com/cosmos/cosmos-sdk/runtime” store “github.com/cosmos/cosmos-sdk/store/types” “github.com/cosmos/cosmos-sdk/std” sdk “github.com/cosmos/cosmos-sdk/types” “github.com/cosmos/cosmos-sdk/types/module” “github.com/cosmos/cosmos-sdk/x/auth” authkeeper “github.com/cosmos/cosmos-sdk/x/auth/keeper” authtypes “github.com/cosmos/cosmos-sdk/x/auth/types” “github.com/cosmos/cosmos-sdk/x/authz” authzkeeper “github.com/cosmos/cosmos-sdk/x/authz/keeper” authzmodule “github.com/cosmos/cosmos-sdk/x/authz/module” bank “github.com/cosmos/cosmos-sdk/x/bank” bankkeeper “github.com/cosmos/cosmos-sdk/x/bank/keeper” banktypes “github.com/cosmos/cosmos-sdk/x/bank/types” “github.com/cosmos/cosmos-sdk/x/capability” capabilitykeeper “github.com/cosmos/cosmos-sdk/x/capability/keeper” capabilitytypes “github.com/cosmos/cosmos-sdk/x/capability/types” “github.com/cosmos/cosmos-sdk/x/crisis” crisiskeeper “github.com/cosmos/cosmos-sdk/x/crisis/keeper” crisistypes “github.com/cosmos/cosmos-sdk/x/crisis/types” “github.com/cosmos/cosmos-sdk/x/distribution” distrkeeper “github.com/cosmos/cosmos-sdk/x/distribution/keeper” distrtypes “github.com/cosmos/cosmos-sdk/x/distribution/types” “github.com/cosmos/cosmos-sdk/x/evidence” evidencekeeper “github.com/cosmos/cosmos-sdk/x/evidence/keeper” evidencetypes “github.com/cosmos/cosmos-sdk/x/evidence/types” “github.com/cosmos/cosmos-sdk/x/genutil” genutiltypes “github.com/cosmos/cosmos-sdk/x/genutil/types” “github.com/cosmos/cosmos-sdk/x/gov” govkeeper “github.com/cosmos/cosmos-sdk/x/gov/keeper” govtypes “github.com/cosmos/cosmos-sdk/x/gov/types” “github.com/cosmos/cosmos-sdk/x/mint” mintkeeper “github.com/cosmos/cosmos-sdk/x/mint/keeper” minttypes “github.com/cosmos/cosmos-sdk/x/mint/types” “github.com/cosmos/cosmos-sdk/x/params” paramskeeper “github.com/cosmos/cosmos-sdk/x/params/keeper” paramstypes “github.com/cosmos/cosmos-sdk/x/params/types” slashing “github.com/cosmos/cosmos-sdk/x/slashing” slashingkeeper “github.com/cosmos/cosmos-sdk/x/slashing/keeper” slashingtypes “github.com/cosmos/cosmos-sdk/x/slashing/types” “github.com/cosmos/cosmos-sdk/x/staking” stakingkeeper “github.com/cosmos/cosmos-sdk/x/staking/keeper” stakingtypes “github.com/cosmos/cosmos-sdk/x/staking/types” “github.com/cosmos/cosmos-sdk/x/upgrade” upgradekeeper “github.com/cosmos/cosmos-sdk/x/upgrade/keeper” upgradetypes “github.com/cosmos/cosmos-sdk/x/upgrade/types” ibc “github.com/cosmos/ibc-go/v3/modules/core” ibckeeper “github.com/cosmos/ibc-go/v3/modules/core/keeper” ibchost “github.com/cosmos/ibc-go/v3/modules/core/types” ibctransfer “github.com/cosmos/ibc-go/v3/modules/apps/transfer” ibctransferkeeper “github.com/cosmos/ibc-go/v3/modules/apps/transfer/keeper” ibctransfertypes “github.com/cosmos/ibc-go/v3/modules/apps/transfer/types” // Moduli personalizzati registrationmodule “github.com/your_username/experience/x/registration” registrationkeeper “github.com/your_username/experience/x/registration/keeper” registrationtypes “github.com/your_username/experience/x/registration/types” expmodule “github.com/your_username/experience/x/exp” expkeeper “github.com/your_username/experience/x/exp/keeper” exptypes “github.com/your_username/experience/x/exp/types” therapymodule “github.com/your_username/experience/x/therapy” therapykeeper “github.com/your_username/experience/x/therapy/keeper” therapytypes “github.com/your_username/experience/x/therapy/types” // Import necessari per il logger e database “github.com/tendermint/tendermint/libs/log” dbm “github.com/tendermint/tm-db”
)
const AppName = “ExperienceApp”
var (
// DefaultNodeHome indica la directory di default per l’applicazione
DefaultNodeHome = os.ExpandEnv(“$HOME/.experienceapp”)
// ModuleBasics definisce il manager di base per tutti i moduli ModuleBasics = module.NewBasicManager( auth.AppModuleBasic{}, genutil.AppModuleBasic{}, bank.AppModuleBasic{}, capability.AppModuleBasic{}, staking.AppModuleBasic{}, mint.AppModuleBasic{}, distribution.AppModuleBasic{}, gov.AppModuleBasic{}, params.AppModuleBasic{}, crisis.AppModuleBasic{}, slashing.AppModuleBasic{}, upgradekeeper.AppModuleBasic{}, evidence.AppModuleBasic{}, ibc.AppModuleBasic{}, ibctransfer.AppModuleBasic{}, authzmodule.AppModuleBasic{}, // Moduli personalizzati registrationmodule.AppModuleBasic{}, expmodule.AppModuleBasic{}, therapymodule.AppModuleBasic{}, ) // ModuleAccountPermissions definisce le autorizzazioni per gli account dei moduli ModuleAccountPermissions = map[string][]string{ authtypes.FeeCollectorName: nil, distrtypes.ModuleName: nil, minttypes.ModuleName: {authtypes.Minter}, stakingtypes.BondedPoolName: {authtypes.Burner, authtypes.Staking}, stakingtypes.NotBondedPoolName: {authtypes.Burner, authtypes.Staking}, govtypes.ModuleName: {authtypes.Burner}, ibctransfertypes.ModuleName: {authtypes.Minter, authtypes.Burner}, }
)
type App struct {
*baseapp.BaseApp
cdc *codec.LegacyAmino
appCodec codec.Codec
interfaceRegistry types.InterfaceRegistry
// Keeper del modulo Cosmos SDK AccountKeeper authkeeper.AccountKeeper BankKeeper bankkeeper.Keeper StakingKeeper stakingkeeper.Keeper SlashingKeeper slashingkeeper.Keeper MintKeeper mintkeeper.Keeper DistrKeeper distrkeeper.Keeper GovKeeper govkeeper.Keeper CrisisKeeper crisiskeeper.Keeper UpgradeKeeper upgradekeeper.Keeper ParamsKeeper paramskeeper.Keeper EvidenceKeeper evidencekeeper.Keeper IBCKeeper ibckeeper.Keeper TransferKeeper ibctransferkeeper.Keeper CapabilityKeeper *capabilitykeeper.Keeper AuthzKeeper authzkeeper.Keeper // Keeper dei moduli personalizzati RegistrationKeeper registrationkeeper.Keeper ExpKeeper expkeeper.Keeper TherapyKeeper therapykeeper.Keeper // Moduli mm *module.Manager sm *module.SimulationManager
}
func NewApp(
logger log.Logger,
db dbm.DB,
traceStore io.Writer,
loadLatest bool,
skipUpgradeHeights map[int64]bool,
homePath string,
invCheckPeriod uint,
encodingConfig runtime.EncodingConfig,
appOpts sdk.AppOptions,
baseAppOptions …func(*baseapp.BaseApp),
) *App {
appCodec := encodingConfig.Marshaler
cdc := encodingConfig.Amino
interfaceRegistry := encodingConfig.InterfaceRegistry
bApp := baseapp.NewBaseApp(AppName, logger, db, encodingConfig.TxConfig.TxDecoder(), baseAppOptions…) bApp.SetCommitMultiStoreTracer(traceStore) bApp.SetVersion(version.Version) bApp.SetInterfaceRegistry(interfaceRegistry) keys := sdk.NewKVStoreKeys( authtypes.StoreKey, banktypes.StoreKey, stakingtypes.StoreKey, minttypes.StoreKey, distrtypes.StoreKey, slashingtypes.StoreKey, govtypes.StoreKey, paramstypes.StoreKey, upgradetypes.StoreKey, evidencetypes.StoreKey, ibchost.StoreKey, ibctransfertypes.StoreKey, capabilitytypes.StoreKey, registrationtypes.StoreKey, exptypes.StoreKey, therapytypes.StoreKey, ) app := &App{ BaseApp: bApp, cdc: cdc, appCodec: appCodec, interfaceRegistry: interfaceRegistry, AccountKeeper: authkeeper.NewAccountKeeper(appCodec, keys[authtypes.StoreKey], authtypes.ProtoBaseAccount, ModuleAccountPermissions), BankKeeper: bankkeeper.NewBaseKeeper(appCodec, keys[banktypes.StoreKey], app.AccountKeeper, app.ParamsKeeper.Subspace(banktypes.ModuleName)), StakingKeeper: stakingkeeper.NewKeeper(appCodec, keys[stakingtypes.StoreKey], app.AccountKeeper, app.BankKeeper, app.ParamsKeeper.Subspace(stakingtypes.ModuleName)), SlashingKeeper: slashingkeeper.NewKeeper(appCodec, keys[slashingtypes.StoreKey], &app.StakingKeeper, app.ParamsKeeper.Subspace(slashingtypes.ModuleName)), MintKeeper: mintkeeper.NewKeeper(appCodec, keys[minttypes.StoreKey], app.ParamsKeeper.Subspace(minttypes.ModuleName), &app.StakingKeeper, app.AccountKeeper), DistrKeeper: distrkeeper.NewKeeper(appCodec, keys[distrtypes.StoreKey], app.ParamsKeeper.Subspace(distrtypes.ModuleName), app.AccountKeeper, app.BankKeeper, &app.StakingKeeper, authtypes.FeeCollectorName), GovKeeper: govkeeper.NewKeeper(appCodec, keys[govtypes.StoreKey], app.ParamsKeeper.Subspace(govtypes.ModuleName), app.AccountKeeper, app.BankKeeper), CrisisKeeper: crisiskeeper.NewKeeper(appCodec, keys[crisistypes.StoreKey], invCheckPeriod, app.BankKeeper, authtypes.FeeCollectorName), UpgradeKeeper: upgradekeeper.NewKeeper(skipUpgradeHeights, keys[upgradetypes.StoreKey], appCodec, homePath), EvidenceKeeper: evidencekeeper.NewKeeper(appCodec, keys[evidencetypes.StoreKey], &app.StakingKeeper, app.AccountKeeper, app.BankKeeper), IBCKeeper: ibckeeper.NewKeeper(appCodec, keys[ibchost.StoreKey], app.StakingKeeper, app.UpgradeKeeper, app.AccountKeeper, app.BankKeeper), TransferKeeper: ibctransferkeeper.NewKeeper(appCodec, keys[ibctransfertypes.StoreKey], app.IBCKeeper.ChannelKeeper, &app.IBCKeeper.PortKeeper, app.AccountKeeper, app.BankKeeper, scopedTransferKeeper), CapabilityKeeper: capabilitykeeper.NewKeeper(appCodec, keys[capabilitytypes.StoreKey], keys[capabilitytypes.MemStoreKey]), AuthzKeeper: authzkeeper.NewKeeper(keys[authz.ModuleName], appCodec, bApp.MsgServiceRouter(), app.AccountKeeper), RegistrationKeeper: registrationkeeper.NewKeeper( appCodec, keys[registrationtypes.StoreKey], app.AccountKeeper, ), ExpKeeper: expkeeper.NewKeeper( appCodec, keys[exptypes.StoreKey], app.BankKeeper, ), TherapyKeeper: therapykeeper.NewKeeper( appCodec, keys[therapytypes.StoreKey], app.AccountKeeper, ), } // Creazione dei router e dei moduli app.mm = module.NewManager( genutil.NewAppModule(app.AccountKeeper, app.StakingKeeper, app.BaseApp.DeliverTx, encodingConfig.TxConfig), auth.NewAppModule(appCodec, app.AccountKeeper, nil), authzmodule.NewAppModule(appCodec, app.AuthzKeeper, app.AccountKeeper, app.BankKeeper), bank.NewAppModule(appCodec, app.BankKeeper, app.AccountKeeper), capability.NewAppModule(appCodec, *app.CapabilityKeeper), crisis.NewAppModule(&app.CrisisKeeper, invCheckPeriod), distribution.NewAppModule(appCodec, app.DistrKeeper, app.AccountKeeper, app.BankKeeper, app.StakingKeeper), evidence.NewAppModule(appCodec, app.EvidenceKeeper), gov.NewAppModule(appCodec, app.GovKeeper, app.AccountKeeper, app.BankKeeper), mint.NewAppModule(appCodec, app.MintKeeper, app.AccountKeeper), slashing.NewAppModule(appCodec, app.SlashingKeeper, app.AccountKeeper, app.BankKeeper, app.StakingKeeper), staking.NewAppModule(appCodec, app.StakingKeeper, app.AccountKeeper, app.BankKeeper), ibc.NewAppModule(app.IBCKeeper), ibctransfer.NewAppModule(app.TransferKeeper), upgrade.NewAppModule(app.UpgradeKeeper), registrationmodule.NewAppModule(appCodec, app.RegistrationKeeper), expmodule.NewAppModule(appCodec, app.ExpKeeper), therapymodule.NewAppModule(appCodec, app.TherapyKeeper), ) app.sm = module.NewSimulationManager( auth.NewAppModule(appCodec, app.AccountKeeper, authsims.RandomGenesisAccounts), authzmodule.NewAppModule(appCodec, app.AuthzKeeper, app.AccountKeeper, app.BankKeeper), bank.NewAppModule(appCodec, app.BankKeeper, app.AccountKeeper), capability.NewAppModule(appCodec, *app.CapabilityKeeper), distribution.NewAppModule(appCodec, app.DistrKeeper, app.AccountKeeper, app.BankKeeper, app.StakingKeeper), evidence.NewAppModule(appCodec, app.EvidenceKeeper), gov.NewAppModule(appCodec, app.GovKeeper, app.AccountKeeper, app.BankKeeper), mint.NewAppModule(appCodec, app.MintKeeper, app.AccountKeeper), slashing.NewAppModule(appCodec, app.SlashingKeeper, app.AccountKeeper, app.BankKeeper, app.StakingKeeper), staking.NewAppModule(appCodec, app.StakingKeeper, app.AccountKeeper, app.BankKeeper), ibc.NewAppModule(app.IBCKeeper), ibctransfer.NewAppModule(app.TransferKeeper), upgrade.NewAppModule(app.UpgradeKeeper), registrationmodule.NewAppModule(appCodec, app.RegistrationKeeper), expmodule.NewAppModule(appCodec, app.ExpKeeper), therapymodule.NewAppModule(appCodec, app.TherapyKeeper), ) // Inizializzare il rounter di base per tutte le transazioni app.mm.SetOrderBeginBlockers( upgradetypes.ModuleName, capabilitytypes.ModuleName, crisistypes.ModuleName, genutiltypes.ModuleName, banktypes.ModuleName, stakingtypes.ModuleName, distribution.ModuleName, slashingtypes.ModuleName, govtypes.ModuleName, ibctransfertypes.ModuleName, evidencetypes.ModuleName, ibchost.ModuleName, authz.ModuleName, paramstypes.ModuleName, // Moduli personalizzati registrationtypes.ModuleName, exptypes.ModuleName, therapytypes.ModuleName, ) // Aggiornamento del router per aggiungere tutte le route dei moduli app.Router().AddRoute(banktypes.ModuleName, bank.NewHandler(app.BankKeeper)). AddRoute(authz.ModuleName, authz.NewHandler(app.AuthzKeeper)). AddRoute(registrationtypes.ModuleName, registrationmodule.NewHandler(app.RegistrationKeeper)). AddRoute(exptypes.ModuleName, expmodule.NewHandler(app.ExpKeeper)). AddRoute(therapytypes.ModuleName, therapymodule.NewHandler(app.TherapyKeeper)) // Assicurare che tutto sia conforme alle specifiche if err := app.LoadLatestVersion(); err != nil { panic(err) } return app
}
// Descrizione Dettagliata del File app.go
// Gestione degli Errori: Assicura che ogni passaggio dell’inizializzazione sia gestito correttamente e che eventuali errori nel caricamento dell’ultima versione della blockchain siano gestiti con un panic.
// Importazioni: Include tutti i pacchetti richiesti da Cosmos SDK e i moduli personalizzati specifici per la blockchain Experience (registration
, exp
, therapy
).
// Costanti e Variabili Globali: Definisce le costanti globali come AppName
e DefaultNodeHome
e imposta ModuleBasics
e ModuleAccountPermissions
per la configurazione dei moduli.
// Struttura App
: Definisce la struttura App
che contiene tutti i keeper dei moduli standard di Cosmos SDK e quelli personalizzati. Include anche il Module Manager
(mm
) e il Simulation Manager
(sm
).
// Funzione NewApp
: Inizializza l’applicazione, configura i keeper per ogni modulo e imposta il manager dei moduli. Configura il router per gestire le transazioni e aggiunge tutti i moduli necessari al Module Manager
e al Simulation Manager
.
// Gestione degli Errori: Assicura che ogni passaggio dell’inizializzazione sia gestito correttamente e che eventuali errori nel caricamento dell’ultima versione della blockchain siano gestiti con un panic.