-
Notifications
You must be signed in to change notification settings - Fork 44
/
process_malicious.go
111 lines (92 loc) · 3.57 KB
/
process_malicious.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package keeper
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
evidencetypes "github.com/cosmos/cosmos-sdk/x/evidence/types"
slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
dclauthTypes "github.com/zigbee-alliance/distributed-compliance-ledger/x/dclauth/types"
"github.com/zigbee-alliance/distributed-compliance-ledger/x/validator/types"
)
// jail a validator.
func (k Keeper) Jail(ctx sdk.Context, validator types.Validator, reason string) {
validator.Power = types.ZeroPower
validator.Jailed = true
validator.JailedReason = reason
k.SetValidator(ctx, validator)
}
// unjail a validator.
func (k Keeper) Unjail(ctx sdk.Context, validator types.Validator) {
validator.Power = types.Power
validator.Jailed = false
validator.JailedReason = ""
k.SetValidator(ctx, validator)
}
// handle a validator signing two blocks at the same height
// Zeros validator power and jail it. So validator will be removed from validator
// set at the end of the block.
func (k Keeper) HandleDoubleSign(ctx sdk.Context, evidence *evidencetypes.Equivocation) {
logger := k.Logger(ctx)
consAddr := evidence.GetConsensusAddress()
validator, found := k.GetValidatorByConsAddr(ctx, consAddr)
if !found {
logger.Error(fmt.Sprintf("Validator by validator address %s not found", consAddr))
return
}
// calculate the age of the evidence.
infractionHeight := evidence.GetHeight()
infractionTime := evidence.GetTime()
ageDuration := ctx.BlockHeader().Time.Sub(infractionTime)
ageBlocks := ctx.BlockHeader().Height - infractionHeight
// Reject evidence if the double-sign is too old. Evidence is considered stale
// if the difference in time and number of blocks is greater than the allowed
// parameters defined.
cp := ctx.ConsensusParams()
if cp != nil && cp.Evidence != nil {
if ageDuration > cp.Evidence.MaxAgeDuration && ageBlocks > cp.Evidence.MaxAgeNumBlocks {
logger.Info(
"ignored equivocation; evidence too old",
"validator", consAddr,
"infraction_height", infractionHeight,
"max_age_num_blocks", cp.Evidence.MaxAgeNumBlocks,
"infraction_time", infractionTime,
"max_age_duration", cp.Evidence.MaxAgeDuration,
)
return
}
}
// double sign confirmed.
reason := fmt.Sprintf("Confirmed double sign from %s at height %d, age of duration %d, age of blocks %d", consAddr, infractionHeight, ageDuration, ageBlocks)
logger.Info(reason)
// Slash validator.
ctx.EventManager().EmitEvent(
sdk.NewEvent(
slashingtypes.EventTypeSlash,
sdk.NewAttribute(slashingtypes.AttributeKeyAddress, consAddr.String()),
sdk.NewAttribute(slashingtypes.AttributeKeyPower, fmt.Sprintf("%d", evidence.GetValidatorPower())),
sdk.NewAttribute(slashingtypes.AttributeKeyReason, slashingtypes.AttributeValueDoubleSign),
sdk.NewAttribute(slashingtypes.AttributeKeyJailed, consAddr.String()),
),
)
// Jail the validator.
if !validator.IsJailed() {
k.Jail(ctx, validator, reason)
}
// Revoked Account
valAddr, err := sdk.ValAddressFromBech32(validator.Owner)
if err != nil {
logger.Info("Error:", sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "Invalid Address: (%s)", err))
}
accAddr := sdk.AccAddress(valAddr)
// Move account to entity revoked account
revokedAccount, err := k.dclauthKeeper.AddAccountToRevokedAccount(
ctx, accAddr, nil, dclauthTypes.RevokedAccount_MaliciousValidator)
if err != nil {
logger.Info("Error:", err)
} else {
k.dclauthKeeper.SetRevokedAccount(ctx, *revokedAccount)
// delete account record
k.dclauthKeeper.RemoveAccount(ctx, accAddr)
}
logger.Info("Error:", err)
}