@@ -8,20 +8,20 @@ package genesis
8
8
9
9
import (
10
10
"flag"
11
- "io/ioutil"
12
11
"math/big"
13
12
"sort"
14
13
"time"
15
14
16
- "github.com/iotexproject/iotex-core/pkg/hash"
17
-
15
+ "github.com/golang/protobuf/proto"
18
16
"github.com/pkg/errors"
19
17
"go.uber.org/config"
20
18
"go.uber.org/zap"
21
19
22
20
"github.com/iotexproject/iotex-core/address"
21
+ "github.com/iotexproject/iotex-core/pkg/hash"
23
22
"github.com/iotexproject/iotex-core/pkg/log"
24
23
"github.com/iotexproject/iotex-core/pkg/unit"
24
+ "github.com/iotexproject/iotex-core/protogen/iotextypes"
25
25
"github.com/iotexproject/iotex-core/test/identityset"
26
26
)
27
27
84
84
Account `ymal:"account"`
85
85
Poll `yaml:"poll"`
86
86
Rewarding `yaml:"rewarding"`
87
- // Digest is the digest of genesis config file
88
- Digest hash.Hash256
89
87
}
90
88
// Blockchain contains blockchain level configs
91
89
Blockchain struct {
@@ -159,14 +157,8 @@ type (
159
157
func New () (Genesis , error ) {
160
158
opts := make ([]config.YAMLOption , 0 )
161
159
opts = append (opts , config .Static (Default ))
162
- genesisDigest := hash .ZeroHash256
163
160
if genesisPath != "" {
164
161
opts = append (opts , config .File (genesisPath ))
165
- genesisCfgBytes , err := ioutil .ReadFile (genesisPath )
166
- if err != nil {
167
- return Genesis {}, err
168
- }
169
- genesisDigest = hash .Hash256b (genesisCfgBytes )
170
162
}
171
163
yaml , err := config .NewYAML (opts ... )
172
164
if err != nil {
@@ -177,10 +169,77 @@ func New() (Genesis, error) {
177
169
if err := yaml .Get (config .Root ).Populate (& genesis ); err != nil {
178
170
return Genesis {}, errors .Wrap (err , "failed to unmarshal yaml genesis to struct" )
179
171
}
180
- genesis .Digest = genesisDigest
181
172
return genesis , nil
182
173
}
183
174
175
+ // Hash is the hash of genesis config
176
+ func (g * Genesis ) Hash () hash.Hash256 {
177
+ gbProto := iotextypes.GenesisBlockchain {
178
+ Timestamp : g .Timestamp ,
179
+ BlockGasLimit : g .BlockGasLimit ,
180
+ ActionGasLimit : g .ActionGasLimit ,
181
+ BlockInterval : g .BlockInterval .Nanoseconds (),
182
+ NumSubEpochs : g .NumSubEpochs ,
183
+ NumDelegates : g .NumDelegates ,
184
+ NumCandidateDelegates : g .NumCandidateDelegates ,
185
+ TimeBasedRotation : g .TimeBasedRotation ,
186
+ }
187
+
188
+ initBalanceAddrs := make ([]string , 0 )
189
+ for initBalanceAddr := range g .InitBalanceMap {
190
+ initBalanceAddrs = append (initBalanceAddrs , initBalanceAddr )
191
+ }
192
+ sort .Strings (initBalanceAddrs )
193
+ initBalances := make ([]string , 0 )
194
+ for _ , initBalanceAddr := range initBalanceAddrs {
195
+ initBalances = append (initBalances , g .InitBalanceMap [initBalanceAddr ])
196
+ }
197
+ aProto := iotextypes.GenesisAccount {
198
+ InitBalanceAddrs : initBalanceAddrs ,
199
+ InitBalances : initBalances ,
200
+ }
201
+
202
+ dProtos := make ([]* iotextypes.GenesisDelegate , 0 )
203
+ for _ , d := range g .Delegates {
204
+ dProto := iotextypes.GenesisDelegate {
205
+ OperatorAddr : d .OperatorAddrStr ,
206
+ RewardAddr : d .RewardAddrStr ,
207
+ Votes : d .VotesStr ,
208
+ }
209
+ dProtos = append (dProtos , & dProto )
210
+ }
211
+ pProto := iotextypes.GenesisPoll {
212
+ EnableGravityChainVoting : g .EnableGravityChainVoting ,
213
+ GravityChainStartHeight : g .GravityChainStartHeight ,
214
+ RegisterContractAddress : g .RegisterContractAddress ,
215
+ StakingContractAddress : g .StakingContractAddress ,
216
+ VoteThreshold : g .VoteThreshold ,
217
+ ScoreThreshold : g .ScoreThreshold ,
218
+ SelfStakingThreshold : g .SelfStakingThreshold ,
219
+ Delegates : dProtos ,
220
+ }
221
+
222
+ rProto := iotextypes.GenesisRewarding {
223
+ InitAdminAddr : g .InitAdminAddrStr ,
224
+ InitBalance : g .InitBalanceStr ,
225
+ BlockReward : g .BlockRewardStr ,
226
+ EpochReward : g .EpochRewardStr ,
227
+ NumDelegatesForEpochReward : g .NumDelegatesForEpochReward ,
228
+ }
229
+
230
+ gProto := iotextypes.Genesis {
231
+ Blockchain : & gbProto ,
232
+ Account : & aProto ,
233
+ Poll : & pProto ,
234
+ Rewarding : & rProto ,
235
+ }
236
+ b , err := proto .Marshal (& gProto )
237
+ if err != nil {
238
+ log .L ().Panic ("Error when marshaling genesis proto" , zap .Error (err ))
239
+ }
240
+ return hash .Hash256b (b )
241
+ }
242
+
184
243
// InitBalances returns the address that have initial balances and the corresponding amounts. The i-th amount is the
185
244
// i-th address' balance.
186
245
func (a * Account ) InitBalances () ([]address.Address , []* big.Int ) {
0 commit comments