-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from bze-alphateam/feature/epochs
Added Epochs module
- Loading branch information
Showing
68 changed files
with
19,743 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,28 @@ | ||
package v700 | ||
|
||
import ( | ||
tokenfactorykeeper "github.com/bze-alphateam/bze/x/tokenfactory/keeper" | ||
tokenfactorytypes "github.com/bze-alphateam/bze/x/tokenfactory/types" | ||
tradebinkeeper "github.com/bze-alphateam/bze/x/tradebin/keeper" | ||
tradebintypes "github.com/bze-alphateam/bze/x/tradebin/types" | ||
burnermodule "github.com/bze-alphateam/bze/x/burner" | ||
burnermoduletypes "github.com/bze-alphateam/bze/x/burner/types" | ||
cointrunkmodule "github.com/bze-alphateam/bze/x/cointrunk" | ||
cointrunkmoduletypes "github.com/bze-alphateam/bze/x/cointrunk/types" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/types/module" | ||
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types" | ||
) | ||
|
||
const UpgradeName = "v7.0.0" | ||
|
||
func CreateUpgradeHandler(factoryKeeper *tokenfactorykeeper.Keeper, tbinKeeper *tradebinkeeper.Keeper) upgradetypes.UpgradeHandler { | ||
func CreateUpgradeHandler( | ||
cfg module.Configurator, | ||
mm *module.Manager, | ||
) upgradetypes.UpgradeHandler { | ||
|
||
return func(ctx sdk.Context, _plan upgradetypes.Plan, vm module.VersionMap) (module.VersionMap, error) { | ||
//set tokenfactory module default params | ||
factoryKeeper.SetParams(ctx, tokenfactorytypes.DefaultParams()) | ||
tbinKeeper.SetParams(ctx, tradebintypes.DefaultParams()) | ||
return vm, nil | ||
//add older modules not present in module_versions yet | ||
vm[burnermoduletypes.ModuleName] = burnermodule.AppModule{}.ConsensusVersion() | ||
vm[cointrunkmoduletypes.ModuleName] = cointrunkmodule.AppModule{}.ConsensusVersion() | ||
|
||
//run default migrations in order to init new module's genesis and to have them in vm | ||
return mm.RunMigrations(ctx, cfg, vm) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package bzeutils | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"runtime" | ||
"runtime/debug" | ||
|
||
"github.com/cosmos/cosmos-sdk/store/types" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
) | ||
|
||
// ApplyFuncIfNoError This function lets you run the function f, but if there's an error or panic | ||
// drop the state machine change and log the error. | ||
// If there is no error, proceeds as normal (but with some slowdown due to SDK store weirdness) | ||
// Try to avoid usage of iterators in f. | ||
// | ||
// If its an out of gas panic, this function will also panic like in normal tx execution flow. | ||
// This is still safe for beginblock / endblock code though, as they do not have out of gas panics. | ||
func ApplyFuncIfNoError(ctx sdk.Context, f func(ctx sdk.Context) error) (err error) { | ||
return applyFunc(ctx, f, ctx.Logger().Error) | ||
} | ||
|
||
// ApplyFuncIfNoErrorLogToDebug is the same as ApplyFuncIfNoError, but sends logs to debug instead of error if there is an error. | ||
func ApplyFuncIfNoErrorLogToDebug(ctx sdk.Context, f func(ctx sdk.Context) error) (err error) { | ||
return applyFunc(ctx, f, ctx.Logger().Debug) | ||
} | ||
|
||
func applyFunc(ctx sdk.Context, f func(ctx sdk.Context) error, logFunc func(string, ...interface{})) (err error) { | ||
// Add a panic safeguard | ||
defer func() { | ||
if recoveryError := recover(); recoveryError != nil { | ||
if isErr, _ := IsOutOfGasError(recoveryError); isErr { | ||
// We panic with the same error, to replicate the normal tx execution flow. | ||
panic(recoveryError) | ||
} else { | ||
PrintPanicRecoveryError(ctx, recoveryError) | ||
err = errors.New("panic occurred during execution") | ||
} | ||
} | ||
}() | ||
// makes a new cache context, which all state changes get wrapped inside of. | ||
cacheCtx, write := ctx.CacheContext() | ||
err = f(cacheCtx) | ||
if err != nil { | ||
logFunc(err.Error()) | ||
} else { | ||
// no error, write the output of f | ||
write() | ||
} | ||
return err | ||
} | ||
|
||
// IsOutOfGasError Frustratingly, this has to return the error descriptor, not an actual error itself | ||
// because the SDK errors here are not actually errors. (They don't implement error interface) | ||
func IsOutOfGasError(err any) (bool, string) { | ||
switch e := err.(type) { | ||
case types.ErrorOutOfGas: | ||
return true, e.Descriptor | ||
case types.ErrorGasOverflow: | ||
return true, e.Descriptor | ||
default: | ||
return false, "" | ||
} | ||
} | ||
|
||
// PrintPanicRecoveryError error logs the recoveryError, along with the stacktrace, if it can be parsed. | ||
// If not emits them to stdout. | ||
func PrintPanicRecoveryError(ctx sdk.Context, recoveryError interface{}) { | ||
errStackTrace := string(debug.Stack()) | ||
switch e := recoveryError.(type) { | ||
case types.ErrorOutOfGas: | ||
ctx.Logger().Debug("out of gas error inside panic recovery block: " + e.Descriptor) | ||
return | ||
case string: | ||
ctx.Logger().Error("Recovering from (string) panic: " + e) | ||
case runtime.Error: | ||
ctx.Logger().Error("recovered (runtime.Error) panic: " + e.Error()) | ||
case error: | ||
ctx.Logger().Error("recovered (error) panic: " + e.Error()) | ||
default: | ||
ctx.Logger().Error("recovered (default) panic. Could not capture logs in ctx, see stdout") | ||
fmt.Println("Recovering from panic ", recoveryError) | ||
debug.PrintStack() | ||
return | ||
} | ||
ctx.Logger().Error("stack trace: " + errStackTrace) | ||
} |
Oops, something went wrong.