Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions contracts/casper.go

Large diffs are not rendered by default.

28 changes: 28 additions & 0 deletions contracts/casper/genesis.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"config": {
"casperBlock": 0,
"eip150Block": 0
},
"nonce": "0x0000000000000056",
"difficulty": "0x2000",
"mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"coinbase": "0x0000000000000000000000000000000000000000",
"timestamp": "0x00",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"extraData": "0x11bbe8db4e347b4e8c937c1c8370e4b5ed33adb3db69cbdb7a38e1e50b1b82fa",
"gasLimit": "0x5f5e100",
"alloc": {
"0xf3e1139f77ad1c643b7f7f8271b08b7084e8b751": {
"balance": "5001002003004005006007008"
},
"0x39ba083c30fce59883775fc729bbe1f9de4dee11": {
"balance": "1001002003004005006007008"
},
"0xd7a3bd6c9ea32eff147d067f907ae6b22d436f91": {
"balance": "1001002003004005006007008"
},
"0xea0f0d55ee82edf248ed648a9a8d213fba8b5081": {
"balance": "5125001002003004005006"
}
}
}
33 changes: 28 additions & 5 deletions core/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/contracts"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/params"
Expand Down Expand Up @@ -235,7 +237,7 @@ func (g *Genesis) ToBlock(db ethdb.Database) *types.Block {
}
}
root := statedb.IntermediateRoot(false)
head := &types.Header{
header := &types.Header{
Number: new(big.Int).SetUint64(g.Number),
Nonce: types.EncodeNonce(g.Nonce),
Time: new(big.Int).SetUint64(g.Timestamp),
Expand All @@ -249,15 +251,36 @@ func (g *Genesis) ToBlock(db ethdb.Database) *types.Block {
Root: root,
}
if g.GasLimit == 0 {
head.GasLimit = params.GenesisGasLimit
header.GasLimit = params.GenesisGasLimit
}
if g.Difficulty == nil {
head.Difficulty = params.GenesisDifficulty
header.Difficulty = params.GenesisDifficulty
}

if g.Config != nil && g.Config.IsCasper(header.Number) {
// TODO: need to put in chain config, but how to ensure it's secure
casperPrivateKey := "a27df3e4f46e7792ea951d7abf853b5a5ac3226bacd57286b9df98324386532f"

// Init casper transactions
txs, _, _ := contracts.CasperInitializers(0, casperPrivateKey)
// Add init balance to null sender address
nullSenderAddr := common.HexToAddress("56f8fa946c92a225444170f59fba81707c755161")
statedb.AddBalance(nullSenderAddr, new(big.Int).Exp(big.NewInt(10), big.NewInt(25), nil))

for _, tx := range txs {
_, used, err := ApplyTransaction(g.Config, nil, &g.Coinbase, new(GasPool).AddGas(g.GasLimit), statedb, header, tx, new(uint64), vm.Config{})
if err != nil {
log.Info("Failed to apply transaction", "hash", tx.Hash().Hex(), "err", err)
}

log.Info("Apply Casper tx", "hash", tx.Hash().Hex(), "used", used, "root", statedb.IntermediateRoot(false).Hex())
}
header.Root = statedb.IntermediateRoot(false)
}
statedb.Commit(false)
statedb.Database().TrieDB().Commit(root, true)
statedb.Database().TrieDB().Commit(header.Root, true)

return types.NewBlock(head, nil, nil, nil)
return types.NewBlock(header, nil, nil, nil)
}

// Commit writes the block and state of a genesis specification to the database.
Expand Down
2 changes: 1 addition & 1 deletion params/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ func (c *ChainConfig) IsHomestead(num *big.Int) bool {
return isForked(c.HomesteadBlock, num)
}

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