core: add missing error checks in genesis block parser

This commit is contained in:
Felix Lange 2017-02-22 14:04:07 +01:00
parent f2c65943d7
commit 31ec73ad9b

View file

@ -28,6 +28,7 @@ import (
"strings" "strings"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/core/state" "github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/ethdb"
@ -67,23 +68,48 @@ func WriteGenesisBlock(chainDb ethdb.Database, reader io.Reader) (*types.Block,
// creating with empty hash always works // creating with empty hash always works
statedb, _ := state.New(common.Hash{}, chainDb) statedb, _ := state.New(common.Hash{}, chainDb)
for addr, account := range genesis.Alloc { for addr, account := range genesis.Alloc {
balance, ok := math.ParseBig(account.Balance)
if !ok {
return nil, fmt.Errorf("invalid balance for account %s: %q", addr, account.Balance)
}
nonce, ok := math.ParseUint64(account.Nonce)
if !ok {
return nil, fmt.Errorf("invalid nonce for account %s: %q", addr, account.Nonce)
}
address := common.HexToAddress(addr) address := common.HexToAddress(addr)
statedb.AddBalance(address, common.String2Big(account.Balance)) statedb.AddBalance(address, balance)
statedb.SetCode(address, common.FromHex(account.Code)) statedb.SetCode(address, common.FromHex(account.Code))
statedb.SetNonce(address, common.String2Big(account.Nonce).Uint64()) statedb.SetNonce(address, nonce)
for key, value := range account.Storage { for key, value := range account.Storage {
statedb.SetState(address, common.HexToHash(key), common.HexToHash(value)) statedb.SetState(address, common.HexToHash(key), common.HexToHash(value))
} }
} }
root, stateBatch := statedb.CommitBatch(false) root, stateBatch := statedb.CommitBatch(false)
difficulty := common.String2Big(genesis.Difficulty) difficulty, ok := math.ParseBig(genesis.Difficulty)
if !ok {
return nil, fmt.Errorf("invalid difficulty: %q", genesis.Difficulty)
}
gaslimit, ok := math.ParseUint64(genesis.GasLimit)
if !ok {
return nil, fmt.Errorf("invalid gas limit: %q", genesis.GasLimit)
}
nonce, ok := math.ParseUint64(genesis.Nonce)
if !ok {
return nil, fmt.Errorf("invalid nonce: %q", genesis.Nonce)
}
timestamp, ok := math.ParseBig(genesis.Timestamp)
if !ok {
return nil, fmt.Errorf("invalid timestamp: %q", genesis.Timestamp)
}
block := types.NewBlock(&types.Header{ block := types.NewBlock(&types.Header{
Nonce: types.EncodeNonce(common.String2Big(genesis.Nonce).Uint64()), Nonce: types.EncodeNonce(nonce),
Time: common.String2Big(genesis.Timestamp), Time: timestamp,
ParentHash: common.HexToHash(genesis.ParentHash), ParentHash: common.HexToHash(genesis.ParentHash),
Extra: common.FromHex(genesis.ExtraData), Extra: common.FromHex(genesis.ExtraData),
GasLimit: common.String2Big(genesis.GasLimit), GasLimit: new(big.Int).SetUint64(gaslimit),
Difficulty: difficulty, Difficulty: difficulty,
MixDigest: common.HexToHash(genesis.Mixhash), MixDigest: common.HexToHash(genesis.Mixhash),
Coinbase: common.HexToAddress(genesis.Coinbase), Coinbase: common.HexToAddress(genesis.Coinbase),
@ -153,7 +179,7 @@ func WriteGenesisBlockForTesting(db ethdb.Database, accounts ...GenesisAccount)
if i != 0 { if i != 0 {
accountJson += "," accountJson += ","
} }
accountJson += fmt.Sprintf(`"0x%x":{"balance":"0x%x"}`, account.Address, account.Balance.Bytes()) accountJson += fmt.Sprintf(`"0x%x":{"balance":"%d"}`, account.Address, account.Balance)
} }
accountJson += "}" accountJson += "}"
@ -163,7 +189,10 @@ func WriteGenesisBlockForTesting(db ethdb.Database, accounts ...GenesisAccount)
"difficulty":"0x%x", "difficulty":"0x%x",
"alloc": %s "alloc": %s
}`, types.EncodeNonce(0), params.GenesisGasLimit.Bytes(), params.GenesisDifficulty.Bytes(), accountJson) }`, types.EncodeNonce(0), params.GenesisGasLimit.Bytes(), params.GenesisDifficulty.Bytes(), accountJson)
block, _ := WriteGenesisBlock(db, strings.NewReader(testGenesis)) block, err := WriteGenesisBlock(db, strings.NewReader(testGenesis))
if err != nil {
panic(err)
}
return block return block
} }