-
Notifications
You must be signed in to change notification settings - Fork 174
/
handler.go
88 lines (74 loc) · 2.72 KB
/
handler.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
package bor
import (
"strconv"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/maticnetwork/heimdall/bor/types"
"github.com/maticnetwork/heimdall/common"
)
// NewHandler returns a handler for "bor" type messages.
func NewHandler(k Keeper) sdk.Handler {
return func(ctx sdk.Context, msg sdk.Msg) sdk.Result {
ctx = ctx.WithEventManager(sdk.NewEventManager())
switch msg := msg.(type) {
case types.MsgProposeSpan:
return HandleMsgProposeSpan(ctx, msg, k)
default:
return sdk.ErrTxDecode("Invalid message in bor module").Result()
}
}
}
// HandleMsgProposeSpan handles proposeSpan msg
func HandleMsgProposeSpan(ctx sdk.Context, msg types.MsgProposeSpan, k Keeper) sdk.Result {
k.Logger(ctx).Debug("Proposing span", "TxData", msg)
// chainManager params
params := k.chainKeeper.GetParams(ctx)
chainParams := params.ChainParams
// check chain id
if chainParams.BorChainID != msg.ChainID {
k.Logger(ctx).Error("Invalid Bor chain id", "msgChainID", msg.ChainID)
return common.ErrInvalidBorChainID(k.Codespace()).Result()
}
// check if last span is up or if greater diff than threshold is found between validator set
lastSpan, err := k.GetLastSpan(ctx)
if err != nil {
k.Logger(ctx).Error("Unable to fetch last span", "Error", err)
return common.ErrSpanNotFound(k.Codespace()).Result()
}
// check all conditions
if lastSpan.ID+1 != msg.ID || msg.StartBlock < lastSpan.StartBlock || msg.EndBlock < msg.StartBlock {
k.Logger(ctx).Error("Blocks not in countinuity",
"lastSpanId", lastSpan.ID,
"lastSpanStartBlock", lastSpan.StartBlock,
"spanId", msg.ID,
"spanStartBlock", msg.StartBlock,
)
return common.ErrSpanNotInCountinuity(k.Codespace()).Result()
}
// freeze for new span
err = k.FreezeSet(ctx, msg.ID, msg.StartBlock, msg.ChainID)
if err != nil {
k.Logger(ctx).Error("Unable to freeze validator set for span", "Error", err)
return common.ErrUnableToFreezeValSet(k.Codespace()).Result()
}
// get last span
lastSpan, err = k.GetLastSpan(ctx)
if err != nil {
k.Logger(ctx).Error("Unable to fetch last span", "Error", err)
return common.ErrSpanNotFound(k.Codespace()).Result()
}
// add events
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeProposeSpan,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
sdk.NewAttribute(types.AttributeKeySuccess, "true"),
sdk.NewAttribute(types.AttributeKeyBorSyncID, strconv.FormatUint(uint64(msg.ID), 10)),
sdk.NewAttribute(sdk.AttributeKeyAmount, strconv.FormatUint(uint64(msg.ID), 10)),
sdk.NewAttribute(types.AttributeKeySpanStartBlock, strconv.FormatUint(uint64(msg.StartBlock), 10)),
),
})
// draft result with events
return sdk.Result{
Events: ctx.EventManager().Events(),
}
}