-
Notifications
You must be signed in to change notification settings - Fork 3
/
api.go
176 lines (147 loc) · 5.41 KB
/
api.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
package node
import (
"github.com/PositionExchange/posichain/consensus/quorum"
"github.com/PositionExchange/posichain/core/types"
"github.com/PositionExchange/posichain/eth/rpc"
"github.com/PositionExchange/posichain/hmy"
"github.com/PositionExchange/posichain/rosetta"
hmy_rpc "github.com/PositionExchange/posichain/rpc"
rpc_common "github.com/PositionExchange/posichain/rpc/common"
"github.com/PositionExchange/posichain/rpc/filters"
"github.com/libp2p/go-libp2p-core/peer"
)
// IsCurrentlyLeader exposes if node is currently the leader node
func (node *Node) IsCurrentlyLeader() bool {
return node.Consensus.IsLeader()
}
// PeerConnectivity ..
func (node *Node) PeerConnectivity() (int, int, int) {
return node.host.C()
}
// ListPeer return list of peers for a certain topic
func (node *Node) ListPeer(topic string) []peer.ID {
return node.host.ListPeer(topic)
}
// ListTopic return list of topics the node subscribed
func (node *Node) ListTopic() []string {
return node.host.ListTopic()
}
// ListBlockedPeer return list of blocked peers
func (node *Node) ListBlockedPeer() []peer.ID {
return node.host.ListBlockedPeer()
}
// PendingCXReceipts returns node.pendingCXReceiptsProof
func (node *Node) PendingCXReceipts() []*types.CXReceiptsProof {
cxReceipts := make([]*types.CXReceiptsProof, len(node.pendingCXReceipts))
i := 0
for _, cxReceipt := range node.pendingCXReceipts {
cxReceipts[i] = cxReceipt
i++
}
return cxReceipts
}
// ReportStakingErrorSink is the report of failed staking transactions this node has (held in memory only)
func (node *Node) ReportStakingErrorSink() types.TransactionErrorReports {
return node.TransactionErrorSink.StakingReport()
}
// GetNodeBootTime ..
func (node *Node) GetNodeBootTime() int64 {
return node.unixTimeAtNodeStart
}
// ReportPlainErrorSink is the report of failed transactions this node has (held in memory only)
func (node *Node) ReportPlainErrorSink() types.TransactionErrorReports {
return node.TransactionErrorSink.PlainReport()
}
// StartRPC start RPC service
func (node *Node) StartRPC() error {
harmony := hmy.New(node, node.TxPool, node.CxPool, node.Consensus.ShardID)
// Gather all the possible APIs to surface
apis := node.APIs(harmony)
return hmy_rpc.StartServers(harmony, apis, node.NodeConfig.RPCServer, node.HarmonyConfig.RPCOpt)
}
// StopRPC stop RPC service
func (node *Node) StopRPC() error {
return hmy_rpc.StopServers()
}
// StartRosetta start rosetta service
func (node *Node) StartRosetta() error {
harmony := hmy.New(node, node.TxPool, node.CxPool, node.Consensus.ShardID)
return rosetta.StartServers(harmony, node.NodeConfig.RosettaServer, node.NodeConfig.RPCServer.RateLimiterEnabled, node.NodeConfig.RPCServer.RequestsPerSecond)
}
// StopRosetta stops rosetta service
func (node *Node) StopRosetta() error {
return rosetta.StopServers()
}
// APIs return the collection of local RPC services.
// NOTE, some of these services probably need to be moved to somewhere else.
func (node *Node) APIs(harmony *hmy.Harmony) []rpc.API {
// Append all the local APIs and return
return []rpc.API{
hmy_rpc.NewPublicNetAPI(node.host, harmony.ChainID, hmy_rpc.V1),
hmy_rpc.NewPublicNetAPI(node.host, harmony.ChainID, hmy_rpc.V2),
hmy_rpc.NewPublicNetAPI(node.host, harmony.ChainID, hmy_rpc.Eth),
hmy_rpc.NewPublicWeb3API(),
filters.NewPublicFilterAPI(harmony, false, "hmy"),
filters.NewPublicFilterAPI(harmony, false, "eth"),
}
}
// GetConsensusMode returns the current consensus mode
func (node *Node) GetConsensusMode() string {
return node.Consensus.GetConsensusMode()
}
// GetConsensusPhase returns the current consensus phase
func (node *Node) GetConsensusPhase() string {
return node.Consensus.GetConsensusPhase()
}
// GetConsensusViewChangingID returns the view changing ID
func (node *Node) GetConsensusViewChangingID() uint64 {
return node.Consensus.GetViewChangingID()
}
// GetConsensusCurViewID returns the current view ID
func (node *Node) GetConsensusCurViewID() uint64 {
return node.Consensus.GetCurBlockViewID()
}
// GetConsensusBlockNum returns the current block number of the consensus
func (node *Node) GetConsensusBlockNum() uint64 {
return node.Consensus.BlockNum()
}
// GetConsensusInternal returns consensus internal data
func (node *Node) GetConsensusInternal() rpc_common.ConsensusInternal {
return rpc_common.ConsensusInternal{
ViewID: node.GetConsensusCurViewID(),
ViewChangeID: node.GetConsensusViewChangingID(),
Mode: node.GetConsensusMode(),
Phase: node.GetConsensusPhase(),
BlockNum: node.GetConsensusBlockNum(),
ConsensusTime: node.Consensus.GetFinality(),
}
}
// IsBackup returns the node is in backup mode
func (node *Node) IsBackup() bool {
return node.Consensus.IsBackup()
}
// SetNodeBackupMode change node backup mode
func (node *Node) SetNodeBackupMode(isBackup bool) bool {
if node.Consensus.IsBackup() == isBackup {
return false
}
node.Consensus.SetIsBackup(isBackup)
node.Consensus.ResetViewChangeState()
return true
}
func (node *Node) GetConfig() rpc_common.Config {
return rpc_common.Config{
HarmonyConfig: *node.HarmonyConfig,
NodeConfig: *node.NodeConfig,
ChainConfig: node.chainConfig,
}
}
// GetLastSigningPower get last signed power
func (node *Node) GetLastSigningPower() (float64, error) {
power, err := node.Consensus.Decider.CurrentTotalPower(quorum.Commit)
if err != nil {
return 0, err
}
round := float64(power.MulInt64(10000).RoundInt64()) / 10000
return round, nil
}