Skip to content

Commit a4bbfd6

Browse files
committed
core, contracts/casper: init transactions for casper
1 parent 44a058f commit a4bbfd6

File tree

4 files changed

+138
-6
lines changed

4 files changed

+138
-6
lines changed

contracts/casper.go

Lines changed: 81 additions & 0 deletions
Large diffs are not rendered by default.

contracts/casper/genesis.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"config": {
3+
"casperBlock": 0,
4+
"eip150Block": 0
5+
},
6+
"nonce": "0x0000000000000056",
7+
"difficulty": "0x2000",
8+
"mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
9+
"coinbase": "0x0000000000000000000000000000000000000000",
10+
"timestamp": "0x00",
11+
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
12+
"extraData": "0x11bbe8db4e347b4e8c937c1c8370e4b5ed33adb3db69cbdb7a38e1e50b1b82fa",
13+
"gasLimit": "0x5f5e100",
14+
"alloc": {
15+
"0xf3e1139f77ad1c643b7f7f8271b08b7084e8b751": {
16+
"balance": "5001002003004005006007008"
17+
},
18+
"0x39ba083c30fce59883775fc729bbe1f9de4dee11": {
19+
"balance": "1001002003004005006007008"
20+
},
21+
"0xd7a3bd6c9ea32eff147d067f907ae6b22d436f91": {
22+
"balance": "1001002003004005006007008"
23+
},
24+
"0xea0f0d55ee82edf248ed648a9a8d213fba8b5081": {
25+
"balance": "5125001002003004005006"
26+
}
27+
}
28+
}

core/genesis.go

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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.

params/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ func (c *ChainConfig) IsHomestead(num *big.Int) bool {
174174
return isForked(c.HomesteadBlock, num)
175175
}
176176

177-
// IsDAO returns whether num is either equal to the DAO fork block or greater.
177+
// IsDAOFork returns whether num is either equal to the DAO fork block or greater.
178178
func (c *ChainConfig) IsDAOFork(num *big.Int) bool {
179179
return isForked(c.DAOForkBlock, num)
180180
}

0 commit comments

Comments
 (0)