github.com/Finschia/finschia-sdk@v0.49.1/x/fbridge/keeper/abci.go (about)

     1  package keeper
     2  
     3  import (
     4  	"fmt"
     5  
     6  	sdk "github.com/Finschia/finschia-sdk/types"
     7  	"github.com/Finschia/finschia-sdk/x/fbridge/types"
     8  )
     9  
    10  func (k Keeper) BeginBlocker(ctx sdk.Context) {
    11  	k.InitMemStore(ctx)
    12  
    13  	proposals := k.GetRoleProposals(ctx)
    14  	for _, proposal := range proposals {
    15  		if ctx.BlockTime().After(proposal.ExpiredAt) {
    16  			if err := k.deleteRoleProposal(ctx, proposal.Id); err != nil {
    17  				panic(err)
    18  			}
    19  		}
    20  	}
    21  }
    22  
    23  func (k Keeper) EndBlocker(ctx sdk.Context) {
    24  	guardianTrustLevel := k.GetParams(ctx).GuardianTrustLevel
    25  	proposals := k.GetRoleProposals(ctx)
    26  	for _, proposal := range proposals {
    27  		votes := k.GetProposalVotes(ctx, proposal.Id)
    28  
    29  		var voteYes uint64 = 0
    30  		for _, vote := range votes {
    31  			if vote.Option == types.OptionYes {
    32  				voteYes++
    33  			}
    34  		}
    35  
    36  		if types.CheckTrustLevelThreshold(k.GetRoleMetadata(ctx).Guardian, voteYes, guardianTrustLevel) || proposal.Proposer == k.GetAuthority() {
    37  			if err := k.updateRole(ctx, proposal.Role, sdk.MustAccAddressFromBech32(proposal.Target)); err != nil {
    38  				panic(err)
    39  			}
    40  
    41  			if err := k.deleteRoleProposal(ctx, proposal.Id); err != nil {
    42  				panic(err)
    43  			}
    44  		}
    45  	}
    46  }
    47  
    48  // RegisterInvariants registers the fbridge module invariants
    49  func RegisterInvariants(ir sdk.InvariantRegistry, k Keeper) {
    50  	ir.RegisterRoute(types.ModuleName, "guardian-invariant", GuardianInvariant(k))
    51  }
    52  
    53  func GuardianInvariant(k Keeper) sdk.Invariant {
    54  	return func(ctx sdk.Context) (string, bool) {
    55  		numGuardian := 0
    56  		for _, p := range k.GetRolePairs(ctx) {
    57  			if p.Role == types.RoleGuardian {
    58  				numGuardian++
    59  			}
    60  		}
    61  
    62  		numBridgeSw := len(k.GetBridgeSwitches(ctx))
    63  		broken := numGuardian != numBridgeSw
    64  		return sdk.FormatInvariant(
    65  			types.ModuleName, "guardian-invariant",
    66  			fmt.Sprintf("number of guardians(%d) != number of bridge switches(%d)", numGuardian, numBridgeSw),
    67  		), broken
    68  	}
    69  }