-
Notifications
You must be signed in to change notification settings - Fork 26
/
state.go
150 lines (146 loc) · 4.53 KB
/
state.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package main
import (
"fmt"
"github.com/protolambda/zrnt/eth2/beacon/altair"
"github.com/protolambda/zrnt/eth2/beacon/bellatrix"
"github.com/protolambda/zrnt/eth2/beacon/capella"
"github.com/protolambda/zrnt/eth2/beacon/common"
"github.com/protolambda/zrnt/eth2/beacon/deneb"
"github.com/protolambda/zrnt/eth2/beacon/phase0"
"github.com/protolambda/ztyp/tree"
)
func setupState(spec *common.Spec, state common.BeaconState, eth1Time common.Timestamp,
eth1BlockHash common.Root, validators []phase0.KickstartValidatorData) error {
if err := state.SetGenesisTime(eth1Time + spec.GENESIS_DELAY); err != nil {
return err
}
var forkVersion common.Version
var previousForkVersion common.Version
switch state.(type) {
case *deneb.BeaconStateView:
forkVersion = spec.DENEB_FORK_VERSION
previousForkVersion = spec.CAPELLA_FORK_VERSION
case *capella.BeaconStateView:
forkVersion = spec.CAPELLA_FORK_VERSION
previousForkVersion = spec.BELLATRIX_FORK_VERSION
case *bellatrix.BeaconStateView:
forkVersion = spec.BELLATRIX_FORK_VERSION
previousForkVersion = spec.ALTAIR_FORK_VERSION
case *altair.BeaconStateView:
forkVersion = spec.ALTAIR_FORK_VERSION
previousForkVersion = spec.GENESIS_FORK_VERSION
default:
forkVersion = spec.GENESIS_FORK_VERSION
previousForkVersion = spec.GENESIS_FORK_VERSION
}
if err := state.SetFork(common.Fork{
PreviousVersion: previousForkVersion,
CurrentVersion: forkVersion,
Epoch: common.GENESIS_EPOCH,
}); err != nil {
return err
}
// Empty deposit-tree
eth1Dat := common.Eth1Data{
DepositRoot: phase0.NewDepositRootsView().HashTreeRoot(tree.GetHashFn()),
DepositCount: 0,
BlockHash: eth1BlockHash,
}
if err := state.SetEth1Data(eth1Dat); err != nil {
return err
}
// Leave the deposit index to 0. No deposits happened.
if i, err := state.Eth1DepositIndex(); err != nil {
return err
} else if i != 0 {
return fmt.Errorf("expected 0 deposit index in state, got %d", i)
}
var emptyBody tree.HTR
switch state.(type) {
case *deneb.BeaconStateView:
emptyBody = deneb.BeaconBlockBodyType(spec).New()
case *capella.BeaconStateView:
emptyBody = capella.BeaconBlockBodyType(spec).New()
case *bellatrix.BeaconStateView:
emptyBody = bellatrix.BeaconBlockBodyType(spec).New()
case *altair.BeaconStateView:
emptyBody = altair.BeaconBlockBodyType(spec).New()
default:
emptyBody = phase0.BeaconBlockBodyType(spec).New()
}
// Setting to a valid BeaconBlockBody HTR has become official test setup behavior in Deneb,
// see initialize_beacon_state_from_eth1.
latestHeader := &common.BeaconBlockHeader{
BodyRoot: emptyBody.HashTreeRoot(tree.GetHashFn()),
}
if err := state.SetLatestBlockHeader(latestHeader); err != nil {
return err
}
// Seed RANDAO with Eth1 entropy
err := state.SeedRandao(spec, eth1BlockHash)
if err != nil {
return err
}
for _, v := range validators {
if err := state.AddValidator(spec, v.Pubkey, v.WithdrawalCredentials, v.Balance); err != nil {
return err
}
}
vals, err := state.Validators()
if err != nil {
return err
}
// Process activations
for i := 0; i < len(validators); i++ {
val, err := vals.Validator(common.ValidatorIndex(i))
if err != nil {
return err
}
vEff, err := val.EffectiveBalance()
if err != nil {
return err
}
if vEff == spec.MAX_EFFECTIVE_BALANCE {
if err := val.SetActivationEligibilityEpoch(common.GENESIS_EPOCH); err != nil {
return err
}
if err := val.SetActivationEpoch(common.GENESIS_EPOCH); err != nil {
return err
}
}
}
if err := state.SetGenesisValidatorsRoot(vals.HashTreeRoot(tree.GetHashFn())); err != nil {
return err
}
if st, ok := state.(common.SyncCommitteeBeaconState); ok {
indicesBounded, err := common.LoadBoundedIndices(vals)
if err != nil {
return err
}
active := common.ActiveIndices(indicesBounded, common.GENESIS_EPOCH)
indices, err := common.ComputeSyncCommitteeIndices(spec, state, common.GENESIS_EPOCH, active)
if err != nil {
return fmt.Errorf("failed to compute sync committee indices: %v", err)
}
pubs, err := common.NewPubkeyCache(vals)
if err != nil {
return err
}
// Note: A duplicate committee is assigned for the current and next committee at genesis
syncCommittee, err := common.IndicesToSyncCommittee(indices, pubs)
if err != nil {
return err
}
syncCommitteeView, err := syncCommittee.View(spec)
if err != nil {
return err
}
if err := st.SetCurrentSyncCommittee(syncCommitteeView); err != nil {
return err
}
if err := st.SetNextSyncCommittee(syncCommitteeView); err != nil {
return err
}
}
return nil
}