github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/tests/gen_difficultytest.go (about) 1 2 //此源码被清华学神尹成大魔王专业翻译分析并修改 3 //尹成QQ77025077 4 //尹成微信18510341407 5 //尹成所在QQ群721929980 6 //尹成邮箱 yinc13@mails.tsinghua.edu.cn 7 //尹成毕业于清华大学,微软区块链领域全球最有价值专家 8 //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620 9 // 10 11 package tests 12 13 import ( 14 "encoding/json" 15 "math/big" 16 17 "github.com/ethereum/go-ethereum/common" 18 "github.com/ethereum/go-ethereum/common/math" 19 ) 20 21 var _ = (*difficultyTestMarshaling)(nil) 22 23 func (d DifficultyTest) MarshalJSON() ([]byte, error) { 24 type DifficultyTest struct { 25 ParentTimestamp *math.HexOrDecimal256 `json:"parentTimestamp"` 26 ParentDifficulty *math.HexOrDecimal256 `json:"parentDifficulty"` 27 UncleHash common.Hash `json:"parentUncles"` 28 CurrentTimestamp *math.HexOrDecimal256 `json:"currentTimestamp"` 29 CurrentBlockNumber math.HexOrDecimal64 `json:"currentBlockNumber"` 30 CurrentDifficulty *math.HexOrDecimal256 `json:"currentDifficulty"` 31 } 32 var enc DifficultyTest 33 enc.ParentTimestamp = (*math.HexOrDecimal256)(d.ParentTimestamp) 34 enc.ParentDifficulty = (*math.HexOrDecimal256)(d.ParentDifficulty) 35 enc.UncleHash = d.UncleHash 36 enc.CurrentTimestamp = (*math.HexOrDecimal256)(d.CurrentTimestamp) 37 enc.CurrentBlockNumber = math.HexOrDecimal64(d.CurrentBlockNumber) 38 enc.CurrentDifficulty = (*math.HexOrDecimal256)(d.CurrentDifficulty) 39 return json.Marshal(&enc) 40 } 41 42 func (d *DifficultyTest) UnmarshalJSON(input []byte) error { 43 type DifficultyTest struct { 44 ParentTimestamp *math.HexOrDecimal256 `json:"parentTimestamp"` 45 ParentDifficulty *math.HexOrDecimal256 `json:"parentDifficulty"` 46 UncleHash *common.Hash `json:"parentUncles"` 47 CurrentTimestamp *math.HexOrDecimal256 `json:"currentTimestamp"` 48 CurrentBlockNumber *math.HexOrDecimal64 `json:"currentBlockNumber"` 49 CurrentDifficulty *math.HexOrDecimal256 `json:"currentDifficulty"` 50 } 51 var dec DifficultyTest 52 if err := json.Unmarshal(input, &dec); err != nil { 53 return err 54 } 55 if dec.ParentTimestamp != nil { 56 d.ParentTimestamp = (*big.Int)(dec.ParentTimestamp) 57 } 58 if dec.ParentDifficulty != nil { 59 d.ParentDifficulty = (*big.Int)(dec.ParentDifficulty) 60 } 61 if dec.UncleHash != nil { 62 d.UncleHash = *dec.UncleHash 63 } 64 if dec.CurrentTimestamp != nil { 65 d.CurrentTimestamp = (*big.Int)(dec.CurrentTimestamp) 66 } 67 if dec.CurrentBlockNumber != nil { 68 d.CurrentBlockNumber = uint64(*dec.CurrentBlockNumber) 69 } 70 if dec.CurrentDifficulty != nil { 71 d.CurrentDifficulty = (*big.Int)(dec.CurrentDifficulty) 72 } 73 return nil 74 }