@@ -7,12 +7,14 @@ package genesis
77
88import (
99 "crypto/ecdsa"
10+ "errors"
1011 "math/big"
1112 "sync/atomic"
1213
1314 "github.com/ethereum/go-ethereum/crypto"
1415
1516 "github.com/vechain/thor/v2/builtin"
17+ "github.com/vechain/thor/v2/builtin/staker"
1618 "github.com/vechain/thor/v2/state"
1719 "github.com/vechain/thor/v2/thor"
1820 "github.com/vechain/thor/v2/tx"
@@ -144,3 +146,121 @@ func NewDevnetWithConfig(config DevConfig) *Genesis {
144146
145147 return & Genesis {builder , id , "devnet" }
146148}
149+
150+ // NewDevnet create genesis for solo mode.
151+ func NewHayabusaDevnet (forkConfig * thor.ForkConfig ) * Genesis {
152+ launchTime := uint64 (1526400000 ) // Default launch time 'Wed May 16 2018 00:00:00 GMT+0800 (CST)'
153+ if launchTime == 0 {
154+ launchTime = uint64 (1526400000 ) // Default launch time 'Wed May 16 2018 00:00:00 GMT+0800 (CST)'
155+ }
156+
157+ executor := DevAccounts ()[0 ].Address
158+ soloBlockSigner := DevAccounts ()[0 ]
159+ keyBaseGasPrice := thor .InitialBaseGasPrice
160+
161+ builder := new (Builder ).
162+ GasLimit (thor .InitialGasLimit ).
163+ Timestamp (launchTime ).
164+ ForkConfig (forkConfig ).
165+ State (func (state * state.State ) error {
166+ // setup builtin contracts
167+ if err := state .SetCode (builtin .Authority .Address , builtin .Authority .RuntimeBytecodes ()); err != nil {
168+ return err
169+ }
170+ if err := state .SetCode (builtin .Energy .Address , builtin .Energy .RuntimeBytecodes ()); err != nil {
171+ return err
172+ }
173+ if err := state .SetCode (builtin .Params .Address , builtin .Params .RuntimeBytecodes ()); err != nil {
174+ return err
175+ }
176+ if err := state .SetCode (builtin .Prototype .Address , builtin .Prototype .RuntimeBytecodes ()); err != nil {
177+ return err
178+ }
179+ if err := state .SetCode (builtin .Extension .Address , builtin .Extension .RuntimeBytecodes ()); err != nil {
180+ return err
181+ }
182+ if err := state .SetCode (builtin .Staker .Address , builtin .Staker .RuntimeBytecodes ()); err != nil {
183+ return err
184+ }
185+
186+ // set parameters
187+ state .SetStorage (builtin .Staker .Address , thor .BytesToBytes32 ([]byte (("staker-low-staking-period" ))), thor .BytesToBytes32 (big .NewInt (12 ).Bytes ()))
188+ state .SetStorage (builtin .Staker .Address , thor .BytesToBytes32 ([]byte (("staker-medium-staking-period" ))), thor .BytesToBytes32 (big .NewInt (30 ).Bytes ()))
189+ state .SetStorage (builtin .Staker .Address , thor .BytesToBytes32 ([]byte (("staker-high-staking-period" ))), thor .BytesToBytes32 (big .NewInt (90 ).Bytes ()))
190+ state .SetStorage (builtin .Staker .Address , thor .BytesToBytes32 ([]byte (("cooldown-period" ))), thor .BytesToBytes32 (big .NewInt (12 ).Bytes ()))
191+ state .SetStorage (builtin .Staker .Address , thor .BytesToBytes32 ([]byte (("epoch-length" ))), thor .BytesToBytes32 (big .NewInt (6 ).Bytes ()))
192+
193+ tokenSupply := & big.Int {}
194+ energySupply := & big.Int {}
195+ for _ , a := range DevAccounts () {
196+ bal , _ := new (big.Int ).SetString ("1000000000000000000000000000" , 10 )
197+ if err := state .SetBalance (a .Address , bal ); err != nil {
198+ return err
199+ }
200+ if err := state .SetEnergy (a .Address , bal , launchTime ); err != nil {
201+ return err
202+ }
203+ tokenSupply .Add (tokenSupply , bal )
204+ energySupply .Add (energySupply , bal )
205+ }
206+ if err := builtin .Energy .Native (state , launchTime ).SetInitialSupply (tokenSupply , energySupply ); err != nil {
207+ return err
208+ }
209+
210+ if err := builtin .Params .Native (state ).Set (thor .KeyMaxBlockProposers , big .NewInt (1 )); err != nil {
211+ return err
212+ }
213+
214+ // adding a soloBlockSigner as a validator and manage balances manually
215+ // NOTE: does not manage energy, as it is not a transaction
216+ if err := builtin .Staker .Native (state ).AddValidator (soloBlockSigner .Address , soloBlockSigner .Address , staker .HighStakingPeriod , staker .MinStake ); err != nil {
217+ return err
218+ }
219+ currentBalance , err := state .GetBalance (soloBlockSigner .Address )
220+ if err != nil {
221+ return err
222+ }
223+ currentBalance = big .NewInt (0 ).Sub (currentBalance , staker .MinStake )
224+ if err := state .SetBalance (soloBlockSigner .Address , currentBalance ); err != nil {
225+ return err
226+ }
227+ if err := state .SetBalance (builtin .Staker .Address , staker .MinStake ); err != nil {
228+ return err
229+ }
230+
231+ transitioned , err := builtin .Staker .Native (state ).Transition (0 )
232+ if err != nil {
233+ return err
234+ }
235+ if ! transitioned {
236+ return errors .New ("the transition of the validator state didn't happen" )
237+ }
238+
239+ return nil
240+ }).
241+ Call (
242+ tx .NewClause (& builtin .Params .Address ).
243+ WithData (mustEncodeInput (builtin .Params .ABI , "set" , thor .KeyExecutorAddress , new (big.Int ).SetBytes (executor [:]))),
244+ thor.Address {}).
245+ Call (
246+ tx .NewClause (& builtin .Params .Address ).WithData (mustEncodeInput (builtin .Params .ABI , "set" , thor .KeyRewardRatio , thor .InitialRewardRatio )),
247+ executor ).
248+ Call (
249+ tx .NewClause (& builtin .Params .Address ).WithData (mustEncodeInput (builtin .Params .ABI , "set" , thor .KeyLegacyTxBaseGasPrice , keyBaseGasPrice )),
250+ executor ).
251+ Call (
252+ tx .NewClause (& builtin .Params .Address ).
253+ WithData (mustEncodeInput (builtin .Params .ABI , "set" , thor .KeyProposerEndorsement , thor .InitialProposerEndorsement )),
254+ executor ).
255+ Call (
256+ tx .NewClause (& builtin .Authority .Address ).
257+ WithData (mustEncodeInput (builtin .Authority .ABI , "add" , soloBlockSigner .Address , soloBlockSigner .Address , thor .BytesToBytes32 ([]byte ("Solo Block Signer" )))),
258+ executor )
259+
260+ id , err := builder .ComputeID ()
261+ if err != nil {
262+ panic (err )
263+ }
264+
265+ return & Genesis {builder , id , "devnetHayabusa" }
266+ }
0 commit comments