Skip to content

Commit bf6edc0

Browse files
committedJan 24, 2024
implement prototype for compact blocks

22 files changed

+802
-136
lines changed
 

‎config/config.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -692,9 +692,7 @@ func DefaultFuzzConnConfig() *FuzzConnConfig {
692692
// MempoolConfig defines the configuration options for the CometBFT mempool
693693
type MempoolConfig struct {
694694
// Mempool version to use:
695-
// 1) "v0" - FIFO mempool.
696-
// 2) "v1" - (default) prioritized mempool.
697-
// 3) "v2" - content addressable transaction pool
695+
// 1) "v2" - (default) content addressable transaction pool
698696
Version string `mapstructure:"version"`
699697
// RootDir is the root directory for all data. This should be configured via
700698
// the $CMTHOME env variable or --home cmd flag rather than overriding this
@@ -763,7 +761,7 @@ type MempoolConfig struct {
763761
// DefaultMempoolConfig returns a default configuration for the CometBFT mempool
764762
func DefaultMempoolConfig() *MempoolConfig {
765763
return &MempoolConfig{
766-
Version: MempoolV1,
764+
Version: MempoolV2,
767765
Recheck: true,
768766
Broadcast: true,
769767
WalPath: "",
@@ -798,6 +796,9 @@ func (cfg *MempoolConfig) WalEnabled() bool {
798796
// ValidateBasic performs basic validation (checking param bounds, etc.) and
799797
// returns an error if any check fails.
800798
func (cfg *MempoolConfig) ValidateBasic() error {
799+
if cfg.Version != MempoolV2 {
800+
return errors.New("only v2 mempool is supported for compact blocks")
801+
}
801802
if cfg.Size < 0 {
802803
return errors.New("size can't be negative")
803804
}

‎config/toml.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -343,9 +343,7 @@ dial_timeout = "{{ .P2P.DialTimeout }}"
343343
[mempool]
344344
345345
# Mempool version to use:
346-
# 1) "v0" - FIFO mempool.
347-
# 2) "v1" - (default) prioritized mempool.
348-
# 3) "v2" - content addressable transaction pool
346+
# 1) "v2" - (default) content addressable transaction pool
349347
version = "{{ .Mempool.Version }}"
350348
351349
# Recheck (default: true) defines whether CometBFT should recheck the

‎consensus/byzantine_test.go

+25-36
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,8 @@ import (
2020
"github.com/tendermint/tendermint/libs/log"
2121
"github.com/tendermint/tendermint/libs/service"
2222
cmtsync "github.com/tendermint/tendermint/libs/sync"
23-
mempl "github.com/tendermint/tendermint/mempool"
2423

25-
cfg "github.com/tendermint/tendermint/config"
26-
mempoolv2 "github.com/tendermint/tendermint/mempool/cat"
27-
mempoolv0 "github.com/tendermint/tendermint/mempool/v0"
28-
mempoolv1 "github.com/tendermint/tendermint/mempool/v1"
24+
"github.com/tendermint/tendermint/mempool/cat"
2925
"github.com/tendermint/tendermint/p2p"
3026
cmtcons "github.com/tendermint/tendermint/proto/tendermint/consensus"
3127
cmtproto "github.com/tendermint/tendermint/proto/tendermint/types"
@@ -48,6 +44,7 @@ func TestByzantinePrevoteEquivocation(t *testing.T) {
4844

4945
genDoc, privVals := randGenesisDoc(nValidators, false, 30)
5046
css := make([]*State, nValidators)
47+
catReactors := make([]*cat.Reactor, nValidators)
5148

5249
for i := 0; i < nValidators; i++ {
5350
logger := consensusLogger().With("test", "byzantine", "validator", i)
@@ -72,33 +69,21 @@ func TestByzantinePrevoteEquivocation(t *testing.T) {
7269
proxyAppConnConMem := abcicli.NewLocalClient(mtx, app)
7370

7471
// Make Mempool
75-
var mempool mempl.Mempool
76-
77-
switch thisConfig.Mempool.Version {
78-
case cfg.MempoolV0:
79-
mempool = mempoolv0.NewCListMempool(config.Mempool,
80-
proxyAppConnConMem,
81-
state.LastBlockHeight,
82-
mempoolv0.WithPreCheck(sm.TxPreCheck(state)),
83-
mempoolv0.WithPostCheck(sm.TxPostCheck(state)))
84-
case cfg.MempoolV1:
85-
mempool = mempoolv1.NewTxMempool(logger,
86-
config.Mempool,
87-
proxyAppConnConMem,
88-
state.LastBlockHeight,
89-
mempoolv1.WithPreCheck(sm.TxPreCheck(state)),
90-
mempoolv1.WithPostCheck(sm.TxPostCheck(state)),
91-
)
92-
case cfg.MempoolV2:
93-
mempool = mempoolv2.NewTxPool(
94-
logger,
95-
config.Mempool,
96-
proxyAppConnConMem,
97-
state.LastBlockHeight,
98-
mempoolv2.WithPreCheck(sm.TxPreCheck(state)),
99-
mempoolv2.WithPostCheck(sm.TxPostCheck(state)),
100-
)
101-
}
72+
mempool := cat.NewTxPool(
73+
logger,
74+
config.Mempool,
75+
proxyAppConnConMem,
76+
state.LastBlockHeight,
77+
cat.WithPreCheck(sm.TxPreCheck(state)),
78+
cat.WithPostCheck(sm.TxPostCheck(state)),
79+
)
80+
var err error
81+
catReactors[i], err = cat.NewReactor(mempool, &cat.ReactorOptions{
82+
ListenOnly: !config.Mempool.Broadcast,
83+
MaxTxSize: config.Mempool.MaxTxBytes,
84+
MaxGossipDelay: config.Mempool.MaxGossipDelay,
85+
})
86+
require.NoError(t, err)
10287

10388
if thisConfig.Consensus.WaitForTxs() {
10489
mempool.EnableTxsAvailable()
@@ -112,7 +97,7 @@ func TestByzantinePrevoteEquivocation(t *testing.T) {
11297

11398
// Make State
11499
blockExec := sm.NewBlockExecutor(stateStore, log.TestingLogger(), proxyAppConnCon, mempool, evpool)
115-
cs := NewState(thisConfig.Consensus, state, blockExec, blockStore, mempool, evpool)
100+
cs := NewState(thisConfig.Consensus, state, blockExec, blockStore, mempool, catReactors[i], evpool)
116101
cs.SetLogger(cs.Logger)
117102
// set private validator
118103
pv := privVals[i]
@@ -154,6 +139,7 @@ func TestByzantinePrevoteEquivocation(t *testing.T) {
154139
// make connected switches and start all reactors
155140
p2p.MakeConnectedSwitches(config.P2P, nValidators, func(i int, s *p2p.Switch) *p2p.Switch {
156141
s.AddReactor("CONSENSUS", reactors[i])
142+
s.AddReactor("MEMPOOL", catReactors[i])
157143
s.SetLogger(reactors[i].conS.Logger.With("module", "p2p"))
158144
return s
159145
}, p2p.Connect2Switches)
@@ -230,9 +216,10 @@ func TestByzantinePrevoteEquivocation(t *testing.T) {
230216
}
231217
proposerAddr := lazyProposer.privValidatorPubKey.Address()
232218

233-
block, blockParts := lazyProposer.blockExec.CreateProposalBlock(
219+
block := lazyProposer.blockExec.CreateProposalBlock(
234220
lazyProposer.Height, lazyProposer.state, commit, proposerAddr,
235221
)
222+
blockParts := block.MakePartSet(types.BlockPartSizeBytes)
236223

237224
// Flush the WAL. Otherwise, we may not recompute the same proposal to sign,
238225
// and the privValidator will refuse to sign anything.
@@ -480,7 +467,8 @@ func byzantineDecideProposalFunc(t *testing.T, height int64, round int32, cs *St
480467
// Avoid sending on internalMsgQueue and running consensus state.
481468

482469
// Create a new proposal block from state/txs from the mempool.
483-
block1, blockParts1 := cs.createProposalBlock()
470+
block1 := cs.createProposalBlock()
471+
blockParts1 := block1.MakePartSet(types.BlockPartSizeBytes)
484472
polRound := cs.TwoThirdPrevoteRound
485473
propBlockID := types.BlockID{Hash: block1.Hash(), PartSetHeader: blockParts1.Header()}
486474
proposal1 := types.NewProposal(height, round, polRound, propBlockID)
@@ -495,7 +483,8 @@ func byzantineDecideProposalFunc(t *testing.T, height int64, round int32, cs *St
495483
deliverTxsRange(cs, 0, 1)
496484

497485
// Create a new proposal block from state/txs from the mempool.
498-
block2, blockParts2 := cs.createProposalBlock()
486+
block2 := cs.createProposalBlock()
487+
blockParts2 := block2.MakePartSet(types.BlockPartSizeBytes)
499488
polRound = cs.TwoThirdPrevoteRound
500489
propBlockID = types.BlockID{Hash: block2.Hash(), PartSetHeader: blockParts2.Header()}
501490
proposal2 := types.NewProposal(height, round, polRound, propBlockID)

‎consensus/common_test.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,9 @@ func decideProposal(
207207
round int32,
208208
) (proposal *types.Proposal, block *types.Block) {
209209
cs1.mtx.Lock()
210-
block, blockParts := cs1.createProposalBlock()
210+
block = cs1.createProposalBlock()
211+
blockParts := block.MakePartSet(types.BlockPartSizeBytes)
212+
211213
validRound := cs1.TwoThirdPrevoteRound
212214
chainID := cs1.state.ChainID
213215
cs1.mtx.Unlock()
@@ -447,7 +449,7 @@ func newStateWithConfigAndBlockStore(
447449
}
448450

449451
blockExec := sm.NewBlockExecutor(stateStore, log.TestingLogger(), proxyAppConnCon, mempool, evpool)
450-
cs := NewState(thisConfig.Consensus, state, blockExec, blockStore, mempool, evpool)
452+
cs := NewState(thisConfig.Consensus, state, blockExec, blockStore, mempool, nil, evpool)
451453
cs.SetLogger(log.TestingLogger().With("module", "consensus"))
452454
cs.SetPrivValidator(pv)
453455

‎consensus/metrics.go

+50
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
package consensus
22

33
import (
4+
"encoding/json"
5+
"fmt"
6+
"path/filepath"
47
"strings"
58
"time"
69

710
"github.com/go-kit/kit/metrics"
811
"github.com/go-kit/kit/metrics/discard"
912
cstypes "github.com/tendermint/tendermint/consensus/types"
13+
"github.com/tendermint/tendermint/libs/os"
1014

1115
prometheus "github.com/go-kit/kit/metrics/prometheus"
1216
stdprometheus "github.com/prometheus/client_golang/prometheus"
@@ -299,3 +303,49 @@ func (m *Metrics) MarkStep(s cstypes.RoundStepType) {
299303
}
300304
m.stepStart = time.Now()
301305
}
306+
307+
type JSONMetrics struct {
308+
dir string
309+
interval int
310+
StartTime time.Time
311+
EndTime time.Time
312+
Blocks uint64
313+
Rounds uint64
314+
SentConsensusBytes uint64
315+
SentCompactBlocks uint64
316+
SentCompactBytes uint64
317+
CompactBlockFailures uint64
318+
SentBlockParts uint64
319+
SentBlockPartsBytes uint64
320+
}
321+
322+
func NewJSONMetrics(dir string) *JSONMetrics {
323+
return &JSONMetrics{
324+
dir: dir,
325+
StartTime: time.Now().UTC(),
326+
}
327+
}
328+
329+
func (m *JSONMetrics) Save() {
330+
m.EndTime = time.Now().UTC()
331+
content, err := json.MarshalIndent(m, "", " ")
332+
if err != nil {
333+
panic(err)
334+
}
335+
path := filepath.Join(m.dir, fmt.Sprintf("metrics_%d.json", m.interval))
336+
os.MustWriteFile(path, content, 0644)
337+
m.StartTime = m.EndTime
338+
m.interval++
339+
m.reset()
340+
}
341+
342+
func (m *JSONMetrics) reset() {
343+
m.Blocks = 0
344+
m.Rounds = 0
345+
m.SentConsensusBytes = 0
346+
m.SentBlockParts = 0
347+
m.SentBlockPartsBytes = 0
348+
m.SentCompactBlocks = 0
349+
m.SentCompactBytes = 0
350+
m.CompactBlockFailures = 0
351+
}

‎consensus/reactor_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ func TestReactorWithEvidence(t *testing.T) {
215215

216216
// Make State
217217
blockExec := sm.NewBlockExecutor(stateStore, log.TestingLogger(), proxyAppConnCon, mempool, evpool)
218-
cs := NewState(thisConfig.Consensus, state, blockExec, blockStore, mempool, evpool2)
218+
cs := NewState(thisConfig.Consensus, state, blockExec, blockStore, mempool, nil, evpool2)
219219
cs.SetLogger(log.TestingLogger().With("module", "consensus"))
220220
cs.SetPrivValidator(pv)
221221

‎consensus/replay_file.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ func (pb *playback) replayReset(count int, newStepSub types.Subscription) error
130130
pb.cs.Wait()
131131

132132
newCS := NewState(pb.cs.config, pb.genesisState.Copy(), pb.cs.blockExec,
133-
pb.cs.blockStore, pb.cs.txNotifier, pb.cs.evpool)
133+
pb.cs.blockStore, pb.cs.txNotifier, pb.cs.txFetcher, pb.cs.evpool)
134134
newCS.SetEventBus(pb.cs.eventBus)
135135
newCS.startForReplay()
136136

@@ -333,7 +333,7 @@ func newConsensusStateForReplay(config cfg.BaseConfig, csConfig *cfg.ConsensusCo
333333
blockExec := sm.NewBlockExecutor(stateStore, log.TestingLogger(), proxyApp.Consensus(), mempool, evpool)
334334

335335
consensusState := NewState(csConfig, state.Copy(), blockExec,
336-
blockStore, mempool, evpool)
336+
blockStore, mempool, nil, evpool)
337337

338338
consensusState.SetEventBus(eventBus)
339339
return consensusState

‎consensus/replay_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ func TestSimulateValidatorsChange(t *testing.T) {
373373
newValidatorTx1 := kvstore.MakeValSetChangeTx(valPubKey1ABCI, testMinPower)
374374
err = assertMempool(css[0].txNotifier).CheckTx(newValidatorTx1, nil, mempl.TxInfo{})
375375
assert.Nil(t, err)
376-
propBlock, _ := css[0].createProposalBlock() // changeProposer(t, cs1, vs2)
376+
propBlock := css[0].createProposalBlock() // changeProposer(t, cs1, vs2)
377377
propBlockParts := propBlock.MakePartSet(partSize)
378378
blockID := types.BlockID{Hash: propBlock.Hash(), PartSetHeader: propBlockParts.Header()}
379379

@@ -403,7 +403,7 @@ func TestSimulateValidatorsChange(t *testing.T) {
403403
updateValidatorTx1 := kvstore.MakeValSetChangeTx(updatePubKey1ABCI, 25)
404404
err = assertMempool(css[0].txNotifier).CheckTx(updateValidatorTx1, nil, mempl.TxInfo{})
405405
assert.Nil(t, err)
406-
propBlock, _ = css[0].createProposalBlock() // changeProposer(t, cs1, vs2)
406+
propBlock = css[0].createProposalBlock() // changeProposer(t, cs1, vs2)
407407
propBlockParts = propBlock.MakePartSet(partSize)
408408
blockID = types.BlockID{Hash: propBlock.Hash(), PartSetHeader: propBlockParts.Header()}
409409

@@ -440,7 +440,7 @@ func TestSimulateValidatorsChange(t *testing.T) {
440440
newValidatorTx3 := kvstore.MakeValSetChangeTx(newVal3ABCI, testMinPower)
441441
err = assertMempool(css[0].txNotifier).CheckTx(newValidatorTx3, nil, mempl.TxInfo{})
442442
assert.Nil(t, err)
443-
propBlock, _ = css[0].createProposalBlock() // changeProposer(t, cs1, vs2)
443+
propBlock = css[0].createProposalBlock() // changeProposer(t, cs1, vs2)
444444
propBlockParts = propBlock.MakePartSet(partSize)
445445
blockID = types.BlockID{Hash: propBlock.Hash(), PartSetHeader: propBlockParts.Header()}
446446
newVss := make([]*validatorStub, nVals+1)
@@ -515,7 +515,7 @@ func TestSimulateValidatorsChange(t *testing.T) {
515515
removeValidatorTx3 := kvstore.MakeValSetChangeTx(newVal3ABCI, 0)
516516
err = assertMempool(css[0].txNotifier).CheckTx(removeValidatorTx3, nil, mempl.TxInfo{})
517517
assert.Nil(t, err)
518-
propBlock, _ = css[0].createProposalBlock() // changeProposer(t, cs1, vs2)
518+
propBlock = css[0].createProposalBlock() // changeProposer(t, cs1, vs2)
519519
propBlockParts = propBlock.MakePartSet(partSize)
520520
blockID = types.BlockID{Hash: propBlock.Hash(), PartSetHeader: propBlockParts.Header()}
521521
newVss = make([]*validatorStub, nVals+3)

0 commit comments

Comments
 (0)
Please sign in to comment.