forked from maticnetwork/heimdall
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.go
92 lines (77 loc) · 2.76 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
89
90
91
92
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("✅ Validating proposed span msg",
"spanId", msg.ID,
"startBlock", msg.StartBlock,
"endBlock", msg.EndBlock,
"seed", msg.Seed.String(),
)
// 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()
}
// Validate span continuity
if lastSpan.ID+1 != msg.ID || msg.StartBlock != lastSpan.EndBlock+1 || msg.EndBlock < msg.StartBlock {
k.Logger(ctx).Error("Blocks not in countinuity",
"lastSpanId", lastSpan.ID,
"spanId", msg.ID,
"lastSpanStartBlock", lastSpan.StartBlock,
"lastSpanEndBlock", lastSpan.EndBlock,
"spanStartBlock", msg.StartBlock,
"spanEndBlock", msg.EndBlock,
)
return common.ErrSpanNotInCountinuity(k.Codespace()).Result()
}
// Validate Span duration
spanDuration := k.GetParams(ctx).SpanDuration
if spanDuration != (msg.EndBlock - msg.StartBlock + 1) {
k.Logger(ctx).Error("Span duration of proposed span is wrong",
"proposedSpanDuration", msg.EndBlock-msg.StartBlock+1,
"paramsSpanDuration", spanDuration,
)
return common.ErrInvalidSpanDuration(k.Codespace()).Result()
}
// add events
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeProposeSpan,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
sdk.NewAttribute(types.AttributeKeySpanID, strconv.FormatUint(msg.ID, 10)),
sdk.NewAttribute(types.AttributeKeySpanStartBlock, strconv.FormatUint(msg.StartBlock, 10)),
sdk.NewAttribute(types.AttributeKeySpanEndBlock, strconv.FormatUint(msg.EndBlock, 10)),
),
})
// draft result with events
return sdk.Result{
Events: ctx.EventManager().Events(),
}
}