forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
246 lines (214 loc) · 6.47 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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package genesis
import (
"encoding/base64"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils"
"github.com/ava-labs/avalanchego/utils/constants"
"github.com/ava-labs/avalanchego/utils/formatting/address"
"github.com/ava-labs/avalanchego/utils/math"
"github.com/ava-labs/avalanchego/vms/platformvm/signer"
)
var (
_ utils.Sortable[Allocation] = Allocation{}
errInvalidGenesisJSON = errors.New("could not unmarshal genesis JSON")
)
type LockedAmount struct {
Amount uint64 `json:"amount"`
Locktime uint64 `json:"locktime"`
}
type Allocation struct {
ETHAddr ids.ShortID `json:"ethAddr"`
AVAXAddr ids.ShortID `json:"avaxAddr"`
InitialAmount uint64 `json:"initialAmount"`
UnlockSchedule []LockedAmount `json:"unlockSchedule"`
}
func (a Allocation) Unparse(networkID uint32) (UnparsedAllocation, error) {
ua := UnparsedAllocation{
InitialAmount: a.InitialAmount,
UnlockSchedule: a.UnlockSchedule,
ETHAddr: "0x" + hex.EncodeToString(a.ETHAddr.Bytes()),
}
avaxAddr, err := address.Format(
"X",
constants.GetHRP(networkID),
a.AVAXAddr.Bytes(),
)
ua.AVAXAddr = avaxAddr
return ua, err
}
func (a Allocation) Less(other Allocation) bool {
return a.InitialAmount < other.InitialAmount ||
(a.InitialAmount == other.InitialAmount && a.AVAXAddr.Less(other.AVAXAddr))
}
type Staker struct {
NodeID ids.NodeID `json:"nodeID"`
RewardAddress ids.ShortID `json:"rewardAddress"`
DelegationFee uint32 `json:"delegationFee"`
Signer *signer.ProofOfPossession `json:"signer,omitempty"`
}
func (s Staker) Unparse(networkID uint32) (UnparsedStaker, error) {
avaxAddr, err := address.Format(
"X",
constants.GetHRP(networkID),
s.RewardAddress.Bytes(),
)
return UnparsedStaker{
NodeID: s.NodeID,
RewardAddress: avaxAddr,
DelegationFee: s.DelegationFee,
Signer: s.Signer,
}, err
}
// Config contains the genesis addresses used to construct a genesis
type Config struct {
NetworkID uint32 `json:"networkID"`
Allocations []Allocation `json:"allocations"`
StartTime uint64 `json:"startTime"`
InitialStakeDuration uint64 `json:"initialStakeDuration"`
InitialStakeDurationOffset uint64 `json:"initialStakeDurationOffset"`
InitialStakedFunds []ids.ShortID `json:"initialStakedFunds"`
InitialStakers []Staker `json:"initialStakers"`
CChainGenesis string `json:"cChainGenesis"`
Message string `json:"message"`
}
func (c Config) Unparse() (UnparsedConfig, error) {
uc := UnparsedConfig{
NetworkID: c.NetworkID,
Allocations: make([]UnparsedAllocation, len(c.Allocations)),
StartTime: c.StartTime,
InitialStakeDuration: c.InitialStakeDuration,
InitialStakeDurationOffset: c.InitialStakeDurationOffset,
InitialStakedFunds: make([]string, len(c.InitialStakedFunds)),
InitialStakers: make([]UnparsedStaker, len(c.InitialStakers)),
CChainGenesis: c.CChainGenesis,
Message: c.Message,
}
for i, a := range c.Allocations {
ua, err := a.Unparse(uc.NetworkID)
if err != nil {
return uc, err
}
uc.Allocations[i] = ua
}
for i, isa := range c.InitialStakedFunds {
avaxAddr, err := address.Format(
"X",
constants.GetHRP(uc.NetworkID),
isa.Bytes(),
)
if err != nil {
return uc, err
}
uc.InitialStakedFunds[i] = avaxAddr
}
for i, is := range c.InitialStakers {
uis, err := is.Unparse(c.NetworkID)
if err != nil {
return uc, err
}
uc.InitialStakers[i] = uis
}
return uc, nil
}
func (c *Config) InitialSupply() (uint64, error) {
initialSupply := uint64(0)
for _, allocation := range c.Allocations {
newInitialSupply, err := math.Add64(initialSupply, allocation.InitialAmount)
if err != nil {
return 0, err
}
for _, unlock := range allocation.UnlockSchedule {
newInitialSupply, err = math.Add64(newInitialSupply, unlock.Amount)
if err != nil {
return 0, err
}
}
initialSupply = newInitialSupply
}
return initialSupply, nil
}
var (
// MainnetConfig is the config that should be used to generate the mainnet
// genesis.
MainnetConfig Config
// FujiConfig is the config that should be used to generate the fuji
// genesis.
FujiConfig Config
// LocalConfig is the config that should be used to generate a local
// genesis.
LocalConfig Config
)
func init() {
unparsedMainnetConfig := UnparsedConfig{}
unparsedFujiConfig := UnparsedConfig{}
unparsedLocalConfig := UnparsedConfig{}
err := utils.Err(
json.Unmarshal(mainnetGenesisConfigJSON, &unparsedMainnetConfig),
json.Unmarshal(fujiGenesisConfigJSON, &unparsedFujiConfig),
json.Unmarshal(localGenesisConfigJSON, &unparsedLocalConfig),
)
if err != nil {
panic(err)
}
MainnetConfig, err = unparsedMainnetConfig.Parse()
if err != nil {
panic(err)
}
FujiConfig, err = unparsedFujiConfig.Parse()
if err != nil {
panic(err)
}
LocalConfig, err = unparsedLocalConfig.Parse()
if err != nil {
panic(err)
}
}
func GetConfig(networkID uint32) *Config {
switch networkID {
case constants.MainnetID:
return &MainnetConfig
case constants.FujiID:
return &FujiConfig
case constants.LocalID:
return &LocalConfig
default:
tempConfig := LocalConfig
tempConfig.NetworkID = networkID
return &tempConfig
}
}
// GetConfigFile loads a *Config from a provided filepath.
func GetConfigFile(fp string) (*Config, error) {
bytes, err := os.ReadFile(filepath.Clean(fp))
if err != nil {
return nil, fmt.Errorf("unable to load file %s: %w", fp, err)
}
return parseGenesisJSONBytesToConfig(bytes)
}
// GetConfigContent loads a *Config from a provided environment variable
func GetConfigContent(genesisContent string) (*Config, error) {
bytes, err := base64.StdEncoding.DecodeString(genesisContent)
if err != nil {
return nil, fmt.Errorf("unable to decode base64 content: %w", err)
}
return parseGenesisJSONBytesToConfig(bytes)
}
func parseGenesisJSONBytesToConfig(bytes []byte) (*Config, error) {
var unparsedConfig UnparsedConfig
if err := json.Unmarshal(bytes, &unparsedConfig); err != nil {
return nil, fmt.Errorf("%w: %w", errInvalidGenesisJSON, err)
}
config, err := unparsedConfig.Parse()
if err != nil {
return nil, fmt.Errorf("unable to parse config: %w", err)
}
return &config, nil
}