forked from irisnet/irishub-sdk-go
/
node.go
204 lines (180 loc) · 6.72 KB
/
node.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package types
import (
ctypes "github.com/tendermint/tendermint/rpc/core/types"
"github.com/tendermint/tendermint/types"
"time"
)
//=========================================Node Status==============================================================================================
// Node Status
type ResultStatus struct {
NodeInfo DefaultNodeInfo `json:"node_info"`
SyncInfo SyncInfo `json:"sync_info"`
ValidatorInfo ValidatorInfo `json:"validator_info"`
}
type DefaultNodeInfo struct {
ProtocolVersion ProtocolVersion `json:"protocol_version"`
DefaultNodeID string `json:"id"` // authenticated identifier
ListenAddr string `json:"listen_addr"` // accepting incoming
Network string `json:"network"` // network/chain ID
Version string `json:"version"` // major.minor.revision
Channels string `json:"channels"` // channels this node knows about
Moniker string `json:"moniker"` // arbitrary moniker
Other DefaultNodeInfoOther `json:"other"` // other application specific data
}
// Info about the node's syncing state
type SyncInfo struct {
LatestBlockHash string `json:"latest_block_hash"`
LatestAppHash string `json:"latest_app_hash"`
LatestBlockHeight int64 `json:"latest_block_height"`
LatestBlockTime time.Time `json:"latest_block_time"`
EarliestBlockHash string `json:"earliest_block_hash"`
EarliestAppHash string `json:"earliest_app_hash"`
EarliestBlockHeight int64 `json:"earliest_block_height"`
EarliestBlockTime time.Time `json:"earliest_block_time"`
CatchingUp bool `json:"catching_up"`
}
// Info about the node's validator
type ValidatorInfo struct {
Address string `json:"address"`
PubKey PubKey `json:"pub_key"`
VotingPower int64 `json:"voting_power"`
}
type ProtocolVersion struct {
P2P uint64 `json:"p2p"`
Block uint64 `json:"block"`
App uint64 `json:"app"`
}
type DefaultNodeInfoOther struct {
TxIndex string `json:"tx_index"`
RPCAddress string `json:"rpc_address"`
}
func ParseNodeStatus(rs *ctypes.ResultStatus) ResultStatus {
nodeInfo := DefaultNodeInfo{
ProtocolVersion: ProtocolVersion{
P2P: uint64(rs.NodeInfo.ProtocolVersion.P2P),
Block: uint64(rs.NodeInfo.ProtocolVersion.Block),
App: uint64(rs.NodeInfo.ProtocolVersion.App),
},
DefaultNodeID: string(rs.NodeInfo.DefaultNodeID),
ListenAddr: rs.NodeInfo.ListenAddr,
Network: rs.NodeInfo.Network,
Version: rs.NodeInfo.Version,
Channels: rs.NodeInfo.Channels.String(),
Moniker: rs.NodeInfo.Moniker,
Other: DefaultNodeInfoOther{
TxIndex: rs.NodeInfo.Other.TxIndex,
RPCAddress: rs.NodeInfo.Other.RPCAddress,
},
}
syncInfo := SyncInfo{
LatestBlockHash: rs.SyncInfo.LatestBlockHash.String(),
LatestAppHash: rs.SyncInfo.LatestAppHash.String(),
LatestBlockHeight: rs.SyncInfo.LatestBlockHeight,
LatestBlockTime: rs.SyncInfo.LatestBlockTime,
EarliestBlockHash: rs.SyncInfo.EarliestBlockHash.String(),
EarliestAppHash: rs.SyncInfo.EarliestBlockHash.String(),
EarliestBlockHeight: rs.SyncInfo.EarliestBlockHeight,
EarliestBlockTime: rs.SyncInfo.EarliestBlockTime,
CatchingUp: rs.SyncInfo.CatchingUp,
}
var pubKey PubKey
if bz, err := codec.MarshalJSON(rs.ValidatorInfo.PubKey); err == nil {
_ = codec.UnmarshalJSON(bz, &pubKey)
}
validatorInfo := ValidatorInfo{
Address: rs.ValidatorInfo.Address.String(),
PubKey: pubKey,
VotingPower: rs.ValidatorInfo.VotingPower,
}
return ResultStatus{
NodeInfo: nodeInfo,
SyncInfo: syncInfo,
ValidatorInfo: validatorInfo,
}
}
//=========================================Genesis==============================================================================================
// GenesisDoc defines the initial conditions for a tendermint blockchain, in particular its validator set.
type GenesisDoc struct {
GenesisTime time.Time `json:"genesis_time"`
ChainID string `json:"chain_id"`
ConsensusParams *ConsensusParams `json:"consensus_params,omitempty"`
Validators []GenesisValidator `json:"validators,omitempty"`
AppHash string `json:"app_hash"`
AppState string `json:"app_state,omitempty"`
}
type ConsensusParams struct {
Block BlockParams `json:"block"`
Evidence EvidenceParams `json:"evidence"`
Validator ValidatorParams `json:"validator"`
}
// ValidatorParams restrict the public key types validators can use.
// NOTE: uses ABCI pubkey naming, not Amino names.
type ValidatorParams struct {
PubKeyTypes []string `json:"pub_key_types"`
}
// HashedParams is a subset of ConsensusParams.
// It is amino encoded and hashed into
// the Header.ConsensusHash.
type HashedParams struct {
BlockMaxBytes int64
BlockMaxGas int64
}
// BlockParams define limits on the block size and gas plus minimum time
// between blocks.
type BlockParams struct {
MaxBytes int64 `json:"max_bytes"`
MaxGas int64 `json:"max_gas"`
// Minimum time increment between consecutive blocks (in milliseconds)
// Not exposed to the application.
TimeIotaMs int64 `json:"time_iota_ms"`
}
// EvidenceParams determine how we handle evidence of malfeasance.
type EvidenceParams struct {
MaxAgeNumBlocks int64 `json:"max_age_num_blocks"` // only accept new evidence more recent than this
MaxAgeDuration time.Duration `json:"max_age_duration"`
}
// GenesisValidator is an initial validator.
type GenesisValidator struct {
Address string `json:"address"`
PubKey PubKey `json:"pub_key"`
Power int64 `json:"power"`
Name string `json:"name"`
}
func ParseGenesis(g *types.GenesisDoc) GenesisDoc {
consensusParams := ConsensusParams{
Block: BlockParams{
MaxBytes: g.ConsensusParams.Block.MaxBytes,
MaxGas: g.ConsensusParams.Block.MaxGas,
TimeIotaMs: g.ConsensusParams.Block.TimeIotaMs,
},
Evidence: EvidenceParams{
MaxAgeNumBlocks: g.ConsensusParams.Evidence.MaxAgeNumBlocks,
MaxAgeDuration: g.ConsensusParams.Evidence.MaxAgeDuration,
},
Validator: ValidatorParams{
PubKeyTypes: g.ConsensusParams.Validator.PubKeyTypes,
},
}
validators := make([]GenesisValidator, 0)
for _, v := range g.Validators {
var pubKey PubKey
if bz, err := codec.MarshalJSON(v.PubKey); err == nil {
_ = codec.UnmarshalJSON(bz, &pubKey)
}
validators = append(validators, GenesisValidator{
Address: v.Address.String(),
PubKey: pubKey,
Power: v.Power,
Name: v.Name,
})
}
res := GenesisDoc{
GenesisTime: g.GenesisTime,
ChainID: g.ChainID,
ConsensusParams: &consensusParams,
Validators: validators,
AppHash: g.AppHash.String(),
AppState: string(g.AppState),
}
return res
}