This repository has been archived by the owner on May 13, 2022. It is now read-only.
/
node_view.go
116 lines (101 loc) · 2.78 KB
/
node_view.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
package tendermint
import (
"fmt"
"github.com/hyperledger/burrow/crypto"
"github.com/hyperledger/burrow/txs"
"github.com/streadway/simpleuuid"
"github.com/tendermint/tendermint/consensus"
ctypes "github.com/tendermint/tendermint/consensus/types"
"github.com/tendermint/tendermint/p2p"
"github.com/tendermint/tendermint/state"
"github.com/tendermint/tendermint/types"
)
type NodeView struct {
tmNode *Node
publicKey crypto.PublicKey
txDecoder txs.Decoder
runID simpleuuid.UUID
}
func NewNodeView(tmNode *Node, txDecoder txs.Decoder, runID simpleuuid.UUID) (*NodeView, error) {
publicKey, err := crypto.PublicKeyFromTendermintPubKey(tmNode.PrivValidator().GetPubKey())
if err != nil {
return nil, err
}
tmNode.BlockStore()
return &NodeView{
tmNode: tmNode,
publicKey: publicKey,
txDecoder: txDecoder,
runID: runID,
}, nil
}
func (nv *NodeView) ValidatorPublicKey() crypto.PublicKey {
if nv == nil {
return crypto.PublicKey{}
}
return nv.publicKey
}
func (nv *NodeView) ValidatorAddress() crypto.Address {
if nv == nil {
return crypto.Address{}
}
return nv.publicKey.GetAddress()
}
func (nv *NodeView) NodeInfo() *NodeInfo {
if nv == nil {
return nil
}
ni, ok := nv.tmNode.NodeInfo().(p2p.DefaultNodeInfo)
if ok {
return NewNodeInfo(ni)
}
return &NodeInfo{}
}
func (nv *NodeView) IsFastSyncing() bool {
if nv == nil {
return true
}
return nv.tmNode.ConsensusReactor().FastSync()
}
func (nv *NodeView) Peers() p2p.IPeerSet {
return nv.tmNode.Switch().Peers()
}
func (nv *NodeView) BlockStore() state.BlockStoreRPC {
return nv.tmNode.BlockStore()
}
func (nv *NodeView) RunID() simpleuuid.UUID {
if nv == nil {
return []byte("00000000-0000-0000-0000-000000000000")
}
return nv.runID
}
// Pass -1 to get all available transactions
func (nv *NodeView) MempoolTransactions(maxTxs int) ([]*txs.Envelope, error) {
var transactions []*txs.Envelope
for _, txBytes := range nv.tmNode.MempoolReactor().Mempool.ReapMaxTxs(maxTxs) {
txEnv, err := nv.txDecoder.DecodeTx(txBytes)
if err != nil {
return nil, err
}
transactions = append(transactions, txEnv)
}
return transactions, nil
}
func (nv *NodeView) RoundState() *ctypes.RoundState {
return nv.tmNode.ConsensusState().GetRoundState()
}
func (nv *NodeView) RoundStateJSON() ([]byte, error) {
return nv.tmNode.ConsensusState().GetRoundStateJSON()
}
func (nv *NodeView) PeerRoundStates() ([]*ctypes.PeerRoundState, error) {
peers := nv.tmNode.Switch().Peers().List()
peerRoundStates := make([]*ctypes.PeerRoundState, len(peers))
for i, peer := range peers {
peerState, ok := peer.Get(types.PeerStateKey).(*consensus.PeerState)
if !ok {
return nil, fmt.Errorf("could not get PeerState for peer: %s", peer)
}
peerRoundStates[i] = peerState.GetRoundState()
}
return peerRoundStates, nil
}