/
eth1_types.go
68 lines (61 loc) · 1.68 KB
/
eth1_types.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package types
import (
"encoding/json"
"errors"
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/prysmaticlabs/prysm/v4/encoding/bytesutil"
)
// HeaderInfo specifies the block header information in the ETH 1.0 chain.
type HeaderInfo struct {
Number *big.Int `json:"number"`
Hash common.Hash `json:"hash"`
Time uint64 `json:"timestamp"`
}
// Copy sends out a copy of the current header info.
func (h *HeaderInfo) Copy() *HeaderInfo {
return &HeaderInfo{
Hash: bytesutil.ToBytes32(h.Hash[:]),
Number: new(big.Int).Set(h.Number),
Time: h.Time,
}
}
// MarshalJSON marshals as JSON.
func (h *HeaderInfo) MarshalJSON() ([]byte, error) {
type HeaderInfoJson struct {
Number *hexutil.Big `json:"number"`
Hash common.Hash `json:"hash"`
Time hexutil.Uint64 `json:"timestamp"`
}
var enc HeaderInfoJson
enc.Number = (*hexutil.Big)(h.Number)
enc.Hash = h.Hash
enc.Time = hexutil.Uint64(h.Time)
return json.Marshal(enc)
}
// UnmarshalJSON unmarshals from JSON.
func (h *HeaderInfo) UnmarshalJSON(data []byte) error {
type HeaderInfoJson struct {
Number *hexutil.Big `json:"number"`
Hash *common.Hash `json:"hash"`
Time *hexutil.Uint64 `json:"timestamp"`
}
var dec HeaderInfoJson
if err := json.Unmarshal(data, &dec); err != nil {
return err
}
if dec.Time == nil {
return errors.New("missing required field 'timestamp'")
}
h.Time = uint64(*dec.Time)
if dec.Number == nil {
return errors.New("missing required field 'number'")
}
h.Number = (*big.Int)(dec.Number)
if dec.Hash == nil {
return errors.New("missing required field 'hash'")
}
h.Hash = *dec.Hash
return nil
}