diff --git a/app/app.go b/app/app.go index fdb8289e..2a009762 100644 --- a/app/app.go +++ b/app/app.go @@ -125,10 +125,10 @@ import ( palomamempool "github.com/palomachain/paloma/app/mempool" appparams "github.com/palomachain/paloma/app/params" - // xchain "github.com/palomachain/paloma/internal/x-chain" - // consensusmodule "github.com/palomachain/paloma/x/consensus" - // consensusmodulekeeper "github.com/palomachain/paloma/x/consensus/keeper" - // consensusmoduletypes "github.com/palomachain/paloma/x/consensus/types" + consensusmodule "github.com/palomachain/paloma/x/consensus" + consensusmodulekeeper "github.com/palomachain/paloma/x/consensus/keeper" + consensusmoduletypes "github.com/palomachain/paloma/x/consensus/types" + gravitymodule "github.com/palomachain/paloma/x/gravity" gravityclient "github.com/palomachain/paloma/x/gravity/client" gravitymodulekeeper "github.com/palomachain/paloma/x/gravity/keeper" @@ -198,14 +198,14 @@ var ( evidence.AppModuleBasic{}, vesting.AppModuleBasic{}, schedulermodule.AppModuleBasic{}, - // consensusmodule.AppModuleBasic{}, + consensusmodule.AppModuleBasic{}, // valsetmodule.AppModuleBasic{}, wasm.AppModuleBasic{}, evm.AppModuleBasic{}, gravitymodule.AppModuleBasic{}, // palomamodule.AppModuleBasic{}, // treasurymodule.AppModuleBasic{}, - // consensus.AppModuleBasic{}, + consensus.AppModuleBasic{}, transfer.AppModuleBasic{}, ibc.AppModuleBasic{}, ica.AppModuleBasic{}, @@ -285,7 +285,7 @@ type App struct { TransferKeeper ibctransferkeeper.Keeper SchedulerKeeper schedulermodulekeeper.Keeper - // ConsensusKeeper consensusmodulekeeper.Keeper + ConsensusKeeper consensusmodulekeeper.Keeper // ValsetKeeper valsetmodulekeeper.Keeper // PalomaKeeper palomamodulekeeper.Keeper // TreasuryKeeper treasurymodulekeeper.Keeper @@ -293,7 +293,7 @@ type App struct { wasmKeeper wasmkeeper.Keeper EvmKeeper evmmodulekeeper.Keeper - // mm is the module manager + // ModuleManager is the module manager ModuleManager *module.Manager BasicModuleManager module.BasicManager @@ -363,7 +363,7 @@ func New( icacontrollertypes.StoreKey, capabilitytypes.StoreKey, schedulermoduletypes.StoreKey, - // consensusmoduletypes.StoreKey, + consensusmoduletypes.StoreKey, // valsetmoduletypes.StoreKey, // treasurymoduletypes.StoreKey, evmmoduletypes.StoreKey, @@ -376,7 +376,7 @@ func New( memKeys := storetypes.NewMemoryStoreKeys( capabilitytypes.MemStoreKey, // valsetmoduletypes.MemStoreKey, - // consensusmoduletypes.MemStoreKey, + consensusmoduletypes.MemStoreKey, evmmoduletypes.MemStoreKey, schedulermoduletypes.MemStoreKey, // treasurymoduletypes.MemStoreKey, @@ -415,7 +415,7 @@ func New( scopedICAControllerKeeper := app.CapabilityKeeper.ScopeToModule(icacontrollertypes.SubModuleName) scopedTransferKeeper := app.CapabilityKeeper.ScopeToModule(ibctransfertypes.ModuleName) scopedWasmKeeper := app.CapabilityKeeper.ScopeToModule(wasmtypes.ModuleName) - // scopedConsensusKeeper := app.CapabilityKeeper.ScopeToModule(consensusmoduletypes.ModuleName) + scopedConsensusKeeper := app.CapabilityKeeper.ScopeToModule(consensusmoduletypes.ModuleName) app.CapabilityKeeper.Seal() @@ -575,16 +575,15 @@ func New( // sdk.DefaultPowerReduction, // ) - // consensusRegistry := consensusmodulekeeper.NewRegistry() + consensusRegistry := consensusmodulekeeper.NewRegistry() - // app.ConsensusKeeper = *consensusmodulekeeper.NewKeeper( - // appCodec, - // keys[consensusmoduletypes.StoreKey], - // memKeys[consensusmoduletypes.MemStoreKey], - // app.GetSubspace(consensusmoduletypes.ModuleName), - // app.ValsetKeeper, - // consensusRegistry, - // ) + app.ConsensusKeeper = *consensusmodulekeeper.NewKeeper( + appCodec, + runtime.NewKVStoreService(keys[consensusmoduletypes.StoreKey]), + app.GetSubspace(consensusmoduletypes.ModuleName), + nil, // TODO: pass valset keeper + consensusRegistry, + ) app.EvmKeeper = *evmmodulekeeper.NewKeeper( appCodec, @@ -657,7 +656,7 @@ func New( // nil, // app.SchedulerKeeper, // ) - // app.ScopedConsensusKeeper = scopedConsensusKeeper + app.ScopedConsensusKeeper = scopedConsensusKeeper govRouter := govv1beta1.NewRouter() govRouter. @@ -772,7 +771,7 @@ func New( // must be passed by reference here. evmModule := evm.NewAppModule(appCodec, app.EvmKeeper, app.AccountKeeper, app.BankKeeper) - // consensusModule := consensusmodule.NewAppModule(appCodec, app.ConsensusKeeper, app.AccountKeeper, app.BankKeeper) + consensusModule := consensusmodule.NewAppModule(appCodec, app.ConsensusKeeper, app.AccountKeeper, app.BankKeeper) // valsetModule := valsetmodule.NewAppModule(appCodec, app.ValsetKeeper, app.AccountKeeper, app.BankKeeper) schedulerModule := schedulermodule.NewAppModule(appCodec, app.SchedulerKeeper, app.AccountKeeper, app.BankKeeper) // palomaModule := palomamodule.NewAppModule(appCodec, app.PalomaKeeper, app.AccountKeeper, app.BankKeeper) @@ -800,7 +799,7 @@ func New( evidence.NewAppModule(app.EvidenceKeeper), params.NewAppModule(app.ParamsKeeper), schedulerModule, - // consensusModule, + consensusModule, // valsetModule, evmModule, gravityModule, @@ -840,7 +839,7 @@ func New( stakingtypes.ModuleName, feegrant.ModuleName, schedulermoduletypes.ModuleName, - // consensusmoduletypes.ModuleName, + consensusmoduletypes.ModuleName, govtypes.ModuleName, crisistypes.ModuleName, banktypes.ModuleName, @@ -873,7 +872,7 @@ func New( slashingtypes.ModuleName, evidencetypes.ModuleName, feegrant.ModuleName, - // consensusmoduletypes.ModuleName, + consensusmoduletypes.ModuleName, crisistypes.ModuleName, banktypes.ModuleName, paramstypes.ModuleName, @@ -916,7 +915,7 @@ func New( evidencetypes.ModuleName, ibctransfertypes.ModuleName, schedulermoduletypes.ModuleName, - // consensusmoduletypes.ModuleName, + consensusmoduletypes.ModuleName, // valsetmoduletypes.ModuleName, // palomamoduletypes.ModuleName, ibctransfertypes.ModuleName, @@ -1009,9 +1008,9 @@ func New( } } - // consensusRegistry.Add( - // app.EvmKeeper, - // ) + consensusRegistry.Add( + app.EvmKeeper, + ) return app } @@ -1170,7 +1169,7 @@ func initParamsKeeper(appCodec codec.BinaryCodec, legacyAmino *codec.LegacyAmino paramsKeeper.Subspace(icahosttypes.SubModuleName) paramsKeeper.Subspace(icacontrollertypes.SubModuleName) paramsKeeper.Subspace(schedulermoduletypes.ModuleName) - // paramsKeeper.Subspace(consensusmoduletypes.ModuleName) + paramsKeeper.Subspace(consensusmoduletypes.ModuleName) // paramsKeeper.Subspace(valsetmoduletypes.ModuleName) paramsKeeper.Subspace(wasmtypes.ModuleName) paramsKeeper.Subspace(evmmoduletypes.ModuleName) diff --git a/cmd/palomad/conifg.go b/cmd/palomad/conifg.go new file mode 100644 index 00000000..24cdab31 --- /dev/null +++ b/cmd/palomad/conifg.go @@ -0,0 +1,13 @@ +package main + +import tmcfg "github.com/cometbft/cometbft/config" + +func initCometBFTConfig() *tmcfg.Config { + cfg := tmcfg.DefaultConfig() + + // these values put a higher strain on node memory + // cfg.P2P.MaxNumInboundPeers = 100 + // cfg.P2P.MaxNumOutboundPeers = 40 + + return cfg +} diff --git a/go.mod b/go.mod index 74fda82b..b99674d1 100644 --- a/go.mod +++ b/go.mod @@ -79,8 +79,30 @@ require ( ) require ( - // cloud.google.com/go v0.110.10 // indirect - // cloud.google.com/go/compute v1.23.3 // indirect + github.com/consensys/bavard v0.1.13 // indirect + github.com/consensys/gnark-crypto v0.12.1 // indirect + github.com/crate-crypto/go-kzg-4844 v0.7.0 // indirect + github.com/ethereum/c-kzg-4844 v0.4.0 // indirect + github.com/mmcloughlin/addchain v0.4.0 // indirect + github.com/rivo/uniseg v0.2.0 // indirect + github.com/supranational/blst v0.3.11 // indirect + rsc.io/tmplfunc v0.0.3 // indirect +) + +require ( + github.com/consensys/bavard v0.1.13 // indirect + github.com/consensys/gnark-crypto v0.12.1 // indirect + github.com/crate-crypto/go-kzg-4844 v0.7.0 // indirect + github.com/ethereum/c-kzg-4844 v0.4.0 // indirect + github.com/mmcloughlin/addchain v0.4.0 // indirect + github.com/rivo/uniseg v0.2.0 // indirect + github.com/supranational/blst v0.3.11 // indirect + rsc.io/tmplfunc v0.0.3 // indirect +) + +require ( + cloud.google.com/go v0.110.10 // indirect + cloud.google.com/go/compute v1.23.3 // indirect cloud.google.com/go/compute/metadata v0.2.3 // indirect // cloud.google.com/go/iam v1.1.5 // indirect cloud.google.com/go/storage v1.30.1 // indirect diff --git a/x/consensus/genesis.go b/x/consensus/genesis.go index 255f404c..4fbdfd98 100644 --- a/x/consensus/genesis.go +++ b/x/consensus/genesis.go @@ -1,19 +1,20 @@ package consensus import ( - sdk "github.com/cosmos/cosmos-sdk/types" + "context" + "github.com/palomachain/paloma/x/consensus/keeper" "github.com/palomachain/paloma/x/consensus/types" ) // InitGenesis initializes the capability module's state from a provided genesis // state. -func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState types.GenesisState) { +func InitGenesis(ctx context.Context, k keeper.Keeper, genState types.GenesisState) { k.SetParams(ctx, genState.Params) } // ExportGenesis returns the capability module's exported genesis. -func ExportGenesis(ctx sdk.Context, k keeper.Keeper) *types.GenesisState { +func ExportGenesis(ctx context.Context, k keeper.Keeper) *types.GenesisState { genesis := types.DefaultGenesis() genesis.Params = k.GetParams(ctx) diff --git a/x/consensus/keeper/attest.go b/x/consensus/keeper/attest.go index 8f735648..05728782 100644 --- a/x/consensus/keeper/attest.go +++ b/x/consensus/keeper/attest.go @@ -1,35 +1,36 @@ package keeper import ( + "context" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/palomachain/paloma/util/liblog" ) // CheckAndProcessAttestedMessages is supposed to be used within the // EndBlocker. It will get messages for the attestators that have reached a // consensus and process them. -func (k Keeper) CheckAndProcessAttestedMessages(ctx sdk.Context) error { +func (k Keeper) CheckAndProcessAttestedMessages(ctx context.Context) error { + sdkCtx := sdk.UnwrapSDKContext(ctx) for _, supportedConsensusQueue := range k.registry.slice { - opts, err := supportedConsensusQueue.SupportedQueues(ctx) + opts, err := supportedConsensusQueue.SupportedQueues(sdkCtx) if err != nil { return err } for _, opt := range opts { - msgs, err := k.GetMessagesFromQueue(ctx, opt.QueueTypeName, 0) + msgs, err := k.GetMessagesFromQueue(sdkCtx, opt.QueueTypeName, 0) if err != nil { return err } for _, msg := range msgs { - k.Logger(ctx).Debug( - "check-and-process-attested-messages-queue", - "id", msg.GetId(), - "nonce", msg.Nonce(), - ) - cq, err := k.getConsensusQueue(ctx, opt.QueueTypeName) + liblog.FromSDKLogger(k.Logger(sdkCtx)).WithFields("id", msg.GetId(), "nonce", msg.Nonce()).Debug( + "check-and-process-attested-messages-queue.") + cq, err := k.getConsensusQueue(sdkCtx, opt.QueueTypeName) if err != nil { return err } - if err := opt.ProcessMessageForAttestation(ctx, cq, msg); err != nil { + if err := opt.ProcessMessageForAttestation(sdkCtx, cq, msg); err != nil { return err } } diff --git a/x/consensus/keeper/cleanup.go b/x/consensus/keeper/cleanup.go index 2da0c7b5..f5c1032d 100644 --- a/x/consensus/keeper/cleanup.go +++ b/x/consensus/keeper/cleanup.go @@ -1,24 +1,27 @@ package keeper import ( + "context" + sdk "github.com/cosmos/cosmos-sdk/types" ) -func (k Keeper) DeleteOldMessages(ctx sdk.Context, blocksAgo int64) error { +func (k Keeper) DeleteOldMessages(ctx context.Context, blocksAgo int64) error { + sdkCtx := sdk.UnwrapSDKContext(ctx) for _, supportedConsensusQueue := range k.registry.slice { - opts, err := supportedConsensusQueue.SupportedQueues(ctx) + opts, err := supportedConsensusQueue.SupportedQueues(sdkCtx) if err != nil { return err } for _, opt := range opts { - msgs, err := k.GetMessagesFromQueue(ctx, opt.QueueTypeName, 9999) + msgs, err := k.GetMessagesFromQueue(sdkCtx, opt.QueueTypeName, 9999) if err != nil { return err } for _, msg := range msgs { - if ctx.BlockHeight()-msg.GetAddedAtBlockHeight() > blocksAgo { - err = k.DeleteJob(ctx, opt.QueueTypeName, msg.GetId()) + if sdkCtx.BlockHeight()-msg.GetAddedAtBlockHeight() > blocksAgo { + err = k.DeleteJob(sdkCtx, opt.QueueTypeName, msg.GetId()) if err != nil { return err } diff --git a/x/consensus/keeper/concensus_keeper.go b/x/consensus/keeper/concensus_keeper.go index 4ead39d0..da138874 100644 --- a/x/consensus/keeper/concensus_keeper.go +++ b/x/consensus/keeper/concensus_keeper.go @@ -1,6 +1,9 @@ package keeper import ( + "context" + + "cosmossdk.io/math" "github.com/VolumeFi/whoops" codectypes "github.com/cosmos/cosmos-sdk/codec/types" sdk "github.com/cosmos/cosmos-sdk/types" @@ -16,9 +19,10 @@ import ( var defaultResponseMessageCount = 1000 // getConsensusQueue gets the consensus queue for the given type. -func (k Keeper) getConsensusQueue(ctx sdk.Context, queueTypeName string) (consensus.Queuer, error) { +func (k Keeper) getConsensusQueue(ctx context.Context, queueTypeName string) (consensus.Queuer, error) { + sdkCtx := sdk.UnwrapSDKContext(ctx) for _, q := range k.registry.slice { - supportedQueues, err := q.SupportedQueues(ctx) + supportedQueues, err := q.SupportedQueues(sdkCtx) if err != nil { return nil, err } @@ -54,37 +58,36 @@ func (k Keeper) getConsensusQueue(ctx sdk.Context, queueTypeName string) (consen return nil, ErrConsensusQueueNotImplemented.Format(queueTypeName) } -func (k Keeper) RemoveConsensusQueue(ctx sdk.Context, queueTypeName string) error { - cq, err := k.getConsensusQueue(ctx, queueTypeName) +func (k Keeper) RemoveConsensusQueue(ctx context.Context, queueTypeName string) error { + sdkCtx := sdk.UnwrapSDKContext(ctx) + cq, err := k.getConsensusQueue(sdkCtx, queueTypeName) if err != nil { return err } - consensus.RemoveQueueCompletely(ctx, cq) + consensus.RemoveQueueCompletely(sdkCtx, cq) return nil } -func (k Keeper) PutMessageInQueue(ctx sdk.Context, queueTypeName string, msg consensus.ConsensusMsg, opts *consensus.PutOptions) (uint64, error) { - cq, err := k.getConsensusQueue(ctx, queueTypeName) +func (k Keeper) PutMessageInQueue(ctx context.Context, queueTypeName string, msg consensus.ConsensusMsg, opts *consensus.PutOptions) (uint64, error) { + sdkCtx := sdk.UnwrapSDKContext(ctx) + cq, err := k.getConsensusQueue(sdkCtx, queueTypeName) if err != nil { - k.Logger(ctx).Error("error while getting consensus queue", "error", err) + liblog.FromSDKLogger(k.Logger(sdkCtx)).WithError(err).Error("error while getting consensus queue.") return 0, err } - msgID, err := cq.Put(ctx, msg, opts) + msgID, err := cq.Put(sdkCtx, msg, opts) if err != nil { - k.Logger(ctx).Error("error while putting message into queue", "error", err) + liblog.FromSDKLogger(k.Logger(sdkCtx)).WithError(err).Error("error while putting message into queue.") return 0, err } - k.Logger(ctx).Info( - "put message into consensus queue", - "queue-type-name", queueTypeName, - "message-id", msgID, - ) + liblog.FromSDKLogger(k.Logger(sdkCtx)).WithFields("queue-type-name", queueTypeName, "message-id", msgID).Info("put message into consensus queue.") return msgID, nil } // GetMessagesForSigning returns messages for a single validator that needs to be signed. -func (k Keeper) GetMessagesForSigning(ctx sdk.Context, queueTypeName string, valAddress sdk.ValAddress) (msgs []types.QueuedSignedMessageI, err error) { - msgs, err = k.GetMessagesFromQueue(ctx, queueTypeName, 0) +func (k Keeper) GetMessagesForSigning(ctx context.Context, queueTypeName string, valAddress sdk.ValAddress) (msgs []types.QueuedSignedMessageI, err error) { + sdkCtx := sdk.UnwrapSDKContext(ctx) + msgs, err = k.GetMessagesFromQueue(sdkCtx, queueTypeName, 0) if err != nil { return nil, err } @@ -107,8 +110,9 @@ func (k Keeper) GetMessagesForSigning(ctx sdk.Context, queueTypeName string, val } // GetMessagesForRelaying returns messages for a single validator to relay. -func (k Keeper) GetMessagesForRelaying(ctx sdk.Context, queueTypeName string, valAddress sdk.ValAddress) (msgs []types.QueuedSignedMessageI, err error) { - msgs, err = k.GetMessagesFromQueue(ctx, queueTypeName, 0) +func (k Keeper) GetMessagesForRelaying(ctx context.Context, queueTypeName string, valAddress sdk.ValAddress) (msgs []types.QueuedSignedMessageI, err error) { + sdkCtx := sdk.UnwrapSDKContext(ctx) + msgs, err = k.GetMessagesFromQueue(sdkCtx, queueTypeName, 0) if err != nil { return nil, err } @@ -118,7 +122,7 @@ func (k Keeper) GetMessagesForRelaying(ctx sdk.Context, queueTypeName string, va for _, v := range msgs { cm, err := v.ConsensusMsg(k.cdc) if err != nil { - liblog.FromSDKLogger(k.Logger(ctx)).WithError(err).Error("Failed to get consensus msg") + liblog.FromSDKLogger(k.Logger(sdkCtx)).WithError(err).Error("Failed to get consensus msg") continue } @@ -145,7 +149,7 @@ func (k Keeper) GetMessagesForRelaying(ctx sdk.Context, queueTypeName string, va cm, err := msg.ConsensusMsg(k.cdc) if err != nil { // NO cross chain message, just return true - liblog.FromSDKLogger(k.Logger(ctx)).WithError(err).Error("Failed to get consensus msg") + liblog.FromSDKLogger(k.Logger(sdkCtx)).WithError(err).Error("Failed to get consensus msg") return true } @@ -170,7 +174,7 @@ func (k Keeper) GetMessagesForRelaying(ctx sdk.Context, queueTypeName string, va msgs = slice.Filter(msgs, func(msg types.QueuedSignedMessageI) bool { var unpackedMsg evmtypes.TurnstoneMsg if err := k.cdc.UnpackAny(msg.GetMsg(), &unpackedMsg); err != nil { - k.Logger(ctx).With("err", err).Error("Failed to unpack message") + liblog.FromSDKLogger(k.Logger(sdkCtx)).WithError(err).Error("Failed to unpack message.") return false } @@ -190,8 +194,9 @@ func (k Keeper) GetMessagesForRelaying(ctx sdk.Context, queueTypeName string, va } // GetMessagesForAttesting returns messages for a single validator to attest. -func (k Keeper) GetMessagesForAttesting(ctx sdk.Context, queueTypeName string, valAddress sdk.ValAddress) (msgs []types.QueuedSignedMessageI, err error) { - msgs, err = k.GetMessagesFromQueue(ctx, queueTypeName, 0) +func (k Keeper) GetMessagesForAttesting(ctx context.Context, queueTypeName string, valAddress sdk.ValAddress) (msgs []types.QueuedSignedMessageI, err error) { + sdkCtx := sdk.UnwrapSDKContext(ctx) + msgs, err = k.GetMessagesFromQueue(sdkCtx, queueTypeName, 0) if err != nil { return nil, err } @@ -220,16 +225,17 @@ func (k Keeper) GetMessagesForAttesting(ctx sdk.Context, queueTypeName string, v } // GetMessagesFromQueue gets N messages from the queue. -func (k Keeper) GetMessagesFromQueue(ctx sdk.Context, queueTypeName string, n int) (msgs []types.QueuedSignedMessageI, err error) { - cq, err := k.getConsensusQueue(ctx, queueTypeName) +func (k Keeper) GetMessagesFromQueue(ctx context.Context, queueTypeName string, n int) (msgs []types.QueuedSignedMessageI, err error) { + sdkCtx := sdk.UnwrapSDKContext(ctx) + cq, err := k.getConsensusQueue(sdkCtx, queueTypeName) if err != nil { - k.Logger(ctx).Error("error while getting consensus queue", "err", err) + liblog.FromSDKLogger(k.Logger(sdkCtx)).WithError(err).Error("error while getting consensus queue.") return nil, err } - msgs, err = cq.GetAll(ctx) + msgs, err = cq.GetAll(sdkCtx) if err != nil { - k.Logger(ctx).Error("error while getting all messages from queue", "err", err) + liblog.FromSDKLogger(k.Logger(sdkCtx)).WithError(err).Error("error while getting all messages from queue.") return nil, err } @@ -240,32 +246,34 @@ func (k Keeper) GetMessagesFromQueue(ctx sdk.Context, queueTypeName string, n in return } -func (k Keeper) DeleteJob(ctx sdk.Context, queueTypeName string, id uint64) (err error) { - cq, err := k.getConsensusQueue(ctx, queueTypeName) +func (k Keeper) DeleteJob(ctx context.Context, queueTypeName string, id uint64) (err error) { + sdkCtx := sdk.UnwrapSDKContext(ctx) + cq, err := k.getConsensusQueue(sdkCtx, queueTypeName) if err != nil { - k.Logger(ctx).Error("error while getting consensus queue", "err", err) + liblog.FromSDKLogger(k.Logger(sdkCtx)).WithError(err).Error("error while getting consensus queue.") return err } - return cq.Remove(ctx, id) + return cq.Remove(sdkCtx, id) } // GetMessagesThatHaveReachedConsensus returns messages from a given // queueTypeName that have reached consensus based on the latest snapshot // available. -func (k Keeper) GetMessagesThatHaveReachedConsensus(ctx sdk.Context, queueTypeName string) ([]types.QueuedSignedMessageI, error) { +func (k Keeper) GetMessagesThatHaveReachedConsensus(ctx context.Context, queueTypeName string) ([]types.QueuedSignedMessageI, error) { + sdkCtx := sdk.UnwrapSDKContext(ctx) var consensusReached []types.QueuedSignedMessageI err := whoops.Try(func() { - cq, err := k.getConsensusQueue(ctx, queueTypeName) + cq, err := k.getConsensusQueue(sdkCtx, queueTypeName) whoops.Assert(err) - msgs := whoops.Must(cq.GetAll(ctx)) + msgs := whoops.Must(cq.GetAll(sdkCtx)) if len(msgs) == 0 { return } - snapshot := whoops.Must(k.valset.GetCurrentSnapshot(ctx)) + snapshot := whoops.Must(k.valset.GetCurrentSnapshot(sdkCtx)) - if len(snapshot.Validators) == 0 || snapshot.TotalShares.Equal(sdk.ZeroInt()) { + if len(snapshot.Validators) == 0 || snapshot.TotalShares.Equal(math.ZeroInt()) { return } @@ -275,12 +283,12 @@ func (k Keeper) GetMessagesThatHaveReachedConsensus(ctx sdk.Context, queueTypeNa } for _, msg := range msgs { - msgTotal := sdk.ZeroInt() + msgTotal := math.ZeroInt() // add shares of validators that have signed the message for _, signData := range msg.GetSignData() { signedValidator, ok := validatorMap[signData.ValAddress.String()] if !ok { - k.Logger(ctx).Info("validator not found", "validator", signData.ValAddress) + liblog.FromSDKLogger(k.Logger(sdkCtx)).WithFields("validator", signData.ValAddress).Info("validator not found.") continue } msgTotal = msgTotal.Add(signedValidator.ShareCount) @@ -295,7 +303,7 @@ func (k Keeper) GetMessagesThatHaveReachedConsensus(ctx sdk.Context, queueTypeNa // could lose precision using floating point arithmetic. // If we multiply both sides with 3, we don't need to do division. // 3 * msgTotal >= 2 * snapshotTotal - if msgTotal.Mul(sdk.NewInt(3)).GTE(snapshot.TotalShares.Mul(sdk.NewInt(2))) { + if msgTotal.Mul(math.NewInt(3)).GTE(snapshot.TotalShares.Mul(math.NewInt(2))) { // consensus has been reached consensusReached = append(consensusReached, msg) } @@ -309,19 +317,20 @@ func (k Keeper) GetMessagesThatHaveReachedConsensus(ctx sdk.Context, queueTypeNa // AddMessageSignature adds signatures to the messages. func (k Keeper) AddMessageSignature( - ctx sdk.Context, + ctx context.Context, valAddr sdk.ValAddress, msgs []*types.ConsensusMessageSignature, ) error { err := whoops.Try(func() { for _, msg := range msgs { + sdkCtx := sdk.UnwrapSDKContext(ctx) cq := whoops.Must( - k.getConsensusQueue(ctx, msg.GetQueueTypeName()), + k.getConsensusQueue(sdkCtx, msg.GetQueueTypeName()), ) chainType, chainReferenceID := cq.ChainInfo() publicKey := whoops.Must(k.valset.GetSigningKey( - ctx, + sdkCtx, valAddr, chainType, chainReferenceID, @@ -330,7 +339,7 @@ func (k Keeper) AddMessageSignature( whoops.Assert( cq.AddSignature( - ctx, + sdkCtx, msg.Id, &types.SignData{ ValAddress: valAddr, @@ -341,37 +350,35 @@ func (k Keeper) AddMessageSignature( ), ) - k.Logger(ctx).Info("added message signature", - "message-id", msg.GetId(), - "queue-type-name", msg.GetQueueTypeName(), + liblog.FromSDKLogger(k.Logger(sdkCtx)).WithFields("message-id", msg.GetId(), "queue-type-name", msg.GetQueueTypeName(), "signed-by-address", msg.GetSignedByAddress(), "chain-type", chainType, - "chain-reference-id", chainReferenceID, - ) + "chain-reference-id", chainReferenceID).Info("added message signature.") } }) + sdkCtx := sdk.UnwrapSDKContext(ctx) + if err != nil { - k.Logger(ctx).Error("error while adding messages signatures", - "err", err, - ) + liblog.FromSDKLogger(k.Logger(sdkCtx)).WithError(err).Error("error while adding messages signatures.") } return err } func (k Keeper) AddMessageEvidence( - ctx sdk.Context, + ctx context.Context, valAddr sdk.ValAddress, msg *types.MsgAddEvidence, ) error { + sdkCtx := sdk.UnwrapSDKContext(ctx) err := whoops.Try(func() { cq := whoops.Must( - k.getConsensusQueue(ctx, msg.GetQueueTypeName()), + k.getConsensusQueue(sdkCtx, msg.GetQueueTypeName()), ) whoops.Assert( cq.AddEvidence( - ctx, + sdkCtx, msg.GetMessageID(), &types.Evidence{ ValAddress: valAddr, @@ -380,28 +387,24 @@ func (k Keeper) AddMessageEvidence( ), ) chainType, chainReferenceID := cq.ChainInfo() - k.Logger(ctx).Info("added message evidence", - "message-id", msg.GetMessageID(), - "queue-type-name", msg.GetQueueTypeName(), - "chain-type", chainType, - "chain-reference-id", chainReferenceID, - ) + liblog.FromSDKLogger(k.Logger(sdkCtx)).WithFields("message-id", msg.GetMessageID(), "queue-type-name", msg.GetQueueTypeName(), + "chain-type", chainType, "chain-reference-id", chainReferenceID).Info("added message evidence.") }) if err != nil { - k.Logger(ctx).Error("error while adding message evidence", - "err", err, - ) + liblog.FromSDKLogger(k.Logger(sdkCtx)).WithError(err).Error("error while adding message evidence.") } return err } func (k Keeper) SetMessagePublicAccessData( - ctx sdk.Context, + ctx context.Context, valAddr sdk.ValAddress, msg *types.MsgSetPublicAccessData, ) error { - cq, err := k.getConsensusQueue(ctx, msg.GetQueueTypeName()) + sdkCtx := sdk.UnwrapSDKContext(ctx) + + cq, err := k.getConsensusQueue(sdkCtx, msg.GetQueueTypeName()) if err != nil { return err } @@ -410,30 +413,26 @@ func (k Keeper) SetMessagePublicAccessData( ValAddress: valAddr, Data: msg.GetData(), } - err = cq.SetPublicAccessData(ctx, msg.GetMessageID(), payload) + err = cq.SetPublicAccessData(sdkCtx, msg.GetMessageID(), payload) if err != nil { - k.Logger(ctx).Error("error while adding message public access data", "err", err) + liblog.FromSDKLogger(k.Logger(sdkCtx)).WithError(err).Error("error while adding message public access data.") return err } chainType, chainReferenceID := cq.ChainInfo() - k.Logger(ctx).Info("added message public access data", - "message-id", msg.GetMessageID(), - "queue-type-name", msg.GetQueueTypeName(), - "chain-type", chainType, - "chain-reference-id", chainReferenceID, - "public-access-data", hexutil.Encode(payload.Data), - ) + liblog.FromSDKLogger(k.Logger(sdkCtx)).WithFields("message-id", msg.GetMessageID(), "queue-type-name", + msg.GetQueueTypeName(), "chain-type", chainType, "chain-reference-id", chainReferenceID, "public-access-data", hexutil.Encode(payload.Data)).Info("added message public access data.") return nil } func (k Keeper) SetMessageErrorData( - ctx sdk.Context, + ctx context.Context, valAddr sdk.ValAddress, msg *types.MsgSetErrorData, ) error { - cq, err := k.getConsensusQueue(ctx, msg.GetQueueTypeName()) + sdkCtx := sdk.UnwrapSDKContext(ctx) + cq, err := k.getConsensusQueue(sdkCtx, msg.GetQueueTypeName()) if err != nil { return err } @@ -442,20 +441,18 @@ func (k Keeper) SetMessageErrorData( ValAddress: valAddr, Data: msg.GetData(), } - err = cq.SetErrorData(ctx, msg.GetMessageID(), payload) + err = cq.SetErrorData(sdkCtx, msg.GetMessageID(), payload) if err != nil { - k.Logger(ctx).Error("error while adding error data", "err", err) + liblog.FromSDKLogger(k.Logger(sdkCtx)).WithError(err).Error("error while adding error data.") return err } chainType, chainReferenceID := cq.ChainInfo() - k.Logger(ctx).Info("added error data", - "message-id", msg.GetMessageID(), + liblog.FromSDKLogger(k.Logger(sdkCtx)).WithFields("message-id", msg.GetMessageID(), "queue-type-name", msg.GetQueueTypeName(), "chain-type", chainType, "chain-reference-id", chainReferenceID, - "error-data", hexutil.Encode(payload.Data), - ) + "error-data", hexutil.Encode(payload.Data)).Info("added error data.") return nil } diff --git a/x/consensus/keeper/concensus_keeper_test.go b/x/consensus/keeper/concensus_keeper_test.go index 40ec9467..e9a0768b 100644 --- a/x/consensus/keeper/concensus_keeper_test.go +++ b/x/consensus/keeper/concensus_keeper_test.go @@ -1,8 +1,10 @@ package keeper import ( + "context" "testing" + "cosmossdk.io/math" "github.com/cometbft/cometbft/crypto/secp256k1" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/palomachain/paloma/x/consensus/keeper/consensus" @@ -123,9 +125,10 @@ func TestGetMessagesFromQueue(t *testing.T) { expectedCount: 50, }, } - addMessages := func(ctx sdk.Context, k Keeper, queue string, numMessages int) { + addMessages := func(ctx context.Context, k Keeper, queue string, numMessages int) { + sdkCtx := sdk.UnwrapSDKContext(ctx) for i := 0; i < numMessages; i++ { - _, err := k.PutMessageInQueue(ctx, queue, &types.SimpleMessage{}, nil) + _, err := k.PutMessageInQueue(sdkCtx, queue, &types.SimpleMessage{}, nil) require.NoError(t, err) } } @@ -265,23 +268,23 @@ func TestGetMessagesForRelaying(t *testing.T) { func TestGettingMessagesThatHaveReachedConsensus(t *testing.T) { testValidators := []valsettypes.Validator{ { - ShareCount: sdk.NewInt(2), + ShareCount: math.NewInt(2), Address: sdk.ValAddress("val1"), }, { - ShareCount: sdk.NewInt(3), + ShareCount: math.NewInt(3), Address: sdk.ValAddress("val2"), }, { - ShareCount: sdk.NewInt(6), + ShareCount: math.NewInt(6), Address: sdk.ValAddress("val3"), }, { - ShareCount: sdk.NewInt(7), + ShareCount: math.NewInt(7), Address: sdk.ValAddress("val4"), }, } - total := sdk.NewInt(18) + total := math.NewInt(18) _ = testValidators _ = total @@ -290,7 +293,7 @@ func TestGettingMessagesThatHaveReachedConsensus(t *testing.T) { keeper *Keeper ms mockedServices cq *consensusmock.Queuer - ctx sdk.Context + ctx context.Context } for _, tt := range []struct { @@ -315,17 +318,19 @@ func TestGettingMessagesThatHaveReachedConsensus(t *testing.T) { { name: "if there is a message but snapshot does not exist", preRun: func(t *testing.T, sd setupData) { + sdkCtx := sdk.UnwrapSDKContext(sd.ctx) msg := &types.SimpleMessage{} // sd.cq.On("GetAll", mock.Anything).Return([]types.QueuedSignedMessageI{msg}, nil).Once() - sd.keeper.PutMessageInQueue(sd.ctx, defaultQueueName, msg, nil) + sd.keeper.PutMessageInQueue(sdkCtx, defaultQueueName, msg, nil) sd.ms.ValsetKeeper.On("GetCurrentSnapshot", mock.Anything).Return(&valsettypes.Snapshot{}, nil) }, }, { name: "with messages returned but no signature data it returns nothing", preRun: func(t *testing.T, sd setupData) { + sdkCtx := sdk.UnwrapSDKContext(sd.ctx) msg := &types.SimpleMessage{} - sd.keeper.PutMessageInQueue(sd.ctx, defaultQueueName, msg, nil) + sd.keeper.PutMessageInQueue(sdkCtx, defaultQueueName, msg, nil) // msg := consensustypesmock.NewQueuedSignedMessageI(t) // msg.On("GetSignData").Return(nil).Once() // sd.cq.On("GetAll", mock.Anything).Return([]types.QueuedSignedMessageI{msg}, nil).Once() @@ -341,8 +346,9 @@ func TestGettingMessagesThatHaveReachedConsensus(t *testing.T) { { name: "with a single signature only which is not enough it returns nothing", preRun: func(t *testing.T, sd setupData) { + sdkCtx := sdk.UnwrapSDKContext(sd.ctx) msg := &types.SimpleMessage{} - _, err := sd.keeper.PutMessageInQueue(sd.ctx, defaultQueueName, msg, nil) + _, err := sd.keeper.PutMessageInQueue(sdkCtx, defaultQueueName, msg, nil) require.NoError(t, err) sd.ms.ValsetKeeper.On("GetSigningKey", mock.Anything, sdk.ValAddress("val1"), "evm", "test", "bob").Return( []byte("signing-key"), @@ -355,7 +361,7 @@ func TestGettingMessagesThatHaveReachedConsensus(t *testing.T) { }, nil, ) - err = sd.keeper.AddMessageSignature(sd.ctx, sdk.ValAddress("val1"), []*types.ConsensusMessageSignature{ + err = sd.keeper.AddMessageSignature(sdkCtx, sdk.ValAddress("val1"), []*types.ConsensusMessageSignature{ { Id: 1, QueueTypeName: defaultQueueName, @@ -370,8 +376,9 @@ func TestGettingMessagesThatHaveReachedConsensus(t *testing.T) { name: "with enough signatures for a consensus it returns messages", expMsgsLen: 1, preRun: func(t *testing.T, sd setupData) { + sdkCtx := sdk.UnwrapSDKContext(sd.ctx) msg := &types.SimpleMessage{} - _, err := sd.keeper.PutMessageInQueue(sd.ctx, defaultQueueName, msg, nil) + _, err := sd.keeper.PutMessageInQueue(sdkCtx, defaultQueueName, msg, nil) require.NoError(t, err) sd.ms.ValsetKeeper.On("GetSigningKey", mock.Anything, sdk.ValAddress("val3"), "evm", "test", "bob3").Return( @@ -390,7 +397,7 @@ func TestGettingMessagesThatHaveReachedConsensus(t *testing.T) { }, nil, ) - err = sd.keeper.AddMessageSignature(sd.ctx, sdk.ValAddress("val3"), []*types.ConsensusMessageSignature{ + err = sd.keeper.AddMessageSignature(sdkCtx, sdk.ValAddress("val3"), []*types.ConsensusMessageSignature{ { Id: 1, QueueTypeName: defaultQueueName, @@ -399,7 +406,7 @@ func TestGettingMessagesThatHaveReachedConsensus(t *testing.T) { }, }) require.NoError(t, err) - err = sd.keeper.AddMessageSignature(sd.ctx, sdk.ValAddress("val4"), []*types.ConsensusMessageSignature{ + err = sd.keeper.AddMessageSignature(sdkCtx, sdk.ValAddress("val4"), []*types.ConsensusMessageSignature{ { Id: 1, QueueTypeName: defaultQueueName, @@ -414,9 +421,10 @@ func TestGettingMessagesThatHaveReachedConsensus(t *testing.T) { name: "with multiple messages where only one has enough signatures", expMsgsLen: 1, preRun: func(t *testing.T, sd setupData) { + sdkCtx := sdk.UnwrapSDKContext(sd.ctx) msg := &types.SimpleMessage{} - _, err := sd.keeper.PutMessageInQueue(sd.ctx, defaultQueueName, msg, nil) - _, err = sd.keeper.PutMessageInQueue(sd.ctx, defaultQueueName, msg, nil) + _, err := sd.keeper.PutMessageInQueue(sdkCtx, defaultQueueName, msg, nil) + _, err = sd.keeper.PutMessageInQueue(sdkCtx, defaultQueueName, msg, nil) require.NoError(t, err) sd.ms.ValsetKeeper.On("GetSigningKey", mock.Anything, sdk.ValAddress("val3"), "evm", "test", "bob3").Return( @@ -436,7 +444,7 @@ func TestGettingMessagesThatHaveReachedConsensus(t *testing.T) { nil, ) t.Run("adding message signatures to first message which has enough signatures", func(t *testing.T) { - err = sd.keeper.AddMessageSignature(sd.ctx, sdk.ValAddress("val3"), []*types.ConsensusMessageSignature{ + err = sd.keeper.AddMessageSignature(sdkCtx, sdk.ValAddress("val3"), []*types.ConsensusMessageSignature{ { Id: 1, QueueTypeName: defaultQueueName, @@ -445,7 +453,7 @@ func TestGettingMessagesThatHaveReachedConsensus(t *testing.T) { }, }) require.NoError(t, err) - err = sd.keeper.AddMessageSignature(sd.ctx, sdk.ValAddress("val4"), []*types.ConsensusMessageSignature{ + err = sd.keeper.AddMessageSignature(sdkCtx, sdk.ValAddress("val4"), []*types.ConsensusMessageSignature{ { Id: 1, QueueTypeName: defaultQueueName, @@ -456,7 +464,7 @@ func TestGettingMessagesThatHaveReachedConsensus(t *testing.T) { require.NoError(t, err) }) t.Run("adding message signatures to a second message doesn't have enough signatures", func(t *testing.T) { - err = sd.keeper.AddMessageSignature(sd.ctx, sdk.ValAddress("val4"), []*types.ConsensusMessageSignature{ + err = sd.keeper.AddMessageSignature(sdkCtx, sdk.ValAddress("val4"), []*types.ConsensusMessageSignature{ { Id: 2, QueueTypeName: defaultQueueName, @@ -472,10 +480,11 @@ func TestGettingMessagesThatHaveReachedConsensus(t *testing.T) { name: "with multiple messages where all have enough signatures", expMsgsLen: 2, preRun: func(t *testing.T, sd setupData) { + sdkCtx := sdk.UnwrapSDKContext(sd.ctx) msg := &types.SimpleMessage{} - _, err := sd.keeper.PutMessageInQueue(sd.ctx, defaultQueueName, msg, nil) + _, err := sd.keeper.PutMessageInQueue(sdkCtx, defaultQueueName, msg, nil) require.NoError(t, err) - _, err = sd.keeper.PutMessageInQueue(sd.ctx, defaultQueueName, msg, nil) + _, err = sd.keeper.PutMessageInQueue(sdkCtx, defaultQueueName, msg, nil) require.NoError(t, err) sd.ms.ValsetKeeper.On("GetSigningKey", mock.Anything, sdk.ValAddress("val2"), "evm", "test", "bob2").Return( @@ -500,7 +509,7 @@ func TestGettingMessagesThatHaveReachedConsensus(t *testing.T) { nil, ) t.Run("adding message signatures to first message which has enough signatures", func(t *testing.T) { - err = sd.keeper.AddMessageSignature(sd.ctx, sdk.ValAddress("val2"), []*types.ConsensusMessageSignature{ + err = sd.keeper.AddMessageSignature(sdkCtx, sdk.ValAddress("val2"), []*types.ConsensusMessageSignature{ { Id: 1, QueueTypeName: defaultQueueName, @@ -509,7 +518,7 @@ func TestGettingMessagesThatHaveReachedConsensus(t *testing.T) { }, }) require.NoError(t, err) - err = sd.keeper.AddMessageSignature(sd.ctx, sdk.ValAddress("val3"), []*types.ConsensusMessageSignature{ + err = sd.keeper.AddMessageSignature(sdkCtx, sdk.ValAddress("val3"), []*types.ConsensusMessageSignature{ { Id: 1, QueueTypeName: defaultQueueName, @@ -518,7 +527,7 @@ func TestGettingMessagesThatHaveReachedConsensus(t *testing.T) { }, }) require.NoError(t, err) - err = sd.keeper.AddMessageSignature(sd.ctx, sdk.ValAddress("val4"), []*types.ConsensusMessageSignature{ + err = sd.keeper.AddMessageSignature(sdkCtx, sdk.ValAddress("val4"), []*types.ConsensusMessageSignature{ { Id: 1, QueueTypeName: defaultQueueName, @@ -529,7 +538,7 @@ func TestGettingMessagesThatHaveReachedConsensus(t *testing.T) { require.NoError(t, err) }) t.Run("adding message signatures to second message which has enough signatures", func(t *testing.T) { - err = sd.keeper.AddMessageSignature(sd.ctx, sdk.ValAddress("val3"), []*types.ConsensusMessageSignature{ + err = sd.keeper.AddMessageSignature(sdkCtx, sdk.ValAddress("val3"), []*types.ConsensusMessageSignature{ { Id: 2, QueueTypeName: defaultQueueName, @@ -538,7 +547,7 @@ func TestGettingMessagesThatHaveReachedConsensus(t *testing.T) { }, }) require.NoError(t, err) - err = sd.keeper.AddMessageSignature(sd.ctx, sdk.ValAddress("val4"), []*types.ConsensusMessageSignature{ + err = sd.keeper.AddMessageSignature(sdkCtx, sdk.ValAddress("val4"), []*types.ConsensusMessageSignature{ { Id: 2, QueueTypeName: defaultQueueName, @@ -553,8 +562,9 @@ func TestGettingMessagesThatHaveReachedConsensus(t *testing.T) { { name: "if it's signed by a validator which is not in the snapshot it skips it", preRun: func(t *testing.T, sd setupData) { + sdkCtx := sdk.UnwrapSDKContext(sd.ctx) msg := &types.SimpleMessage{} - _, err := sd.keeper.PutMessageInQueue(sd.ctx, defaultQueueName, msg, nil) + _, err := sd.keeper.PutMessageInQueue(sdkCtx, defaultQueueName, msg, nil) require.NoError(t, err) sd.ms.ValsetKeeper.On("GetSigningKey", mock.Anything, sdk.ValAddress("404"), "evm", "test", "404").Return( @@ -569,7 +579,7 @@ func TestGettingMessagesThatHaveReachedConsensus(t *testing.T) { }, nil, ) - err = sd.keeper.AddMessageSignature(sd.ctx, sdk.ValAddress("404"), []*types.ConsensusMessageSignature{ + err = sd.keeper.AddMessageSignature(sdkCtx, sdk.ValAddress("404"), []*types.ConsensusMessageSignature{ { Id: 1, QueueTypeName: defaultQueueName, @@ -709,7 +719,7 @@ type queueSupporter struct { opt *consensus.QueueOptions } -func (q queueSupporter) SupportedQueues(ctx sdk.Context) ([]consensus.SupportsConsensusQueueAction, error) { +func (q queueSupporter) SupportedQueues(ctx context.Context) ([]consensus.SupportsConsensusQueueAction, error) { return []consensus.SupportsConsensusQueueAction{ { QueueOptions: *q.opt, diff --git a/x/consensus/keeper/consensus/batch.go b/x/consensus/keeper/consensus/batch.go index 4c560b45..8deb9d8a 100644 --- a/x/consensus/keeper/consensus/batch.go +++ b/x/consensus/keeper/consensus/batch.go @@ -1,8 +1,10 @@ package consensus import ( + "context" + + "cosmossdk.io/store/prefix" codectypes "github.com/cosmos/cosmos-sdk/codec/types" - "github.com/cosmos/cosmos-sdk/store/prefix" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/palomachain/paloma/x/consensus/types" ) @@ -27,12 +29,13 @@ func NewBatchQueue(qo QueueOptions) BatchQueue { } } -func (c BatchQueue) Put(ctx sdk.Context, msg ConsensusMsg, opts *PutOptions) (uint64, error) { +func (c BatchQueue) Put(ctx context.Context, msg ConsensusMsg, opts *PutOptions) (uint64, error) { + sdkCtx := sdk.UnwrapSDKContext(ctx) if !c.batchedTypeChecker(msg) { return 0, ErrIncorrectMessageType.Format(msg) } - newID := c.base.qo.Ider.IncrementNextID(ctx, consensusBatchQueueIDCounterKey) + newID := c.base.qo.Ider.IncrementNextID(sdkCtx, consensusBatchQueueIDCounterKey) anyMsg, err := codectypes.NewAnyWithValue(msg) if err != nil { @@ -47,12 +50,13 @@ func (c BatchQueue) Put(ctx sdk.Context, msg ConsensusMsg, opts *PutOptions) (ui if err != nil { return 0, err } - c.batchQueue(ctx).Set(sdk.Uint64ToBigEndian(newID), data) + c.batchQueue(sdkCtx).Set(sdk.Uint64ToBigEndian(newID), data) return newID, nil } -func (c BatchQueue) ProcessBatches(ctx sdk.Context) error { - queue := c.batchQueue(ctx) +func (c BatchQueue) ProcessBatches(ctx context.Context) error { + sdkCtx := sdk.UnwrapSDKContext(ctx) + queue := c.batchQueue(sdkCtx) deleteKeys := [][]byte{} iterator := queue.Iterator(nil, nil) @@ -90,7 +94,7 @@ func (c BatchQueue) ProcessBatches(ctx sdk.Context) error { } for _, batch := range batches { - _, err := c.base.Put(ctx, batch, nil) + _, err := c.base.Put(sdkCtx, batch, nil) if err != nil { return err } @@ -100,45 +104,55 @@ func (c BatchQueue) ProcessBatches(ctx sdk.Context) error { } // batchQueue returns queue of messages that have been batched -func (c BatchQueue) batchQueue(ctx sdk.Context) prefix.Store { - store := c.base.qo.Sg.Store(ctx) +func (c BatchQueue) batchQueue(ctx context.Context) prefix.Store { + sdkCtx := sdk.UnwrapSDKContext(ctx) + store := c.base.qo.Sg.Store(sdkCtx) return prefix.NewStore(store, []byte("batching:"+c.base.signingQueueKey())) } -func (c BatchQueue) AddSignature(ctx sdk.Context, id uint64, signData *types.SignData) error { - return c.base.AddSignature(ctx, id, signData) +func (c BatchQueue) AddSignature(ctx context.Context, id uint64, signData *types.SignData) error { + sdkCtx := sdk.UnwrapSDKContext(ctx) + return c.base.AddSignature(sdkCtx, id, signData) } -func (c BatchQueue) Remove(ctx sdk.Context, msgID uint64) error { - return c.base.Remove(ctx, msgID) +func (c BatchQueue) Remove(ctx context.Context, msgID uint64) error { + sdkCtx := sdk.UnwrapSDKContext(ctx) + return c.base.Remove(sdkCtx, msgID) } -func (c BatchQueue) GetMsgByID(ctx sdk.Context, id uint64) (types.QueuedSignedMessageI, error) { - return c.base.GetMsgByID(ctx, id) +func (c BatchQueue) GetMsgByID(ctx context.Context, id uint64) (types.QueuedSignedMessageI, error) { + sdkCtx := sdk.UnwrapSDKContext(ctx) + return c.base.GetMsgByID(sdkCtx, id) } -func (c BatchQueue) GetAll(ctx sdk.Context) ([]types.QueuedSignedMessageI, error) { - return c.base.GetAll(ctx) +func (c BatchQueue) GetAll(ctx context.Context) ([]types.QueuedSignedMessageI, error) { + sdkCtx := sdk.UnwrapSDKContext(ctx) + return c.base.GetAll(sdkCtx) } -func (c BatchQueue) AddEvidence(ctx sdk.Context, id uint64, evidence *types.Evidence) error { - return c.base.AddEvidence(ctx, id, evidence) +func (c BatchQueue) AddEvidence(ctx context.Context, id uint64, evidence *types.Evidence) error { + sdkCtx := sdk.UnwrapSDKContext(ctx) + return c.base.AddEvidence(sdkCtx, id, evidence) } -func (c BatchQueue) SetPublicAccessData(ctx sdk.Context, id uint64, data *types.PublicAccessData) error { - return c.base.SetPublicAccessData(ctx, id, data) +func (c BatchQueue) SetPublicAccessData(ctx context.Context, id uint64, data *types.PublicAccessData) error { + sdkCtx := sdk.UnwrapSDKContext(ctx) + return c.base.SetPublicAccessData(sdkCtx, id, data) } -func (c BatchQueue) GetPublicAccessData(ctx sdk.Context, id uint64) (*types.PublicAccessData, error) { - return c.base.GetPublicAccessData(ctx, id) +func (c BatchQueue) GetPublicAccessData(ctx context.Context, id uint64) (*types.PublicAccessData, error) { + sdkCtx := sdk.UnwrapSDKContext(ctx) + return c.base.GetPublicAccessData(sdkCtx, id) } -func (c BatchQueue) SetErrorData(ctx sdk.Context, id uint64, data *types.ErrorData) error { - return c.base.SetErrorData(ctx, id, data) +func (c BatchQueue) SetErrorData(ctx context.Context, id uint64, data *types.ErrorData) error { + sdkCtx := sdk.UnwrapSDKContext(ctx) + return c.base.SetErrorData(sdkCtx, id, data) } -func (c BatchQueue) GetErrorData(ctx sdk.Context, id uint64) (*types.ErrorData, error) { - return c.base.GetErrorData(ctx, id) +func (c BatchQueue) GetErrorData(ctx context.Context, id uint64) (*types.ErrorData, error) { + sdkCtx := sdk.UnwrapSDKContext(ctx) + return c.base.GetErrorData(sdkCtx, id) } func (c BatchQueue) ChainInfo() (types.ChainType, string) { diff --git a/x/consensus/keeper/consensus/batch_test.go b/x/consensus/keeper/consensus/batch_test.go index c77a795b..b271b3b4 100644 --- a/x/consensus/keeper/consensus/batch_test.go +++ b/x/consensus/keeper/consensus/batch_test.go @@ -4,10 +4,12 @@ import ( "fmt" "testing" - tmdb "github.com/cometbft/cometbft-db" + "cosmossdk.io/log" + "cosmossdk.io/store" + "cosmossdk.io/store/metrics" + storetypes "cosmossdk.io/store/types" tmproto "github.com/cometbft/cometbft/proto/tendermint/types" - "github.com/cosmos/cosmos-sdk/store" - storetypes "github.com/cosmos/cosmos-sdk/store/types" + tmdb "github.com/cosmos/cosmos-db" sdk "github.com/cosmos/cosmos-sdk/types" keeperutil "github.com/palomachain/paloma/util/keeper" "github.com/palomachain/paloma/x/consensus/types" @@ -16,10 +18,10 @@ import ( ) func TestBatching(t *testing.T) { - storeKey := sdk.NewKVStoreKey(types.StoreKey) + storeKey := storetypes.NewKVStoreKey(types.StoreKey) memStoreKey := storetypes.NewMemoryStoreKey(types.MemStoreKey) db := tmdb.NewMemDB() - stateStore := store.NewCommitMultiStore(db) + stateStore := store.NewCommitMultiStore(db, log.NewNopLogger(), metrics.NewNoOpMetrics()) stateStore.MountStoreWithDB(storeKey, storetypes.StoreTypeIAVL, db) stateStore.MountStoreWithDB(memStoreKey, storetypes.StoreTypeMemory, nil) assert.NoError(t, stateStore.LoadLatestVersion()) diff --git a/x/consensus/keeper/consensus/consensus.go b/x/consensus/keeper/consensus/consensus.go index caa62fee..34abb7e3 100644 --- a/x/consensus/keeper/consensus/consensus.go +++ b/x/consensus/keeper/consensus/consensus.go @@ -2,11 +2,13 @@ package consensus import ( "bytes" + "context" "fmt" + "cosmossdk.io/store/prefix" + storetypes "cosmossdk.io/store/types" "github.com/cosmos/cosmos-sdk/codec" codectypes "github.com/cosmos/cosmos-sdk/codec/types" - "github.com/cosmos/cosmos-sdk/store/prefix" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/gogoproto/proto" keeperutil "github.com/palomachain/paloma/util/keeper" @@ -126,7 +128,8 @@ func NewQueue(qo QueueOptions) Queue { } // Put puts raw message into a signing queue. -func (c Queue) Put(ctx sdk.Context, msg ConsensusMsg, opts *PutOptions) (uint64, error) { +func (c Queue) Put(ctx context.Context, msg ConsensusMsg, opts *PutOptions) (uint64, error) { + sdkCtx := sdk.UnwrapSDKContext(ctx) requireSignatures := true var publicAccessData *types.PublicAccessData @@ -141,7 +144,7 @@ func (c Queue) Put(ctx sdk.Context, msg ConsensusMsg, opts *PutOptions) (uint64, if !c.qo.TypeCheck(msg) { return 0, ErrIncorrectMessageType.Format(msg) } - newID := c.qo.Ider.IncrementNextID(ctx, consensusQueueIDCounterKey) + newID := c.qo.Ider.IncrementNextID(sdkCtx, consensusQueueIDCounterKey) // just so it's clear that nonce is an actual ID nonce := newID anyMsg, err := codectypes.NewAnyWithValue(msg) @@ -152,24 +155,25 @@ func (c Queue) Put(ctx sdk.Context, msg ConsensusMsg, opts *PutOptions) (uint64, Id: newID, Msg: anyMsg, SignData: []*types.SignData{}, - AddedAtBlockHeight: ctx.BlockHeight(), - AddedAt: ctx.BlockTime(), + AddedAtBlockHeight: sdkCtx.BlockHeight(), + AddedAt: sdkCtx.BlockTime(), RequireSignatures: requireSignatures, PublicAccessData: publicAccessData, BytesToSign: c.qo.BytesToSignCalculator(msg, types.Salt{ Nonce: nonce, }), } - if err := c.save(ctx, queuedMsg); err != nil { + if err := c.save(sdkCtx, queuedMsg); err != nil { return 0, err } return newID, nil } // getAll returns all messages from a signing queu -func (c Queue) GetAll(ctx sdk.Context) ([]types.QueuedSignedMessageI, error) { +func (c Queue) GetAll(ctx context.Context) ([]types.QueuedSignedMessageI, error) { + sdkCtx := sdk.UnwrapSDKContext(ctx) var msgs []types.QueuedSignedMessageI - queue := c.queue(ctx) + queue := c.queue(sdkCtx) iterator := queue.Iterator(nil, nil) defer iterator.Close() @@ -187,20 +191,22 @@ func (c Queue) GetAll(ctx sdk.Context) ([]types.QueuedSignedMessageI, error) { return msgs, nil } -func (c Queue) AddEvidence(ctx sdk.Context, msgID uint64, evidence *types.Evidence) error { - msg, err := c.GetMsgByID(ctx, msgID) +func (c Queue) AddEvidence(ctx context.Context, msgID uint64, evidence *types.Evidence) error { + sdkCtx := sdk.UnwrapSDKContext(ctx) + msg, err := c.GetMsgByID(sdkCtx, msgID) if err != nil { return err } msg.AddEvidence(*evidence) - return c.save(ctx, msg) + return c.save(sdkCtx, msg) } // SetPublicAccessData sets data that should be visible publically so that other can provide proofs. -func (c Queue) SetPublicAccessData(ctx sdk.Context, msgID uint64, data *types.PublicAccessData) error { - msg, err := c.GetMsgByID(ctx, msgID) +func (c Queue) SetPublicAccessData(ctx context.Context, msgID uint64, data *types.PublicAccessData) error { + sdkCtx := sdk.UnwrapSDKContext(ctx) + msg, err := c.GetMsgByID(sdkCtx, msgID) if err != nil { return err } @@ -211,11 +217,12 @@ func (c Queue) SetPublicAccessData(ctx sdk.Context, msgID uint64, data *types.Pu msg.SetPublicAccessData(data) - return c.save(ctx, msg) + return c.save(sdkCtx, msg) } -func (c Queue) GetPublicAccessData(ctx sdk.Context, msgID uint64) (*types.PublicAccessData, error) { - msg, err := c.GetMsgByID(ctx, msgID) +func (c Queue) GetPublicAccessData(ctx context.Context, msgID uint64) (*types.PublicAccessData, error) { + sdkCtx := sdk.UnwrapSDKContext(ctx) + msg, err := c.GetMsgByID(sdkCtx, msgID) if err != nil { return nil, err } @@ -223,8 +230,9 @@ func (c Queue) GetPublicAccessData(ctx sdk.Context, msgID uint64) (*types.Public return msg.GetPublicAccessData(), nil } -func (c Queue) SetErrorData(ctx sdk.Context, msgID uint64, data *types.ErrorData) error { - msg, err := c.GetMsgByID(ctx, msgID) +func (c Queue) SetErrorData(ctx context.Context, msgID uint64, data *types.ErrorData) error { + sdkCtx := sdk.UnwrapSDKContext(ctx) + msg, err := c.GetMsgByID(sdkCtx, msgID) if err != nil { return err } @@ -235,11 +243,12 @@ func (c Queue) SetErrorData(ctx sdk.Context, msgID uint64, data *types.ErrorData msg.SetErrorData(data) - return c.save(ctx, msg) + return c.save(sdkCtx, msg) } -func (c Queue) GetErrorData(ctx sdk.Context, msgID uint64) (*types.ErrorData, error) { - msg, err := c.GetMsgByID(ctx, msgID) +func (c Queue) GetErrorData(ctx context.Context, msgID uint64) (*types.ErrorData, error) { + sdkCtx := sdk.UnwrapSDKContext(ctx) + msg, err := c.GetMsgByID(sdkCtx, msgID) if err != nil { return nil, err } @@ -248,8 +257,9 @@ func (c Queue) GetErrorData(ctx sdk.Context, msgID uint64) (*types.ErrorData, er } // AddSignature adds a signature to the message and checks if the signature is valid. -func (c Queue) AddSignature(ctx sdk.Context, msgID uint64, signData *types.SignData) error { - msg, err := c.GetMsgByID(ctx, msgID) +func (c Queue) AddSignature(ctx context.Context, msgID uint64, signData *types.SignData) error { + sdkCtx := sdk.UnwrapSDKContext(ctx) + msg, err := c.GetMsgByID(sdkCtx, msgID) if err != nil { return err } @@ -271,19 +281,20 @@ func (c Queue) AddSignature(ctx sdk.Context, msgID uint64, signData *types.SignD msg.AddSignData(signData) - return c.save(ctx, msg) + return c.save(sdkCtx, msg) } // remove removes the message from the queue. -func (c Queue) Remove(ctx sdk.Context, msgID uint64) error { - _, err := c.GetMsgByID(ctx, msgID) +func (c Queue) Remove(ctx context.Context, msgID uint64) error { + sdkCtx := sdk.UnwrapSDKContext(ctx) + _, err := c.GetMsgByID(sdkCtx, msgID) if err != nil { return err } - queue := c.queue(ctx) + queue := c.queue(sdkCtx) queue.Delete(sdk.Uint64ToBigEndian(msgID)) - keeperutil.EmitEvent(keeperutil.ModuleNameFunc(types.ModuleName), ctx, types.ItemRemovedEventKey, + keeperutil.EmitEvent(keeperutil.ModuleNameFunc(types.ModuleName), sdkCtx, types.ItemRemovedEventKey, types.ItemRemovedEventID.With(fmt.Sprintf("%d", msgID)), types.ItemRemovedChainReferenceID.With(c.qo.ChainReferenceID), ) @@ -291,8 +302,9 @@ func (c Queue) Remove(ctx sdk.Context, msgID uint64) error { } // getMsgByID given a message ID, it returns the message -func (c Queue) GetMsgByID(ctx sdk.Context, id uint64) (types.QueuedSignedMessageI, error) { - queue := c.queue(ctx) +func (c Queue) GetMsgByID(ctx context.Context, id uint64) (types.QueuedSignedMessageI, error) { + sdkCtx := sdk.UnwrapSDKContext(ctx) + queue := c.queue(sdkCtx) data := queue.Get(sdk.Uint64ToBigEndian(id)) if data == nil { @@ -308,7 +320,8 @@ func (c Queue) GetMsgByID(ctx sdk.Context, id uint64) (types.QueuedSignedMessage } // save saves the message into the queue -func (c Queue) save(ctx sdk.Context, msg types.QueuedSignedMessageI) error { +func (c Queue) save(ctx context.Context, msg types.QueuedSignedMessageI) error { + sdkCtx := sdk.UnwrapSDKContext(ctx) if msg.GetId() == 0 { return ErrUnableToSaveMessageWithoutID } @@ -316,13 +329,14 @@ func (c Queue) save(ctx sdk.Context, msg types.QueuedSignedMessageI) error { if err != nil { return err } - c.queue(ctx).Set(sdk.Uint64ToBigEndian(msg.GetId()), data) + c.queue(sdkCtx).Set(sdk.Uint64ToBigEndian(msg.GetId()), data) return nil } // queue is a simple helper function to return the queue store -func (c Queue) queue(ctx sdk.Context) prefix.Store { - store := c.qo.Sg.Store(ctx) +func (c Queue) queue(ctx context.Context) prefix.Store { + sdkCtx := sdk.UnwrapSDKContext(ctx) + store := c.qo.Sg.Store(sdkCtx) return prefix.NewStore(store, []byte(c.signingQueueKey())) } @@ -338,13 +352,14 @@ func (c Queue) ChainInfo() (types.ChainType, string) { return c.qo.ChainType, c.qo.ChainReferenceID } -func RemoveQueueCompletely(ctx sdk.Context, cq Queuer) { - var store sdk.KVStore +func RemoveQueueCompletely(ctx context.Context, cq Queuer) { + sdkCtx := sdk.UnwrapSDKContext(ctx) + var store storetypes.KVStore switch typ := cq.(type) { case Queue: - store = typ.queue(ctx) + store = typ.queue(sdkCtx) case BatchQueue: - store = typ.batchQueue(ctx) + store = typ.batchQueue(sdkCtx) default: panic("cq type is unknown!") } diff --git a/x/consensus/keeper/consensus/consensus_test.go b/x/consensus/keeper/consensus/consensus_test.go index c2150280..e19cc9b4 100644 --- a/x/consensus/keeper/consensus/consensus_test.go +++ b/x/consensus/keeper/consensus/consensus_test.go @@ -3,10 +3,12 @@ package consensus import ( "testing" - tmdb "github.com/cometbft/cometbft-db" + "cosmossdk.io/log" + "cosmossdk.io/store" + "cosmossdk.io/store/metrics" + storetypes "cosmossdk.io/store/types" tmproto "github.com/cometbft/cometbft/proto/tendermint/types" - "github.com/cosmos/cosmos-sdk/store" - storetypes "github.com/cosmos/cosmos-sdk/store/types" + tmdb "github.com/cosmos/cosmos-db" sdk "github.com/cosmos/cosmos-sdk/types" keeperutil "github.com/palomachain/paloma/util/keeper" "github.com/palomachain/paloma/x/consensus/types" @@ -16,10 +18,10 @@ import ( ) func TestConsensusQueueAllMethods(t *testing.T) { - storeKey := sdk.NewKVStoreKey(types.StoreKey) + storeKey := storetypes.NewKVStoreKey(types.StoreKey) memStoreKey := storetypes.NewMemoryStoreKey(types.MemStoreKey) db := tmdb.NewMemDB() - stateStore := store.NewCommitMultiStore(db) + stateStore := store.NewCommitMultiStore(db, log.NewNopLogger(), metrics.NewNoOpMetrics()) stateStore.MountStoreWithDB(storeKey, storetypes.StoreTypeIAVL, db) stateStore.MountStoreWithDB(memStoreKey, storetypes.StoreTypeMemory, nil) assert.NoError(t, stateStore.LoadLatestVersion()) diff --git a/x/consensus/keeper/consensus/types.go b/x/consensus/keeper/consensus/types.go index b5d9e78f..e4f87375 100644 --- a/x/consensus/keeper/consensus/types.go +++ b/x/consensus/keeper/consensus/types.go @@ -1,7 +1,8 @@ package consensus import ( - sdk "github.com/cosmos/cosmos-sdk/types" + "context" + "github.com/palomachain/paloma/x/consensus/types" ) @@ -20,29 +21,29 @@ type PutOptions struct { //go:generate mockery --name=Queuer type Queuer interface { - Put(sdk.Context, ConsensusMsg, *PutOptions) (uint64, error) - AddSignature(ctx sdk.Context, id uint64, signData *types.SignData) error - AddEvidence(ctx sdk.Context, id uint64, evidence *types.Evidence) error - SetPublicAccessData(ctx sdk.Context, id uint64, data *types.PublicAccessData) error - GetPublicAccessData(ctx sdk.Context, id uint64) (*types.PublicAccessData, error) - SetErrorData(ctx sdk.Context, id uint64, data *types.ErrorData) error - GetErrorData(ctx sdk.Context, id uint64) (*types.ErrorData, error) - Remove(sdk.Context, uint64) error - GetAll(sdk.Context) ([]types.QueuedSignedMessageI, error) - GetMsgByID(ctx sdk.Context, id uint64) (types.QueuedSignedMessageI, error) + Put(context.Context, ConsensusMsg, *PutOptions) (uint64, error) + AddSignature(ctx context.Context, id uint64, signData *types.SignData) error + AddEvidence(ctx context.Context, id uint64, evidence *types.Evidence) error + SetPublicAccessData(ctx context.Context, id uint64, data *types.PublicAccessData) error + GetPublicAccessData(ctx context.Context, id uint64) (*types.PublicAccessData, error) + SetErrorData(ctx context.Context, id uint64, data *types.ErrorData) error + GetErrorData(ctx context.Context, id uint64) (*types.ErrorData, error) + Remove(context.Context, uint64) error + GetAll(context.Context) ([]types.QueuedSignedMessageI, error) + GetMsgByID(ctx context.Context, id uint64) (types.QueuedSignedMessageI, error) ChainInfo() (types.ChainType, string) } type QueueBatcher interface { Queuer - ProcessBatches(ctx sdk.Context) error + ProcessBatches(ctx context.Context) error } type SupportsConsensusQueueAction struct { QueueOptions - ProcessMessageForAttestation func(ctx sdk.Context, q Queuer, msg types.QueuedSignedMessageI) error + ProcessMessageForAttestation func(ctx context.Context, q Queuer, msg types.QueuedSignedMessageI) error } type SupportsConsensusQueue interface { - SupportedQueues(ctx sdk.Context) ([]SupportsConsensusQueueAction, error) + SupportedQueues(ctx context.Context) ([]SupportsConsensusQueueAction, error) } diff --git a/x/consensus/keeper/keeper.go b/x/consensus/keeper/keeper.go index 09ffbad6..7aaffacf 100644 --- a/x/consensus/keeper/keeper.go +++ b/x/consensus/keeper/keeper.go @@ -1,22 +1,25 @@ package keeper import ( + "context" "fmt" - "github.com/cometbft/cometbft/libs/log" + "cosmossdk.io/core/store" + "cosmossdk.io/log" + storetypes "cosmossdk.io/store/types" "github.com/cosmos/cosmos-sdk/codec" - storetypes "github.com/cosmos/cosmos-sdk/store/types" + "github.com/cosmos/cosmos-sdk/runtime" sdk "github.com/cosmos/cosmos-sdk/types" paramtypes "github.com/cosmos/cosmos-sdk/x/params/types" keeperutil "github.com/palomachain/paloma/util/keeper" + "github.com/palomachain/paloma/util/liblog" "github.com/palomachain/paloma/x/consensus/types" ) type ( Keeper struct { cdc codec.BinaryCodec - storeKey storetypes.StoreKey - memKey storetypes.StoreKey + storeKey store.KVStoreService paramstore paramtypes.Subspace ider keeperutil.IDGenerator @@ -29,21 +32,15 @@ type ( func NewKeeper( cdc codec.BinaryCodec, - storeKey, - memKey storetypes.StoreKey, + storeKey store.KVStoreService, ps paramtypes.Subspace, valsetKeeper types.ValsetKeeper, reg *registry, ) *Keeper { - // set KeyTable if it has not already been set - if !ps.HasKeyTable() { - ps = ps.WithKeyTable(types.ParamKeyTable()) - } k := &Keeper{ cdc: cdc, storeKey: storeKey, - memKey: memKey, paramstore: ps, valset: valsetKeeper, registry: reg, @@ -54,10 +51,11 @@ func NewKeeper( return k } -func (k Keeper) Logger(ctx sdk.Context) log.Logger { - return ctx.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName)) +func (k Keeper) Logger(ctx context.Context) log.Logger { + sdkCtx := sdk.UnwrapSDKContext(ctx) + return liblog.FromSDKLogger(k.Logger(sdkCtx)).With("module", fmt.Sprintf("x/%s", types.ModuleName)) } -func (k Keeper) Store(ctx sdk.Context) sdk.KVStore { - return ctx.KVStore(k.storeKey) +func (k Keeper) Store(ctx context.Context) storetypes.KVStore { + return runtime.KVStoreAdapter(k.storeKey.OpenKVStore(ctx)) } diff --git a/x/consensus/keeper/keeper_setup_test.go b/x/consensus/keeper/keeper_setup_test.go index 7e20e495..4ecc4cf9 100644 --- a/x/consensus/keeper/keeper_setup_test.go +++ b/x/consensus/keeper/keeper_setup_test.go @@ -3,13 +3,14 @@ package keeper import ( "testing" - tmdb "github.com/cometbft/cometbft-db" - "github.com/cometbft/cometbft/libs/log" + "cosmossdk.io/log" + "cosmossdk.io/store" + "cosmossdk.io/store/metrics" + storetypes "cosmossdk.io/store/types" tmproto "github.com/cometbft/cometbft/proto/tendermint/types" + tmdb "github.com/cosmos/cosmos-db" "github.com/cosmos/cosmos-sdk/codec" codectypes "github.com/cosmos/cosmos-sdk/codec/types" - "github.com/cosmos/cosmos-sdk/store" - storetypes "github.com/cosmos/cosmos-sdk/store/types" sdk "github.com/cosmos/cosmos-sdk/types" typesparams "github.com/cosmos/cosmos-sdk/x/params/types" "github.com/palomachain/paloma/x/consensus/types" @@ -25,11 +26,11 @@ type mockedServices struct { func newConsensusKeeper(t testing.TB) (*Keeper, mockedServices, sdk.Context) { logger := log.NewNopLogger() - storeKey := sdk.NewKVStoreKey(types.StoreKey) + storeKey := storetypes.NewKVStoreKey(types.StoreKey) memStoreKey := storetypes.NewMemoryStoreKey(types.MemStoreKey) db := tmdb.NewMemDB() - stateStore := store.NewCommitMultiStore(db) + stateStore := store.NewCommitMultiStore(db, log.NewNopLogger(), metrics.NewNoOpMetrics()) stateStore.MountStoreWithDB(storeKey, storetypes.StoreTypeIAVL, db) stateStore.MountStoreWithDB(memStoreKey, storetypes.StoreTypeMemory, nil) require.NoError(t, stateStore.LoadLatestVersion()) diff --git a/x/consensus/keeper/params.go b/x/consensus/keeper/params.go index 09347e5f..a96ed3e4 100644 --- a/x/consensus/keeper/params.go +++ b/x/consensus/keeper/params.go @@ -1,16 +1,19 @@ package keeper import ( + "context" + sdk "github.com/cosmos/cosmos-sdk/types" "github.com/palomachain/paloma/x/consensus/types" ) // GetParams get all parameters as types.Params -func (k Keeper) GetParams(ctx sdk.Context) types.Params { +func (k Keeper) GetParams(ctx context.Context) types.Params { return types.NewParams() } // SetParams set the params -func (k Keeper) SetParams(ctx sdk.Context, params types.Params) { - k.paramstore.SetParamSet(ctx, ¶ms) +func (k Keeper) SetParams(ctx context.Context, params types.Params) { + sdkCtx := sdk.UnwrapSDKContext(ctx) + k.paramstore.SetParamSet(sdkCtx, ¶ms) } diff --git a/x/consensus/module.go b/x/consensus/module.go index e8bcb676..c8da1491 100644 --- a/x/consensus/module.go +++ b/x/consensus/module.go @@ -1,6 +1,7 @@ package consensus import ( + "context" "encoding/json" "fmt" @@ -113,6 +114,11 @@ func NewAppModule( } } +// This Function is required by cosmos-sdk but unused. https://github.com/cosmos/cosmos-sdk/blob/7d5c2dbae68530bc9070983ce0b2c8a1efe23412/x/bank/module.go#L100 +func (am AppModule) IsOnePerModuleType() {} + +func (am AppModule) IsAppModule() {} + // Name returns the capability module's name. func (am AppModule) Name() string { return am.AppModuleBasic.Name() @@ -133,7 +139,7 @@ func (am AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {} // InitGenesis performs the capability module's genesis initialization It returns // no validator updates. -func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, gs json.RawMessage) []abci.ValidatorUpdate { +func (am AppModule) InitGenesis(ctx context.Context, cdc codec.JSONCodec, gs json.RawMessage) []abci.ValidatorUpdate { var genState types.GenesisState // Initialize global index to index in genesis state cdc.MustUnmarshalJSON(gs, &genState) @@ -144,7 +150,7 @@ func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, gs json.Ra } // ExportGenesis returns the capability module's exported genesis state as raw JSON bytes. -func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.RawMessage { +func (am AppModule) ExportGenesis(ctx context.Context, cdc codec.JSONCodec) json.RawMessage { genState := ExportGenesis(ctx, am.keeper) return cdc.MustMarshalJSON(genState) } @@ -153,12 +159,13 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw func (AppModule) ConsensusVersion() uint64 { return 1 } // BeginBlock executes all ABCI BeginBlock logic respective to the capability module. -func (am AppModule) BeginBlock(ctx sdk.Context, _ abci.RequestBeginBlock) { +func (am AppModule) BeginBlock(context.Context) { } // EndBlock executes all ABCI EndBlock logic respective to the capability module. It // returns no validator updates. -func (am AppModule) EndBlock(ctx sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate { +func (am AppModule) EEndBlock(ct context.Context) ([]abci.ValidatorUpdate, error) { + ctx := sdk.UnwrapSDKContext(ct) am.keeper.Logger(ctx).Info("abci-validator-size", abci.ValidatorUpdates{}.Len()) if err := am.keeper.CheckAndProcessAttestedMessages(ctx); err != nil { am.keeper.Logger(ctx).Error("error while attesting to messages", "err", err) @@ -170,5 +177,5 @@ func (am AppModule) EndBlock(ctx sdk.Context, _ abci.RequestEndBlock) []abci.Val am.keeper.Logger(ctx).Error("error while deleting old messages", "err", err) } } - return []abci.ValidatorUpdate{} + return []abci.ValidatorUpdate{}, nil } diff --git a/x/consensus/module_simulation.go b/x/consensus/module_simulation.go index b0412ce0..4d1fe525 100644 --- a/x/consensus/module_simulation.go +++ b/x/consensus/module_simulation.go @@ -4,7 +4,6 @@ import ( "math/rand" "github.com/cosmos/cosmos-sdk/baseapp" - sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/module" simtypes "github.com/cosmos/cosmos-sdk/types/simulation" "github.com/cosmos/cosmos-sdk/x/simulation" @@ -55,14 +54,14 @@ func (AppModule) ProposalContents(_ module.SimulationState) []simtypes.WeightedP } // RegisterStoreDecoder registers a decoder -func (am AppModule) RegisterStoreDecoder(_ sdk.StoreDecoderRegistry) {} +func (am AppModule) RegisterStoreDecoder(_ simtypes.StoreDecoderRegistry) {} // WeightedOperations returns the all the gov module operations with their respective weights. func (am AppModule) WeightedOperations(simState module.SimulationState) []simtypes.WeightedOperation { operations := make([]simtypes.WeightedOperation, 0) var weightMsgAddMessagesSignatures int - simState.AppParams.GetOrGenerate(simState.Cdc, opWeightMsgAddMessagesSignatures, &weightMsgAddMessagesSignatures, nil, + simState.AppParams.GetOrGenerate(opWeightMsgAddMessagesSignatures, &weightMsgAddMessagesSignatures, nil, func(_ *rand.Rand) { weightMsgAddMessagesSignatures = defaultWeightMsgAddMessagesSignatures }, @@ -73,7 +72,7 @@ func (am AppModule) WeightedOperations(simState module.SimulationState) []simtyp )) var weightMsgDeleteJob int - simState.AppParams.GetOrGenerate(simState.Cdc, opWeightMsgDeleteJob, &weightMsgDeleteJob, nil, + simState.AppParams.GetOrGenerate(opWeightMsgDeleteJob, &weightMsgDeleteJob, nil, func(_ *rand.Rand) { weightMsgDeleteJob = defaultWeightMsgDeleteJob }, @@ -84,7 +83,7 @@ func (am AppModule) WeightedOperations(simState module.SimulationState) []simtyp )) var weightMsgAddEvidence int - simState.AppParams.GetOrGenerate(simState.Cdc, opWeightMsgAddEvidence, &weightMsgAddEvidence, nil, + simState.AppParams.GetOrGenerate(opWeightMsgAddEvidence, &weightMsgAddEvidence, nil, func(_ *rand.Rand) { weightMsgAddEvidence = defaultWeightMsgAddEvidence }, @@ -95,7 +94,7 @@ func (am AppModule) WeightedOperations(simState module.SimulationState) []simtyp )) var weightMsgSetPublicAccessData int - simState.AppParams.GetOrGenerate(simState.Cdc, opWeightMsgSetPublicAccessData, &weightMsgSetPublicAccessData, nil, + simState.AppParams.GetOrGenerate(opWeightMsgSetPublicAccessData, &weightMsgSetPublicAccessData, nil, func(_ *rand.Rand) { weightMsgSetPublicAccessData = defaultWeightMsgSetPublicAccessData }, diff --git a/x/consensus/types/attest.go b/x/consensus/types/attest.go index 153187c3..13c318c1 100644 --- a/x/consensus/types/attest.go +++ b/x/consensus/types/attest.go @@ -1,8 +1,6 @@ package types -import ( - sdk "github.com/cosmos/cosmos-sdk/types" -) +import "context" //go:generate mockery --name=AttestTask type AttestTask interface { @@ -21,8 +19,8 @@ type AttestResult struct { //go:generate mockery --name=Attestator type Attestator interface { // ValidateEvidence takes a task and an evidence and does a validation to make sure that it's correct. - ValidateEvidence(ctx sdk.Context, task AttestTask, evidence Evidence) error + ValidateEvidence(ctx context.Context, task AttestTask, evidence Evidence) error // ProcessAllEvidence processes all given evidences and internally does whatever it needs to do with // that information. It returns the result back to the caller. - ProcessAllEvidence(ctx sdk.Context, task AttestTask, evidence []Evidence) (AttestResult, error) + ProcessAllEvidence(ctx context.Context, task AttestTask, evidence []Evidence) (AttestResult, error) } diff --git a/x/consensus/types/errors.go b/x/consensus/types/errors.go index 21f5660e..eb0dc1ed 100644 --- a/x/consensus/types/errors.go +++ b/x/consensus/types/errors.go @@ -3,7 +3,7 @@ package types // DONTCOVER import ( - sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" + sdkerrors "cosmossdk.io/errors" ) // x/consensus module sentinel errors diff --git a/x/consensus/types/expected_keepers.go b/x/consensus/types/expected_keepers.go index e13c1b3b..0385628a 100644 --- a/x/consensus/types/expected_keepers.go +++ b/x/consensus/types/expected_keepers.go @@ -1,27 +1,28 @@ package types import ( + "context" + sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/x/auth/types" valsettypes "github.com/palomachain/paloma/x/valset/types" ) // AccountKeeper defines the expected account keeper used for simulations (noalias) type AccountKeeper interface { - GetAccount(ctx sdk.Context, addr sdk.AccAddress) types.AccountI + GetAccount(ctx context.Context, addr sdk.AccAddress) sdk.AccountI // Methods imported from account should be defined here } // BankKeeper defines the expected interface needed to retrieve account balances. type BankKeeper interface { - SpendableCoins(ctx sdk.Context, addr sdk.AccAddress) sdk.Coins + SpendableCoins(ctx context.Context, addr sdk.AccAddress) sdk.Coins // Methods imported from bank should be defined here } //go:generate mockery --name=ValsetKeeper type ValsetKeeper interface { - GetSigningKey(ctx sdk.Context, valAddr sdk.ValAddress, chainType, chainReferenceID, signedByAddress string) ([]byte, error) - GetCurrentSnapshot(ctx sdk.Context) (*valsettypes.Snapshot, error) - CanAcceptValidator(ctx sdk.Context, valAddr sdk.ValAddress) error - KeepValidatorAlive(ctx sdk.Context, valAddr sdk.ValAddress, pigeonVersion string) error + GetSigningKey(ctx context.Context, valAddr sdk.ValAddress, chainType, chainReferenceID, signedByAddress string) ([]byte, error) + GetCurrentSnapshot(ctx context.Context) (*valsettypes.Snapshot, error) + CanAcceptValidator(ctx context.Context, valAddr sdk.ValAddress) error + KeepValidatorAlive(ctx context.Context, valAddr sdk.ValAddress, pigeonVersion string) error } diff --git a/x/consensus/types/genesis.go b/x/consensus/types/genesis.go index e8bbda15..86f12d71 100644 --- a/x/consensus/types/genesis.go +++ b/x/consensus/types/genesis.go @@ -1,7 +1,7 @@ package types import ( - host "github.com/cosmos/ibc-go/v7/modules/core/24-host" + host "github.com/cosmos/ibc-go/v8/modules/core/24-host" ) // DefaultIndex is the default capability global index diff --git a/x/consensus/types/message_add_evidence.go b/x/consensus/types/message_add_evidence.go index e68e24a5..252eac51 100644 --- a/x/consensus/types/message_add_evidence.go +++ b/x/consensus/types/message_add_evidence.go @@ -9,10 +9,6 @@ const TypeMsgAddEvidence = "add_evidence" var _ sdk.Msg = &MsgAddEvidence{} -func (msg *MsgAddEvidence) Route() string { - return RouterKey -} - func (msg *MsgAddEvidence) Type() string { return TypeMsgAddEvidence } @@ -21,11 +17,6 @@ func (msg *MsgAddEvidence) GetSigners() []sdk.AccAddress { return libmeta.GetSigners(msg) } -func (msg *MsgAddEvidence) GetSignBytes() []byte { - bz := ModuleCdc.MustMarshalJSON(msg) - return sdk.MustSortJSON(bz) -} - func (msg *MsgAddEvidence) ValidateBasic() error { return libmeta.ValidateBasic(msg) } diff --git a/x/consensus/types/message_add_messages_signatures.go b/x/consensus/types/message_add_messages_signatures.go index b662da85..679a0d4b 100644 --- a/x/consensus/types/message_add_messages_signatures.go +++ b/x/consensus/types/message_add_messages_signatures.go @@ -15,10 +15,6 @@ func NewMsgAddMessagesSignatures(creator string) *MsgAddMessagesSignatures { } } -func (msg *MsgAddMessagesSignatures) Route() string { - return RouterKey -} - func (msg *MsgAddMessagesSignatures) Type() string { return TypeMsgAddMessagesSignatures } @@ -27,11 +23,6 @@ func (msg *MsgAddMessagesSignatures) GetSigners() []sdk.AccAddress { return libmeta.GetSigners(msg) } -func (msg *MsgAddMessagesSignatures) GetSignBytes() []byte { - bz := ModuleCdc.MustMarshalJSON(msg) - return sdk.MustSortJSON(bz) -} - func (msg *MsgAddMessagesSignatures) ValidateBasic() error { return libmeta.ValidateBasic(msg) } diff --git a/x/consensus/types/message_delete_job.go b/x/consensus/types/message_delete_job.go index 8bf0bb5b..8efedd9e 100644 --- a/x/consensus/types/message_delete_job.go +++ b/x/consensus/types/message_delete_job.go @@ -22,10 +22,6 @@ func NewMsgDeleteJob(creator string, queueTypeName string, messageID uint64) *Ms } } -func (msg *MsgDeleteJob) Route() string { - return RouterKey -} - func (msg *MsgDeleteJob) Type() string { return TypeMsgDeleteJob } @@ -34,11 +30,6 @@ func (msg *MsgDeleteJob) GetSigners() []sdk.AccAddress { return libmeta.GetSigners(msg) } -func (msg *MsgDeleteJob) GetSignBytes() []byte { - bz := ModuleCdc.MustMarshalJSON(msg) - return sdk.MustSortJSON(bz) -} - func (msg *MsgDeleteJob) ValidateBasic() error { return libmeta.ValidateBasic(msg) } diff --git a/x/consensus/types/message_set_error_data.go b/x/consensus/types/message_set_error_data.go index 20ac3194..2d545cb8 100644 --- a/x/consensus/types/message_set_error_data.go +++ b/x/consensus/types/message_set_error_data.go @@ -9,10 +9,6 @@ const TypeMsgSetErrorData = "set_error_data" var _ sdk.Msg = &MsgSetErrorData{} -func (msg *MsgSetErrorData) Route() string { - return RouterKey -} - func (msg *MsgSetErrorData) Type() string { return TypeMsgSetErrorData } @@ -21,11 +17,6 @@ func (msg *MsgSetErrorData) GetSigners() []sdk.AccAddress { return libmeta.GetSigners(msg) } -func (msg *MsgSetErrorData) GetSignBytes() []byte { - bz := ModuleCdc.MustMarshalJSON(msg) - return sdk.MustSortJSON(bz) -} - func (msg *MsgSetErrorData) ValidateBasic() error { return libmeta.ValidateBasic(msg) } diff --git a/x/consensus/types/message_set_public_access_data.go b/x/consensus/types/message_set_public_access_data.go index d2362b2a..11336468 100644 --- a/x/consensus/types/message_set_public_access_data.go +++ b/x/consensus/types/message_set_public_access_data.go @@ -9,10 +9,6 @@ const TypeMsgSetPublicAccessData = "set_public_access_data" var _ sdk.Msg = &MsgSetPublicAccessData{} -func (msg *MsgSetPublicAccessData) Route() string { - return RouterKey -} - func (msg *MsgSetPublicAccessData) Type() string { return TypeMsgSetPublicAccessData } @@ -21,11 +17,6 @@ func (msg *MsgSetPublicAccessData) GetSigners() []sdk.AccAddress { return libmeta.GetSigners(msg) } -func (msg *MsgSetPublicAccessData) GetSignBytes() []byte { - bz := ModuleCdc.MustMarshalJSON(msg) - return sdk.MustSortJSON(bz) -} - func (msg *MsgSetPublicAccessData) ValidateBasic() error { return libmeta.ValidateBasic(msg) } diff --git a/x/consensus/types/mocks/Attestator.go b/x/consensus/types/mocks/Attestator.go index 17d709a3..46c94086 100644 --- a/x/consensus/types/mocks/Attestator.go +++ b/x/consensus/types/mocks/Attestator.go @@ -3,6 +3,7 @@ package mocks import ( + "context" types "github.com/cosmos/cosmos-sdk/types" consensustypes "github.com/palomachain/paloma/x/consensus/types" mock "github.com/stretchr/testify/mock" @@ -14,22 +15,23 @@ type Attestator struct { } // ProcessAllEvidence provides a mock function with given fields: ctx, task, evidence -func (_m *Attestator) ProcessAllEvidence(ctx types.Context, task consensustypes.AttestTask, evidence []consensustypes.Evidence) (consensustypes.AttestResult, error) { +func (_m *Attestator) ProcessAllEvidence(ctx context.Context, task consensustypes.AttestTask, evidence []consensustypes.Evidence) (consensustypes.AttestResult, error) { ret := _m.Called(ctx, task, evidence) + sdkCtx:=types.UnwrapSDKContext(ctx) var r0 consensustypes.AttestResult var r1 error if rf, ok := ret.Get(0).(func(types.Context, consensustypes.AttestTask, []consensustypes.Evidence) (consensustypes.AttestResult, error)); ok { - return rf(ctx, task, evidence) + return rf(sdkCtx, task, evidence) } if rf, ok := ret.Get(0).(func(types.Context, consensustypes.AttestTask, []consensustypes.Evidence) consensustypes.AttestResult); ok { - r0 = rf(ctx, task, evidence) + r0 = rf(sdkCtx, task, evidence) } else { r0 = ret.Get(0).(consensustypes.AttestResult) } if rf, ok := ret.Get(1).(func(types.Context, consensustypes.AttestTask, []consensustypes.Evidence) error); ok { - r1 = rf(ctx, task, evidence) + r1 = rf(sdkCtx, task, evidence) } else { r1 = ret.Error(1) } @@ -38,12 +40,12 @@ func (_m *Attestator) ProcessAllEvidence(ctx types.Context, task consensustypes. } // ValidateEvidence provides a mock function with given fields: ctx, task, evidence -func (_m *Attestator) ValidateEvidence(ctx types.Context, task consensustypes.AttestTask, evidence consensustypes.Evidence) error { +func (_m *Attestator) ValidateEvidence(ctx context.Context, task consensustypes.AttestTask, evidence consensustypes.Evidence) error { ret := _m.Called(ctx, task, evidence) - + sdkCtx:=types.UnwrapSDKContext(ctx) var r0 error if rf, ok := ret.Get(0).(func(types.Context, consensustypes.AttestTask, consensustypes.Evidence) error); ok { - r0 = rf(ctx, task, evidence) + r0 = rf(sdkCtx, task, evidence) } else { r0 = ret.Error(0) }