/
tm_node.go
165 lines (138 loc) · 4.28 KB
/
tm_node.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
// Copyright (C) 2023 Gobalsky Labs Limited
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package abci
import (
"fmt"
"code.vegaprotocol.io/vega/core/blockchain"
"code.vegaprotocol.io/vega/logging"
"github.com/cometbft/cometbft/abci/types"
"github.com/cometbft/cometbft/config"
bftconfig "github.com/cometbft/cometbft/config"
"github.com/cometbft/cometbft/libs/service"
nm "github.com/cometbft/cometbft/node"
"github.com/cometbft/cometbft/p2p"
"github.com/cometbft/cometbft/privval"
"github.com/cometbft/cometbft/proxy"
tmtypes "github.com/cometbft/cometbft/types"
"github.com/spf13/viper"
)
type TmNode struct {
conf blockchain.Config
node service.Service
MempoolSize int64
}
const namedLogger = "tendermint"
func NewTmNode(
conf blockchain.Config,
log *logging.Logger,
homeDir string,
app types.Application,
genesisDoc *tmtypes.GenesisDoc,
) (*TmNode, error) {
log = log.Named(namedLogger)
log.SetLevel(conf.Tendermint.Level.Get())
config, err := loadConfig(homeDir)
if err != nil {
return nil, err
}
viper.SetConfigFile(fmt.Sprintf("%s/%s", homeDir, "config/config.toml"))
if err := viper.ReadInConfig(); err != nil {
return nil, fmt.Errorf("reading tendermint config: %v", err)
}
if err := viper.Unmarshal(config); err != nil {
return nil, fmt.Errorf("decoding tendermint config: %v", err)
}
overwriteConfig(config)
if err := config.ValidateBasic(); err != nil {
return nil, fmt.Errorf("invalid tendermint configuration data: %v", err)
}
if genesisDoc == nil {
genesisDoc, err = tmtypes.GenesisDocFromFile(config.GenesisFile())
if err != nil {
return nil, fmt.Errorf("loading tendermint genesis document: %v", err)
}
}
genesisDocProvider := func() (*tmtypes.GenesisDoc, error) {
return genesisDoc, nil
}
// read private validator
pv := privval.LoadFilePV(
config.PrivValidatorKeyFile(),
config.PrivValidatorStateFile(),
)
// read node key
nodeKey, err := p2p.LoadNodeKey(config.NodeKeyFile())
if err != nil {
return nil, fmt.Errorf("failed to load node's key: %w", err)
}
// create logger
logger := &TmLogger{log.ToSugared()}
// create node
node, err := nm.NewNode(
config,
pv,
nodeKey,
proxy.NewLocalClientCreator(app),
genesisDocProvider,
bftconfig.DefaultDBProvider,
nm.DefaultMetricsProvider(config.Instrumentation),
logger)
if err != nil {
return nil, fmt.Errorf("failed to create new Tendermint node: %w", err)
}
// acc := abciclient.NewLocalCreator(app)
// node, err := nm.New(config, logger, acc, genesisDoc)
// if err != nil {
// return nil, fmt.Errorf("creating tendermint node: %v", err)
// }
return &TmNode{conf, node, config.Mempool.MaxTxsBytes}, nil
}
func (*TmNode) ReloadConf(cfg blockchain.Config) {
}
func (t *TmNode) GetClient() (*LocalClient, error) {
return newLocalClient(t.node)
}
func (t *TmNode) Start() error {
return t.node.Start()
}
func (t *TmNode) Stop() error {
if t.node != nil && t.node.IsRunning() {
if err := t.node.Stop(); err != nil {
return err
}
}
<-t.node.Quit()
return nil
}
func loadConfig(homeDir string) (*config.Config, error) {
conf := config.DefaultConfig()
if err := viper.Unmarshal(conf); err != nil {
return nil, err
}
conf.SetRoot(homeDir)
if err := conf.ValidateBasic(); err != nil {
return nil, fmt.Errorf("error in config file: %w", err)
}
return conf, nil
}
// we want to force validators to skip timeout on commit so they don't wait after consensus has been reached.
func overwriteConfig(config *config.Config) {
config.Consensus.TimeoutCommit = 0
config.Consensus.CreateEmptyBlocks = true
// ensure rechecking tx is enabled
config.Mempool.Recheck = true
// enforce compatibility
config.P2P.MaxPacketMsgPayloadSize = 16384
}