-
Notifications
You must be signed in to change notification settings - Fork 178
/
headers.go
97 lines (88 loc) · 2.93 KB
/
headers.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
package convert
import (
"fmt"
"google.golang.org/protobuf/types/known/timestamppb"
"github.com/onflow/flow/protobuf/go/flow/entities"
"github.com/onflow/flow-go/model/flow"
)
// BlockHeaderToMessage converts a flow.Header to a protobuf message
func BlockHeaderToMessage(
h *flow.Header,
signerIDs flow.IdentifierList,
) (*entities.BlockHeader, error) {
id := h.ID()
t := timestamppb.New(h.Timestamp)
var lastViewTC *entities.TimeoutCertificate
if h.LastViewTC != nil {
newestQC := h.LastViewTC.NewestQC
lastViewTC = &entities.TimeoutCertificate{
View: h.LastViewTC.View,
HighQcViews: h.LastViewTC.NewestQCViews,
SignerIndices: h.LastViewTC.SignerIndices,
SigData: h.LastViewTC.SigData,
HighestQc: &entities.QuorumCertificate{
View: newestQC.View,
BlockId: newestQC.BlockID[:],
SignerIndices: newestQC.SignerIndices,
SigData: newestQC.SigData,
},
}
}
parentVoterIds := IdentifiersToMessages(signerIDs)
return &entities.BlockHeader{
Id: id[:],
ParentId: h.ParentID[:],
Height: h.Height,
PayloadHash: h.PayloadHash[:],
Timestamp: t,
View: h.View,
ParentView: h.ParentView,
ParentVoterIndices: h.ParentVoterIndices,
ParentVoterIds: parentVoterIds,
ParentVoterSigData: h.ParentVoterSigData,
ProposerId: h.ProposerID[:],
ProposerSigData: h.ProposerSigData,
ChainId: h.ChainID.String(),
LastViewTc: lastViewTC,
}, nil
}
// MessageToBlockHeader converts a protobuf message to a flow.Header
func MessageToBlockHeader(m *entities.BlockHeader) (*flow.Header, error) {
chainId, err := MessageToChainId(m.ChainId)
if err != nil {
return nil, fmt.Errorf("failed to convert ChainId: %w", err)
}
var lastViewTC *flow.TimeoutCertificate
if m.LastViewTc != nil {
newestQC := m.LastViewTc.HighestQc
if newestQC == nil {
return nil, fmt.Errorf("invalid structure newest QC should be present")
}
lastViewTC = &flow.TimeoutCertificate{
View: m.LastViewTc.View,
NewestQCViews: m.LastViewTc.HighQcViews,
SignerIndices: m.LastViewTc.SignerIndices,
SigData: m.LastViewTc.SigData,
NewestQC: &flow.QuorumCertificate{
View: newestQC.View,
BlockID: MessageToIdentifier(newestQC.BlockId),
SignerIndices: newestQC.SignerIndices,
SigData: newestQC.SigData,
},
}
}
return &flow.Header{
ParentID: MessageToIdentifier(m.ParentId),
Height: m.Height,
PayloadHash: MessageToIdentifier(m.PayloadHash),
Timestamp: m.Timestamp.AsTime(),
View: m.View,
ParentView: m.ParentView,
ParentVoterIndices: m.ParentVoterIndices,
ParentVoterSigData: m.ParentVoterSigData,
ProposerID: MessageToIdentifier(m.ProposerId),
ProposerSigData: m.ProposerSigData,
ChainID: *chainId,
LastViewTC: lastViewTC,
}, nil
}