forked from babylonlabs-io/babylon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheader_handler.go
73 lines (65 loc) · 2.33 KB
/
header_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
package keeper
import (
"context"
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/babylonlabs-io/babylon/x/zoneconcierge/types"
)
// HandleHeaderWithValidCommit handles a CZ header with a valid QC
func (k Keeper) HandleHeaderWithValidCommit(ctx context.Context, txHash []byte, header *types.HeaderInfo, isOnFork bool) {
sdkCtx := sdk.UnwrapSDKContext(ctx)
babylonHeader := sdkCtx.HeaderInfo()
indexedHeader := types.IndexedHeader{
ChainId: header.ChainId,
Hash: header.AppHash,
Height: header.Height,
Time: &header.Time,
BabylonHeaderHash: babylonHeader.AppHash,
BabylonHeaderHeight: uint64(babylonHeader.Height),
BabylonEpoch: k.GetEpoch(ctx).EpochNumber,
BabylonTxHash: txHash,
}
k.Logger(sdkCtx).Debug("found new IBC header", "header", indexedHeader)
var (
chainInfo *types.ChainInfo
err error
)
if !k.HasChainInfo(ctx, indexedHeader.ChainId) {
// chain info does not exist yet, initialise chain info for this chain
chainInfo, err = k.InitChainInfo(ctx, indexedHeader.ChainId)
if err != nil {
panic(fmt.Errorf("failed to initialize chain info of %s: %w", indexedHeader.ChainId, err))
}
} else {
// get chain info
chainInfo, err = k.GetChainInfo(ctx, indexedHeader.ChainId)
if err != nil {
panic(fmt.Errorf("failed to get chain info of %s: %w", indexedHeader.ChainId, err))
}
}
if isOnFork {
// insert header to fork index
if err := k.insertForkHeader(ctx, indexedHeader.ChainId, &indexedHeader); err != nil {
panic(err)
}
// update the latest fork in chain info
if err := k.tryToUpdateLatestForkHeader(ctx, indexedHeader.ChainId, &indexedHeader); err != nil {
panic(err)
}
} else {
// ensure the header is the latest one, otherwise ignore it
// NOTE: while an old header is considered acceptable in IBC-Go (see Case_valid_past_update), but
// ZoneConcierge should not checkpoint it since Babylon requires monotonic checkpointing
if !chainInfo.IsLatestHeader(&indexedHeader) {
return
}
// insert header to canonical chain index
if err := k.insertHeader(ctx, indexedHeader.ChainId, &indexedHeader); err != nil {
panic(err)
}
// update the latest canonical header in chain info
if err := k.updateLatestHeader(ctx, indexedHeader.ChainId, &indexedHeader); err != nil {
panic(err)
}
}
}