@@ -28,8 +28,10 @@ import (
2828 "github.com/ethereum/go-ethereum/common"
2929 "github.com/ethereum/go-ethereum/common/hexutil"
3030 "github.com/ethereum/go-ethereum/common/math"
31+ "github.com/ethereum/go-ethereum/contracts"
3132 "github.com/ethereum/go-ethereum/core/state"
3233 "github.com/ethereum/go-ethereum/core/types"
34+ "github.com/ethereum/go-ethereum/core/vm"
3335 "github.com/ethereum/go-ethereum/ethdb"
3436 "github.com/ethereum/go-ethereum/log"
3537 "github.com/ethereum/go-ethereum/params"
@@ -235,7 +237,7 @@ func (g *Genesis) ToBlock(db ethdb.Database) *types.Block {
235237 }
236238 }
237239 root := statedb .IntermediateRoot (false )
238- head := & types.Header {
240+ header := & types.Header {
239241 Number : new (big.Int ).SetUint64 (g .Number ),
240242 Nonce : types .EncodeNonce (g .Nonce ),
241243 Time : new (big.Int ).SetUint64 (g .Timestamp ),
@@ -249,15 +251,36 @@ func (g *Genesis) ToBlock(db ethdb.Database) *types.Block {
249251 Root : root ,
250252 }
251253 if g .GasLimit == 0 {
252- head .GasLimit = params .GenesisGasLimit
254+ header .GasLimit = params .GenesisGasLimit
253255 }
254256 if g .Difficulty == nil {
255- head .Difficulty = params .GenesisDifficulty
257+ header .Difficulty = params .GenesisDifficulty
258+ }
259+
260+ if g .Config != nil && g .Config .IsCasper (header .Number ) {
261+ // TODO: need to put in chain config, but how to ensure it's secure
262+ casperPrivateKey := "a27df3e4f46e7792ea951d7abf853b5a5ac3226bacd57286b9df98324386532f"
263+
264+ // Init casper transactions
265+ txs , _ , _ := contracts .CasperInitializers (0 , casperPrivateKey )
266+ // Add init balance to null sender address
267+ nullSenderAddr := common .HexToAddress ("56f8fa946c92a225444170f59fba81707c755161" )
268+ statedb .AddBalance (nullSenderAddr , new (big.Int ).Exp (big .NewInt (10 ), big .NewInt (25 ), nil ))
269+
270+ for _ , tx := range txs {
271+ _ , used , err := ApplyTransaction (g .Config , nil , & g .Coinbase , new (GasPool ).AddGas (g .GasLimit ), statedb , header , tx , new (uint64 ), vm.Config {})
272+ if err != nil {
273+ log .Info ("Failed to apply transaction" , "hash" , tx .Hash ().Hex (), "err" , err )
274+ }
275+
276+ log .Info ("Apply Casper tx" , "hash" , tx .Hash ().Hex (), "used" , used , "root" , statedb .IntermediateRoot (false ).Hex ())
277+ }
278+ header .Root = statedb .IntermediateRoot (false )
256279 }
257280 statedb .Commit (false )
258- statedb .Database ().TrieDB ().Commit (root , true )
281+ statedb .Database ().TrieDB ().Commit (header . Root , true )
259282
260- return types .NewBlock (head , nil , nil , nil )
283+ return types .NewBlock (header , nil , nil , nil )
261284}
262285
263286// Commit writes the block and state of a genesis specification to the database.
0 commit comments