-
Notifications
You must be signed in to change notification settings - Fork 25
/
history.go
97 lines (77 loc) · 2.82 KB
/
history.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 capella
import (
"github.com/protolambda/ztyp/codec"
"github.com/protolambda/ztyp/tree"
. "github.com/protolambda/ztyp/view"
"github.com/protolambda/zrnt/eth2/beacon/common"
)
// HistoricalSummary is a summary of HistoricalBatch and was introduced in Capella
type HistoricalSummary struct {
BlockSummaryRoot common.Root
StateSummaryRoot common.Root
}
func (hs *HistoricalSummary) View() *ContainerView {
a, b := RootView(hs.BlockSummaryRoot), RootView(hs.StateSummaryRoot)
c, _ := HistoricalSummaryType.FromFields(&a, &b)
return c
}
func (hs *HistoricalSummary) Deserialize(dr *codec.DecodingReader) error {
return dr.FixedLenContainer(&hs.BlockSummaryRoot, &hs.StateSummaryRoot)
}
func (hs *HistoricalSummary) Serialize(w *codec.EncodingWriter) error {
return w.FixedLenContainer(&hs.BlockSummaryRoot, &hs.StateSummaryRoot)
}
func (*HistoricalSummary) ByteLength() uint64 {
return 32 * 2
}
func (*HistoricalSummary) FixedLength() uint64 {
return 32 * 2
}
func (hs *HistoricalSummary) HashTreeRoot(hFn tree.HashFn) common.Root {
return hFn.HashTreeRoot(&hs.BlockSummaryRoot, &hs.StateSummaryRoot)
}
var HistoricalSummaryType = ContainerType("HistoricalSummary", []FieldDef{
{"block_summary_root", RootType},
{"state_summary_root", RootType},
})
// HistoricalSummaries are the summaries of historical batches
type HistoricalSummaries []HistoricalSummary
func (a *HistoricalSummaries) Deserialize(spec *common.Spec, dr *codec.DecodingReader) error {
return dr.List(func() codec.Deserializable {
i := len(*a)
*a = append(*a, HistoricalSummary{})
return &((*a)[i])
}, HistoricalSummaryType.TypeByteLength(), uint64(spec.HISTORICAL_ROOTS_LIMIT))
}
func (a HistoricalSummaries) Serialize(_ *common.Spec, w *codec.EncodingWriter) error {
return w.List(func(i uint64) codec.Serializable {
return &a[i]
}, HistoricalSummaryType.TypeByteLength(), uint64(len(a)))
}
func (a HistoricalSummaries) ByteLength(_ *common.Spec) (out uint64) {
return HistoricalSummaryType.TypeByteLength() * uint64(len(a))
}
func (*HistoricalSummaries) FixedLength(*common.Spec) uint64 {
return 0
}
func (li HistoricalSummaries) HashTreeRoot(spec *common.Spec, hFn tree.HashFn) common.Root {
length := uint64(len(li))
return hFn.ComplexListHTR(func(i uint64) tree.HTR {
if i < length {
return &li[i]
}
return nil
}, length, uint64(spec.HISTORICAL_ROOTS_LIMIT))
}
func HistoricalSummariesType(spec *common.Spec) ListTypeDef {
return ListType(HistoricalSummaryType, uint64(spec.HISTORICAL_ROOTS_LIMIT))
}
type HistoricalSummariesView struct{ *ComplexListView }
func AsHistoricalSummaries(v View, err error) (*HistoricalSummariesView, error) {
c, err := AsComplexList(v, err)
return &HistoricalSummariesView{c}, err
}
func (h *HistoricalSummariesView) Append(summary HistoricalSummary) error {
v := summary.View()
return h.ComplexListView.Append(v)
}