-
Notifications
You must be signed in to change notification settings - Fork 25
/
history.go
61 lines (56 loc) · 1.62 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
package common
import "github.com/protolambda/ztyp/tree"
// Return the block root at a recent slot. Only valid to SLOTS_PER_HISTORICAL_ROOT slots ago.
func GetBlockRootAtSlot(spec *Spec, state BeaconState, slot Slot) (Root, error) {
blockRoots, err := state.BlockRoots()
if err != nil {
return Root{}, err
}
return blockRoots.GetRoot(slot)
}
// Return the block root at a recent epoch. Only valid to SLOTS_PER_HISTORICAL_ROOT slots ago.
func GetBlockRoot(spec *Spec, state BeaconState, epoch Epoch) (Root, error) {
blockRoots, err := state.BlockRoots()
if err != nil {
return Root{}, err
}
startSlot, err := spec.EpochStartSlot(epoch)
if err != nil {
return Root{}, err
}
return blockRoots.GetRoot(startSlot)
}
func SetRecentRoots(spec *Spec, state BeaconState, slot Slot, blockRoot Root, stateRoot Root) error {
blockRootsBatch, err := state.BlockRoots()
if err != nil {
return err
}
stateRootsBatch, err := state.StateRoots()
if err != nil {
return err
}
if err := blockRootsBatch.SetRoot(slot%spec.SLOTS_PER_HISTORICAL_ROOT, blockRoot); err != nil {
return err
}
if err := stateRootsBatch.SetRoot(slot%spec.SLOTS_PER_HISTORICAL_ROOT, stateRoot); err != nil {
return err
}
return nil
}
func UpdateHistoricalRoots(state BeaconState) error {
histRoots, err := state.HistoricalRoots()
if err != nil {
return err
}
blockRoots, err := state.BlockRoots()
if err != nil {
return err
}
stateRoots, err := state.StateRoots()
if err != nil {
return err
}
// emulating HistoricalBatch here
hFn := tree.GetHashFn()
return histRoots.Append(tree.Hash(blockRoots.HashTreeRoot(hFn), stateRoots.HashTreeRoot(hFn)))
}