-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBlockInfo.go
90 lines (84 loc) · 3.39 KB
/
BlockInfo.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
*
* This file is part of go-palletone.
* go-palletone is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* go-palletone is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with go-palletone. If not, see <http://www.gnu.org/licenses/>.
* /
*
* * @author PalletOne core developer <dev@pallet.one>
* * @date 2018-2019
*
*/
package adaptor
import (
"encoding/hex"
"encoding/json"
)
//BlockInfo 区块的基本信息
type BlockInfo struct {
BlockID []byte `json:"block_id"` //交易被打包到了哪个区块ID
BlockHeight uint `json:"block_height"` //交易被打包到的区块的高度
Timestamp uint64 `json:"timestamp"` //交易被打包的时间戳
ParentBlockID []byte `json:"parent_block_id"` //父区块ID
HeaderRawData []byte `json:"header_raw_data"` //区块头的原始信息
TxsRoot []byte `json:"txs_root"` //默克尔根
ProducerAddress string `json:"producer_address"` //生产者地址
IsStable bool `json:"is_stable"` //是否已经稳定不可逆
}
func (block *BlockInfo) String() string {
d, _ := json.Marshal(block)
return string(d)
}
type blockInfo4Json struct {
BlockID string `json:"block_id"` //交易被打包到了哪个区块ID
BlockHeight uint `json:"block_height"` //交易被打包到的区块的高度
Timestamp uint64 `json:"timestamp"` //交易被打包的时间戳
ParentBlockID string `json:"parent_block_id"` //父区块ID
HeaderRawData string `json:"header_raw_data"` //区块头的原始信息
TxsRoot string `json:"txs_root"` //默克尔根
ProducerAddress string `json:"producer_address"` //生产者地址
IsStable bool `json:"is_stable"` //是否已经稳定不可逆
}
func convertBlockInfo2Json(b BlockInfo) blockInfo4Json {
return blockInfo4Json{
BlockID: hex.EncodeToString(b.BlockID),
BlockHeight: b.BlockHeight,
Timestamp: b.Timestamp,
ParentBlockID: hex.EncodeToString(b.ParentBlockID),
HeaderRawData: hex.EncodeToString(b.HeaderRawData),
TxsRoot: hex.EncodeToString(b.TxsRoot),
ProducerAddress: b.ProducerAddress,
IsStable: b.IsStable,
}
}
func setBlockInfoFromJson(b *BlockInfo, bjson blockInfo4Json) {
b.BlockID, _ = hex.DecodeString(bjson.BlockID)
b.BlockHeight = bjson.BlockHeight
b.Timestamp = bjson.Timestamp
b.ParentBlockID, _ = hex.DecodeString(bjson.ParentBlockID)
b.HeaderRawData, _ = hex.DecodeString(bjson.HeaderRawData)
b.TxsRoot, _ = hex.DecodeString(bjson.TxsRoot)
b.ProducerAddress = bjson.ProducerAddress
b.IsStable = bjson.IsStable
}
func (block *BlockInfo) MarshalJSON() ([]byte, error) {
b4json := convertBlockInfo2Json(*block)
return json.Marshal(b4json)
}
func (block *BlockInfo) UnmarshalJSON(input []byte) error {
b4Json := blockInfo4Json{}
err := json.Unmarshal(input, &b4Json)
if err != nil {
return err
}
setBlockInfoFromJson(block, b4Json)
return nil
}