@@ -8,20 +8,20 @@ package genesis
88
99import (
1010 "flag"
11- "io/ioutil"
1211 "math/big"
1312 "sort"
1413 "time"
1514
16- "github.com/iotexproject/iotex-core/pkg/hash"
17-
15+ "github.com/golang/protobuf/proto"
1816 "github.com/pkg/errors"
1917 "go.uber.org/config"
2018 "go.uber.org/zap"
2119
2220 "github.com/iotexproject/iotex-core/address"
21+ "github.com/iotexproject/iotex-core/pkg/hash"
2322 "github.com/iotexproject/iotex-core/pkg/log"
2423 "github.com/iotexproject/iotex-core/pkg/unit"
24+ "github.com/iotexproject/iotex-core/protogen/iotextypes"
2525 "github.com/iotexproject/iotex-core/test/identityset"
2626)
2727
8484 Account `ymal:"account"`
8585 Poll `yaml:"poll"`
8686 Rewarding `yaml:"rewarding"`
87- // Digest is the digest of genesis config file
88- Digest hash.Hash256
8987 }
9088 // Blockchain contains blockchain level configs
9189 Blockchain struct {
@@ -159,14 +157,8 @@ type (
159157func New () (Genesis , error ) {
160158 opts := make ([]config.YAMLOption , 0 )
161159 opts = append (opts , config .Static (Default ))
162- genesisDigest := hash .ZeroHash256
163160 if genesisPath != "" {
164161 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 )
170162 }
171163 yaml , err := config .NewYAML (opts ... )
172164 if err != nil {
@@ -177,10 +169,77 @@ func New() (Genesis, error) {
177169 if err := yaml .Get (config .Root ).Populate (& genesis ); err != nil {
178170 return Genesis {}, errors .Wrap (err , "failed to unmarshal yaml genesis to struct" )
179171 }
180- genesis .Digest = genesisDigest
181172 return genesis , nil
182173}
183174
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+
184243// InitBalances returns the address that have initial balances and the corresponding amounts. The i-th amount is the
185244// i-th address' balance.
186245func (a * Account ) InitBalances () ([]address.Address , []* big.Int ) {
0 commit comments