forked from protolambda/eth2-testnet-genesis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
state.go
133 lines (129 loc) · 3.77 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
package main
import (
"fmt"
"github.com/protolambda/zrnt/eth2/beacon/altair"
"github.com/protolambda/zrnt/eth2/beacon/common"
"github.com/protolambda/zrnt/eth2/beacon/merge"
"github.com/protolambda/zrnt/eth2/beacon/phase0"
"github.com/protolambda/zrnt/eth2/configs"
"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
switch state.(type) {
case *merge.BeaconStateView:
forkVersion = spec.BELLATRIX_FORK_VERSION
case *altair.BeaconStateView:
forkVersion = spec.ALTAIR_FORK_VERSION
default:
forkVersion = spec.GENESIS_FORK_VERSION
}
if err := state.SetFork(common.Fork{
PreviousVersion: spec.GENESIS_FORK_VERSION,
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 *merge.BeaconStateView:
emptyBody = merge.BeaconBlockBodyType(configs.Mainnet).New()
case *altair.BeaconStateView:
emptyBody = altair.BeaconBlockBodyType(configs.Mainnet).New()
default:
emptyBody = phase0.BeaconBlockBodyType(configs.Mainnet).New()
}
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
}