header.gno
2.63 Kb · 90 lines
1package cometbls
2
3import (
4 "crypto/cometblszk"
5 "time"
6
7 "gno.land/p/nt/ufmt/v0"
8 "gno.land/p/onbloc/ibc/union/types"
9)
10
11type Header struct {
12 SignedHeader *LightHeader
13 TrustedHeight *types.Height
14 ZeroKnowledgeProof []byte
15}
16
17func NewHeader(
18 signedHeader *LightHeader,
19 trustedHeight *types.Height,
20 zeroKnowledgeProof []byte,
21) *Header {
22 return &Header{
23 SignedHeader: signedHeader,
24 TrustedHeight: trustedHeight,
25 ZeroKnowledgeProof: zeroKnowledgeProof,
26 }
27}
28
29func (Header) ClientType() string {
30 return ClientType
31}
32
33func (h *Header) ConsensusState() *ConsensusState {
34 return &ConsensusState{
35 Timestamp: uint64(h.GetTime().UnixNano()),
36 Root: MerkleRoot{Hash: h.SignedHeader.AppHash},
37 NextValidatorsHash: h.SignedHeader.NextValidatorsHash,
38 }
39}
40
41// GetHeight returns the height of the signed (untrusted) header. CometBLS
42// carries a single revision, so it shares the trusted height's revision number.
43func (h *Header) GetHeight() types.Height {
44 return types.NewHeightWithRevision(h.TrustedHeight.RevisionNumber, uint64(h.SignedHeader.Height))
45}
46
47func (h *Header) GetTime() time.Time {
48 return time.Unix(h.SignedHeader.TimeSeconds, int64(h.SignedHeader.TimeNanos)).UTC()
49}
50
51// ValidateBasic calls the SignedHeader ValidateBasic function and checks
52// that validatorsets are not nil.
53// NOTE: TrustedHeight and TrustedValidators may be empty when creating client
54// with MsgCreateClient
55func (h *Header) ValidateBasic() error {
56 if h.SignedHeader == nil {
57 return errorWithDetails(ErrInvalidHeader, "signed header cannot be nil")
58 }
59
60 if len(h.ZeroKnowledgeProof) == 0 {
61 return errorWithDetails(ErrInvalidHeader, "zero-knowledge proof cannot be empty")
62 }
63
64 // TrustedHeight is less than Header for updates and misbehaviour
65 if h.TrustedHeight.GTE(h.GetHeight()) {
66 return errorWithDetails(
67 ErrInvalidHeaderHeight,
68 ufmt.Sprintf("TrustedHeight %d must be less than header height %d", h.TrustedHeight, h.GetHeight()),
69 )
70 }
71
72 return nil
73}
74
75// LightHeader is the bespoke CometBLS light header. It is an alias of
76// crypto/cometblszk.LightHeader so a SignedHeader feeds VerifyZKP directly
77// without re-mapping, and its TimeSeconds/TimeNanos split mirrors the ABI wire
78// format (see headerSchema in encoding.gno).
79type LightHeader = cometblszk.LightHeader
80
81func NewLightHeader(height, timeSeconds int64, timeNanos int32, validatorsHash, nextValidatorsHash, appHash []byte) *LightHeader {
82 return &LightHeader{
83 Height: height,
84 TimeSeconds: timeSeconds,
85 TimeNanos: timeNanos,
86 ValidatorsHash: validatorsHash,
87 NextValidatorsHash: nextValidatorsHash,
88 AppHash: appHash,
89 }
90}