This repository has been archived by the owner on May 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 346
/
tendermint.go
151 lines (132 loc) · 4.17 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
package tendermint
import (
"encoding/json"
"io/ioutil"
"os"
"path"
"github.com/tendermint/tendermint/version"
"github.com/hyperledger/burrow/binary"
"github.com/hyperledger/burrow/consensus/abci"
"github.com/hyperledger/burrow/crypto"
"github.com/hyperledger/burrow/genesis"
"github.com/hyperledger/burrow/logging"
"github.com/hyperledger/burrow/logging/structure"
"github.com/tendermint/tendermint/config"
"github.com/tendermint/tendermint/crypto/ed25519"
"github.com/tendermint/tendermint/node"
"github.com/tendermint/tendermint/p2p"
"github.com/tendermint/tendermint/proxy"
tmTypes "github.com/tendermint/tendermint/types"
dbm "github.com/tendermint/tm-db"
)
func init() {
// Tendermint now sets this dynamically in it's build... we could also automate setting it
version.TMCoreSemVer = "0.34.3"
}
// Serves as a wrapper around the Tendermint node's closeable resources (database connections)
type Node struct {
*node.Node
closers []interface {
Close() error
}
}
func DBProvider(ID string, backendType dbm.BackendType, dbDir string) (dbm.DB, error) {
return dbm.NewDB(ID, backendType, dbDir)
}
// Since Tendermint doesn't close its DB connections
func (n *Node) DBProvider(ctx *node.DBContext) (dbm.DB, error) {
db, err := DBProvider(ctx.ID, dbm.BackendType(ctx.Config.DBBackend), ctx.Config.DBDir())
if err != nil {
return nil, err
}
n.closers = append(n.closers, db)
return db, nil
}
func (n *Node) Close() {
for _, closer := range n.closers {
closer.Close()
}
}
func NewNode(conf *config.Config, privValidator tmTypes.PrivValidator, genesisDoc *tmTypes.GenesisDoc,
app *abci.App, metricsProvider node.MetricsProvider, logger *logging.Logger, options ...node.Option) (*Node, error) {
var err error
// disable Tendermint's RPC
conf.RPC.ListenAddress = ""
nodeKey, err := EnsureNodeKey(conf.NodeKeyFile())
if err != nil {
return nil, err
}
nde := &Node{}
nde.Node, err = node.NewNode(conf, privValidator,
nodeKey, proxy.NewLocalClientCreator(app),
func() (*tmTypes.GenesisDoc, error) {
return genesisDoc, nil
},
nde.DBProvider,
metricsProvider,
NewLogger(logger.WithPrefix(structure.ComponentKey, structure.Tendermint).
With(structure.ScopeKey, "tendermint.NewNode")),
options...)
if err != nil {
return nil, err
}
app.SetMempoolLocker(nde.Mempool())
return nde, nil
}
func DeriveGenesisDoc(burrowGenesisDoc *genesis.GenesisDoc, appHash []byte) *tmTypes.GenesisDoc {
validators := make([]tmTypes.GenesisValidator, len(burrowGenesisDoc.Validators))
for i, validator := range burrowGenesisDoc.Validators {
validators[i] = tmTypes.GenesisValidator{
Address: validator.PublicKey.TendermintAddress(),
PubKey: validator.PublicKey.TendermintPubKey(),
Name: validator.Name,
Power: int64(validator.Amount),
}
}
consensusParams := tmTypes.DefaultConsensusParams()
// This is the smallest increment we can use to get a strictly increasing sequence
// of block time - we set it low to avoid skew
// if the BlockTimeIota is longer than the average block time
consensusParams.Block.TimeIotaMs = 1
return &tmTypes.GenesisDoc{
ChainID: burrowGenesisDoc.GetChainID(),
GenesisTime: burrowGenesisDoc.GenesisTime,
Validators: validators,
AppHash: appHash,
ConsensusParams: consensusParams,
InitialHeight: 1,
}
}
func NewNodeInfo(ni p2p.DefaultNodeInfo) *NodeInfo {
address, _ := crypto.AddressFromHexString(string(ni.ID()))
return &NodeInfo{
ID: address,
Moniker: ni.Moniker,
ListenAddress: ni.ListenAddr,
Version: ni.Version,
Channels: binary.HexBytes(ni.Channels),
Network: ni.Network,
RPCAddress: ni.Other.RPCAddress,
TxIndex: ni.Other.TxIndex,
}
}
func NewNodeKey() *p2p.NodeKey {
privKey := ed25519.GenPrivKey()
return &p2p.NodeKey{
PrivKey: privKey,
}
}
func WriteNodeKey(nodeKeyFile string, key json.RawMessage) error {
err := os.MkdirAll(path.Dir(nodeKeyFile), 0777)
if err != nil {
return err
}
return ioutil.WriteFile(nodeKeyFile, key, 0600)
}
func EnsureNodeKey(nodeKeyFile string) (*p2p.NodeKey, error) {
err := os.MkdirAll(path.Dir(nodeKeyFile), 0777)
if err != nil {
return nil, err
}
return p2p.LoadOrGenNodeKey(nodeKeyFile)
}