Skip to content

Commit

Permalink
Add the upgrade and gov logger
Browse files Browse the repository at this point in the history
  • Loading branch information
MrXJC committed Jan 18, 2019
1 parent c498507 commit 4b63605
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 27 deletions.
22 changes: 10 additions & 12 deletions modules/gov/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@ func Execute(ctx sdk.Context, gk Keeper, p Proposal) (err error) {
}

func TaxUsageProposalExecute(ctx sdk.Context, gk Keeper, p *TaxUsageProposal) (err error) {
logger := ctx.Logger().With("module", "x/gov")
burn := false
if p.TaxUsage.Usage == UsageTypeBurn {
burn = true
} else {
_, found := gk.guardianKeeper.GetTrustee(ctx, p.TaxUsage.DestAddress)
if !found {
logger.Error("Execute TaxUsageProposal Failure", "info",
ctx.Logger().Error("Execute TaxUsageProposal Failure", "info",
fmt.Sprintf("the destination address [%s] is not a trustee now", p.TaxUsage.DestAddress))
return
}
Expand All @@ -37,45 +36,44 @@ func TaxUsageProposalExecute(ctx sdk.Context, gk Keeper, p *TaxUsageProposal) (e
}

func ParameterProposalExecute(ctx sdk.Context, gk Keeper, pp *ParameterProposal) (err error) {
logger := ctx.Logger().With("module", "x/gov")
logger.Info("Execute ParameterProposal begin", "info", fmt.Sprintf("current height:%d", ctx.BlockHeight()))
ctx.Logger().Info("Execute ParameterProposal begin", "info", fmt.Sprintf("current height:%d", ctx.BlockHeight()))
for _, param := range pp.Params {
paramSet, _ := gk.paramsKeeper.GetParamSet(param.Subspace)
value, _ := paramSet.Validate(param.Key, param.Value)
subspace, found := gk.paramsKeeper.GetSubspace(param.Subspace)
if found {
subspace.Set(ctx, []byte(param.Key), value)
ctx.Logger().Info("Execute ParameterProposal Successed", "key", param.Key, "value", param.Value)
} else {
ctx.Logger().Info("Execute ParameterProposal Failed", "key", param.Key, "value", param.Value)
}

logger.Info("Execute ParameterProposal begin", "info", fmt.Sprintf("%s = %s", param.Key, param.Value))
}

return
}

func SoftwareUpgradeProposalExecute(ctx sdk.Context, gk Keeper, sp *SoftwareUpgradeProposal) error {
logger := ctx.Logger().With("module", "x/gov")

if _, ok := gk.protocolKeeper.GetUpgradeConfig(ctx); ok {
logger.Info("Execute SoftwareProposal Failure", "info",
fmt.Sprintf("Software Upgrade Switch Period is in process. current height:%d", ctx.BlockHeight()))
ctx.Logger().Info("Execute SoftwareProposal Failure", "info",
fmt.Sprintf("Software Upgrade Switch Period is in process."))
return nil
}
if !gk.protocolKeeper.IsValidVersion(ctx, sp.ProtocolDefinition.Version) {
logger.Info("Execute SoftwareProposal Failure", "info",
ctx.Logger().Info("Execute SoftwareProposal Failure", "info",
fmt.Sprintf("version [%v] in SoftwareUpgradeProposal isn't valid ", sp.ProposalID))
return nil
}
if uint64(ctx.BlockHeight())+1 >= sp.ProtocolDefinition.Height {
logger.Info("Execute SoftwareProposal Failure", "info",
ctx.Logger().Info("Execute SoftwareProposal Failure", "info",
fmt.Sprintf("switch height must be more than blockHeight + 1"))
return nil
}

gk.protocolKeeper.SetUpgradeConfig(ctx, sdk.NewUpgradeConfig(sp.ProposalID, sp.ProtocolDefinition))

logger.Info("Execute SoftwareProposal Success", "info",
fmt.Sprintf("current height:%d", ctx.BlockHeight()))
ctx.Logger().Info("Execute SoftwareProposal Success")

return nil
}
Expand Down
9 changes: 4 additions & 5 deletions modules/gov/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,13 +229,12 @@ func handleMsgVote(ctx sdk.Context, keeper Keeper, msg MsgVote) sdk.Result {
// Called every block, process inflation, update validator set
func EndBlocker(ctx sdk.Context, keeper Keeper) (resTags sdk.Tags) {

logger := ctx.Logger().With("module", "x/gov")

ctx = ctx.WithLogger(ctx.Logger().With("handler", "endBlock").With("module", "iris/gov"))
resTags = sdk.NewTags()

if ctx.BlockHeight() == keeper.GetSystemHaltHeight(ctx) {
resTags = resTags.AppendTag(tmstate.HaltTagKey, []byte(tmstate.HaltTagValue))
logger.Info(fmt.Sprintf("SystemHalt Start!!!"))
ctx.Logger().Info(fmt.Sprintf("SystemHalt Start!!!"))
}

inactiveIterator := keeper.InactiveProposalQueueIterator(ctx, ctx.BlockHeader().Time)
Expand All @@ -252,7 +251,7 @@ func EndBlocker(ctx sdk.Context, keeper Keeper) (resTags sdk.Tags) {
resTags = resTags.AppendTag(tags.ProposalID, []byte(string(proposalID)))

keeper.RemoveFromInactiveProposalQueue(ctx, inactiveProposal.GetDepositEndTime(), inactiveProposal.GetProposalID())
logger.Info(
ctx.Logger().Info(
fmt.Sprintf("proposal %d (%s) didn't meet minimum deposit of %s (had only %s); deleted",
inactiveProposal.GetProposalID(),
inactiveProposal.GetTitle(),
Expand Down Expand Up @@ -288,7 +287,7 @@ func EndBlocker(ctx sdk.Context, keeper Keeper) (resTags sdk.Tags) {
keeper.RemoveFromActiveProposalQueue(ctx, activeProposal.GetVotingEndTime(), activeProposal.GetProposalID())
activeProposal.SetTallyResult(tallyResults)
keeper.SetProposal(ctx, activeProposal)
logger.Info(fmt.Sprintf("proposal %d (%s) tallied; result: %v",
ctx.Logger().Info(fmt.Sprintf("proposal %d (%s) tallied; result: %v",
activeProposal.GetProposalID(), activeProposal.GetTitle(), result))

resTags = resTags.AppendTag(tags.Action, action)
Expand Down
5 changes: 5 additions & 0 deletions modules/gov/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,11 @@ func (keeper Keeper) SubNormalProposalNum(ctx sdk.Context) {
}

func (keeper Keeper) HasReachedTheMaxProposalNum(ctx sdk.Context, pl ProposalLevel) (uint64, bool) {
ctx.Logger().Debug("Proposals Distribution",
"CriticalProposalNum" , keeper.GetCriticalProposalNum(ctx),
"ImportantProposalNum", keeper.GetImportantProposalNum(ctx),
"NormalProposalNum" , keeper.GetNormalProposalNum(ctx))

maxNum := keeper.GetMaxNumByProposalLevel(ctx, pl)
switch pl {
case ProposalLevelCritical:
Expand Down
29 changes: 24 additions & 5 deletions modules/upgrade/handler.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,46 @@
package upgrade

import (
"fmt"
sdk "github.com/irisnet/irishub/types"
"strconv"
)

// do switch
func EndBlocker(ctx sdk.Context, uk Keeper) (tags sdk.Tags) {

ctx = ctx.WithLogger(ctx.Logger().With("handler", "endBlock").With("module", "iris/distribution"))

tags = sdk.NewTags()
upgradeConfig,ok := uk.protocolKeeper.GetUpgradeConfig(ctx)
upgradeConfig, ok := uk.protocolKeeper.GetUpgradeConfig(ctx)
if ok {
validator,found := uk.sk.GetValidatorByConsAddr(ctx,(sdk.ConsAddress)(ctx.BlockHeader().ProposerAddress));
if!found {
panic(fmt.Sprint("Proposer is not a bonded validator whose consaddress is %s", (sdk.ConsAddress)(ctx.BlockHeader().ProposerAddress).String()))
}

if ctx.BlockHeader().Version.App == upgradeConfig.Protocol.Version {
uk.SetSignal(ctx, upgradeConfig.Protocol.Version, (sdk.ConsAddress)(ctx.BlockHeader().ProposerAddress).String())
uk.SetSignal(ctx, upgradeConfig.Protocol.Version, validator.ConsAddress().String())

ctx.Logger().Info(
fmt.Sprintf("Validator ConsAddress %s has downloaded the latest software which protocolVersion is %d ",
validator.GetOperator().String(), ctx, upgradeConfig.Protocol.Version))
} else {
uk.DeleteSignal(ctx, upgradeConfig.Protocol.Version, (sdk.ConsAddress)(ctx.BlockHeader().ProposerAddress).String())
uk.DeleteSignal(ctx, upgradeConfig.Protocol.Version, validator.ConsAddress().String())

ctx.Logger().Info(
fmt.Sprintf("Validator ConsAddress %s has restarted the old software which protocolVersion is %d ",
validator.GetOperator().String(), ctx, upgradeConfig.Protocol.Version))
}

if uint64(ctx.BlockHeight())+1 == upgradeConfig.Protocol.Height {
success := tally(ctx, upgradeConfig.Protocol.Version, uk, upgradeConfig.Protocol.Threshold)

if success {
ctx.Logger().Info("Software Upgrade is successful.", "version", upgradeConfig.Protocol.Version)
uk.protocolKeeper.SetCurrentVersion(ctx, upgradeConfig.Protocol.Version)
} else {
ctx.Logger().Info("Software Upgrade is failure.", "version", upgradeConfig.Protocol.Version)
uk.protocolKeeper.SetLastFailedVersion(ctx, upgradeConfig.Protocol.Version)
}

Expand All @@ -29,8 +49,7 @@ func EndBlocker(ctx sdk.Context, uk Keeper) (tags sdk.Tags) {
}
}

// TODO: const CurrentVersionTagKey CurrentSoftwareTagKey
tags = tags.AppendTag(sdk.AppVersionTag, []byte(strconv.FormatUint(uk.protocolKeeper.GetCurrentVersion(ctx),10)))
tags = tags.AppendTag(sdk.AppVersionTag, []byte(strconv.FormatUint(uk.protocolKeeper.GetCurrentVersion(ctx), 10)))

return tags
}
13 changes: 8 additions & 5 deletions modules/upgrade/tally.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,26 @@ import (
sdk "github.com/irisnet/irishub/types"
)

func tally(ctx sdk.Context,versionProtocol uint64, k Keeper, threshold sdk.Dec) (passes bool) {
func tally(ctx sdk.Context, versionProtocol uint64, k Keeper, threshold sdk.Dec) (passes bool) {

totalVotingPower := sdk.ZeroDec()
switchVotingPower := sdk.ZeroDec()
signalsVotingPower := sdk.ZeroDec()

k.sk.IterateBondedValidatorsByPower(ctx, func(index int64, validator sdk.Validator) (stop bool) {
totalVotingPower = totalVotingPower.Add(validator.GetPower())
valAcc := validator.GetConsAddr().String()
if ok := k.GetSignal(ctx, versionProtocol, valAcc); ok {
switchVotingPower = switchVotingPower.Add(validator.GetPower())
signalsVotingPower = signalsVotingPower.Add(validator.GetPower())
}

return false
})

ctx.Logger().Info("Tally Start", "SiganlsVotingPower", signalsVotingPower.String(),
"TotalVotingPower", totalVotingPower.String(),
"SiganlsVotingPower/TotalVotingPower", signalsVotingPower.Quo(totalVotingPower).String(),
"Threshold", threshold.String())
// If more than 95% of validator update , do switch
if switchVotingPower.Quo(totalVotingPower).GT(threshold) {
if signalsVotingPower.Quo(totalVotingPower).GT(threshold) {
return true
}
return false
Expand Down

0 comments on commit 4b63605

Please sign in to comment.