forked from hyperledger-archives/burrow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tendermint.go
285 lines (250 loc) · 10.4 KB
/
tendermint.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// Copyright 2017 Monax Industries Limited
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package tendermint
import (
"fmt"
"path"
"strings"
abci_types "github.com/tendermint/abci/types"
crypto "github.com/tendermint/go-crypto"
p2p "github.com/tendermint/go-p2p"
tendermint_consensus "github.com/tendermint/tendermint/consensus"
node "github.com/tendermint/tendermint/node"
proxy "github.com/tendermint/tendermint/proxy"
tendermint_types "github.com/tendermint/tendermint/types"
edb_event "github.com/monax/eris-db/event"
config "github.com/monax/eris-db/config"
manager_types "github.com/monax/eris-db/manager/types"
// files "github.com/monax/eris-db/files"
blockchain_types "github.com/monax/eris-db/blockchain/types"
consensus_types "github.com/monax/eris-db/consensus/types"
"github.com/monax/eris-db/logging"
"github.com/monax/eris-db/logging/loggers"
"github.com/monax/eris-db/txs"
"github.com/tendermint/go-wire"
)
type Tendermint struct {
tmintNode *node.Node
tmintConfig *TendermintConfig
chainId string
logger loggers.InfoTraceLogger
}
// Compiler checks to ensure Tendermint successfully implements
// eris-db/definitions Consensus and Blockchain
var _ consensus_types.ConsensusEngine = (*Tendermint)(nil)
var _ blockchain_types.Blockchain = (*Tendermint)(nil)
func NewTendermint(moduleConfig *config.ModuleConfig,
application manager_types.Application,
logger loggers.InfoTraceLogger) (*Tendermint, error) {
// re-assert proper configuration for module
if moduleConfig.Version != GetTendermintVersion().GetMinorVersionString() {
return nil, fmt.Errorf("Version string %s did not match %s",
moduleConfig.Version, GetTendermintVersion().GetMinorVersionString())
}
// loading the module has ensured the working and data directory
// for tendermint have been created, but the config files needs
// to be written in tendermint's root directory.
// NOTE: [ben] as elsewhere Sub panics if config file does not have this
// subtree. To shield in go-routine, or PR to viper.
if !moduleConfig.Config.IsSet("configuration") {
return nil, fmt.Errorf("Failed to extract Tendermint configuration subtree.")
}
tendermintConfigViper, err := config.ViperSubConfig(moduleConfig.Config, "configuration")
if tendermintConfigViper == nil {
return nil,
fmt.Errorf("Failed to extract Tendermint configuration subtree: %s", err)
}
// wrap a copy of the viper config in a tendermint/go-config interface
tmintConfig := GetTendermintConfig(tendermintConfigViper)
// complete the tendermint configuration with default flags
tmintConfig.AssertTendermintDefaults(moduleConfig.ChainId,
moduleConfig.WorkDir, moduleConfig.DataDir, moduleConfig.RootDir)
privateValidatorFilePath := path.Join(moduleConfig.RootDir,
moduleConfig.Config.GetString("private_validator_file"))
if moduleConfig.Config.GetString("private_validator_file") == "" {
return nil, fmt.Errorf("No private validator file provided.")
}
// override tendermint configurations to force consistency with overruling
// settings
tmintConfig.AssertTendermintConsistency(moduleConfig,
privateValidatorFilePath)
chainId := tmintConfig.GetString("chain_id")
logging.TraceMsg(logger, "Loaded Tendermint sub-configuration",
"chainId", chainId,
"genesisFile", tmintConfig.GetString("genesis_file"),
"nodeLocalAddress", tmintConfig.GetString("node_laddr"),
"moniker", tmintConfig.GetString("moniker"),
"seeds", tmintConfig.GetString("seeds"),
"fastSync", tmintConfig.GetBool("fast_sync"),
"rpcLocalAddress", tmintConfig.GetString("rpc_laddr"),
"databaseDirectory", tmintConfig.GetString("db_dir"),
"privateValidatorFile", tmintConfig.GetString("priv_validator_file"),
"privValFile", moduleConfig.Config.GetString("private_validator_file"))
// TODO: [ben] do not "or Generate Validator keys", rather fail directly
// TODO: [ben] implement the signer for Private validator over eris-keys
// TODO: [ben] copy from rootDir to tendermint workingDir;
privateValidator := tendermint_types.LoadOrGenPrivValidator(
path.Join(moduleConfig.RootDir,
moduleConfig.Config.GetString("private_validator_file")))
// TODO: [Silas] we want to something better than this like not not have it in
// the config at all, but for now I think it's much safer to make sure we are
// not running the tendermint RPC as it could lead to unexpected behaviour,
// not least if we accidentally try to run it on the same address as our own
if tmintConfig.GetString("rpc_laddr") != "" {
logging.InfoMsg(logger, "Force disabling Tendermint's native RPC",
"provided_rpc_laddr", tmintConfig.GetString("rpc_laddr"))
tmintConfig.Set("rpc_laddr", "")
}
newNode := node.NewNode(tmintConfig, privateValidator,
proxy.NewLocalClientCreator(application))
listener := p2p.NewDefaultListener("tcp", tmintConfig.GetString("node_laddr"),
tmintConfig.GetBool("skip_upnp"))
newNode.AddListener(listener)
// TODO: [ben] delay starting the node to a different function, to hand
// control over events to Core
if err := newNode.Start(); err != nil {
newNode.Stop()
return nil, fmt.Errorf("Failed to start Tendermint consensus node: %v", err)
}
logging.InfoMsg(logger, "Tendermint consensus node started",
"nodeAddress", tmintConfig.GetString("node_laddr"),
"transportProtocol", "tcp",
"upnp", !tmintConfig.GetBool("skip_upnp"),
"moniker", tmintConfig.GetString("moniker"))
// If seedNode is provided by config, dial out.
if tmintConfig.GetString("seeds") != "" {
seeds := strings.Split(tmintConfig.GetString("seeds"), ",")
newNode.DialSeeds(seeds)
logging.TraceMsg(logger, "Tendermint node called seeds",
"seeds", seeds)
}
return &Tendermint{
tmintNode: newNode,
tmintConfig: tmintConfig,
chainId: chainId,
logger: logger,
}, nil
}
//------------------------------------------------------------------------------
// Blockchain implementation
func (tendermint *Tendermint) Height() int {
return tendermint.tmintNode.BlockStore().Height()
}
func (tendermint *Tendermint) BlockMeta(height int) *tendermint_types.BlockMeta {
return tendermint.tmintNode.BlockStore().LoadBlockMeta(height)
}
func (tendermint *Tendermint) Block(height int) *tendermint_types.Block {
return tendermint.tmintNode.BlockStore().LoadBlock(height)
}
func (tendermint *Tendermint) ChainId() string {
return tendermint.chainId
}
// Consensus implementation
func (tendermint *Tendermint) IsListening() bool {
return tendermint.tmintNode.Switch().IsListening()
}
func (tendermint *Tendermint) Listeners() []p2p.Listener {
var copyListeners []p2p.Listener
// copy slice of Listeners
copy(copyListeners[:], tendermint.tmintNode.Switch().Listeners())
return copyListeners
}
func (tendermint *Tendermint) Peers() []*consensus_types.Peer {
p2pPeers := tendermint.tmintNode.Switch().Peers().List()
peers := make([]*consensus_types.Peer, 0)
for _, peer := range p2pPeers {
peers = append(peers, &consensus_types.Peer{
NodeInfo: peer.NodeInfo,
IsOutbound: peer.IsOutbound(),
})
}
return peers
}
func (tendermint *Tendermint) NodeInfo() *p2p.NodeInfo {
var copyNodeInfo = new(p2p.NodeInfo)
// call Switch().NodeInfo is not go-routine safe, so copy
*copyNodeInfo = *tendermint.tmintNode.Switch().NodeInfo()
tendermint.tmintNode.ConsensusState().GetRoundState()
return copyNodeInfo
}
func (tendermint *Tendermint) PublicValidatorKey() crypto.PubKey {
// TODO: [ben] this is abetment, not yet a go-routine safe solution
var copyPublicValidatorKey crypto.PubKey
// crypto.PubKey is an interface so copy underlying struct
publicKey := tendermint.tmintNode.PrivValidator().PubKey
switch publicKey.(type) {
case crypto.PubKeyEd25519:
// type crypto.PubKeyEd25519 is [32]byte
copyKeyBytes := publicKey.(crypto.PubKeyEd25519)
copyPublicValidatorKey = crypto.PubKey(copyKeyBytes)
default:
// TODO: [ben] add error return to all these calls
copyPublicValidatorKey = nil
}
return copyPublicValidatorKey
}
func (tendermint *Tendermint) Events() edb_event.EventEmitter {
return edb_event.NewEvents(tendermint.tmintNode.EventSwitch(), tendermint.logger)
}
func (tendermint *Tendermint) BroadcastTransaction(transaction []byte,
callback func(*abci_types.Response)) error {
return tendermint.tmintNode.MempoolReactor().BroadcastTx(transaction, callback)
}
func (tendermint *Tendermint) ListUnconfirmedTxs(
maxTxs int) ([]txs.Tx, error) {
tendermintTxs := tendermint.tmintNode.MempoolReactor().Mempool.Reap(maxTxs)
transactions := make([]txs.Tx, len(tendermintTxs))
for i, txBytes := range tendermintTxs {
tx, err := txs.DecodeTx(txBytes)
if err != nil {
return nil, err
}
transactions[i] = tx
}
return transactions, nil
}
func (tendermint *Tendermint) ListValidators() []consensus_types.Validator {
return consensus_types.FromTendermintValidators(tendermint.tmintNode.
ConsensusState().Validators.Validators)
}
func (tendermint *Tendermint) ConsensusState() *consensus_types.ConsensusState {
return consensus_types.FromRoundState(tendermint.tmintNode.ConsensusState().
GetRoundState())
}
func (tendermint *Tendermint) PeerConsensusStates() map[string]string {
peers := tendermint.tmintNode.Switch().Peers().List()
peerConsensusStates := make(map[string]string,
len(peers))
for _, peer := range peers {
peerState := peer.Data.Get(tendermint_types.PeerStateKey).(*tendermint_consensus.PeerState)
peerRoundState := peerState.GetRoundState()
// TODO: implement a proper mapping, this is a nasty way of marshalling
// to JSON
peerConsensusStates[peer.Key] = string(wire.JSONBytes(peerRoundState))
}
return peerConsensusStates
}
//------------------------------------------------------------------------------
// Helper functions
// func marshalConfigToDisk(filePath string, tendermintConfig *viper.Viper) error {
//
// tendermintConfig.Unmarshal
// // marshal interface to toml bytes
// bytesConfig, err := toml.Marshal(tendermintConfig)
// if err != nil {
// return fmt.Fatalf("Failed to marshal Tendermint configuration to bytes: %v",
// err)
// }
// return files.WriteAndBackup(filePath, bytesConfig)
// }