-
Notifications
You must be signed in to change notification settings - Fork 6
/
config.go
108 lines (89 loc) · 2.96 KB
/
config.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
package explorer
import (
"encoding/hex"
"encoding/json"
"fmt"
"io"
peerstore "github.com/libp2p/go-libp2p-peerstore"
multiaddr "github.com/multiformats/go-multiaddr"
"github.com/phoreproject/synapse/beacon"
"github.com/phoreproject/synapse/beacon/config"
"github.com/phoreproject/synapse/p2p"
beaconapp "github.com/phoreproject/synapse/beacon/app"
)
// Config is the explorer app config.
type Config struct {
GenesisTime uint64
DataDirectory string
InitialValidatorList []beacon.InitialValidatorEntry
NetworkConfig *config.Config
Resync bool
ListeningAddress string
DiscoveryOptions p2p.DiscoveryOptions
}
// GenerateConfigFromChainConfig generates a new config from the passed in network config
// which should be loaded from a JSON file.
func GenerateConfigFromChainConfig(chainConfig beaconapp.ChainConfig) (*Config, error) {
c := Config{
GenesisTime: chainConfig.GenesisTime,
}
c.InitialValidatorList = make([]beacon.InitialValidatorEntry, chainConfig.InitialValidators.NumValidators)
for i := range c.InitialValidatorList {
validator := chainConfig.InitialValidators.Validators[i]
pubKeyBytes, err := hex.DecodeString(validator.PubKey)
if err != nil {
return nil, err
}
var pubKey [96]byte
copy(pubKey[:], pubKeyBytes)
sigBytes, err := hex.DecodeString(validator.ProofOfPossession)
if err != nil {
return nil, err
}
var signature [48]byte
copy(signature[:], sigBytes)
withdrawalCredentialsBytes, err := hex.DecodeString(validator.WithdrawalCredentials)
if err != nil {
return nil, err
}
var withdrawalCredentials [32]byte
copy(withdrawalCredentials[:], withdrawalCredentialsBytes)
c.InitialValidatorList[i] = beacon.InitialValidatorEntry{
PubKey: pubKey,
ProofOfPossession: signature,
WithdrawalCredentials: withdrawalCredentials,
WithdrawalShard: validator.WithdrawalShard,
DepositSize: validator.DepositSize,
}
}
c.GenesisTime = chainConfig.GenesisTime
c.DiscoveryOptions = p2p.NewDiscoveryOptions()
c.DiscoveryOptions.PeerAddresses = make([]peerstore.PeerInfo, len(chainConfig.BootstrapPeers))
networkConfig, found := config.NetworkIDs[chainConfig.NetworkID]
if !found {
return nil, fmt.Errorf("error getting network config for ID: %s", chainConfig.NetworkID)
}
c.NetworkConfig = &networkConfig
for i := range c.DiscoveryOptions.PeerAddresses {
a, err := multiaddr.NewMultiaddr(chainConfig.BootstrapPeers[i])
if err != nil {
return nil, err
}
peerInfo, err := peerstore.InfoFromP2pAddr(a)
if err != nil {
return nil, err
}
c.DiscoveryOptions.PeerAddresses[i] = *peerInfo
}
return &c, nil
}
// ReadChainFileToConfig reads a network config from the reader.
func ReadChainFileToConfig(r io.Reader) (*Config, error) {
var networkConfig beaconapp.ChainConfig
d := json.NewDecoder(r)
err := d.Decode(&networkConfig)
if err != nil {
return nil, err
}
return GenerateConfigFromChainConfig(networkConfig)
}