@@ -28,8 +28,10 @@ import (
28
28
"github.com/ethereum/go-ethereum/common"
29
29
"github.com/ethereum/go-ethereum/common/hexutil"
30
30
"github.com/ethereum/go-ethereum/common/math"
31
+ "github.com/ethereum/go-ethereum/contracts"
31
32
"github.com/ethereum/go-ethereum/core/state"
32
33
"github.com/ethereum/go-ethereum/core/types"
34
+ "github.com/ethereum/go-ethereum/core/vm"
33
35
"github.com/ethereum/go-ethereum/ethdb"
34
36
"github.com/ethereum/go-ethereum/log"
35
37
"github.com/ethereum/go-ethereum/params"
@@ -235,7 +237,7 @@ func (g *Genesis) ToBlock(db ethdb.Database) *types.Block {
235
237
}
236
238
}
237
239
root := statedb .IntermediateRoot (false )
238
- head := & types.Header {
240
+ header := & types.Header {
239
241
Number : new (big.Int ).SetUint64 (g .Number ),
240
242
Nonce : types .EncodeNonce (g .Nonce ),
241
243
Time : new (big.Int ).SetUint64 (g .Timestamp ),
@@ -249,15 +251,36 @@ func (g *Genesis) ToBlock(db ethdb.Database) *types.Block {
249
251
Root : root ,
250
252
}
251
253
if g .GasLimit == 0 {
252
- head .GasLimit = params .GenesisGasLimit
254
+ header .GasLimit = params .GenesisGasLimit
253
255
}
254
256
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 )
256
279
}
257
280
statedb .Commit (false )
258
- statedb .Database ().TrieDB ().Commit (root , true )
281
+ statedb .Database ().TrieDB ().Commit (header . Root , true )
259
282
260
- return types .NewBlock (head , nil , nil , nil )
283
+ return types .NewBlock (header , nil , nil , nil )
261
284
}
262
285
263
286
// Commit writes the block and state of a genesis specification to the database.
0 commit comments