types: block json unmarshal method added

This commit is contained in:
obscuren 2015-05-31 15:53:17 +02:00
parent fa4aefee44
commit b26f5e0bb7
3 changed files with 30 additions and 6 deletions

View File

@ -36,16 +36,16 @@ func Big(num string) *big.Int {
return n return n
} }
// BigD // Bytes2Big
// //
// Shortcut for new(big.Int).SetBytes(...) func BytesToBig(data []byte) *big.Int {
func Bytes2Big(data []byte) *big.Int {
n := new(big.Int) n := new(big.Int)
n.SetBytes(data) n.SetBytes(data)
return n return n
} }
func BigD(data []byte) *big.Int { return Bytes2Big(data) } func Bytes2Big(data []byte) *big.Int { return BytesToBig(data) }
func BigD(data []byte) *big.Int { return BytesToBig(data) }
func String2Big(num string) *big.Int { func String2Big(num string) *big.Int {
n := new(big.Int) n := new(big.Int)

View File

@ -36,7 +36,7 @@ func GenesisBlock(db common.Database) *types.Block {
Balance string Balance string
Code string Code string
} }
err := json.Unmarshal(GenesisData, &accounts) err := json.Unmarshal(GenesisAccounts, &accounts)
if err != nil { if err != nil {
fmt.Println("enable to decode genesis json data:", err) fmt.Println("enable to decode genesis json data:", err)
os.Exit(1) os.Exit(1)
@ -57,7 +57,7 @@ func GenesisBlock(db common.Database) *types.Block {
return genesis return genesis
} }
var GenesisData = []byte(`{ var GenesisAccounts = []byte(`{
"0000000000000000000000000000000000000001": {"balance": "1"}, "0000000000000000000000000000000000000001": {"balance": "1"},
"0000000000000000000000000000000000000002": {"balance": "1"}, "0000000000000000000000000000000000000002": {"balance": "1"},
"0000000000000000000000000000000000000003": {"balance": "1"}, "0000000000000000000000000000000000000003": {"balance": "1"},

View File

@ -1,7 +1,9 @@
package types package types
import ( import (
"bytes"
"encoding/binary" "encoding/binary"
"encoding/json"
"fmt" "fmt"
"io" "io"
"math/big" "math/big"
@ -80,6 +82,28 @@ func (self *Header) RlpData() interface{} {
return self.rlpData(true) return self.rlpData(true)
} }
func (h *Header) UnmarshalJSON(data []byte) error {
var ext struct {
ParentHash string
Coinbase string
Difficulty string
GasLimit string
Time uint64
Extra string
}
dec := json.NewDecoder(bytes.NewReader(data))
if err := dec.Decode(&ext); err != nil {
return err
}
h.ParentHash = common.HexToHash(ext.ParentHash)
h.Coinbase = common.HexToAddress(ext.Coinbase)
h.Difficulty = common.String2Big(ext.Difficulty)
h.Time = ext.Time
h.Extra = []byte(ext.Extra)
return nil
}
func rlpHash(x interface{}) (h common.Hash) { func rlpHash(x interface{}) (h common.Hash) {
hw := sha3.NewKeccak256() hw := sha3.NewKeccak256()
rlp.Encode(hw, x) rlp.Encode(hw, x)