forked from hyperledger-archives/burrow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_view.go
97 lines (83 loc) · 2.54 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
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
validatorPublicKey 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{
validatorPublicKey: publicKey,
tmNode: tmNode,
txDecoder: txDecoder,
runID: runID,
}, nil
}
func (nv *NodeView) ValidatorPublicKey() crypto.PublicKey {
return nv.validatorPublicKey
}
func (nv *NodeView) NodeInfo() *NodeInfo {
ni, ok := nv.tmNode.NodeInfo().(p2p.DefaultNodeInfo)
if ok {
return NewNodeInfo(ni)
}
return &NodeInfo{}
}
func (nv *NodeView) IsFastSyncing() bool {
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 {
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
}