From bf6edc023364c8d9f227b20f2212d83b041ef93f Mon Sep 17 00:00:00 2001 From: Callum Waters <cmwaters19@gmail.com> Date: Sat, 20 Jan 2024 13:04:31 +1300 Subject: [PATCH 001/127] implement prototype for compact blocks --- config/config.go | 9 +- config/toml.go | 4 +- consensus/byzantine_test.go | 61 +++---- consensus/common_test.go | 6 +- consensus/metrics.go | 50 ++++++ consensus/reactor_test.go | 2 +- consensus/replay_file.go | 4 +- consensus/replay_test.go | 8 +- consensus/state.go | 76 ++++++++- consensus/state_test.go | 2 +- consensus/wal_generator.go | 2 +- go.mod | 4 +- go.sum | 32 ++++ mempool/cat/block_builder.go | 263 ++++++++++++++++++++++++++++++ mempool/cat/block_builder_test.go | 243 +++++++++++++++++++++++++++ mempool/cat/pool.go | 10 ++ mempool/cat/reactor.go | 25 +-- mempool/metrics.go | 50 ++++++ node/node.go | 74 ++------- node/node_test.go | 4 +- state/execution.go | 5 +- test/maverick/consensus/state.go | 4 +- 22 files changed, 802 insertions(+), 136 deletions(-) create mode 100644 mempool/cat/block_builder.go create mode 100644 mempool/cat/block_builder_test.go diff --git a/config/config.go b/config/config.go index 1a202715a0..e30eb83092 100644 --- a/config/config.go +++ b/config/config.go @@ -692,9 +692,7 @@ func DefaultFuzzConnConfig() *FuzzConnConfig { // MempoolConfig defines the configuration options for the CometBFT mempool type MempoolConfig struct { // Mempool version to use: - // 1) "v0" - FIFO mempool. - // 2) "v1" - (default) prioritized mempool. - // 3) "v2" - content addressable transaction pool + // 1) "v2" - (default) content addressable transaction pool Version string `mapstructure:"version"` // RootDir is the root directory for all data. This should be configured via // the $CMTHOME env variable or --home cmd flag rather than overriding this @@ -763,7 +761,7 @@ type MempoolConfig struct { // DefaultMempoolConfig returns a default configuration for the CometBFT mempool func DefaultMempoolConfig() *MempoolConfig { return &MempoolConfig{ - Version: MempoolV1, + Version: MempoolV2, Recheck: true, Broadcast: true, WalPath: "", @@ -798,6 +796,9 @@ func (cfg *MempoolConfig) WalEnabled() bool { // ValidateBasic performs basic validation (checking param bounds, etc.) and // returns an error if any check fails. func (cfg *MempoolConfig) ValidateBasic() error { + if cfg.Version != MempoolV2 { + return errors.New("only v2 mempool is supported for compact blocks") + } if cfg.Size < 0 { return errors.New("size can't be negative") } diff --git a/config/toml.go b/config/toml.go index 34c99aba7a..f88b9a13ee 100644 --- a/config/toml.go +++ b/config/toml.go @@ -343,9 +343,7 @@ dial_timeout = "{{ .P2P.DialTimeout }}" [mempool] # Mempool version to use: -# 1) "v0" - FIFO mempool. -# 2) "v1" - (default) prioritized mempool. -# 3) "v2" - content addressable transaction pool +# 1) "v2" - (default) content addressable transaction pool version = "{{ .Mempool.Version }}" # Recheck (default: true) defines whether CometBFT should recheck the diff --git a/consensus/byzantine_test.go b/consensus/byzantine_test.go index 03b94e1856..25bdc46261 100644 --- a/consensus/byzantine_test.go +++ b/consensus/byzantine_test.go @@ -20,12 +20,8 @@ import ( "github.com/tendermint/tendermint/libs/log" "github.com/tendermint/tendermint/libs/service" cmtsync "github.com/tendermint/tendermint/libs/sync" - mempl "github.com/tendermint/tendermint/mempool" - cfg "github.com/tendermint/tendermint/config" - mempoolv2 "github.com/tendermint/tendermint/mempool/cat" - mempoolv0 "github.com/tendermint/tendermint/mempool/v0" - mempoolv1 "github.com/tendermint/tendermint/mempool/v1" + "github.com/tendermint/tendermint/mempool/cat" "github.com/tendermint/tendermint/p2p" cmtcons "github.com/tendermint/tendermint/proto/tendermint/consensus" cmtproto "github.com/tendermint/tendermint/proto/tendermint/types" @@ -48,6 +44,7 @@ func TestByzantinePrevoteEquivocation(t *testing.T) { genDoc, privVals := randGenesisDoc(nValidators, false, 30) css := make([]*State, nValidators) + catReactors := make([]*cat.Reactor, nValidators) for i := 0; i < nValidators; i++ { logger := consensusLogger().With("test", "byzantine", "validator", i) @@ -72,33 +69,21 @@ func TestByzantinePrevoteEquivocation(t *testing.T) { proxyAppConnConMem := abcicli.NewLocalClient(mtx, app) // Make Mempool - var mempool mempl.Mempool - - switch thisConfig.Mempool.Version { - case cfg.MempoolV0: - mempool = mempoolv0.NewCListMempool(config.Mempool, - proxyAppConnConMem, - state.LastBlockHeight, - mempoolv0.WithPreCheck(sm.TxPreCheck(state)), - mempoolv0.WithPostCheck(sm.TxPostCheck(state))) - case cfg.MempoolV1: - mempool = mempoolv1.NewTxMempool(logger, - config.Mempool, - proxyAppConnConMem, - state.LastBlockHeight, - mempoolv1.WithPreCheck(sm.TxPreCheck(state)), - mempoolv1.WithPostCheck(sm.TxPostCheck(state)), - ) - case cfg.MempoolV2: - mempool = mempoolv2.NewTxPool( - logger, - config.Mempool, - proxyAppConnConMem, - state.LastBlockHeight, - mempoolv2.WithPreCheck(sm.TxPreCheck(state)), - mempoolv2.WithPostCheck(sm.TxPostCheck(state)), - ) - } + mempool := cat.NewTxPool( + logger, + config.Mempool, + proxyAppConnConMem, + state.LastBlockHeight, + cat.WithPreCheck(sm.TxPreCheck(state)), + cat.WithPostCheck(sm.TxPostCheck(state)), + ) + var err error + catReactors[i], err = cat.NewReactor(mempool, &cat.ReactorOptions{ + ListenOnly: !config.Mempool.Broadcast, + MaxTxSize: config.Mempool.MaxTxBytes, + MaxGossipDelay: config.Mempool.MaxGossipDelay, + }) + require.NoError(t, err) if thisConfig.Consensus.WaitForTxs() { mempool.EnableTxsAvailable() @@ -112,7 +97,7 @@ func TestByzantinePrevoteEquivocation(t *testing.T) { // Make State blockExec := sm.NewBlockExecutor(stateStore, log.TestingLogger(), proxyAppConnCon, mempool, evpool) - cs := NewState(thisConfig.Consensus, state, blockExec, blockStore, mempool, evpool) + cs := NewState(thisConfig.Consensus, state, blockExec, blockStore, mempool, catReactors[i], evpool) cs.SetLogger(cs.Logger) // set private validator pv := privVals[i] @@ -154,6 +139,7 @@ func TestByzantinePrevoteEquivocation(t *testing.T) { // make connected switches and start all reactors p2p.MakeConnectedSwitches(config.P2P, nValidators, func(i int, s *p2p.Switch) *p2p.Switch { s.AddReactor("CONSENSUS", reactors[i]) + s.AddReactor("MEMPOOL", catReactors[i]) s.SetLogger(reactors[i].conS.Logger.With("module", "p2p")) return s }, p2p.Connect2Switches) @@ -230,9 +216,10 @@ func TestByzantinePrevoteEquivocation(t *testing.T) { } proposerAddr := lazyProposer.privValidatorPubKey.Address() - block, blockParts := lazyProposer.blockExec.CreateProposalBlock( + block := lazyProposer.blockExec.CreateProposalBlock( lazyProposer.Height, lazyProposer.state, commit, proposerAddr, ) + blockParts := block.MakePartSet(types.BlockPartSizeBytes) // Flush the WAL. Otherwise, we may not recompute the same proposal to sign, // and the privValidator will refuse to sign anything. @@ -480,7 +467,8 @@ func byzantineDecideProposalFunc(t *testing.T, height int64, round int32, cs *St // Avoid sending on internalMsgQueue and running consensus state. // Create a new proposal block from state/txs from the mempool. - block1, blockParts1 := cs.createProposalBlock() + block1 := cs.createProposalBlock() + blockParts1 := block1.MakePartSet(types.BlockPartSizeBytes) polRound := cs.TwoThirdPrevoteRound propBlockID := types.BlockID{Hash: block1.Hash(), PartSetHeader: blockParts1.Header()} proposal1 := types.NewProposal(height, round, polRound, propBlockID) @@ -495,7 +483,8 @@ func byzantineDecideProposalFunc(t *testing.T, height int64, round int32, cs *St deliverTxsRange(cs, 0, 1) // Create a new proposal block from state/txs from the mempool. - block2, blockParts2 := cs.createProposalBlock() + block2 := cs.createProposalBlock() + blockParts2 := block2.MakePartSet(types.BlockPartSizeBytes) polRound = cs.TwoThirdPrevoteRound propBlockID = types.BlockID{Hash: block2.Hash(), PartSetHeader: blockParts2.Header()} proposal2 := types.NewProposal(height, round, polRound, propBlockID) diff --git a/consensus/common_test.go b/consensus/common_test.go index 511b004ea6..a489d1183f 100644 --- a/consensus/common_test.go +++ b/consensus/common_test.go @@ -207,7 +207,9 @@ func decideProposal( round int32, ) (proposal *types.Proposal, block *types.Block) { cs1.mtx.Lock() - block, blockParts := cs1.createProposalBlock() + block = cs1.createProposalBlock() + blockParts := block.MakePartSet(types.BlockPartSizeBytes) + validRound := cs1.TwoThirdPrevoteRound chainID := cs1.state.ChainID cs1.mtx.Unlock() @@ -447,7 +449,7 @@ func newStateWithConfigAndBlockStore( } blockExec := sm.NewBlockExecutor(stateStore, log.TestingLogger(), proxyAppConnCon, mempool, evpool) - cs := NewState(thisConfig.Consensus, state, blockExec, blockStore, mempool, evpool) + cs := NewState(thisConfig.Consensus, state, blockExec, blockStore, mempool, nil, evpool) cs.SetLogger(log.TestingLogger().With("module", "consensus")) cs.SetPrivValidator(pv) diff --git a/consensus/metrics.go b/consensus/metrics.go index c684c339f6..3221b565f6 100644 --- a/consensus/metrics.go +++ b/consensus/metrics.go @@ -1,12 +1,16 @@ package consensus import ( + "encoding/json" + "fmt" + "path/filepath" "strings" "time" "github.com/go-kit/kit/metrics" "github.com/go-kit/kit/metrics/discard" cstypes "github.com/tendermint/tendermint/consensus/types" + "github.com/tendermint/tendermint/libs/os" prometheus "github.com/go-kit/kit/metrics/prometheus" stdprometheus "github.com/prometheus/client_golang/prometheus" @@ -299,3 +303,49 @@ func (m *Metrics) MarkStep(s cstypes.RoundStepType) { } m.stepStart = time.Now() } + +type JSONMetrics struct { + dir string + interval int + StartTime time.Time + EndTime time.Time + Blocks uint64 + Rounds uint64 + SentConsensusBytes uint64 + SentCompactBlocks uint64 + SentCompactBytes uint64 + CompactBlockFailures uint64 + SentBlockParts uint64 + SentBlockPartsBytes uint64 +} + +func NewJSONMetrics(dir string) *JSONMetrics { + return &JSONMetrics{ + dir: dir, + StartTime: time.Now().UTC(), + } +} + +func (m *JSONMetrics) Save() { + m.EndTime = time.Now().UTC() + content, err := json.MarshalIndent(m, "", " ") + if err != nil { + panic(err) + } + path := filepath.Join(m.dir, fmt.Sprintf("metrics_%d.json", m.interval)) + os.MustWriteFile(path, content, 0644) + m.StartTime = m.EndTime + m.interval++ + m.reset() +} + +func (m *JSONMetrics) reset() { + m.Blocks = 0 + m.Rounds = 0 + m.SentConsensusBytes = 0 + m.SentBlockParts = 0 + m.SentBlockPartsBytes = 0 + m.SentCompactBlocks = 0 + m.SentCompactBytes = 0 + m.CompactBlockFailures = 0 +} diff --git a/consensus/reactor_test.go b/consensus/reactor_test.go index a82aa38859..a521353367 100644 --- a/consensus/reactor_test.go +++ b/consensus/reactor_test.go @@ -215,7 +215,7 @@ func TestReactorWithEvidence(t *testing.T) { // Make State blockExec := sm.NewBlockExecutor(stateStore, log.TestingLogger(), proxyAppConnCon, mempool, evpool) - cs := NewState(thisConfig.Consensus, state, blockExec, blockStore, mempool, evpool2) + cs := NewState(thisConfig.Consensus, state, blockExec, blockStore, mempool, nil, evpool2) cs.SetLogger(log.TestingLogger().With("module", "consensus")) cs.SetPrivValidator(pv) diff --git a/consensus/replay_file.go b/consensus/replay_file.go index 26cedffe52..f264dba4c5 100644 --- a/consensus/replay_file.go +++ b/consensus/replay_file.go @@ -130,7 +130,7 @@ func (pb *playback) replayReset(count int, newStepSub types.Subscription) error pb.cs.Wait() newCS := NewState(pb.cs.config, pb.genesisState.Copy(), pb.cs.blockExec, - pb.cs.blockStore, pb.cs.txNotifier, pb.cs.evpool) + pb.cs.blockStore, pb.cs.txNotifier, pb.cs.txFetcher, pb.cs.evpool) newCS.SetEventBus(pb.cs.eventBus) newCS.startForReplay() @@ -333,7 +333,7 @@ func newConsensusStateForReplay(config cfg.BaseConfig, csConfig *cfg.ConsensusCo blockExec := sm.NewBlockExecutor(stateStore, log.TestingLogger(), proxyApp.Consensus(), mempool, evpool) consensusState := NewState(csConfig, state.Copy(), blockExec, - blockStore, mempool, evpool) + blockStore, mempool, nil, evpool) consensusState.SetEventBus(eventBus) return consensusState diff --git a/consensus/replay_test.go b/consensus/replay_test.go index 69c36112bd..6cb99f8dd9 100644 --- a/consensus/replay_test.go +++ b/consensus/replay_test.go @@ -373,7 +373,7 @@ func TestSimulateValidatorsChange(t *testing.T) { newValidatorTx1 := kvstore.MakeValSetChangeTx(valPubKey1ABCI, testMinPower) err = assertMempool(css[0].txNotifier).CheckTx(newValidatorTx1, nil, mempl.TxInfo{}) assert.Nil(t, err) - propBlock, _ := css[0].createProposalBlock() // changeProposer(t, cs1, vs2) + propBlock := css[0].createProposalBlock() // changeProposer(t, cs1, vs2) propBlockParts := propBlock.MakePartSet(partSize) blockID := types.BlockID{Hash: propBlock.Hash(), PartSetHeader: propBlockParts.Header()} @@ -403,7 +403,7 @@ func TestSimulateValidatorsChange(t *testing.T) { updateValidatorTx1 := kvstore.MakeValSetChangeTx(updatePubKey1ABCI, 25) err = assertMempool(css[0].txNotifier).CheckTx(updateValidatorTx1, nil, mempl.TxInfo{}) assert.Nil(t, err) - propBlock, _ = css[0].createProposalBlock() // changeProposer(t, cs1, vs2) + propBlock = css[0].createProposalBlock() // changeProposer(t, cs1, vs2) propBlockParts = propBlock.MakePartSet(partSize) blockID = types.BlockID{Hash: propBlock.Hash(), PartSetHeader: propBlockParts.Header()} @@ -440,7 +440,7 @@ func TestSimulateValidatorsChange(t *testing.T) { newValidatorTx3 := kvstore.MakeValSetChangeTx(newVal3ABCI, testMinPower) err = assertMempool(css[0].txNotifier).CheckTx(newValidatorTx3, nil, mempl.TxInfo{}) assert.Nil(t, err) - propBlock, _ = css[0].createProposalBlock() // changeProposer(t, cs1, vs2) + propBlock = css[0].createProposalBlock() // changeProposer(t, cs1, vs2) propBlockParts = propBlock.MakePartSet(partSize) blockID = types.BlockID{Hash: propBlock.Hash(), PartSetHeader: propBlockParts.Header()} newVss := make([]*validatorStub, nVals+1) @@ -515,7 +515,7 @@ func TestSimulateValidatorsChange(t *testing.T) { removeValidatorTx3 := kvstore.MakeValSetChangeTx(newVal3ABCI, 0) err = assertMempool(css[0].txNotifier).CheckTx(removeValidatorTx3, nil, mempl.TxInfo{}) assert.Nil(t, err) - propBlock, _ = css[0].createProposalBlock() // changeProposer(t, cs1, vs2) + propBlock = css[0].createProposalBlock() // changeProposer(t, cs1, vs2) propBlockParts = propBlock.MakePartSet(partSize) blockID = types.BlockID{Hash: propBlock.Hash(), PartSetHeader: propBlockParts.Header()} newVss = make([]*validatorStub, nVals+3) diff --git a/consensus/state.go b/consensus/state.go index 398abc0856..efa0d63c36 100644 --- a/consensus/state.go +++ b/consensus/state.go @@ -2,10 +2,12 @@ package consensus import ( "bytes" + "context" "errors" "fmt" "io" "os" + "path/filepath" "runtime/debug" "sort" "time" @@ -67,6 +69,27 @@ type txNotifier interface { TxsAvailable() <-chan struct{} } +type TxFetcher interface { + // For constructing the compact block + FetchKeysFromTxs(context.Context, [][]byte) ([][]byte, error) + // For reconstructing the full block from the compact block + FetchTxsFromKeys(context.Context, []byte, [][]byte) ([][]byte, error) +} + +var _ TxFetcher = (*StandardTxFetcher)(nil) + +// StandardTxFetcher implements TxFetcher. Unlike the CATPool, it gossips the entire transaction set +// instead of the set of keys. This aligns with Tendermint's vanilla block propagation method +type StandardTxFetcher struct{} + +func (tf *StandardTxFetcher) FetchKeysFromTxs(_ context.Context, txs [][]byte) ([][]byte, error) { + return txs, nil +} + +func (tf *StandardTxFetcher) FetchTxsFromKeys(_ context.Context, _ []byte, txs [][]byte) ([][]byte, error) { + return txs, nil +} + // interface to the evidence pool type evidencePool interface { // reports conflicting votes to the evidence pool to be processed into evidence @@ -92,6 +115,8 @@ type State struct { // notify us if txs are available txNotifier txNotifier + // fetch txs based on tx keys. Used for compact blocks. + txFetcher TxFetcher // add evidence to the pool // when it's detected @@ -141,8 +166,8 @@ type State struct { evsw cmtevents.EventSwitch // for reporting metrics - metrics *Metrics - + metrics *Metrics + jsonMetrics *JSONMetrics traceClient *trace.Client } @@ -156,14 +181,21 @@ func NewState( blockExec *sm.BlockExecutor, blockStore sm.BlockStore, txNotifier txNotifier, + txFetcher TxFetcher, evpool evidencePool, options ...StateOption, ) *State { + path := filepath.Join(config.RootDir, "data", "consensus") + if err := cmtos.EnsureDir(path, 0700); err != nil { + panic(err) + } + cs := &State{ config: config, blockExec: blockExec, blockStore: blockStore, txNotifier: txNotifier, + txFetcher: txFetcher, peerMsgQueue: make(chan msgInfo, msgQueueSize), internalMsgQueue: make(chan msgInfo, msgQueueSize), timeoutTicker: NewTimeoutTicker(), @@ -174,9 +206,14 @@ func NewState( evpool: evpool, evsw: cmtevents.NewEventSwitch(), metrics: NopMetrics(), + jsonMetrics: NewJSONMetrics(path), traceClient: &trace.Client{}, } + if cs.txFetcher == nil { + cs.txFetcher = &StandardTxFetcher{} + } + // set function defaults (may be overwritten before calling Start) cs.decideProposal = cs.defaultDecideProposal cs.doPrevote = cs.defaultDoPrevote @@ -1156,10 +1193,19 @@ func (cs *State) defaultDecideProposal(height int64, round int32) { block, blockParts = cs.TwoThirdPrevoteBlock, cs.TwoThirdPrevoteBlockParts } else { // Create a new proposal block from state/txs from the mempool. - block, blockParts = cs.createProposalBlock() + block = cs.createProposalBlock() if block == nil { return } + + keys, err := cs.txFetcher.FetchKeysFromTxs(context.Background(), block.Txs.ToSliceOfBytes()) + if err != nil { + cs.Logger.Error("failed to fetch tx keys", "err", err) + return + } + + block.Txs = types.ToTxs(keys) + blockParts = block.MakePartSet(types.BlockPartSizeBytes) } // Flush the WAL. Otherwise, we may not recompute the same proposal to sign, @@ -1211,7 +1257,7 @@ func (cs *State) isProposalComplete() bool { // // NOTE: keep it side-effect free for clarity. // CONTRACT: cs.privValidator is not nil. -func (cs *State) createProposalBlock() (block *types.Block, blockParts *types.PartSet) { +func (cs *State) createProposalBlock() *types.Block { if cs.privValidator == nil { panic("entered createProposalBlock with privValidator being nil") } @@ -1229,14 +1275,14 @@ func (cs *State) createProposalBlock() (block *types.Block, blockParts *types.Pa default: // This shouldn't happen. cs.Logger.Error("propose step; cannot propose anything without commit for the previous block") - return + return nil } if cs.privValidatorPubKey == nil { // If this node is a validator & proposer in the current round, it will // miss the opportunity to create a block. cs.Logger.Error("propose step; empty priv validator public key", "err", errPubKeyIsNotSet) - return + return nil } proposerAddr := cs.privValidatorPubKey.Address() @@ -1949,7 +1995,25 @@ func (cs *State) addProposalBlockPart(msg *BlockPartMessage, peerID p2p.ID) (add if err != nil { return added, err } + blockHash := cs.ProposalBlockParts.Header().Hash + timeout := cs.config.Propose(round) + // Yield the lock while we fetch the transactions from the mempool so that votes + // and other operations can be processed. + cs.mtx.Unlock() + + ctx, cancel := context.WithTimeout(context.Background(), timeout) + defer cancel() + + txs, err := cs.txFetcher.FetchTxsFromKeys(ctx, blockHash, block.Data.Txs.ToSliceOfBytes()) + + cs.mtx.Lock() + if err != nil { + cs.Logger.Error("failed to fetch transactions for compact block", "err", err) + cs.jsonMetrics.CompactBlockFailures++ + return true, err + } + block.Data.Txs = types.ToTxs(txs) cs.ProposalBlock = block // NOTE: it's possible to receive complete proposal blocks for future rounds without having the proposal diff --git a/consensus/state_test.go b/consensus/state_test.go index 5e3a76c545..2202a3eaaa 100644 --- a/consensus/state_test.go +++ b/consensus/state_test.go @@ -192,7 +192,7 @@ func TestStateBadProposal(t *testing.T) { proposalCh := subscribe(cs1.eventBus, types.EventQueryCompleteProposal) voteCh := subscribe(cs1.eventBus, types.EventQueryVote) - propBlock, _ := cs1.createProposalBlock() // changeProposer(t, cs1, vs2) + propBlock := cs1.createProposalBlock() // changeProposer(t, cs1, vs2) // make the second validator the proposer by incrementing round round++ diff --git a/consensus/wal_generator.go b/consensus/wal_generator.go index f49cedf949..068ab6ad5f 100644 --- a/consensus/wal_generator.go +++ b/consensus/wal_generator.go @@ -85,7 +85,7 @@ func WALGenerateNBlocks(t *testing.T, wr io.Writer, numBlocks int) (err error) { mempool := emptyMempool{} evpool := sm.EmptyEvidencePool{} blockExec := sm.NewBlockExecutor(stateStore, log.TestingLogger(), proxyApp.Consensus(), mempool, evpool) - consensusState := NewState(config.Consensus, state.Copy(), blockExec, blockStore, mempool, evpool) + consensusState := NewState(config.Consensus, state.Copy(), blockExec, blockStore, mempool, nil, evpool) consensusState.SetLogger(logger) consensusState.SetEventBus(eventBus) if privValidator != nil { diff --git a/go.mod b/go.mod index 2c49f3b373..79a8bb3a5b 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,8 @@ module github.com/tendermint/tendermint -go 1.19 +go 1.21 + +toolchain go1.21.6 require ( github.com/BurntSushi/toml v1.2.1 diff --git a/go.sum b/go.sum index 5647ff806a..342441a98f 100644 --- a/go.sum +++ b/go.sum @@ -55,6 +55,7 @@ github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym github.com/ChainSafe/go-schnorrkel v1.0.0 h1:3aDA67lAykLaG1y3AOjs88dMxC88PgUuHRrLeDnvGIM= github.com/ChainSafe/go-schnorrkel v1.0.0/go.mod h1:dpzHYVxLZcp8pjlV+O+UR8K0Hp/z7vcchBSbMBEhCw4= github.com/DATA-DOG/go-sqlmock v1.5.0 h1:Shsta01QNfFxHCfpW6YH2STWB0MudeXXEWMr20OEh60= +github.com/DATA-DOG/go-sqlmock v1.5.0/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM= github.com/Djarvur/go-err113 v0.0.0-20210108212216-aea10b59be24 h1:sHglBQTwgx+rWPdisA5ynNEsoARbiCBOyGcJM4/OzsM= github.com/Djarvur/go-err113 v0.0.0-20210108212216-aea10b59be24/go.mod h1:4UJr5HIiMZrwgkSPdsjy2uOQExX/WEILpIrO9UPGuXs= github.com/GaijinEntertainment/go-exhaustruct/v2 v2.3.0 h1:+r1rSv4gvYn0wmRjC8X7IAzX8QezqtFV9m0MUHFJgts= @@ -75,6 +76,7 @@ github.com/OpenPeeDeeP/depguard v1.1.1/go.mod h1:JtAMzWkmFEzDPyAd+W0NHl1lvpQKTvT github.com/ProtonMail/go-crypto v0.0.0-20230217124315-7d5c6f04bbb8 h1:wPbRQzjjwFc0ih8puEVAOFGELsn1zoIIYdxvML7mDxA= github.com/ProtonMail/go-crypto v0.0.0-20230217124315-7d5c6f04bbb8/go.mod h1:I0gYDMZ6Z5GRU7l58bNFSkPTFN6Yl12dsUlAZ8xy98g= github.com/VividCortex/gohistogram v1.0.0 h1:6+hBz+qvs0JOrrNhhmR7lFxo5sINxBCGXrdtl/UvroE= +github.com/VividCortex/gohistogram v1.0.0/go.mod h1:Pf5mBqqDxYaXu3hDrrU+w6nw50o/4+TcAqDqk/vUH7g= github.com/Workiva/go-datastructures v1.0.53 h1:J6Y/52yX10Xc5JjXmGtWoSSxs3mZnGSaq37xZZh7Yig= github.com/Workiva/go-datastructures v1.0.53/go.mod h1:1yZL+zfsztete+ePzZz/Zb1/t5BnDuE2Ya2MMGhzP6A= github.com/acomagu/bufpipe v1.0.4 h1:e3H4WUzM3npvo5uv95QuJM3cQspFNtFBzvJ2oNjKIDQ= @@ -101,6 +103,7 @@ github.com/ashanbrown/forbidigo v1.5.1/go.mod h1:Y8j9jy9ZYAEHXdu723cUlraTqbzjKF1 github.com/ashanbrown/makezero v1.1.1 h1:iCQ87C0V0vSyO+M9E/FZYbu65auqH0lnsOkf5FcB28s= github.com/ashanbrown/makezero v1.1.1/go.mod h1:i1bJLCRSCHOcOa9Y6MyF2FTfMZMFdHvxKHxgO5Z1axI= github.com/benbjohnson/clock v1.3.0 h1:ip6w0uFQkncKQ979AypyG0ER7mqUSBdKLOgAle/AT8A= +github.com/benbjohnson/clock v1.3.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8= github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= @@ -153,6 +156,7 @@ github.com/celestiaorg/nmt v0.20.0/go.mod h1:Oz15Ub6YPez9uJV0heoU4WpFctxazuIhKyU github.com/cenkalti/backoff v2.2.1+incompatible h1:tNowT99t7UNflLxfYYSlKYsBpXdEet03Pg2g16Swow4= github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM= github.com/cenkalti/backoff/v4 v4.1.3 h1:cFAlzYUlVYDysBEH2T5hyJZMh3+5+WCBvSnK6Q8UtC4= +github.com/cenkalti/backoff/v4 v4.1.3/go.mod h1:scbssz8iZGpm3xbr14ovlUdkxfGXNInqkPWOWmG2CLw= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko= github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc= @@ -184,6 +188,7 @@ github.com/containerd/console v1.0.3/go.mod h1:7LqA/THxQ86k76b8c/EMSiaJ3h1eZkMkX github.com/containerd/continuity v0.3.0 h1:nisirsYROK15TAMVukJOUyGJjz4BNQJBVsNvAXZJ/eg= github.com/containerd/continuity v0.3.0/go.mod h1:wJEAIwKOm/pBZuBd0JmeTvnLquTB1Ag8espWhkykbPM= github.com/containerd/stargz-snapshotter/estargz v0.12.1 h1:+7nYmHJb0tEkcRaAW+MHqoKaJYZmkikupxCqVtmPuY0= +github.com/containerd/stargz-snapshotter/estargz v0.12.1/go.mod h1:12VUuCq3qPq4y8yUW+l5w3+oXV3cx2Po3KSe/SmPGqw= github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE= github.com/coreos/go-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8NzMklzPG4d5KIOhIy30Tk= github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk= @@ -199,6 +204,7 @@ github.com/creachadair/taskgroup v0.3.2 h1:zlfutDS+5XG40AOxcHDSThxKzns8Tnr9jnr6V github.com/creachadair/taskgroup v0.3.2/go.mod h1:wieWwecHVzsidg2CsUnFinW1faVN4+kq+TDlRJQ0Wbk= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/creack/pty v1.1.18 h1:n56/Zwd5o6whRC5PMGretI4IdRLlmBXYNjScPaBgsbY= +github.com/creack/pty v1.1.18/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4= github.com/curioswitch/go-reassign v0.2.0 h1:G9UZyOcpk/d7Gd6mqYgd8XYWFMw/znxwGDUstnC9DIo= github.com/curioswitch/go-reassign v0.2.0/go.mod h1:x6OpXuWvgfQaMGks2BZybTngWjT84hqJfKoO8Tt/Roc= github.com/cyberdelia/templates v0.0.0-20141128023046-ca7fffd4298c/go.mod h1:GyV+0YP4qX0UQ7r2MoYZ+AvYDp12OF5yg4q8rGnyNh4= @@ -219,6 +225,7 @@ github.com/deepmap/oapi-codegen v1.8.2/go.mod h1:YLgSKSDv/bZQB7N4ws6luhozi3cEdRk github.com/denis-tingaikin/go-header v0.4.3 h1:tEaZKAlqql6SKCY++utLmkPLd6K8IBM20Ha7UVm+mtU= github.com/denis-tingaikin/go-header v0.4.3/go.mod h1:0wOCWuN71D5qIgE2nz9KrKmuYBAC2Mra5RassOIQ2/c= github.com/denisenkom/go-mssqldb v0.12.0 h1:VtrkII767ttSPNRfFekePK3sctr+joXgO58stqQbtUA= +github.com/denisenkom/go-mssqldb v0.12.0/go.mod h1:iiK0YP1ZeepvmBQk/QpLEhhTNJgfzrpArPY/aFvc9yU= github.com/dgraph-io/badger/v2 v2.2007.4 h1:TRWBQg8UrlUhaFdco01nO2uXwzKS7zd+HVdwV/GHc4o= github.com/dgraph-io/badger/v2 v2.2007.4/go.mod h1:vSw/ax2qojzbN6eXHIx6KPKtCSHJN/Uz0X0VPruTIhk= github.com/dgraph-io/ristretto v0.0.3-0.20200630154024-f66de99634de/go.mod h1:KPxhHT9ZxKefz+PCeOGsrHpl1qZ7i70dGTu2u+Ahh6E= @@ -274,6 +281,7 @@ github.com/fortytw2/leaktest v1.3.0 h1:u8491cBMTQ8ft8aeV+adlcytMZylmA5nnwwkRZjI8 github.com/fortytw2/leaktest v1.3.0/go.mod h1:jDsjWgpAGjm2CA7WthBh/CdZYEPF31XHquHwclZch5g= github.com/frankban/quicktest v1.11.3/go.mod h1:wRf/ReqHper53s+kmmSZizM8NamnL3IM0I9ntUbOk+k= github.com/frankban/quicktest v1.14.3 h1:FJKSZTDHjyhriyC81FLQ0LY93eSai0ZyR/ZIkd3ZUKE= +github.com/frankban/quicktest v1.14.3/go.mod h1:mgiwOwqx65TmIk1wJ6Q7wvnVMocbUorkibMOrVTHZps= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY= @@ -321,6 +329,7 @@ github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre github.com/go-openapi/jsonpointer v0.19.5/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34VGT37dQOVbmoatg= github.com/go-openapi/swag v0.19.5/go.mod h1:POnQmlKehdgb5mhVOsnJFsivZCEZ/vjK9gh66Z9tfKk= github.com/go-sql-driver/mysql v1.7.0 h1:ueSltNNllEqE3qcWBTD0iQd3IpL/6U+mJxLkazJ7YPc= +github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= github.com/go-toolsmith/astcast v1.1.0 h1:+JN9xZV1A+Re+95pgnMgDboWNVnIMMQXwfBwLRPgSC8= github.com/go-toolsmith/astcast v1.1.0/go.mod h1:qdcuFWeGGS2xX5bLM/c3U9lewg7+Zu4mr+xPwZIB4ZU= @@ -334,6 +343,7 @@ github.com/go-toolsmith/astfmt v1.1.0/go.mod h1:OrcLlRwu0CuiIBp/8b5PYF9ktGVZUjlN github.com/go-toolsmith/astp v1.1.0 h1:dXPuCl6u2llURjdPLLDxJeZInAeZ0/eZwFJmqZMnpQA= github.com/go-toolsmith/astp v1.1.0/go.mod h1:0T1xFGz9hicKs8Z5MfAqSUitoUYS30pDMsRVIDHs8CA= github.com/go-toolsmith/pkgload v1.2.2 h1:0CtmHq/02QhxcF7E9N5LIFcYFsMR5rdovfqTtRKkgIk= +github.com/go-toolsmith/pkgload v1.2.2/go.mod h1:R2hxLNRKuAsiXCo2i5J6ZQPhnPMOVtU+f0arbFPWCus= github.com/go-toolsmith/strparse v1.0.0/go.mod h1:YI2nUKP9YGZnL/L1/DLFBfixrcjslWct4wyljWhSRy8= github.com/go-toolsmith/strparse v1.1.0 h1:GAioeZUK9TGxnLS+qfdqNbA4z0SSm5zVNtCQiyP2Bvw= github.com/go-toolsmith/strparse v1.1.0/go.mod h1:7ksGy58fsaQkGQlY8WVoBFNyEPMGuJin1rfoPS4lBSQ= @@ -355,7 +365,9 @@ github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7a github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q= github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q= github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe h1:lXe2qZdvpiX5WZkZR4hgp4KJVfY3nMkvmwbVkpv1rVY= +github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0= github.com/golang-sql/sqlexp v0.0.0-20170517235910-f1bb20e5a188 h1:+eHOFJl1BaXrQxKX+T06f78590z4qA2ZzBTqahsKSE4= +github.com/golang-sql/sqlexp v0.0.0-20170517235910-f1bb20e5a188/go.mod h1:vXjM/+wXQnTPR4KqTKDgJukSZ6amVRtWMPEjE6sQoK8= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= github.com/golang/glog v1.1.2 h1:DVjP2PbBOzHyzA+dn3WhHIq4NdVu3Q+pvivFICf/7fo= github.com/golang/glog v1.1.2/go.mod h1:zR+okUeTbrL6EL3xHUDxZuEtGv04p5shwip1+mL/rLQ= @@ -434,6 +446,7 @@ github.com/google/go-containerregistry v0.13.0 h1:y1C7Z3e149OJbOPDBxLYR8ITPz8dTK github.com/google/go-containerregistry v0.13.0/go.mod h1:J9FQ+eSS4a1aC2GNZxvNpbWhgp0487v+cgiilB4FqDo= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/gofuzz v1.2.0 h1:xRy4A+RhZaiKjJ1bPfwQ8sedCA+YS2YcCHW6ec7JMi0= +github.com/google/gofuzz v1.2.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs= github.com/google/martian/v3 v3.0.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0= github.com/google/martian/v3 v3.1.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0= @@ -454,6 +467,7 @@ github.com/google/pprof v0.0.0-20230228050547-1710fef4ab10 h1:CqYfpuYIjnlNxM3msd github.com/google/pprof v0.0.0-20230228050547-1710fef4ab10/go.mod h1:79YE0hCXdHag9sBkw2o+N/YnZtTkXi0UT9Nnixa5eYk= github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI= github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4= +github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ= github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4= github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= @@ -476,6 +490,7 @@ github.com/gostaticanalysis/nilerr v0.1.1 h1:ThE+hJP0fEp4zWLkWHWcRyI2Od0p7DlgYG3 github.com/gostaticanalysis/nilerr v0.1.1/go.mod h1:wZYb6YI5YAxxq0i1+VJbY0s2YONW0HU0GPE3+5PWN4A= github.com/gostaticanalysis/testutil v0.3.1-0.20210208050101-bfb5c8eec0e4/go.mod h1:D+FIZ+7OahH3ePw/izIEeH5I06eKs1IKI4Xr64/Am3M= github.com/gostaticanalysis/testutil v0.4.0 h1:nhdCmubdmDF6VEatUNjgUZBJKWRqugoISdUv3PPQgHY= +github.com/gostaticanalysis/testutil v0.4.0/go.mod h1:bLIoPefWXrRi/ssLFWX1dx7Repi5x3CuviD3dgAZaBU= github.com/gotestyourself/gotestyourself v2.2.0+incompatible h1:AQwinXlbQR2HvPjQZOmDhRqsv5mZf+Jb1RnSLxcqZcI= github.com/gotestyourself/gotestyourself v2.2.0+incompatible/go.mod h1:zZKM6oeNM8k+FRljX1mnzVYeS8wiGgQyvST1/GafPbY= github.com/gtank/merlin v0.1.1-0.20191105220539-8318aed1a79f/go.mod h1:T86dnYJhcGOh5BjZFCJWTDeTK7XW8uE+E21Cy/bIQ+s= @@ -525,6 +540,7 @@ github.com/jessevdk/go-flags v1.5.0/go.mod h1:Fw0T6WPc1dYxT4mKEZRfG5kJhaTDP9pj1c github.com/jgautheron/goconst v1.5.1 h1:HxVbL1MhydKs8R8n/HE5NPvzfaYmQJA3o879lE4+WcM= github.com/jgautheron/goconst v1.5.1/go.mod h1:aAosetZ5zaeC/2EfMeRswtxUFBpe2Hr7HzkgX4fanO4= github.com/jhump/protoreflect v1.15.1 h1:HUMERORf3I3ZdX05WaQ6MIpd/NJ434hTp5YiKgfCL6c= +github.com/jhump/protoreflect v1.15.1/go.mod h1:jD/2GMKKE6OqX8qTjhADU1e6DShO+gavG9e0Q693nKo= github.com/jingyugao/rowserrcheck v1.1.1 h1:zibz55j/MJtLsjP1OF4bSdgXxwL1b+Vn7Tjzq7gFzUs= github.com/jingyugao/rowserrcheck v1.1.1/go.mod h1:4yvlZSDb3IyDTUZJUmpZfm2Hwok+Dtp+nu2qOq+er9c= github.com/jinzhu/copier v0.3.5 h1:GlvfUwHk62RokgqVNvYsku0TATCF7bAHVwEXoBh3iJg= @@ -569,6 +585,7 @@ github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFB github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= +github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= @@ -624,6 +641,7 @@ github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/ github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0= github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= github.com/mattn/go-sqlite3 v1.14.9 h1:10HX2Td0ocZpYEjhilsuo6WWtUqttj2Kb0KtD86/KYA= +github.com/mattn/go-sqlite3 v1.14.9/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= github.com/matttproud/golang_protobuf_extensions v1.0.4 h1:mmDVorXM7PCGKw94cs5zkfA9PSy5pEvNWRP0ET0TIVo= github.com/matttproud/golang_protobuf_extensions v1.0.4/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4= @@ -678,11 +696,13 @@ github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108 github.com/onsi/ginkgo v1.14.0 h1:2mOpI4JVVPBN+WQRa0WKH2eXR+Ey+uK4n7Zj0aYpIQA= github.com/onsi/ginkgo v1.14.0/go.mod h1:iSB4RoI2tjJc9BBv4NKIKWKya62Rps+oPG/Lv9klQyY= github.com/onsi/ginkgo/v2 v2.8.0 h1:pAM+oBNPrpXRs+E/8spkeGx9QgekbRVyr74EUvRVOUI= +github.com/onsi/ginkgo/v2 v2.8.0/go.mod h1:6JsQiECmxCa3V5st74AL/AmsV482EDdVrGaVW6z3oYU= github.com/onsi/gomega v1.4.1/go.mod h1:C1qb7wdrVGGVU+Z6iS04AVkA3Q65CEZX59MT0QO5uiA= github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY= github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo= github.com/onsi/gomega v1.26.0 h1:03cDLK28U6hWvCAns6NeydX3zIm4SF3ci69ulidS32Q= +github.com/onsi/gomega v1.26.0/go.mod h1:r+zV744Re+DiYCIPRlYOTxn0YkOLcAnW8k1xXdMPGhM= github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U= github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM= github.com/opencontainers/image-spec v1.1.0-rc2 h1:2zx/Stx4Wc5pIPDvIxHXvXtQFW/7XWJGmnM7r3wg034= @@ -694,6 +714,7 @@ github.com/opencontainers/selinux v1.10.0/go.mod h1:2i0OySw99QjzBBQByd1Gr9gSjvuh github.com/ory/dockertest v3.3.5+incompatible h1:iLLK6SQwIhcbrG783Dghaaa3WPzGc+4Emza6EbVUUGA= github.com/ory/dockertest v3.3.5+incompatible/go.mod h1:1vX4m9wsvi00u5bseYwXaSnhNrne+V0E6LAcBILJdPs= github.com/ory/dockertest/v3 v3.9.1 h1:v4dkG+dlu76goxMiTT2j8zV7s4oPPEppKT8K8p2f1kY= +github.com/ory/dockertest/v3 v3.9.1/go.mod h1:42Ir9hmvaAPm0Mgibk6mBPi7SFvTXxEcnztDYOJ//uM= github.com/otiai10/copy v1.2.0 h1:HvG945u96iNadPoG2/Ja2+AUJeW5YuFQMixq9yirC+k= github.com/otiai10/copy v1.2.0/go.mod h1:rrF5dJ5F0t/EWSYODDu4j9/vEeYHMkc8jt0zJChqQWw= github.com/otiai10/curr v0.0.0-20150429015615-9b4961190c95/go.mod h1:9qAhocn7zKJG+0mI8eUu6xqkFDYS2kb2saOteoSB3cE= @@ -766,6 +787,7 @@ github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 h1:N/ElC8H3+5X github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4= github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8= +github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= github.com/rs/cors v1.8.3 h1:O+qNyWn7Z+F9M0ILBHgMVPuB1xTOucVd5gtaYyXBpRo= github.com/rs/cors v1.8.3/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU= github.com/rs/xid v1.4.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= @@ -882,8 +904,11 @@ github.com/tenntenn/text/transform v0.0.0-20200319021203-7eef512accb3/go.mod h1: github.com/tetafro/godot v1.4.11 h1:BVoBIqAf/2QdbFmSwAWnaIqDivZdOV0ZRwEm6jivLKw= github.com/tetafro/godot v1.4.11/go.mod h1:LR3CJpxDVGlYOWn3ZZg1PgNZdTUvzsZWu8xaEohUpn8= github.com/tidwall/gjson v1.14.4 h1:uo0p8EbA09J7RQaflQ1aBRffTR7xedD2bcIVSYxLnkM= +github.com/tidwall/gjson v1.14.4/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA= +github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM= github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4= +github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= github.com/timakin/bodyclose v0.0.0-20221125081123-e39cf3fc478e h1:MV6KaVu/hzByHP0UvJ4HcMGE/8a6A4Rggc/0wx2AvJo= github.com/timakin/bodyclose v0.0.0-20221125081123-e39cf3fc478e/go.mod h1:27bSVNWSBOHm+qRp1T9qzaIpsWEP6TbUnei/43HK+PQ= github.com/timonwong/loggercheck v0.9.4 h1:HKKhqrjcVj8sxL7K77beXh0adEm6DLjV/QOGeMXEVi4= @@ -906,6 +931,7 @@ github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyC github.com/valyala/fasttemplate v1.0.1/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPUpymEIMZ47gx8= github.com/valyala/fasttemplate v1.2.1/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ= github.com/vbatts/tar-split v0.11.2 h1:Via6XqJr0hceW4wff3QRzD5gAk/tatMw/4ZA7cTlIME= +github.com/vbatts/tar-split v0.11.2/go.mod h1:vV3ZuO2yWSVsz+pfFzDG/upWH1JhjOiEaWq6kXyQ3VI= github.com/vektra/mockery/v2 v2.23.1 h1:N59FENM2d/gWE6Ns5JPuf9a7jqQWeheGefZqvuvb1dM= github.com/vektra/mockery/v2 v2.23.1/go.mod h1:Zh3Kv1ckKs6FokhlVLcCu6UTyzfS3M8mpROz1lBNp+w= github.com/vishvananda/netlink v1.1.0/go.mod h1:cTgwzPIzzgDAYoQrMm0EdrjRUBkTqKYppBueQtXaqoE= @@ -913,8 +939,11 @@ github.com/vishvananda/netns v0.0.0-20191106174202-0a2b9b5464df/go.mod h1:JP3t17 github.com/xanzy/ssh-agent v0.3.3 h1:+/15pJfg/RsTxqYcX6fHqOXZwwMP+2VyYWJeWM2qQFM= github.com/xanzy/ssh-agent v0.3.3/go.mod h1:6dzNDKs0J9rVPHPhaGCukekBHKqfl+L3KghI1Bc68Uw= github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb h1:zGWFAtiMcyryUHoUjUJX0/lt1H2+i2Ka2n+D3DImSNo= +github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU= github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 h1:EzJWgHovont7NscjpAxXsDA8S8BMYve8Y5+7cuRE7R0= +github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ= github.com/xeipuuv/gojsonschema v1.2.0 h1:LhYJRs+L4fBtjZUfuSZIKGeVu0QRy8e5Xi7D17UxZ74= +github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y= github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q= github.com/yagipy/maintidx v1.0.0 h1:h5NvIsCz+nRDapQ0exNv4aJ0yXSI0420omVANTv3GJM= github.com/yagipy/maintidx v1.0.0/go.mod h1:0qNf/I/CCZXSMhsRsrEPDZ+DkekpKLXAJfsTACwgXLk= @@ -950,6 +979,7 @@ go.opentelemetry.io/otel/trace v1.15.1/go.mod h1:IWdQG/5N1x7f6YUlmdLeJvH9yxtuJAf go.uber.org/atomic v1.10.0 h1:9qC72Qh0+3MqyJbAn8YU5xVq1frD8bn3JtD2oXtafVQ= go.uber.org/atomic v1.10.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0= go.uber.org/goleak v1.1.11 h1:wy28qYRKZgnJTxGxvye5/wgWr1EKjmUDGYox5mGlRlI= +go.uber.org/goleak v1.1.11/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ= go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ= go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= go.uber.org/zap v1.24.0 h1:FiJd5l1UOLj0wCgbSE0rwwXHzEdAZS6hiiSnxJN/D60= @@ -1224,6 +1254,7 @@ golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxb golang.org/x/time v0.0.0-20201208040808-7e3f01d25324/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20210220033141-f8bda1e9f3ba/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.1.0 h1:xYY+Bajn2a7VBmTM5GikTmnK8ZuX8YgnQCqZpbBNtmA= +golang.org/x/time v0.1.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/tools v0.0.0-20180525024113-a5b4c53f6e8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= @@ -1432,6 +1463,7 @@ gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo= gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw= gotest.tools/v3 v3.0.3 h1:4AuOwCGf4lLR9u3YOe2awrHygurzhO/HeQ6laiA6Sx0= +gotest.tools/v3 v3.0.3/go.mod h1:Z7Lb0S5l+klDB31fvDQX8ss/FlKDxtlFlw3Oa8Ymbl8= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= diff --git a/mempool/cat/block_builder.go b/mempool/cat/block_builder.go new file mode 100644 index 0000000000..ee6722a812 --- /dev/null +++ b/mempool/cat/block_builder.go @@ -0,0 +1,263 @@ +package cat + +import ( + "context" + "fmt" + "sync" + "time" + + "github.com/tendermint/tendermint/types" +) + +// FetchTxsFromKeys is called upon by consensus upon receiving a complete compact block. +// The method iterates through the keys in the compact block. For the transactions it +// already has it adds them to a list. For the transactions that are missing it uses a +// block request to track and retrieve them. Once all transactions are retrieved, it returns +// the complete set to the consensus engine. This can be called multiple times sequentially +// with the same blockID and is thread safe +func (memR *Reactor) FetchTxsFromKeys(ctx context.Context, blockID []byte, compactData [][]byte) ([][]byte, error) { + if request, ok := memR.blockFetcher.GetRequest(blockID); ok { + memR.Logger.Debug("tracking existing request for block transactions") + // we already have a request for this block + return request.WaitForBlock(ctx) + } + + txs := make([][]byte, len(compactData)) + missingKeys := make(map[int]types.TxKey, len(compactData)) + + // iterate through the keys to know what transactions we have and what are missing + for i, key := range compactData { + txKey, err := types.TxKeyFromBytes(key) + if err != nil { + return nil, fmt.Errorf("incorrect compact blocks format: %w", err) + } + wtx := memR.mempool.store.get(txKey) + if wtx != nil { + txs[i] = wtx.tx + } else { + missingKeys[i] = txKey + } + } + memR.Logger.Info("fetching transactions from peers", "blockID", blockID, "numTxs", len(txs), "numMissing", len(missingKeys)) + + memR.mempool.jsonMetrics.Lock() + memR.mempool.jsonMetrics.TransactionsMissing = append(memR.mempool.jsonMetrics.TransactionsMissing, uint64(len(missingKeys))) + memR.mempool.jsonMetrics.Transactions = append(memR.mempool.jsonMetrics.Transactions, uint64(len(compactData))) + // Check if we got lucky and already had all the transactions. + if len(missingKeys) == 0 { + memR.mempool.jsonMetrics.TimeTakenFetchingTxs = append(memR.mempool.jsonMetrics.TimeTakenFetchingTxs, 0) + memR.mempool.jsonMetrics.Unlock() + return txs, nil + } + memR.mempool.jsonMetrics.Unlock() + + // setup a request for this block and begin to track and retrieve all missing transactions + request := memR.blockFetcher.NewRequest( + blockID, + memR.mempool.Height(), + missingKeys, + txs, + ) + defer func() { + timeTaken := request.TimeTaken() + if timeTaken == 0 { + return + } + memR.mempool.jsonMetrics.Lock() + memR.mempool.jsonMetrics.TimeTakenFetchingTxs = append(memR.mempool.jsonMetrics.TimeTakenFetchingTxs, timeTaken) + memR.mempool.jsonMetrics.Unlock() + }() + + // Wait for the reactor to retrieve and post all transactions. + return request.WaitForBlock(ctx) +} + +// FetchKeysFromTxs is in many ways the opposite method. It takes a full block generated by the application +// and reduces it to the set of keys that need to be gossiped from one mempool to another nodes mempool +// in order to recreate the full block. +func (memR *Reactor) FetchKeysFromTxs(ctx context.Context, txs [][]byte) ([][]byte, error) { + keys := make([][]byte, len(txs)) + for idx, tx := range txs { + // check if the context has been cancelled + if ctx.Err() != nil { + return nil, ctx.Err() + } + key := types.Tx(tx).Key() + keys[idx] = key[:] + has := memR.mempool.store.has(key) + if !has { + // If the mempool provided the initial transactions yet received from + // consensus a transaction it doesn't recognize, this implies that + // either a tx was mutated or was added by the application. In either + // case, it is likely no other mempool has this transaction so we + // preemptively broadcast it to all other peers + // + // We don't set the priority, gasWanted or sender fields because we + // don't know them. + wtx := newWrappedTx(tx, key, memR.mempool.Height(), 0, 0, "") + memR.broadcastNewTx(wtx) + // For safety we also store this transaction in the mempool (ignoring + // all size limits) so that we can retrieve it later if needed. Note + // as we're broadcasting it to all peers, we should not receive a `WantTx` + // unless it gets rejected by the application in CheckTx. + // + // Consensus will have an in memory copy of the entire block which includes + // this transaction so it should not need it. + memR.mempool.store.set(wtx) + } + } + + // return the keys back to the consensus engine + return keys, nil +} + +type blockFetcher struct { + // mutex to manage concurrent calls to different parts + mtx sync.Mutex + // requests are a map of all processing block requests + // by blockID. + requests map[string]*blockRequest +} + +// NewBlockFetcher returns a new blockFetcher for managing block requests +func NewBlockFetcher() *blockFetcher { + return &blockFetcher{ + requests: make(map[string]*blockRequest), + } +} + +func (bf *blockFetcher) GetRequest(blockID []byte) (*blockRequest, bool) { + bf.mtx.Lock() + defer bf.mtx.Unlock() + request, ok := bf.requests[string(blockID)] + return request, ok +} + +// NewRequest creates a new block request and returns it. +// If a request already exists it returns that instead +func (bf *blockFetcher) NewRequest( + blockID []byte, + height int64, + missingKeys map[int]types.TxKey, + txs [][]byte, +) *blockRequest { + bf.mtx.Lock() + defer bf.mtx.Unlock() + if request, ok := bf.requests[string(blockID)]; ok { + return request + } + request := NewBlockRequest(height, missingKeys, txs) + bf.requests[string(blockID)] = request + bf.pruneOldRequests(height) + return request +} + +// TryAddMissingTx loops through all current requests and tries to add +// the given transaction (if it is missing). +func (bf *blockFetcher) TryAddMissingTx(key types.TxKey, tx []byte) { + bf.mtx.Lock() + defer bf.mtx.Unlock() + for _, request := range bf.requests { + request.TryAddMissingTx(key, tx) + } +} + +// PruneOldRequests removes any requests that are older than the given height. +func (bf *blockFetcher) pruneOldRequests(height int64) { + for blockID, request := range bf.requests { + if request.height < height { + delete(bf.requests, blockID) + } + } +} + +// blockRequests handle the lifecycle of individual block requests. +type blockRequest struct { + // immutable fields + height int64 + doneCh chan struct{} + + mtx sync.Mutex + // track the remaining keys that are missing + missingKeysByIndex map[int]types.TxKey + missingKeys map[string]int + // the txs in the block + txs [][]byte + + // used for metrics + startTime time.Time + endTime time.Time +} + +func NewBlockRequest( + height int64, + missingKeys map[int]types.TxKey, + txs [][]byte, +) *blockRequest { + mk := make(map[string]int, len(missingKeys)) + for i, key := range missingKeys { + mk[key.String()] = i + } + return &blockRequest{ + height: height, + missingKeysByIndex: missingKeys, + missingKeys: mk, + txs: txs, + doneCh: make(chan struct{}), + startTime: time.Now().UTC(), + } +} + +// WaitForBlock is a blocking call that waits for the block to be fetched and completed. +// It can be called concurrently. If the block was already fetched it returns immediately. +func (br *blockRequest) WaitForBlock(ctx context.Context) ([][]byte, error) { + if br.IsDone() { + return br.txs, nil + } + + for { + select { + case <-ctx.Done(): + return nil, ctx.Err() + case <-br.doneCh: + br.mtx.Lock() + defer br.mtx.Unlock() + br.endTime = time.Now().UTC() + return br.txs, nil + } + } +} + +// TryAddMissingTx checks if a given transactions was missing and if so +// adds it to the block request. +func (br *blockRequest) TryAddMissingTx(key types.TxKey, tx []byte) bool { + br.mtx.Lock() + defer br.mtx.Unlock() + if index, ok := br.missingKeys[key.String()]; ok { + delete(br.missingKeys, key.String()) + delete(br.missingKeysByIndex, index) + br.txs[index] = tx + // check if there is any more transactions remaining + if len(br.missingKeys) == 0 { + // Yaay! We're done! + close(br.doneCh) + } + return true + } + return false +} + +// IsDone returns whether all transactions in the block have been received. +// This is done by measuring the amount of missing keys. +func (br *blockRequest) IsDone() bool { + br.mtx.Lock() + defer br.mtx.Unlock() + return len(br.missingKeys) == 0 +} + +func (br *blockRequest) TimeTaken() uint64 { + if br.endTime.IsZero() { + return 0 + } + return uint64(br.endTime.Sub(br.startTime).Milliseconds()) +} diff --git a/mempool/cat/block_builder_test.go b/mempool/cat/block_builder_test.go new file mode 100644 index 0000000000..ec0089da85 --- /dev/null +++ b/mempool/cat/block_builder_test.go @@ -0,0 +1,243 @@ +package cat + +import ( + "context" + "fmt" + "math/rand" + "sync" + "testing" + "time" + + "github.com/tendermint/tendermint/crypto/tmhash" + "github.com/tendermint/tendermint/mempool" + "github.com/tendermint/tendermint/p2p" + memproto "github.com/tendermint/tendermint/proto/tendermint/mempool" + "github.com/tendermint/tendermint/types" + "github.com/stretchr/testify/require" +) + +func TestBlockRequest(t *testing.T) { + ctx, cancel := context.WithTimeout(context.Background(), time.Second) + defer cancel() + tx1, tx2 := types.Tx("hello"), types.Tx("world") + key1, key2 := tx1.Key(), tx2.Key() + txs := make([][]byte, 2) + missingKeys := map[int]types.TxKey{ + 0: key1, + 1: key2, + } + + request := NewBlockRequest(1, missingKeys, txs) + + require.True(t, request.TryAddMissingTx(key1, tx1)) + // cannot add the same missing tx twice + require.False(t, request.TryAddMissingTx(key1, tx1)) + require.False(t, request.IsDone()) + + // test that we adhere to the context deadline + shortCtx, cancel := context.WithTimeout(context.Background(), time.Millisecond) + defer cancel() + _, err := request.WaitForBlock(shortCtx) + require.Error(t, err) + + // test that all txs mean the block `IsDone` + require.True(t, request.TryAddMissingTx(key2, tx2)) + require.True(t, request.IsDone()) + + // waiting for the block should instantly return + txs, err = request.WaitForBlock(ctx) + require.NoError(t, err) + require.Equal(t, txs, [][]byte{tx1, tx2}) +} + +func TestBlockRequestConcurrently(t *testing.T) { + ctx, cancel := context.WithTimeout(context.Background(), time.Second) + defer cancel() + const numTxs = 10 + allTxs := make([][]byte, numTxs) + txKeys := make([]types.TxKey, numTxs) + missingKeys := make(map[int]types.TxKey) + txs := make([][]byte, numTxs) + for i := 0; i < numTxs; i++ { + tx := types.Tx(fmt.Sprintf("tx%d", i)) + allTxs[i] = tx + txKeys[i] = tx.Key() + if i%3 == 0 { + txs[i] = tx + } else { + missingKeys[i] = txKeys[i] + } + } + + request := NewBlockRequest(1, missingKeys, txs) + + wg := sync.WaitGroup{} + for i := 0; i < numTxs; i++ { + wg.Add(1) + go func(i int) { + defer wg.Done() + request.TryAddMissingTx(txKeys[i], txs[i]) + }(i) + } + + // wait for the block + result, err := request.WaitForBlock(ctx) + require.NoError(t, err) + require.Len(t, result, numTxs) + for i := 0; i < numTxs; i++ { + require.Equal(t, string(result[i]), string(txs[i])) + } + wg.Wait() +} + +func TestBlockFetcherSimple(t *testing.T) { + bf := NewBlockFetcher() + tx := types.Tx("hello world") + key := tx.Key() + missingKeys := map[int]types.TxKey{ + 0: key, + } + blockID := []byte("blockID") + req := bf.NewRequest(blockID, 1, missingKeys, make([][]byte, 1)) + req2, ok := bf.GetRequest(blockID) + require.True(t, ok) + require.Equal(t, req, req2) + // a different request for the same blockID should + // return the same original request object. + req3 := bf.NewRequest(blockID, 2, missingKeys, make([][]byte, 2)) + require.Equal(t, req, req3) + + req4 := bf.NewRequest([]byte("differentBlockID"), 1, missingKeys, make([][]byte, 1)) + + bf.TryAddMissingTx(key, tx) + require.False(t, req4.TryAddMissingTx(key, tx)) + require.True(t, req.IsDone()) + require.Len(t, bf.requests, 2) +} + +func TestBlockFetcherConcurrentRequests(t *testing.T) { + var ( + bf = NewBlockFetcher() + numBlocks = 5 + numRequestsPerBlock = 5 + numTxs = 5 + requestWG = sync.WaitGroup{} + goRoutinesWG = sync.WaitGroup{} + allTxs = make([][]byte, numTxs) + txs = make([][]byte, numTxs) + missingKeys = make(map[int]types.TxKey) + ) + + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + + for i := 0; i < numTxs; i++ { + tx := types.Tx(fmt.Sprintf("tx%d", i)) + allTxs[i] = tx + if i%3 == 0 { + txs[i] = tx + } else { + missingKeys[i] = tx.Key() + } + } + + for i := 0; i < numBlocks; i++ { + requestWG.Add(1) + for j := 0; j < numRequestsPerBlock; j++ { + goRoutinesWG.Add(1) + go func(blockID []byte, routine int) { + defer goRoutinesWG.Done() + // create a copy of the missingKeys and txs + mk := make(map[int]types.TxKey) + for i, k := range missingKeys { + mk[i] = k + } + txsCopy := make([][]byte, len(txs)) + copy(txsCopy, txs) + request := bf.NewRequest(blockID, 1, mk, txs) + if routine == 0 { + requestWG.Done() + } + _, _ = request.WaitForBlock(ctx) + }([]byte(fmt.Sprintf("blockID%d", i)), j) + } + goRoutinesWG.Add(1) + go func() { + defer goRoutinesWG.Done() + // Wait until all the request have started + requestWG.Wait() + for _, tx := range allTxs { + bf.TryAddMissingTx(types.Tx(tx).Key(), tx) + } + }() + } + goRoutinesWG.Wait() + + for i := 0; i < numBlocks; i++ { + blockID := []byte(fmt.Sprintf("blockID%d", i)) + request, ok := bf.GetRequest(blockID) + require.True(t, ok) + require.True(t, request.IsDone()) + result, err := request.WaitForBlock(ctx) + require.NoError(t, err) + require.Equal(t, result, txs) + } +} + +func TestFetchTxsFromKeys(t *testing.T) { + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + defer cancel() + reactor, pool := setupReactor(t) + + numTxs := 10 + txs := make([][]byte, numTxs) + keys := make([][]byte, numTxs) + peer := genPeer() + blockID := tmhash.Sum([]byte("blockID")) + wg := sync.WaitGroup{} + for i := 0; i < numTxs; i++ { + tx := newDefaultTx(fmt.Sprintf("tx%d", i)) + txs[i] = tx + key := tx.Key() + keys[i] = key[:] + // every 1 in 3 transactions proposed in the block, the node + // already has in their mempool and doesn't need to fetch + if i%3 == 0 { + t.Log("adding tx to mempool", i) + err := pool.CheckTx(tx, nil, mempool.TxInfo{}) + require.NoError(t, err) + } else { + wg.Add(1) + go func() { + defer wg.Done() + time.Sleep(time.Duration(rand.Int63n(100)) * time.Millisecond) + reactor.ReceiveEnvelope(p2p.Envelope{ + Src: peer, + Message: &memproto.Txs{Txs: [][]byte{tx}}, + ChannelID: mempool.MempoolChannel, + }) + }() + } + } + + reactor.InitPeer(peer) + + go func() { + reactor.ReceiveEnvelope(p2p.Envelope{ + Src: peer, + Message: &memproto.Txs{Txs: txs}, + ChannelID: mempool.MempoolChannel, + }) + }() + + resultTxs, err := reactor.FetchTxsFromKeys(ctx, blockID, keys) + require.NoError(t, err) + require.Equal(t, len(txs), len(resultTxs)) + for idx, tx := range resultTxs { + require.Equal(t, txs[idx], tx) + } + repeatResult, err := reactor.FetchTxsFromKeys(ctx, blockID, keys) + require.NoError(t, err) + require.Equal(t, resultTxs, repeatResult) + wg.Wait() +} diff --git a/mempool/cat/pool.go b/mempool/cat/pool.go index 23d56ac1f6..6c98f74169 100644 --- a/mempool/cat/pool.go +++ b/mempool/cat/pool.go @@ -3,6 +3,7 @@ package cat import ( "errors" "fmt" + "path/filepath" "runtime" "sort" "sync" @@ -13,6 +14,7 @@ import ( abci "github.com/tendermint/tendermint/abci/types" "github.com/tendermint/tendermint/config" "github.com/tendermint/tendermint/libs/log" + tmos "github.com/tendermint/tendermint/libs/os" "github.com/tendermint/tendermint/mempool" "github.com/tendermint/tendermint/proxy" "github.com/tendermint/tendermint/types" @@ -52,6 +54,7 @@ type TxPool struct { config *config.MempoolConfig proxyAppConn proxy.AppConnMempool metrics *mempool.Metrics + jsonMetrics *mempool.JSONMetrics // these values are modified once per height updateMtx sync.Mutex @@ -86,6 +89,12 @@ func NewTxPool( height int64, options ...TxPoolOption, ) *TxPool { + // set up the directory for tracking metrics + path := filepath.Join(cfg.RootDir, "data", "mempool") + if err := tmos.EnsureDir(path, 0700); err != nil { + panic(err) + } + txmp := &TxPool{ logger: logger, config: cfg, @@ -99,6 +108,7 @@ func NewTxPool( store: newStore(), broadcastCh: make(chan *wrappedTx), txsToBeBroadcast: make([]types.TxKey, 0), + jsonMetrics: mempool.NewJSONMetrics(path), } for _, opt := range options { diff --git a/mempool/cat/reactor.go b/mempool/cat/reactor.go index 7b2b5a9baa..6a566e64d0 100644 --- a/mempool/cat/reactor.go +++ b/mempool/cat/reactor.go @@ -37,11 +37,12 @@ const ( // spec under /.spec.md type Reactor struct { p2p.BaseReactor - opts *ReactorOptions - mempool *TxPool - ids *mempoolIDs - requests *requestScheduler - traceClient *trace.Client + opts *ReactorOptions + mempool *TxPool + ids *mempoolIDs + requests *requestScheduler + blockFetcher *blockFetcher + traceClient *trace.Client } type ReactorOptions struct { @@ -87,11 +88,12 @@ func NewReactor(mempool *TxPool, opts *ReactorOptions) (*Reactor, error) { return nil, err } memR := &Reactor{ - opts: opts, - mempool: mempool, - ids: newMempoolIDs(), - requests: newRequestScheduler(opts.MaxGossipDelay, defaultGlobalRequestTimeout), - traceClient: &trace.Client{}, + opts: opts, + mempool: mempool, + ids: newMempoolIDs(), + requests: newRequestScheduler(opts.MaxGossipDelay, defaultGlobalRequestTimeout), + blockFetcher: NewBlockFetcher(), + traceClient: &trace.Client{}, } memR.BaseReactor = *p2p.NewBaseReactor("Mempool", memR) return memR, nil @@ -256,6 +258,9 @@ func (memR *Reactor) ReceiveEnvelope(e p2p.Envelope) { memR.Logger.Info("Could not add tx", "txKey", key, "err", err) return } + // If a block has been proposed with this transaction and + // consensus was waiting for it, it will now be published. + memR.blockFetcher.TryAddMissingTx(key, tx) if !memR.opts.ListenOnly { // We broadcast only transactions that we deem valid and actually have in our mempool. memR.broadcastSeenTx(key) diff --git a/mempool/metrics.go b/mempool/metrics.go index 6c00ce59bc..a3d73da3ac 100644 --- a/mempool/metrics.go +++ b/mempool/metrics.go @@ -1,6 +1,13 @@ package mempool import ( + "encoding/json" + "fmt" + "path/filepath" + "sync" + "time" + + "github.com/tendermint/tendermint/libs/os" "github.com/go-kit/kit/metrics" "github.com/go-kit/kit/metrics/discard" "github.com/go-kit/kit/metrics/prometheus" @@ -154,3 +161,46 @@ func NopMetrics() *Metrics { RerequestedTxs: discard.NewCounter(), } } + +type JSONMetrics struct { + dir string + interval int + sync.Mutex + StartTime time.Time + EndTime time.Time + Blocks uint64 + Transactions []uint64 + TransactionsMissing []uint64 + // measured in ms + TimeTakenFetchingTxs []uint64 +} + +func NewJSONMetrics(dir string) *JSONMetrics { + return &JSONMetrics{ + dir: dir, + StartTime: time.Now().UTC(), + Transactions: make([]uint64, 0), + TransactionsMissing: make([]uint64, 0), + TimeTakenFetchingTxs: make([]uint64, 0), + } +} + +func (m *JSONMetrics) Save() { + m.EndTime = time.Now().UTC() + content, err := json.MarshalIndent(m, "", " ") + if err != nil { + panic(err) + } + path := filepath.Join(m.dir, fmt.Sprintf("metrics_%d.json", m.interval)) + os.MustWriteFile(path, content, 0644) + m.StartTime = m.EndTime + m.interval++ + m.reset() +} + +func (m *JSONMetrics) reset() { + m.Blocks = 0 + m.Transactions = make([]uint64, 0) + m.TransactionsMissing = make([]uint64, 0) + m.TimeTakenFetchingTxs = make([]uint64, 0) +} diff --git a/node/node.go b/node/node.go index 018e0c7985..6a8aad11a8 100644 --- a/node/node.go +++ b/node/node.go @@ -33,9 +33,7 @@ import ( "github.com/tendermint/tendermint/libs/service" "github.com/tendermint/tendermint/light" mempl "github.com/tendermint/tendermint/mempool" - mempoolv2 "github.com/tendermint/tendermint/mempool/cat" - mempoolv0 "github.com/tendermint/tendermint/mempool/v0" - mempoolv1 "github.com/tendermint/tendermint/mempool/v1" + "github.com/tendermint/tendermint/mempool/cat" "github.com/tendermint/tendermint/p2p" "github.com/tendermint/tendermint/p2p/pex" "github.com/tendermint/tendermint/privval" @@ -379,22 +377,22 @@ func createMempoolAndMempoolReactor( memplMetrics *mempl.Metrics, logger log.Logger, traceClient *trace.Client, -) (mempl.Mempool, p2p.Reactor) { +) (*cat.TxPool, *cat.Reactor) { switch config.Mempool.Version { case cfg.MempoolV2: - mp := mempoolv2.NewTxPool( + mp := cat.NewTxPool( logger, config.Mempool, proxyApp.Mempool(), state.LastBlockHeight, - mempoolv2.WithMetrics(memplMetrics), - mempoolv2.WithPreCheck(sm.TxPreCheck(state)), - mempoolv2.WithPostCheck(sm.TxPostCheck(state)), + cat.WithMetrics(memplMetrics), + cat.WithPreCheck(sm.TxPreCheck(state)), + cat.WithPostCheck(sm.TxPostCheck(state)), ) - reactor, err := mempoolv2.NewReactor( + reactor, err := cat.NewReactor( mp, - &mempoolv2.ReactorOptions{ + &cat.ReactorOptions{ ListenOnly: !config.Mempool.Broadcast, MaxTxSize: config.Mempool.MaxTxBytes, TraceClient: traceClient, @@ -410,52 +408,6 @@ func createMempoolAndMempoolReactor( } reactor.SetLogger(logger) - return mp, reactor - case cfg.MempoolV1: - mp := mempoolv1.NewTxMempool( - logger, - config.Mempool, - proxyApp.Mempool(), - state.LastBlockHeight, - mempoolv1.WithMetrics(memplMetrics), - mempoolv1.WithPreCheck(sm.TxPreCheck(state)), - mempoolv1.WithPostCheck(sm.TxPostCheck(state)), - mempoolv1.WithTraceClient(traceClient), - ) - - reactor := mempoolv1.NewReactor( - config.Mempool, - mp, - traceClient, - ) - if config.Consensus.WaitForTxs() { - mp.EnableTxsAvailable() - } - reactor.SetLogger(logger) - - return mp, reactor - - case cfg.MempoolV0: - mp := mempoolv0.NewCListMempool( - config.Mempool, - proxyApp.Mempool(), - state.LastBlockHeight, - mempoolv0.WithMetrics(memplMetrics), - mempoolv0.WithPreCheck(sm.TxPreCheck(state)), - mempoolv0.WithPostCheck(sm.TxPostCheck(state)), - ) - - mp.SetLogger(logger) - - reactor := mempoolv0.NewReactor( - config.Mempool, - mp, - ) - if config.Consensus.WaitForTxs() { - mp.EnableTxsAvailable() - } - reactor.SetLogger(logger) - return mp, reactor default: @@ -508,7 +460,8 @@ func createConsensusReactor(config *cfg.Config, state sm.State, blockExec *sm.BlockExecutor, blockStore sm.BlockStore, - mempool mempl.Mempool, + catpool *cat.TxPool, + catReactor *cat.Reactor, evidencePool *evidence.Pool, privValidator types.PrivValidator, csMetrics *cs.Metrics, @@ -522,7 +475,8 @@ func createConsensusReactor(config *cfg.Config, state.Copy(), blockExec, blockStore, - mempool, + catpool, + catReactor, evidencePool, cs.StateMetrics(csMetrics), cs.SetTraceClient(traceClient), @@ -902,7 +856,7 @@ func NewNode(config *cfg.Config, csMetrics.FastSyncing.Set(1) } consensusReactor, consensusState := createConsensusReactor( - config, state, blockExec, blockStore, mempool, evidencePool, + config, state, blockExec, blockStore, mempool, mempoolReactor, evidencePool, privValidator, csMetrics, stateSync || fastSync, eventBus, consensusLogger, influxdbClient, ) @@ -1483,7 +1437,7 @@ func makeNodeInfo( } if config.Mempool.Version == cfg.MempoolV2 { - nodeInfo.Channels = append(nodeInfo.Channels, mempoolv2.MempoolStateChannel) + nodeInfo.Channels = append(nodeInfo.Channels, cat.MempoolStateChannel) } lAddr := config.P2P.ExternalAddress diff --git a/node/node_test.go b/node/node_test.go index db486b80c1..55a178babf 100644 --- a/node/node_test.go +++ b/node/node_test.go @@ -318,7 +318,7 @@ func TestCreateProposalBlock(t *testing.T) { ) commit := types.NewCommit(height-1, 0, types.BlockID{}, nil) - block, _ := blockExec.CreateProposalBlock( + block := blockExec.CreateProposalBlock( height, state, commit, proposerAddr, @@ -408,7 +408,7 @@ func TestMaxProposalBlockSize(t *testing.T) { ) commit := types.NewCommit(height-1, 0, types.BlockID{}, nil) - block, _ := blockExec.CreateProposalBlock( + block := blockExec.CreateProposalBlock( height, state, commit, proposerAddr, diff --git a/state/execution.go b/state/execution.go index fc5a929af9..d0dfc707f9 100644 --- a/state/execution.go +++ b/state/execution.go @@ -97,7 +97,7 @@ func (blockExec *BlockExecutor) CreateProposalBlock( height int64, state State, commit *types.Commit, proposerAddr []byte, -) (*types.Block, *types.PartSet) { +) *types.Block { maxBytes := state.ConsensusParams.Block.MaxBytes maxGas := state.ConsensusParams.Block.MaxGas @@ -158,13 +158,14 @@ func (blockExec *BlockExecutor) CreateProposalBlock( panic(err) } - return state.MakeBlock( + block, _ := state.MakeBlock( height, newData, commit, evidence, proposerAddr, ) + return block } func (blockExec *BlockExecutor) ProcessProposal( diff --git a/test/maverick/consensus/state.go b/test/maverick/consensus/state.go index ae5afad0ae..8edab1b6e3 100644 --- a/test/maverick/consensus/state.go +++ b/test/maverick/consensus/state.go @@ -1284,7 +1284,9 @@ func (cs *State) createProposalBlock() (block *types.Block, blockParts *types.Pa } proposerAddr := cs.privValidatorPubKey.Address() - return cs.blockExec.CreateProposalBlock(cs.Height, cs.state, commit, proposerAddr) + block = cs.blockExec.CreateProposalBlock(cs.Height, cs.state, commit, proposerAddr) + blockParts = block.MakePartSet(types.BlockPartSizeBytes) + return } // Enter: any +2/3 prevotes at next round. From 211694b25ec3ac657ebb93658db02d2c81c32dee Mon Sep 17 00:00:00 2001 From: Callum Waters <cmwaters19@gmail.com> Date: Wed, 24 Jan 2024 17:10:00 -0600 Subject: [PATCH 002/127] add compact block messages to the reactor --- consensus/msgs.go | 19 ++ consensus/reactor.go | 57 +++- consensus/state.go | 122 ++++++-- consensus/types/peer_round_state.go | 5 +- consensus/types/round_state.go | 17 +- proto/tendermint/consensus/message.go | 10 + proto/tendermint/consensus/types.pb.go | 387 +++++++++++++++++++++---- proto/tendermint/consensus/types.proto | 9 +- 8 files changed, 521 insertions(+), 105 deletions(-) diff --git a/consensus/msgs.go b/consensus/msgs.go index 2675206c3f..c2a02c08f1 100644 --- a/consensus/msgs.go +++ b/consensus/msgs.go @@ -60,6 +60,16 @@ func MsgToProto(msg Message) (*cmtcons.Message, error) { } return m.Wrap().(*cmtcons.Message), nil + case *CompactBlockMessage: + block, err := msg.Block.ToProto() + if err != nil { + return nil, fmt.Errorf("msg to proto error: %w", err) + } + m := &cmtcons.CompactBlock{ + Block: block, + } + return m.Wrap().(*cmtcons.Message), nil + case *BlockPartMessage: parts, err := msg.Part.ToProto() if err != nil { @@ -188,6 +198,15 @@ func MsgFromProto(p *cmtcons.Message) (Message, error) { Round: msg.Round, Part: parts, } + case *cmtcons.CompactBlock: + block, err := types.BlockFromProto(msg.Block) + if err != nil { + return nil, fmt.Errorf("compactBlock msg to proto error: %w", err) + } + pb = &CompactBlockMessage{ + Block: block, + } + case *cmtcons.Vote: vote, err := types.VoteFromProto(msg.Vote) if err != nil { diff --git a/consensus/reactor.go b/consensus/reactor.go index f0c1937c45..f199bfca53 100644 --- a/consensus/reactor.go +++ b/consensus/reactor.go @@ -335,6 +335,9 @@ func (conR *Reactor) ReceiveEnvelope(e p2p.Envelope) { conR.conS.peerMsgQueue <- msgInfo{msg, e.Src.ID()} case *ProposalPOLMessage: ps.ApplyProposalPOLMessage(msg) + case *CompactBlockMessage: + ps.SetHasBlock(msg.Block.Height, ps.PRS.Round) + conR.conS.peerMsgQueue <- msgInfo{msg, e.Src.ID()} case *BlockPartMessage: ps.SetHasProposalBlockPart(msg.Height, msg.Round, int(msg.Part.Index)) conR.Metrics.BlockParts.With("peer_id", string(e.Src.ID())).Add(1) @@ -358,6 +361,8 @@ func (conR *Reactor) ReceiveEnvelope(e p2p.Envelope) { ps.EnsureVoteBitArrays(height, valSize) ps.EnsureVoteBitArrays(height-1, lastCommitSize) ps.SetHasVote(msg.Vote) + // If we receive a vote we can deduce that an honest peer also has the block + ps.SetHasBlock(msg.Vote.Height, msg.Vote.Round) cs.peerMsgQueue <- msgInfo{msg, e.Src.ID()} @@ -578,25 +583,22 @@ OUTER_LOOP: rs := conR.getRoundState() prs := ps.GetRoundState() - // Send proposal Block parts? - if rs.ProposalBlockParts.HasHeader(prs.ProposalBlockPartSetHeader) { - if index, ok := rs.ProposalBlockParts.BitArray().Sub(prs.ProposalBlockParts.Copy()).PickRandom(); ok { - part := rs.ProposalBlockParts.GetPart(index) - parts, err := part.ToProto() + // Send compact block + if !prs.Block && rs.ProposalBlockParts.HasHeader(prs.ProposalBlockPartSetHeader) { + logger.Info("Peer has proposal but not block", "height", prs.Height, "round", prs.Round) + if rs.ProposalCompactBlock != nil { + compactBlock, err := rs.ProposalCompactBlock.ToProto() if err != nil { panic(err) } - logger.Debug("Sending block part", "height", prs.Height, "round", prs.Round) + logger.Info("Sending compact block", "height", prs.Height, "round", prs.Round) if p2p.SendEnvelopeShim(peer, p2p.Envelope{ //nolint: staticcheck ChannelID: DataChannel, - Message: &cmtcons.BlockPart{ - Height: rs.Height, // This tells peer that this part applies to us. - Round: rs.Round, // This tells peer that this part applies to us. - Part: *parts, + Message: &cmtcons.CompactBlock{ + Block: compactBlock, }, }, logger) { - schema.WriteBlockPart(conR.traceClient, rs.Height, rs.Round, peer.ID(), part.Index, schema.TransferTypeUpload) - ps.SetHasProposalBlockPart(prs.Height, prs.Round, index) + ps.SetHasBlock(prs.Height, prs.Round) } continue OUTER_LOOP } @@ -641,7 +643,7 @@ OUTER_LOOP: if rs.Proposal != nil && !prs.Proposal { // Proposal: share the proposal metadata with peer. { - logger.Debug("Sending proposal", "height", prs.Height, "round", prs.Round) + logger.Info("Sending proposal", "height", prs.Height, "round", prs.Round) if p2p.SendEnvelopeShim(peer, p2p.Envelope{ //nolint: staticcheck ChannelID: DataChannel, Message: &cmtcons.Proposal{Proposal: *rs.Proposal.ToProto()}, @@ -1162,6 +1164,18 @@ func (ps *PeerState) SetHasProposalBlockPart(height int64, round int32, index in ps.PRS.ProposalBlockParts.SetIndex(index, true) } +// SetHasCompactBlock sets the given block part index as known for the peer. +func (ps *PeerState) SetHasBlock(height int64, round int32) { + ps.mtx.Lock() + defer ps.mtx.Unlock() + + if ps.PRS.Height != height || ps.PRS.Round != round { + return + } + + ps.PRS.Block = true +} + // PickSendVote picks a vote and sends it to the peer. // Returns true if vote was sent. func (ps *PeerState) PickSendVote(votes types.VoteSetReader) bool { @@ -1692,6 +1706,23 @@ func (m *ProposalPOLMessage) String() string { //------------------------------------- +// CompactBlockMessage is sent when gossipping a piece of the proposed block. +type CompactBlockMessage struct { + Block *types.Block +} + +// ValidateBasic performs basic validation. +func (m *CompactBlockMessage) ValidateBasic() error { + return m.Block.ValidateBasic() +} + +// String returns a string representation. +func (m *CompactBlockMessage) String() string { + return fmt.Sprintf("[CompactBlock H:%v]", m.Block.Height) +} + +//------------------------------------- + // BlockPartMessage is sent when gossipping a piece of the proposed block. type BlockPartMessage struct { Height int64 diff --git a/consensus/state.go b/consensus/state.go index efa0d63c36..5d61bc973e 100644 --- a/consensus/state.go +++ b/consensus/state.go @@ -869,6 +869,13 @@ func (cs *State) handleMsg(mi msgInfo) { // once proposal is set, we can receive block parts err = cs.setProposal(msg.Proposal) + case *CompactBlockMessage: + err = cs.addCompactBlock(msg, peerID) + + if err == nil { + cs.handleCompleteProposal(msg.Block.Height) + } + case *BlockPartMessage: // if the proposal is complete, we'll enterPrevote or tryFinalizeCommit added, err = cs.addProposalBlockPart(msg, peerID) @@ -1197,6 +1204,9 @@ func (cs *State) defaultDecideProposal(height int64, round int32) { if block == nil { return } + fmt.Println(block) + blockParts = block.MakePartSet(types.BlockPartSizeBytes) + fmt.Println("generated blockParts", blockParts.Header(), "squareSize", block.Data.SquareSize, "numTxs", len(block.Txs)) keys, err := cs.txFetcher.FetchKeysFromTxs(context.Background(), block.Txs.ToSliceOfBytes()) if err != nil { @@ -1205,7 +1215,8 @@ func (cs *State) defaultDecideProposal(height int64, round int32) { } block.Txs = types.ToTxs(keys) - blockParts = block.MakePartSet(types.BlockPartSizeBytes) + secondBlockParst := block.MakePartSet(types.BlockPartSizeBytes) + fmt.Println("normal block parts", blockParts.Header(), "compact block parts", secondBlockParst.Header()) } // Flush the WAL. Otherwise, we may not recompute the same proposal to sign, @@ -1223,6 +1234,7 @@ func (cs *State) defaultDecideProposal(height int64, round int32) { // send proposal and block parts on internal msg queue cs.sendInternalMessage(msgInfo{&ProposalMessage{proposal}, ""}) + cs.sendInternalMessage(msgInfo{&CompactBlockMessage{block}, ""}) for i := 0; i < int(blockParts.Total()); i++ { part := blockParts.GetPart(i) @@ -1932,7 +1944,95 @@ func (cs *State) defaultSetProposal(proposal *types.Proposal) error { cs.ProposalBlockParts = types.NewPartSetFromHeader(proposal.BlockID.PartSetHeader) } - cs.Logger.Info("received proposal", "proposal", proposal, "proposer", pubKey.Address()) + cs.Logger.Info("received proposal", "proposal", proposal, "partSetHeader", proposal.BlockID.PartSetHeader) + return nil +} + +func (cs *State) addCompactBlock(msg *CompactBlockMessage, peerID p2p.ID) error { + fmt.Println("received compact block message") + compactBlock := msg.Block + height := compactBlock.Height + + // Blocks might be reused, so round mismatch is OK + if cs.Height != height { + cs.Logger.Debug("received compact block from wrong height", "height", height) + cs.metrics.BlockGossipPartsReceived.With("matches_current", "false").Add(1) + return nil + } + + // We're not expecting a block part. + if cs.ProposalBlockParts == nil || cs.Proposal == nil { + cs.metrics.BlockGossipPartsReceived.With("matches_current", "false").Add(1) + // NOTE: this can happen when we've gone to a higher round and + // then receive parts from the previous round - not necessarily a bad peer. + cs.Logger.Debug( + "received a block part when we are not expecting any", + "height", height, + "peer", peerID, + ) + return nil + } + + blockHash := cs.Proposal.BlockID.Hash + timeout := cs.config.Propose(cs.Round) + + fmt.Println("fetching txs from keys") + + // Yield the lock while we fetch the transactions from the mempool so that votes + // and other operations can be processed. + cs.mtx.Unlock() + + ctx, cancel := context.WithTimeout(context.Background(), timeout) + defer cancel() + + txs, err := cs.txFetcher.FetchTxsFromKeys(ctx, blockHash, compactBlock.Data.Txs.ToSliceOfBytes()) + + cs.mtx.Lock() + if err != nil { + cs.jsonMetrics.CompactBlockFailures++ + if ctx.Err() != nil { + return nil + } + cs.Logger.Error("failed to fetch transactions for compact block", "err", err) + return err + } + block := &types.Block{ + Header: compactBlock.Header, + LastCommit: compactBlock.LastCommit, + Evidence: compactBlock.Evidence, + } + block.Data, _ = types.DataFromProto(&cmtproto.Data{ + Txs: txs, + Hash: block.Header.DataHash, + }) + + if err := block.ValidateBasic(); err != nil { + return fmt.Errorf("received invalid block: %w", err) + } + + if !bytes.Equal(block.Hash(), cs.Proposal.BlockID.Hash) { + return fmt.Errorf("received compact block with header hash [%v] that does not match proposal [%v]", block.Hash(), cs.Proposal.BlockID.Hash) + } + + // check that the part set header matched that of the + fmt.Println(block) + partSet := block.MakePartSet(types.BlockPartSizeBytes) + fmt.Println(partSet.Header()) + if !partSet.HasHeader(cs.Proposal.BlockID.PartSetHeader) { + return fmt.Errorf("received compact block with part set header [%v] that does not match proposal [%v]", partSet.Header(), cs.Proposal.BlockID.PartSetHeader) + } + + cs.ProposalCompactBlock = compactBlock + cs.ProposalBlock = block + cs.ProposalBlockParts = partSet + + // NOTE: it's possible to receive complete proposal blocks for future rounds without having the proposal + cs.Logger.Info("assembled proposal block from compact block", "height", cs.ProposalBlock.Height, "hash", cs.ProposalBlock.Hash()) + + if err := cs.eventBus.PublishEventCompleteProposal(cs.CompleteProposalEvent()); err != nil { + cs.Logger.Error("failed publishing event complete proposal", "err", err) + } + return nil } @@ -1995,25 +2095,7 @@ func (cs *State) addProposalBlockPart(msg *BlockPartMessage, peerID p2p.ID) (add if err != nil { return added, err } - blockHash := cs.ProposalBlockParts.Header().Hash - timeout := cs.config.Propose(round) - // Yield the lock while we fetch the transactions from the mempool so that votes - // and other operations can be processed. - cs.mtx.Unlock() - - ctx, cancel := context.WithTimeout(context.Background(), timeout) - defer cancel() - - txs, err := cs.txFetcher.FetchTxsFromKeys(ctx, blockHash, block.Data.Txs.ToSliceOfBytes()) - - cs.mtx.Lock() - if err != nil { - cs.Logger.Error("failed to fetch transactions for compact block", "err", err) - cs.jsonMetrics.CompactBlockFailures++ - return true, err - } - block.Data.Txs = types.ToTxs(txs) cs.ProposalBlock = block // NOTE: it's possible to receive complete proposal blocks for future rounds without having the proposal diff --git a/consensus/types/peer_round_state.go b/consensus/types/peer_round_state.go index 07283c5b4d..5eacaf9057 100644 --- a/consensus/types/peer_round_state.go +++ b/consensus/types/peer_round_state.go @@ -21,7 +21,10 @@ type PeerRoundState struct { StartTime time.Time `json:"start_time"` // True if peer has proposal for this round - Proposal bool `json:"proposal"` + Proposal bool `json:"proposal"` + + // True if peer has block + Block bool `json:"block"` ProposalBlockPartSetHeader types.PartSetHeader `json:"proposal_block_part_set_header"` ProposalBlockParts *bits.BitArray `json:"proposal_block_parts"` // Proposal's POL round. -1 if none. diff --git a/consensus/types/round_state.go b/consensus/types/round_state.go index 3b11c2e174..1030b2ae91 100644 --- a/consensus/types/round_state.go +++ b/consensus/types/round_state.go @@ -71,14 +71,15 @@ type RoundState struct { StartTime time.Time `json:"start_time"` // Subjective time when +2/3 precommits for Block at Round were found - CommitTime time.Time `json:"commit_time"` - Validators *types.ValidatorSet `json:"validators"` - Proposal *types.Proposal `json:"proposal"` - ProposalBlock *types.Block `json:"proposal_block"` - ProposalBlockParts *types.PartSet `json:"proposal_block_parts"` - LockedRound int32 `json:"locked_round"` - LockedBlock *types.Block `json:"locked_block"` - LockedBlockParts *types.PartSet `json:"locked_block_parts"` + CommitTime time.Time `json:"commit_time"` + Validators *types.ValidatorSet `json:"validators"` + Proposal *types.Proposal `json:"proposal"` + ProposalCompactBlock *types.Block `json:"proposal_compact_block"` + ProposalBlock *types.Block `json:"proposal_block"` + ProposalBlockParts *types.PartSet `json:"proposal_block_parts"` + LockedRound int32 `json:"locked_round"` + LockedBlock *types.Block `json:"locked_block"` + LockedBlockParts *types.PartSet `json:"locked_block_parts"` // Last known round with POL for non-nil valid block. TwoThirdPrevoteRound int32 `json:"valid_round"` diff --git a/proto/tendermint/consensus/message.go b/proto/tendermint/consensus/message.go index 51ac3b48f5..1bc10a0915 100644 --- a/proto/tendermint/consensus/message.go +++ b/proto/tendermint/consensus/message.go @@ -16,6 +16,7 @@ var _ p2p.Wrapper = &NewValidBlock{} var _ p2p.Wrapper = &NewRoundStep{} var _ p2p.Wrapper = &HasVote{} var _ p2p.Wrapper = &BlockPart{} +var _ p2p.Wrapper = &CompactBlock{} func (m *VoteSetBits) Wrap() proto.Message { cm := &Message{} @@ -42,6 +43,12 @@ func (m *Vote) Wrap() proto.Message { return cm } +func (m *CompactBlock) Wrap() proto.Message { + cm := &Message{} + cm.Sum = &Message_CompactBlock{CompactBlock: m} + return cm +} + func (m *BlockPart) Wrap() proto.Message { cm := &Message{} cm.Sum = &Message_BlockPart{BlockPart: m} @@ -88,6 +95,9 @@ func (m *Message) Unwrap() (proto.Message, error) { case *Message_ProposalPol: return m.GetProposalPol(), nil + case *Message_CompactBlock: + return m.GetCompactBlock(), nil + case *Message_BlockPart: return m.GetBlockPart(), nil diff --git a/proto/tendermint/consensus/types.pb.go b/proto/tendermint/consensus/types.pb.go index 98550a3b26..f24e21fb7a 100644 --- a/proto/tendermint/consensus/types.pb.go +++ b/proto/tendermint/consensus/types.pb.go @@ -288,7 +288,52 @@ func (m *ProposalPOL) GetProposalPol() bits.BitArray { return bits.BitArray{} } -// BlockPart is sent when gossipping a piece of the proposed block. +// CompactBlock is for gossiping a block +type CompactBlock struct { + Block *types.Block `protobuf:"bytes,1,opt,name=block,proto3" json:"block,omitempty"` +} + +func (m *CompactBlock) Reset() { *m = CompactBlock{} } +func (m *CompactBlock) String() string { return proto.CompactTextString(m) } +func (*CompactBlock) ProtoMessage() {} +func (*CompactBlock) Descriptor() ([]byte, []int) { + return fileDescriptor_81a22d2efc008981, []int{4} +} +func (m *CompactBlock) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *CompactBlock) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_CompactBlock.Marshal(b, m, deterministic) + } else { + b = b[:cap(b)] + n, err := m.MarshalToSizedBuffer(b) + if err != nil { + return nil, err + } + return b[:n], nil + } +} +func (m *CompactBlock) XXX_Merge(src proto.Message) { + xxx_messageInfo_CompactBlock.Merge(m, src) +} +func (m *CompactBlock) XXX_Size() int { + return m.Size() +} +func (m *CompactBlock) XXX_DiscardUnknown() { + xxx_messageInfo_CompactBlock.DiscardUnknown(m) +} + +var xxx_messageInfo_CompactBlock proto.InternalMessageInfo + +func (m *CompactBlock) GetBlock() *types.Block { + if m != nil { + return m.Block + } + return nil +} + +// BlockPart is sent when catching up type BlockPart struct { Height int64 `protobuf:"varint,1,opt,name=height,proto3" json:"height,omitempty"` Round int32 `protobuf:"varint,2,opt,name=round,proto3" json:"round,omitempty"` @@ -299,7 +344,7 @@ func (m *BlockPart) Reset() { *m = BlockPart{} } func (m *BlockPart) String() string { return proto.CompactTextString(m) } func (*BlockPart) ProtoMessage() {} func (*BlockPart) Descriptor() ([]byte, []int) { - return fileDescriptor_81a22d2efc008981, []int{4} + return fileDescriptor_81a22d2efc008981, []int{5} } func (m *BlockPart) XXX_Unmarshal(b []byte) error { return m.Unmarshal(b) @@ -358,7 +403,7 @@ func (m *Vote) Reset() { *m = Vote{} } func (m *Vote) String() string { return proto.CompactTextString(m) } func (*Vote) ProtoMessage() {} func (*Vote) Descriptor() ([]byte, []int) { - return fileDescriptor_81a22d2efc008981, []int{5} + return fileDescriptor_81a22d2efc008981, []int{6} } func (m *Vote) XXX_Unmarshal(b []byte) error { return m.Unmarshal(b) @@ -406,7 +451,7 @@ func (m *HasVote) Reset() { *m = HasVote{} } func (m *HasVote) String() string { return proto.CompactTextString(m) } func (*HasVote) ProtoMessage() {} func (*HasVote) Descriptor() ([]byte, []int) { - return fileDescriptor_81a22d2efc008981, []int{6} + return fileDescriptor_81a22d2efc008981, []int{7} } func (m *HasVote) XXX_Unmarshal(b []byte) error { return m.Unmarshal(b) @@ -475,7 +520,7 @@ func (m *VoteSetMaj23) Reset() { *m = VoteSetMaj23{} } func (m *VoteSetMaj23) String() string { return proto.CompactTextString(m) } func (*VoteSetMaj23) ProtoMessage() {} func (*VoteSetMaj23) Descriptor() ([]byte, []int) { - return fileDescriptor_81a22d2efc008981, []int{7} + return fileDescriptor_81a22d2efc008981, []int{8} } func (m *VoteSetMaj23) XXX_Unmarshal(b []byte) error { return m.Unmarshal(b) @@ -545,7 +590,7 @@ func (m *VoteSetBits) Reset() { *m = VoteSetBits{} } func (m *VoteSetBits) String() string { return proto.CompactTextString(m) } func (*VoteSetBits) ProtoMessage() {} func (*VoteSetBits) Descriptor() ([]byte, []int) { - return fileDescriptor_81a22d2efc008981, []int{8} + return fileDescriptor_81a22d2efc008981, []int{9} } func (m *VoteSetBits) XXX_Unmarshal(b []byte) error { return m.Unmarshal(b) @@ -620,6 +665,7 @@ type Message struct { // *Message_HasVote // *Message_VoteSetMaj23 // *Message_VoteSetBits + // *Message_CompactBlock Sum isMessage_Sum `protobuf_oneof:"sum"` } @@ -627,7 +673,7 @@ func (m *Message) Reset() { *m = Message{} } func (m *Message) String() string { return proto.CompactTextString(m) } func (*Message) ProtoMessage() {} func (*Message) Descriptor() ([]byte, []int) { - return fileDescriptor_81a22d2efc008981, []int{9} + return fileDescriptor_81a22d2efc008981, []int{10} } func (m *Message) XXX_Unmarshal(b []byte) error { return m.Unmarshal(b) @@ -689,6 +735,9 @@ type Message_VoteSetMaj23 struct { type Message_VoteSetBits struct { VoteSetBits *VoteSetBits `protobuf:"bytes,9,opt,name=vote_set_bits,json=voteSetBits,proto3,oneof" json:"vote_set_bits,omitempty"` } +type Message_CompactBlock struct { + CompactBlock *CompactBlock `protobuf:"bytes,10,opt,name=compact_block,json=compactBlock,proto3,oneof" json:"compact_block,omitempty"` +} func (*Message_NewRoundStep) isMessage_Sum() {} func (*Message_NewValidBlock) isMessage_Sum() {} @@ -699,6 +748,7 @@ func (*Message_Vote) isMessage_Sum() {} func (*Message_HasVote) isMessage_Sum() {} func (*Message_VoteSetMaj23) isMessage_Sum() {} func (*Message_VoteSetBits) isMessage_Sum() {} +func (*Message_CompactBlock) isMessage_Sum() {} func (m *Message) GetSum() isMessage_Sum { if m != nil { @@ -770,6 +820,13 @@ func (m *Message) GetVoteSetBits() *VoteSetBits { return nil } +func (m *Message) GetCompactBlock() *CompactBlock { + if x, ok := m.GetSum().(*Message_CompactBlock); ok { + return x.CompactBlock + } + return nil +} + // XXX_OneofWrappers is for the internal use of the proto package. func (*Message) XXX_OneofWrappers() []interface{} { return []interface{}{ @@ -782,6 +839,7 @@ func (*Message) XXX_OneofWrappers() []interface{} { (*Message_HasVote)(nil), (*Message_VoteSetMaj23)(nil), (*Message_VoteSetBits)(nil), + (*Message_CompactBlock)(nil), } } @@ -790,6 +848,7 @@ func init() { proto.RegisterType((*NewValidBlock)(nil), "tendermint.consensus.NewValidBlock") proto.RegisterType((*Proposal)(nil), "tendermint.consensus.Proposal") proto.RegisterType((*ProposalPOL)(nil), "tendermint.consensus.ProposalPOL") + proto.RegisterType((*CompactBlock)(nil), "tendermint.consensus.CompactBlock") proto.RegisterType((*BlockPart)(nil), "tendermint.consensus.BlockPart") proto.RegisterType((*Vote)(nil), "tendermint.consensus.Vote") proto.RegisterType((*HasVote)(nil), "tendermint.consensus.HasVote") @@ -801,61 +860,63 @@ func init() { func init() { proto.RegisterFile("tendermint/consensus/types.proto", fileDescriptor_81a22d2efc008981) } var fileDescriptor_81a22d2efc008981 = []byte{ - // 853 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xd4, 0x56, 0x4f, 0x8f, 0xdb, 0x44, - 0x14, 0xb7, 0x59, 0x67, 0x93, 0x7d, 0xde, 0xec, 0xc2, 0x68, 0x5b, 0x85, 0x00, 0x49, 0x30, 0x97, - 0x15, 0x42, 0x0e, 0xca, 0x1e, 0x90, 0x0a, 0x12, 0x60, 0xfe, 0xd4, 0xad, 0x9a, 0x36, 0x72, 0x4a, - 0x85, 0xb8, 0x58, 0x4e, 0x3c, 0x4a, 0x86, 0xc6, 0x1e, 0xcb, 0x33, 0xc9, 0xb2, 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0x6f, 0x2c, 0x23, 0xba, 0x90, 0x75, 0x41, 0x77, - 0xe1, 0x50, 0xf7, 0x56, 0x74, 0xfe, 0xcb, 0xf5, 0x55, 0x6e, 0xb6, 0x86, 0xe6, 0x3c, 0x85, 0x03, - 0x6f, 0xc3, 0xc9, 0x0d, 0x6b, 0xfb, 0x21, 0x58, 0x82, 0x7b, 0x75, 0xf6, 0xed, 0xea, 0x52, 0xaa, - 0x33, 0xa5, 0xa7, 0x33, 0x00, 0xeb, 0x09, 0xe5, 0xa2, 0x03, 0xad, 0x15, 0xe5, 0x58, 0xb1, 0x59, - 0x11, 0x29, 0xbc, 0x02, 0xe9, 0xe3, 0xfc, 0x68, 0x42, 0xdd, 0x8f, 0x98, 0x8c, 0xbb, 0x59, 0x7e, - 0x67, 0x60, 0x09, 0x34, 0x99, 0xdf, 0x51, 0x55, 0xab, 0x8d, 0xc9, 0x2c, 0xc5, 0xf1, 0x90, 0xcd, - 0x1e, 0x5f, 0x64, 0x38, 0x90, 0xce, 0x02, 0x8a, 0xa4, 0x31, 0xfe, 0x41, 0x36, 0x54, 0x2d, 0x28, - 0x36, 0xce, 0xaf, 0x26, 0x1c, 0x8a, 0x0c, 0xc6, 0x98, 0x0f, 0xa3, 0xef, 0x07, 0x67, 0xff, 0x47, - 0x26, 0x5f, 0x41, 0xa3, 0x68, 0x70, 0x12, 0xab, 0xee, 0x7e, 0xf3, 0xe5, 0x40, 0x59, 0xbb, 0x7b, - 0x5f, 0x7a, 0xc7, 0x82, 0xe5, 0xf5, 0x8b, 0x6e, 0x5d, 0x7d, 0x08, 0xea, 0x32, 0xf6, 0x5e, 0xec, - 0xfc, 0x61, 0x82, 0xad, 0x52, 0xf7, 0x08, 0x67, 0xaf, 0x4e, 0xe6, 0xe8, 0x0e, 0xd4, 0x44, 0x07, - 0x30, 0x39, 0x9c, 0xff, 0xb5, 0xb9, 0x8b, 0x10, 0xe7, 0x2f, 0x0b, 0xea, 0x43, 0xcc, 0x58, 0x34, - 0x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0x67, 0xeb, 0x8e, 0xf9, 0x7c, 0xdd, 0x31, 0x7f, 0x5f, - 0x77, 0xcc, 0x9f, 0x2e, 0x3b, 0xc6, 0xf3, 0xcb, 0x8e, 0xf1, 0xdb, 0x65, 0xc7, 0xf8, 0xee, 0xe3, - 0x19, 0xe1, 0xf3, 0xe5, 0xc4, 0x9d, 0xd2, 0xa4, 0xaf, 0xbf, 0x26, 0xca, 0x65, 0xf1, 0xea, 0xa8, - 0x7a, 0xb7, 0x4c, 0xf6, 0xa5, 0xed, 0xec, 0xef, 0x00, 0x00, 0x00, 0xff, 0xff, 0xc7, 0x5c, 0x91, - 0x04, 0xd6, 0x08, 0x00, 0x00, + // 896 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xd4, 0x56, 0x4f, 0x6f, 0xe3, 0x44, + 0x14, 0xb7, 0xb7, 0x49, 0x93, 0xbe, 0x24, 0x2d, 0x8c, 0xba, 0x4b, 0x28, 0x90, 0x16, 0x73, 0xa9, + 0x10, 0x24, 0x28, 0x3d, 0x20, 0x2d, 0x20, 0x20, 0x0b, 0xac, 0xbb, 0xda, 0xee, 0x46, 0xce, 0xb2, + 0x42, 0x5c, 0x2c, 0xc7, 0x1e, 0x25, 0xc3, 0xc6, 0x1e, 0xcb, 0x33, 0x6d, 0xe9, 0x95, 0x4f, 0xc0, + 0x07, 0xe0, 0x6b, 0x20, 0x71, 0xe5, 0xb6, 0xc7, 0x3d, 0x72, 0x5a, 0xa1, 0xf6, 0x23, 0x20, 0xee, + 0x68, 0xde, 0x4c, 0x92, 0x29, 0x75, 0x03, 0xbd, 0x20, 0x71, 0x9b, 0xf1, 0x7b, 0xef, 0xe7, 0xdf, + 0xfb, 0xf7, 0xb3, 0x61, 0x4f, 0xd2, 0x2c, 0xa1, 0x45, 0xca, 0x32, 0xd9, 0x8b, 0x79, 0x26, 0x68, + 0x26, 0x8e, 0x45, 0x4f, 0x9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0x3f, 0x5e, 0x74, + 0x9c, 0x17, 0x17, 0x1d, 0xe7, 0xb7, 0x8b, 0x8e, 0xf3, 0xed, 0x47, 0x13, 0x26, 0xa7, 0xc7, 0xe3, + 0x6e, 0xcc, 0xd3, 0x9e, 0xfd, 0x5f, 0xb3, 0x3c, 0xea, 0xbf, 0xa3, 0xb2, 0xff, 0xab, 0xf1, 0x3a, + 0xda, 0x0e, 0xfe, 0x0a, 0x00, 0x00, 0xff, 0xff, 0xf5, 0xdd, 0x83, 0xc3, 0x7e, 0x09, 0x00, 0x00, } func (m *NewRoundStep) Marshal() (dAtA []byte, err error) { @@ -1047,6 +1108,41 @@ func (m *ProposalPOL) MarshalToSizedBuffer(dAtA []byte) (int, error) { return len(dAtA) - i, nil } +func (m *CompactBlock) Marshal() (dAtA []byte, err error) { + size := m.Size() + dAtA = make([]byte, size) + n, err := m.MarshalToSizedBuffer(dAtA[:size]) + if err != nil { + return nil, err + } + return dAtA[:n], nil +} + +func (m *CompactBlock) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *CompactBlock) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + if m.Block != nil { + { + size, err := m.Block.MarshalToSizedBuffer(dAtA[:i]) + if err != nil { + return 0, err + } + i -= size + i = encodeVarintTypes(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0xa + } + return len(dAtA) - i, nil +} + func (m *BlockPart) Marshal() (dAtA []byte, err error) { size := m.Size() dAtA = make([]byte, size) @@ -1495,6 +1591,27 @@ func (m *Message_VoteSetBits) MarshalToSizedBuffer(dAtA []byte) (int, error) { } return len(dAtA) - i, nil } +func (m *Message_CompactBlock) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *Message_CompactBlock) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + if m.CompactBlock != nil { + { + size, err := m.CompactBlock.MarshalToSizedBuffer(dAtA[:i]) + if err != nil { + return 0, err + } + i -= size + i = encodeVarintTypes(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0x52 + } + return len(dAtA) - i, nil +} func encodeVarintTypes(dAtA []byte, offset int, v uint64) int { offset -= sovTypes(v) base := offset @@ -1582,6 +1699,19 @@ func (m *ProposalPOL) Size() (n int) { return n } +func (m *CompactBlock) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + if m.Block != nil { + l = m.Block.Size() + n += 1 + l + sovTypes(uint64(l)) + } + return n +} + func (m *BlockPart) Size() (n int) { if m == nil { return 0 @@ -1795,6 +1925,18 @@ func (m *Message_VoteSetBits) Size() (n int) { } return n } +func (m *Message_CompactBlock) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + if m.CompactBlock != nil { + l = m.CompactBlock.Size() + n += 1 + l + sovTypes(uint64(l)) + } + return n +} func sovTypes(x uint64) (n int) { return (math_bits.Len64(x|1) + 6) / 7 @@ -2328,6 +2470,92 @@ func (m *ProposalPOL) Unmarshal(dAtA []byte) error { } return nil } +func (m *CompactBlock) Unmarshal(dAtA []byte) error { + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTypes + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: CompactBlock: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: CompactBlock: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Block", wireType) + } + var msglen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTypes + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + msglen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if msglen < 0 { + return ErrInvalidLengthTypes + } + postIndex := iNdEx + msglen + if postIndex < 0 { + return ErrInvalidLengthTypes + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + if m.Block == nil { + m.Block = &types.Block{} + } + if err := m.Block.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex + default: + iNdEx = preIndex + skippy, err := skipTypes(dAtA[iNdEx:]) + if err != nil { + return err + } + if (skippy < 0) || (iNdEx+skippy) < 0 { + return ErrInvalidLengthTypes + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} func (m *BlockPart) Unmarshal(dAtA []byte) error { l := len(dAtA) iNdEx := 0 @@ -3318,6 +3546,41 @@ func (m *Message) Unmarshal(dAtA []byte) error { } m.Sum = &Message_VoteSetBits{v} iNdEx = postIndex + case 10: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field CompactBlock", wireType) + } + var msglen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTypes + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + msglen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if msglen < 0 { + return ErrInvalidLengthTypes + } + postIndex := iNdEx + msglen + if postIndex < 0 { + return ErrInvalidLengthTypes + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + v := &CompactBlock{} + if err := v.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + m.Sum = &Message_CompactBlock{v} + iNdEx = postIndex default: iNdEx = preIndex skippy, err := skipTypes(dAtA[iNdEx:]) diff --git a/proto/tendermint/consensus/types.proto b/proto/tendermint/consensus/types.proto index 5048f85452..8b483fd92f 100644 --- a/proto/tendermint/consensus/types.proto +++ b/proto/tendermint/consensus/types.proto @@ -5,6 +5,7 @@ option go_package = "github.com/tendermint/tendermint/proto/tendermint/consensus import "gogoproto/gogo.proto"; import "tendermint/types/types.proto"; +import "tendermint/types/block.proto"; import "tendermint/libs/bits/types.proto"; // NewRoundStep is sent for every step taken in the ConsensusState. @@ -40,7 +41,12 @@ message ProposalPOL { tendermint.libs.bits.BitArray proposal_pol = 3 [(gogoproto.nullable) = false]; } -// BlockPart is sent when gossipping a piece of the proposed block. +// CompactBlock is for gossiping a block +message CompactBlock { + tendermint.types.Block block = 1; +} + +// BlockPart is sent when catching up message BlockPart { int64 height = 1; int32 round = 2; @@ -88,5 +94,6 @@ message Message { HasVote has_vote = 7; VoteSetMaj23 vote_set_maj23 = 8; VoteSetBits vote_set_bits = 9; + CompactBlock compact_block = 10; } } From 567cf3f893d821843a1eb1b2f4efacbf8b3df853 Mon Sep 17 00:00:00 2001 From: Callum Waters <cmwaters19@gmail.com> Date: Wed, 24 Jan 2024 17:54:20 -0600 Subject: [PATCH 003/127] checkpoint --- consensus/reactor.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/consensus/reactor.go b/consensus/reactor.go index f199bfca53..67f6e50e68 100644 --- a/consensus/reactor.go +++ b/consensus/reactor.go @@ -361,8 +361,6 @@ func (conR *Reactor) ReceiveEnvelope(e p2p.Envelope) { ps.EnsureVoteBitArrays(height, valSize) ps.EnsureVoteBitArrays(height-1, lastCommitSize) ps.SetHasVote(msg.Vote) - // If we receive a vote we can deduce that an honest peer also has the block - ps.SetHasBlock(msg.Vote.Height, msg.Vote.Round) cs.peerMsgQueue <- msgInfo{msg, e.Src.ID()} From 22639ab69502e00a6321c31191e6a7c971e75ace Mon Sep 17 00:00:00 2001 From: Callum Waters <cmwaters19@gmail.com> Date: Thu, 25 Jan 2024 10:01:35 -0600 Subject: [PATCH 004/127] checkpoint --- consensus/byzantine_test.go | 10 ++++++++-- consensus/reactor.go | 2 +- consensus/state.go | 12 ++++++++++-- consensus/types/peer_round_state.go | 2 ++ 4 files changed, 21 insertions(+), 5 deletions(-) diff --git a/consensus/byzantine_test.go b/consensus/byzantine_test.go index 25bdc46261..4b96e343ad 100644 --- a/consensus/byzantine_test.go +++ b/consensus/byzantine_test.go @@ -219,8 +219,13 @@ func TestByzantinePrevoteEquivocation(t *testing.T) { block := lazyProposer.blockExec.CreateProposalBlock( lazyProposer.Height, lazyProposer.state, commit, proposerAddr, ) + blockHash := block.Hash() blockParts := block.MakePartSet(types.BlockPartSizeBytes) + keys, err := lazyProposer.txFetcher.FetchKeysFromTxs(context.Background(), block.Txs.ToSliceOfBytes()) + require.NoError(t, err) + block.Txs = types.ToTxs(keys) + // Flush the WAL. Otherwise, we may not recompute the same proposal to sign, // and the privValidator will refuse to sign anything. if err := lazyProposer.wal.FlushAndSync(); err != nil { @@ -228,7 +233,7 @@ func TestByzantinePrevoteEquivocation(t *testing.T) { } // Make proposal - propBlockID := types.BlockID{Hash: block.Hash(), PartSetHeader: blockParts.Header()} + propBlockID := types.BlockID{Hash: blockHash, PartSetHeader: blockParts.Header()} proposal := types.NewProposal(height, round, lazyProposer.TwoThirdPrevoteRound, propBlockID) p := proposal.ToProto() if err := lazyProposer.privValidator.SignProposal(lazyProposer.state.ChainID, p); err == nil { @@ -236,6 +241,7 @@ func TestByzantinePrevoteEquivocation(t *testing.T) { // send proposal and block parts on internal msg queue lazyProposer.sendInternalMessage(msgInfo{&ProposalMessage{proposal}, ""}) + lazyProposer.sendInternalMessage(msgInfo{&CompactBlockMessage{block}, ""}) for i := 0; i < int(blockParts.Total()); i++ { part := blockParts.GetPart(i) lazyProposer.sendInternalMessage(msgInfo{&BlockPartMessage{lazyProposer.Height, lazyProposer.Round, part}, ""}) @@ -294,7 +300,7 @@ func TestByzantinePrevoteEquivocation(t *testing.T) { } case <-time.After(20 * time.Second): for i, reactor := range reactors { - t.Logf("Consensus Reactor %d\n%v", i, reactor) + t.Logf("Consensus Reactor %d\n%v", i, reactor.conS.GetRoundState()) } t.Fatalf("Timed out waiting for validators to commit evidence") } diff --git a/consensus/reactor.go b/consensus/reactor.go index 67f6e50e68..eadc192488 100644 --- a/consensus/reactor.go +++ b/consensus/reactor.go @@ -583,7 +583,6 @@ OUTER_LOOP: // Send compact block if !prs.Block && rs.ProposalBlockParts.HasHeader(prs.ProposalBlockPartSetHeader) { - logger.Info("Peer has proposal but not block", "height", prs.Height, "round", prs.Round) if rs.ProposalCompactBlock != nil { compactBlock, err := rs.ProposalCompactBlock.ToProto() if err != nil { @@ -1414,6 +1413,7 @@ func (ps *PeerState) ApplyNewRoundStepMessage(msg *NewRoundStepMessage) { ps.PRS.StartTime = startTime if psHeight != msg.Height || psRound != msg.Round { ps.PRS.Proposal = false + ps.PRS.Block = false ps.PRS.ProposalBlockPartSetHeader = types.PartSetHeader{} ps.PRS.ProposalBlockParts = nil ps.PRS.ProposalPOLRound = -1 diff --git a/consensus/state.go b/consensus/state.go index 5d61bc973e..080e59382d 100644 --- a/consensus/state.go +++ b/consensus/state.go @@ -1193,11 +1193,13 @@ func (cs *State) isProposer(address []byte) bool { func (cs *State) defaultDecideProposal(height int64, round int32) { var block *types.Block var blockParts *types.PartSet + var blockHash []byte // Decide on block if cs.TwoThirdPrevoteBlock != nil { // If there is valid block, choose that. block, blockParts = cs.TwoThirdPrevoteBlock, cs.TwoThirdPrevoteBlockParts + blockHash = block.Hash() } else { // Create a new proposal block from state/txs from the mempool. block = cs.createProposalBlock() @@ -1206,6 +1208,7 @@ func (cs *State) defaultDecideProposal(height int64, round int32) { } fmt.Println(block) blockParts = block.MakePartSet(types.BlockPartSizeBytes) + blockHash = block.Hash() fmt.Println("generated blockParts", blockParts.Header(), "squareSize", block.Data.SquareSize, "numTxs", len(block.Txs)) keys, err := cs.txFetcher.FetchKeysFromTxs(context.Background(), block.Txs.ToSliceOfBytes()) @@ -1226,7 +1229,7 @@ func (cs *State) defaultDecideProposal(height int64, round int32) { } // Make proposal - propBlockID := types.BlockID{Hash: block.Hash(), PartSetHeader: blockParts.Header()} + propBlockID := types.BlockID{Hash: blockHash, PartSetHeader: blockParts.Header()} proposal := types.NewProposal(height, round, cs.TwoThirdPrevoteRound, propBlockID) p := proposal.ToProto() if err := cs.privValidator.SignProposal(cs.state.ChainID, p); err == nil { @@ -1953,6 +1956,11 @@ func (cs *State) addCompactBlock(msg *CompactBlockMessage, peerID p2p.ID) error compactBlock := msg.Block height := compactBlock.Height + if cs.ProposalBlock != nil { + // We already have the proposal block. + return nil + } + // Blocks might be reused, so round mismatch is OK if cs.Height != height { cs.Logger.Debug("received compact block from wrong height", "height", height) @@ -2450,7 +2458,7 @@ func (cs *State) signAddVote(msgType cmtproto.SignedMsgType, hash []byte, header vote, err := cs.signVote(msgType, hash, header) if err == nil { cs.sendInternalMessage(msgInfo{&VoteMessage{vote}, ""}) - cs.Logger.Debug("signed and pushed vote", "height", cs.Height, "round", cs.Round, "vote", vote) + cs.Logger.Info("signed and pushed vote", "height", cs.Height, "round", cs.Round, "vote", vote) return vote } diff --git a/consensus/types/peer_round_state.go b/consensus/types/peer_round_state.go index 5eacaf9057..ca85f03005 100644 --- a/consensus/types/peer_round_state.go +++ b/consensus/types/peer_round_state.go @@ -54,6 +54,7 @@ func (prs PeerRoundState) StringIndented(indent string) string { return fmt.Sprintf(`PeerRoundState{ %s %v/%v/%v @%v %s Proposal %v -> %v +%s Block %v %s POL %v (round %v) %s Prevotes %v %s Precommits %v @@ -62,6 +63,7 @@ func (prs PeerRoundState) StringIndented(indent string) string { %s}`, indent, prs.Height, prs.Round, prs.Step, prs.StartTime, indent, prs.ProposalBlockPartSetHeader, prs.ProposalBlockParts, + indent, prs.Block, indent, prs.ProposalPOL, prs.ProposalPOLRound, indent, prs.Prevotes, indent, prs.Precommits, From d4f62fd824f6107392e83446cf684e9a78e8f638 Mon Sep 17 00:00:00 2001 From: Callum Waters <cmwaters19@gmail.com> Date: Thu, 25 Jan 2024 17:52:35 -0600 Subject: [PATCH 005/127] fix e2e tests --- consensus/reactor.go | 2 +- consensus/state.go | 15 ++++----------- mempool/cat/block_builder.go | 11 +++++++++++ mempool/cat/block_builder_test.go | 2 +- mempool/cat/pool.go | 14 ++++++++++++-- mempool/cat/reactor.go | 8 +++++--- mempool/metrics.go | 2 +- test/e2e/docker/Dockerfile | 2 +- test/e2e/networks/ci.toml | 4 ++-- test/e2e/networks/simple.toml | 3 +++ test/e2e/runner/rpc.go | 2 +- test/e2e/runner/setup.go | 1 + 12 files changed, 43 insertions(+), 23 deletions(-) diff --git a/consensus/reactor.go b/consensus/reactor.go index eadc192488..212a9d4238 100644 --- a/consensus/reactor.go +++ b/consensus/reactor.go @@ -344,7 +344,7 @@ func (conR *Reactor) ReceiveEnvelope(e p2p.Envelope) { schema.WriteBlockPart(conR.traceClient, msg.Height, msg.Round, e.Src.ID(), msg.Part.Index, schema.TransferTypeDownload) conR.conS.peerMsgQueue <- msgInfo{msg, e.Src.ID()} default: - conR.Logger.Error(fmt.Sprintf("Unknown message type %v", reflect.TypeOf(msg))) + conR.Logger.Error(fmt.Sprintf("Unknown message type on data channel %v", reflect.TypeOf(msg))) } case VoteChannel: diff --git a/consensus/state.go b/consensus/state.go index 080e59382d..cd98393519 100644 --- a/consensus/state.go +++ b/consensus/state.go @@ -716,6 +716,7 @@ func (cs *State) updateToState(state sm.State) { cs.Proposal = nil cs.ProposalBlock = nil cs.ProposalBlockParts = nil + cs.ProposalCompactBlock = nil cs.LockedRound = -1 cs.LockedBlock = nil cs.LockedBlockParts = nil @@ -1206,10 +1207,8 @@ func (cs *State) defaultDecideProposal(height int64, round int32) { if block == nil { return } - fmt.Println(block) blockParts = block.MakePartSet(types.BlockPartSizeBytes) blockHash = block.Hash() - fmt.Println("generated blockParts", blockParts.Header(), "squareSize", block.Data.SquareSize, "numTxs", len(block.Txs)) keys, err := cs.txFetcher.FetchKeysFromTxs(context.Background(), block.Txs.ToSliceOfBytes()) if err != nil { @@ -1218,8 +1217,6 @@ func (cs *State) defaultDecideProposal(height int64, round int32) { } block.Txs = types.ToTxs(keys) - secondBlockParst := block.MakePartSet(types.BlockPartSizeBytes) - fmt.Println("normal block parts", blockParts.Header(), "compact block parts", secondBlockParst.Header()) } // Flush the WAL. Otherwise, we may not recompute the same proposal to sign, @@ -1952,18 +1949,17 @@ func (cs *State) defaultSetProposal(proposal *types.Proposal) error { } func (cs *State) addCompactBlock(msg *CompactBlockMessage, peerID p2p.ID) error { - fmt.Println("received compact block message") compactBlock := msg.Block height := compactBlock.Height if cs.ProposalBlock != nil { // We already have the proposal block. - return nil + return nil } // Blocks might be reused, so round mismatch is OK if cs.Height != height { - cs.Logger.Debug("received compact block from wrong height", "height", height) + cs.Logger.Debug("received compact block from wrong height", "height", height, "currentHeight", cs.Height) cs.metrics.BlockGossipPartsReceived.With("matches_current", "false").Add(1) return nil } @@ -1984,8 +1980,6 @@ func (cs *State) addCompactBlock(msg *CompactBlockMessage, peerID p2p.ID) error blockHash := cs.Proposal.BlockID.Hash timeout := cs.config.Propose(cs.Round) - fmt.Println("fetching txs from keys") - // Yield the lock while we fetch the transactions from the mempool so that votes // and other operations can be processed. cs.mtx.Unlock() @@ -1999,6 +1993,7 @@ func (cs *State) addCompactBlock(msg *CompactBlockMessage, peerID p2p.ID) error if err != nil { cs.jsonMetrics.CompactBlockFailures++ if ctx.Err() != nil { + cs.Logger.Info("failed to fetch transactions within the timeout", "timeout", timeout) return nil } cs.Logger.Error("failed to fetch transactions for compact block", "err", err) @@ -2023,9 +2018,7 @@ func (cs *State) addCompactBlock(msg *CompactBlockMessage, peerID p2p.ID) error } // check that the part set header matched that of the - fmt.Println(block) partSet := block.MakePartSet(types.BlockPartSizeBytes) - fmt.Println(partSet.Header()) if !partSet.HasHeader(cs.Proposal.BlockID.PartSetHeader) { return fmt.Errorf("received compact block with part set header [%v] that does not match proposal [%v]", partSet.Header(), cs.Proposal.BlockID.PartSetHeader) } diff --git a/mempool/cat/block_builder.go b/mempool/cat/block_builder.go index ee6722a812..648f87d6fe 100644 --- a/mempool/cat/block_builder.go +++ b/mempool/cat/block_builder.go @@ -68,6 +68,11 @@ func (memR *Reactor) FetchTxsFromKeys(ctx context.Context, blockID []byte, compa memR.mempool.jsonMetrics.Unlock() }() + // request the missing transactions if we haven't already + for _, key := range missingKeys { + memR.findNewPeerToRequestTx(key) + } + // Wait for the reactor to retrieve and post all transactions. return request.WaitForBlock(ctx) } @@ -261,3 +266,9 @@ func (br *blockRequest) TimeTaken() uint64 { } return uint64(br.endTime.Sub(br.startTime).Milliseconds()) } + +func (br *blockRequest) NewTxsFound() string { + br.mtx.Lock() + defer br.mtx.Unlock() + return fmt.Sprintf("BlockRequest{height:%d, missing:%d}", br.height, len(br.missingKeys)) +} diff --git a/mempool/cat/block_builder_test.go b/mempool/cat/block_builder_test.go index ec0089da85..dd015eb8dd 100644 --- a/mempool/cat/block_builder_test.go +++ b/mempool/cat/block_builder_test.go @@ -8,12 +8,12 @@ import ( "testing" "time" + "github.com/stretchr/testify/require" "github.com/tendermint/tendermint/crypto/tmhash" "github.com/tendermint/tendermint/mempool" "github.com/tendermint/tendermint/p2p" memproto "github.com/tendermint/tendermint/proto/tendermint/mempool" "github.com/tendermint/tendermint/types" - "github.com/stretchr/testify/require" ) func TestBlockRequest(t *testing.T) { diff --git a/mempool/cat/pool.go b/mempool/cat/pool.go index 6c98f74169..c84f225c91 100644 --- a/mempool/cat/pool.go +++ b/mempool/cat/pool.go @@ -64,6 +64,9 @@ type TxPool struct { postCheckFn mempool.PostCheckFunc height int64 // the latest height passed to Update lastPurgeTime time.Time // the last time we attempted to purge transactions via the TTL + // cache of committed transactions to be pruned the following height + // we don't want to prune immediately because lagging nodes may still be requesting these transactions + committedCache map[types.TxKey]struct{} // Thread-safe cache of rejected transactions for quick look-up rejectedTxCache *LRUTxCache @@ -497,9 +500,16 @@ func (txmp *TxPool) Update( txmp.updateMtx.Unlock() txmp.metrics.SuccessfulTxs.Add(float64(len(blockTxs))) + + for txKey := range txmp.committedCache { + // Remove the transaction from the mempool. + txmp.removeTxByKey(txKey) + } + + // add the recently committed transactions to the cache + txmp.committedCache = make(map[types.TxKey]struct{}) for _, tx := range blockTxs { - // Regardless of success, remove the transaction from the mempool. - txmp.removeTxByKey(tx.Key()) + txmp.committedCache[tx.Key()] = struct{}{} } txmp.purgeExpiredTxs(blockHeight) diff --git a/mempool/cat/reactor.go b/mempool/cat/reactor.go index 6a566e64d0..00d5dedcde 100644 --- a/mempool/cat/reactor.go +++ b/mempool/cat/reactor.go @@ -451,7 +451,7 @@ func (memR *Reactor) requestTx(txKey types.TxKey, peer p2p.Peer) { memR.mempool.metrics.RequestedTxs.Add(1) requested := memR.requests.Add(txKey, memR.ids.GetIDForPeer(peer.ID()), memR.findNewPeerToRequestTx) if !requested { - memR.Logger.Error("have already marked a tx as requested", "txKey", txKey, "peerID", peer.ID()) + memR.Logger.Debug("have already marked a tx as requested", "txKey", txKey, "peerID", peer.ID()) } } } @@ -464,7 +464,7 @@ func (memR *Reactor) findNewPeerToRequestTx(txKey types.TxKey) { return } - // pop the next peer in the list of remaining peers that have seen the tx + // get the next peer in the list of remaining peers that have seen the tx // and does not already have an outbound request for that tx seenMap := memR.mempool.seenByPeersSet.Get(txKey) var peerID uint16 @@ -479,12 +479,14 @@ func (memR *Reactor) findNewPeerToRequestTx(txKey types.TxKey) { // No other free peer has the transaction we are looking for. // We give up 🤷♂️ and hope either a peer responds late or the tx // is gossiped again - memR.Logger.Info("no other peer has the tx we are looking for", "txKey", txKey) + memR.Logger.Debug("no other peer has the tx we are looking for", "txKey", txKey) + // TODO: should add a metric to see how common this is return } peer := memR.ids.GetPeer(peerID) if peer == nil { // we disconnected from that peer, retry again until we exhaust the list + memR.mempool.seenByPeersSet.Remove(txKey, peerID) memR.findNewPeerToRequestTx(txKey) } else { memR.mempool.metrics.RerequestedTxs.Add(1) diff --git a/mempool/metrics.go b/mempool/metrics.go index a3d73da3ac..8bff737148 100644 --- a/mempool/metrics.go +++ b/mempool/metrics.go @@ -7,11 +7,11 @@ import ( "sync" "time" - "github.com/tendermint/tendermint/libs/os" "github.com/go-kit/kit/metrics" "github.com/go-kit/kit/metrics/discard" "github.com/go-kit/kit/metrics/prometheus" stdprometheus "github.com/prometheus/client_golang/prometheus" + "github.com/tendermint/tendermint/libs/os" ) const ( diff --git a/test/e2e/docker/Dockerfile b/test/e2e/docker/Dockerfile index c81db55fc8..f0fab31982 100644 --- a/test/e2e/docker/Dockerfile +++ b/test/e2e/docker/Dockerfile @@ -1,7 +1,7 @@ # We need to build in a Linux environment to support C libraries, e.g. RocksDB. # We use Debian instead of Alpine, so that we can use binary database packages # instead of spending time compiling them. -FROM golang:1.20-bullseye +FROM golang:1.21.6-bullseye RUN apt-get -qq update -y && apt-get -qq upgrade -y >/dev/null RUN apt-get -qq install -y libleveldb-dev librocksdb-dev >/dev/null diff --git a/test/e2e/networks/ci.toml b/test/e2e/networks/ci.toml index be264ba4b4..480b77b708 100644 --- a/test/e2e/networks/ci.toml +++ b/test/e2e/networks/ci.toml @@ -6,6 +6,8 @@ initial_height = 1000 initial_state = { initial01 = "a", initial02 = "b", initial03 = "c" } # The most common case (e.g. Cosmos SDK-based chains). abci_protocol = "builtin" +load_tx_connections = 10 +load_tx_batch_size = 10 [validators] validator01 = 100 @@ -39,7 +41,6 @@ seeds = ["seed01"] seeds = ["seed01"] snapshot_interval = 5 perturb = ["disconnect"] -misbehaviors = { 1018 = "double-prevote" } [node.validator02] seeds = ["seed02"] @@ -66,7 +67,6 @@ start_at = 1005 # Becomes part of the validator set at 1010 seeds = ["seed02"] database = "cleveldb" fast_sync = "v0" -mempool_version = "v1" privval_protocol = "tcp" perturb = ["kill", "pause", "disconnect", "restart"] diff --git a/test/e2e/networks/simple.toml b/test/e2e/networks/simple.toml index 96b81f79fe..791705cf65 100644 --- a/test/e2e/networks/simple.toml +++ b/test/e2e/networks/simple.toml @@ -1,3 +1,6 @@ +load_tx_connections = 10 +load_tx_batch_size = 10 + [node.validator01] [node.validator02] [node.validator03] diff --git a/test/e2e/runner/rpc.go b/test/e2e/runner/rpc.go index 39792ba07d..ab16ef69a8 100644 --- a/test/e2e/runner/rpc.go +++ b/test/e2e/runner/rpc.go @@ -55,7 +55,7 @@ func waitForHeight(testnet *e2e.Testnet, height int64) (*types.Block, *types.Blo if len(clients) == 0 { return nil, nil, errors.New("unable to connect to any network nodes") } - if time.Since(lastIncrease) >= 20*time.Second { + if time.Since(lastIncrease) >= 30*time.Second { if maxResult == nil { return nil, nil, errors.New("chain stalled at unknown height") } diff --git a/test/e2e/runner/setup.go b/test/e2e/runner/setup.go index 8fdecf6a77..d240b96856 100644 --- a/test/e2e/runner/setup.go +++ b/test/e2e/runner/setup.go @@ -157,6 +157,7 @@ func MakeGenesis(testnet *e2e.Testnet) (types.GenesisDoc, error) { // MakeConfig generates a CometBFT config for a node. func MakeConfig(node *e2e.Node) (*config.Config, error) { cfg := config.DefaultConfig() + cfg.Consensus.TimeoutPropose = 5 * time.Second cfg.Moniker = node.Name cfg.ProxyApp = AppAddressTCP cfg.RPC.ListenAddress = "tcp://0.0.0.0:26657" From 27a6fe3b11531570f6abdc461df714d505e9827c Mon Sep 17 00:00:00 2001 From: Callum Waters <cmwaters19@gmail.com> Date: Thu, 25 Jan 2024 18:48:09 -0600 Subject: [PATCH 006/127] add has compact block message --- consensus/byzantine_test.go | 2 +- consensus/msgs.go | 15 + consensus/reactor.go | 76 +++-- consensus/state.go | 7 +- proto/tendermint/consensus/message.go | 9 + proto/tendermint/consensus/types.pb.go | 455 +++++++++++++++++++++---- proto/tendermint/consensus/types.proto | 28 +- 7 files changed, 480 insertions(+), 112 deletions(-) diff --git a/consensus/byzantine_test.go b/consensus/byzantine_test.go index 4b96e343ad..c315cfe9a6 100644 --- a/consensus/byzantine_test.go +++ b/consensus/byzantine_test.go @@ -241,7 +241,7 @@ func TestByzantinePrevoteEquivocation(t *testing.T) { // send proposal and block parts on internal msg queue lazyProposer.sendInternalMessage(msgInfo{&ProposalMessage{proposal}, ""}) - lazyProposer.sendInternalMessage(msgInfo{&CompactBlockMessage{block}, ""}) + lazyProposer.sendInternalMessage(msgInfo{&CompactBlockMessage{block, proposal.Round}, ""}) for i := 0; i < int(blockParts.Total()); i++ { part := blockParts.GetPart(i) lazyProposer.sendInternalMessage(msgInfo{&BlockPartMessage{lazyProposer.Height, lazyProposer.Round, part}, ""}) diff --git a/consensus/msgs.go b/consensus/msgs.go index c2a02c08f1..1c2f1b43ef 100644 --- a/consensus/msgs.go +++ b/consensus/msgs.go @@ -67,6 +67,14 @@ func MsgToProto(msg Message) (*cmtcons.Message, error) { } m := &cmtcons.CompactBlock{ Block: block, + Round: msg.Round, + } + return m.Wrap().(*cmtcons.Message), nil + + case *HasBlockMessage: + m := &cmtcons.HasCompactBlock{ + Height: msg.Height, + Round: msg.Round, } return m.Wrap().(*cmtcons.Message), nil @@ -205,6 +213,13 @@ func MsgFromProto(p *cmtcons.Message) (Message, error) { } pb = &CompactBlockMessage{ Block: block, + Round: msg.Round, + } + + case *cmtcons.HasCompactBlock: + pb = &HasBlockMessage{ + Height: msg.Height, + Round: msg.Round, } case *cmtcons.Vote: diff --git a/consensus/reactor.go b/consensus/reactor.go index 212a9d4238..c2987bdb45 100644 --- a/consensus/reactor.go +++ b/consensus/reactor.go @@ -336,8 +336,10 @@ func (conR *Reactor) ReceiveEnvelope(e p2p.Envelope) { case *ProposalPOLMessage: ps.ApplyProposalPOLMessage(msg) case *CompactBlockMessage: - ps.SetHasBlock(msg.Block.Height, ps.PRS.Round) + ps.SetHasBlock(msg.Block.Height, msg.Round) conR.conS.peerMsgQueue <- msgInfo{msg, e.Src.ID()} + case *HasBlockMessage: + ps.SetHasBlock(msg.Height, msg.Round) case *BlockPartMessage: ps.SetHasProposalBlockPart(msg.Height, msg.Round, int(msg.Part.Index)) conR.Metrics.BlockParts.With("peer_id", string(e.Src.ID())).Add(1) @@ -449,6 +451,13 @@ func (conR *Reactor) subscribeToBroadcastEvents() { conR.Logger.Error("Error adding listener for events", "err", err) } + if err := conR.conS.evsw.AddListenerForEvent(subscriber, types.EventCompleteProposal, + func(data cmtevents.EventData) { + conR.broadcastHasBlockMessage(data.(*types.EventDataCompleteProposal)) + }); err != nil { + conR.Logger.Error("Error adding listener for events", "err", err) + } + if err := conR.conS.evsw.AddListenerForEvent(subscriber, types.EventValidBlock, func(data cmtevents.EventData) { conR.broadcastNewValidBlockMessage(data.(*cstypes.RoundState)) @@ -505,28 +514,17 @@ func (conR *Reactor) broadcastHasVoteMessage(vote *types.Vote) { ChannelID: StateChannel, Message: msg, }) - /* - // TODO: Make this broadcast more selective. - for _, peer := range conR.Switch.Peers().List() { - ps, ok := peer.Get(PeerStateKey).(*PeerState) - if !ok { - panic(fmt.Sprintf("Peer %v has no state", peer)) - } - prs := ps.GetRoundState() - if prs.Height == vote.Height { - // TODO: Also filter on round? - e := p2p.Envelope{ - ChannelID: StateChannel, struct{ ConsensusMessage }{msg}, - Message: p, - } - p2p.TrySendEnvelopeShim(peer, e) //nolint: staticcheck - } else { - // Height doesn't match - // TODO: check a field, maybe CatchupCommitRound? - // TODO: But that requires changing the struct field comment. - } - } - */ +} + +// Broadcasts HasBlockMessage to peers that care. +func (conR *Reactor) broadcastHasBlockMessage(data *types.EventDataCompleteProposal) { + conR.Switch.BroadcastEnvelope(p2p.Envelope{ + ChannelID: DataChannel, + Message: &cmtcons.HasCompactBlock{ + Height: data.Height, + Round: data.Round, + }, + }) } func makeRoundStepMessage(rs *cstypes.RoundState) (nrsMsg *cmtcons.NewRoundStep) { @@ -593,6 +591,7 @@ OUTER_LOOP: ChannelID: DataChannel, Message: &cmtcons.CompactBlock{ Block: compactBlock, + Round: rs.Round, }, }, logger) { ps.SetHasBlock(prs.Height, prs.Round) @@ -640,7 +639,7 @@ OUTER_LOOP: if rs.Proposal != nil && !prs.Proposal { // Proposal: share the proposal metadata with peer. { - logger.Info("Sending proposal", "height", prs.Height, "round", prs.Round) + logger.Debug("Sending proposal", "height", prs.Height, "round", prs.Round) if p2p.SendEnvelopeShim(peer, p2p.Envelope{ //nolint: staticcheck ChannelID: DataChannel, Message: &cmtcons.Proposal{Proposal: *rs.Proposal.ToProto()}, @@ -685,7 +684,10 @@ func (conR *Reactor) gossipDataForCatchup(logger log.Logger, rs *cstypes.RoundSt time.Sleep(conR.conS.config.PeerGossipSleepDuration) return } else if !blockMeta.BlockID.PartSetHeader.Equals(prs.ProposalBlockPartSetHeader) { - logger.Info("Peer ProposalBlockPartSetHeader mismatch, sleeping", + // this happens when the peer is on a different round to the round of the proposal + // that was eventually committed. They should eventually receive 2/3 precommits and + // update the part set header to the one of the block that is committed + logger.Debug("Peer ProposalBlockPartSetHeader mismatch, sleeping", "blockPartSetHeader", blockMeta.BlockID.PartSetHeader, "peerBlockPartSetHeader", prs.ProposalBlockPartSetHeader) time.Sleep(conR.conS.config.PeerGossipSleepDuration) return @@ -1707,6 +1709,7 @@ func (m *ProposalPOLMessage) String() string { // CompactBlockMessage is sent when gossipping a piece of the proposed block. type CompactBlockMessage struct { Block *types.Block + Round int32 } // ValidateBasic performs basic validation. @@ -1716,7 +1719,28 @@ func (m *CompactBlockMessage) ValidateBasic() error { // String returns a string representation. func (m *CompactBlockMessage) String() string { - return fmt.Sprintf("[CompactBlock H:%v]", m.Block.Height) + return fmt.Sprintf("[CompactBlock H:%d, R: %d]", m.Block.Height, m.Round) +} + +//------------------------------------- + +// HasBlockMessage is sent when gossipping the receiving of a block +type HasBlockMessage struct { + Height int64 + Round int32 +} + +// ValidateBasic performs basic validation. +func (m *HasBlockMessage) ValidateBasic() error { + if m.Height <= 0 { + return errors.New("negative or zero height") + } + return nil +} + +// String returns a string representation. +func (m *HasBlockMessage) String() string { + return fmt.Sprintf("[HasBlock H:%d, R: %d]", m.Height, m.Round) } //------------------------------------- diff --git a/consensus/state.go b/consensus/state.go index cd98393519..3062e659a7 100644 --- a/consensus/state.go +++ b/consensus/state.go @@ -1234,7 +1234,10 @@ func (cs *State) defaultDecideProposal(height int64, round int32) { // send proposal and block parts on internal msg queue cs.sendInternalMessage(msgInfo{&ProposalMessage{proposal}, ""}) - cs.sendInternalMessage(msgInfo{&CompactBlockMessage{block}, ""}) + cs.sendInternalMessage(msgInfo{&CompactBlockMessage{ + Block: block, + Round: p.Round, + }, ""}) for i := 0; i < int(blockParts.Total()); i++ { part := blockParts.GetPart(i) @@ -2028,7 +2031,7 @@ func (cs *State) addCompactBlock(msg *CompactBlockMessage, peerID p2p.ID) error cs.ProposalBlockParts = partSet // NOTE: it's possible to receive complete proposal blocks for future rounds without having the proposal - cs.Logger.Info("assembled proposal block from compact block", "height", cs.ProposalBlock.Height, "hash", cs.ProposalBlock.Hash()) + cs.Logger.Info("assembled proposal block from compact block", "height", cs.ProposalBlock.Height, "round", cs.Round, "hash", cs.ProposalBlock.Hash()) if err := cs.eventBus.PublishEventCompleteProposal(cs.CompleteProposalEvent()); err != nil { cs.Logger.Error("failed publishing event complete proposal", "err", err) diff --git a/proto/tendermint/consensus/message.go b/proto/tendermint/consensus/message.go index 1bc10a0915..063c78a9aa 100644 --- a/proto/tendermint/consensus/message.go +++ b/proto/tendermint/consensus/message.go @@ -49,6 +49,12 @@ func (m *CompactBlock) Wrap() proto.Message { return cm } +func (m *HasCompactBlock) Wrap() proto.Message { + cm := &Message{} + cm.Sum = &Message_HasCompactBlock{HasCompactBlock: m} + return cm +} + func (m *BlockPart) Wrap() proto.Message { cm := &Message{} cm.Sum = &Message_BlockPart{BlockPart: m} @@ -98,6 +104,9 @@ func (m *Message) Unwrap() (proto.Message, error) { case *Message_CompactBlock: return m.GetCompactBlock(), nil + case *Message_HasCompactBlock: + return m.GetHasCompactBlock(), nil + case *Message_BlockPart: return m.GetBlockPart(), nil diff --git a/proto/tendermint/consensus/types.pb.go b/proto/tendermint/consensus/types.pb.go index f24e21fb7a..6a7322e1d7 100644 --- a/proto/tendermint/consensus/types.pb.go +++ b/proto/tendermint/consensus/types.pb.go @@ -291,6 +291,7 @@ func (m *ProposalPOL) GetProposalPol() bits.BitArray { // CompactBlock is for gossiping a block type CompactBlock struct { Block *types.Block `protobuf:"bytes,1,opt,name=block,proto3" json:"block,omitempty"` + Round int32 `protobuf:"varint,2,opt,name=round,proto3" json:"round,omitempty"` } func (m *CompactBlock) Reset() { *m = CompactBlock{} } @@ -333,6 +334,66 @@ func (m *CompactBlock) GetBlock() *types.Block { return nil } +func (m *CompactBlock) GetRound() int32 { + if m != nil { + return m.Round + } + return 0 +} + +// HasCompactBlock to signal that the node has the block +type HasCompactBlock struct { + Height int64 `protobuf:"varint,1,opt,name=height,proto3" json:"height,omitempty"` + Round int32 `protobuf:"varint,2,opt,name=round,proto3" json:"round,omitempty"` +} + +func (m *HasCompactBlock) Reset() { *m = HasCompactBlock{} } +func (m *HasCompactBlock) String() string { return proto.CompactTextString(m) } +func (*HasCompactBlock) ProtoMessage() {} +func (*HasCompactBlock) Descriptor() ([]byte, []int) { + return fileDescriptor_81a22d2efc008981, []int{5} +} +func (m *HasCompactBlock) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *HasCompactBlock) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_HasCompactBlock.Marshal(b, m, deterministic) + } else { + b = b[:cap(b)] + n, err := m.MarshalToSizedBuffer(b) + if err != nil { + return nil, err + } + return b[:n], nil + } +} +func (m *HasCompactBlock) XXX_Merge(src proto.Message) { + xxx_messageInfo_HasCompactBlock.Merge(m, src) +} +func (m *HasCompactBlock) XXX_Size() int { + return m.Size() +} +func (m *HasCompactBlock) XXX_DiscardUnknown() { + xxx_messageInfo_HasCompactBlock.DiscardUnknown(m) +} + +var xxx_messageInfo_HasCompactBlock proto.InternalMessageInfo + +func (m *HasCompactBlock) GetHeight() int64 { + if m != nil { + return m.Height + } + return 0 +} + +func (m *HasCompactBlock) GetRound() int32 { + if m != nil { + return m.Round + } + return 0 +} + // BlockPart is sent when catching up type BlockPart struct { Height int64 `protobuf:"varint,1,opt,name=height,proto3" json:"height,omitempty"` @@ -344,7 +405,7 @@ func (m *BlockPart) Reset() { *m = BlockPart{} } func (m *BlockPart) String() string { return proto.CompactTextString(m) } func (*BlockPart) ProtoMessage() {} func (*BlockPart) Descriptor() ([]byte, []int) { - return fileDescriptor_81a22d2efc008981, []int{5} + return fileDescriptor_81a22d2efc008981, []int{6} } func (m *BlockPart) XXX_Unmarshal(b []byte) error { return m.Unmarshal(b) @@ -403,7 +464,7 @@ func (m *Vote) Reset() { *m = Vote{} } func (m *Vote) String() string { return proto.CompactTextString(m) } func (*Vote) ProtoMessage() {} func (*Vote) Descriptor() ([]byte, []int) { - return fileDescriptor_81a22d2efc008981, []int{6} + return fileDescriptor_81a22d2efc008981, []int{7} } func (m *Vote) XXX_Unmarshal(b []byte) error { return m.Unmarshal(b) @@ -451,7 +512,7 @@ func (m *HasVote) Reset() { *m = HasVote{} } func (m *HasVote) String() string { return proto.CompactTextString(m) } func (*HasVote) ProtoMessage() {} func (*HasVote) Descriptor() ([]byte, []int) { - return fileDescriptor_81a22d2efc008981, []int{7} + return fileDescriptor_81a22d2efc008981, []int{8} } func (m *HasVote) XXX_Unmarshal(b []byte) error { return m.Unmarshal(b) @@ -520,7 +581,7 @@ func (m *VoteSetMaj23) Reset() { *m = VoteSetMaj23{} } func (m *VoteSetMaj23) String() string { return proto.CompactTextString(m) } func (*VoteSetMaj23) ProtoMessage() {} func (*VoteSetMaj23) Descriptor() ([]byte, []int) { - return fileDescriptor_81a22d2efc008981, []int{8} + return fileDescriptor_81a22d2efc008981, []int{9} } func (m *VoteSetMaj23) XXX_Unmarshal(b []byte) error { return m.Unmarshal(b) @@ -590,7 +651,7 @@ func (m *VoteSetBits) Reset() { *m = VoteSetBits{} } func (m *VoteSetBits) String() string { return proto.CompactTextString(m) } func (*VoteSetBits) ProtoMessage() {} func (*VoteSetBits) Descriptor() ([]byte, []int) { - return fileDescriptor_81a22d2efc008981, []int{9} + return fileDescriptor_81a22d2efc008981, []int{10} } func (m *VoteSetBits) XXX_Unmarshal(b []byte) error { return m.Unmarshal(b) @@ -666,6 +727,7 @@ type Message struct { // *Message_VoteSetMaj23 // *Message_VoteSetBits // *Message_CompactBlock + // *Message_HasCompactBlock Sum isMessage_Sum `protobuf_oneof:"sum"` } @@ -673,7 +735,7 @@ func (m *Message) Reset() { *m = Message{} } func (m *Message) String() string { return proto.CompactTextString(m) } func (*Message) ProtoMessage() {} func (*Message) Descriptor() ([]byte, []int) { - return fileDescriptor_81a22d2efc008981, []int{10} + return fileDescriptor_81a22d2efc008981, []int{11} } func (m *Message) XXX_Unmarshal(b []byte) error { return m.Unmarshal(b) @@ -738,17 +800,21 @@ type Message_VoteSetBits struct { type Message_CompactBlock struct { CompactBlock *CompactBlock `protobuf:"bytes,10,opt,name=compact_block,json=compactBlock,proto3,oneof" json:"compact_block,omitempty"` } +type Message_HasCompactBlock struct { + HasCompactBlock *HasCompactBlock `protobuf:"bytes,11,opt,name=has_compact_block,json=hasCompactBlock,proto3,oneof" json:"has_compact_block,omitempty"` +} -func (*Message_NewRoundStep) isMessage_Sum() {} -func (*Message_NewValidBlock) isMessage_Sum() {} -func (*Message_Proposal) isMessage_Sum() {} -func (*Message_ProposalPol) isMessage_Sum() {} -func (*Message_BlockPart) isMessage_Sum() {} -func (*Message_Vote) isMessage_Sum() {} -func (*Message_HasVote) isMessage_Sum() {} -func (*Message_VoteSetMaj23) isMessage_Sum() {} -func (*Message_VoteSetBits) isMessage_Sum() {} -func (*Message_CompactBlock) isMessage_Sum() {} +func (*Message_NewRoundStep) isMessage_Sum() {} +func (*Message_NewValidBlock) isMessage_Sum() {} +func (*Message_Proposal) isMessage_Sum() {} +func (*Message_ProposalPol) isMessage_Sum() {} +func (*Message_BlockPart) isMessage_Sum() {} +func (*Message_Vote) isMessage_Sum() {} +func (*Message_HasVote) isMessage_Sum() {} +func (*Message_VoteSetMaj23) isMessage_Sum() {} +func (*Message_VoteSetBits) isMessage_Sum() {} +func (*Message_CompactBlock) isMessage_Sum() {} +func (*Message_HasCompactBlock) isMessage_Sum() {} func (m *Message) GetSum() isMessage_Sum { if m != nil { @@ -827,6 +893,13 @@ func (m *Message) GetCompactBlock() *CompactBlock { return nil } +func (m *Message) GetHasCompactBlock() *HasCompactBlock { + if x, ok := m.GetSum().(*Message_HasCompactBlock); ok { + return x.HasCompactBlock + } + return nil +} + // XXX_OneofWrappers is for the internal use of the proto package. func (*Message) XXX_OneofWrappers() []interface{} { return []interface{}{ @@ -840,6 +913,7 @@ func (*Message) XXX_OneofWrappers() []interface{} { (*Message_VoteSetMaj23)(nil), (*Message_VoteSetBits)(nil), (*Message_CompactBlock)(nil), + (*Message_HasCompactBlock)(nil), } } @@ -849,6 +923,7 @@ func init() { proto.RegisterType((*Proposal)(nil), "tendermint.consensus.Proposal") proto.RegisterType((*ProposalPOL)(nil), "tendermint.consensus.ProposalPOL") proto.RegisterType((*CompactBlock)(nil), "tendermint.consensus.CompactBlock") + proto.RegisterType((*HasCompactBlock)(nil), "tendermint.consensus.HasCompactBlock") proto.RegisterType((*BlockPart)(nil), "tendermint.consensus.BlockPart") proto.RegisterType((*Vote)(nil), "tendermint.consensus.Vote") proto.RegisterType((*HasVote)(nil), "tendermint.consensus.HasVote") @@ -860,63 +935,66 @@ func init() { func init() { proto.RegisterFile("tendermint/consensus/types.proto", fileDescriptor_81a22d2efc008981) } var fileDescriptor_81a22d2efc008981 = []byte{ - // 896 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xd4, 0x56, 0x4f, 0x6f, 0xe3, 0x44, - 0x14, 0xb7, 0xb7, 0x49, 0x93, 0xbe, 0x24, 0x2d, 0x8c, 0xba, 0x4b, 0x28, 0x90, 0x16, 0x73, 0xa9, - 0x10, 0x24, 0x28, 0x3d, 0x20, 0x2d, 0x20, 0x20, 0x0b, 0xac, 0xbb, 0xda, 0xee, 0x46, 0xce, 0xb2, - 0x42, 0x5c, 0x2c, 0xc7, 0x1e, 0x25, 0xc3,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6a, - 0x2f, 0xae, 0x6a, 0xe8, 0x89, 0xbd, 0xc8, 0xf7, 0xa1, 0xb5, 0xc0, 0x52, 0xf3, 0xd4, 0xde, 0x58, - 0x55, 0x44, 0x6b, 0x91, 0x54, 0x11, 0x4f, 0xac, 0xbd, 0x3a, 0x84, 0x56, 0xac, 0x75, 0xd8, 0xcc, - 0x05, 0xac, 0xe2, 0x64, 0x4b, 0xb6, 0xe2, 0x14, 0x5b, 0xf7, 0x41, 0x15, 0xd6, 0xc4, 0x71, 0x3a, - 0xf8, 0xfa, 0xf9, 0x79, 0xc7, 0x7d, 0x71, 0xde, 0x71, 0x7f, 0x3f, 0xef, 0xb8, 0x3f, 0x5e, 0x74, - 0x9c, 0x17, 0x17, 0x1d, 0xe7, 0xb7, 0x8b, 0x8e, 0xf3, 0xed, 0x47, 0x13, 0x26, 0xa7, 0xc7, 0xe3, - 0x6e, 0xcc, 0xd3, 0x9e, 0xfd, 0x5f, 0xb3, 0x3c, 0xea, 0xbf, 0xa3, 0xb2, 0xff, 0xab, 0xf1, 0x3a, - 0xda, 0x0e, 0xfe, 0x0a, 0x00, 0x00, 0xff, 0xff, 0xf5, 0xdd, 0x83, 0xc3, 0x7e, 0x09, 0x00, 0x00, + // 932 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xd4, 0x56, 0x41, 0x6f, 0x1b, 0x45, + 0x14, 0xf6, 0x36, 0x76, 0xec, 0x3c, 0xdb, 0x31, 0x1d, 0xa5, 0xc5, 0x04, 0x70, 0xc2, 0x22, 0xa4, + 0x08, 0x81, 0x8d, 0x9c, 0x03, 0x52, 0x41, 0x2a, 0xb8, 0x40, 0x37, 0x55, 0xd3, 0x5a, 0xeb, 0x52, + 0x21, 0x2e, 0xab, 0xf5, 0xee, 0xc8, 0x1e, 0xea, 0xdd, 0x59, 0xed, 0x4c, 0x12, 0x72, 0xe5, 0x17, + 0x70, 0xe2, 0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b5, 0x8e, 0xed, 0xda, 0x55, 0x0d, 0xb5, + 0x17, 0x57, 0x35, 0xf4, 0xd8, 0x5e, 0xe4, 0xfb, 0xd0, 0x5c, 0x60, 0xa9, 0x79, 0x6a, 0x6f, 0x5c, + 0x45, 0xa2, 0xb5, 0x48, 0x8a, 0xc4, 0x63, 0x6b, 0xaf, 0x0e, 0xa0, 0x19, 0x69, 0x19, 0x36, 0x73, + 0x01, 0x57, 0xe5, 0x64, 0x2b, 0xb6, 0xca, 0x29, 0xb2, 0x15, 0x7c, 0x04, 0x37, 0x15, 0x37, 0xab, + 0x70, 0x75, 0x84, 0xfb, 0xe0, 0x52, 0x92, 0x5e, 0x41, 0x6c, 0x4d, 0x57, 0x5f, 0x0d, 0x2a, 0xb0, + 0x26, 0x8e, 0x92, 0xc1, 0xb7, 0xcf, 0xcf, 0x3a, 0xce, 0x8b, 0xb3, 0x8e, 0xf3, 0xe7, 0x59, 0xc7, + 0xf9, 0xf9, 0xbc, 0x53, 0x7a, 0x71, 0xde, 0x29, 0xfd, 0x71, 0xde, 0x29, 0x7d, 0xff, 0xd9, 0x84, + 0xc9, 0xe9, 0xd1, 0xb8, 0x1b, 0xf1, 0xa4, 0x67, 0xff, 0x83, 0x2d, 0x8f, 0xfa, 0x4f, 0xae, 0xe8, + 0x5f, 0x70, 0xbc, 0x8e, 0xb6, 0xfd, 0x7f, 0x02, 0x00, 0x00, 0xff, 0xff, 0xed, 0x49, 0x80, 0x76, + 0x2a, 0x0a, 0x00, 0x00, } func (m *NewRoundStep) Marshal() (dAtA []byte, err error) { @@ -1128,6 +1206,11 @@ func (m *CompactBlock) MarshalToSizedBuffer(dAtA []byte) (int, error) { _ = i var l int _ = l + if m.Round != 0 { + i = encodeVarintTypes(dAtA, i, uint64(m.Round)) + i-- + dAtA[i] = 0x10 + } if m.Block != nil { { size, err := m.Block.MarshalToSizedBuffer(dAtA[:i]) @@ -1143,6 +1226,39 @@ func (m *CompactBlock) MarshalToSizedBuffer(dAtA []byte) (int, error) { return len(dAtA) - i, nil } +func (m *HasCompactBlock) Marshal() (dAtA []byte, err error) { + size := m.Size() + dAtA = make([]byte, size) + n, err := m.MarshalToSizedBuffer(dAtA[:size]) + if err != nil { + return nil, err + } + return dAtA[:n], nil +} + +func (m *HasCompactBlock) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *HasCompactBlock) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + if m.Round != 0 { + i = encodeVarintTypes(dAtA, i, uint64(m.Round)) + i-- + dAtA[i] = 0x10 + } + if m.Height != 0 { + i = encodeVarintTypes(dAtA, i, uint64(m.Height)) + i-- + dAtA[i] = 0x8 + } + return len(dAtA) - i, nil +} + func (m *BlockPart) Marshal() (dAtA []byte, err error) { size := m.Size() dAtA = make([]byte, size) @@ -1612,6 +1728,27 @@ func (m *Message_CompactBlock) MarshalToSizedBuffer(dAtA []byte) (int, error) { } return len(dAtA) - i, nil } +func (m *Message_HasCompactBlock) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *Message_HasCompactBlock) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + if m.HasCompactBlock != nil { + { + size, err := m.HasCompactBlock.MarshalToSizedBuffer(dAtA[:i]) + if err != nil { + return 0, err + } + i -= size + i = encodeVarintTypes(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0x5a + } + return len(dAtA) - i, nil +} func encodeVarintTypes(dAtA []byte, offset int, v uint64) int { offset -= sovTypes(v) base := offset @@ -1709,6 +1846,24 @@ func (m *CompactBlock) Size() (n int) { l = m.Block.Size() n += 1 + l + sovTypes(uint64(l)) } + if m.Round != 0 { + n += 1 + sovTypes(uint64(m.Round)) + } + return n +} + +func (m *HasCompactBlock) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + if m.Height != 0 { + n += 1 + sovTypes(uint64(m.Height)) + } + if m.Round != 0 { + n += 1 + sovTypes(uint64(m.Round)) + } return n } @@ -1937,6 +2092,18 @@ func (m *Message_CompactBlock) Size() (n int) { } return n } +func (m *Message_HasCompactBlock) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + if m.HasCompactBlock != nil { + l = m.HasCompactBlock.Size() + n += 1 + l + sovTypes(uint64(l)) + } + return n +} func sovTypes(x uint64) (n int) { return (math_bits.Len64(x|1) + 6) / 7 @@ -2535,6 +2702,113 @@ func (m *CompactBlock) Unmarshal(dAtA []byte) error { return err } iNdEx = postIndex + case 2: + if wireType != 0 { + return fmt.Errorf("proto: wrong wireType = %d for field Round", wireType) + } + m.Round = 0 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTypes + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + m.Round |= int32(b&0x7F) << shift + if b < 0x80 { + break + } + } + default: + iNdEx = preIndex + skippy, err := skipTypes(dAtA[iNdEx:]) + if err != nil { + return err + } + if (skippy < 0) || (iNdEx+skippy) < 0 { + return ErrInvalidLengthTypes + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} +func (m *HasCompactBlock) Unmarshal(dAtA []byte) error { + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTypes + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: HasCompactBlock: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: HasCompactBlock: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 0 { + return fmt.Errorf("proto: wrong wireType = %d for field Height", wireType) + } + m.Height = 0 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTypes + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + m.Height |= int64(b&0x7F) << shift + if b < 0x80 { + break + } + } + case 2: + if wireType != 0 { + return fmt.Errorf("proto: wrong wireType = %d for field Round", wireType) + } + m.Round = 0 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTypes + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + m.Round |= int32(b&0x7F) << shift + if b < 0x80 { + break + } + } default: iNdEx = preIndex skippy, err := skipTypes(dAtA[iNdEx:]) @@ -3581,6 +3855,41 @@ func (m *Message) Unmarshal(dAtA []byte) error { } m.Sum = &Message_CompactBlock{v} iNdEx = postIndex + case 11: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field HasCompactBlock", wireType) + } + var msglen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTypes + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + msglen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if msglen < 0 { + return ErrInvalidLengthTypes + } + postIndex := iNdEx + msglen + if postIndex < 0 { + return ErrInvalidLengthTypes + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + v := &HasCompactBlock{} + if err := v.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + m.Sum = &Message_HasCompactBlock{v} + iNdEx = postIndex default: iNdEx = preIndex skippy, err := skipTypes(dAtA[iNdEx:]) diff --git a/proto/tendermint/consensus/types.proto b/proto/tendermint/consensus/types.proto index 8b483fd92f..8db2b5e80f 100644 --- a/proto/tendermint/consensus/types.proto +++ b/proto/tendermint/consensus/types.proto @@ -44,6 +44,13 @@ message ProposalPOL { // CompactBlock is for gossiping a block message CompactBlock { tendermint.types.Block block = 1; + int32 round = 2; +} + +// HasCompactBlock to signal that the node has the block +message HasCompactBlock { + int64 height = 1; + int32 round = 2; } // BlockPart is sent when catching up @@ -85,15 +92,16 @@ message VoteSetBits { message Message { oneof sum { - NewRoundStep new_round_step = 1; - NewValidBlock new_valid_block = 2; - Proposal proposal = 3; - ProposalPOL proposal_pol = 4; - BlockPart block_part = 5; - Vote vote = 6; - HasVote has_vote = 7; - VoteSetMaj23 vote_set_maj23 = 8; - VoteSetBits vote_set_bits = 9; - CompactBlock compact_block = 10; + NewRoundStep new_round_step = 1; + NewValidBlock new_valid_block = 2; + Proposal proposal = 3; + ProposalPOL proposal_pol = 4; + BlockPart block_part = 5; + Vote vote = 6; + HasVote has_vote = 7; + VoteSetMaj23 vote_set_maj23 = 8; + VoteSetBits vote_set_bits = 9; + CompactBlock compact_block = 10; + HasCompactBlock has_compact_block = 11; } } From f0e972e5a55c02891829132c34e63c7b316445a2 Mon Sep 17 00:00:00 2001 From: Callum Waters <cmwaters19@gmail.com> Date: Fri, 26 Jan 2024 14:54:33 -0600 Subject: [PATCH 007/127] use go 1.19 --- go.mod | 4 +--- go.sum | 32 -------------------------------- 2 files changed, 1 insertion(+), 35 deletions(-) diff --git a/go.mod b/go.mod index 79a8bb3a5b..2c49f3b373 100644 --- a/go.mod +++ b/go.mod @@ -1,8 +1,6 @@ module github.com/tendermint/tendermint -go 1.21 - -toolchain go1.21.6 +go 1.19 require ( github.com/BurntSushi/toml v1.2.1 diff --git a/go.sum b/go.sum index 342441a98f..5647ff806a 100644 --- a/go.sum +++ b/go.sum @@ -55,7 +55,6 @@ github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym github.com/ChainSafe/go-schnorrkel v1.0.0 h1:3aDA67lAykLaG1y3AOjs88dMxC88PgUuHRrLeDnvGIM= github.com/ChainSafe/go-schnorrkel v1.0.0/go.mod h1:dpzHYVxLZcp8pjlV+O+UR8K0Hp/z7vcchBSbMBEhCw4= github.com/DATA-DOG/go-sqlmock v1.5.0 h1:Shsta01QNfFxHCfpW6YH2STWB0MudeXXEWMr20OEh60= -github.com/DATA-DOG/go-sqlmock v1.5.0/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM= github.com/Djarvur/go-err113 v0.0.0-20210108212216-aea10b59be24 h1:sHglBQTwgx+rWPdisA5ynNEsoARbiCBOyGcJM4/OzsM= github.com/Djarvur/go-err113 v0.0.0-20210108212216-aea10b59be24/go.mod h1:4UJr5HIiMZrwgkSPdsjy2uOQExX/WEILpIrO9UPGuXs= github.com/GaijinEntertainment/go-exhaustruct/v2 v2.3.0 h1:+r1rSv4gvYn0wmRjC8X7IAzX8QezqtFV9m0MUHFJgts= @@ -76,7 +75,6 @@ github.com/OpenPeeDeeP/depguard v1.1.1/go.mod h1:JtAMzWkmFEzDPyAd+W0NHl1lvpQKTvT github.com/ProtonMail/go-crypto v0.0.0-20230217124315-7d5c6f04bbb8 h1:wPbRQzjjwFc0ih8puEVAOFGELsn1zoIIYdxvML7mDxA= github.com/ProtonMail/go-crypto v0.0.0-20230217124315-7d5c6f04bbb8/go.mod h1:I0gYDMZ6Z5GRU7l58bNFSkPTFN6Yl12dsUlAZ8xy98g= github.com/VividCortex/gohistogram v1.0.0 h1:6+hBz+qvs0JOrrNhhmR7lFxo5sINxBCGXrdtl/UvroE= -github.com/VividCortex/gohistogram v1.0.0/go.mod h1:Pf5mBqqDxYaXu3hDrrU+w6nw50o/4+TcAqDqk/vUH7g= github.com/Workiva/go-datastructures v1.0.53 h1:J6Y/52yX10Xc5JjXmGtWoSSxs3mZnGSaq37xZZh7Yig= github.com/Workiva/go-datastructures v1.0.53/go.mod h1:1yZL+zfsztete+ePzZz/Zb1/t5BnDuE2Ya2MMGhzP6A= github.com/acomagu/bufpipe v1.0.4 h1:e3H4WUzM3npvo5uv95QuJM3cQspFNtFBzvJ2oNjKIDQ= @@ -103,7 +101,6 @@ github.com/ashanbrown/forbidigo v1.5.1/go.mod h1:Y8j9jy9ZYAEHXdu723cUlraTqbzjKF1 github.com/ashanbrown/makezero v1.1.1 h1:iCQ87C0V0vSyO+M9E/FZYbu65auqH0lnsOkf5FcB28s= github.com/ashanbrown/makezero v1.1.1/go.mod h1:i1bJLCRSCHOcOa9Y6MyF2FTfMZMFdHvxKHxgO5Z1axI= github.com/benbjohnson/clock v1.3.0 h1:ip6w0uFQkncKQ979AypyG0ER7mqUSBdKLOgAle/AT8A= -github.com/benbjohnson/clock v1.3.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8= github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= @@ -156,7 +153,6 @@ github.com/celestiaorg/nmt v0.20.0/go.mod h1:Oz15Ub6YPez9uJV0heoU4WpFctxazuIhKyU github.com/cenkalti/backoff v2.2.1+incompatible h1:tNowT99t7UNflLxfYYSlKYsBpXdEet03Pg2g16Swow4= github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM= github.com/cenkalti/backoff/v4 v4.1.3 h1:cFAlzYUlVYDysBEH2T5hyJZMh3+5+WCBvSnK6Q8UtC4= -github.com/cenkalti/backoff/v4 v4.1.3/go.mod h1:scbssz8iZGpm3xbr14ovlUdkxfGXNInqkPWOWmG2CLw= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko= github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc= @@ -188,7 +184,6 @@ github.com/containerd/console v1.0.3/go.mod h1:7LqA/THxQ86k76b8c/EMSiaJ3h1eZkMkX github.com/containerd/continuity v0.3.0 h1:nisirsYROK15TAMVukJOUyGJjz4BNQJBVsNvAXZJ/eg= github.com/containerd/continuity v0.3.0/go.mod h1:wJEAIwKOm/pBZuBd0JmeTvnLquTB1Ag8espWhkykbPM= github.com/containerd/stargz-snapshotter/estargz v0.12.1 h1:+7nYmHJb0tEkcRaAW+MHqoKaJYZmkikupxCqVtmPuY0= -github.com/containerd/stargz-snapshotter/estargz v0.12.1/go.mod h1:12VUuCq3qPq4y8yUW+l5w3+oXV3cx2Po3KSe/SmPGqw= github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE= github.com/coreos/go-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8NzMklzPG4d5KIOhIy30Tk= github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk= @@ -204,7 +199,6 @@ github.com/creachadair/taskgroup v0.3.2 h1:zlfutDS+5XG40AOxcHDSThxKzns8Tnr9jnr6V github.com/creachadair/taskgroup v0.3.2/go.mod h1:wieWwecHVzsidg2CsUnFinW1faVN4+kq+TDlRJQ0Wbk= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/creack/pty v1.1.18 h1:n56/Zwd5o6whRC5PMGretI4IdRLlmBXYNjScPaBgsbY= -github.com/creack/pty v1.1.18/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4= github.com/curioswitch/go-reassign v0.2.0 h1:G9UZyOcpk/d7Gd6mqYgd8XYWFMw/znxwGDUstnC9DIo= github.com/curioswitch/go-reassign v0.2.0/go.mod h1:x6OpXuWvgfQaMGks2BZybTngWjT84hqJfKoO8Tt/Roc= github.com/cyberdelia/templates v0.0.0-20141128023046-ca7fffd4298c/go.mod h1:GyV+0YP4qX0UQ7r2MoYZ+AvYDp12OF5yg4q8rGnyNh4= @@ -225,7 +219,6 @@ github.com/deepmap/oapi-codegen v1.8.2/go.mod h1:YLgSKSDv/bZQB7N4ws6luhozi3cEdRk github.com/denis-tingaikin/go-header v0.4.3 h1:tEaZKAlqql6SKCY++utLmkPLd6K8IBM20Ha7UVm+mtU= github.com/denis-tingaikin/go-header v0.4.3/go.mod h1:0wOCWuN71D5qIgE2nz9KrKmuYBAC2Mra5RassOIQ2/c= github.com/denisenkom/go-mssqldb v0.12.0 h1:VtrkII767ttSPNRfFekePK3sctr+joXgO58stqQbtUA= -github.com/denisenkom/go-mssqldb v0.12.0/go.mod h1:iiK0YP1ZeepvmBQk/QpLEhhTNJgfzrpArPY/aFvc9yU= github.com/dgraph-io/badger/v2 v2.2007.4 h1:TRWBQg8UrlUhaFdco01nO2uXwzKS7zd+HVdwV/GHc4o= github.com/dgraph-io/badger/v2 v2.2007.4/go.mod h1:vSw/ax2qojzbN6eXHIx6KPKtCSHJN/Uz0X0VPruTIhk= github.com/dgraph-io/ristretto v0.0.3-0.20200630154024-f66de99634de/go.mod h1:KPxhHT9ZxKefz+PCeOGsrHpl1qZ7i70dGTu2u+Ahh6E= @@ -281,7 +274,6 @@ github.com/fortytw2/leaktest v1.3.0 h1:u8491cBMTQ8ft8aeV+adlcytMZylmA5nnwwkRZjI8 github.com/fortytw2/leaktest v1.3.0/go.mod h1:jDsjWgpAGjm2CA7WthBh/CdZYEPF31XHquHwclZch5g= github.com/frankban/quicktest v1.11.3/go.mod h1:wRf/ReqHper53s+kmmSZizM8NamnL3IM0I9ntUbOk+k= github.com/frankban/quicktest v1.14.3 h1:FJKSZTDHjyhriyC81FLQ0LY93eSai0ZyR/ZIkd3ZUKE= -github.com/frankban/quicktest v1.14.3/go.mod h1:mgiwOwqx65TmIk1wJ6Q7wvnVMocbUorkibMOrVTHZps= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY= @@ -329,7 +321,6 @@ github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre github.com/go-openapi/jsonpointer v0.19.5/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34VGT37dQOVbmoatg= github.com/go-openapi/swag v0.19.5/go.mod h1:POnQmlKehdgb5mhVOsnJFsivZCEZ/vjK9gh66Z9tfKk= github.com/go-sql-driver/mysql v1.7.0 h1:ueSltNNllEqE3qcWBTD0iQd3IpL/6U+mJxLkazJ7YPc= -github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= github.com/go-toolsmith/astcast v1.1.0 h1:+JN9xZV1A+Re+95pgnMgDboWNVnIMMQXwfBwLRPgSC8= github.com/go-toolsmith/astcast v1.1.0/go.mod h1:qdcuFWeGGS2xX5bLM/c3U9lewg7+Zu4mr+xPwZIB4ZU= @@ -343,7 +334,6 @@ github.com/go-toolsmith/astfmt v1.1.0/go.mod h1:OrcLlRwu0CuiIBp/8b5PYF9ktGVZUjlN github.com/go-toolsmith/astp v1.1.0 h1:dXPuCl6u2llURjdPLLDxJeZInAeZ0/eZwFJmqZMnpQA= github.com/go-toolsmith/astp v1.1.0/go.mod h1:0T1xFGz9hicKs8Z5MfAqSUitoUYS30pDMsRVIDHs8CA= github.com/go-toolsmith/pkgload v1.2.2 h1:0CtmHq/02QhxcF7E9N5LIFcYFsMR5rdovfqTtRKkgIk= -github.com/go-toolsmith/pkgload v1.2.2/go.mod h1:R2hxLNRKuAsiXCo2i5J6ZQPhnPMOVtU+f0arbFPWCus= github.com/go-toolsmith/strparse v1.0.0/go.mod h1:YI2nUKP9YGZnL/L1/DLFBfixrcjslWct4wyljWhSRy8= github.com/go-toolsmith/strparse v1.1.0 h1:GAioeZUK9TGxnLS+qfdqNbA4z0SSm5zVNtCQiyP2Bvw= github.com/go-toolsmith/strparse v1.1.0/go.mod h1:7ksGy58fsaQkGQlY8WVoBFNyEPMGuJin1rfoPS4lBSQ= @@ -365,9 +355,7 @@ github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7a github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q= github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q= github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe h1:lXe2qZdvpiX5WZkZR4hgp4KJVfY3nMkvmwbVkpv1rVY= -github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0= github.com/golang-sql/sqlexp v0.0.0-20170517235910-f1bb20e5a188 h1:+eHOFJl1BaXrQxKX+T06f78590z4qA2ZzBTqahsKSE4= -github.com/golang-sql/sqlexp v0.0.0-20170517235910-f1bb20e5a188/go.mod h1:vXjM/+wXQnTPR4KqTKDgJukSZ6amVRtWMPEjE6sQoK8= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= github.com/golang/glog v1.1.2 h1:DVjP2PbBOzHyzA+dn3WhHIq4NdVu3Q+pvivFICf/7fo= github.com/golang/glog v1.1.2/go.mod h1:zR+okUeTbrL6EL3xHUDxZuEtGv04p5shwip1+mL/rLQ= @@ -446,7 +434,6 @@ github.com/google/go-containerregistry v0.13.0 h1:y1C7Z3e149OJbOPDBxLYR8ITPz8dTK github.com/google/go-containerregistry v0.13.0/go.mod h1:J9FQ+eSS4a1aC2GNZxvNpbWhgp0487v+cgiilB4FqDo= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/gofuzz v1.2.0 h1:xRy4A+RhZaiKjJ1bPfwQ8sedCA+YS2YcCHW6ec7JMi0= -github.com/google/gofuzz v1.2.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs= github.com/google/martian/v3 v3.0.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0= github.com/google/martian/v3 v3.1.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0= @@ -467,7 +454,6 @@ github.com/google/pprof v0.0.0-20230228050547-1710fef4ab10 h1:CqYfpuYIjnlNxM3msd github.com/google/pprof v0.0.0-20230228050547-1710fef4ab10/go.mod h1:79YE0hCXdHag9sBkw2o+N/YnZtTkXi0UT9Nnixa5eYk= github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI= github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4= -github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ= github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4= github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= @@ -490,7 +476,6 @@ github.com/gostaticanalysis/nilerr v0.1.1 h1:ThE+hJP0fEp4zWLkWHWcRyI2Od0p7DlgYG3 github.com/gostaticanalysis/nilerr v0.1.1/go.mod h1:wZYb6YI5YAxxq0i1+VJbY0s2YONW0HU0GPE3+5PWN4A= github.com/gostaticanalysis/testutil v0.3.1-0.20210208050101-bfb5c8eec0e4/go.mod h1:D+FIZ+7OahH3ePw/izIEeH5I06eKs1IKI4Xr64/Am3M= github.com/gostaticanalysis/testutil v0.4.0 h1:nhdCmubdmDF6VEatUNjgUZBJKWRqugoISdUv3PPQgHY= -github.com/gostaticanalysis/testutil v0.4.0/go.mod h1:bLIoPefWXrRi/ssLFWX1dx7Repi5x3CuviD3dgAZaBU= github.com/gotestyourself/gotestyourself v2.2.0+incompatible h1:AQwinXlbQR2HvPjQZOmDhRqsv5mZf+Jb1RnSLxcqZcI= github.com/gotestyourself/gotestyourself v2.2.0+incompatible/go.mod h1:zZKM6oeNM8k+FRljX1mnzVYeS8wiGgQyvST1/GafPbY= github.com/gtank/merlin v0.1.1-0.20191105220539-8318aed1a79f/go.mod h1:T86dnYJhcGOh5BjZFCJWTDeTK7XW8uE+E21Cy/bIQ+s= @@ -540,7 +525,6 @@ github.com/jessevdk/go-flags v1.5.0/go.mod h1:Fw0T6WPc1dYxT4mKEZRfG5kJhaTDP9pj1c github.com/jgautheron/goconst v1.5.1 h1:HxVbL1MhydKs8R8n/HE5NPvzfaYmQJA3o879lE4+WcM= github.com/jgautheron/goconst v1.5.1/go.mod h1:aAosetZ5zaeC/2EfMeRswtxUFBpe2Hr7HzkgX4fanO4= github.com/jhump/protoreflect v1.15.1 h1:HUMERORf3I3ZdX05WaQ6MIpd/NJ434hTp5YiKgfCL6c= -github.com/jhump/protoreflect v1.15.1/go.mod h1:jD/2GMKKE6OqX8qTjhADU1e6DShO+gavG9e0Q693nKo= github.com/jingyugao/rowserrcheck v1.1.1 h1:zibz55j/MJtLsjP1OF4bSdgXxwL1b+Vn7Tjzq7gFzUs= github.com/jingyugao/rowserrcheck v1.1.1/go.mod h1:4yvlZSDb3IyDTUZJUmpZfm2Hwok+Dtp+nu2qOq+er9c= github.com/jinzhu/copier v0.3.5 h1:GlvfUwHk62RokgqVNvYsku0TATCF7bAHVwEXoBh3iJg= @@ -585,7 +569,6 @@ github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFB github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= -github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= @@ -641,7 +624,6 @@ github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/ github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0= github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= github.com/mattn/go-sqlite3 v1.14.9 h1:10HX2Td0ocZpYEjhilsuo6WWtUqttj2Kb0KtD86/KYA= -github.com/mattn/go-sqlite3 v1.14.9/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= github.com/matttproud/golang_protobuf_extensions v1.0.4 h1:mmDVorXM7PCGKw94cs5zkfA9PSy5pEvNWRP0ET0TIVo= github.com/matttproud/golang_protobuf_extensions v1.0.4/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4= @@ -696,13 +678,11 @@ github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108 github.com/onsi/ginkgo v1.14.0 h1:2mOpI4JVVPBN+WQRa0WKH2eXR+Ey+uK4n7Zj0aYpIQA= github.com/onsi/ginkgo v1.14.0/go.mod h1:iSB4RoI2tjJc9BBv4NKIKWKya62Rps+oPG/Lv9klQyY= github.com/onsi/ginkgo/v2 v2.8.0 h1:pAM+oBNPrpXRs+E/8spkeGx9QgekbRVyr74EUvRVOUI= -github.com/onsi/ginkgo/v2 v2.8.0/go.mod h1:6JsQiECmxCa3V5st74AL/AmsV482EDdVrGaVW6z3oYU= github.com/onsi/gomega v1.4.1/go.mod h1:C1qb7wdrVGGVU+Z6iS04AVkA3Q65CEZX59MT0QO5uiA= github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY= github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo= github.com/onsi/gomega v1.26.0 h1:03cDLK28U6hWvCAns6NeydX3zIm4SF3ci69ulidS32Q= -github.com/onsi/gomega v1.26.0/go.mod h1:r+zV744Re+DiYCIPRlYOTxn0YkOLcAnW8k1xXdMPGhM= github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U= github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM= github.com/opencontainers/image-spec v1.1.0-rc2 h1:2zx/Stx4Wc5pIPDvIxHXvXtQFW/7XWJGmnM7r3wg034= @@ -714,7 +694,6 @@ github.com/opencontainers/selinux v1.10.0/go.mod h1:2i0OySw99QjzBBQByd1Gr9gSjvuh github.com/ory/dockertest v3.3.5+incompatible h1:iLLK6SQwIhcbrG783Dghaaa3WPzGc+4Emza6EbVUUGA= github.com/ory/dockertest v3.3.5+incompatible/go.mod h1:1vX4m9wsvi00u5bseYwXaSnhNrne+V0E6LAcBILJdPs= github.com/ory/dockertest/v3 v3.9.1 h1:v4dkG+dlu76goxMiTT2j8zV7s4oPPEppKT8K8p2f1kY= -github.com/ory/dockertest/v3 v3.9.1/go.mod h1:42Ir9hmvaAPm0Mgibk6mBPi7SFvTXxEcnztDYOJ//uM= github.com/otiai10/copy v1.2.0 h1:HvG945u96iNadPoG2/Ja2+AUJeW5YuFQMixq9yirC+k= github.com/otiai10/copy v1.2.0/go.mod h1:rrF5dJ5F0t/EWSYODDu4j9/vEeYHMkc8jt0zJChqQWw= github.com/otiai10/curr v0.0.0-20150429015615-9b4961190c95/go.mod h1:9qAhocn7zKJG+0mI8eUu6xqkFDYS2kb2saOteoSB3cE= @@ -787,7 +766,6 @@ github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 h1:N/ElC8H3+5X github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4= github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8= -github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= github.com/rs/cors v1.8.3 h1:O+qNyWn7Z+F9M0ILBHgMVPuB1xTOucVd5gtaYyXBpRo= github.com/rs/cors v1.8.3/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU= github.com/rs/xid v1.4.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= @@ -904,11 +882,8 @@ github.com/tenntenn/text/transform v0.0.0-20200319021203-7eef512accb3/go.mod h1: github.com/tetafro/godot v1.4.11 h1:BVoBIqAf/2QdbFmSwAWnaIqDivZdOV0ZRwEm6jivLKw= github.com/tetafro/godot v1.4.11/go.mod h1:LR3CJpxDVGlYOWn3ZZg1PgNZdTUvzsZWu8xaEohUpn8= github.com/tidwall/gjson v1.14.4 h1:uo0p8EbA09J7RQaflQ1aBRffTR7xedD2bcIVSYxLnkM= -github.com/tidwall/gjson v1.14.4/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA= -github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM= github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4= -github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= github.com/timakin/bodyclose v0.0.0-20221125081123-e39cf3fc478e h1:MV6KaVu/hzByHP0UvJ4HcMGE/8a6A4Rggc/0wx2AvJo= github.com/timakin/bodyclose v0.0.0-20221125081123-e39cf3fc478e/go.mod h1:27bSVNWSBOHm+qRp1T9qzaIpsWEP6TbUnei/43HK+PQ= github.com/timonwong/loggercheck v0.9.4 h1:HKKhqrjcVj8sxL7K77beXh0adEm6DLjV/QOGeMXEVi4= @@ -931,7 +906,6 @@ github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyC github.com/valyala/fasttemplate v1.0.1/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPUpymEIMZ47gx8= github.com/valyala/fasttemplate v1.2.1/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ= github.com/vbatts/tar-split v0.11.2 h1:Via6XqJr0hceW4wff3QRzD5gAk/tatMw/4ZA7cTlIME= -github.com/vbatts/tar-split v0.11.2/go.mod h1:vV3ZuO2yWSVsz+pfFzDG/upWH1JhjOiEaWq6kXyQ3VI= github.com/vektra/mockery/v2 v2.23.1 h1:N59FENM2d/gWE6Ns5JPuf9a7jqQWeheGefZqvuvb1dM= github.com/vektra/mockery/v2 v2.23.1/go.mod h1:Zh3Kv1ckKs6FokhlVLcCu6UTyzfS3M8mpROz1lBNp+w= github.com/vishvananda/netlink v1.1.0/go.mod h1:cTgwzPIzzgDAYoQrMm0EdrjRUBkTqKYppBueQtXaqoE= @@ -939,11 +913,8 @@ github.com/vishvananda/netns v0.0.0-20191106174202-0a2b9b5464df/go.mod h1:JP3t17 github.com/xanzy/ssh-agent v0.3.3 h1:+/15pJfg/RsTxqYcX6fHqOXZwwMP+2VyYWJeWM2qQFM= github.com/xanzy/ssh-agent v0.3.3/go.mod h1:6dzNDKs0J9rVPHPhaGCukekBHKqfl+L3KghI1Bc68Uw= github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb h1:zGWFAtiMcyryUHoUjUJX0/lt1H2+i2Ka2n+D3DImSNo= -github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU= github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 h1:EzJWgHovont7NscjpAxXsDA8S8BMYve8Y5+7cuRE7R0= -github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ= github.com/xeipuuv/gojsonschema v1.2.0 h1:LhYJRs+L4fBtjZUfuSZIKGeVu0QRy8e5Xi7D17UxZ74= -github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y= github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q= github.com/yagipy/maintidx v1.0.0 h1:h5NvIsCz+nRDapQ0exNv4aJ0yXSI0420omVANTv3GJM= github.com/yagipy/maintidx v1.0.0/go.mod h1:0qNf/I/CCZXSMhsRsrEPDZ+DkekpKLXAJfsTACwgXLk= @@ -979,7 +950,6 @@ go.opentelemetry.io/otel/trace v1.15.1/go.mod h1:IWdQG/5N1x7f6YUlmdLeJvH9yxtuJAf go.uber.org/atomic v1.10.0 h1:9qC72Qh0+3MqyJbAn8YU5xVq1frD8bn3JtD2oXtafVQ= go.uber.org/atomic v1.10.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0= go.uber.org/goleak v1.1.11 h1:wy28qYRKZgnJTxGxvye5/wgWr1EKjmUDGYox5mGlRlI= -go.uber.org/goleak v1.1.11/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ= go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ= go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= go.uber.org/zap v1.24.0 h1:FiJd5l1UOLj0wCgbSE0rwwXHzEdAZS6hiiSnxJN/D60= @@ -1254,7 +1224,6 @@ golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxb golang.org/x/time v0.0.0-20201208040808-7e3f01d25324/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20210220033141-f8bda1e9f3ba/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.1.0 h1:xYY+Bajn2a7VBmTM5GikTmnK8ZuX8YgnQCqZpbBNtmA= -golang.org/x/time v0.1.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/tools v0.0.0-20180525024113-a5b4c53f6e8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= @@ -1463,7 +1432,6 @@ gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo= gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw= gotest.tools/v3 v3.0.3 h1:4AuOwCGf4lLR9u3YOe2awrHygurzhO/HeQ6laiA6Sx0= -gotest.tools/v3 v3.0.3/go.mod h1:Z7Lb0S5l+klDB31fvDQX8ss/FlKDxtlFlw3Oa8Ymbl8= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= From 1cb6666606c7b9a5636c6da1508e351c64320fe5 Mon Sep 17 00:00:00 2001 From: Callum Waters <cmwaters19@gmail.com> Date: Fri, 26 Jan 2024 18:29:05 -0600 Subject: [PATCH 008/127] fix tests --- consensus/reactor.go | 8 +++++++ consensus/reactor_test.go | 3 ++- mempool/cat/block_builder.go | 8 +++---- mempool/cat/block_builder_test.go | 12 +++++----- mempool/cat/pool.go | 38 +++++++++++++++++++------------ mempool/cat/pool_test.go | 9 ++++++-- mempool/cat/reactor.go | 15 ++++++++++-- mempool/cat/store.go | 25 ++++++++++++++++++++ state/execution.go | 2 +- test/e2e/docker/Dockerfile | 2 +- test/e2e/networks/ci.toml | 2 -- test/e2e/networks/simple.toml | 3 --- test/e2e/runner/setup.go | 1 - 13 files changed, 90 insertions(+), 38 deletions(-) diff --git a/consensus/reactor.go b/consensus/reactor.go index c2987bdb45..b7c067b0be 100644 --- a/consensus/reactor.go +++ b/consensus/reactor.go @@ -1368,6 +1368,14 @@ func (ps *PeerState) BlockPartsSent() int { return ps.Stats.BlockParts } +// HasBlock returns true if the peer has received a valid block for the current round +func (ps *PeerState) HasBlock() bool { + ps.mtx.Lock() + defer ps.mtx.Unlock() + + return ps.PRS.Block +} + // SetHasVote sets the given vote as known by the peer func (ps *PeerState) SetHasVote(vote *types.Vote) { ps.mtx.Lock() diff --git a/consensus/reactor_test.go b/consensus/reactor_test.go index a521353367..df5ab38156 100644 --- a/consensus/reactor_test.go +++ b/consensus/reactor_test.go @@ -367,7 +367,7 @@ func TestReactorRecordsVotesAndBlockParts(t *testing.T) { ps := peer.Get(types.PeerStateKey).(*PeerState) assert.Equal(t, true, ps.VotesSent() > 0, "number of votes sent should have increased") - assert.Equal(t, true, ps.BlockPartsSent() > 0, "number of votes sent should have increased") + assert.Equal(t, true, ps.HasBlock(), "should have block") } //------------------------------------------------------------- @@ -1056,6 +1056,7 @@ func TestMarshalJSONPeerState(t *testing.T) { "step": 0, "start_time": "0001-01-01T00:00:00Z", "proposal": false, + "block": false, "proposal_block_part_set_header": {"total":0, "hash":""}, "proposal_block_parts": null, diff --git a/mempool/cat/block_builder.go b/mempool/cat/block_builder.go index 648f87d6fe..fd7ce40ec0 100644 --- a/mempool/cat/block_builder.go +++ b/mempool/cat/block_builder.go @@ -52,7 +52,7 @@ func (memR *Reactor) FetchTxsFromKeys(ctx context.Context, blockID []byte, compa memR.mempool.jsonMetrics.Unlock() // setup a request for this block and begin to track and retrieve all missing transactions - request := memR.blockFetcher.NewRequest( + request := memR.blockFetcher.newRequest( blockID, memR.mempool.Height(), missingKeys, @@ -124,8 +124,8 @@ type blockFetcher struct { requests map[string]*blockRequest } -// NewBlockFetcher returns a new blockFetcher for managing block requests -func NewBlockFetcher() *blockFetcher { +// newBlockFetcher returns a new blockFetcher for managing block requests +func newBlockFetcher() *blockFetcher { return &blockFetcher{ requests: make(map[string]*blockRequest), } @@ -140,7 +140,7 @@ func (bf *blockFetcher) GetRequest(blockID []byte) (*blockRequest, bool) { // NewRequest creates a new block request and returns it. // If a request already exists it returns that instead -func (bf *blockFetcher) NewRequest( +func (bf *blockFetcher) newRequest( blockID []byte, height int64, missingKeys map[int]types.TxKey, diff --git a/mempool/cat/block_builder_test.go b/mempool/cat/block_builder_test.go index dd015eb8dd..f63b187d2c 100644 --- a/mempool/cat/block_builder_test.go +++ b/mempool/cat/block_builder_test.go @@ -91,23 +91,23 @@ func TestBlockRequestConcurrently(t *testing.T) { } func TestBlockFetcherSimple(t *testing.T) { - bf := NewBlockFetcher() + bf := newBlockFetcher() tx := types.Tx("hello world") key := tx.Key() missingKeys := map[int]types.TxKey{ 0: key, } blockID := []byte("blockID") - req := bf.NewRequest(blockID, 1, missingKeys, make([][]byte, 1)) + req := bf.newRequest(blockID, 1, missingKeys, make([][]byte, 1)) req2, ok := bf.GetRequest(blockID) require.True(t, ok) require.Equal(t, req, req2) // a different request for the same blockID should // return the same original request object. - req3 := bf.NewRequest(blockID, 2, missingKeys, make([][]byte, 2)) + req3 := bf.newRequest(blockID, 2, missingKeys, make([][]byte, 2)) require.Equal(t, req, req3) - req4 := bf.NewRequest([]byte("differentBlockID"), 1, missingKeys, make([][]byte, 1)) + req4 := bf.newRequest([]byte("differentBlockID"), 1, missingKeys, make([][]byte, 1)) bf.TryAddMissingTx(key, tx) require.False(t, req4.TryAddMissingTx(key, tx)) @@ -117,7 +117,7 @@ func TestBlockFetcherSimple(t *testing.T) { func TestBlockFetcherConcurrentRequests(t *testing.T) { var ( - bf = NewBlockFetcher() + bf = newBlockFetcher() numBlocks = 5 numRequestsPerBlock = 5 numTxs = 5 @@ -154,7 +154,7 @@ func TestBlockFetcherConcurrentRequests(t *testing.T) { } txsCopy := make([][]byte, len(txs)) copy(txsCopy, txs) - request := bf.NewRequest(blockID, 1, mk, txs) + request := bf.newRequest(blockID, 1, mk, txs) if routine == 0 { requestWG.Done() } diff --git a/mempool/cat/pool.go b/mempool/cat/pool.go index c84f225c91..4606f82a8e 100644 --- a/mempool/cat/pool.go +++ b/mempool/cat/pool.go @@ -194,6 +194,16 @@ func (txmp *TxPool) Get(txKey types.TxKey) (types.Tx, bool) { return types.Tx{}, false } +// GetCommitted retrieves a committed transaction based on the key. +// It returns the transaction and a bool indicating if the transaction exists or not. +func (txmp *TxPool) GetCommitted(txKey types.TxKey) (types.Tx, bool) { + wtx := txmp.store.getCommitted(txKey) + if wtx != nil { + return wtx.tx, true + } + return types.Tx{}, false +} + // IsRejectedTx returns true if the transaction was recently rejected and is // currently within the cache func (txmp *TxPool) IsRejectedTx(txKey types.TxKey) bool { @@ -374,7 +384,6 @@ func (txmp *TxPool) RemoveTxByKey(txKey types.TxKey) error { func (txmp *TxPool) removeTxByKey(txKey types.TxKey) { txmp.rejectedTxCache.Push(txKey) _ = txmp.store.remove(txKey) - txmp.seenByPeersSet.RemoveKey(txKey) } // Flush purges the contents of the mempool and the cache, leaving both empty. @@ -501,19 +510,25 @@ func (txmp *TxPool) Update( txmp.metrics.SuccessfulTxs.Add(float64(len(blockTxs))) - for txKey := range txmp.committedCache { - // Remove the transaction from the mempool. - txmp.removeTxByKey(txKey) - } + txmp.store.clearCommitted() // add the recently committed transactions to the cache - txmp.committedCache = make(map[types.TxKey]struct{}) - for _, tx := range blockTxs { - txmp.committedCache[tx.Key()] = struct{}{} + keys := make([]types.TxKey, len(blockTxs)) + for idx, tx := range blockTxs { + keys[idx] = tx.Key() + // this prevents the node from reprocessing recently committed transactions + txmp.rejectedTxCache.Push(keys[idx]) } + txmp.store.markAsCommitted(keys) + // purge transactions that are past the TTL txmp.purgeExpiredTxs(blockHeight) + // prune record of peers seen transactions after an hour + // We assume by then that the transaction will no longer + // need to be requested + txmp.seenByPeersSet.Prune(time.Now().Add(time.Hour)) + // If there any uncommitted transactions left in the mempool, we either // initiate re-CheckTx per remaining transaction or notify that remaining // transactions are left. @@ -739,13 +754,6 @@ func (txmp *TxPool) purgeExpiredTxs(blockHeight int64) { numExpired := txmp.store.purgeExpiredTxs(expirationHeight, expirationAge) txmp.metrics.EvictedTxs.Add(float64(numExpired)) - - // purge old evicted and seen transactions - if txmp.config.TTLDuration == 0 { - // ensure that seenByPeersSet are eventually pruned - expirationAge = now.Add(-time.Hour) - } - txmp.seenByPeersSet.Prune(expirationAge) } func (txmp *TxPool) notifyTxsAvailable() { diff --git a/mempool/cat/pool_test.go b/mempool/cat/pool_test.go index ec954ae9c5..18dbaad61c 100644 --- a/mempool/cat/pool_test.go +++ b/mempool/cat/pool_test.go @@ -210,6 +210,8 @@ func TestTxPool_Size(t *testing.T) { txmp.Lock() require.NoError(t, txmp.Update(1, rawTxs[:50], responses, nil, nil)) + // txs are removed with a one block delay so we need to call this twice + require.NoError(t, txmp.Update(1, nil, nil, nil, nil)) txmp.Unlock() require.Equal(t, len(rawTxs)/2, txmp.Size()) @@ -288,6 +290,8 @@ func TestTxPool_Eviction(t *testing.T) { // Free up some space so we can add back previously evicted txs err = txmp.Update(1, types.Txs{types.Tx("key10=0123456789abcdef=11")}, []*abci.ResponseDeliverTx{{Code: abci.CodeTypeOK}}, nil, nil) require.NoError(t, err) + // need to call Update twice to actually remove the transaction + _ = txmp.Update(2, nil, nil, nil, nil) require.False(t, txExists("key10=0123456789abcdef=11")) mustCheckTx(t, txmp, "key3=0002=10") require.True(t, txExists("key3=0002=10")) @@ -598,9 +602,8 @@ func TestTxPool_ExpiredTxs_NumBlocks(t *testing.T) { responses[i] = &abci.ResponseDeliverTx{Code: abci.CodeTypeOK} } - txmp.Lock() require.NoError(t, txmp.Update(txmp.height+1, reapedTxs, responses, nil, nil)) - txmp.Unlock() + require.NoError(t, txmp.Update(txmp.height+1, nil, nil, nil, nil)) require.Equal(t, 95, txmp.Size()) @@ -695,6 +698,8 @@ func TestTxPool_RemoveBlobTx(t *testing.T) { err = txmp.Update(1, []types.Tx{indexWrapper}, abciResponses(1, abci.CodeTypeOK), nil, nil) require.NoError(t, err) + err = txmp.Update(1, nil, nil, nil, nil) + require.NoError(t, err) require.EqualValues(t, 0, txmp.Size()) require.EqualValues(t, 0, txmp.SizeBytes()) } diff --git a/mempool/cat/reactor.go b/mempool/cat/reactor.go index 00d5dedcde..4cfff65cd5 100644 --- a/mempool/cat/reactor.go +++ b/mempool/cat/reactor.go @@ -92,7 +92,7 @@ func NewReactor(mempool *TxPool, opts *ReactorOptions) (*Reactor, error) { mempool: mempool, ids: newMempoolIDs(), requests: newRequestScheduler(opts.MaxGossipDelay, defaultGlobalRequestTimeout), - blockFetcher: NewBlockFetcher(), + blockFetcher: newBlockFetcher(), traceClient: &trace.Client{}, } memR.BaseReactor = *p2p.NewBaseReactor("Mempool", memR) @@ -187,6 +187,14 @@ func (memR *Reactor) InitPeer(peer p2p.Peer) p2p.Peer { return peer } +// AddPeer broadcasts all the transactions that this node has seen +func (memR *Reactor) AddPeer(peer p2p.Peer) { + keys := memR.mempool.store.getAllKeys() + for _, key := range keys { + memR.broadcastSeenTx(key) + } +} + // RemovePeer implements Reactor. For all current outbound requests to this // peer it will find a new peer to rerequest the same transactions. func (memR *Reactor) RemovePeer(peer p2p.Peer, reason interface{}) { @@ -195,7 +203,6 @@ func (memR *Reactor) RemovePeer(peer p2p.Peer, reason interface{}) { // we won't receive any responses from them. outboundRequests := memR.requests.ClearAllRequestsFrom(peerID) for key := range outboundRequests { - memR.mempool.metrics.RequestedTxs.Add(1) memR.findNewPeerToRequestTx(key) } } @@ -323,6 +330,10 @@ func (memR *Reactor) ReceiveEnvelope(e p2p.Envelope) { return } tx, has := memR.mempool.Get(txKey) + if !has { + // see if the tx was recently committed + tx, has = memR.mempool.GetCommitted(txKey) + } if has && !memR.opts.ListenOnly { peerID := memR.ids.GetIDForPeer(e.Src.ID()) schema.WriteMempoolTx( diff --git a/mempool/cat/store.go b/mempool/cat/store.go index 94ac9e0b2a..d8a5e09b43 100644 --- a/mempool/cat/store.go +++ b/mempool/cat/store.go @@ -12,6 +12,7 @@ type store struct { mtx sync.RWMutex bytes int64 txs map[types.TxKey]*wrappedTx + committedTxs map[types.TxKey]*wrappedTx } func newStore() *store { @@ -41,6 +42,12 @@ func (s *store) get(txKey types.TxKey) *wrappedTx { return s.txs[txKey] } +func (s *store) getCommitted(txKey types.TxKey) *wrappedTx { + s.mtx.RLock() + defer s.mtx.RUnlock() + return s.committedTxs[txKey] +} + func (s *store) has(txKey types.TxKey) bool { s.mtx.RLock() defer s.mtx.RUnlock() @@ -48,6 +55,24 @@ func (s *store) has(txKey types.TxKey) bool { return has } +func (s *store) markAsCommitted(txKeys []types.TxKey) { + s.mtx.Lock() + defer s.mtx.Unlock() + for _, key := range txKeys { + if tx, exists := s.txs[key]; exists { + s.bytes -= tx.size() + delete(s.txs, key) + s.committedTxs[key] = tx + } + } +} + +func (s *store) clearCommitted() { + s.mtx.Lock() + defer s.mtx.Unlock() + s.committedTxs = make(map[types.TxKey]*wrappedTx) +} + func (s *store) remove(txKey types.TxKey) bool { s.mtx.Lock() defer s.mtx.Unlock() diff --git a/state/execution.go b/state/execution.go index d0dfc707f9..aabea931f7 100644 --- a/state/execution.go +++ b/state/execution.go @@ -255,7 +255,7 @@ func (blockExec *BlockExecutor) ApplyBlock( // Update the state with the block and responses. state, err = updateState(state, blockID, &block.Header, abciResponses, validatorUpdates) if err != nil { - return state, 0, fmt.Errorf("commit failed for application: %v", err) + return state, 0, fmt.Errorf("failed to update state: %v", err) } // Lock mempool, commit app state, update mempoool. diff --git a/test/e2e/docker/Dockerfile b/test/e2e/docker/Dockerfile index f0fab31982..c81db55fc8 100644 --- a/test/e2e/docker/Dockerfile +++ b/test/e2e/docker/Dockerfile @@ -1,7 +1,7 @@ # We need to build in a Linux environment to support C libraries, e.g. RocksDB. # We use Debian instead of Alpine, so that we can use binary database packages # instead of spending time compiling them. -FROM golang:1.21.6-bullseye +FROM golang:1.20-bullseye RUN apt-get -qq update -y && apt-get -qq upgrade -y >/dev/null RUN apt-get -qq install -y libleveldb-dev librocksdb-dev >/dev/null diff --git a/test/e2e/networks/ci.toml b/test/e2e/networks/ci.toml index 480b77b708..cb54439502 100644 --- a/test/e2e/networks/ci.toml +++ b/test/e2e/networks/ci.toml @@ -6,8 +6,6 @@ initial_height = 1000 initial_state = { initial01 = "a", initial02 = "b", initial03 = "c" } # The most common case (e.g. Cosmos SDK-based chains). abci_protocol = "builtin" -load_tx_connections = 10 -load_tx_batch_size = 10 [validators] validator01 = 100 diff --git a/test/e2e/networks/simple.toml b/test/e2e/networks/simple.toml index 791705cf65..96b81f79fe 100644 --- a/test/e2e/networks/simple.toml +++ b/test/e2e/networks/simple.toml @@ -1,6 +1,3 @@ -load_tx_connections = 10 -load_tx_batch_size = 10 - [node.validator01] [node.validator02] [node.validator03] diff --git a/test/e2e/runner/setup.go b/test/e2e/runner/setup.go index d240b96856..8fdecf6a77 100644 --- a/test/e2e/runner/setup.go +++ b/test/e2e/runner/setup.go @@ -157,7 +157,6 @@ func MakeGenesis(testnet *e2e.Testnet) (types.GenesisDoc, error) { // MakeConfig generates a CometBFT config for a node. func MakeConfig(node *e2e.Node) (*config.Config, error) { cfg := config.DefaultConfig() - cfg.Consensus.TimeoutPropose = 5 * time.Second cfg.Moniker = node.Name cfg.ProxyApp = AppAddressTCP cfg.RPC.ListenAddress = "tcp://0.0.0.0:26657" From 4874e7703ad2b7a48717be87fcabf9516ff9b54e Mon Sep 17 00:00:00 2001 From: Callum Waters <cmwaters19@gmail.com> Date: Fri, 26 Jan 2024 18:34:23 -0600 Subject: [PATCH 009/127] fix consensus tests --- consensus/reactor.go | 10 +--------- consensus/reactor_test.go | 2 +- 2 files changed, 2 insertions(+), 10 deletions(-) diff --git a/consensus/reactor.go b/consensus/reactor.go index b7c067b0be..f59019acc6 100644 --- a/consensus/reactor.go +++ b/consensus/reactor.go @@ -986,7 +986,7 @@ func (conR *Reactor) peerStatsRoutine() { if numVotes := ps.RecordVote(); numVotes%votesToContributeToBecomeGoodPeer == 0 { conR.Switch.MarkPeerAsGood(peer) } - case *BlockPartMessage: + case *BlockPartMessage, *CompactBlockMessage: if numParts := ps.RecordBlockPart(); numParts%blocksToContributeToBecomeGoodPeer == 0 { conR.Switch.MarkPeerAsGood(peer) } @@ -1368,14 +1368,6 @@ func (ps *PeerState) BlockPartsSent() int { return ps.Stats.BlockParts } -// HasBlock returns true if the peer has received a valid block for the current round -func (ps *PeerState) HasBlock() bool { - ps.mtx.Lock() - defer ps.mtx.Unlock() - - return ps.PRS.Block -} - // SetHasVote sets the given vote as known by the peer func (ps *PeerState) SetHasVote(vote *types.Vote) { ps.mtx.Lock() diff --git a/consensus/reactor_test.go b/consensus/reactor_test.go index df5ab38156..7b5a36e9b5 100644 --- a/consensus/reactor_test.go +++ b/consensus/reactor_test.go @@ -367,7 +367,7 @@ func TestReactorRecordsVotesAndBlockParts(t *testing.T) { ps := peer.Get(types.PeerStateKey).(*PeerState) assert.Equal(t, true, ps.VotesSent() > 0, "number of votes sent should have increased") - assert.Equal(t, true, ps.HasBlock(), "should have block") + assert.Equal(t, true, ps.BlockPartsSent(), "should have block") } //------------------------------------------------------------- From 187d26c36b5c16aa8e2a5ee061b2c7784fbd9a14 Mon Sep 17 00:00:00 2001 From: Callum Waters <cmwaters19@gmail.com> Date: Fri, 26 Jan 2024 18:38:06 -0600 Subject: [PATCH 010/127] fix test I just broke --- consensus/reactor_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/consensus/reactor_test.go b/consensus/reactor_test.go index 7b5a36e9b5..725d2398f5 100644 --- a/consensus/reactor_test.go +++ b/consensus/reactor_test.go @@ -367,7 +367,7 @@ func TestReactorRecordsVotesAndBlockParts(t *testing.T) { ps := peer.Get(types.PeerStateKey).(*PeerState) assert.Equal(t, true, ps.VotesSent() > 0, "number of votes sent should have increased") - assert.Equal(t, true, ps.BlockPartsSent(), "should have block") + assert.Equal(t, true, ps.BlockPartsSent() > 0, "should have block") } //------------------------------------------------------------- From 0f529f65c5110ece0ad81d53d64452a390072f61 Mon Sep 17 00:00:00 2001 From: Callum Waters <cmwaters19@gmail.com> Date: Fri, 26 Jan 2024 18:53:43 -0600 Subject: [PATCH 011/127] linting --- consensus/reactor_test.go | 1 - mempool/cat/pool.go | 3 --- mempool/cat/pool_test.go | 2 -- mempool/cat/store.go | 6 +++--- 4 files changed, 3 insertions(+), 9 deletions(-) diff --git a/consensus/reactor_test.go b/consensus/reactor_test.go index 725d2398f5..4f6fab8ebd 100644 --- a/consensus/reactor_test.go +++ b/consensus/reactor_test.go @@ -367,7 +367,6 @@ func TestReactorRecordsVotesAndBlockParts(t *testing.T) { ps := peer.Get(types.PeerStateKey).(*PeerState) assert.Equal(t, true, ps.VotesSent() > 0, "number of votes sent should have increased") - assert.Equal(t, true, ps.BlockPartsSent() > 0, "should have block") } //------------------------------------------------------------- diff --git a/mempool/cat/pool.go b/mempool/cat/pool.go index 4606f82a8e..8159493060 100644 --- a/mempool/cat/pool.go +++ b/mempool/cat/pool.go @@ -64,9 +64,6 @@ type TxPool struct { postCheckFn mempool.PostCheckFunc height int64 // the latest height passed to Update lastPurgeTime time.Time // the last time we attempted to purge transactions via the TTL - // cache of committed transactions to be pruned the following height - // we don't want to prune immediately because lagging nodes may still be requesting these transactions - committedCache map[types.TxKey]struct{} // Thread-safe cache of rejected transactions for quick look-up rejectedTxCache *LRUTxCache diff --git a/mempool/cat/pool_test.go b/mempool/cat/pool_test.go index 18dbaad61c..a7cd549806 100644 --- a/mempool/cat/pool_test.go +++ b/mempool/cat/pool_test.go @@ -565,8 +565,6 @@ func TestTxPool_ExpiredTxs_Timestamp(t *testing.T) { time.Sleep(3 * time.Millisecond) // Trigger an update so that pruning will occur. - txmp.Lock() - defer txmp.Unlock() require.NoError(t, txmp.Update(txmp.height+1, nil, nil, nil, nil)) // All the transactions in the original set should have been purged. diff --git a/mempool/cat/store.go b/mempool/cat/store.go index d8a5e09b43..376a06381b 100644 --- a/mempool/cat/store.go +++ b/mempool/cat/store.go @@ -9,9 +9,9 @@ import ( // simple, thread-safe in memory store for transactions type store struct { - mtx sync.RWMutex - bytes int64 - txs map[types.TxKey]*wrappedTx + mtx sync.RWMutex + bytes int64 + txs map[types.TxKey]*wrappedTx committedTxs map[types.TxKey]*wrappedTx } From 64d3f6d54d1558521e47487baf6ceeb5faa61af1 Mon Sep 17 00:00:00 2001 From: Callum Waters <cmwaters19@gmail.com> Date: Fri, 26 Jan 2024 18:56:31 -0600 Subject: [PATCH 012/127] lint --- mempool/cat/block_builder.go | 4 ++-- mempool/cat/block_builder_test.go | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/mempool/cat/block_builder.go b/mempool/cat/block_builder.go index fd7ce40ec0..c2ec90ced7 100644 --- a/mempool/cat/block_builder.go +++ b/mempool/cat/block_builder.go @@ -151,7 +151,7 @@ func (bf *blockFetcher) newRequest( if request, ok := bf.requests[string(blockID)]; ok { return request } - request := NewBlockRequest(height, missingKeys, txs) + request := newBlockRequest(height, missingKeys, txs) bf.requests[string(blockID)] = request bf.pruneOldRequests(height) return request @@ -194,7 +194,7 @@ type blockRequest struct { endTime time.Time } -func NewBlockRequest( +func newBlockRequest( height int64, missingKeys map[int]types.TxKey, txs [][]byte, diff --git a/mempool/cat/block_builder_test.go b/mempool/cat/block_builder_test.go index f63b187d2c..2d89e71399 100644 --- a/mempool/cat/block_builder_test.go +++ b/mempool/cat/block_builder_test.go @@ -27,7 +27,7 @@ func TestBlockRequest(t *testing.T) { 1: key2, } - request := NewBlockRequest(1, missingKeys, txs) + request := newBlockRequest(1, missingKeys, txs) require.True(t, request.TryAddMissingTx(key1, tx1)) // cannot add the same missing tx twice @@ -69,7 +69,7 @@ func TestBlockRequestConcurrently(t *testing.T) { } } - request := NewBlockRequest(1, missingKeys, txs) + request := newBlockRequest(1, missingKeys, txs) wg := sync.WaitGroup{} for i := 0; i < numTxs; i++ { From c530d1e2066b2be145d6679837df5ba7293c87c0 Mon Sep 17 00:00:00 2001 From: Callum Waters <cmwaters19@gmail.com> Date: Fri, 26 Jan 2024 19:11:44 -0600 Subject: [PATCH 013/127] fix data race --- mempool/cat/block_builder_test.go | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/mempool/cat/block_builder_test.go b/mempool/cat/block_builder_test.go index 2d89e71399..3336dad7b5 100644 --- a/mempool/cat/block_builder_test.go +++ b/mempool/cat/block_builder_test.go @@ -121,7 +121,6 @@ func TestBlockFetcherConcurrentRequests(t *testing.T) { numBlocks = 5 numRequestsPerBlock = 5 numTxs = 5 - requestWG = sync.WaitGroup{} goRoutinesWG = sync.WaitGroup{} allTxs = make([][]byte, numTxs) txs = make([][]byte, numTxs) @@ -142,7 +141,6 @@ func TestBlockFetcherConcurrentRequests(t *testing.T) { } for i := 0; i < numBlocks; i++ { - requestWG.Add(1) for j := 0; j < numRequestsPerBlock; j++ { goRoutinesWG.Add(1) go func(blockID []byte, routine int) { @@ -155,17 +153,16 @@ func TestBlockFetcherConcurrentRequests(t *testing.T) { txsCopy := make([][]byte, len(txs)) copy(txsCopy, txs) request := bf.newRequest(blockID, 1, mk, txs) - if routine == 0 { - requestWG.Done() - } _, _ = request.WaitForBlock(ctx) }([]byte(fmt.Sprintf("blockID%d", i)), j) } + } + goRoutinesWG.Wait() + + for i := 0; i < numBlocks; i++ { goRoutinesWG.Add(1) go func() { defer goRoutinesWG.Done() - // Wait until all the request have started - requestWG.Wait() for _, tx := range allTxs { bf.TryAddMissingTx(types.Tx(tx).Key(), tx) } From 3e6c0753fbb5b46ee7ce35a72de36e6f22186e28 Mon Sep 17 00:00:00 2001 From: Callum Waters <cmwaters19@gmail.com> Date: Fri, 26 Jan 2024 19:14:33 -0600 Subject: [PATCH 014/127] go fmt --- mempool/cat/block_builder_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mempool/cat/block_builder_test.go b/mempool/cat/block_builder_test.go index 3336dad7b5..a1b9b3ef50 100644 --- a/mempool/cat/block_builder_test.go +++ b/mempool/cat/block_builder_test.go @@ -158,7 +158,7 @@ func TestBlockFetcherConcurrentRequests(t *testing.T) { } } goRoutinesWG.Wait() - + for i := 0; i < numBlocks; i++ { goRoutinesWG.Add(1) go func() { From 9ab18d4dcbd4de8d3a381693eb8904189ebf584f Mon Sep 17 00:00:00 2001 From: Callum Waters <cmwaters19@gmail.com> Date: Fri, 19 Jul 2024 16:02:42 +0200 Subject: [PATCH 015/127] gofmt --- state/mocks/block_store.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/state/mocks/block_store.go b/state/mocks/block_store.go index ee7d9aa5db..b726c5e0c1 100644 --- a/state/mocks/block_store.go +++ b/state/mocks/block_store.go @@ -5,8 +5,8 @@ package mocks import ( mock "github.com/stretchr/testify/mock" - types "github.com/tendermint/tendermint/types" cmtstore "github.com/tendermint/tendermint/proto/tendermint/store" + types "github.com/tendermint/tendermint/types" ) // BlockStore is an autogenerated mock type for the BlockStore type From 8f63838a4092bffdc150fff095f03006cb90370b Mon Sep 17 00:00:00 2001 From: Callum Waters <cmwaters19@gmail.com> Date: Fri, 19 Jul 2024 16:50:12 +0200 Subject: [PATCH 016/127] save metrics --- consensus/state.go | 2 ++ mempool/cat/pool.go | 1 + 2 files changed, 3 insertions(+) diff --git a/consensus/state.go b/consensus/state.go index 33d2a99d33..a557c5294f 100644 --- a/consensus/state.go +++ b/consensus/state.go @@ -1801,6 +1801,8 @@ func (cs *State) finalizeCommit(height int64) { // NewHeightStep! cs.updateToState(stateCopy) + cs.jsonMetrics.Save() + fail.Fail() // XXX // Private validator might have changed it's key pair => refetch pubkey. diff --git a/mempool/cat/pool.go b/mempool/cat/pool.go index 1cb0ae33b1..0d98632a29 100644 --- a/mempool/cat/pool.go +++ b/mempool/cat/pool.go @@ -559,6 +559,7 @@ func (txmp *TxPool) Update( txmp.notifyTxsAvailable() } } + txmp.jsonMetrics.Save() return nil } From 71b752ef662e1ecc56459126899792d90603ea82 Mon Sep 17 00:00:00 2001 From: Callum Waters <cmwaters19@gmail.com> Date: Fri, 19 Jul 2024 17:02:42 +0200 Subject: [PATCH 017/127] enable metrics --- consensus/metrics.go | 28 +++++++++++++--------------- consensus/reactor.go | 2 ++ consensus/state.go | 8 +++++++- mempool/cat/pool.go | 5 ++++- 4 files changed, 26 insertions(+), 17 deletions(-) diff --git a/consensus/metrics.go b/consensus/metrics.go index 2f454e17b3..303f0f0cd5 100644 --- a/consensus/metrics.go +++ b/consensus/metrics.go @@ -334,18 +334,17 @@ func (m *Metrics) MarkStep(s cstypes.RoundStepType) { } type JSONMetrics struct { - dir string - interval int - StartTime time.Time - EndTime time.Time - Blocks uint64 - Rounds uint64 - SentConsensusBytes uint64 - SentCompactBlocks uint64 - SentCompactBytes uint64 - CompactBlockFailures uint64 - SentBlockParts uint64 - SentBlockPartsBytes uint64 + dir string + interval int + StartTime time.Time + EndTime time.Time + Blocks uint64 + Rounds uint64 + SentCompactBlocks uint64 + CompactBlockFailures uint64 + SentBlockParts uint64 + ReceivedBlockParts uint64 + ReceivedCompactBlocks uint64 } func NewJSONMetrics(dir string) *JSONMetrics { @@ -371,10 +370,9 @@ func (m *JSONMetrics) Save() { func (m *JSONMetrics) reset() { m.Blocks = 0 m.Rounds = 0 - m.SentConsensusBytes = 0 m.SentBlockParts = 0 - m.SentBlockPartsBytes = 0 m.SentCompactBlocks = 0 - m.SentCompactBytes = 0 m.CompactBlockFailures = 0 + m.ReceivedBlockParts = 0 + m.ReceivedCompactBlocks = 0 } diff --git a/consensus/reactor.go b/consensus/reactor.go index e1fa75a972..f55fe6647d 100644 --- a/consensus/reactor.go +++ b/consensus/reactor.go @@ -694,6 +694,7 @@ OUTER_LOOP: }, }, logger) { ps.SetHasBlock(prs.Height, prs.Round) + conR.conS.jsonMetrics.SentCompactBlocks++ } continue OUTER_LOOP } @@ -840,6 +841,7 @@ func (conR *Reactor) gossipDataForCatchup(logger log.Logger, rs *cstypes.RoundSt string(peer.ID()), schema.Upload, ) + conR.conS.jsonMetrics.SentBlockParts++ } else { logger.Debug("Sending block part for catchup failed") // sleep to avoid retrying too fast diff --git a/consensus/state.go b/consensus/state.go index a557c5294f..59109f39ec 100644 --- a/consensus/state.go +++ b/consensus/state.go @@ -1801,7 +1801,11 @@ func (cs *State) finalizeCommit(height int64) { // NewHeightStep! cs.updateToState(stateCopy) - cs.jsonMetrics.Save() + cs.jsonMetrics.Blocks++ + // Save every 20 blocks + if cs.Height%20 == 0 { + cs.jsonMetrics.Save() + } fail.Fail() // XXX @@ -2003,6 +2007,7 @@ func (cs *State) addCompactBlock(msg *CompactBlockMessage, peerID p2p.ID) error blockHash := cs.Proposal.BlockID.Hash timeout := cs.config.Propose(cs.Round) + cs.jsonMetrics.ReceivedCompactBlocks++ // Yield the lock while we fetch the transactions from the mempool so that votes // and other operations can be processed. @@ -2098,6 +2103,7 @@ func (cs *State) addProposalBlockPart(msg *BlockPartMessage, peerID p2p.ID) (add } cs.metrics.BlockGossipPartsReceived.With("matches_current", "true").Add(1) + cs.jsonMetrics.ReceivedBlockParts++ if cs.ProposalBlockParts.ByteSize() > cs.state.ConsensusParams.Block.MaxBytes { return added, fmt.Errorf("total size of proposal block parts exceeds maximum block bytes (%d > %d)", diff --git a/mempool/cat/pool.go b/mempool/cat/pool.go index 0d98632a29..35d7e9c74b 100644 --- a/mempool/cat/pool.go +++ b/mempool/cat/pool.go @@ -559,7 +559,10 @@ func (txmp *TxPool) Update( txmp.notifyTxsAvailable() } } - txmp.jsonMetrics.Save() + // save every 20 blocks + if blockHeight%20 == 0 { + txmp.jsonMetrics.Save() + } return nil } From c2e6331b8f0c920ff68f27e8f89f97a18832e273 Mon Sep 17 00:00:00 2001 From: Callum Waters <cmwaters19@gmail.com> Date: Fri, 19 Jul 2024 17:07:37 +0200 Subject: [PATCH 018/127] shift where we measure compact blocks --- consensus/state.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/consensus/state.go b/consensus/state.go index 59109f39ec..f8f3b8fccd 100644 --- a/consensus/state.go +++ b/consensus/state.go @@ -1979,6 +1979,7 @@ func (cs *State) defaultSetProposal(proposal *types.Proposal) error { func (cs *State) addCompactBlock(msg *CompactBlockMessage, peerID p2p.ID) error { compactBlock := msg.Block height := compactBlock.Height + cs.jsonMetrics.ReceivedCompactBlocks++ if cs.ProposalBlock != nil { // We already have the proposal block. @@ -2007,7 +2008,6 @@ func (cs *State) addCompactBlock(msg *CompactBlockMessage, peerID p2p.ID) error blockHash := cs.Proposal.BlockID.Hash timeout := cs.config.Propose(cs.Round) - cs.jsonMetrics.ReceivedCompactBlocks++ // Yield the lock while we fetch the transactions from the mempool so that votes // and other operations can be processed. From 1cbd36171918e492ee5975efb3250b4eff731e2d Mon Sep 17 00:00:00 2001 From: Callum Waters <cmwaters19@gmail.com> Date: Wed, 24 Jul 2024 14:38:13 +0200 Subject: [PATCH 019/127] fix: panic in prometheus --- mempool/cat/pool.go | 9 ++++++++- mempool/metrics.go | 31 +++++++++++++++---------------- mempool/v1/mempool.go | 20 ++++++++------------ 3 files changed, 31 insertions(+), 29 deletions(-) diff --git a/mempool/cat/pool.go b/mempool/cat/pool.go index 35d7e9c74b..3e2abe297b 100644 --- a/mempool/cat/pool.go +++ b/mempool/cat/pool.go @@ -783,7 +783,14 @@ func (txmp *TxPool) purgeExpiredTxs(blockHeight int64) { for _, tx := range purgedTxs { txmp.evictedTxCache.Push(tx.key) } - txmp.metrics.EvictedTxs.Add(float64(numExpired)) + txmp.metrics.ExpiredTxs.Add(float64(numExpired)) + + // purge old evicted and seen transactions + if txmp.config.TTLDuration == 0 { + // ensure that seenByPeersSet are eventually pruned + expirationAge = now.Add(-time.Hour) + } + txmp.seenByPeersSet.Prune(expirationAge) } func (txmp *TxPool) notifyTxsAvailable() { diff --git a/mempool/metrics.go b/mempool/metrics.go index 0a58e144e3..528a23b361 100644 --- a/mempool/metrics.go +++ b/mempool/metrics.go @@ -18,17 +18,6 @@ const ( // MetricsSubsystem is a subsystem shared by all metrics exposed by this // package. MetricsSubsystem = "mempool" - - TypeLabel = "type" - - FailedPrecheck = "precheck" - FailedAdding = "adding" - FailedRecheck = "recheck" - - EvictedNewTxFullMempool = "full-removed-incoming" - EvictedExistingTxFullMempool = "full-removed-existing" - EvictedTxExpiredBlocks = "expired-ttl-blocks" - EvictedTxExpiredTime = "expired-ttl-time" ) // Metrics contains metrics exposed by this package. @@ -49,10 +38,13 @@ type Metrics struct { // EvictedTxs defines the number of evicted transactions. These are valid // transactions that passed CheckTx and existed in the mempool but were later - // evicted to make room for higher priority valid transactions that passed - // CheckTx. + // evicted to make room for higher priority valid transactions EvictedTxs metrics.Counter + // ExpiredTxs defines transactions that were removed from the mempool due + // to a TTL + ExpiredTxs metrics.Counter + // SuccessfulTxs defines the number of transactions that successfully made // it into a block. SuccessfulTxs metrics.Counter @@ -82,7 +74,6 @@ func PrometheusMetrics(namespace string, labelsAndValues ...string) *Metrics { for i := 0; i < len(labelsAndValues); i += 2 { labels = append(labels, labelsAndValues[i]) } - typedCounterLabels := append(append(make([]string, 0, len(labels)+1), labels...), TypeLabel) return &Metrics{ Size: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{ Namespace: namespace, @@ -111,14 +102,21 @@ func PrometheusMetrics(namespace string, labelsAndValues ...string) *Metrics { Subsystem: MetricsSubsystem, Name: "failed_txs", Help: "Number of failed transactions.", - }, typedCounterLabels).With(labelsAndValues...), + }, labels).With(labelsAndValues...), EvictedTxs: prometheus.NewCounterFrom(stdprometheus.CounterOpts{ Namespace: namespace, Subsystem: MetricsSubsystem, Name: "evicted_txs", Help: "Number of evicted transactions.", - }, typedCounterLabels).With(labelsAndValues...), + }, labels).With(labelsAndValues...), + + ExpiredTxs: prometheus.NewCounterFrom(stdprometheus.CounterOpts{ + Namespace: namespace, + Subsystem: MetricsSubsystem, + Name: "expired_txs", + Help: "Number of expired transactions.", + }, labels).With(labelsAndValues...), SuccessfulTxs: prometheus.NewCounterFrom(stdprometheus.CounterOpts{ Namespace: namespace, @@ -165,6 +163,7 @@ func NopMetrics() *Metrics { TxSizeBytes: discard.NewHistogram(), FailedTxs: discard.NewCounter(), EvictedTxs: discard.NewCounter(), + ExpiredTxs: discard.NewCounter(), SuccessfulTxs: discard.NewCounter(), RecheckTimes: discard.NewCounter(), AlreadySeenTxs: discard.NewCounter(), diff --git a/mempool/v1/mempool.go b/mempool/v1/mempool.go index fa41cef889..7cfd60f336 100644 --- a/mempool/v1/mempool.go +++ b/mempool/v1/mempool.go @@ -204,7 +204,7 @@ func (txmp *TxMempool) CheckTx(tx types.Tx, cb func(*abci.Response), txInfo memp // If a precheck hook is defined, call it before invoking the application. if txmp.preCheck != nil { if err := txmp.preCheck(tx); err != nil { - txmp.metrics.FailedTxs.With(mempool.TypeLabel, mempool.FailedPrecheck).Add(1) + txmp.metrics.FailedTxs.Add(1) return 0, mempool.ErrPreCheck{Reason: err} } } @@ -500,7 +500,7 @@ func (txmp *TxMempool) addNewTransaction(wtx *WrappedTx, checkTxRes *abci.Respon "post_check_err", err, ) - txmp.metrics.FailedTxs.With(mempool.TypeLabel, mempool.FailedAdding).Add(1) + txmp.metrics.FailedTxs.Add(1) // Remove the invalid transaction from the cache, unless the operator has // instructed us to keep invalid transactions. @@ -568,7 +568,7 @@ func (txmp *TxMempool) addNewTransaction(wtx *WrappedTx, checkTxRes *abci.Respon checkTxRes.MempoolError = fmt.Sprintf("rejected valid incoming transaction; mempool is full (%X)", wtx.tx.Hash()) - txmp.metrics.EvictedTxs.With(mempool.TypeLabel, mempool.EvictedNewTxFullMempool).Add(1) + txmp.metrics.EvictedTxs.Add(1) // Add it to evicted transactions cache txmp.evictedTxs.Push(wtx.tx) return @@ -602,7 +602,7 @@ func (txmp *TxMempool) addNewTransaction(wtx *WrappedTx, checkTxRes *abci.Respon ) txmp.removeTxByElement(vic) txmp.cache.Remove(w.tx) - txmp.metrics.EvictedTxs.With(mempool.TypeLabel, mempool.EvictedExistingTxFullMempool).Add(1) + txmp.metrics.EvictedTxs.Add(1) // Add it to evicted transactions cache txmp.evictedTxs.Push(w.tx) // We may not need to evict all the eligible transactions. Bail out @@ -681,7 +681,7 @@ func (txmp *TxMempool) handleRecheckResult(tx types.Tx, checkTxRes *abci.Respons "code", checkTxRes.Code, ) txmp.removeTxByElement(elt) - txmp.metrics.FailedTxs.With(mempool.TypeLabel, mempool.FailedRecheck).Add(1) + txmp.metrics.FailedTxs.Add(1) if !txmp.config.KeepInvalidTxsInCache { txmp.cache.Remove(wtx.tx) } @@ -791,16 +791,12 @@ func (txmp *TxMempool) purgeExpiredTxs(blockHeight int64) { next := cur.Next() w := cur.Value.(*WrappedTx) - if txmp.config.TTLNumBlocks > 0 && (blockHeight-w.height) > txmp.config.TTLNumBlocks { + if txmp.config.TTLNumBlocks > 0 && (blockHeight-w.height) > txmp.config.TTLNumBlocks || + txmp.config.TTLDuration > 0 && now.Sub(w.timestamp) > txmp.config.TTLDuration { txmp.removeTxByElement(cur) txmp.cache.Remove(w.tx) - txmp.metrics.EvictedTxs.With(mempool.TypeLabel, mempool.EvictedTxExpiredBlocks).Add(1) txmp.evictedTxs.Push(w.tx) - } else if txmp.config.TTLDuration > 0 && now.Sub(w.timestamp) > txmp.config.TTLDuration { - txmp.removeTxByElement(cur) - txmp.cache.Remove(w.tx) - txmp.evictedTxs.Push(w.tx) - txmp.metrics.EvictedTxs.With(mempool.TypeLabel, mempool.EvictedTxExpiredTime).Add(1) + txmp.metrics.ExpiredTxs.Add(1) } cur = next } From 1fe85226cd97b9bf3e69c5bf1f7e99087f6d211b Mon Sep 17 00:00:00 2001 From: Callum Waters <cmwaters19@gmail.com> Date: Mon, 29 Jul 2024 13:37:23 +0200 Subject: [PATCH 020/127] add the square size to the constructed data --- consensus/state.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/consensus/state.go b/consensus/state.go index f8f3b8fccd..037b941dd5 100644 --- a/consensus/state.go +++ b/consensus/state.go @@ -2032,11 +2032,9 @@ func (cs *State) addCompactBlock(msg *CompactBlockMessage, peerID p2p.ID) error Header: compactBlock.Header, LastCommit: compactBlock.LastCommit, Evidence: compactBlock.Evidence, + Data: compactBlock.Data, } - block.Data, _ = types.DataFromProto(&cmtproto.Data{ - Txs: txs, - Hash: block.Header.DataHash, - }) + block.Txs = types.ToTxs(txs) if err := block.ValidateBasic(); err != nil { return fmt.Errorf("received invalid block: %w", err) From 1846523a16b28db00d5d2b840154183e45d06c90 Mon Sep 17 00:00:00 2001 From: Callum Waters <cmwaters19@gmail.com> Date: Tue, 30 Jul 2024 13:43:09 +0200 Subject: [PATCH 021/127] add more metrics for compact blocks --- consensus/metrics.go | 26 ++++++++++++++++++++++++++ consensus/reactor.go | 3 +-- consensus/state.go | 13 ++----------- mempool/cat/block_builder.go | 15 +++------------ mempool/cat/pool.go | 6 ------ mempool/metrics.go | 4 ++++ 6 files changed, 36 insertions(+), 31 deletions(-) diff --git a/consensus/metrics.go b/consensus/metrics.go index 303f0f0cd5..97fc5dab33 100644 --- a/consensus/metrics.go +++ b/consensus/metrics.go @@ -102,6 +102,10 @@ type Metrics struct { // The amount of proposals that failed to be received in time TimedOutProposals metrics.Counter + + CompactBlocksReceived metrics.Counter + CompactBlocksSent metrics.Counter + CompactBlocksFailed metrics.Counter } // PrometheusMetrics returns Metrics build using Prometheus client library. @@ -279,6 +283,24 @@ func PrometheusMetrics(namespace string, labelsAndValues ...string) *Metrics { Name: "timed_out_proposals", Help: "Number of proposals that failed to be received in time", }, labels).With(labelsAndValues...), + CompactBlocksReceived: prometheus.NewCounterFrom(stdprometheus.CounterOpts{ + Namespace: namespace, + Subsystem: MetricsSubsystem, + Name: "compact_blocks_received", + Help: "Number of compact blocks received by the node", + }, labels).With(labelsAndValues...), + CompactBlocksSent: prometheus.NewCounterFrom(stdprometheus.CounterOpts{ + Namespace: namespace, + Subsystem: MetricsSubsystem, + Name: "compact_blocks_sent", + Help: "Number of compact blocks sent by the node", + }, labels).With(labelsAndValues...), + CompactBlocksFailed: prometheus.NewCounterFrom(stdprometheus.CounterOpts{ + Namespace: namespace, + Subsystem: MetricsSubsystem, + Name: "compact_blocks_failed", + Help: "Number of compact blocks failed to be received by the node", + }, labels).With(labelsAndValues...), } } @@ -317,6 +339,10 @@ func NopMetrics() *Metrics { FullPrevoteMessageDelay: discard.NewGauge(), ApplicationRejectedProposals: discard.NewCounter(), TimedOutProposals: discard.NewCounter(), + + CompactBlocksReceived: discard.NewCounter(), + CompactBlocksSent: discard.NewCounter(), + CompactBlocksFailed: discard.NewCounter(), } } diff --git a/consensus/reactor.go b/consensus/reactor.go index f55fe6647d..97b6bf7cd9 100644 --- a/consensus/reactor.go +++ b/consensus/reactor.go @@ -694,7 +694,7 @@ OUTER_LOOP: }, }, logger) { ps.SetHasBlock(prs.Height, prs.Round) - conR.conS.jsonMetrics.SentCompactBlocks++ + conR.conS.metrics.CompactBlocksSent.Add(1) } continue OUTER_LOOP } @@ -841,7 +841,6 @@ func (conR *Reactor) gossipDataForCatchup(logger log.Logger, rs *cstypes.RoundSt string(peer.ID()), schema.Upload, ) - conR.conS.jsonMetrics.SentBlockParts++ } else { logger.Debug("Sending block part for catchup failed") // sleep to avoid retrying too fast diff --git a/consensus/state.go b/consensus/state.go index 037b941dd5..37f060147e 100644 --- a/consensus/state.go +++ b/consensus/state.go @@ -167,7 +167,6 @@ type State struct { // for reporting metrics metrics *Metrics - jsonMetrics *JSONMetrics traceClient trace.Tracer } @@ -206,7 +205,6 @@ func NewState( evpool: evpool, evsw: cmtevents.NewEventSwitch(), metrics: NopMetrics(), - jsonMetrics: NewJSONMetrics(path), traceClient: trace.NoOpTracer(), } @@ -1801,12 +1799,6 @@ func (cs *State) finalizeCommit(height int64) { // NewHeightStep! cs.updateToState(stateCopy) - cs.jsonMetrics.Blocks++ - // Save every 20 blocks - if cs.Height%20 == 0 { - cs.jsonMetrics.Save() - } - fail.Fail() // XXX // Private validator might have changed it's key pair => refetch pubkey. @@ -1979,7 +1971,7 @@ func (cs *State) defaultSetProposal(proposal *types.Proposal) error { func (cs *State) addCompactBlock(msg *CompactBlockMessage, peerID p2p.ID) error { compactBlock := msg.Block height := compactBlock.Height - cs.jsonMetrics.ReceivedCompactBlocks++ + cs.metrics.CompactBlocksReceived.Add(1) if cs.ProposalBlock != nil { // We already have the proposal block. @@ -2020,7 +2012,7 @@ func (cs *State) addCompactBlock(msg *CompactBlockMessage, peerID p2p.ID) error cs.mtx.Lock() if err != nil { - cs.jsonMetrics.CompactBlockFailures++ + cs.metrics.CompactBlocksFailed.Add(1) if ctx.Err() != nil { cs.Logger.Info("failed to fetch transactions within the timeout", "timeout", timeout) return nil @@ -2101,7 +2093,6 @@ func (cs *State) addProposalBlockPart(msg *BlockPartMessage, peerID p2p.ID) (add } cs.metrics.BlockGossipPartsReceived.With("matches_current", "true").Add(1) - cs.jsonMetrics.ReceivedBlockParts++ if cs.ProposalBlockParts.ByteSize() > cs.state.ConsensusParams.Block.MaxBytes { return added, fmt.Errorf("total size of proposal block parts exceeds maximum block bytes (%d > %d)", diff --git a/mempool/cat/block_builder.go b/mempool/cat/block_builder.go index c2ec90ced7..a2abfb5da3 100644 --- a/mempool/cat/block_builder.go +++ b/mempool/cat/block_builder.go @@ -39,17 +39,13 @@ func (memR *Reactor) FetchTxsFromKeys(ctx context.Context, blockID []byte, compa } } memR.Logger.Info("fetching transactions from peers", "blockID", blockID, "numTxs", len(txs), "numMissing", len(missingKeys)) + memR.mempool.metrics.MissingTxs.Add(float64(len(missingKeys))) - memR.mempool.jsonMetrics.Lock() - memR.mempool.jsonMetrics.TransactionsMissing = append(memR.mempool.jsonMetrics.TransactionsMissing, uint64(len(missingKeys))) - memR.mempool.jsonMetrics.Transactions = append(memR.mempool.jsonMetrics.Transactions, uint64(len(compactData))) // Check if we got lucky and already had all the transactions. if len(missingKeys) == 0 { - memR.mempool.jsonMetrics.TimeTakenFetchingTxs = append(memR.mempool.jsonMetrics.TimeTakenFetchingTxs, 0) - memR.mempool.jsonMetrics.Unlock() + memR.Logger.Info("fetched all txs, none missing", "blockID", blockID) return txs, nil } - memR.mempool.jsonMetrics.Unlock() // setup a request for this block and begin to track and retrieve all missing transactions request := memR.blockFetcher.newRequest( @@ -60,12 +56,7 @@ func (memR *Reactor) FetchTxsFromKeys(ctx context.Context, blockID []byte, compa ) defer func() { timeTaken := request.TimeTaken() - if timeTaken == 0 { - return - } - memR.mempool.jsonMetrics.Lock() - memR.mempool.jsonMetrics.TimeTakenFetchingTxs = append(memR.mempool.jsonMetrics.TimeTakenFetchingTxs, timeTaken) - memR.mempool.jsonMetrics.Unlock() + memR.Logger.Info("fetched txs", "timeTaken", timeTaken, "numMissing", len(missingKeys)) }() // request the missing transactions if we haven't already diff --git a/mempool/cat/pool.go b/mempool/cat/pool.go index 3e2abe297b..cc6400d09e 100644 --- a/mempool/cat/pool.go +++ b/mempool/cat/pool.go @@ -54,7 +54,6 @@ type TxPool struct { config *config.MempoolConfig proxyAppConn proxy.AppConnMempool metrics *mempool.Metrics - jsonMetrics *mempool.JSONMetrics // these values are modified once per height updateMtx sync.Mutex @@ -111,7 +110,6 @@ func NewTxPool( store: newStore(), broadcastCh: make(chan *wrappedTx), txsToBeBroadcast: make([]types.TxKey, 0), - jsonMetrics: mempool.NewJSONMetrics(path), } for _, opt := range options { @@ -559,10 +557,6 @@ func (txmp *TxPool) Update( txmp.notifyTxsAvailable() } } - // save every 20 blocks - if blockHeight%20 == 0 { - txmp.jsonMetrics.Save() - } return nil } diff --git a/mempool/metrics.go b/mempool/metrics.go index 528a23b361..1d1c31ca87 100644 --- a/mempool/metrics.go +++ b/mempool/metrics.go @@ -64,6 +64,10 @@ type Metrics struct { // RerequestedTxs defines the number of times that a requested tx // never received a response in time and a new request was made. RerequestedTxs metrics.Counter + + // MissingTxs defines the number of transactions that were not found in the mempool + // from the current proposal + MissingTxs metrics.Counter } // PrometheusMetrics returns Metrics build using Prometheus client library. From 1c4f76eca6f09afa10f26560767d053a832d3e14 Mon Sep 17 00:00:00 2001 From: Callum Waters <cmwaters19@gmail.com> Date: Tue, 30 Jul 2024 13:57:47 +0200 Subject: [PATCH 022/127] fix usage of metrics --- mempool/metrics.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/mempool/metrics.go b/mempool/metrics.go index 1d1c31ca87..485d669b38 100644 --- a/mempool/metrics.go +++ b/mempool/metrics.go @@ -156,6 +156,13 @@ func PrometheusMetrics(namespace string, labelsAndValues ...string) *Metrics { Name: "rerequested_txs", Help: "Number of times a transaction was requested again after a previous request timed out", }, labels).With(labelsAndValues...), + + MissingTxs: prometheus.NewCounterFrom(stdprometheus.CounterOpts{ + Namespace: namespace, + Subsystem: MetricsSubsystem, + Name: "missing_txs", + Help: "Number of transactions that were not found in the mempool from the current proposal", + }, labels).With(labelsAndValues...), } } @@ -173,6 +180,7 @@ func NopMetrics() *Metrics { AlreadySeenTxs: discard.NewCounter(), RequestedTxs: discard.NewCounter(), RerequestedTxs: discard.NewCounter(), + MissingTxs: discard.NewCounter(), } } From 9ad05ed17086f7bfe36697ba9c5619f67165fd2b Mon Sep 17 00:00:00 2001 From: Callum Waters <cmwaters19@gmail.com> Date: Tue, 30 Jul 2024 19:24:48 +0200 Subject: [PATCH 023/127] fix error handling in cat pool and consensus --- consensus/state.go | 6 ++++++ mempool/cat/pool.go | 18 ++++++++++-------- mempool/cat/reactor.go | 4 ++-- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/consensus/state.go b/consensus/state.go index 37f060147e..d722b5ab83 100644 --- a/consensus/state.go +++ b/consensus/state.go @@ -1985,6 +1985,12 @@ func (cs *State) addCompactBlock(msg *CompactBlockMessage, peerID p2p.ID) error return nil } + if cs.Round != msg.Round { + cs.Logger.Debug("received compact block from wrong round", "round", msg.Round, "currentRound", cs.Round) + cs.metrics.BlockGossipPartsReceived.With("matches_current", "false").Add(1) + return nil + } + // We're not expecting a block part. if cs.ProposalBlockParts == nil || cs.Proposal == nil { cs.metrics.BlockGossipPartsReceived.With("matches_current", "false").Add(1) diff --git a/mempool/cat/pool.go b/mempool/cat/pool.go index cc6400d09e..71f1037fa1 100644 --- a/mempool/cat/pool.go +++ b/mempool/cat/pool.go @@ -24,8 +24,9 @@ import ( var _ mempool.Mempool = (*TxPool)(nil) var ( - ErrTxInMempool = errors.New("tx already exists in mempool") - ErrTxAlreadyRejected = errors.New("tx was previously rejected") + ErrTxInMempool = errors.New("tx already exists in mempool") + ErrTxAlreadyRejected = errors.New("tx was previously rejected") + ErrTxRecentlyCommitted = errors.New("tx was recently committed") ) // TxPoolOption sets an optional parameter on the TxPool. @@ -66,6 +67,8 @@ type TxPool struct { // Thread-safe cache of rejected transactions for quick look-up rejectedTxCache *LRUTxCache + // Thread-safe cache of committed transactions for quick look-up + committedTxCache *LRUTxCache // Thread-safe cache of evicted transactions for quick look-up evictedTxCache *LRUTxCache // Thread-safe list of transactions peers have seen that we have not yet seen @@ -316,10 +319,11 @@ func (txmp *TxPool) markToBeBroadcast(key types.TxKey) { // sufficient priority and space else if evicted it will return an error func (txmp *TxPool) TryAddNewTx(tx types.Tx, key types.TxKey, txInfo mempool.TxInfo) (*abci.ResponseCheckTx, error) { // First check any of the caches to see if we can conclude early. We may have already seen and processed - // the transaction if: - // - We are connected to nodes running v0 or v1 which simply flood the network - // - If a client submits a transaction to multiple nodes (via RPC) - // - We send multiple requests and the first peer eventually responds after the second peer has already provided the tx + // the transaction, or it may have already been committed. + if wtx := txmp.store.getCommitted(key); wtx != nil { + return nil, ErrTxRecentlyCommitted + } + if txmp.IsRejectedTx(key) { // The peer has sent us a transaction that we have previously marked as invalid. Since `CheckTx` can // be non-deterministic, we don't punish the peer but instead just ignore the tx @@ -531,8 +535,6 @@ func (txmp *TxPool) Update( keys := make([]types.TxKey, len(blockTxs)) for idx, tx := range blockTxs { keys[idx] = tx.Key() - // this prevents the node from reprocessing recently committed transactions - txmp.rejectedTxCache.Push(keys[idx]) } txmp.store.markAsCommitted(keys) diff --git a/mempool/cat/reactor.go b/mempool/cat/reactor.go index b1f321054c..ae3ee2a59a 100644 --- a/mempool/cat/reactor.go +++ b/mempool/cat/reactor.go @@ -262,8 +262,8 @@ func (memR *Reactor) ReceiveEnvelope(e p2p.Envelope) { memR.Logger.Debug("received new trasaction", "peerID", peerID, "txKey", key) } _, err = memR.mempool.TryAddNewTx(ntx, key, txInfo) - if err != nil && err != ErrTxInMempool { - memR.Logger.Info("Could not add tx", "txKey", key, "err", err) + if err != nil && (err != ErrTxInMempool || err != ErrTxRecentlyCommitted) { + memR.Logger.Info("Could not add tx from peer", "peerID", peerID, "txKey", key, "err", err) return } // If a block has been proposed with this transaction and From 93774a73d69b8a8fcddbb126ebc67ed71d394598 Mon Sep 17 00:00:00 2001 From: Callum Waters <cmwaters19@gmail.com> Date: Wed, 31 Jul 2024 14:50:49 +0200 Subject: [PATCH 024/127] checkpoint --- mempool/cat/reactor.go | 2 +- rpc/core/mempool.go | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/mempool/cat/reactor.go b/mempool/cat/reactor.go index ae3ee2a59a..86391f8993 100644 --- a/mempool/cat/reactor.go +++ b/mempool/cat/reactor.go @@ -262,7 +262,7 @@ func (memR *Reactor) ReceiveEnvelope(e p2p.Envelope) { memR.Logger.Debug("received new trasaction", "peerID", peerID, "txKey", key) } _, err = memR.mempool.TryAddNewTx(ntx, key, txInfo) - if err != nil && (err != ErrTxInMempool || err != ErrTxRecentlyCommitted) { + if err != nil && err != ErrTxInMempool && err != ErrTxRecentlyCommitted { memR.Logger.Info("Could not add tx from peer", "peerID", peerID, "txKey", key, "err", err) return } diff --git a/rpc/core/mempool.go b/rpc/core/mempool.go index 0aec02cffe..44431699dd 100644 --- a/rpc/core/mempool.go +++ b/rpc/core/mempool.go @@ -43,6 +43,7 @@ func BroadcastTxSync(ctx *rpctypes.Context, tx types.Tx) (*ctypes.ResultBroadcas }, mempl.TxInfo{}) if err != nil { + fmt.Println("Error on broadcastTxSync", err) return nil, err } From 6f8f70a8ec825e6f1c03d83f4ec3eaa8f0b930eb Mon Sep 17 00:00:00 2001 From: Callum Waters <cmwaters19@gmail.com> Date: Wed, 31 Jul 2024 16:11:22 +0200 Subject: [PATCH 025/127] print out account sequence --- rpc/core/mempool.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/rpc/core/mempool.go b/rpc/core/mempool.go index 44431699dd..90dc99c806 100644 --- a/rpc/core/mempool.go +++ b/rpc/core/mempool.go @@ -35,7 +35,9 @@ func BroadcastTxAsync(ctx *rpctypes.Context, tx types.Tx) (*ctypes.ResultBroadca // More: https://docs.cometbft.com/v0.34/rpc/#/Tx/broadcast_tx_sync func BroadcastTxSync(ctx *rpctypes.Context, tx types.Tx) (*ctypes.ResultBroadcastTx, error) { resCh := make(chan *abci.Response, 1) - err := GetEnvironment().Mempool.CheckTx(tx, func(res *abci.Response) { + env := GetEnvironment() + env.Logger.Info("BroadcastTxSync") + err := env.Mempool.CheckTx(tx, func(res *abci.Response) { select { case <-ctx.Context().Done(): case resCh <- res: @@ -43,7 +45,7 @@ func BroadcastTxSync(ctx *rpctypes.Context, tx types.Tx) (*ctypes.ResultBroadcas }, mempl.TxInfo{}) if err != nil { - fmt.Println("Error on broadcastTxSync", err) + env.Logger.Error("Error on broadcastTxSync", "err", err) return nil, err } From 82a8958ba2d08e15a014b24d2585ee90e55f0367 Mon Sep 17 00:00:00 2001 From: Callum Waters <cmwaters19@gmail.com> Date: Thu, 8 Aug 2024 16:02:02 +0200 Subject: [PATCH 026/127] track if we see missing transcations which are invalid --- mempool/cat/block_builder.go | 13 +++++++++++++ mempool/cat/reactor.go | 4 ++++ 2 files changed, 17 insertions(+) diff --git a/mempool/cat/block_builder.go b/mempool/cat/block_builder.go index a2abfb5da3..cdbc2d835c 100644 --- a/mempool/cat/block_builder.go +++ b/mempool/cat/block_builder.go @@ -158,6 +158,19 @@ func (bf *blockFetcher) TryAddMissingTx(key types.TxKey, tx []byte) { } } +// IsMissingTx checks if a given transaction is missing from any of the block requests. +func (bf *blockFetcher) IsMissingTx(key types.TxKey) bool { + bf.mtx.Lock() + defer bf.mtx.Unlock() + for _, request := range bf.requests { + _, ok := request.missingKeys[key.String()] + if ok { + return true + } + } + return false +} + // PruneOldRequests removes any requests that are older than the given height. func (bf *blockFetcher) pruneOldRequests(height int64) { for blockID, request := range bf.requests { diff --git a/mempool/cat/reactor.go b/mempool/cat/reactor.go index 86391f8993..0dae5b6fa3 100644 --- a/mempool/cat/reactor.go +++ b/mempool/cat/reactor.go @@ -263,6 +263,10 @@ func (memR *Reactor) ReceiveEnvelope(e p2p.Envelope) { } _, err = memR.mempool.TryAddNewTx(ntx, key, txInfo) if err != nil && err != ErrTxInMempool && err != ErrTxRecentlyCommitted { + if memR.blockFetcher.IsMissingTx(key) { + memR.Logger.Error("tx in block is not valid by mempool") + } + memR.Logger.Info("Could not add tx from peer", "peerID", peerID, "txKey", key, "err", err) return } From d1b3c4d8fb871539de2dd73a8b8264275b07b2c1 Mon Sep 17 00:00:00 2001 From: Callum Waters <cmwaters19@gmail.com> Date: Fri, 9 Aug 2024 14:42:57 +0200 Subject: [PATCH 027/127] add more logs around the mempool --- mempool/cat/pool.go | 2 +- mempool/cat/reactor.go | 14 ++++++++------ mempool/cat/store.go | 7 +++++++ 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/mempool/cat/pool.go b/mempool/cat/pool.go index 71f1037fa1..39795110af 100644 --- a/mempool/cat/pool.go +++ b/mempool/cat/pool.go @@ -320,7 +320,7 @@ func (txmp *TxPool) markToBeBroadcast(key types.TxKey) { func (txmp *TxPool) TryAddNewTx(tx types.Tx, key types.TxKey, txInfo mempool.TxInfo) (*abci.ResponseCheckTx, error) { // First check any of the caches to see if we can conclude early. We may have already seen and processed // the transaction, or it may have already been committed. - if wtx := txmp.store.getCommitted(key); wtx != nil { + if txmp.store.hasCommitted(key) { return nil, ErrTxRecentlyCommitted } diff --git a/mempool/cat/reactor.go b/mempool/cat/reactor.go index 0dae5b6fa3..7be198207b 100644 --- a/mempool/cat/reactor.go +++ b/mempool/cat/reactor.go @@ -29,7 +29,7 @@ const ( // peerHeightDiff signifies the tolerance in difference in height between the peer and the height // the node received the tx - peerHeightDiff = 10 + peerHeightDiff = 2 ) // Reactor handles mempool tx broadcasting logic amongst peers. For the main @@ -303,7 +303,7 @@ func (memR *Reactor) ReceiveEnvelope(e p2p.Envelope) { peerID := memR.ids.GetIDForPeer(e.Src.ID()) memR.mempool.PeerHasTx(peerID, txKey) // Check if we don't already have the transaction and that it was recently rejected - if memR.mempool.Has(txKey) || memR.mempool.IsRejectedTx(txKey) { + if memR.mempool.Has(txKey) || memR.mempool.IsRejectedTx(txKey) || memR.mempool.store.hasCommitted(txKey) { memR.Logger.Debug("received a seen tx for a tx we already have", "txKey", txKey) return } @@ -341,7 +341,7 @@ func (memR *Reactor) ReceiveEnvelope(e p2p.Envelope) { } if has && !memR.opts.ListenOnly { peerID := memR.ids.GetIDForPeer(e.Src.ID()) - memR.Logger.Debug("sending a tx in response to a want msg", "peer", peerID) + memR.Logger.Info("sending a tx in response to a want msg", "peer", peerID) if p2p.SendEnvelopeShim(e.Src, p2p.Envelope{ //nolint:staticcheck ChannelID: mempool.MempoolChannel, Message: &protomem.Txs{Txs: [][]byte{tx}}, @@ -404,7 +404,9 @@ func (memR *Reactor) broadcastSeenTx(txKey types.TxKey) { continue } - peer.Send(MempoolStateChannel, bz) //nolint:staticcheck + if peer.Send(MempoolStateChannel, bz) { + memR.Logger.Info("sent seen tx to peer", "peerID", peer.ID(), "txKey", txKey) + } } } @@ -450,7 +452,6 @@ func (memR *Reactor) requestTx(txKey types.TxKey, peer p2p.Peer) { // we have disconnected from the peer return } - memR.Logger.Debug("requesting tx", "txKey", txKey, "peerID", peer.ID()) msg := &protomem.Message{ Sum: &protomem.Message_WantTx{ WantTx: &protomem.WantTx{TxKey: txKey[:]}, @@ -463,10 +464,11 @@ func (memR *Reactor) requestTx(txKey types.TxKey, peer p2p.Peer) { success := peer.Send(MempoolStateChannel, bz) //nolint:staticcheck if success { + memR.Logger.Info("requested tx", "txKey", txKey, "peerID", peer.ID()) memR.mempool.metrics.RequestedTxs.Add(1) requested := memR.requests.Add(txKey, memR.ids.GetIDForPeer(peer.ID()), memR.findNewPeerToRequestTx) if !requested { - memR.Logger.Debug("have already marked a tx as requested", "txKey", txKey, "peerID", peer.ID()) + memR.Logger.Error("have already marked a tx as requested", "txKey", txKey, "peerID", peer.ID()) } } } diff --git a/mempool/cat/store.go b/mempool/cat/store.go index 19fd54ba58..e6f21dbee2 100644 --- a/mempool/cat/store.go +++ b/mempool/cat/store.go @@ -50,6 +50,13 @@ func (s *store) getCommitted(txKey types.TxKey) *wrappedTx { return s.committedTxs[txKey] } +func (s *store) hasCommitted(txKey types.TxKey) bool { + s.mtx.RLock() + defer s.mtx.RUnlock() + _, has := s.committedTxs[txKey] + return has +} + func (s *store) has(txKey types.TxKey) bool { s.mtx.RLock() defer s.mtx.RUnlock() From 8a5d7971f8f065ef7414042b7cff83cfdced480c Mon Sep 17 00:00:00 2001 From: Callum Waters <cmwaters19@gmail.com> Date: Fri, 9 Aug 2024 14:49:39 +0200 Subject: [PATCH 028/127] remove jitter --- mempool/cat/reactor.go | 5 ----- 1 file changed, 5 deletions(-) diff --git a/mempool/cat/reactor.go b/mempool/cat/reactor.go index 7be198207b..0ee6118e31 100644 --- a/mempool/cat/reactor.go +++ b/mempool/cat/reactor.go @@ -2,7 +2,6 @@ package cat import ( "fmt" - "math/rand" "time" "github.com/gogo/protobuf/proto" @@ -384,10 +383,6 @@ func (memR *Reactor) broadcastSeenTx(txKey types.TxKey) { panic(err) } - // Add jitter to when the node broadcasts it's seen txs to stagger when nodes - // in the network broadcast their seenTx messages. - time.Sleep(time.Duration(rand.Intn(10)*10) * time.Millisecond) //nolint:gosec - for id, peer := range memR.ids.GetAll() { if p, ok := peer.Get(types.PeerStateKey).(PeerState); ok { // make sure peer isn't too far behind. This can happen From e937057ceecaf148b9cec2a97b2dfc1cb3bfaea6 Mon Sep 17 00:00:00 2001 From: Callum Waters <cmwaters19@gmail.com> Date: Fri, 9 Aug 2024 15:28:17 +0200 Subject: [PATCH 029/127] add inclusion delay --- mempool/cat/pool.go | 16 +++++++++++++++- mempool/cat/reactor.go | 7 ++++--- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/mempool/cat/pool.go b/mempool/cat/pool.go index 39795110af..f1291ffb77 100644 --- a/mempool/cat/pool.go +++ b/mempool/cat/pool.go @@ -29,6 +29,10 @@ var ( ErrTxRecentlyCommitted = errors.New("tx was recently committed") ) +// InclusionDelay is the amount of time a transaction must be in the mempool +// before it is included in the block. +const InclusionDelay = time.Second + // TxPoolOption sets an optional parameter on the TxPool. type TxPoolOption func(*TxPool) @@ -452,9 +456,16 @@ func (txmp *TxPool) allEntriesSorted() []*wrappedTx { // constraints, the result will also be empty. func (txmp *TxPool) ReapMaxBytesMaxGas(maxBytes, maxGas int64) types.Txs { var totalGas, totalBytes int64 + currentTime := time.Now() var keep []types.Tx //nolint:prealloc for _, w := range txmp.allEntriesSorted() { + // skip transactions that have been in the mempool for less than the inclusion delay + // This gives time for the transaction to be broadcast to all peers + if currentTime.Sub(w.timestamp) < InclusionDelay { + break + } + // N.B. When computing byte size, we need to include the overhead for // encoding as protobuf to send to the application. This actually overestimates it // as we add the proto overhead to each transaction @@ -479,8 +490,11 @@ func (txmp *TxPool) ReapMaxBytesMaxGas(maxBytes, maxGas int64) types.Txs { // does not have that many transactions available. func (txmp *TxPool) ReapMaxTxs(max int) types.Txs { var keep []types.Tx //nolint:prealloc - + currentTime := time.Now() for _, w := range txmp.allEntriesSorted() { + if currentTime.Sub(w.timestamp) < InclusionDelay { + break + } if max >= 0 && len(keep) >= max { break } diff --git a/mempool/cat/reactor.go b/mempool/cat/reactor.go index 0ee6118e31..266428bdde 100644 --- a/mempool/cat/reactor.go +++ b/mempool/cat/reactor.go @@ -340,7 +340,7 @@ func (memR *Reactor) ReceiveEnvelope(e p2p.Envelope) { } if has && !memR.opts.ListenOnly { peerID := memR.ids.GetIDForPeer(e.Src.ID()) - memR.Logger.Info("sending a tx in response to a want msg", "peer", peerID) + memR.Logger.Info("sending a tx in response to a want msg", "peer", peerID, "txKey", txKey) if p2p.SendEnvelopeShim(e.Src, p2p.Envelope{ //nolint:staticcheck ChannelID: mempool.MempoolChannel, Message: &protomem.Txs{Txs: [][]byte{tx}}, @@ -399,10 +399,11 @@ func (memR *Reactor) broadcastSeenTx(txKey types.TxKey) { continue } - if peer.Send(MempoolStateChannel, bz) { - memR.Logger.Info("sent seen tx to peer", "peerID", peer.ID(), "txKey", txKey) + if !peer.Send(MempoolStateChannel, bz) { + memR.Logger.Error("failed to send seen tx to peer", "peerID", peer.ID(), "txKey", txKey) } } + memR.Logger.Info("broadcasted seen tx to all peers", "tx_key", txKey.String()) } // broadcastNewTx broadcast new transaction to all peers unless we are already sure they have seen the tx. From 74cac47564bade7e20fae80fe7a3e4880ff87296 Mon Sep 17 00:00:00 2001 From: Callum Waters <cmwaters19@gmail.com> Date: Fri, 9 Aug 2024 16:41:01 +0200 Subject: [PATCH 030/127] jump to 3 second delay --- mempool/cat/pool.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/mempool/cat/pool.go b/mempool/cat/pool.go index f1291ffb77..8f5a32e38c 100644 --- a/mempool/cat/pool.go +++ b/mempool/cat/pool.go @@ -31,7 +31,7 @@ var ( // InclusionDelay is the amount of time a transaction must be in the mempool // before it is included in the block. -const InclusionDelay = time.Second +const InclusionDelay = 3 * time.Second // TxPoolOption sets an optional parameter on the TxPool. type TxPoolOption func(*TxPool) @@ -71,8 +71,6 @@ type TxPool struct { // Thread-safe cache of rejected transactions for quick look-up rejectedTxCache *LRUTxCache - // Thread-safe cache of committed transactions for quick look-up - committedTxCache *LRUTxCache // Thread-safe cache of evicted transactions for quick look-up evictedTxCache *LRUTxCache // Thread-safe list of transactions peers have seen that we have not yet seen From dfa438254e7061f6790cb3a8ca1212535a6cfbd0 Mon Sep 17 00:00:00 2001 From: Callum Waters <cmwaters19@gmail.com> Date: Fri, 9 Aug 2024 16:53:58 +0200 Subject: [PATCH 031/127] improve logs --- mempool/cat/block_builder.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/mempool/cat/block_builder.go b/mempool/cat/block_builder.go index cdbc2d835c..7d31abca63 100644 --- a/mempool/cat/block_builder.go +++ b/mempool/cat/block_builder.go @@ -38,15 +38,18 @@ func (memR *Reactor) FetchTxsFromKeys(ctx context.Context, blockID []byte, compa missingKeys[i] = txKey } } - memR.Logger.Info("fetching transactions from peers", "blockID", blockID, "numTxs", len(txs), "numMissing", len(missingKeys)) + memR.Logger.Info("fetching transactions from peers", "numTxs", len(txs), "numMissing", len(missingKeys)) memR.mempool.metrics.MissingTxs.Add(float64(len(missingKeys))) // Check if we got lucky and already had all the transactions. if len(missingKeys) == 0 { - memR.Logger.Info("fetched all txs, none missing", "blockID", blockID) return txs, nil } + for _, missingTx := range missingKeys { + memR.findNewPeerToRequestTx(missingTx) + } + // setup a request for this block and begin to track and retrieve all missing transactions request := memR.blockFetcher.newRequest( blockID, @@ -55,8 +58,9 @@ func (memR *Reactor) FetchTxsFromKeys(ctx context.Context, blockID []byte, compa txs, ) defer func() { + initialNumMissing := len(missingKeys) timeTaken := request.TimeTaken() - memR.Logger.Info("fetched txs", "timeTaken", timeTaken, "numMissing", len(missingKeys)) + memR.Logger.Info("fetched txs", "timeTaken", timeTaken, "numRetrieved", initialNumMissing-len(request.missingKeys), "numMissing", len(request.missingKeys)) }() // request the missing transactions if we haven't already From a56bbb77dec74fb69e82dc21547408c0777f1f81 Mon Sep 17 00:00:00 2001 From: Callum Waters <cmwaters19@gmail.com> Date: Fri, 9 Aug 2024 21:07:02 +0200 Subject: [PATCH 032/127] checkpoint --- consensus/state.go | 20 +++++++++++++++++--- consensus/types/round_state.go | 1 + mempool/cat/block_builder.go | 1 + mempool/cat/pool.go | 2 +- mempool/cat/reactor.go | 2 +- 5 files changed, 21 insertions(+), 5 deletions(-) diff --git a/consensus/state.go b/consensus/state.go index d722b5ab83..143a9b6191 100644 --- a/consensus/state.go +++ b/consensus/state.go @@ -874,6 +874,10 @@ func (cs *State) handleMsg(mi msgInfo) { // once proposal is set, we can receive block parts err = cs.setProposal(msg.Proposal) + if err != nil && cs.ProposalBlockParts.IsComplete() { + + } + case *CompactBlockMessage: err = cs.addCompactBlock(msg, peerID) @@ -1079,8 +1083,6 @@ func (cs *State) enterNewRound(height int64, round int32) { } else { logger.Info("resetting proposal info", "proposer", propAddress) cs.Proposal = nil - cs.ProposalBlock = nil - cs.ProposalBlockParts = nil } logger.Debug("entering new round", @@ -1202,9 +1204,13 @@ func (cs *State) defaultDecideProposal(height int64, round int32) { // Decide on block if cs.TwoThirdPrevoteBlock != nil { - // If there is valid block, choose that. + // If there is a block that received two third prevotes, choose that. block, blockParts = cs.TwoThirdPrevoteBlock, cs.TwoThirdPrevoteBlockParts blockHash = block.Hash() + } else if cs.IsProposalBlockValid { + // If there is a valid block, choose that. + block, blockParts = cs.ProposalBlock, cs.ProposalBlockParts + blockHash = block.Hash() } else { // Create a new proposal block from state/txs from the mempool. schema.WriteABCI(cs.traceClient, schema.PrepareProposalStart, height, round) @@ -1351,6 +1357,8 @@ func (cs *State) defaultDoPrevote(height int64, round int32) { return } + cs.IsProposalBlockValid = false + // If ProposalBlock is nil, prevote nil. if cs.ProposalBlock == nil { logger.Debug("prevote step: ProposalBlock is nil") @@ -1387,6 +1395,8 @@ func (cs *State) defaultDoPrevote(height int64, round int32) { return } + cs.IsProposalBlockValid = true + // Prevote cs.ProposalBlock // NOTE: the proposal signature is validated when it is received, // and the proposal block parts are validated as they are received (against the merkle hash in the proposal) @@ -1957,6 +1967,10 @@ func (cs *State) defaultSetProposal(proposal *types.Proposal) error { proposal.Signature = p.Signature cs.Proposal = proposal + if cs.ProposalBlock != nil && !bytes.Equal(cs.ProposalBlock.Hash(), proposal.BlockID.Hash) { + cs.ProposalBlock = nil + cs.ProposalBlockParts = nil + } // We don't update cs.ProposalBlockParts if it is already set. // This happens if we're already in cstypes.RoundStepCommit or if there is a valid block in the current round. // TODO: We can check if Proposal is for a different block as this is a sign of misbehavior! diff --git a/consensus/types/round_state.go b/consensus/types/round_state.go index 1030b2ae91..3c5651a2ed 100644 --- a/consensus/types/round_state.go +++ b/consensus/types/round_state.go @@ -77,6 +77,7 @@ type RoundState struct { ProposalCompactBlock *types.Block `json:"proposal_compact_block"` ProposalBlock *types.Block `json:"proposal_block"` ProposalBlockParts *types.PartSet `json:"proposal_block_parts"` + IsProposalBlockValid bool `json:"is_proposal_block_valid"` LockedRound int32 `json:"locked_round"` LockedBlock *types.Block `json:"locked_block"` LockedBlockParts *types.PartSet `json:"locked_block_parts"` diff --git a/mempool/cat/block_builder.go b/mempool/cat/block_builder.go index 7d31abca63..fe374d444a 100644 --- a/mempool/cat/block_builder.go +++ b/mempool/cat/block_builder.go @@ -231,6 +231,7 @@ func (br *blockRequest) WaitForBlock(ctx context.Context) ([][]byte, error) { for { select { case <-ctx.Done(): + br.endTime = time.Now().UTC() return nil, ctx.Err() case <-br.doneCh: br.mtx.Lock() diff --git a/mempool/cat/pool.go b/mempool/cat/pool.go index 8f5a32e38c..5f44b567ad 100644 --- a/mempool/cat/pool.go +++ b/mempool/cat/pool.go @@ -31,7 +31,7 @@ var ( // InclusionDelay is the amount of time a transaction must be in the mempool // before it is included in the block. -const InclusionDelay = 3 * time.Second +const InclusionDelay = 1 * time.Second // TxPoolOption sets an optional parameter on the TxPool. type TxPoolOption func(*TxPool) diff --git a/mempool/cat/reactor.go b/mempool/cat/reactor.go index 266428bdde..d5d7335d8a 100644 --- a/mempool/cat/reactor.go +++ b/mempool/cat/reactor.go @@ -492,7 +492,7 @@ func (memR *Reactor) findNewPeerToRequestTx(txKey types.TxKey) { // No other free peer has the transaction we are looking for. // We give up 🤷♂️ and hope either a peer responds late or the tx // is gossiped again - memR.Logger.Debug("no other peer has the tx we are looking for", "txKey", txKey) + memR.Logger.Info("no other peer has the tx we are looking for", "txKey", txKey) // TODO: should add a metric to see how common this is return } From 905ba36d3c931eaa596b79656d956d551c6ffb11 Mon Sep 17 00:00:00 2001 From: Callum Waters <cmwaters19@gmail.com> Date: Mon, 12 Aug 2024 18:20:27 +0200 Subject: [PATCH 033/127] Revert "checkpoint" This reverts commit a56bbb77dec74fb69e82dc21547408c0777f1f81. --- consensus/state.go | 20 +++----------------- consensus/types/round_state.go | 1 - mempool/cat/block_builder.go | 1 - mempool/cat/pool.go | 2 +- mempool/cat/reactor.go | 2 +- 5 files changed, 5 insertions(+), 21 deletions(-) diff --git a/consensus/state.go b/consensus/state.go index 143a9b6191..d722b5ab83 100644 --- a/consensus/state.go +++ b/consensus/state.go @@ -874,10 +874,6 @@ func (cs *State) handleMsg(mi msgInfo) { // once proposal is set, we can receive block parts err = cs.setProposal(msg.Proposal) - if err != nil && cs.ProposalBlockParts.IsComplete() { - - } - case *CompactBlockMessage: err = cs.addCompactBlock(msg, peerID) @@ -1083,6 +1079,8 @@ func (cs *State) enterNewRound(height int64, round int32) { } else { logger.Info("resetting proposal info", "proposer", propAddress) cs.Proposal = nil + cs.ProposalBlock = nil + cs.ProposalBlockParts = nil } logger.Debug("entering new round", @@ -1204,13 +1202,9 @@ func (cs *State) defaultDecideProposal(height int64, round int32) { // Decide on block if cs.TwoThirdPrevoteBlock != nil { - // If there is a block that received two third prevotes, choose that. + // If there is valid block, choose that. block, blockParts = cs.TwoThirdPrevoteBlock, cs.TwoThirdPrevoteBlockParts blockHash = block.Hash() - } else if cs.IsProposalBlockValid { - // If there is a valid block, choose that. - block, blockParts = cs.ProposalBlock, cs.ProposalBlockParts - blockHash = block.Hash() } else { // Create a new proposal block from state/txs from the mempool. schema.WriteABCI(cs.traceClient, schema.PrepareProposalStart, height, round) @@ -1357,8 +1351,6 @@ func (cs *State) defaultDoPrevote(height int64, round int32) { return } - cs.IsProposalBlockValid = false - // If ProposalBlock is nil, prevote nil. if cs.ProposalBlock == nil { logger.Debug("prevote step: ProposalBlock is nil") @@ -1395,8 +1387,6 @@ func (cs *State) defaultDoPrevote(height int64, round int32) { return } - cs.IsProposalBlockValid = true - // Prevote cs.ProposalBlock // NOTE: the proposal signature is validated when it is received, // and the proposal block parts are validated as they are received (against the merkle hash in the proposal) @@ -1967,10 +1957,6 @@ func (cs *State) defaultSetProposal(proposal *types.Proposal) error { proposal.Signature = p.Signature cs.Proposal = proposal - if cs.ProposalBlock != nil && !bytes.Equal(cs.ProposalBlock.Hash(), proposal.BlockID.Hash) { - cs.ProposalBlock = nil - cs.ProposalBlockParts = nil - } // We don't update cs.ProposalBlockParts if it is already set. // This happens if we're already in cstypes.RoundStepCommit or if there is a valid block in the current round. // TODO: We can check if Proposal is for a different block as this is a sign of misbehavior! diff --git a/consensus/types/round_state.go b/consensus/types/round_state.go index 3c5651a2ed..1030b2ae91 100644 --- a/consensus/types/round_state.go +++ b/consensus/types/round_state.go @@ -77,7 +77,6 @@ type RoundState struct { ProposalCompactBlock *types.Block `json:"proposal_compact_block"` ProposalBlock *types.Block `json:"proposal_block"` ProposalBlockParts *types.PartSet `json:"proposal_block_parts"` - IsProposalBlockValid bool `json:"is_proposal_block_valid"` LockedRound int32 `json:"locked_round"` LockedBlock *types.Block `json:"locked_block"` LockedBlockParts *types.PartSet `json:"locked_block_parts"` diff --git a/mempool/cat/block_builder.go b/mempool/cat/block_builder.go index fe374d444a..7d31abca63 100644 --- a/mempool/cat/block_builder.go +++ b/mempool/cat/block_builder.go @@ -231,7 +231,6 @@ func (br *blockRequest) WaitForBlock(ctx context.Context) ([][]byte, error) { for { select { case <-ctx.Done(): - br.endTime = time.Now().UTC() return nil, ctx.Err() case <-br.doneCh: br.mtx.Lock() diff --git a/mempool/cat/pool.go b/mempool/cat/pool.go index 5f44b567ad..8f5a32e38c 100644 --- a/mempool/cat/pool.go +++ b/mempool/cat/pool.go @@ -31,7 +31,7 @@ var ( // InclusionDelay is the amount of time a transaction must be in the mempool // before it is included in the block. -const InclusionDelay = 1 * time.Second +const InclusionDelay = 3 * time.Second // TxPoolOption sets an optional parameter on the TxPool. type TxPoolOption func(*TxPool) diff --git a/mempool/cat/reactor.go b/mempool/cat/reactor.go index d5d7335d8a..266428bdde 100644 --- a/mempool/cat/reactor.go +++ b/mempool/cat/reactor.go @@ -492,7 +492,7 @@ func (memR *Reactor) findNewPeerToRequestTx(txKey types.TxKey) { // No other free peer has the transaction we are looking for. // We give up 🤷♂️ and hope either a peer responds late or the tx // is gossiped again - memR.Logger.Info("no other peer has the tx we are looking for", "txKey", txKey) + memR.Logger.Debug("no other peer has the tx we are looking for", "txKey", txKey) // TODO: should add a metric to see how common this is return } From 2273f83d376926d2c02d953e8f731325d3af6d98 Mon Sep 17 00:00:00 2001 From: Callum Waters <cmwaters19@gmail.com> Date: Mon, 12 Aug 2024 18:27:29 +0200 Subject: [PATCH 034/127] update logger --- mempool/cat/block_builder.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/mempool/cat/block_builder.go b/mempool/cat/block_builder.go index 7d31abca63..6897d24b16 100644 --- a/mempool/cat/block_builder.go +++ b/mempool/cat/block_builder.go @@ -231,11 +231,10 @@ func (br *blockRequest) WaitForBlock(ctx context.Context) ([][]byte, error) { for { select { case <-ctx.Done(): + br.setEndTime() return nil, ctx.Err() case <-br.doneCh: - br.mtx.Lock() - defer br.mtx.Unlock() - br.endTime = time.Now().UTC() + br.setEndTime() return br.txs, nil } } @@ -275,8 +274,8 @@ func (br *blockRequest) TimeTaken() uint64 { return uint64(br.endTime.Sub(br.startTime).Milliseconds()) } -func (br *blockRequest) NewTxsFound() string { +func (br *blockRequest) setEndTime() { br.mtx.Lock() defer br.mtx.Unlock() - return fmt.Sprintf("BlockRequest{height:%d, missing:%d}", br.height, len(br.missingKeys)) + br.endTime = time.Now().UTC() } From 513949c5c3f15b025edab4a8c95e57a15e40ffa3 Mon Sep 17 00:00:00 2001 From: Callum Waters <cmwaters19@gmail.com> Date: Tue, 13 Aug 2024 12:12:04 +0200 Subject: [PATCH 035/127] checkpoint --- consensus/reactor.go | 6 +++--- consensus/state.go | 18 ++++-------------- mempool/cat/pool.go | 2 +- mempool/metrics.go | 2 +- types/block.go | 11 +++++++++++ 5 files changed, 20 insertions(+), 19 deletions(-) diff --git a/consensus/reactor.go b/consensus/reactor.go index 97b6bf7cd9..654c4895a5 100644 --- a/consensus/reactor.go +++ b/consensus/reactor.go @@ -161,7 +161,7 @@ func (conR *Reactor) GetChannels() []*p2p.ChannelDescriptor { { ID: DataChannel, // maybe split between gossiping current block and catchup stuff // once we gossip the whole block there's nothing left to send until next height or round - Priority: 10, + Priority: 20, SendQueueCapacity: 100, RecvBufferCapacity: 50 * 4096, RecvMessageCapacity: maxMsgSize, @@ -169,7 +169,7 @@ func (conR *Reactor) GetChannels() []*p2p.ChannelDescriptor { }, { ID: VoteChannel, - Priority: 7, + Priority: 10, SendQueueCapacity: 100, RecvBufferCapacity: 100 * 100, RecvMessageCapacity: maxMsgSize, @@ -739,7 +739,7 @@ OUTER_LOOP: if rs.Proposal != nil && !prs.Proposal { // Proposal: share the proposal metadata with peer. { - logger.Debug("Sending proposal", "height", prs.Height, "round", prs.Round) + logger.Info("Sending proposal", "height", prs.Height, "round", prs.Round) if p2p.SendEnvelopeShim(peer, p2p.Envelope{ //nolint: staticcheck ChannelID: DataChannel, Message: &cmtcons.Proposal{Proposal: *rs.Proposal.ToProto()}, diff --git a/consensus/state.go b/consensus/state.go index d722b5ab83..ae0d551029 100644 --- a/consensus/state.go +++ b/consensus/state.go @@ -1222,7 +1222,7 @@ func (cs *State) defaultDecideProposal(height int64, round int32) { return } - block.Txs = types.ToTxs(keys) + cs.ProposalCompactBlock = block.CreateCompactBlock(keys) } // Flush the WAL. Otherwise, we may not recompute the same proposal to sign, @@ -1238,19 +1238,9 @@ func (cs *State) defaultDecideProposal(height int64, round int32) { if err := cs.privValidator.SignProposal(cs.state.ChainID, p); err == nil { proposal.Signature = p.Signature - // send proposal and block parts on internal msg queue - cs.sendInternalMessage(msgInfo{&ProposalMessage{proposal}, ""}) - cs.sendInternalMessage(msgInfo{&CompactBlockMessage{ - Block: block, - Round: p.Round, - }, ""}) - - for i := 0; i < int(blockParts.Total()); i++ { - part := blockParts.GetPart(i) - cs.sendInternalMessage(msgInfo{&BlockPartMessage{cs.Height, cs.Round, part}, ""}) - } - - cs.Logger.Debug("signed proposal", "height", height, "round", round, "proposal", proposal) + cs.Proposal = proposal + cs.ProposalBlock = block + cs.ProposalBlockParts = blockParts } else if !cs.replayMode { cs.Logger.Error("propose step; failed signing proposal", "height", height, "round", round, "err", err) } diff --git a/mempool/cat/pool.go b/mempool/cat/pool.go index 8f5a32e38c..4484c44247 100644 --- a/mempool/cat/pool.go +++ b/mempool/cat/pool.go @@ -31,7 +31,7 @@ var ( // InclusionDelay is the amount of time a transaction must be in the mempool // before it is included in the block. -const InclusionDelay = 3 * time.Second +const InclusionDelay = 2 * time.Second // TxPoolOption sets an optional parameter on the TxPool. type TxPoolOption func(*TxPool) diff --git a/mempool/metrics.go b/mempool/metrics.go index 485d669b38..dcfb47a7e9 100644 --- a/mempool/metrics.go +++ b/mempool/metrics.go @@ -180,7 +180,7 @@ func NopMetrics() *Metrics { AlreadySeenTxs: discard.NewCounter(), RequestedTxs: discard.NewCounter(), RerequestedTxs: discard.NewCounter(), - MissingTxs: discard.NewCounter(), + MissingTxs: discard.NewCounter(), } } diff --git a/types/block.go b/types/block.go index a7ea96c81a..08388782bb 100644 --- a/types/block.go +++ b/types/block.go @@ -97,6 +97,17 @@ func (b *Block) ValidateBasic() error { return nil } +func (b *Block) CreateCompactBlock(keys [][]byte) *Block { + return &Block{ + Header: b.Header, + LastCommit: b.LastCommit, + Evidence: b.Evidence, + Data: Data{ + Txs: ToTxs(keys), + }, + } +} + // fillHeader fills in any remaining header fields that are a function of the // block data NOTE: we expect celestia-app to populate the block DataHash but we // populate it here (in celestia-core) to not break existing tests in this repo. From 567a281a98dc9db4b458a6e22a5eeac9d23cde28 Mon Sep 17 00:00:00 2001 From: Callum Waters <cmwaters19@gmail.com> Date: Tue, 13 Aug 2024 13:49:27 +0200 Subject: [PATCH 036/127] checkpoint --- mempool/cat/block_builder.go | 7 +--- mempool/cat/reactor.go | 2 +- pkg/trace/fileserver.go | 78 ++++++++++++++++++++++-------------- rpc/core/mempool.go | 1 - 4 files changed, 50 insertions(+), 38 deletions(-) diff --git a/mempool/cat/block_builder.go b/mempool/cat/block_builder.go index 6897d24b16..f13b4ecd04 100644 --- a/mempool/cat/block_builder.go +++ b/mempool/cat/block_builder.go @@ -46,10 +46,6 @@ func (memR *Reactor) FetchTxsFromKeys(ctx context.Context, blockID []byte, compa return txs, nil } - for _, missingTx := range missingKeys { - memR.findNewPeerToRequestTx(missingTx) - } - // setup a request for this block and begin to track and retrieve all missing transactions request := memR.blockFetcher.newRequest( blockID, @@ -224,6 +220,7 @@ func newBlockRequest( // WaitForBlock is a blocking call that waits for the block to be fetched and completed. // It can be called concurrently. If the block was already fetched it returns immediately. func (br *blockRequest) WaitForBlock(ctx context.Context) ([][]byte, error) { + defer br.setEndTime() if br.IsDone() { return br.txs, nil } @@ -231,10 +228,8 @@ func (br *blockRequest) WaitForBlock(ctx context.Context) ([][]byte, error) { for { select { case <-ctx.Done(): - br.setEndTime() return nil, ctx.Err() case <-br.doneCh: - br.setEndTime() return br.txs, nil } } diff --git a/mempool/cat/reactor.go b/mempool/cat/reactor.go index 266428bdde..82663b9848 100644 --- a/mempool/cat/reactor.go +++ b/mempool/cat/reactor.go @@ -167,7 +167,7 @@ func (memR *Reactor) GetChannels() []*p2p.ChannelDescriptor { return []*p2p.ChannelDescriptor{ { ID: mempool.MempoolChannel, - Priority: 6, + Priority: 4, RecvMessageCapacity: txMsg.Size(), MessageType: &protomem.Message{}, }, diff --git a/pkg/trace/fileserver.go b/pkg/trace/fileserver.go index 21747d5902..ce9a1daae8 100644 --- a/pkg/trace/fileserver.go +++ b/pkg/trace/fileserver.go @@ -23,6 +23,8 @@ import ( "github.com/aws/aws-sdk-go/service/s3" ) +const jsonL = ".jsonl" + func (lt *LocalTracer) getTableHandler() http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { // Parse the request to get the data @@ -76,7 +78,7 @@ func pump(table string, br *bufio.Reader) (*io.PipeReader, *multipart.Writer) { defer w.Close() defer m.Close() - part, err := m.CreateFormFile("filename", table+".jsonl") + part, err := m.CreateFormFile("filename", table+jsonL) if err != nil { return } @@ -133,7 +135,7 @@ func GetTable(serverURL, table, dirPath string) error { return err } - outputFile, err := os.Create(path.Join(dirPath, table+".jsonl")) + outputFile, err := os.Create(path.Join(dirPath, table+jsonL)) if err != nil { return err } @@ -264,7 +266,9 @@ func (lt *LocalTracer) PushAll() error { // S3Download downloads files that match some prefix from an S3 bucket to a // local directory dst. -func S3Download(dst, prefix string, cfg S3Config) error { +// fileNames is a list of traced jsonl file names to download. If it is empty, all traces are downloaded. +// fileNames should not have .jsonl suffix. +func S3Download(dst, prefix string, cfg S3Config, fileNames ...string) error { // Ensure local directory structure exists err := os.MkdirAll(dst, os.ModePerm) if err != nil { @@ -293,37 +297,51 @@ func S3Download(dst, prefix string, cfg S3Config) error { err = s3Svc.ListObjectsV2Pages(input, func(page *s3.ListObjectsV2Output, lastPage bool) bool { for _, content := range page.Contents { - localFilePath := filepath.Join(dst, prefix, strings.TrimPrefix(*content.Key, prefix)) - fmt.Printf("Downloading %s to %s\n", *content.Key, localFilePath) - - // Create the directories in the path - if err := os.MkdirAll(filepath.Dir(localFilePath), os.ModePerm); err != nil { - return false - } - - // Create a file to write the S3 Object contents to. - f, err := os.Create(localFilePath) - if err != nil { - return false - } + key := *content.Key - resp, err := s3Svc.GetObject(&s3.GetObjectInput{ - Bucket: aws.String(cfg.BucketName), - Key: aws.String(*content.Key), - }) - if err != nil { - f.Close() - continue + // If no fileNames are specified, download all files + if len(fileNames) == 0 { + fileNames = append(fileNames, strings.TrimPrefix(key, prefix)) } - defer resp.Body.Close() - // Copy the contents of the S3 object to the local file - if _, err := io.Copy(f, resp.Body); err != nil { - return false + for _, filename := range fileNames { + // Add .jsonl suffix to the fileNames + fullFilename := filename + jsonL + if strings.HasSuffix(key, fullFilename) { + localFilePath := filepath.Join(dst, prefix, strings.TrimPrefix(key, prefix)) + fmt.Printf("Downloading %s to %s\n", key, localFilePath) + + // Create the directories in the path + if err := os.MkdirAll(filepath.Dir(localFilePath), os.ModePerm); err != nil { + return false + } + + // Create a file to write the S3 Object contents to. + f, err := os.Create(localFilePath) + if err != nil { + return false + } + + resp, err := s3Svc.GetObject(&s3.GetObjectInput{ + Bucket: aws.String(cfg.BucketName), + Key: aws.String(key), + }) + if err != nil { + f.Close() + continue + } + defer resp.Body.Close() + + // Copy the contents of the S3 object to the local file + if _, err := io.Copy(f, resp.Body); err != nil { + f.Close() + return false + } + + fmt.Printf("Successfully downloaded %s to %s\n", key, localFilePath) + f.Close() + } } - - fmt.Printf("Successfully downloaded %s to %s\n", *content.Key, localFilePath) - f.Close() } return !lastPage // continue paging }) diff --git a/rpc/core/mempool.go b/rpc/core/mempool.go index 90dc99c806..c0ac45d024 100644 --- a/rpc/core/mempool.go +++ b/rpc/core/mempool.go @@ -36,7 +36,6 @@ func BroadcastTxAsync(ctx *rpctypes.Context, tx types.Tx) (*ctypes.ResultBroadca func BroadcastTxSync(ctx *rpctypes.Context, tx types.Tx) (*ctypes.ResultBroadcastTx, error) { resCh := make(chan *abci.Response, 1) env := GetEnvironment() - env.Logger.Info("BroadcastTxSync") err := env.Mempool.CheckTx(tx, func(res *abci.Response) { select { case <-ctx.Context().Done(): From dcea04c5cf69513228ac68d4855e66c276c28e65 Mon Sep 17 00:00:00 2001 From: Callum Waters <cmwaters19@gmail.com> Date: Tue, 13 Aug 2024 14:51:45 +0200 Subject: [PATCH 037/127] add more tracing data --- consensus/msgs.go | 4 +- consensus/reactor.go | 34 +++- mempool/cat/block_builder.go | 2 +- pkg/trace/schema/consensus.go | 59 +++++++ proto/tendermint/consensus/message.go | 8 +- proto/tendermint/consensus/types.pb.go | 225 ++++++++++++------------- proto/tendermint/consensus/types.proto | 6 +- 7 files changed, 212 insertions(+), 126 deletions(-) diff --git a/consensus/msgs.go b/consensus/msgs.go index 1c2f1b43ef..ca50fbb0f5 100644 --- a/consensus/msgs.go +++ b/consensus/msgs.go @@ -72,7 +72,7 @@ func MsgToProto(msg Message) (*cmtcons.Message, error) { return m.Wrap().(*cmtcons.Message), nil case *HasBlockMessage: - m := &cmtcons.HasCompactBlock{ + m := &cmtcons.HasBlock{ Height: msg.Height, Round: msg.Round, } @@ -216,7 +216,7 @@ func MsgFromProto(p *cmtcons.Message) (Message, error) { Round: msg.Round, } - case *cmtcons.HasCompactBlock: + case *cmtcons.HasBlock: pb = &HasBlockMessage{ Height: msg.Height, Round: msg.Round, diff --git a/consensus/reactor.go b/consensus/reactor.go index 654c4895a5..917a929e82 100644 --- a/consensus/reactor.go +++ b/consensus/reactor.go @@ -397,8 +397,22 @@ func (conR *Reactor) ReceiveEnvelope(e p2p.Envelope) { case *CompactBlockMessage: ps.SetHasBlock(msg.Block.Height, msg.Round) conR.conS.peerMsgQueue <- msgInfo{msg, e.Src.ID()} + schema.WriteCompactBlock( + conR.traceClient, + msg.Block.Height, + msg.Round, + string(e.Src.ID()), + schema.Download, + ) case *HasBlockMessage: ps.SetHasBlock(msg.Height, msg.Round) + schema.WriteHasBlock( + conR.traceClient, + msg.Height, + msg.Round, + string(e.Src.ID()), + schema.Download, + ) case *BlockPartMessage: ps.SetHasProposalBlockPart(msg.Height, msg.Round, int(msg.Part.Index)) conR.Metrics.BlockParts.With("peer_id", string(e.Src.ID())).Add(1) @@ -609,11 +623,18 @@ func (conR *Reactor) broadcastHasVoteMessage(vote *types.Vote) { func (conR *Reactor) broadcastHasBlockMessage(data *types.EventDataCompleteProposal) { conR.Switch.BroadcastEnvelope(p2p.Envelope{ ChannelID: DataChannel, - Message: &cmtcons.HasCompactBlock{ + Message: &cmtcons.HasBlock{ Height: data.Height, Round: data.Round, }, }) + schema.WriteHasBlock( + conR.traceClient, + data.Height, + data.Round, + "", + schema.Upload, + ) } func makeRoundStepMessage(rs *cstypes.RoundState) (nrsMsg *cmtcons.NewRoundStep) { @@ -667,7 +688,7 @@ func (conR *Reactor) getRoundState() *cstypes.RoundState { } func (conR *Reactor) gossipDataRoutine(peer p2p.Peer, ps *PeerState) { - logger := conR.Logger.With("peer", peer) + logger := conR.Logger.With("peer", peer.ID()) OUTER_LOOP: for { @@ -695,6 +716,13 @@ OUTER_LOOP: }, logger) { ps.SetHasBlock(prs.Height, prs.Round) conR.conS.metrics.CompactBlocksSent.Add(1) + schema.WriteCompactBlock( + conR.traceClient, + prs.Height, + prs.Round, + string(peer.ID()), + schema.Upload, + ) } continue OUTER_LOOP } @@ -1339,7 +1367,7 @@ func (ps *PeerState) SetHasProposalBlockPart(height int64, round int32, index in ps.PRS.ProposalBlockParts.SetIndex(index, true) } -// SetHasCompactBlock sets the given block part index as known for the peer. +// SetHasBlock sets the given block part index as known for the peer. func (ps *PeerState) SetHasBlock(height int64, round int32) { ps.mtx.Lock() defer ps.mtx.Unlock() diff --git a/mempool/cat/block_builder.go b/mempool/cat/block_builder.go index f13b4ecd04..0173887928 100644 --- a/mempool/cat/block_builder.go +++ b/mempool/cat/block_builder.go @@ -45,6 +45,7 @@ func (memR *Reactor) FetchTxsFromKeys(ctx context.Context, blockID []byte, compa if len(missingKeys) == 0 { return txs, nil } + initialNumMissing := len(missingKeys) // setup a request for this block and begin to track and retrieve all missing transactions request := memR.blockFetcher.newRequest( @@ -54,7 +55,6 @@ func (memR *Reactor) FetchTxsFromKeys(ctx context.Context, blockID []byte, compa txs, ) defer func() { - initialNumMissing := len(missingKeys) timeTaken := request.TimeTaken() memR.Logger.Info("fetched txs", "timeTaken", timeTaken, "numRetrieved", initialNumMissing-len(request.missingKeys), "numMissing", len(request.missingKeys)) }() diff --git a/pkg/trace/schema/consensus.go b/pkg/trace/schema/consensus.go index 6584429c2d..4feb7abd49 100644 --- a/pkg/trace/schema/consensus.go +++ b/pkg/trace/schema/consensus.go @@ -15,6 +15,8 @@ func ConsensusTables() []string { VoteTable, ConsensusStateTable, ProposalTable, + CompactBlockTable, + HasBlockTable, } } @@ -247,3 +249,60 @@ func WriteProposal( TransferType: transferType, }) } + +const ( + // CompactBlockTable is the name of the table that stores the consensus compact block + // parts. + CompactBlockTable = "consensus_compact_block" +) + +type CompactBlock struct { + Height int64 `json:"height"` + Round int32 `json:"round"` + PeerID string `json:"peer_id"` + TransferType TransferType `json:"transfer_type"` +} + +func (c CompactBlock) Table() string { + return CompactBlockTable +} + +func WriteCompactBlock( + client trace.Tracer, + height int64, + round int32, + peerID string, + transferType TransferType, +) { + client.Write(CompactBlock{ + Height: height, + Round: round, + PeerID: peerID, + TransferType: transferType, + }) +} + +const ( + HasBlockTable = "consensus_has_block" +) + +type HasBlock struct { + Height int64 `json:"height"` + Round int32 `json:"round"` + PeerID string `json:"peer_id"` + TransferType TransferType `json:"transfer_type"` +} + +func (h HasBlock) Table() string { + return HasBlockTable +} + +func WriteHasBlock( + client trace.Tracer, + height int64, + round int32, + peerID string, + transferType TransferType, +) { + client.Write(HasBlock{Height: height, Round: round, PeerID: peerID, TransferType: transferType}) +} diff --git a/proto/tendermint/consensus/message.go b/proto/tendermint/consensus/message.go index 063c78a9aa..2a5e2dec60 100644 --- a/proto/tendermint/consensus/message.go +++ b/proto/tendermint/consensus/message.go @@ -49,9 +49,9 @@ func (m *CompactBlock) Wrap() proto.Message { return cm } -func (m *HasCompactBlock) Wrap() proto.Message { +func (m *HasBlock) Wrap() proto.Message { cm := &Message{} - cm.Sum = &Message_HasCompactBlock{HasCompactBlock: m} + cm.Sum = &Message_HasBlock{HasBlock: m} return cm } @@ -104,8 +104,8 @@ func (m *Message) Unwrap() (proto.Message, error) { case *Message_CompactBlock: return m.GetCompactBlock(), nil - case *Message_HasCompactBlock: - return m.GetHasCompactBlock(), nil + case *Message_HasBlock: + return m.GetHasBlock(), nil case *Message_BlockPart: return m.GetBlockPart(), nil diff --git a/proto/tendermint/consensus/types.pb.go b/proto/tendermint/consensus/types.pb.go index 6a7322e1d7..5ee921aff4 100644 --- a/proto/tendermint/consensus/types.pb.go +++ b/proto/tendermint/consensus/types.pb.go @@ -341,24 +341,24 @@ func (m *CompactBlock) GetRound() int32 { return 0 } -// HasCompactBlock to signal that the node has the block -type HasCompactBlock struct { +// HasBlock to signal that the node has the block +type HasBlock struct { Height int64 `protobuf:"varint,1,opt,name=height,proto3" json:"height,omitempty"` Round int32 `protobuf:"varint,2,opt,name=round,proto3" json:"round,omitempty"` } -func (m *HasCompactBlock) Reset() { *m = HasCompactBlock{} } -func (m *HasCompactBlock) String() string { return proto.CompactTextString(m) } -func (*HasCompactBlock) ProtoMessage() {} -func (*HasCompactBlock) Descriptor() ([]byte, []int) { +func (m *HasBlock) Reset() { *m = HasBlock{} } +func (m *HasBlock) String() string { return proto.CompactTextString(m) } +func (*HasBlock) ProtoMessage() {} +func (*HasBlock) Descriptor() ([]byte, []int) { return fileDescriptor_81a22d2efc008981, []int{5} } -func (m *HasCompactBlock) XXX_Unmarshal(b []byte) error { +func (m *HasBlock) XXX_Unmarshal(b []byte) error { return m.Unmarshal(b) } -func (m *HasCompactBlock) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { +func (m *HasBlock) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { if deterministic { - return xxx_messageInfo_HasCompactBlock.Marshal(b, m, deterministic) + return xxx_messageInfo_HasBlock.Marshal(b, m, deterministic) } else { b = b[:cap(b)] n, err := m.MarshalToSizedBuffer(b) @@ -368,26 +368,26 @@ func (m *HasCompactBlock) XXX_Marshal(b []byte, deterministic bool) ([]byte, err return b[:n], nil } } -func (m *HasCompactBlock) XXX_Merge(src proto.Message) { - xxx_messageInfo_HasCompactBlock.Merge(m, src) +func (m *HasBlock) XXX_Merge(src proto.Message) { + xxx_messageInfo_HasBlock.Merge(m, src) } -func (m *HasCompactBlock) XXX_Size() int { +func (m *HasBlock) XXX_Size() int { return m.Size() } -func (m *HasCompactBlock) XXX_DiscardUnknown() { - xxx_messageInfo_HasCompactBlock.DiscardUnknown(m) +func (m *HasBlock) XXX_DiscardUnknown() { + xxx_messageInfo_HasBlock.DiscardUnknown(m) } -var xxx_messageInfo_HasCompactBlock proto.InternalMessageInfo +var xxx_messageInfo_HasBlock proto.InternalMessageInfo -func (m *HasCompactBlock) GetHeight() int64 { +func (m *HasBlock) GetHeight() int64 { if m != nil { return m.Height } return 0 } -func (m *HasCompactBlock) GetRound() int32 { +func (m *HasBlock) GetRound() int32 { if m != nil { return m.Round } @@ -727,7 +727,7 @@ type Message struct { // *Message_VoteSetMaj23 // *Message_VoteSetBits // *Message_CompactBlock - // *Message_HasCompactBlock + // *Message_HasBlock Sum isMessage_Sum `protobuf_oneof:"sum"` } @@ -800,21 +800,21 @@ type Message_VoteSetBits struct { type Message_CompactBlock struct { CompactBlock *CompactBlock `protobuf:"bytes,10,opt,name=compact_block,json=compactBlock,proto3,oneof" json:"compact_block,omitempty"` } -type Message_HasCompactBlock struct { - HasCompactBlock *HasCompactBlock `protobuf:"bytes,11,opt,name=has_compact_block,json=hasCompactBlock,proto3,oneof" json:"has_compact_block,omitempty"` +type Message_HasBlock struct { + HasBlock *HasBlock `protobuf:"bytes,11,opt,name=has_block,json=hasBlock,proto3,oneof" json:"has_block,omitempty"` } -func (*Message_NewRoundStep) isMessage_Sum() {} -func (*Message_NewValidBlock) isMessage_Sum() {} -func (*Message_Proposal) isMessage_Sum() {} -func (*Message_ProposalPol) isMessage_Sum() {} -func (*Message_BlockPart) isMessage_Sum() {} -func (*Message_Vote) isMessage_Sum() {} -func (*Message_HasVote) isMessage_Sum() {} -func (*Message_VoteSetMaj23) isMessage_Sum() {} -func (*Message_VoteSetBits) isMessage_Sum() {} -func (*Message_CompactBlock) isMessage_Sum() {} -func (*Message_HasCompactBlock) isMessage_Sum() {} +func (*Message_NewRoundStep) isMessage_Sum() {} +func (*Message_NewValidBlock) isMessage_Sum() {} +func (*Message_Proposal) isMessage_Sum() {} +func (*Message_ProposalPol) isMessage_Sum() {} +func (*Message_BlockPart) isMessage_Sum() {} +func (*Message_Vote) isMessage_Sum() {} +func (*Message_HasVote) isMessage_Sum() {} +func (*Message_VoteSetMaj23) isMessage_Sum() {} +func (*Message_VoteSetBits) isMessage_Sum() {} +func (*Message_CompactBlock) isMessage_Sum() {} +func (*Message_HasBlock) isMessage_Sum() {} func (m *Message) GetSum() isMessage_Sum { if m != nil { @@ -893,9 +893,9 @@ func (m *Message) GetCompactBlock() *CompactBlock { return nil } -func (m *Message) GetHasCompactBlock() *HasCompactBlock { - if x, ok := m.GetSum().(*Message_HasCompactBlock); ok { - return x.HasCompactBlock +func (m *Message) GetHasBlock() *HasBlock { + if x, ok := m.GetSum().(*Message_HasBlock); ok { + return x.HasBlock } return nil } @@ -913,7 +913,7 @@ func (*Message) XXX_OneofWrappers() []interface{} { (*Message_VoteSetMaj23)(nil), (*Message_VoteSetBits)(nil), (*Message_CompactBlock)(nil), - (*Message_HasCompactBlock)(nil), + (*Message_HasBlock)(nil), } } @@ -923,7 +923,7 @@ func init() { proto.RegisterType((*Proposal)(nil), "tendermint.consensus.Proposal") proto.RegisterType((*ProposalPOL)(nil), "tendermint.consensus.ProposalPOL") proto.RegisterType((*CompactBlock)(nil), "tendermint.consensus.CompactBlock") - proto.RegisterType((*HasCompactBlock)(nil), "tendermint.consensus.HasCompactBlock") + proto.RegisterType((*HasBlock)(nil), "tendermint.consensus.HasBlock") proto.RegisterType((*BlockPart)(nil), "tendermint.consensus.BlockPart") proto.RegisterType((*Vote)(nil), "tendermint.consensus.Vote") proto.RegisterType((*HasVote)(nil), "tendermint.consensus.HasVote") @@ -935,66 +935,65 @@ func init() { func init() { proto.RegisterFile("tendermint/consensus/types.proto", fileDescriptor_81a22d2efc008981) } var fileDescriptor_81a22d2efc008981 = []byte{ - // 932 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xd4, 0x56, 0x41, 0x6f, 0x1b, 0x45, - 0x14, 0xf6, 0x36, 0x76, 0xec, 0x3c, 0xdb, 0x31, 0x1d, 0xa5, 0xc5, 0x04, 0x70, 0xc2, 0x22, 0xa4, - 0x08, 0x81, 0x8d, 0x9c, 0x03, 0x52, 0x41, 0x2a, 0xb8, 0x40, 0x37, 0x55, 0xd3, 0x5a, 0xeb, 0x52, - 0x21, 0x2e, 0xab, 0xf5, 0xee, 0xc8, 0x1e, 0xea, 0xdd, 0x59, 0xed, 0x4c, 0x12, 0x72, 0xe5, 0x17, - 0x70, 0xe2, 0xc4, 0xdf, 0x40, 0xe2, 0x27, 0xf4, 0xd8, 0x23, 0xa7, 0x0a, 0x25, 0x3f, 0x01, 0x71, - 0x47, 0xf3, 0x66, 0x6c, 0x8f, 0x9b, 0x4d, 0x20, 0x17, 0xa4, 0xde, 0x66, 0xf7, 0xbd, 0xf7, 0xed, - 0x7b, 0xdf, 0x7b, 0xef, 0x9b, 0x85, 0x5d, 0x49, 0xd3, 0x98, 0xe6, 0x09, 0x4b, 0x65, 0x2f, 0xe2, - 0xa9, 0xa0, 0xa9, 0x38, 0x12, 0x3d, 0x79, 0x9a, 0x51, 0xd1, 0xcd, 0x72, 0x2e, 0x39, 0xd9, 0x5a, - 0x7a, 0x74, 0x17, 0x1e, 0xdb, 0x5b, 0x13, 0x3e, 0xe1, 0xe8, 0xd0, 0x53, 0x27, 0xed, 0xbb, 0xfd, - 0x8e, 0x85, 0x86, 0x18, 0x36, 0x52, 0x81, 0x75, 0x3c, 0xe3, 0xd1, 0x33, 0x63, 0xb5, 0x33, 0x99, - 0xb1, 0xb1, 0xe8, 0x8d, 0x99, 0x5c, 0x89, 0x77, 0x7f, 0x73, 0xa0, 0xf1, 0x88, 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0x1b, 0xf1, 0xa4, 0x67, 0xff, 0x83, 0x2d, 0x8f, 0xfa, 0x4f, 0xae, 0xe8, - 0x5f, 0x70, 0xbc, 0x8e, 0xb6, 0xfd, 0x7f, 0x02, 0x00, 0x00, 0xff, 0xff, 0xed, 0x49, 0x80, 0x76, - 0x2a, 0x0a, 0x00, 0x00, + // 928 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xd4, 0x56, 0x4f, 0x6f, 0xe3, 0x44, + 0x14, 0xb7, 0xb7, 0x49, 0x93, 0xbc, 0x24, 0x2d, 0x8c, 0xba, 0x4b, 0x28, 0x90, 0x16, 0x73, 0xa9, + 0x10, 0x24, 0x28, 0x3d, 0x80, 0x16, 0x10, 0xe0, 0x05, 0xd6, 0x5d, 0x6d, 0x77, 0xa3, 0xc9, 0xb2, + 0x42, 0x5c, 0x2c, 0xc7, 0x1e, 0x25, 0xc3, 0xc6, 0x1e, 0xcb, 0x33, 0x6d, 0xe9, 0x0d, 0xf1, 0x09, + 0xf8, 0x00, 0x7c, 0x0d, 0x24, 0x3e, 0xc2, 0x1e, 0xf7, 0xc8, 0x69, 0x85, 0xda, 0x8f, 0x80, 0xb8, + 0xa3, 0xf9, 0x93, 0x64, 0xc2, 0xba, 0x81, 0x72, 0x40, 0xe2, 0x36, 0xf6, 0x7b, 0xef, 0xe7, 0xf7, + 0x7e, 0xef, 0xbd, 0xdf, 0x18, 0xf6, 0x05, 0xc9, 0x12, 0x52, 0xa4, 0x34, 0x13, 0xfd, 0x98, 0x65, + 0x9c, 0x64, 0xfc, 0x84, 0xf7, 0xc5, 0x79, 0x4e, 0x78, 0x2f, 0x2f, 0x98, 0x60, 0x68, 0x67, 0xe9, + 0xd1, 0x5b, 0x78, 0xec, 0xee, 0x4c, 0xd8, 0x84, 0x29, 0x87, 0xbe, 0x3c, 0x69, 0xdf, 0xdd, 0xd7, + 0x2d, 0x34, 0x85, 0x61, 0x23, 0x95, 0x58, 0xc7, 0x33, 0x16, 0x3f, 0x31, 0x56, 0x3b, 0x93, 0x19, + 0x1d, 0xf3, 0xfe, 0x98, 0x8a, 0x95, 0x78, 0xef, 0x67, 0x17, 0x5a, 0x0f, 0xc8, 0x19, 0x66, 0x27, + 0x59, 0x32, 0x12, 0x24, 0x47, 0xb7, 0x60, 0x73, 0x4a, 0xe8, 0x64, 0x2a, 0x3a, 0xee, 0xbe, 0x7b, + 0xb0, 0x81, 0xcd, 0x13, 0xda, 0x81, 0x6a, 0x21, 0x9d, 0x3a, 0x37, 0xf6, 0xdd, 0x83, 0x2a, 0xd6, + 0x0f, 0x08, 0x41, 0x85, 0x0b, 0x92, 0x77, 0x36, 0xf6, 0xdd, 0x83, 0x36, 0x56, 0x67, 0xf4, 0x3e, + 0x74, 0x38, 0x89, 0x59, 0x96, 0xf0, 0x90, 0xd3, 0x2c, 0x26, 0x21, 0x17, 0x51, 0x21, 0x42, 0x41, + 0x53, 0xd2, 0xa9, 0x28, 0xcc, 0x9b, 0xc6, 0x3e, 0x92, 0xe6, 0x91, 0xb4, 0x3e, 0xa2, 0x29, 0x41, + 0x6f, 0xc3, 0xcb, 0xb3, 0x88, 0x8b, 0x30, 0x66, 0x69, 0x4a, 0x45, 0xa8, 0x3f, 0x57, 0x55, 0x9f, + 0xdb, 0x96, 0x86, 0x3b, 0xea, 0xbd, 0x4a, 0xd5, 0xfb, 0xc3, 0x85, 0xf6, 0x03, 0x72, 0xf6, 0x38, + 0x9a, 0xd1, 0xc4, 0x97, 0x15, 0x5f, 0x33, 0xf1, 0xaf, 0xe1, 0xa6, 0x22, 0x2a, 0xcc, 0x65, 0x6e, + 0x9c, 0x88, 0x70, 0x4a, 0xa2, 0x84, 0x14, 0xaa, 0x92, 0xe6, 0x60, 0xaf, 0x67, 0x75, 0x48, 0xf3, + 0x35, 0x8c, 0x0a, 0x31, 0x22, 0x22, 0x50, 0x6e, 0x7e, 0xe5, 0xe9, 0xf3, 0x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0xc7, 0xcb, 0xae, + 0xf3, 0xec, 0xb2, 0xeb, 0xfc, 0x7a, 0xd9, 0x75, 0xbe, 0xf9, 0x70, 0x42, 0xc5, 0xf4, 0x64, 0xdc, + 0x8b, 0x59, 0xda, 0xb7, 0x7f, 0xb3, 0x96, 0x47, 0xfd, 0xb3, 0x56, 0xf6, 0xbb, 0x37, 0xde, 0x54, + 0xb6, 0xc3, 0x3f, 0x03, 0x00, 0x00, 0xff, 0xff, 0xb0, 0xe4, 0x2b, 0x16, 0x0d, 0x0a, 0x00, 0x00, } func (m *NewRoundStep) Marshal() (dAtA []byte, err error) { @@ -1226,7 +1225,7 @@ func (m *CompactBlock) MarshalToSizedBuffer(dAtA []byte) (int, error) { return len(dAtA) - i, nil } -func (m *HasCompactBlock) Marshal() (dAtA []byte, err error) { +func (m *HasBlock) Marshal() (dAtA []byte, err error) { size := m.Size() dAtA = make([]byte, size) n, err := m.MarshalToSizedBuffer(dAtA[:size]) @@ -1236,12 +1235,12 @@ func (m *HasCompactBlock) Marshal() (dAtA []byte, err error) { return dAtA[:n], nil } -func (m *HasCompactBlock) MarshalTo(dAtA []byte) (int, error) { +func (m *HasBlock) MarshalTo(dAtA []byte) (int, error) { size := m.Size() return m.MarshalToSizedBuffer(dAtA[:size]) } -func (m *HasCompactBlock) MarshalToSizedBuffer(dAtA []byte) (int, error) { +func (m *HasBlock) MarshalToSizedBuffer(dAtA []byte) (int, error) { i := len(dAtA) _ = i var l int @@ -1728,16 +1727,16 @@ func (m *Message_CompactBlock) MarshalToSizedBuffer(dAtA []byte) (int, error) { } return len(dAtA) - i, nil } -func (m *Message_HasCompactBlock) MarshalTo(dAtA []byte) (int, error) { +func (m *Message_HasBlock) MarshalTo(dAtA []byte) (int, error) { size := m.Size() return m.MarshalToSizedBuffer(dAtA[:size]) } -func (m *Message_HasCompactBlock) MarshalToSizedBuffer(dAtA []byte) (int, error) { +func (m *Message_HasBlock) MarshalToSizedBuffer(dAtA []byte) (int, error) { i := len(dAtA) - if m.HasCompactBlock != nil { + if m.HasBlock != nil { { - size, err := m.HasCompactBlock.MarshalToSizedBuffer(dAtA[:i]) + size, err := m.HasBlock.MarshalToSizedBuffer(dAtA[:i]) if err != nil { return 0, err } @@ -1852,7 +1851,7 @@ func (m *CompactBlock) Size() (n int) { return n } -func (m *HasCompactBlock) Size() (n int) { +func (m *HasBlock) Size() (n int) { if m == nil { return 0 } @@ -2092,14 +2091,14 @@ func (m *Message_CompactBlock) Size() (n int) { } return n } -func (m *Message_HasCompactBlock) Size() (n int) { +func (m *Message_HasBlock) Size() (n int) { if m == nil { return 0 } var l int _ = l - if m.HasCompactBlock != nil { - l = m.HasCompactBlock.Size() + if m.HasBlock != nil { + l = m.HasBlock.Size() n += 1 + l + sovTypes(uint64(l)) } return n @@ -2742,7 +2741,7 @@ func (m *CompactBlock) Unmarshal(dAtA []byte) error { } return nil } -func (m *HasCompactBlock) Unmarshal(dAtA []byte) error { +func (m *HasBlock) Unmarshal(dAtA []byte) error { l := len(dAtA) iNdEx := 0 for iNdEx < l { @@ -2765,10 +2764,10 @@ func (m *HasCompactBlock) Unmarshal(dAtA []byte) error { fieldNum := int32(wire >> 3) wireType := int(wire & 0x7) if wireType == 4 { - return fmt.Errorf("proto: HasCompactBlock: wiretype end group for non-group") + return fmt.Errorf("proto: HasBlock: wiretype end group for non-group") } if fieldNum <= 0 { - return fmt.Errorf("proto: HasCompactBlock: illegal tag %d (wire type %d)", fieldNum, wire) + return fmt.Errorf("proto: HasBlock: illegal tag %d (wire type %d)", fieldNum, wire) } switch fieldNum { case 1: @@ -3857,7 +3856,7 @@ func (m *Message) Unmarshal(dAtA []byte) error { iNdEx = postIndex case 11: if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field HasCompactBlock", wireType) + return fmt.Errorf("proto: wrong wireType = %d for field HasBlock", wireType) } var msglen int for shift := uint(0); ; shift += 7 { @@ -3884,11 +3883,11 @@ func (m *Message) Unmarshal(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - v := &HasCompactBlock{} + v := &HasBlock{} if err := v.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { return err } - m.Sum = &Message_HasCompactBlock{v} + m.Sum = &Message_HasBlock{v} iNdEx = postIndex default: iNdEx = preIndex diff --git a/proto/tendermint/consensus/types.proto b/proto/tendermint/consensus/types.proto index 8db2b5e80f..13edd2f91c 100644 --- a/proto/tendermint/consensus/types.proto +++ b/proto/tendermint/consensus/types.proto @@ -47,8 +47,8 @@ message CompactBlock { int32 round = 2; } -// HasCompactBlock to signal that the node has the block -message HasCompactBlock { +// HasBlock to signal that the node has the block +message HasBlock { int64 height = 1; int32 round = 2; } @@ -102,6 +102,6 @@ message Message { VoteSetMaj23 vote_set_maj23 = 8; VoteSetBits vote_set_bits = 9; CompactBlock compact_block = 10; - HasCompactBlock has_compact_block = 11; + HasBlock has_block = 11; } } From c5fd2300effb8f94080534108fbe9156f86ab07b Mon Sep 17 00:00:00 2001 From: Callum Waters <cmwaters19@gmail.com> Date: Tue, 13 Aug 2024 14:58:08 +0200 Subject: [PATCH 038/127] return some logs to debug --- mempool/cat/reactor.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/mempool/cat/reactor.go b/mempool/cat/reactor.go index 82663b9848..a547c3397e 100644 --- a/mempool/cat/reactor.go +++ b/mempool/cat/reactor.go @@ -340,7 +340,7 @@ func (memR *Reactor) ReceiveEnvelope(e p2p.Envelope) { } if has && !memR.opts.ListenOnly { peerID := memR.ids.GetIDForPeer(e.Src.ID()) - memR.Logger.Info("sending a tx in response to a want msg", "peer", peerID, "txKey", txKey) + memR.Logger.Debug("sending a transaction in response to a want msg", "peer", peerID, "txKey", txKey) if p2p.SendEnvelopeShim(e.Src, p2p.Envelope{ //nolint:staticcheck ChannelID: mempool.MempoolChannel, Message: &protomem.Txs{Txs: [][]byte{tx}}, @@ -403,7 +403,7 @@ func (memR *Reactor) broadcastSeenTx(txKey types.TxKey) { memR.Logger.Error("failed to send seen tx to peer", "peerID", peer.ID(), "txKey", txKey) } } - memR.Logger.Info("broadcasted seen tx to all peers", "tx_key", txKey.String()) + memR.Logger.Debug("broadcasted seen tx to all peers", "tx_key", txKey.String()) } // broadcastNewTx broadcast new transaction to all peers unless we are already sure they have seen the tx. @@ -460,7 +460,7 @@ func (memR *Reactor) requestTx(txKey types.TxKey, peer p2p.Peer) { success := peer.Send(MempoolStateChannel, bz) //nolint:staticcheck if success { - memR.Logger.Info("requested tx", "txKey", txKey, "peerID", peer.ID()) + memR.Logger.Debug("requested transaction", "txKey", txKey, "peerID", peer.ID()) memR.mempool.metrics.RequestedTxs.Add(1) requested := memR.requests.Add(txKey, memR.ids.GetIDForPeer(peer.ID()), memR.findNewPeerToRequestTx) if !requested { From b4ebb57a33cc4998b312ae055ae8afa09502615d Mon Sep 17 00:00:00 2001 From: Callum Waters <cmwaters19@gmail.com> Date: Wed, 14 Aug 2024 12:29:57 +0200 Subject: [PATCH 039/127] checkpoint --- consensus/byzantine_test.go | 2 +- consensus/msgs.go | 10 +- consensus/reactor.go | 11 +- consensus/state.go | 6 + proto/tendermint/consensus/types.pb.go | 177 ++++++++++++++++--------- proto/tendermint/consensus/types.proto | 3 +- 6 files changed, 137 insertions(+), 72 deletions(-) diff --git a/consensus/byzantine_test.go b/consensus/byzantine_test.go index c315cfe9a6..a61c033c48 100644 --- a/consensus/byzantine_test.go +++ b/consensus/byzantine_test.go @@ -241,7 +241,7 @@ func TestByzantinePrevoteEquivocation(t *testing.T) { // send proposal and block parts on internal msg queue lazyProposer.sendInternalMessage(msgInfo{&ProposalMessage{proposal}, ""}) - lazyProposer.sendInternalMessage(msgInfo{&CompactBlockMessage{block, proposal.Round}, ""}) + lazyProposer.sendInternalMessage(msgInfo{&CompactBlockMessage{block, proposal.BlockID.Hash, proposal.Round}, ""}) for i := 0; i < int(blockParts.Total()); i++ { part := blockParts.GetPart(i) lazyProposer.sendInternalMessage(msgInfo{&BlockPartMessage{lazyProposer.Height, lazyProposer.Round, part}, ""}) diff --git a/consensus/msgs.go b/consensus/msgs.go index ca50fbb0f5..19db3b3c7f 100644 --- a/consensus/msgs.go +++ b/consensus/msgs.go @@ -66,8 +66,9 @@ func MsgToProto(msg Message) (*cmtcons.Message, error) { return nil, fmt.Errorf("msg to proto error: %w", err) } m := &cmtcons.CompactBlock{ - Block: block, - Round: msg.Round, + Block: block, + BlockHash: msg.BlockHash, + Round: msg.Round, } return m.Wrap().(*cmtcons.Message), nil @@ -212,8 +213,9 @@ func MsgFromProto(p *cmtcons.Message) (Message, error) { return nil, fmt.Errorf("compactBlock msg to proto error: %w", err) } pb = &CompactBlockMessage{ - Block: block, - Round: msg.Round, + Block: block, + BlockHash: msg.BlockHash, + Round: msg.Round, } case *cmtcons.HasBlock: diff --git a/consensus/reactor.go b/consensus/reactor.go index 917a929e82..3ee791fb6a 100644 --- a/consensus/reactor.go +++ b/consensus/reactor.go @@ -710,8 +710,8 @@ OUTER_LOOP: if p2p.SendEnvelopeShim(peer, p2p.Envelope{ //nolint: staticcheck ChannelID: DataChannel, Message: &cmtcons.CompactBlock{ - Block: compactBlock, - Round: rs.Round, + Block: compactBlock, + BlockHash: rs.Proposal.BlockID.Hash, }, }, logger) { ps.SetHasBlock(prs.Height, prs.Round) @@ -1912,8 +1912,9 @@ func (m *ProposalPOLMessage) String() string { // CompactBlockMessage is sent when gossipping a piece of the proposed block. type CompactBlockMessage struct { - Block *types.Block - Round int32 + Block *types.Block + BlockHash []byte + Round int32 } // ValidateBasic performs basic validation. @@ -1923,7 +1924,7 @@ func (m *CompactBlockMessage) ValidateBasic() error { // String returns a string representation. func (m *CompactBlockMessage) String() string { - return fmt.Sprintf("[CompactBlock H:%d, R: %d]", m.Block.Height, m.Round) + return fmt.Sprintf("[CompactBlock Height:%d, Hash: %X, Round: %d]", m.Block.Height, m.BlockHash, m.Round) } //------------------------------------- diff --git a/consensus/state.go b/consensus/state.go index ae0d551029..f5036a31b6 100644 --- a/consensus/state.go +++ b/consensus/state.go @@ -1994,6 +1994,12 @@ func (cs *State) addCompactBlock(msg *CompactBlockMessage, peerID p2p.ID) error return nil } + // compare that this is the correct compact block + if !bytes.Equal(cs.Proposal.BlockID.Hash, msg.BlockHash) { + cs.Logger.Debug("received compact block with a different block hash", "current", cs.Proposal.BlockID.Hash, "got", msg.BlockHash) + return nil + } + blockHash := cs.Proposal.BlockID.Hash timeout := cs.config.Propose(cs.Round) diff --git a/proto/tendermint/consensus/types.pb.go b/proto/tendermint/consensus/types.pb.go index 5ee921aff4..b893c10678 100644 --- a/proto/tendermint/consensus/types.pb.go +++ b/proto/tendermint/consensus/types.pb.go @@ -290,8 +290,9 @@ func (m *ProposalPOL) GetProposalPol() bits.BitArray { // CompactBlock is for gossiping a block type CompactBlock struct { - Block *types.Block `protobuf:"bytes,1,opt,name=block,proto3" json:"block,omitempty"` - Round int32 `protobuf:"varint,2,opt,name=round,proto3" json:"round,omitempty"` + Block *types.Block `protobuf:"bytes,1,opt,name=block,proto3" json:"block,omitempty"` + BlockHash []byte `protobuf:"bytes,2,opt,name=block_hash,json=blockHash,proto3" json:"block_hash,omitempty"` + Round int32 `protobuf:"varint,3,opt,name=round,proto3" json:"round,omitempty"` } func (m *CompactBlock) Reset() { *m = CompactBlock{} } @@ -334,6 +335,13 @@ func (m *CompactBlock) GetBlock() *types.Block { return nil } +func (m *CompactBlock) GetBlockHash() []byte { + if m != nil { + return m.BlockHash + } + return nil +} + func (m *CompactBlock) GetRound() int32 { if m != nil { return m.Round @@ -935,65 +943,67 @@ func init() { func init() { proto.RegisterFile("tendermint/consensus/types.proto", fileDescriptor_81a22d2efc008981) } var fileDescriptor_81a22d2efc008981 = []byte{ - // 928 bytes of a gzipped FileDescriptorProto + // 949 bytes of a gzipped FileDescriptorProto 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xd4, 0x56, 0x4f, 0x6f, 0xe3, 0x44, - 0x14, 0xb7, 0xb7, 0x49, 0x93, 0xbc, 0x24, 0x2d, 0x8c, 0xba, 0x4b, 0x28, 0x90, 0x16, 0x73, 0xa9, - 0x10, 0x24, 0x28, 0x3d, 0x80, 0x16, 0x10, 0xe0, 0x05, 0xd6, 0x5d, 0x6d, 0x77, 0xa3, 0xc9, 0xb2, - 0x42, 0x5c, 0x2c, 0xc7, 0x1e, 0x25, 0xc3, 0xc6, 0x1e, 0xcb, 0x33, 0x6d, 0xe9, 0x0d, 0xf1, 0x09, - 0xf8, 0x00, 0x7c, 0x0d, 0x24, 0x3e, 0xc2, 0x1e, 0xf7, 0xc8, 0x69, 0x85, 0xda, 0x8f, 0x80, 0xb8, - 0xa3, 0xf9, 0x93, 0x64, 0xc2, 0xba, 0x81, 0x72, 0x40, 0xe2, 0x36, 0xf6, 0x7b, 0xef, 0xe7, 0xf7, - 0x7e, 0xef, 0xbd, 0xdf, 0x18, 0xf6, 0x05, 0xc9, 0x12, 0x52, 0xa4, 0x34, 0x13, 0xfd, 0x98, 0x65, - 0x9c, 0x64, 0xfc, 0x84, 0xf7, 0xc5, 0x79, 0x4e, 0x78, 0x2f, 0x2f, 0x98, 0x60, 0x68, 0x67, 0xe9, - 0xd1, 0x5b, 0x78, 0xec, 0xee, 0x4c, 0xd8, 0x84, 0x29, 0x87, 0xbe, 0x3c, 0x69, 0xdf, 0xdd, 0xd7, - 0x2d, 0x34, 0x85, 0x61, 0x23, 0x95, 0x58, 0xc7, 0x33, 0x16, 0x3f, 0x31, 0x56, 0x3b, 0x93, 0x19, - 0x1d, 0xf3, 0xfe, 0x98, 0x8a, 0x95, 0x78, 0xef, 0x67, 0x17, 0x5a, 0x0f, 0xc8, 0x19, 0x66, 0x27, - 0x59, 0x32, 0x12, 0x24, 0x47, 0xb7, 0x60, 0x73, 0x4a, 0xe8, 0x64, 0x2a, 0x3a, 0xee, 0xbe, 0x7b, - 0xb0, 0x81, 0xcd, 0x13, 0xda, 0x81, 0x6a, 0x21, 0x9d, 0x3a, 0x37, 0xf6, 0xdd, 0x83, 0x2a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0xac, 0xa3, 0x6d, 0xff, 0xcf, 0x00, 0x00, 0x00, 0xff, 0xff, 0x74, 0xde, 0xe9, + 0x27, 0x2c, 0x0a, 0x00, 0x00, } func (m *NewRoundStep) Marshal() (dAtA []byte, err error) { @@ -1208,7 +1218,14 @@ func (m *CompactBlock) MarshalToSizedBuffer(dAtA []byte) (int, error) { if m.Round != 0 { i = encodeVarintTypes(dAtA, i, uint64(m.Round)) i-- - dAtA[i] = 0x10 + dAtA[i] = 0x18 + } + if len(m.BlockHash) > 0 { + i -= len(m.BlockHash) + copy(dAtA[i:], m.BlockHash) + i = encodeVarintTypes(dAtA, i, uint64(len(m.BlockHash))) + i-- + dAtA[i] = 0x12 } if m.Block != nil { { @@ -1845,6 +1862,10 @@ func (m *CompactBlock) Size() (n int) { l = m.Block.Size() n += 1 + l + sovTypes(uint64(l)) } + l = len(m.BlockHash) + if l > 0 { + n += 1 + l + sovTypes(uint64(l)) + } if m.Round != 0 { n += 1 + sovTypes(uint64(m.Round)) } @@ -2702,6 +2723,40 @@ func (m *CompactBlock) Unmarshal(dAtA []byte) error { } iNdEx = postIndex case 2: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field BlockHash", wireType) + } + var byteLen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTypes + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + byteLen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if byteLen < 0 { + return ErrInvalidLengthTypes + } + postIndex := iNdEx + byteLen + if postIndex < 0 { + return ErrInvalidLengthTypes + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.BlockHash = append(m.BlockHash[:0], dAtA[iNdEx:postIndex]...) + if m.BlockHash == nil { + m.BlockHash = []byte{} + } + iNdEx = postIndex + case 3: if wireType != 0 { return fmt.Errorf("proto: wrong wireType = %d for field Round", wireType) } diff --git a/proto/tendermint/consensus/types.proto b/proto/tendermint/consensus/types.proto index 13edd2f91c..77057fdaff 100644 --- a/proto/tendermint/consensus/types.proto +++ b/proto/tendermint/consensus/types.proto @@ -44,7 +44,8 @@ message ProposalPOL { // CompactBlock is for gossiping a block message CompactBlock { tendermint.types.Block block = 1; - int32 round = 2; + bytes block_hash = 2; + int32 round = 3; } // HasBlock to signal that the node has the block From 33004a3541d01835d71e2dcb735fdda6c45dcb44 Mon Sep 17 00:00:00 2001 From: Callum Waters <cmwaters19@gmail.com> Date: Wed, 14 Aug 2024 12:45:47 +0200 Subject: [PATCH 040/127] sneaky fix --- consensus/reactor.go | 1 + types/block.go | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/consensus/reactor.go b/consensus/reactor.go index 3ee791fb6a..6a299cba0e 100644 --- a/consensus/reactor.go +++ b/consensus/reactor.go @@ -712,6 +712,7 @@ OUTER_LOOP: Message: &cmtcons.CompactBlock{ Block: compactBlock, BlockHash: rs.Proposal.BlockID.Hash, + Round: rs.Round, }, }, logger) { ps.SetHasBlock(prs.Height, prs.Round) diff --git a/types/block.go b/types/block.go index 08388782bb..8c235ff0a0 100644 --- a/types/block.go +++ b/types/block.go @@ -103,7 +103,8 @@ func (b *Block) CreateCompactBlock(keys [][]byte) *Block { LastCommit: b.LastCommit, Evidence: b.Evidence, Data: Data{ - Txs: ToTxs(keys), + Txs: ToTxs(keys), + SquareSize: b.Data.SquareSize, }, } } From 1bb12bf45b6c1bcb78cf39be232e329f32c0872d Mon Sep 17 00:00:00 2001 From: Callum Waters <cmwaters19@gmail.com> Date: Wed, 14 Aug 2024 14:53:54 +0200 Subject: [PATCH 041/127] fix --- consensus/state.go | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/consensus/state.go b/consensus/state.go index f5036a31b6..b2cc3b7440 100644 --- a/consensus/state.go +++ b/consensus/state.go @@ -1215,15 +1215,14 @@ func (cs *State) defaultDecideProposal(height int64, round int32) { } blockParts = block.MakePartSet(types.BlockPartSizeBytes) blockHash = block.Hash() + } - keys, err := cs.txFetcher.FetchKeysFromTxs(context.Background(), block.Txs.ToSliceOfBytes()) - if err != nil { - cs.Logger.Error("failed to fetch tx keys", "err", err) - return - } - - cs.ProposalCompactBlock = block.CreateCompactBlock(keys) + keys, err := cs.txFetcher.FetchKeysFromTxs(context.Background(), block.Txs.ToSliceOfBytes()) + if err != nil { + cs.Logger.Error("failed to fetch tx keys", "err", err) + return } + block.Data.Txs = types.ToTxs(keys) // Flush the WAL. Otherwise, we may not recompute the same proposal to sign, // and the privValidator will refuse to sign anything. @@ -1238,9 +1237,12 @@ func (cs *State) defaultDecideProposal(height int64, round int32) { if err := cs.privValidator.SignProposal(cs.state.ChainID, p); err == nil { proposal.Signature = p.Signature - cs.Proposal = proposal - cs.ProposalBlock = block - cs.ProposalBlockParts = blockParts + cs.sendInternalMessage(msgInfo{&ProposalMessage{proposal}, ""}) + cs.sendInternalMessage(msgInfo{&CompactBlockMessage{block, blockHash, proposal.Round}, ""}) + for i := 0; i < int(blockParts.Total()); i++ { + part := blockParts.GetPart(i) + cs.sendInternalMessage(msgInfo{&BlockPartMessage{cs.Height, cs.Round, part}, ""}) + } } else if !cs.replayMode { cs.Logger.Error("propose step; failed signing proposal", "height", height, "round", round, "err", err) } From b1086d24c175f438c0474bafbbd38195ee23a319 Mon Sep 17 00:00:00 2001 From: Callum Waters <cmwaters19@gmail.com> Date: Thu, 15 Aug 2024 11:46:57 +0200 Subject: [PATCH 042/127] checkpoint --- mempool/cat/block_builder.go | 2 ++ mempool/cat/pool.go | 4 +++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/mempool/cat/block_builder.go b/mempool/cat/block_builder.go index 0173887928..3ff0d6bf34 100644 --- a/mempool/cat/block_builder.go +++ b/mempool/cat/block_builder.go @@ -61,6 +61,8 @@ func (memR *Reactor) FetchTxsFromKeys(ctx context.Context, blockID []byte, compa // request the missing transactions if we haven't already for _, key := range missingKeys { + numHaveTx := len(memR.mempool.seenByPeersSet.Get(key)) + memR.Logger.Info("searching for missing tx", "numHaveTx", numHaveTx) memR.findNewPeerToRequestTx(key) } diff --git a/mempool/cat/pool.go b/mempool/cat/pool.go index 4484c44247..4f7a5df875 100644 --- a/mempool/cat/pool.go +++ b/mempool/cat/pool.go @@ -367,7 +367,9 @@ func (txmp *TxPool) TryAddNewTx(tx types.Tx, key types.TxKey, txInfo mempool.TxI txmp.rejectedTxCache.Push(key) } txmp.metrics.FailedTxs.Add(1) - return rsp, fmt.Errorf("application rejected transaction with code %d (Log: %s)", rsp.Code, rsp.Log) + // we don't return an error when there has been a fail code. Instead the + // client is expected to read the error code and the raw log + return rsp, nil } // Create wrapped tx From aab522ac4053e4ee91073d155f347017703cabae Mon Sep 17 00:00:00 2001 From: Callum Waters <cmwaters19@gmail.com> Date: Thu, 15 Aug 2024 15:47:12 +0200 Subject: [PATCH 043/127] fix nil pointer dereference --- consensus/reactor.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/consensus/reactor.go b/consensus/reactor.go index 6a299cba0e..9768897577 100644 --- a/consensus/reactor.go +++ b/consensus/reactor.go @@ -701,7 +701,7 @@ OUTER_LOOP: // Send compact block if !prs.Block && rs.ProposalBlockParts.HasHeader(prs.ProposalBlockPartSetHeader) { - if rs.ProposalCompactBlock != nil { + if rs.ProposalCompactBlock != nil && rs.Proposal != nil { compactBlock, err := rs.ProposalCompactBlock.ToProto() if err != nil { panic(err) From d46ad2d2f3873f28c6918312bb8c2e77a6963b75 Mon Sep 17 00:00:00 2001 From: Callum Waters <cmwaters19@gmail.com> Date: Fri, 16 Aug 2024 15:05:34 +0200 Subject: [PATCH 044/127] fix pruning and resetting --- consensus/state.go | 1 + mempool/cat/pool.go | 2 +- mempool/cat/reactor.go | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/consensus/state.go b/consensus/state.go index b2cc3b7440..11fe27ee3a 100644 --- a/consensus/state.go +++ b/consensus/state.go @@ -1081,6 +1081,7 @@ func (cs *State) enterNewRound(height int64, round int32) { cs.Proposal = nil cs.ProposalBlock = nil cs.ProposalBlockParts = nil + cs.ProposalCompactBlock = nil } logger.Debug("entering new round", diff --git a/mempool/cat/pool.go b/mempool/cat/pool.go index 4f7a5df875..f4e5d78769 100644 --- a/mempool/cat/pool.go +++ b/mempool/cat/pool.go @@ -558,7 +558,7 @@ func (txmp *TxPool) Update( // prune record of peers seen transactions after an hour // We assume by then that the transaction will no longer // need to be requested - txmp.seenByPeersSet.Prune(time.Now().Add(time.Hour)) + txmp.seenByPeersSet.Prune(time.Now().Add(-time.Hour)) // If there any uncommitted transactions left in the mempool, we either // initiate re-CheckTx per remaining transaction or notify that remaining diff --git a/mempool/cat/reactor.go b/mempool/cat/reactor.go index a547c3397e..eb2650d884 100644 --- a/mempool/cat/reactor.go +++ b/mempool/cat/reactor.go @@ -464,7 +464,7 @@ func (memR *Reactor) requestTx(txKey types.TxKey, peer p2p.Peer) { memR.mempool.metrics.RequestedTxs.Add(1) requested := memR.requests.Add(txKey, memR.ids.GetIDForPeer(peer.ID()), memR.findNewPeerToRequestTx) if !requested { - memR.Logger.Error("have already marked a tx as requested", "txKey", txKey, "peerID", peer.ID()) + memR.Logger.Debug("have already marked a tx as requested", "txKey", txKey, "peerID", peer.ID()) } } } From 5aefb63592d16bbd2f4dd68fdb2411d057f7ab2c Mon Sep 17 00:00:00 2001 From: Callum Waters <cmwaters19@gmail.com> Date: Fri, 16 Aug 2024 17:39:31 +0200 Subject: [PATCH 045/127] define a send queue capacity --- mempool/cat/pool.go | 14 +++++--------- mempool/cat/reactor.go | 6 ++++++ 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/mempool/cat/pool.go b/mempool/cat/pool.go index f4e5d78769..d0f311fa0a 100644 --- a/mempool/cat/pool.go +++ b/mempool/cat/pool.go @@ -31,7 +31,10 @@ var ( // InclusionDelay is the amount of time a transaction must be in the mempool // before it is included in the block. -const InclusionDelay = 2 * time.Second +const ( + InclusionDelay = 2 * time.Second + SeenSetPruneInterval = 10 * time.Minute +) // TxPoolOption sets an optional parameter on the TxPool. type TxPoolOption func(*TxPool) @@ -558,7 +561,7 @@ func (txmp *TxPool) Update( // prune record of peers seen transactions after an hour // We assume by then that the transaction will no longer // need to be requested - txmp.seenByPeersSet.Prune(time.Now().Add(-time.Hour)) + txmp.seenByPeersSet.Prune(time.Now().Add(-SeenSetPruneInterval)) // If there any uncommitted transactions left in the mempool, we either // initiate re-CheckTx per remaining transaction or notify that remaining @@ -794,13 +797,6 @@ func (txmp *TxPool) purgeExpiredTxs(blockHeight int64) { txmp.evictedTxCache.Push(tx.key) } txmp.metrics.ExpiredTxs.Add(float64(numExpired)) - - // purge old evicted and seen transactions - if txmp.config.TTLDuration == 0 { - // ensure that seenByPeersSet are eventually pruned - expirationAge = now.Add(-time.Hour) - } - txmp.seenByPeersSet.Prune(expirationAge) } func (txmp *TxPool) notifyTxsAvailable() { diff --git a/mempool/cat/reactor.go b/mempool/cat/reactor.go index eb2650d884..1a726870a7 100644 --- a/mempool/cat/reactor.go +++ b/mempool/cat/reactor.go @@ -168,12 +168,14 @@ func (memR *Reactor) GetChannels() []*p2p.ChannelDescriptor { { ID: mempool.MempoolChannel, Priority: 4, + SendQueueCapacity: 50, RecvMessageCapacity: txMsg.Size(), MessageType: &protomem.Message{}, }, { ID: MempoolStateChannel, Priority: 5, + SendQueueCapacity: 500, RecvMessageCapacity: stateMsg.Size(), MessageType: &protomem.Message{}, }, @@ -437,6 +439,8 @@ func (memR *Reactor) broadcastNewTx(wtx *wrappedTx) { if peer.Send(mempool.MempoolChannel, bz) { //nolint:staticcheck memR.mempool.PeerHasTx(id, wtx.key) + } else { + memR.Logger.Error("failed to send new tx to peer", "peerID", peer.ID(), "txKey", wtx.key) } } } @@ -466,6 +470,8 @@ func (memR *Reactor) requestTx(txKey types.TxKey, peer p2p.Peer) { if !requested { memR.Logger.Debug("have already marked a tx as requested", "txKey", txKey, "peerID", peer.ID()) } + } else { + memR.Logger.Error("failed to send message to request transaction", "txKey", txKey, "peerID", peer.ID()) } } From 178f81a65c0ffd9bd86d3c3b90f1d24b48caa540 Mon Sep 17 00:00:00 2001 From: Callum Waters <cmwaters19@gmail.com> Date: Tue, 20 Aug 2024 12:25:27 +0200 Subject: [PATCH 046/127] add more tracing data --- mempool/cat/block_builder.go | 18 ++++++++++++++++++ mempool/cat/reactor.go | 12 ++++++++++-- mempool/metrics.go | 12 ++++++++++++ pkg/trace/schema/mempool.go | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 73 insertions(+), 2 deletions(-) diff --git a/mempool/cat/block_builder.go b/mempool/cat/block_builder.go index 3ff0d6bf34..44908ec01c 100644 --- a/mempool/cat/block_builder.go +++ b/mempool/cat/block_builder.go @@ -6,6 +6,7 @@ import ( "sync" "time" + "github.com/tendermint/tendermint/pkg/trace/schema" "github.com/tendermint/tendermint/types" ) @@ -43,6 +44,14 @@ func (memR *Reactor) FetchTxsFromKeys(ctx context.Context, blockID []byte, compa // Check if we got lucky and already had all the transactions. if len(missingKeys) == 0 { + schema.WriteMempoolRecoveryStats( + memR.traceClient, + 0, + 0, + len(compactData), + 0, + blockID, + ) return txs, nil } initialNumMissing := len(missingKeys) @@ -57,6 +66,15 @@ func (memR *Reactor) FetchTxsFromKeys(ctx context.Context, blockID []byte, compa defer func() { timeTaken := request.TimeTaken() memR.Logger.Info("fetched txs", "timeTaken", timeTaken, "numRetrieved", initialNumMissing-len(request.missingKeys), "numMissing", len(request.missingKeys)) + schema.WriteMempoolRecoveryStats( + memR.traceClient, + initialNumMissing, + initialNumMissing-len(request.missingKeys), + len(compactData), + timeTaken, + blockID, + ) + memR.mempool.metrics.RecoveryRate.Observe(float64(initialNumMissing-len(request.missingKeys)) / float64(initialNumMissing)) }() // request the missing transactions if we haven't already diff --git a/mempool/cat/reactor.go b/mempool/cat/reactor.go index 1a726870a7..00f8998444 100644 --- a/mempool/cat/reactor.go +++ b/mempool/cat/reactor.go @@ -77,6 +77,10 @@ func (opts *ReactorOptions) VerifyAndComplete() error { return fmt.Errorf("max gossip delay (%d) cannot be negative", opts.MaxGossipDelay) } + if opts.TraceClient == nil { + opts.TraceClient = trace.NoOpTracer() + } + return nil } @@ -92,7 +96,7 @@ func NewReactor(mempool *TxPool, opts *ReactorOptions) (*Reactor, error) { ids: newMempoolIDs(), requests: newRequestScheduler(opts.MaxGossipDelay, defaultGlobalRequestTimeout), blockFetcher: newBlockFetcher(), - traceClient: trace.NoOpTracer(), + traceClient: opts.TraceClient, } memR.BaseReactor = *p2p.NewBaseReactor("Mempool", memR) return memR, nil @@ -340,6 +344,9 @@ func (memR *Reactor) ReceiveEnvelope(e p2p.Envelope) { // see if the tx was recently committed tx, has = memR.mempool.GetCommitted(txKey) } + // TODO: consider handling the case where we receive a HasTx message from a peer + // before we receive a WantTx message from them. In this case we might + // ignore the request if we know it's no longer valid. if has && !memR.opts.ListenOnly { peerID := memR.ids.GetIDForPeer(e.Src.ID()) memR.Logger.Debug("sending a transaction in response to a want msg", "peer", peerID, "txKey", txKey) @@ -391,7 +398,8 @@ func (memR *Reactor) broadcastSeenTx(txKey types.TxKey) { // if the peer is blocksyncing still and catching up // in which case we just skip sending the transaction if p.GetHeight() < memR.mempool.Height()-peerHeightDiff { - memR.Logger.Debug("peer is too far behind us. Skipping broadcast of seen tx") + memR.Logger.Debug("peer is too far behind us. Skipping broadcast of seen tx", "peerID", peer.ID(), + "peerHeight", p.GetHeight(), "ourHeight", memR.mempool.Height()) continue } } diff --git a/mempool/metrics.go b/mempool/metrics.go index dcfb47a7e9..ed8ba1c7cf 100644 --- a/mempool/metrics.go +++ b/mempool/metrics.go @@ -68,6 +68,9 @@ type Metrics struct { // MissingTxs defines the number of transactions that were not found in the mempool // from the current proposal MissingTxs metrics.Counter + + // RecoveryRate measures what percentage of missing transactions were able to be fetched + RecoveryRate metrics.Histogram } // PrometheusMetrics returns Metrics build using Prometheus client library. @@ -163,6 +166,14 @@ func PrometheusMetrics(namespace string, labelsAndValues ...string) *Metrics { Name: "missing_txs", Help: "Number of transactions that were not found in the mempool from the current proposal", }, labels).With(labelsAndValues...), + + RecoveryRate: prometheus.NewHistogramFrom(stdprometheus.HistogramOpts{ + Namespace: namespace, + Subsystem: MetricsSubsystem, + Name: "recovery_rate", + Help: "Percentage of missing transactions that were able to be fetched", + Buckets: stdprometheus.LinearBuckets(0, 0.1, 10), + }, labels).With(labelsAndValues...), } } @@ -181,6 +192,7 @@ func NopMetrics() *Metrics { RequestedTxs: discard.NewCounter(), RerequestedTxs: discard.NewCounter(), MissingTxs: discard.NewCounter(), + RecoveryRate: discard.NewHistogram(), } } diff --git a/pkg/trace/schema/mempool.go b/pkg/trace/schema/mempool.go index f2198fdf31..4b8b7d6b1c 100644 --- a/pkg/trace/schema/mempool.go +++ b/pkg/trace/schema/mempool.go @@ -98,3 +98,36 @@ func WriteMempoolPeerState( TxHash: bytes.HexBytes(txHash).String(), }) } + +const ( + MempoolRecoveryTable = "mempool_recovery" +) + +type MempoolRecovery struct { + Missing int `json:"missing"` + Recovered int `json:"recovered"` + Total int `json:"total"` + TimeTaken uint64 `json:"time_taken"` + BlockID string `json:"block_id"` +} + +func (m MempoolRecovery) Table() string { + return MempoolRecoveryTable +} + +func WriteMempoolRecoveryStats( + client trace.Tracer, + missing int, + recovered int, + total int, + timeTaken uint64, + blockID []byte, +) { + client.Write(MempoolRecovery{ + Missing: missing, + Recovered: recovered, + Total: total, + TimeTaken: timeTaken, + BlockID: bytes.HexBytes(blockID).String(), + }) +} From aaf89cf9bfc0acfeaa7e68b3db5629e6ebce9f8b Mon Sep 17 00:00:00 2001 From: Callum Waters <cmwaters19@gmail.com> Date: Fri, 23 Aug 2024 18:42:13 +0200 Subject: [PATCH 047/127] register mempool recovery table --- pkg/trace/schema/mempool.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/trace/schema/mempool.go b/pkg/trace/schema/mempool.go index 4b8b7d6b1c..118a91d3b4 100644 --- a/pkg/trace/schema/mempool.go +++ b/pkg/trace/schema/mempool.go @@ -10,6 +10,7 @@ func MempoolTables() []string { return []string{ MempoolTxTable, MempoolPeerStateTable, + MempoolRecoveryTable, } } From c42c6739697079ff6a668de97fac56767b8c4465 Mon Sep 17 00:00:00 2001 From: Callum Waters <cmwaters19@gmail.com> Date: Mon, 26 Aug 2024 10:59:31 +0200 Subject: [PATCH 048/127] allow the fetching of compact blocks to last the entire round --- consensus/reactor.go | 2 +- consensus/state.go | 5 ++++- consensus/types/round_state.go | 20 +++++++++++--------- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/consensus/reactor.go b/consensus/reactor.go index 9768897577..fa7f9817be 100644 --- a/consensus/reactor.go +++ b/consensus/reactor.go @@ -831,7 +831,7 @@ func (conR *Reactor) gossipDataForCatchup(logger log.Logger, rs *cstypes.RoundSt } else if !blockMeta.BlockID.PartSetHeader.Equals(prs.ProposalBlockPartSetHeader) { // this happens when the peer is on a different round to the round of the proposal // that was eventually committed. They should eventually receive 2/3 precommits and - // update the part set header to the one of the block that is committed + // update the part set header to the one of the block that is committed logger.Debug("Peer ProposalBlockPartSetHeader mismatch, sleeping", "blockPartSetHeader", blockMeta.BlockID.PartSetHeader, "peerBlockPartSetHeader", prs.ProposalBlockPartSetHeader) time.Sleep(conR.conS.config.PeerGossipSleepDuration) diff --git a/consensus/state.go b/consensus/state.go index 11fe27ee3a..1009520e70 100644 --- a/consensus/state.go +++ b/consensus/state.go @@ -1078,6 +1078,7 @@ func (cs *State) enterNewRound(height int64, round int32) { // for round 0. } else { logger.Info("resetting proposal info", "proposer", propAddress) + cs.RoundState.CancelAwaitCompactBlock() cs.Proposal = nil cs.ProposalBlock = nil cs.ProposalBlockParts = nil @@ -2010,8 +2011,10 @@ func (cs *State) addCompactBlock(msg *CompactBlockMessage, peerID p2p.ID) error // and other operations can be processed. cs.mtx.Unlock() - ctx, cancel := context.WithTimeout(context.Background(), timeout) + ctx, cancel := context.WithCancel(context.Background()) defer cancel() + // this will get triggered whenever we move into a new round. + cs.RoundState.CancelAwaitCompactBlock = cancel txs, err := cs.txFetcher.FetchTxsFromKeys(ctx, blockHash, compactBlock.Data.Txs.ToSliceOfBytes()) diff --git a/consensus/types/round_state.go b/consensus/types/round_state.go index 1030b2ae91..aa74084cf0 100644 --- a/consensus/types/round_state.go +++ b/consensus/types/round_state.go @@ -1,6 +1,7 @@ package types import ( + "context" "encoding/json" "fmt" "time" @@ -71,15 +72,16 @@ type RoundState struct { StartTime time.Time `json:"start_time"` // Subjective time when +2/3 precommits for Block at Round were found - CommitTime time.Time `json:"commit_time"` - Validators *types.ValidatorSet `json:"validators"` - Proposal *types.Proposal `json:"proposal"` - ProposalCompactBlock *types.Block `json:"proposal_compact_block"` - ProposalBlock *types.Block `json:"proposal_block"` - ProposalBlockParts *types.PartSet `json:"proposal_block_parts"` - LockedRound int32 `json:"locked_round"` - LockedBlock *types.Block `json:"locked_block"` - LockedBlockParts *types.PartSet `json:"locked_block_parts"` + CommitTime time.Time `json:"commit_time"` + Validators *types.ValidatorSet `json:"validators"` + Proposal *types.Proposal `json:"proposal"` + ProposalCompactBlock *types.Block `json:"proposal_compact_block"` + CancelAwaitCompactBlock context.CancelFunc + ProposalBlock *types.Block `json:"proposal_block"` + ProposalBlockParts *types.PartSet `json:"proposal_block_parts"` + LockedRound int32 `json:"locked_round"` + LockedBlock *types.Block `json:"locked_block"` + LockedBlockParts *types.PartSet `json:"locked_block_parts"` // Last known round with POL for non-nil valid block. TwoThirdPrevoteRound int32 `json:"valid_round"` From 9ff3319bfc72ff23a55a58eac88d170cc5eb850f Mon Sep 17 00:00:00 2001 From: Callum Waters <cmwaters19@gmail.com> Date: Mon, 26 Aug 2024 22:11:54 +0200 Subject: [PATCH 049/127] repropose the valid block if it exists --- consensus/state.go | 95 ++++++++++++++++++++++++---------- consensus/types/round_state.go | 6 ++- test/e2e/runner/rpc.go | 12 +++-- 3 files changed, 81 insertions(+), 32 deletions(-) diff --git a/consensus/state.go b/consensus/state.go index 1009520e70..02d04dcb4d 100644 --- a/consensus/state.go +++ b/consensus/state.go @@ -581,7 +581,6 @@ func (cs *State) updateRoundStep(round int32, step cstypes.RoundStepType) { // enterNewRound(height, 0) at cs.StartTime. func (cs *State) scheduleRound0(rs *cstypes.RoundState) { - // cs.Logger.Info("scheduleRound0", "now", cmttime.Now(), "startTime", cs.StartTime) sleepDuration := rs.StartTime.Sub(cmttime.Now()) cs.scheduleTimeout(sleepDuration, rs.Height, 0, cstypes.RoundStepNewHeight) } @@ -714,9 +713,14 @@ func (cs *State) updateToState(state sm.State) { cs.Validators = validators cs.Proposal = nil + cs.FetchCompactBlockCtx = nil + cs.CancelAwaitCompactBlock = nil cs.ProposalBlock = nil cs.ProposalBlockParts = nil cs.ProposalCompactBlock = nil + cs.ValidCompactBlock = nil + cs.ValidBlock = nil + cs.ValidBlockParts = nil cs.LockedRound = -1 cs.LockedBlock = nil cs.LockedBlockParts = nil @@ -877,10 +881,6 @@ func (cs *State) handleMsg(mi msgInfo) { case *CompactBlockMessage: err = cs.addCompactBlock(msg, peerID) - if err == nil { - cs.handleCompleteProposal(msg.Block.Height) - } - case *BlockPartMessage: // if the proposal is complete, we'll enterPrevote or tryFinalizeCommit added, err = cs.addProposalBlockPart(msg, peerID) @@ -1078,7 +1078,11 @@ func (cs *State) enterNewRound(height int64, round int32) { // for round 0. } else { logger.Info("resetting proposal info", "proposer", propAddress) - cs.RoundState.CancelAwaitCompactBlock() + if cs.CancelAwaitCompactBlock != nil { + cs.CancelAwaitCompactBlock() + cs.CancelAwaitCompactBlock = nil + } + cs.FetchCompactBlockCtx = nil cs.Proposal = nil cs.ProposalBlock = nil cs.ProposalBlockParts = nil @@ -1207,6 +1211,11 @@ func (cs *State) defaultDecideProposal(height int64, round int32) { // If there is valid block, choose that. block, blockParts = cs.TwoThirdPrevoteBlock, cs.TwoThirdPrevoteBlockParts blockHash = block.Hash() + } else if cs.ValidBlock != nil { + // If there is a valid block, choose that. + block = cs.ValidBlock + blockParts = block.MakePartSet(types.BlockPartSizeBytes) + blockHash = block.Hash() } else { // Create a new proposal block from state/txs from the mempool. schema.WriteABCI(cs.traceClient, schema.PrepareProposalStart, height, round) @@ -1347,7 +1356,7 @@ func (cs *State) defaultDoPrevote(height int64, round int32) { // If ProposalBlock is nil, prevote nil. if cs.ProposalBlock == nil { - logger.Debug("prevote step: ProposalBlock is nil") + logger.Info("prevote step: ProposalBlock is nil") cs.metrics.TimedOutProposals.Add(1) cs.signAddVote(cmtproto.PrevoteType, nil, types.PartSetHeader{}) return @@ -1381,6 +1390,12 @@ func (cs *State) defaultDoPrevote(height int64, round int32) { return } + // Mark this block as valid. If we ever become the proposer in a future round within this height, + // we will first proposed the TwoThirdPrevoteBlock, and then the ValidBlock. + cs.ValidBlock = cs.ProposalBlock + cs.ValidBlockParts = cs.ProposalBlockParts + cs.ValidCompactBlock = cs.ProposalCompactBlock + // Prevote cs.ProposalBlock // NOTE: the proposal signature is validated when it is received, // and the proposal block parts are validated as they are received (against the merkle hash in the proposal) @@ -1951,10 +1966,16 @@ func (cs *State) defaultSetProposal(proposal *types.Proposal) error { proposal.Signature = p.Signature cs.Proposal = proposal - // We don't update cs.ProposalBlockParts if it is already set. - // This happens if we're already in cstypes.RoundStepCommit or if there is a valid block in the current round. - // TODO: We can check if Proposal is for a different block as this is a sign of misbehavior! - if cs.ProposalBlockParts == nil { + + if cs.ValidBlock != nil && bytes.Equal(proposal.BlockID.Hash, cs.ValidBlock.Hash()) { + // The proposal is for an existing valid block that we already have. + cs.ProposalBlock = cs.ValidBlock + cs.ProposalCompactBlock = cs.ValidCompactBlock + cs.ProposalBlockParts = cs.ValidBlockParts + } else if cs.ProposalBlockParts == nil { + // We don't update cs.ProposalBlockParts if it is already set. + // This happens if we're already in cstypes.RoundStepCommit or if there is a valid block in the current round. + // TODO: We can check if Proposal is for a different block as this is a sign of misbehavior! cs.ProposalBlockParts = types.NewPartSetFromHeader(proposal.BlockID.PartSetHeader) } @@ -2004,29 +2025,43 @@ func (cs *State) addCompactBlock(msg *CompactBlockMessage, peerID p2p.ID) error return nil } - blockHash := cs.Proposal.BlockID.Hash - timeout := cs.config.Propose(cs.Round) - - // Yield the lock while we fetch the transactions from the mempool so that votes - // and other operations can be processed. - cs.mtx.Unlock() + if cs.FetchCompactBlockCtx == nil { + ctx, cancel := context.WithCancel(context.Background()) + cs.FetchCompactBlockCtx = ctx + cs.CancelAwaitCompactBlock = cancel + } - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() - // this will get triggered whenever we move into a new round. - cs.RoundState.CancelAwaitCompactBlock = cancel + // we start this as a goroutine so as to not block the reactor + // from recieving other messages or timeouts while the mempool + // attempts to fetch any missing transactions. If the block is + // correctly reconstructed, the handleCompleteProposal function + // will be called to process the block and move to the next step. + go cs.fetchCompactBlock(cs.FetchCompactBlockCtx, cs.Proposal.BlockID.Hash, compactBlock, cs.Round) + return nil +} +func (cs *State) fetchCompactBlock(ctx context.Context, blockHash []byte, compactBlock *types.Block, round int32) { txs, err := cs.txFetcher.FetchTxsFromKeys(ctx, blockHash, compactBlock.Data.Txs.ToSliceOfBytes()) - + cs.Logger.Info("requesting lock") cs.mtx.Lock() + cs.Logger.Info("got lock") + defer cs.mtx.Unlock() if err != nil { cs.metrics.CompactBlocksFailed.Add(1) if ctx.Err() != nil { + timeout := cs.config.Propose(round) cs.Logger.Info("failed to fetch transactions within the timeout", "timeout", timeout) - return nil } cs.Logger.Error("failed to fetch transactions for compact block", "err", err) - return err + } + if cs.Round != round || cs.Height != compactBlock.Height { + // we've moved into a later round/height where we no longer have the proposal + return + } + if cs.ProposalBlock != nil || cs.Proposal == nil { + // we already have the proposal block from an earlier iteration + // or we've moved into a later round/height where we no longer have the proposal + return } block := &types.Block{ Header: compactBlock.Header, @@ -2037,19 +2072,23 @@ func (cs *State) addCompactBlock(msg *CompactBlockMessage, peerID p2p.ID) error block.Txs = types.ToTxs(txs) if err := block.ValidateBasic(); err != nil { - return fmt.Errorf("received invalid block: %w", err) + cs.Logger.Error("received invalid block", "err", err) + return } if !bytes.Equal(block.Hash(), cs.Proposal.BlockID.Hash) { - return fmt.Errorf("received compact block with header hash [%v] that does not match proposal [%v]", block.Hash(), cs.Proposal.BlockID.Hash) + cs.Logger.Error("received compact block with header hash [%v] that does not match proposal [%v]", block.Hash(), cs.Proposal.BlockID.Hash) + return } // check that the part set header matched that of the partSet := block.MakePartSet(types.BlockPartSizeBytes) if !partSet.HasHeader(cs.Proposal.BlockID.PartSetHeader) { - return fmt.Errorf("received compact block with part set header [%v] that does not match proposal [%v]", partSet.Header(), cs.Proposal.BlockID.PartSetHeader) + cs.Logger.Error("received compact block with part set header [%v] that does not match proposal [%v]", partSet.Header(), cs.Proposal.BlockID.PartSetHeader) + return } + defer cs.CancelAwaitCompactBlock() cs.ProposalCompactBlock = compactBlock cs.ProposalBlock = block cs.ProposalBlockParts = partSet @@ -2061,7 +2100,7 @@ func (cs *State) addCompactBlock(msg *CompactBlockMessage, peerID p2p.ID) error cs.Logger.Error("failed publishing event complete proposal", "err", err) } - return nil + cs.handleCompleteProposal(compactBlock.Height) } // NOTE: block is not necessarily valid. diff --git a/consensus/types/round_state.go b/consensus/types/round_state.go index aa74084cf0..7d55a4d06e 100644 --- a/consensus/types/round_state.go +++ b/consensus/types/round_state.go @@ -75,10 +75,14 @@ type RoundState struct { CommitTime time.Time `json:"commit_time"` Validators *types.ValidatorSet `json:"validators"` Proposal *types.Proposal `json:"proposal"` - ProposalCompactBlock *types.Block `json:"proposal_compact_block"` + FetchCompactBlockCtx context.Context CancelAwaitCompactBlock context.CancelFunc + ProposalCompactBlock *types.Block `json:"proposal_compact_block"` ProposalBlock *types.Block `json:"proposal_block"` ProposalBlockParts *types.PartSet `json:"proposal_block_parts"` + ValidCompactBlock *types.Block `json:"valid_compact_block"` + ValidBlock *types.Block `json:"valid_block"` + ValidBlockParts *types.PartSet `json:"valid_compact_block_parts"` LockedRound int32 `json:"locked_round"` LockedBlock *types.Block `json:"locked_block"` LockedBlockParts *types.PartSet `json:"locked_block_parts"` diff --git a/test/e2e/runner/rpc.go b/test/e2e/runner/rpc.go index ab16ef69a8..1a21c61fcc 100644 --- a/test/e2e/runner/rpc.go +++ b/test/e2e/runner/rpc.go @@ -71,17 +71,23 @@ func waitForNode(node *e2e.Node, height int64, timeout time.Duration) (*rpctypes if err != nil { return nil, err } + lastHeight := int64(0) ctx, cancel := context.WithTimeout(context.Background(), timeout) defer cancel() for { status, err := client.Status(ctx) switch { case errors.Is(err, context.DeadlineExceeded): - return nil, fmt.Errorf("timed out waiting for %v to reach height %v", node.Name, height) + return nil, fmt.Errorf("timed out waiting for %v to reach height %v (last height: %v)", node.Name, height, lastHeight) case errors.Is(err, context.Canceled): return nil, err - case err == nil && status.SyncInfo.LatestBlockHeight >= height: - return status, nil + case err == nil: + if status.SyncInfo.LatestBlockHeight >= height { + return status, nil + } + lastHeight = status.SyncInfo.LatestBlockHeight + default: + return nil, err } time.Sleep(300 * time.Millisecond) From 7578e10b193c28d1affa4ac7c9e8a4b21159284e Mon Sep 17 00:00:00 2001 From: Callum Waters <cmwaters19@gmail.com> Date: Tue, 27 Aug 2024 15:17:47 +0200 Subject: [PATCH 050/127] use updateRoundStep to track consensus state changes --- consensus/state.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/consensus/state.go b/consensus/state.go index 02d04dcb4d..ea8d736743 100644 --- a/consensus/state.go +++ b/consensus/state.go @@ -573,6 +573,7 @@ func (cs *State) updateRoundStep(round int32, step cstypes.RoundStepType) { } if cs.Step != step { cs.metrics.MarkStep(cs.Step) + schema.WriteRoundState(cs.traceClient, cs.Height, round, uint8(step)) } } cs.Round = round @@ -746,10 +747,6 @@ func (cs *State) newStep() { cs.nSteps++ - step := uint8(cs.RoundState.Step) - - schema.WriteRoundState(cs.traceClient, cs.Height, cs.Round, step) - // newStep is called by updateToState in NewState before the eventBus is set! if cs.eventBus != nil { if err := cs.eventBus.PublishEventNewRoundStep(rs); err != nil { @@ -1586,6 +1583,7 @@ func (cs *State) enterPrecommitWait(height int64, round int32) { defer func() { // Done enterPrecommitWait: cs.TriggeredTimeoutPrecommit = true + cs.updateRoundStep(round, cstypes.RoundStepPrecommitWait) cs.newStep() }() From 34774cd5030758da085d374498b8838a0337467c Mon Sep 17 00:00:00 2001 From: Callum Waters <cmwaters19@gmail.com> Date: Tue, 27 Aug 2024 18:39:06 +0200 Subject: [PATCH 051/127] add compact hash to the proposal type --- abci/types/types.pb.go | 1 - consensus/reactor.go | 6 +- proto/tendermint/types/types.pb.go | 272 +++++++++++++++++------------ proto/tendermint/types/types.proto | 1 + types/proposal.go | 41 +++-- types/proposal_test.go | 9 +- 6 files changed, 199 insertions(+), 131 deletions(-) diff --git a/abci/types/types.pb.go b/abci/types/types.pb.go index c4af564c5e..5fcb1ec4c3 100644 --- a/abci/types/types.pb.go +++ b/abci/types/types.pb.go @@ -1408,7 +1408,6 @@ func (m *RequestProcessProposal) GetBlockData() *types1.Data { type Response struct { // Types that are valid to be assigned to Value: - // // *Response_Exception // *Response_Echo // *Response_Flush diff --git a/consensus/reactor.go b/consensus/reactor.go index fa7f9817be..9df81566df 100644 --- a/consensus/reactor.go +++ b/consensus/reactor.go @@ -153,7 +153,7 @@ func (conR *Reactor) GetChannels() []*p2p.ChannelDescriptor { return []*p2p.ChannelDescriptor{ { ID: StateChannel, - Priority: 6, + Priority: 5, SendQueueCapacity: 100, RecvMessageCapacity: maxMsgSize, MessageType: &cmtcons.Message{}, @@ -161,7 +161,7 @@ func (conR *Reactor) GetChannels() []*p2p.ChannelDescriptor { { ID: DataChannel, // maybe split between gossiping current block and catchup stuff // once we gossip the whole block there's nothing left to send until next height or round - Priority: 20, + Priority: 10, SendQueueCapacity: 100, RecvBufferCapacity: 50 * 4096, RecvMessageCapacity: maxMsgSize, @@ -169,7 +169,7 @@ func (conR *Reactor) GetChannels() []*p2p.ChannelDescriptor { }, { ID: VoteChannel, - Priority: 10, + Priority: 20, SendQueueCapacity: 100, RecvBufferCapacity: 100 * 100, RecvMessageCapacity: maxMsgSize, diff --git a/proto/tendermint/types/types.pb.go b/proto/tendermint/types/types.pb.go index 3e778caf53..a9a94555ee 100644 --- a/proto/tendermint/types/types.pb.go +++ b/proto/tendermint/types/types.pb.go @@ -797,13 +797,14 @@ func (m *CommitSig) GetSignature() []byte { } type Proposal struct { - Type SignedMsgType `protobuf:"varint,1,opt,name=type,proto3,enum=tendermint.types.SignedMsgType" json:"type,omitempty"` - Height int64 `protobuf:"varint,2,opt,name=height,proto3" json:"height,omitempty"` - Round int32 `protobuf:"varint,3,opt,name=round,proto3" json:"round,omitempty"` - PolRound int32 `protobuf:"varint,4,opt,name=pol_round,json=polRound,proto3" json:"pol_round,omitempty"` - BlockID BlockID `protobuf:"bytes,5,opt,name=block_id,json=blockId,proto3" json:"block_id"` - Timestamp time.Time `protobuf:"bytes,6,opt,name=timestamp,proto3,stdtime" json:"timestamp"` - Signature []byte `protobuf:"bytes,7,opt,name=signature,proto3" json:"signature,omitempty"` + Type SignedMsgType `protobuf:"varint,1,opt,name=type,proto3,enum=tendermint.types.SignedMsgType" json:"type,omitempty"` + Height int64 `protobuf:"varint,2,opt,name=height,proto3" json:"height,omitempty"` + Round int32 `protobuf:"varint,3,opt,name=round,proto3" json:"round,omitempty"` + PolRound int32 `protobuf:"varint,4,opt,name=pol_round,json=polRound,proto3" json:"pol_round,omitempty"` + BlockID BlockID `protobuf:"bytes,5,opt,name=block_id,json=blockId,proto3" json:"block_id"` + Timestamp time.Time `protobuf:"bytes,6,opt,name=timestamp,proto3,stdtime" json:"timestamp"` + Signature []byte `protobuf:"bytes,7,opt,name=signature,proto3" json:"signature,omitempty"` + CompactHash []byte `protobuf:"bytes,8,opt,name=compact_hash,json=compactHash,proto3" json:"compact_hash,omitempty"` } func (m *Proposal) Reset() { *m = Proposal{} } @@ -888,6 +889,13 @@ func (m *Proposal) GetSignature() []byte { return nil } +func (m *Proposal) GetCompactHash() []byte { + if m != nil { + return m.CompactHash + } + return nil +} + type SignedHeader struct { Header *Header `protobuf:"bytes,1,opt,name=header,proto3" json:"header,omitempty"` Commit *Commit `protobuf:"bytes,2,opt,name=commit,proto3" json:"commit,omitempty"` @@ -1522,111 +1530,112 @@ func init() { func init() { proto.RegisterFile("tendermint/types/types.proto", fileDescriptor_d3a6e55e2345de56) } var fileDescriptor_d3a6e55e2345de56 = []byte{ - // 1661 bytes of a gzipped FileDescriptorProto + // 1676 bytes of a gzipped FileDescriptorProto 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xbc, 0x58, 0x4f, 0x6f, 0xe3, 0xc6, 0x15, 0x37, 0x25, 0x4a, 0xa2, 0x9e, 0x24, 0x9b, 0x26, 0x9c, 0x8d, 0x56, 0x9b, 0x95, 0x55, 0x15, 0x6d, 0x9d, 0x34, 0x90, 0xb7, 0x4e, 0xd1, 0xb4, 0x87, 0x1c, 0x2c, 0xdb, 0xd9, 0x68, 0xe3, 0x7f, - 0xa5, 0x94, 0x0d, 0x5a, 0x14, 0x20, 0x28, 0x73, 0x56, 0x62, 0x43, 0x71, 0x58, 0xce, 0xc8, 0xf6, - 0xe6, 0x13, 0x14, 0xbe, 0x34, 0xa7, 0xde, 0x7c, 0x4a, 0x0f, 0xbd, 0xf7, 0x0b, 0x14, 0x3d, 0xe5, - 0x98, 0x5b, 0x7b, 0x69, 0x5a, 0xec, 0x02, 0x45, 0x3e, 0x46, 0x31, 0x6f, 0x86, 0x14, 0x65, 0x49, - 0x6d, 0xb0, 0x08, 0x72, 0x31, 0x38, 0xef, 0xfd, 0xde, 0x9b, 0xf7, 0x77, 0xde, 0x93, 0xe1, 0x0d, - 0x4e, 0x42, 0x8f, 0xc4, 0x13, 0x3f, 0xe4, 0xbb, 0xfc, 0x79, 0x44, 0x98, 0xfc, 0xdb, 0x89, 0x62, - 0xca, 0xa9, 0x65, 0xce, 0xb8, 0x1d, 0xa4, 0x37, 0xb6, 0x46, 0x74, 0x44, 0x91, 0xb9, 0x2b, 0xbe, - 0x24, 0xae, 0xb1, 0x3d, 0xa2, 0x74, 0x14,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b7, 0xd4, 0xa2, 0x2c, 0xb1, - 0xfd, 0x1f, 0x1d, 0x8a, 0x2a, 0x18, 0xef, 0x41, 0x49, 0x85, 0x15, 0x2f, 0xac, 0xec, 0x3d, 0xcc, - 0x6a, 0x54, 0xac, 0xce, 0x01, 0x0d, 0x19, 0x09, 0xd9, 0x94, 0x29, 0x7d, 0x89, 0x8c, 0xf5, 0x43, - 0x30, 0x2e, 0xc6, 0xae, 0x1f, 0x3a, 0xbe, 0x87, 0x16, 0x95, 0xbb, 0x95, 0x17, 0x5f, 0x6d, 0x97, - 0x0e, 0x04, 0xad, 0x77, 0x68, 0x97, 0x90, 0xd9, 0xf3, 0xac, 0x7b, 0x50, 0x1c, 0x13, 0x7f, 0x34, - 0xe6, 0x18, 0x96, 0xbc, 0xad, 0x4e, 0xd6, 0xcf, 0x41, 0x17, 0x05, 0x51, 0xd7, 0xf1, 0xee, 0x46, - 0x47, 0x56, 0x4b, 0x27, 0xa9, 0x96, 0xce, 0x20, 0xa9, 0x96, 0xae, 0x21, 0x2e, 0xfe, 0xec, 0x5f, - 0xdb, 0x9a, 0x8d, 0x12, 0xd6, 0x01, 0xd4, 0x02, 0x97, 0x71, 0x67, 0x28, 0xc2, 0x26, 0xae, 0x2f, - 0xa0, 0x8a, 0xfb, 0x8b, 0x01, 0x51, 0x81, 0x55, 0xa6, 0x57, 0x84, 0x94, 0x24, 0x79, 0xd6, 0x0e, - 0x98, 0xa8, 0xe4, 0x82, 0x4e, 0x26, 0x3e, 0x77, 0x30, 0xee, 0x45, 0x8c, 0xfb, 0xba, 0xa0, 0x1f, - 0x20, 0xf9, 0x03, 0x91, 0x81, 0x07, 0x50, 0xf6, 0x5c, 0xee, 0x4a, 0x48, 0x09, 0x21, 0x86, 0x20, - 0x20, 0xf3, 0x47, 0xb0, 0x91, 0x56, 0x1d, 0x93, 0x10, 0x43, 0x6a, 0x99, 0x91, 0x11, 0xf8, 0x08, - 0xb6, 0x42, 0x72, 0xcd, 0x9d, 0xbb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0xda, 0x3f, 0x5e, + 0x36, 0xd7, 0x7e, 0xfd, 0xee, 0xd8, 0xe7, 0x93, 0xd9, 0xb0, 0x33, 0xa2, 0xd3, 0xdd, 0xec, 0xaf, + 0xf8, 0xf9, 0xa7, 0xfc, 0x6f, 0xc2, 0xed, 0x5f, 0xf8, 0xc3, 0x22, 0xd2, 0xdf, 0xf9, 0x6f, 0x00, + 0x00, 0x00, 0xff, 0xff, 0x49, 0x2d, 0x89, 0xfb, 0xa2, 0x10, 0x00, 0x00, } func (m *PartSetHeader) Marshal() (dAtA []byte, err error) { @@ -2168,6 +2177,13 @@ func (m *Proposal) MarshalToSizedBuffer(dAtA []byte) (int, error) { _ = i var l int _ = l + if len(m.CompactHash) > 0 { + i -= len(m.CompactHash) + copy(dAtA[i:], m.CompactHash) + i = encodeVarintTypes(dAtA, i, uint64(len(m.CompactHash))) + i-- + dAtA[i] = 0x42 + } if len(m.Signature) > 0 { i -= len(m.Signature) copy(dAtA[i:], m.Signature) @@ -2967,6 +2983,10 @@ func (m *Proposal) Size() (n int) { if l > 0 { n += 1 + l + sovTypes(uint64(l)) } + l = len(m.CompactHash) + if l > 0 { + n += 1 + l + sovTypes(uint64(l)) + } return n } @@ -5128,6 +5148,40 @@ func (m *Proposal) Unmarshal(dAtA []byte) error { m.Signature = []byte{} } iNdEx = postIndex + case 8: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field CompactHash", wireType) + } + var byteLen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTypes + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + byteLen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if byteLen < 0 { + return ErrInvalidLengthTypes + } + postIndex := iNdEx + byteLen + if postIndex < 0 { + return ErrInvalidLengthTypes + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.CompactHash = append(m.CompactHash[:0], dAtA[iNdEx:postIndex]...) + if m.CompactHash == nil { + m.CompactHash = []byte{} + } + iNdEx = postIndex default: iNdEx = preIndex skippy, err := skipTypes(dAtA[iNdEx:]) diff --git a/proto/tendermint/types/types.proto b/proto/tendermint/types/types.proto index d3ccf4a28f..9ccacdb80c 100644 --- a/proto/tendermint/types/types.proto +++ b/proto/tendermint/types/types.proto @@ -155,6 +155,7 @@ message Proposal { google.protobuf.Timestamp timestamp = 6 [(gogoproto.nullable) = false, (gogoproto.stdtime) = true]; bytes signature = 7; + bytes compact_hash = 8; } message SignedHeader { diff --git a/types/proposal.go b/types/proposal.go index 3401f6f055..44af822d66 100644 --- a/types/proposal.go +++ b/types/proposal.go @@ -23,25 +23,27 @@ var ( // a so-called Proof-of-Lock (POL) round, as noted in the POLRound. // If POLRound >= 0, then BlockID corresponds to the block that is locked in POLRound. type Proposal struct { - Type cmtproto.SignedMsgType - Height int64 `json:"height"` - Round int32 `json:"round"` // there can not be greater than 2_147_483_647 rounds - POLRound int32 `json:"pol_round"` // -1 if null. - BlockID BlockID `json:"block_id"` - Timestamp time.Time `json:"timestamp"` - Signature []byte `json:"signature"` + Type cmtproto.SignedMsgType + Height int64 `json:"height"` + Round int32 `json:"round"` // there can not be greater than 2_147_483_647 rounds + POLRound int32 `json:"pol_round"` // -1 if null. + BlockID BlockID `json:"block_id"` + Timestamp time.Time `json:"timestamp"` + Signature []byte `json:"signature"` + CompactHash []byte `json:"compact_hash"` } // NewProposal returns a new Proposal. // If there is no POLRound, polRound should be -1. -func NewProposal(height int64, round int32, polRound int32, blockID BlockID) *Proposal { +func NewProposal(height int64, round int32, polRound int32, blockID BlockID, compactHash []byte) *Proposal { return &Proposal{ - Type: cmtproto.ProposalType, - Height: height, - Round: round, - BlockID: blockID, - POLRound: polRound, - Timestamp: cmttime.Now(), + Type: cmtproto.ProposalType, + Height: height, + Round: round, + BlockID: blockID, + POLRound: polRound, + Timestamp: cmttime.Now(), + CompactHash: compactHash, } } @@ -76,6 +78,10 @@ func (p *Proposal) ValidateBasic() error { if len(p.Signature) > MaxSignatureSize { return fmt.Errorf("signature is too big (max: %d)", MaxSignatureSize) } + + if err := ValidateHash(p.CompactHash); err != nil { + return fmt.Errorf("wrong CompactHash: %v", err) + } return nil } @@ -114,6 +120,11 @@ func ProposalSignBytes(chainID string, p *cmtproto.Proposal) []byte { panic(err) } + // simply append the compact hash to the end of the bytes + if p.CompactHash != nil { + bz = append(bz, p.CompactHash...) + } + return bz } @@ -131,6 +142,7 @@ func (p *Proposal) ToProto() *cmtproto.Proposal { pb.PolRound = p.POLRound pb.Timestamp = p.Timestamp pb.Signature = p.Signature + pb.CompactHash = p.CompactHash return pb } @@ -156,6 +168,7 @@ func ProposalFromProto(pp *cmtproto.Proposal) (*Proposal, error) { p.POLRound = pp.PolRound p.Timestamp = pp.Timestamp p.Signature = pp.Signature + p.CompactHash = pp.CompactHash return p, p.ValidateBasic() } diff --git a/types/proposal_test.go b/types/proposal_test.go index 505cec7862..26a647a6aa 100644 --- a/types/proposal_test.go +++ b/types/proposal_test.go @@ -61,7 +61,8 @@ func TestProposalVerifySignature(t *testing.T) { prop := NewProposal( 4, 2, 2, - BlockID{cmtrand.Bytes(tmhash.Size), PartSetHeader{777, cmtrand.Bytes(tmhash.Size)}}) + BlockID{cmtrand.Bytes(tmhash.Size), PartSetHeader{777, cmtrand.Bytes(tmhash.Size)}}, + tmhash.Sum([]byte("compacthash"))) p := prop.ToProto() signBytes := ProposalSignBytes("test_chain_id", p) @@ -152,7 +153,7 @@ func TestProposalValidateBasic(t *testing.T) { t.Run(tc.testName, func(t *testing.T) { prop := NewProposal( 4, 2, 2, - blockID) + blockID, tmhash.Sum([]byte("compacthash"))) p := prop.ToProto() err := privVal.SignProposal("test_chain_id", p) prop.Signature = p.Signature @@ -164,9 +165,9 @@ func TestProposalValidateBasic(t *testing.T) { } func TestProposalProtoBuf(t *testing.T) { - proposal := NewProposal(1, 2, 3, makeBlockID([]byte("hash"), 2, []byte("part_set_hash"))) + proposal := NewProposal(1, 2, 3, makeBlockID([]byte("hash"), 2, []byte("part_set_hash")), tmhash.Sum([]byte("compacthash"))) proposal.Signature = []byte("sig") - proposal2 := NewProposal(1, 2, 3, BlockID{}) + proposal2 := NewProposal(1, 2, 3, BlockID{}, tmhash.Sum([]byte("compacthash"))) testCases := []struct { msg string From 955714f301189d65c332038a4889a75268ca80d7 Mon Sep 17 00:00:00 2001 From: Callum Waters <cmwaters19@gmail.com> Date: Tue, 27 Aug 2024 18:37:24 +0200 Subject: [PATCH 052/127] chore: remove maverick directory --- test/e2e/generator/generate.go | 31 - test/e2e/node/config.go | 1 - test/e2e/node/main.go | 50 +- test/e2e/pkg/manifest.go | 10 - test/e2e/pkg/testnet.go | 47 - test/e2e/runner/main.go | 8 - test/e2e/runner/setup.go | 7 - test/e2e/tests/evidence_test.go | 57 - test/maverick/README.md | 51 - test/maverick/consensus/misbehavior.go | 412 ----- test/maverick/consensus/msgs.go | 115 -- test/maverick/consensus/reactor.go | 1484 ---------------- test/maverick/consensus/replay.go | 535 ------ test/maverick/consensus/replay_file.go | 341 ---- test/maverick/consensus/replay_stubs.go | 95 - test/maverick/consensus/state.go | 1996 ---------------------- test/maverick/consensus/ticker.go | 134 -- test/maverick/consensus/wal.go | 407 ----- test/maverick/consensus/wal_generator.go | 232 --- test/maverick/main.go | 242 --- test/maverick/node/node.go | 1523 ----------------- test/maverick/node/privval.go | 356 ---- 22 files changed, 3 insertions(+), 8131 deletions(-) delete mode 100644 test/e2e/tests/evidence_test.go delete mode 100644 test/maverick/README.md delete mode 100644 test/maverick/consensus/misbehavior.go delete mode 100644 test/maverick/consensus/msgs.go delete mode 100644 test/maverick/consensus/reactor.go delete mode 100644 test/maverick/consensus/replay.go delete mode 100644 test/maverick/consensus/replay_file.go delete mode 100644 test/maverick/consensus/replay_stubs.go delete mode 100644 test/maverick/consensus/state.go delete mode 100644 test/maverick/consensus/ticker.go delete mode 100644 test/maverick/consensus/wal.go delete mode 100644 test/maverick/consensus/wal_generator.go delete mode 100644 test/maverick/main.go delete mode 100644 test/maverick/node/node.go delete mode 100644 test/maverick/node/privval.go diff --git a/test/e2e/generator/generate.go b/test/e2e/generator/generate.go index 2b4817974f..74a83776d8 100644 --- a/test/e2e/generator/generate.go +++ b/test/e2e/generator/generate.go @@ -52,13 +52,6 @@ var ( "restart": 0.1, "upgrade": 0.3, } - nodeMisbehaviors = weightedChoice{ - // FIXME: evidence disabled due to node panicking when not - // having sufficient block history to process evidence. - // https://github.com/tendermint/tendermint/issues/5617 - // misbehaviorOption{"double-prevote"}: 1, - misbehaviorOption{}: 9, - } lightNodePerturbations = probSetChoice{ "upgrade": 0.3, } @@ -293,17 +286,6 @@ func generateNode( node.SnapshotInterval = 3 } - if node.Mode == string(e2e.ModeValidator) { - misbehaveAt := startAt + 5 + int64(r.Intn(10)) - if startAt == 0 { - misbehaveAt += initialHeight - 1 - } - node.Misbehaviors = nodeMisbehaviors.Choose(r).(misbehaviorOption).atHeight(misbehaveAt) - if len(node.Misbehaviors) != 0 { - node.PrivvalProtocol = "file" - } - } - // If a node which does not persist state also does not retain blocks, randomly // choose to either persist state or retain all blocks. if node.PersistInterval != nil && *node.PersistInterval == 0 && node.RetainBlocks > 0 { @@ -344,19 +326,6 @@ func ptrUint64(i uint64) *uint64 { return &i } -type misbehaviorOption struct { - misbehavior string -} - -func (m misbehaviorOption) atHeight(height int64) map[string]string { - misbehaviorMap := make(map[string]string) - if m.misbehavior == "" { - return misbehaviorMap - } - misbehaviorMap[strconv.Itoa(int(height))] = m.misbehavior - return misbehaviorMap -} - // Parses strings like "v0.34.21:1,v0.34.22:2" to represent two versions // ("v0.34.21" and "v0.34.22") with weights of 1 and 2 respectively. // Versions may be specified as cometbft/e2e-node:v0.34.27-alpha.1:1 or diff --git a/test/e2e/node/config.go b/test/e2e/node/config.go index 6941d0a1c9..954d382e4a 100644 --- a/test/e2e/node/config.go +++ b/test/e2e/node/config.go @@ -23,7 +23,6 @@ type Config struct { PrivValServer string `toml:"privval_server"` PrivValKey string `toml:"privval_key"` PrivValState string `toml:"privval_state"` - Misbehaviors map[string]string `toml:"misbehaviors"` KeyType string `toml:"key_type"` } diff --git a/test/e2e/node/main.go b/test/e2e/node/main.go index 8a02ed7338..3e3fce4d65 100644 --- a/test/e2e/node/main.go +++ b/test/e2e/node/main.go @@ -7,7 +7,6 @@ import ( "net/http" "os" "path/filepath" - "strconv" "strings" "time" @@ -30,8 +29,6 @@ import ( rpcserver "github.com/tendermint/tendermint/rpc/jsonrpc/server" "github.com/tendermint/tendermint/test/e2e/app" e2e "github.com/tendermint/tendermint/test/e2e/pkg" - mcs "github.com/tendermint/tendermint/test/maverick/consensus" - maverick "github.com/tendermint/tendermint/test/maverick/node" ) var logger = log.NewTMLogger(log.NewSyncWriter(os.Stdout)) @@ -75,14 +72,10 @@ func run(configFile string) error { case "socket", "grpc": err = startApp(cfg) case "builtin": - if len(cfg.Misbehaviors) == 0 { - if cfg.Mode == string(e2e.ModeLight) { - err = startLightClient(cfg) - } else { - err = startNode(cfg) - } + if cfg.Mode == string(e2e.ModeLight) { + err = startLightClient(cfg) } else { - err = startMaverick(cfg) + err = startNode(cfg) } default: err = fmt.Errorf("invalid protocol %q", cfg.Protocol) @@ -204,43 +197,6 @@ func startLightClient(cfg *Config) error { return nil } -// FIXME: Temporarily disconnected maverick until it is redesigned -// startMaverick starts a Maverick node that runs the application directly. It assumes the CometBFT -// configuration is in $CMTHOME/config/cometbft.toml. -func startMaverick(cfg *Config) error { - app, err := app.NewApplication(cfg.App()) - if err != nil { - return err - } - - cmtcfg, logger, nodeKey, err := setupNode() - if err != nil { - return fmt.Errorf("failed to setup config: %w", err) - } - - misbehaviors := make(map[int64]mcs.Misbehavior, len(cfg.Misbehaviors)) - for heightString, misbehaviorString := range cfg.Misbehaviors { - height, _ := strconv.ParseInt(heightString, 10, 64) - misbehaviors[height] = mcs.MisbehaviorList[misbehaviorString] - } - - n, err := maverick.NewNode(cmtcfg, - maverick.LoadOrGenFilePV(cmtcfg.PrivValidatorKeyFile(), cmtcfg.PrivValidatorStateFile()), - nodeKey, - proxy.NewLocalClientCreator(app), - maverick.DefaultGenesisDocProviderFunc(cmtcfg), - maverick.DefaultDBProvider, - maverick.DefaultMetricsProvider(cmtcfg.Instrumentation), - logger, - misbehaviors, - ) - if err != nil { - return err - } - - return n.Start() -} - // startSigner starts a signer server connecting to the given endpoint. func startSigner(cfg *Config) error { filePV := privval.LoadFilePV(cfg.PrivValKey, cfg.PrivValState) diff --git a/test/e2e/pkg/manifest.go b/test/e2e/pkg/manifest.go index b6ea5d41e2..c50b108bbd 100644 --- a/test/e2e/pkg/manifest.go +++ b/test/e2e/pkg/manifest.go @@ -151,16 +151,6 @@ type ManifestNode struct { // restart: restarts the node, shutting it down with SIGTERM Perturb []string `toml:"perturb"` - // Misbehaviors sets how a validator behaves during consensus at a - // certain height. Multiple misbehaviors at different heights can be used - // - // An example of misbehaviors - // { 10 = "double-prevote", 20 = "double-prevote"} - // - // For more information, look at the readme in the maverick folder. - // A list of all behaviors can be found in ../maverick/consensus/behavior.go - Misbehaviors map[string]string `toml:"misbehaviors"` - // SendNoLoad determines if the e2e test should send load to this node. // It defaults to false so unless the configured, the node will // receive load. diff --git a/test/e2e/pkg/testnet.go b/test/e2e/pkg/testnet.go index 6819f88c17..eb279e3018 100644 --- a/test/e2e/pkg/testnet.go +++ b/test/e2e/pkg/testnet.go @@ -16,7 +16,6 @@ import ( "github.com/tendermint/tendermint/crypto/ed25519" "github.com/tendermint/tendermint/crypto/secp256k1" rpchttp "github.com/tendermint/tendermint/rpc/client/http" - mcs "github.com/tendermint/tendermint/test/maverick/consensus" ) const ( @@ -102,7 +101,6 @@ type Node struct { Seeds []*Node PersistentPeers []*Node Perturbations []Perturbation - Misbehaviors map[int64]string SendNoLoad bool Prometheus bool PrometheusProxyPort uint32 @@ -207,7 +205,6 @@ func LoadTestnet(manifest Manifest, fname string, ifd InfrastructureData) (*Test SnapshotInterval: nodeManifest.SnapshotInterval, RetainBlocks: nodeManifest.RetainBlocks, Perturbations: []Perturbation{}, - Misbehaviors: make(map[int64]string), SendNoLoad: nodeManifest.SendNoLoad, TracePushConfig: ifd.TracePushConfig, TracePullAddress: ifd.TracePullAddress, @@ -240,13 +237,6 @@ func LoadTestnet(manifest Manifest, fname string, ifd InfrastructureData) (*Test for _, p := range nodeManifest.Perturb { node.Perturbations = append(node.Perturbations, Perturbation(p)) } - for heightString, misbehavior := range nodeManifest.Misbehaviors { - height, err := strconv.ParseInt(heightString, 10, 64) - if err != nil { - return nil, fmt.Errorf("unable to parse height %s to int64: %w", heightString, err) - } - node.Misbehaviors[height] = misbehavior - } testnet.Nodes = append(testnet.Nodes, node) } @@ -436,30 +426,6 @@ func (n Node) Validate(testnet Testnet) error { } } - if (n.PrivvalProtocol != "file" || n.Mode != "validator") && len(n.Misbehaviors) != 0 { - return errors.New("must be using \"file\" privval protocol to implement misbehaviors") - } - - for height, misbehavior := range n.Misbehaviors { - if height < n.StartAt { - return fmt.Errorf("misbehavior height %d is below node start height %d", - height, n.StartAt) - } - if height < testnet.InitialHeight { - return fmt.Errorf("misbehavior height %d is below network initial height %d", - height, testnet.InitialHeight) - } - exists := false - for possibleBehaviors := range mcs.MisbehaviorList { - if possibleBehaviors == misbehavior { - exists = true - } - } - if !exists { - return fmt.Errorf("misbehavior %s does not exist", misbehavior) - } - } - return nil } @@ -512,19 +478,6 @@ func (t Testnet) HasPerturbations() bool { return false } -// LastMisbehaviorHeight returns the height of the last misbehavior. -func (t Testnet) LastMisbehaviorHeight() int64 { - lastHeight := int64(0) - for _, node := range t.Nodes { - for height := range node.Misbehaviors { - if height > lastHeight { - lastHeight = height - } - } - } - return lastHeight -} - // Address returns a P2P endpoint address for the node. func (n Node) AddressP2P(withID bool) string { ip := n.IP.String() diff --git a/test/e2e/runner/main.go b/test/e2e/runner/main.go index 30d0d3232f..b3e1996fb1 100644 --- a/test/e2e/runner/main.go +++ b/test/e2e/runner/main.go @@ -139,14 +139,6 @@ func NewCLI() *CLI { return err } - if lastMisbehavior := cli.testnet.LastMisbehaviorHeight(); lastMisbehavior > 0 { - // wait for misbehaviors before starting perturbations. We do a separate - // wait for another 5 blocks, since the last misbehavior height may be - // in the past depending on network startup ordering. - if err := WaitUntil(cli.testnet, lastMisbehavior); err != nil { - return err - } - } if err := Wait(cli.testnet, 5); err != nil { // allow some txs to go through return err } diff --git a/test/e2e/runner/setup.go b/test/e2e/runner/setup.go index f2cec6b227..15f0f6c696 100644 --- a/test/e2e/runner/setup.go +++ b/test/e2e/runner/setup.go @@ -10,7 +10,6 @@ import ( "path/filepath" "regexp" "sort" - "strconv" "strings" "time" @@ -312,12 +311,6 @@ func MakeAppConfig(node *e2e.Node) ([]byte, error) { } } - misbehaviors := make(map[string]string) - for height, misbehavior := range node.Misbehaviors { - misbehaviors[strconv.Itoa(int(height))] = misbehavior - } - cfg["misbehaviors"] = misbehaviors - if len(node.Testnet.ValidatorUpdates) > 0 { validatorUpdates := map[string]map[string]int64{} for height, validators := range node.Testnet.ValidatorUpdates { diff --git a/test/e2e/tests/evidence_test.go b/test/e2e/tests/evidence_test.go deleted file mode 100644 index ea24b51e5c..0000000000 --- a/test/e2e/tests/evidence_test.go +++ /dev/null @@ -1,57 +0,0 @@ -package e2e_test - -import ( - "bytes" - "testing" - - "github.com/stretchr/testify/require" - - e2e "github.com/tendermint/tendermint/test/e2e/pkg" - "github.com/tendermint/tendermint/types" -) - -// assert that all nodes that have blocks at the height of a misbehavior has evidence -// for that misbehavior -func TestEvidence_Misbehavior(t *testing.T) { - blocks := fetchBlockChain(t) - testNode(t, func(t *testing.T, node e2e.Node) { - seenEvidence := make(map[int64]struct{}) - for _, block := range blocks { - // Find any evidence blaming this node in this block - var nodeEvidence types.Evidence - for _, evidence := range block.Evidence.Evidence { - switch evidence := evidence.(type) { - case *types.DuplicateVoteEvidence: - if bytes.Equal(evidence.VoteA.ValidatorAddress, node.PrivvalKey.PubKey().Address()) { - nodeEvidence = evidence - } - default: - t.Fatalf("unexpected evidence type %T", evidence) - } - } - if nodeEvidence == nil { - continue // no evidence for the node at this height - } - - // Check that evidence was as expected - misbehavior, ok := node.Misbehaviors[nodeEvidence.Height()] - require.True(t, ok, "found unexpected evidence %v in height %v", - nodeEvidence, block.Height) - - switch misbehavior { - case "double-prevote": - require.IsType(t, &types.DuplicateVoteEvidence{}, nodeEvidence, "unexpected evidence type") - default: - t.Fatalf("unknown misbehavior %v", misbehavior) - } - - seenEvidence[nodeEvidence.Height()] = struct{}{} - } - // see if there is any evidence that we were expecting but didn't see - for height, misbehavior := range node.Misbehaviors { - _, ok := seenEvidence[height] - require.True(t, ok, "expected evidence for %v misbehavior at height %v by node but was never found", - misbehavior, height) - } - }) -} diff --git a/test/maverick/README.md b/test/maverick/README.md deleted file mode 100644 index d0b8fb799c..0000000000 --- a/test/maverick/README.md +++ /dev/null @@ -1,51 +0,0 @@ -# Maverick - - - -A byzantine node used to test CometBFT consensus against a plethora of different faulty misbehaviors. Designed to easily create new faulty misbehaviors to examine how a CometBFT network reacts to the misbehavior. Can also be used for fuzzy testing with different network arrangements. - -## Misbehaviors - -A misbehavior allows control at the following stages as highlighted by the struct below - -```go -type Misbehavior struct { - String string - - EnterPropose func(cs *State, height int64, round int32) - - EnterPrevote func(cs *State, height int64, round int32) - - EnterPrecommit func(cs *State, height int64, round int32) - - ReceivePrevote func(cs *State, prevote *types.Vote) - - ReceivePrecommit func(cs *State, precommit *types.Vote) - - ReceiveProposal func(cs *State, proposal *types.Proposal) error -} -``` - -At each of these events, the node can exhibit a different misbehavior. To create a new misbehavior define a function that builds off the existing default misbehavior and then overrides one or more of these functions. Then append it to the misbehaviors list so the node recognizes it like so: - -```go -var MisbehaviorList = map[string]Misbehavior{ - "double-prevote": DoublePrevoteMisbehavior(), -} -``` - -## Setup - -The maverick node takes most of the functionality from the existing CometBFT CLI. To install this, in the directory of this readme, run: - -```bash -go build -``` - -Use `maverick init` to initialize a single node and `maverick node` to run it. This will run it normally unless you use the misbehaviors flag as follows: - -```bash -maverick node --proxy_app persistent_kvstore --misbehaviors double-vote,10 -``` - -This would cause the node to vote twice in every round at height 10. To add more misbehaviors at different heights, append the next misbehavior and height after the first (with comma separation). diff --git a/test/maverick/consensus/misbehavior.go b/test/maverick/consensus/misbehavior.go deleted file mode 100644 index 86d492e70a..0000000000 --- a/test/maverick/consensus/misbehavior.go +++ /dev/null @@ -1,412 +0,0 @@ -package consensus - -import ( - "fmt" - - cmtcon "github.com/tendermint/tendermint/consensus" - cstypes "github.com/tendermint/tendermint/consensus/types" - "github.com/tendermint/tendermint/libs/log" - "github.com/tendermint/tendermint/p2p" - cmtcons "github.com/tendermint/tendermint/proto/tendermint/consensus" - cmtproto "github.com/tendermint/tendermint/proto/tendermint/types" - "github.com/tendermint/tendermint/types" -) - -// MisbehaviorList encompasses a list of all possible behaviors -var MisbehaviorList = map[string]Misbehavior{ - "double-prevote": DoublePrevoteMisbehavior(), -} - -type Misbehavior struct { - Name string - - EnterPropose func(cs *State, height int64, round int32) - - EnterPrevote func(cs *State, height int64, round int32) - - EnterPrecommit func(cs *State, height int64, round int32) - - ReceivePrevote func(cs *State, prevote *types.Vote) - - ReceivePrecommit func(cs *State, precommit *types.Vote) - - ReceiveProposal func(cs *State, proposal *types.Proposal) error -} - -// BEHAVIORS - -func DefaultMisbehavior() Misbehavior { - return Misbehavior{ - Name: "default", - EnterPropose: defaultEnterPropose, - EnterPrevote: defaultEnterPrevote, - EnterPrecommit: defaultEnterPrecommit, - ReceivePrevote: defaultReceivePrevote, - ReceivePrecommit: defaultReceivePrecommit, - ReceiveProposal: defaultReceiveProposal, - } -} - -// DoublePrevoteMisbehavior will make a node prevote both nil and a block in the same -// height and round. -func DoublePrevoteMisbehavior() Misbehavior { - b := DefaultMisbehavior() - b.Name = "double-prevote" - b.EnterPrevote = func(cs *State, height int64, round int32) { - - // If a block is locked, prevote that. - if cs.LockedBlock != nil { - cs.Logger.Debug("enterPrevote: already locked on a block, prevoting locked block") - cs.signAddVote(cmtproto.PrevoteType, cs.LockedBlock.Hash(), cs.LockedBlockParts.Header()) - return - } - - // If ProposalBlock is nil, prevote nil. - if cs.ProposalBlock == nil { - cs.Logger.Debug("enterPrevote: ProposalBlock is nil") - cs.signAddVote(cmtproto.PrevoteType, nil, types.PartSetHeader{}) - return - } - - // Validate proposal block - err := cs.blockExec.ValidateBlock(cs.state, cs.ProposalBlock) - if err != nil { - // ProposalBlock is invalid, prevote nil. - cs.Logger.Error("enterPrevote: ProposalBlock is invalid", "err", err) - cs.signAddVote(cmtproto.PrevoteType, nil, types.PartSetHeader{}) - return - } - - if cs.sw == nil { - cs.Logger.Error("nil switch") - return - } - - prevote, err := cs.signVote(cmtproto.PrevoteType, cs.ProposalBlock.Hash(), cs.ProposalBlockParts.Header()) - if err != nil { - cs.Logger.Error("enterPrevote: Unable to sign block", "err", err) - } - - nilPrevote, err := cs.signVote(cmtproto.PrevoteType, nil, types.PartSetHeader{}) - if err != nil { - cs.Logger.Error("enterPrevote: Unable to sign block", "err", err) - } - - // add our own vote - cs.sendInternalMessage(msgInfo{&cmtcon.VoteMessage{Vote: prevote}, ""}) - - cs.Logger.Info("Sending conflicting votes") - peers := cs.sw.Peers().List() - // there has to be at least two other peers connected else this behavior works normally - for idx, peer := range peers { - if idx%2 == 0 { // sign the proposal block - p2p.SendEnvelopeShim(peer, p2p.Envelope{ //nolint: staticcheck - ChannelID: VoteChannel, - Message: &cmtcons.Vote{ - Vote: prevote.ToProto(), - }, - }, cs.Logger) - } else { // sign a nil block - p2p.SendEnvelopeShim(peer, p2p.Envelope{ //nolint: staticcheck - ChannelID: VoteChannel, - Message: &cmtcons.Vote{ - Vote: nilPrevote.ToProto(), - }, - }, cs.Logger) - } - } - } - return b -} - -// DEFAULTS - -func defaultEnterPropose(cs *State, height int64, round int32) { - logger := cs.Logger.With("height", height, "round", round) - // If we don't get the proposal and all block parts quick enough, enterPrevote - cs.scheduleTimeout(cs.config.Propose(round), height, round, cstypes.RoundStepPropose) - - // Nothing more to do if we're not a validator - if cs.privValidator == nil { - logger.Debug("This node is not a validator") - return - } - logger.Debug("This node is a validator") - - pubKey, err := cs.privValidator.GetPubKey() - if err != nil { - // If this node is a validator & proposer in the currentx round, it will - // miss the opportunity to create a block. - logger.Error("Error on retrival of pubkey", "err", err) - return - } - address := pubKey.Address() - - // if not a validator, we're done - if !cs.Validators.HasAddress(address) { - logger.Debug("This node is not a validator", "addr", address, "vals", cs.Validators) - return - } - - if cs.isProposer(address) { - logger.Debug("enterPropose: our turn to propose", - "proposer", address, - ) - cs.decideProposal(height, round) - } else { - logger.Debug("enterPropose: not our turn to propose", - "proposer", cs.Validators.GetProposer().Address, - ) - } -} - -func defaultEnterPrevote(cs *State, height int64, round int32) { - logger := cs.Logger.With("height", height, "round", round) - - // If a block is locked, prevote that. - if cs.LockedBlock != nil { - logger.Debug("enterPrevote: already locked on a block, prevoting locked block") - cs.signAddVote(cmtproto.PrevoteType, cs.LockedBlock.Hash(), cs.LockedBlockParts.Header()) - return - } - - // If ProposalBlock is nil, prevote nil. - if cs.ProposalBlock == nil { - logger.Debug("enterPrevote: ProposalBlock is nil") - cs.signAddVote(cmtproto.PrevoteType, nil, types.PartSetHeader{}) - return - } - - // Validate proposal block - err := cs.blockExec.ValidateBlock(cs.state, cs.ProposalBlock) - if err != nil { - // ProposalBlock is invalid, prevote nil. - logger.Error("enterPrevote: ProposalBlock is invalid", "err", err) - cs.signAddVote(cmtproto.PrevoteType, nil, types.PartSetHeader{}) - return - } - - // Prevote cs.ProposalBlock - // NOTE: the proposal signature is validated when it is received, - // and the proposal block parts are validated as they are received (against the merkle hash in the proposal) - logger.Debug("enterPrevote: ProposalBlock is valid") - cs.signAddVote(cmtproto.PrevoteType, cs.ProposalBlock.Hash(), cs.ProposalBlockParts.Header()) -} - -func defaultEnterPrecommit(cs *State, height int64, round int32) { - logger := cs.Logger.With("height", height, "round", round) - - // check for a polka - blockID, ok := cs.Votes.Prevotes(round).TwoThirdsMajority() - - // If we don't have a polka, we must precommit nil. - if !ok { - if cs.LockedBlock != nil { - logger.Debug("enterPrecommit: no +2/3 prevotes during enterPrecommit while we're locked; precommitting nil") - } else { - logger.Debug("enterPrecommit: no +2/3 prevotes during enterPrecommit; precommitting nil.") - } - cs.signAddVote(cmtproto.PrecommitType, nil, types.PartSetHeader{}) - return - } - - // At this point +2/3 prevoted for a particular block or nil. - _ = cs.eventBus.PublishEventPolka(cs.RoundStateEvent()) - - // the latest POLRound should be this round. - polRound, _ := cs.Votes.POLInfo() - if polRound < round { - panic(fmt.Sprintf("This POLRound should be %v but got %v", round, polRound)) - } - - // +2/3 prevoted nil. Unlock and precommit nil. - if len(blockID.Hash) == 0 { - if cs.LockedBlock == nil { - logger.Debug("enterPrecommit: +2/3 prevoted for nil") - } else { - logger.Debug("enterPrecommit: +2/3 prevoted for nil; unlocking") - cs.LockedRound = -1 - cs.LockedBlock = nil - cs.LockedBlockParts = nil - _ = cs.eventBus.PublishEventUnlock(cs.RoundStateEvent()) - } - cs.signAddVote(cmtproto.PrecommitType, nil, types.PartSetHeader{}) - return - } - - // At this point, +2/3 prevoted for a particular block. - - // If we're already locked on that block, precommit it, and update the LockedRound - if cs.LockedBlock.HashesTo(blockID.Hash) { - logger.Debug("enterPrecommit: +2/3 prevoted locked block; relocking") - cs.LockedRound = round - _ = cs.eventBus.PublishEventRelock(cs.RoundStateEvent()) - cs.signAddVote(cmtproto.PrecommitType, blockID.Hash, blockID.PartSetHeader) - return - } - - // If +2/3 prevoted for proposal block, stage and precommit it - if cs.ProposalBlock.HashesTo(blockID.Hash) { - logger.Debug("enterPrecommit: +2/3 prevoted proposal block; locking", "hash", blockID.Hash) - // Validate the block. - if err := cs.blockExec.ValidateBlock(cs.state, cs.ProposalBlock); err != nil { - panic(fmt.Sprintf("enterPrecommit: +2/3 prevoted for an invalid block: %v", err)) - } - cs.LockedRound = round - cs.LockedBlock = cs.ProposalBlock - cs.LockedBlockParts = cs.ProposalBlockParts - _ = cs.eventBus.PublishEventLock(cs.RoundStateEvent()) - cs.signAddVote(cmtproto.PrecommitType, blockID.Hash, blockID.PartSetHeader) - return - } - - // There was a polka in this round for a block we don't have. - // Fetch that block, unlock, and precommit nil. - // The +2/3 prevotes for this round is the POL for our unlock. - logger.Debug("enterPrecommit: +2/3 prevotes for a block we don't have; voting nil", "blockID", blockID) - cs.LockedRound = -1 - cs.LockedBlock = nil - cs.LockedBlockParts = nil - if !cs.ProposalBlockParts.HasHeader(blockID.PartSetHeader) { - cs.ProposalBlock = nil - cs.ProposalBlockParts = types.NewPartSetFromHeader(blockID.PartSetHeader) - } - _ = cs.eventBus.PublishEventUnlock(cs.RoundStateEvent()) - cs.signAddVote(cmtproto.PrecommitType, nil, types.PartSetHeader{}) -} - -func defaultReceivePrevote(cs *State, vote *types.Vote) { - height := cs.Height - prevotes := cs.Votes.Prevotes(vote.Round) - - // If +2/3 prevotes for a block or nil for *any* round: - if blockID, ok := prevotes.TwoThirdsMajority(); ok { - - // There was a polka! - // If we're locked but this is a recent polka, unlock. - // If it matches our ProposalBlock, update the ValidBlock - - // Unlock if `cs.LockedRound < vote.Round <= cs.Round` - // NOTE: If vote.Round > cs.Round, we'll deal with it when we get to vote.Round - if (cs.LockedBlock != nil) && - (cs.LockedRound < vote.Round) && - (vote.Round <= cs.Round) && - !cs.LockedBlock.HashesTo(blockID.Hash) { - - cs.Logger.Info("Unlocking because of POL.", "lockedRound", cs.LockedRound, "POLRound", vote.Round) - cs.LockedRound = -1 - cs.LockedBlock = nil - cs.LockedBlockParts = nil - _ = cs.eventBus.PublishEventUnlock(cs.RoundStateEvent()) - } - - // Update Valid* if we can. - // NOTE: our proposal block may be nil or not what received a polka.. - if len(blockID.Hash) != 0 && (cs.TwoThirdPrevoteRound < vote.Round) && (vote.Round == cs.Round) { - - if cs.ProposalBlock.HashesTo(blockID.Hash) { - cs.Logger.Info( - "Updating ValidBlock because of POL.", "validRound", cs.TwoThirdPrevoteRound, "POLRound", vote.Round) - cs.TwoThirdPrevoteRound = vote.Round - cs.TwoThirdPrevoteBlock = cs.ProposalBlock - cs.TwoThirdPrevoteBlockParts = cs.ProposalBlockParts - } else { - cs.Logger.Info( - "valid block we do not know about; set ProposalBlock=nil", - "proposal", log.NewLazyBlockHash(cs.ProposalBlock), - "blockID", blockID.Hash, - ) - - // We're getting the wrong block. - cs.ProposalBlock = nil - } - if !cs.ProposalBlockParts.HasHeader(blockID.PartSetHeader) { - cs.ProposalBlockParts = types.NewPartSetFromHeader(blockID.PartSetHeader) - } - cs.evsw.FireEvent(types.EventValidBlock, &cs.RoundState) - _ = cs.eventBus.PublishEventValidBlock(cs.RoundStateEvent()) - } - } - - // If +2/3 prevotes for *anything* for future round: - switch { - case cs.Round < vote.Round && prevotes.HasTwoThirdsAny(): - // Round-skip if there is any 2/3+ of votes ahead of us - cs.enterNewRound(height, vote.Round) - case cs.Round == vote.Round && cstypes.RoundStepPrevote <= cs.Step: // current round - blockID, ok := prevotes.TwoThirdsMajority() - if ok && (cs.isProposalComplete() || len(blockID.Hash) == 0) { - cs.enterPrecommit(height, vote.Round) - } else if prevotes.HasTwoThirdsAny() { - cs.enterPrevoteWait(height, vote.Round) - } - case cs.Proposal != nil && 0 <= cs.Proposal.POLRound && cs.Proposal.POLRound == vote.Round: - // If the proposal is now complete, enter prevote of cs.Round. - if cs.isProposalComplete() { - cs.enterPrevote(height, cs.Round) - } - } - -} - -func defaultReceivePrecommit(cs *State, vote *types.Vote) { - height := cs.Height - precommits := cs.Votes.Precommits(vote.Round) - cs.Logger.Info("Added to precommit", "vote", vote, "precommits", precommits.StringShort()) - - blockID, ok := precommits.TwoThirdsMajority() - if ok { - // Executed as TwoThirdsMajority could be from a higher round - cs.enterNewRound(height, vote.Round) - cs.enterPrecommit(height, vote.Round) - if len(blockID.Hash) != 0 { - cs.enterCommit(height, vote.Round) - if cs.config.SkipTimeoutCommit && precommits.HasAll() { - cs.enterNewRound(cs.Height, 0) - } - } else { - cs.enterPrecommitWait(height, vote.Round) - } - } else if cs.Round <= vote.Round && precommits.HasTwoThirdsAny() { - cs.enterNewRound(height, vote.Round) - cs.enterPrecommitWait(height, vote.Round) - } -} - -func defaultReceiveProposal(cs *State, proposal *types.Proposal) error { - // Already have one - // TODO: possibly catch double proposals - if cs.Proposal != nil { - return nil - } - - // Does not apply - if proposal.Height != cs.Height || proposal.Round != cs.Round { - return nil - } - - // Verify POLRound, which must be -1 or in range [0, proposal.Round). - if proposal.POLRound < -1 || - (proposal.POLRound >= 0 && proposal.POLRound >= proposal.Round) { - return ErrInvalidProposalPOLRound - } - - p := proposal.ToProto() - // Verify signature - if !cs.Validators.GetProposer().PubKey.VerifySignature( - types.ProposalSignBytes(cs.state.ChainID, p), proposal.Signature) { - return ErrInvalidProposalSignature - } - - proposal.Signature = p.Signature - cs.Proposal = proposal - // We don't update cs.ProposalBlockParts if it is already set. - // This happens if we're already in cstypes.RoundStepCommit or if there is a valid block in the current round. - // TODO: We can check if Proposal is for a different block as this is a sign of misbehavior! - if cs.ProposalBlockParts == nil { - cs.ProposalBlockParts = types.NewPartSetFromHeader(proposal.BlockID.PartSetHeader) - } - - cs.Logger.Info("received proposal", "proposal", proposal) - return nil -} diff --git a/test/maverick/consensus/msgs.go b/test/maverick/consensus/msgs.go deleted file mode 100644 index d0e6e2b3a1..0000000000 --- a/test/maverick/consensus/msgs.go +++ /dev/null @@ -1,115 +0,0 @@ -package consensus - -import ( - "errors" - "fmt" - - cmtcon "github.com/tendermint/tendermint/consensus" - cstypes "github.com/tendermint/tendermint/consensus/types" - cmtmath "github.com/tendermint/tendermint/libs/math" - "github.com/tendermint/tendermint/p2p" - cmtcons "github.com/tendermint/tendermint/proto/tendermint/consensus" - cmtproto "github.com/tendermint/tendermint/proto/tendermint/types" - "github.com/tendermint/tendermint/types" -) - -func WALToProto(msg cmtcon.WALMessage) (*cmtcons.WALMessage, error) { - var pb cmtcons.WALMessage - - switch msg := msg.(type) { - case types.EventDataRoundState: - pb = cmtcons.WALMessage{ - Sum: &cmtcons.WALMessage_EventDataRoundState{ - EventDataRoundState: &cmtproto.EventDataRoundState{ - Height: msg.Height, - Round: msg.Round, - Step: msg.Step, - }, - }, - } - case msgInfo: - consMsg, err := cmtcon.MsgToProto(msg.Msg) - if err != nil { - return nil, err - } - pb = cmtcons.WALMessage{ - Sum: &cmtcons.WALMessage_MsgInfo{ - MsgInfo: &cmtcons.MsgInfo{ - Msg: *consMsg, - PeerID: string(msg.PeerID), - }, - }, - } - case timeoutInfo: - pb = cmtcons.WALMessage{ - Sum: &cmtcons.WALMessage_TimeoutInfo{ - TimeoutInfo: &cmtcons.TimeoutInfo{ - Duration: msg.Duration, - Height: msg.Height, - Round: msg.Round, - Step: uint32(msg.Step), - }, - }, - } - case cmtcon.EndHeightMessage: - pb = cmtcons.WALMessage{ - Sum: &cmtcons.WALMessage_EndHeight{ - EndHeight: &cmtcons.EndHeight{ - Height: msg.Height, - }, - }, - } - default: - return nil, fmt.Errorf("to proto: wal message not recognized: %T", msg) - } - - return &pb, nil -} - -// WALFromProto takes a proto wal message and return a consensus walMessage and error -func WALFromProto(msg *cmtcons.WALMessage) (cmtcon.WALMessage, error) { - if msg == nil { - return nil, errors.New("nil WAL message") - } - var pb cmtcon.WALMessage - - switch msg := msg.Sum.(type) { - case *cmtcons.WALMessage_EventDataRoundState: - pb = types.EventDataRoundState{ - Height: msg.EventDataRoundState.Height, - Round: msg.EventDataRoundState.Round, - Step: msg.EventDataRoundState.Step, - } - case *cmtcons.WALMessage_MsgInfo: - walMsg, err := cmtcon.MsgFromProto(&msg.MsgInfo.Msg) - if err != nil { - return nil, fmt.Errorf("msgInfo from proto error: %w", err) - } - pb = msgInfo{ - Msg: walMsg, - PeerID: p2p.ID(msg.MsgInfo.PeerID), - } - - case *cmtcons.WALMessage_TimeoutInfo: - tis, err := cmtmath.SafeConvertUint8(int64(msg.TimeoutInfo.Step)) - // deny message based on possible overflow - if err != nil { - return nil, fmt.Errorf("denying message due to possible overflow: %w", err) - } - pb = timeoutInfo{ - Duration: msg.TimeoutInfo.Duration, - Height: msg.TimeoutInfo.Height, - Round: msg.TimeoutInfo.Round, - Step: cstypes.RoundStepType(tis), - } - return pb, nil - case *cmtcons.WALMessage_EndHeight: - pb := cmtcon.EndHeightMessage{ - Height: msg.EndHeight.Height, - } - return pb, nil - default: - return nil, fmt.Errorf("from proto: wal message not recognized: %T", msg) - } - return pb, nil -} diff --git a/test/maverick/consensus/reactor.go b/test/maverick/consensus/reactor.go deleted file mode 100644 index e59ecaa79b..0000000000 --- a/test/maverick/consensus/reactor.go +++ /dev/null @@ -1,1484 +0,0 @@ -package consensus - -import ( - "errors" - "fmt" - "reflect" - "sync" - "time" - - "github.com/gogo/protobuf/proto" - - cmtcon "github.com/tendermint/tendermint/consensus" - cstypes "github.com/tendermint/tendermint/consensus/types" - "github.com/tendermint/tendermint/libs/bits" - cmtevents "github.com/tendermint/tendermint/libs/events" - cmtjson "github.com/tendermint/tendermint/libs/json" - "github.com/tendermint/tendermint/libs/log" - cmtsync "github.com/tendermint/tendermint/libs/sync" - "github.com/tendermint/tendermint/p2p" - cmtcons "github.com/tendermint/tendermint/proto/tendermint/consensus" - cmtproto "github.com/tendermint/tendermint/proto/tendermint/types" - sm "github.com/tendermint/tendermint/state" - "github.com/tendermint/tendermint/types" - cmttime "github.com/tendermint/tendermint/types/time" -) - -const ( - StateChannel = byte(0x20) - DataChannel = byte(0x21) - VoteChannel = byte(0x22) - VoteSetBitsChannel = byte(0x23) - - maxMsgSize = 1048576 // 1MB; NOTE/TODO: keep in sync with types.PartSet sizes. - - blocksToContributeToBecomeGoodPeer = 10000 - votesToContributeToBecomeGoodPeer = 10000 -) - -//----------------------------------------------------------------------------- - -// Reactor defines a reactor for the consensus service. -type Reactor struct { - p2p.BaseReactor // BaseService + p2p.Switch - - conS *State - - mtx cmtsync.RWMutex - waitSync bool - eventBus *types.EventBus - - Metrics *cmtcon.Metrics -} - -type ReactorOption func(*Reactor) - -// NewReactor returns a new Reactor with the given -// consensusState. -func NewReactor(consensusState *State, waitSync bool, options ...ReactorOption) *Reactor { - conR := &Reactor{ - conS: consensusState, - waitSync: waitSync, - Metrics: cmtcon.NopMetrics(), - } - conR.BaseReactor = *p2p.NewBaseReactor("Consensus", conR) - - for _, option := range options { - option(conR) - } - - return conR -} - -// OnStart implements BaseService by subscribing to events, which later will be -// broadcasted to other peers and starting state if we're not in fast sync. -func (conR *Reactor) OnStart() error { - conR.Logger.Info("Reactor ", "waitSync", conR.WaitSync()) - - // start routine that computes peer statistics for evaluating peer quality - go conR.peerStatsRoutine() - - conR.subscribeToBroadcastEvents() - - if !conR.WaitSync() { - conR.conS.SetSwitch(conR.Switch) - err := conR.conS.Start() - if err != nil { - return err - } - } - - return nil -} - -// OnStop implements BaseService by unsubscribing from events and stopping -// state. -func (conR *Reactor) OnStop() { - conR.unsubscribeFromBroadcastEvents() - if err := conR.conS.Stop(); err != nil { - conR.Logger.Error("Error stopping consensus state", "err", err) - } - if !conR.WaitSync() { - conR.conS.Wait() - } -} - -// SwitchToConsensus switches from fast_sync mode to consensus mode. -// It resets the state, turns off fast_sync, and starts the consensus state-machine -func (conR *Reactor) SwitchToConsensus(state sm.State, skipWAL bool) { - conR.Logger.Info("SwitchToConsensus") - - // We have no votes, so reconstruct LastCommit from SeenCommit. - if state.LastBlockHeight > 0 { - conR.conS.reconstructLastCommit(state) - } - - // NOTE: The line below causes broadcastNewRoundStepRoutine() to broadcast a - // NewRoundStepMessage. - conR.conS.updateToState(state) - - conR.mtx.Lock() - conR.waitSync = false - conR.mtx.Unlock() - conR.Metrics.FastSyncing.Set(0) - conR.Metrics.StateSyncing.Set(0) - - if skipWAL { - conR.conS.doWALCatchup = false - } - conR.conS.SetSwitch(conR.Switch) - err := conR.conS.Start() - if err != nil { - panic(fmt.Sprintf(`Failed to start consensus state: %v - -conS: -%+v - -conR: -%+v`, err, conR.conS, conR)) - } -} - -// GetChannels implements Reactor -func (conR *Reactor) GetChannels() []*p2p.ChannelDescriptor { - // TODO optimize - return []*p2p.ChannelDescriptor{ - { - ID: StateChannel, - Priority: 6, - SendQueueCapacity: 100, - RecvMessageCapacity: maxMsgSize, - MessageType: &cmtcons.Message{}, - }, - { - ID: DataChannel, // maybe split between gossiping current block and catchup stuff - // once we gossip the whole block there's nothing left to send until next height or round - Priority: 10, - SendQueueCapacity: 100, - RecvBufferCapacity: 50 * 4096, - RecvMessageCapacity: maxMsgSize, - MessageType: &cmtcons.Message{}, - }, - { - ID: VoteChannel, - Priority: 7, - SendQueueCapacity: 100, - RecvBufferCapacity: 100 * 100, - RecvMessageCapacity: maxMsgSize, - MessageType: &cmtcons.Message{}, - }, - { - ID: VoteSetBitsChannel, - Priority: 1, - SendQueueCapacity: 2, - RecvBufferCapacity: 1024, - RecvMessageCapacity: maxMsgSize, - MessageType: &cmtcons.Message{}, - }, - } -} - -// InitPeer implements Reactor by creating a state for the peer. -func (conR *Reactor) InitPeer(peer p2p.Peer) p2p.Peer { - peerState := NewPeerState(peer).SetLogger(conR.Logger) - peer.Set(types.PeerStateKey, peerState) - return peer -} - -// AddPeer implements Reactor by spawning multiple gossiping goroutines for the -// peer. -func (conR *Reactor) AddPeer(peer p2p.Peer) { - if !conR.IsRunning() { - return - } - - peerState, ok := peer.Get(types.PeerStateKey).(*PeerState) - if !ok { - panic(fmt.Sprintf("peer %v has no state", peer)) - } - // Begin routines for this peer. - go conR.gossipDataRoutine(peer, peerState) - go conR.gossipVotesRoutine(peer, peerState) - go conR.queryMaj23Routine(peer, peerState) - - // Send our state to peer. - // If we're fast_syncing, broadcast a RoundStepMessage later upon SwitchToConsensus(). - if !conR.WaitSync() { - conR.sendNewRoundStepMessage(peer) - } -} - -// RemovePeer is a noop. -func (conR *Reactor) RemovePeer(peer p2p.Peer, reason interface{}) { - if !conR.IsRunning() { - return - } - // TODO - // ps, ok := peer.Get(PeerStateKey).(*PeerState) - // if !ok { - // panic(fmt.Sprintf("Peer %v has no state", peer)) - // } - // ps.Disconnect() -} - -// Receive implements Reactor -// NOTE: We process these messages even when we're fast_syncing. -// Messages affect either a peer state or the consensus state. -// Peer state updates can happen in parallel, but processing of -// proposals, block parts, and votes are ordered by the receiveRoutine -// NOTE: blocks on consensus state for proposals, block parts, and votes -func (conR *Reactor) ReceiveEnvelope(e p2p.Envelope) { - if !conR.IsRunning() { - conR.Logger.Debug("Receive", "src", e.Src, "chId", e.ChannelID) - return - } - m := e.Message - if wm, ok := m.(p2p.Wrapper); ok { - m = wm.Wrap() - } - msg, err := cmtcon.MsgFromProto(m.(*cmtcons.Message)) - if err != nil { - conR.Logger.Error("Error decoding message", "src", e.Src, "chId", e.ChannelID, "err", err) - conR.Switch.StopPeerForError(e.Src, err) - return - } - - if err := msg.ValidateBasic(); err != nil { - conR.Logger.Error("Peer sent us invalid msg", "peer", e.Src, "msg", e.Message, "err", err) - conR.Switch.StopPeerForError(e.Src, err) - return - } - - conR.Logger.Debug("Receive", "src", e.Src, "chId", e.ChannelID, "msg", e.Message) - - // Get peer states - ps, ok := e.Src.Get(types.PeerStateKey).(*PeerState) - if !ok { - panic(fmt.Sprintf("Peer %v has no state", e.Src)) - } - - switch e.ChannelID { - case StateChannel: - switch msg := msg.(type) { - case *cmtcon.NewRoundStepMessage: - conR.conS.mtx.Lock() - initialHeight := conR.conS.state.InitialHeight - conR.conS.mtx.Unlock() - if err = msg.ValidateHeight(initialHeight); err != nil { - conR.Logger.Error("Peer sent us invalid msg", "peer", e.Src, "msg", msg, "err", err) - conR.Switch.StopPeerForError(e.Src, err) - return - } - ps.ApplyNewRoundStepMessage(msg) - case *cmtcon.NewValidBlockMessage: - ps.ApplyNewValidBlockMessage(msg) - case *cmtcon.HasVoteMessage: - ps.ApplyHasVoteMessage(msg) - case *cmtcon.VoteSetMaj23Message: - cs := conR.conS - cs.mtx.Lock() - height, votes := cs.Height, cs.Votes - cs.mtx.Unlock() - if height != msg.Height { - return - } - // Peer claims to have a maj23 for some BlockID at H,R,S, - err := votes.SetPeerMaj23(msg.Round, msg.Type, ps.peer.ID(), msg.BlockID) - if err != nil { - conR.Switch.StopPeerForError(e.Src, err) - return - } - // Respond with a VoteSetBitsMessage showing which votes we have. - // (and consequently shows which we don't have) - var ourVotes *bits.BitArray - switch msg.Type { - case cmtproto.PrevoteType: - ourVotes = votes.Prevotes(msg.Round).BitArrayByBlockID(msg.BlockID) - case cmtproto.PrecommitType: - ourVotes = votes.Precommits(msg.Round).BitArrayByBlockID(msg.BlockID) - default: - panic("Bad VoteSetBitsMessage field Type. Forgot to add a check in ValidateBasic?") - } - m := &cmtcons.VoteSetBits{ - Height: msg.Height, - Round: msg.Round, - Type: msg.Type, - BlockID: msg.BlockID.ToProto(), - } - v := ourVotes.ToProto() - if v != nil { - m.Votes = *v - } - - p2p.TrySendEnvelopeShim(e.Src, p2p.Envelope{ //nolint: staticcheck - ChannelID: VoteSetBitsChannel, - Message: m, - }, conR.Logger) - default: - conR.Logger.Error(fmt.Sprintf("Unknown message type %v", reflect.TypeOf(msg))) - } - - case DataChannel: - if conR.WaitSync() { - conR.Logger.Info("Ignoring message received during sync", "msg", msg) - return - } - switch msg := msg.(type) { - case *cmtcon.ProposalMessage: - ps.SetHasProposal(msg.Proposal) - conR.conS.peerMsgQueue <- msgInfo{msg, e.Src.ID()} - case *cmtcon.ProposalPOLMessage: - ps.ApplyProposalPOLMessage(msg) - case *cmtcon.BlockPartMessage: - ps.SetHasProposalBlockPart(msg.Height, msg.Round, int(msg.Part.Index)) - conR.Metrics.BlockParts.With("peer_id", string(e.Src.ID())).Add(1) - conR.conS.peerMsgQueue <- msgInfo{msg, e.Src.ID()} - default: - conR.Logger.Error(fmt.Sprintf("Unknown message type %v", reflect.TypeOf(msg))) - } - - case VoteChannel: - if conR.WaitSync() { - conR.Logger.Info("Ignoring message received during sync", "msg", msg) - return - } - switch msg := msg.(type) { - case *cmtcon.VoteMessage: - cs := conR.conS - cs.mtx.RLock() - height, valSize, lastCommitSize := cs.Height, cs.Validators.Size(), cs.LastCommit.Size() - cs.mtx.RUnlock() - ps.EnsureVoteBitArrays(height, valSize) - ps.EnsureVoteBitArrays(height-1, lastCommitSize) - ps.SetHasVote(msg.Vote) - - cs.peerMsgQueue <- msgInfo{msg, e.Src.ID()} - - default: - // don't punish (leave room for soft upgrades) - conR.Logger.Error(fmt.Sprintf("Unknown message type %v", reflect.TypeOf(msg))) - } - - case VoteSetBitsChannel: - if conR.WaitSync() { - conR.Logger.Info("Ignoring message received during sync", "msg", msg) - return - } - switch msg := msg.(type) { - case *cmtcon.VoteSetBitsMessage: - cs := conR.conS - cs.mtx.Lock() - height, votes := cs.Height, cs.Votes - cs.mtx.Unlock() - - if height == msg.Height { - var ourVotes *bits.BitArray - switch msg.Type { - case cmtproto.PrevoteType: - ourVotes = votes.Prevotes(msg.Round).BitArrayByBlockID(msg.BlockID) - case cmtproto.PrecommitType: - ourVotes = votes.Precommits(msg.Round).BitArrayByBlockID(msg.BlockID) - default: - panic("Bad VoteSetBitsMessage field Type. Forgot to add a check in ValidateBasic?") - } - ps.ApplyVoteSetBitsMessage(msg, ourVotes) - } else { - ps.ApplyVoteSetBitsMessage(msg, nil) - } - default: - // don't punish (leave room for soft upgrades) - conR.Logger.Error(fmt.Sprintf("Unknown message type %v", reflect.TypeOf(msg))) - } - - default: - conR.Logger.Error(fmt.Sprintf("Unknown chId %X", e.ChannelID)) - } -} - -func (conR *Reactor) Receive(chID byte, peer p2p.Peer, msgBytes []byte) { - msg := &cmtcons.Message{} - err := proto.Unmarshal(msgBytes, msg) - if err != nil { - panic(err) - } - um, err := msg.Unwrap() - if err != nil { - panic(err) - } - conR.ReceiveEnvelope(p2p.Envelope{ - ChannelID: chID, - Src: peer, - Message: um, - }) -} - -// SetEventBus sets event bus. -func (conR *Reactor) SetEventBus(b *types.EventBus) { - conR.eventBus = b - conR.conS.SetEventBus(b) -} - -// WaitSync returns whether the consensus reactor is waiting for state/fast sync. -func (conR *Reactor) WaitSync() bool { - conR.mtx.RLock() - defer conR.mtx.RUnlock() - return conR.waitSync -} - -//-------------------------------------- - -// subscribeToBroadcastEvents subscribes for new round steps and votes -// using internal pubsub defined on state to broadcast -// them to peers upon receiving. -func (conR *Reactor) subscribeToBroadcastEvents() { - const subscriber = "consensus-reactor" - if err := conR.conS.evsw.AddListenerForEvent(subscriber, types.EventNewRoundStep, - func(data cmtevents.EventData) { - conR.broadcastNewRoundStepMessage(data.(*cstypes.RoundState)) - }); err != nil { - conR.Logger.Error("Error adding listener for events", "err", err) - } - - if err := conR.conS.evsw.AddListenerForEvent(subscriber, types.EventValidBlock, - func(data cmtevents.EventData) { - conR.broadcastNewValidBlockMessage(data.(*cstypes.RoundState)) - }); err != nil { - conR.Logger.Error("Error adding listener for events", "err", err) - } - - if err := conR.conS.evsw.AddListenerForEvent(subscriber, types.EventVote, - func(data cmtevents.EventData) { - conR.broadcastHasVoteMessage(data.(*types.Vote)) - }); err != nil { - conR.Logger.Error("Error adding listener for events", "err", err) - } - -} - -func (conR *Reactor) unsubscribeFromBroadcastEvents() { - const subscriber = "consensus-reactor" - conR.conS.evsw.RemoveListener(subscriber) -} - -func (conR *Reactor) broadcastNewRoundStepMessage(rs *cstypes.RoundState) { - conR.Switch.BroadcastEnvelope(p2p.Envelope{ - ChannelID: StateChannel, - Message: &cmtcons.NewRoundStep{ - Height: rs.Height, - Round: rs.Round, - Step: uint32(rs.Step), - SecondsSinceStartTime: int64(time.Since(rs.StartTime).Seconds()), - LastCommitRound: rs.LastCommit.GetRound(), - }, - }) -} - -func (conR *Reactor) broadcastNewValidBlockMessage(rs *cstypes.RoundState) { - psh := rs.ProposalBlockParts.Header() - conR.Switch.BroadcastEnvelope(p2p.Envelope{ - ChannelID: StateChannel, - Message: &cmtcons.NewValidBlock{ - Height: rs.Height, - Round: rs.Round, - BlockPartSetHeader: psh.ToProto(), - BlockParts: rs.ProposalBlockParts.BitArray().ToProto(), - IsCommit: rs.Step == cstypes.RoundStepCommit, - }, - }) -} - -// Broadcasts HasVoteMessage to peers that care. -func (conR *Reactor) broadcastHasVoteMessage(vote *types.Vote) { - conR.Switch.BroadcastEnvelope(p2p.Envelope{ - ChannelID: StateChannel, - Message: &cmtcons.HasVote{ - Height: vote.Height, - Round: vote.Round, - Type: vote.Type, - Index: vote.ValidatorIndex, - }, - }) - /* - // TODO: Make this broadcast more selective. - for _, peer := range conR.Switch.Peers().List() { - ps, ok := peer.Get(PeerStateKey).(*PeerState) - if !ok { - panic(fmt.Sprintf("Peer %v has no state", peer)) - } - prs := ps.GetRoundState() - if prs.Height == vote.Height { - // TODO: Also filter on round? - peer.TrySend(StateChannel, struct{ ConsensusMessage }{msg}) - } else { - // Height doesn't match - // TODO: check a field, maybe CatchupCommitRound? - // TODO: But that requires changing the struct field comment. - } - } - */ -} - -func (conR *Reactor) sendNewRoundStepMessage(peer p2p.Peer) { - rs := conR.conS.GetRoundState() - p2p.TrySendEnvelopeShim(peer, p2p.Envelope{ //nolint: staticcheck - ChannelID: StateChannel, - Message: &cmtcons.NewRoundStep{ - Height: rs.Height, - Round: rs.Round, - Step: uint32(rs.Step), - SecondsSinceStartTime: int64(time.Since(rs.StartTime).Seconds()), - LastCommitRound: rs.LastCommit.GetRound(), - }, - }, conR.Logger) -} - -func (conR *Reactor) gossipDataRoutine(peer p2p.Peer, ps *PeerState) { - logger := conR.Logger.With("peer", peer) - -OUTER_LOOP: - for { - // Manage disconnects from self or peer. - if !peer.IsRunning() || !conR.IsRunning() { - logger.Info("Stopping gossipDataRoutine for peer") - return - } - rs := conR.conS.GetRoundState() - prs := ps.GetRoundState() - - // Send proposal Block parts? - if rs.ProposalBlockParts.HasHeader(prs.ProposalBlockPartSetHeader) { - if index, ok := rs.ProposalBlockParts.BitArray().Sub(prs.ProposalBlockParts.Copy()).PickRandom(); ok { - part := rs.ProposalBlockParts.GetPart(index) - logger.Debug("Sending block part", "height", prs.Height, "round", prs.Round) - p, err := part.ToProto() - if err != nil { - panic(err) - } - if p2p.TrySendEnvelopeShim(peer, p2p.Envelope{ //nolint: staticcheck - ChannelID: DataChannel, - Message: &cmtcons.BlockPart{ - Height: rs.Height, // This tells peer that this part applies to us. - Round: rs.Round, // This tells peer that this part applies to us. - Part: *p, - }, - }, logger) { - ps.SetHasProposalBlockPart(prs.Height, prs.Round, index) - } - continue OUTER_LOOP - } - } - - // If the peer is on a previous height that we have, help catch up. - if (0 < prs.Height) && (prs.Height < rs.Height) && (prs.Height >= conR.conS.blockStore.Base()) { - heightLogger := logger.With("height", prs.Height) - - // if we never received the commit message from the peer, the block parts wont be initialized - if prs.ProposalBlockParts == nil { - blockMeta := conR.conS.blockStore.LoadBlockMeta(prs.Height) - if blockMeta == nil { - heightLogger.Error("Failed to load block meta", - "blockstoreBase", conR.conS.blockStore.Base(), "blockstoreHeight", conR.conS.blockStore.Height()) - time.Sleep(conR.conS.config.PeerGossipSleepDuration) - } else { - ps.InitProposalBlockParts(blockMeta.BlockID.PartSetHeader) - } - // continue the loop since prs is a copy and not effected by this initialization - continue OUTER_LOOP - } - conR.gossipDataForCatchup(heightLogger, rs, prs, ps, peer) - continue OUTER_LOOP - } - - // If height and round don't match, sleep. - if (rs.Height != prs.Height) || (rs.Round != prs.Round) { - time.Sleep(conR.conS.config.PeerGossipSleepDuration) - continue OUTER_LOOP - } - - // By here, height and round match. - // Proposal block parts were already matched and sent if any were wanted. - // (These can match on hash so the round doesn't matter) - // Now consider sending other things, like the Proposal itself. - - // Send Proposal && ProposalPOL BitArray? - if rs.Proposal != nil && !prs.Proposal { - // Proposal: share the proposal metadata with peer. - { - msg := &cmtcons.Proposal{Proposal: *rs.Proposal.ToProto()} - logger.Debug("Sending proposal", "height", prs.Height, "round", prs.Round) - if p2p.SendEnvelopeShim(peer, p2p.Envelope{ //nolint: staticcheck - ChannelID: DataChannel, - Message: msg, - }, logger) { - // NOTE[ZM]: A peer might have received different proposal msg so this Proposal msg will be rejected! - ps.SetHasProposal(rs.Proposal) - } - } - // ProposalPOL: lets peer know which POL votes we have so far. - // Peer must receive ProposalMessage first. - // rs.Proposal was validated, so rs.Proposal.POLRound <= rs.Round, - // so we definitely have rs.Votes.Prevotes(rs.Proposal.POLRound). - if 0 <= rs.Proposal.POLRound { - msg := &cmtcons.ProposalPOL{ - Height: rs.Height, - ProposalPolRound: rs.Proposal.POLRound, - ProposalPol: *rs.Votes.Prevotes(rs.Proposal.POLRound).BitArray().ToProto(), - } - logger.Debug("Sending POL", "height", prs.Height, "round", prs.Round) - p2p.SendEnvelopeShim(peer, p2p.Envelope{ //nolint: staticcheck - ChannelID: DataChannel, - Message: msg, - }, logger) - } - continue OUTER_LOOP - } - - // Nothing to do. Sleep. - time.Sleep(conR.conS.config.PeerGossipSleepDuration) - continue OUTER_LOOP - } -} - -func (conR *Reactor) gossipDataForCatchup(logger log.Logger, rs *cstypes.RoundState, - prs *cstypes.PeerRoundState, ps *PeerState, peer p2p.Peer) { - - if index, ok := prs.ProposalBlockParts.Not().PickRandom(); ok { - // Ensure that the peer's PartSetHeader is correct - blockMeta := conR.conS.blockStore.LoadBlockMeta(prs.Height) - if blockMeta == nil { - logger.Error("Failed to load block meta", "ourHeight", rs.Height, - "blockstoreBase", conR.conS.blockStore.Base(), "blockstoreHeight", conR.conS.blockStore.Height()) - time.Sleep(conR.conS.config.PeerGossipSleepDuration) - return - } else if !blockMeta.BlockID.PartSetHeader.Equals(prs.ProposalBlockPartSetHeader) { - logger.Info("Peer ProposalBlockPartSetHeader mismatch, sleeping", - "blockPartSetHeader", blockMeta.BlockID.PartSetHeader, "peerBlockPartSetHeader", prs.ProposalBlockPartSetHeader) - time.Sleep(conR.conS.config.PeerGossipSleepDuration) - return - } - // Load the part - part := conR.conS.blockStore.LoadBlockPart(prs.Height, index) - if part == nil { - logger.Error("Could not load part", "index", index, - "blockPartSetHeader", blockMeta.BlockID.PartSetHeader, "peerBlockPartSetHeader", prs.ProposalBlockPartSetHeader) - time.Sleep(conR.conS.config.PeerGossipSleepDuration) - return - } - // Send the part - - pp, err := part.ToProto() - if err != nil { - logger.Error("Could not convert part to proto", "index", index, "error", err) - return - } - logger.Debug("Sending block part for catchup", "round", prs.Round, "index", index) - if p2p.SendEnvelopeShim(peer, p2p.Envelope{ //nolint: staticcheck - ChannelID: DataChannel, - Message: &cmtcons.BlockPart{ - Height: prs.Height, // Not our height, so it doesn't matter. - Round: prs.Round, // Not our height, so it doesn't matter. - Part: *pp, - }, - }, logger) { - ps.SetHasProposalBlockPart(prs.Height, prs.Round, index) - } else { - logger.Debug("Sending block part for catchup failed") - } - return - } - time.Sleep(conR.conS.config.PeerGossipSleepDuration) -} - -func (conR *Reactor) gossipVotesRoutine(peer p2p.Peer, ps *PeerState) { - logger := conR.Logger.With("peer", peer) - - // Simple hack to throttle logs upon sleep. - var sleeping = 0 - -OUTER_LOOP: - for { - // Manage disconnects from self or peer. - if !peer.IsRunning() || !conR.IsRunning() { - logger.Info("Stopping gossipVotesRoutine for peer") - return - } - rs := conR.conS.GetRoundState() - prs := ps.GetRoundState() - - switch sleeping { - case 1: // First sleep - sleeping = 2 - case 2: // No more sleep - sleeping = 0 - } - - // If height matches, then send LastCommit, Prevotes, Precommits. - if rs.Height == prs.Height { - heightLogger := logger.With("height", prs.Height) - if conR.gossipVotesForHeight(heightLogger, rs, prs, ps) { - continue OUTER_LOOP - } - } - - // Special catchup logic. - // If peer is lagging by height 1, send LastCommit. - if prs.Height != 0 && rs.Height == prs.Height+1 { - if ps.PickSendVote(rs.LastCommit) { - logger.Debug("Picked rs.LastCommit to send", "height", prs.Height) - continue OUTER_LOOP - } - } - - // Catchup logic - // If peer is lagging by more than 1, send Commit. - if prs.Height != 0 && rs.Height >= prs.Height+2 && prs.Height >= conR.conS.blockStore.Base() { - // Load the block commit for prs.Height, - // which contains precommit signatures for prs.Height. - if commit := conR.conS.blockStore.LoadBlockCommit(prs.Height); commit != nil { - if ps.PickSendVote(commit) { - logger.Debug("Picked Catchup commit to send", "height", prs.Height) - continue OUTER_LOOP - } - } - } - - if sleeping == 0 { - // We sent nothing. Sleep... - sleeping = 1 - logger.Debug("No votes to send, sleeping", "rs.Height", rs.Height, "prs.Height", prs.Height, - "localPV", rs.Votes.Prevotes(rs.Round).BitArray(), "peerPV", prs.Prevotes, - "localPC", rs.Votes.Precommits(rs.Round).BitArray(), "peerPC", prs.Precommits) - } else if sleeping == 2 { - // Continued sleep... - sleeping = 1 - } - - time.Sleep(conR.conS.config.PeerGossipSleepDuration) - continue OUTER_LOOP - } -} - -func (conR *Reactor) gossipVotesForHeight( - logger log.Logger, - rs *cstypes.RoundState, - prs *cstypes.PeerRoundState, - ps *PeerState, -) bool { - - // If there are lastCommits to send... - if prs.Step == cstypes.RoundStepNewHeight { - if ps.PickSendVote(rs.LastCommit) { - logger.Debug("Picked rs.LastCommit to send") - return true - } - } - // If there are POL prevotes to send... - if prs.Step <= cstypes.RoundStepPropose && prs.Round != -1 && prs.Round <= rs.Round && prs.ProposalPOLRound != -1 { - if polPrevotes := rs.Votes.Prevotes(prs.ProposalPOLRound); polPrevotes != nil { - if ps.PickSendVote(polPrevotes) { - logger.Debug("Picked rs.Prevotes(prs.ProposalPOLRound) to send", - "round", prs.ProposalPOLRound) - return true - } - } - } - // If there are prevotes to send... - if prs.Step <= cstypes.RoundStepPrevoteWait && prs.Round != -1 && prs.Round <= rs.Round { - if ps.PickSendVote(rs.Votes.Prevotes(prs.Round)) { - logger.Debug("Picked rs.Prevotes(prs.Round) to send", "round", prs.Round) - return true - } - } - // If there are precommits to send... - if prs.Step <= cstypes.RoundStepPrecommitWait && prs.Round != -1 && prs.Round <= rs.Round { - if ps.PickSendVote(rs.Votes.Precommits(prs.Round)) { - logger.Debug("Picked rs.Precommits(prs.Round) to send", "round", prs.Round) - return true - } - } - // If there are prevotes to send...Needed because of validBlock mechanism - if prs.Round != -1 && prs.Round <= rs.Round { - if ps.PickSendVote(rs.Votes.Prevotes(prs.Round)) { - logger.Debug("Picked rs.Prevotes(prs.Round) to send", "round", prs.Round) - return true - } - } - // If there are POLPrevotes to send... - if prs.ProposalPOLRound != -1 { - if polPrevotes := rs.Votes.Prevotes(prs.ProposalPOLRound); polPrevotes != nil { - if ps.PickSendVote(polPrevotes) { - logger.Debug("Picked rs.Prevotes(prs.ProposalPOLRound) to send", - "round", prs.ProposalPOLRound) - return true - } - } - } - - return false -} - -// NOTE: `queryMaj23Routine` has a simple crude design since it only comes -// into play for liveness when there's a signature DDoS attack happening. -func (conR *Reactor) queryMaj23Routine(peer p2p.Peer, ps *PeerState) { - logger := conR.Logger.With("peer", peer) - -OUTER_LOOP: - for { - // Manage disconnects from self or peer. - if !peer.IsRunning() || !conR.IsRunning() { - logger.Info("Stopping queryMaj23Routine for peer") - return - } - - // Maybe send Height/Round/Prevotes - { - rs := conR.conS.GetRoundState() - prs := ps.GetRoundState() - if rs.Height == prs.Height { - if maj23, ok := rs.Votes.Prevotes(prs.Round).TwoThirdsMajority(); ok { - - p2p.TrySendEnvelopeShim(peer, p2p.Envelope{ //nolint: staticcheck - ChannelID: StateChannel, - Message: &cmtcons.VoteSetMaj23{ - Height: prs.Height, - Round: prs.Round, - Type: cmtproto.PrevoteType, - BlockID: maj23.ToProto(), - }}, logger) - - time.Sleep(conR.conS.config.PeerQueryMaj23SleepDuration) - } - } - } - - // Maybe send Height/Round/Precommits - { - rs := conR.conS.GetRoundState() - prs := ps.GetRoundState() - if rs.Height == prs.Height { - if maj23, ok := rs.Votes.Precommits(prs.Round).TwoThirdsMajority(); ok { - p2p.TrySendEnvelopeShim(peer, p2p.Envelope{ //nolint: staticcheck - ChannelID: StateChannel, - Message: &cmtcons.VoteSetMaj23{ - Height: prs.Height, - Round: prs.Round, - Type: cmtproto.PrecommitType, - BlockID: maj23.ToProto(), - }}, logger) - time.Sleep(conR.conS.config.PeerQueryMaj23SleepDuration) - } - } - } - - // Maybe send Height/Round/ProposalPOL - { - rs := conR.conS.GetRoundState() - prs := ps.GetRoundState() - if rs.Height == prs.Height && prs.ProposalPOLRound >= 0 { - if maj23, ok := rs.Votes.Prevotes(prs.ProposalPOLRound).TwoThirdsMajority(); ok { - p2p.TrySendEnvelopeShim(peer, p2p.Envelope{ //nolint: staticcheck - ChannelID: StateChannel, - Message: &cmtcons.VoteSetMaj23{ - Height: prs.Height, - Round: prs.ProposalPOLRound, - Type: cmtproto.PrevoteType, - BlockID: maj23.ToProto(), - }}, logger) - time.Sleep(conR.conS.config.PeerQueryMaj23SleepDuration) - } - } - } - - // Little point sending LastCommitRound/LastCommit, - // These are fleeting and non-blocking. - - // Maybe send Height/CatchupCommitRound/CatchupCommit. - { - prs := ps.GetRoundState() - if prs.CatchupCommitRound != -1 && prs.Height > 0 && prs.Height <= conR.conS.blockStore.Height() && - prs.Height >= conR.conS.blockStore.Base() { - if commit := conR.conS.LoadCommit(prs.Height); commit != nil { - p2p.TrySendEnvelopeShim(peer, p2p.Envelope{ //nolint: staticcheck - ChannelID: StateChannel, - Message: &cmtcons.VoteSetMaj23{ - Height: prs.Height, - Round: commit.Round, - Type: cmtproto.PrecommitType, - BlockID: commit.BlockID.ToProto(), - }}, logger) - time.Sleep(conR.conS.config.PeerQueryMaj23SleepDuration) - } - } - } - - time.Sleep(conR.conS.config.PeerQueryMaj23SleepDuration) - - continue OUTER_LOOP - } -} - -func (conR *Reactor) peerStatsRoutine() { - for { - if !conR.IsRunning() { - conR.Logger.Info("Stopping peerStatsRoutine") - return - } - - select { - case msg := <-conR.conS.statsMsgQueue: - // Get peer - peer := conR.Switch.Peers().Get(msg.PeerID) - if peer == nil { - conR.Logger.Debug("Attempt to update stats for non-existent peer", - "peer", msg.PeerID) - continue - } - // Get peer state - ps, ok := peer.Get(types.PeerStateKey).(*PeerState) - if !ok { - panic(fmt.Sprintf("Peer %v has no state", peer)) - } - switch msg.Msg.(type) { - case *cmtcon.VoteMessage: - if numVotes := ps.RecordVote(); numVotes%votesToContributeToBecomeGoodPeer == 0 { - conR.Switch.MarkPeerAsGood(peer) - } - case *cmtcon.BlockPartMessage: - if numParts := ps.RecordBlockPart(); numParts%blocksToContributeToBecomeGoodPeer == 0 { - conR.Switch.MarkPeerAsGood(peer) - } - } - case <-conR.conS.Quit(): - return - - case <-conR.Quit(): - return - } - } -} - -// String returns a string representation of the Reactor. -// NOTE: For now, it is just a hard-coded string to avoid accessing unprotected shared variables. -// TODO: improve! -func (conR *Reactor) String() string { - // better not to access shared variables - return "ConsensusReactor" // conR.StringIndented("") -} - -// StringIndented returns an indented string representation of the Reactor -func (conR *Reactor) StringIndented(indent string) string { - s := "ConsensusReactor{\n" - s += indent + " " + conR.conS.StringIndented(indent+" ") + "\n" - for _, peer := range conR.Switch.Peers().List() { - ps, ok := peer.Get(types.PeerStateKey).(*PeerState) - if !ok { - panic(fmt.Sprintf("Peer %v has no state", peer)) - } - s += indent + " " + ps.StringIndented(indent+" ") + "\n" - } - s += indent + "}" - return s -} - -// ReactorMetrics sets the metrics -func ReactorMetrics(metrics *cmtcon.Metrics) ReactorOption { - return func(conR *Reactor) { conR.Metrics = metrics } -} - -//----------------------------------------------------------------------------- - -var ( - ErrPeerStateHeightRegression = errors.New("error peer state height regression") - ErrPeerStateInvalidStartTime = errors.New("error peer state invalid startTime") -) - -// PeerState contains the known state of a peer, including its connection and -// threadsafe access to its PeerRoundState. -// NOTE: THIS GETS DUMPED WITH rpc/core/consensus.go. -// Be mindful of what you Expose. -type PeerState struct { - peer p2p.Peer - logger log.Logger - - mtx sync.Mutex // NOTE: Modify below using setters, never directly. - PRS cstypes.PeerRoundState `json:"round_state"` // Exposed. - Stats *peerStateStats `json:"stats"` // Exposed. -} - -// peerStateStats holds internal statistics for a peer. -type peerStateStats struct { - Votes int `json:"votes"` - BlockParts int `json:"block_parts"` -} - -func (pss peerStateStats) String() string { - return fmt.Sprintf("peerStateStats{votes: %d, blockParts: %d}", - pss.Votes, pss.BlockParts) -} - -// NewPeerState returns a new PeerState for the given Peer -func NewPeerState(peer p2p.Peer) *PeerState { - return &PeerState{ - peer: peer, - logger: log.NewNopLogger(), - PRS: cstypes.PeerRoundState{ - Round: -1, - ProposalPOLRound: -1, - LastCommitRound: -1, - CatchupCommitRound: -1, - }, - Stats: &peerStateStats{}, - } -} - -// SetLogger allows to set a logger on the peer state. Returns the peer state -// itself. -func (ps *PeerState) SetLogger(logger log.Logger) *PeerState { - ps.logger = logger - return ps -} - -// GetRoundState returns an shallow copy of the PeerRoundState. -// There's no point in mutating it since it won't change PeerState. -func (ps *PeerState) GetRoundState() *cstypes.PeerRoundState { - ps.mtx.Lock() - defer ps.mtx.Unlock() - - prs := ps.PRS // copy - return &prs -} - -// ToJSON returns a json of PeerState. -func (ps *PeerState) ToJSON() ([]byte, error) { - ps.mtx.Lock() - defer ps.mtx.Unlock() - - return cmtjson.Marshal(ps) -} - -// GetHeight returns an atomic snapshot of the PeerRoundState's height -// used by the mempool to ensure peers are caught up before broadcasting new txs -func (ps *PeerState) GetHeight() int64 { - ps.mtx.Lock() - defer ps.mtx.Unlock() - return ps.PRS.Height -} - -// SetHasProposal sets the given proposal as known for the peer. -func (ps *PeerState) SetHasProposal(proposal *types.Proposal) { - ps.mtx.Lock() - defer ps.mtx.Unlock() - - if ps.PRS.Height != proposal.Height || ps.PRS.Round != proposal.Round { - return - } - - if ps.PRS.Proposal { - return - } - - ps.PRS.Proposal = true - - // ps.PRS.ProposalBlockParts is set due to NewValidBlockMessage - if ps.PRS.ProposalBlockParts != nil { - return - } - - ps.PRS.ProposalBlockPartSetHeader = proposal.BlockID.PartSetHeader - ps.PRS.ProposalBlockParts = bits.NewBitArray(int(proposal.BlockID.PartSetHeader.Total)) - ps.PRS.ProposalPOLRound = proposal.POLRound - ps.PRS.ProposalPOL = nil // Nil until ProposalPOLMessage received. -} - -// InitProposalBlockParts initializes the peer's proposal block parts header and bit array. -func (ps *PeerState) InitProposalBlockParts(partSetHeader types.PartSetHeader) { - ps.mtx.Lock() - defer ps.mtx.Unlock() - - if ps.PRS.ProposalBlockParts != nil { - return - } - - ps.PRS.ProposalBlockPartSetHeader = partSetHeader - ps.PRS.ProposalBlockParts = bits.NewBitArray(int(partSetHeader.Total)) -} - -// SetHasProposalBlockPart sets the given block part index as known for the peer. -func (ps *PeerState) SetHasProposalBlockPart(height int64, round int32, index int) { - ps.mtx.Lock() - defer ps.mtx.Unlock() - - if ps.PRS.Height != height || ps.PRS.Round != round { - return - } - - ps.PRS.ProposalBlockParts.SetIndex(index, true) -} - -// PickSendVote picks a vote and sends it to the peer. -// Returns true if vote was sent. -func (ps *PeerState) PickSendVote(votes types.VoteSetReader) bool { - if vote, ok := ps.PickVoteToSend(votes); ok { - ps.logger.Debug("Sending vote message", "ps", ps, "vote", vote) - if p2p.TrySendEnvelopeShim(ps.peer, p2p.Envelope{ //nolint: staticcheck - ChannelID: VoteChannel, - Message: &cmtcons.Vote{Vote: vote.ToProto()}, - }, ps.logger) { - ps.SetHasVote(vote) - return true - } - return false - } - return false -} - -// PickVoteToSend picks a vote to send to the peer. -// Returns true if a vote was picked. -// NOTE: `votes` must be the correct Size() for the Height(). -func (ps *PeerState) PickVoteToSend(votes types.VoteSetReader) (vote *types.Vote, ok bool) { - ps.mtx.Lock() - defer ps.mtx.Unlock() - - if votes.Size() == 0 { - return nil, false - } - - height, round, votesType, size := - votes.GetHeight(), votes.GetRound(), cmtproto.SignedMsgType(votes.Type()), votes.Size() - - // Lazily set data using 'votes'. - if votes.IsCommit() { - ps.ensureCatchupCommitRound(height, round, size) - } - ps.ensureVoteBitArrays(height, size) - - psVotes := ps.getVoteBitArray(height, round, votesType) - if psVotes == nil { - return nil, false // Not something worth sending - } - if index, ok := votes.BitArray().Sub(psVotes).PickRandom(); ok { - return votes.GetByIndex(int32(index)), true - } - return nil, false -} - -func (ps *PeerState) getVoteBitArray(height int64, round int32, votesType cmtproto.SignedMsgType) *bits.BitArray { - if !types.IsVoteTypeValid(votesType) { - return nil - } - - if ps.PRS.Height == height { - if ps.PRS.Round == round { - switch votesType { - case cmtproto.PrevoteType: - return ps.PRS.Prevotes - case cmtproto.PrecommitType: - return ps.PRS.Precommits - } - } - if ps.PRS.CatchupCommitRound == round { - switch votesType { - case cmtproto.PrevoteType: - return nil - case cmtproto.PrecommitType: - return ps.PRS.CatchupCommit - } - } - if ps.PRS.ProposalPOLRound == round { - switch votesType { - case cmtproto.PrevoteType: - return ps.PRS.ProposalPOL - case cmtproto.PrecommitType: - return nil - } - } - return nil - } - if ps.PRS.Height == height+1 { - if ps.PRS.LastCommitRound == round { - switch votesType { - case cmtproto.PrevoteType: - return nil - case cmtproto.PrecommitType: - return ps.PRS.LastCommit - } - } - return nil - } - return nil -} - -// 'round': A round for which we have a +2/3 commit. -func (ps *PeerState) ensureCatchupCommitRound(height int64, round int32, numValidators int) { - if ps.PRS.Height != height { - return - } - /* - NOTE: This is wrong, 'round' could change. - e.g. if orig round is not the same as block LastCommit round. - if ps.CatchupCommitRound != -1 && ps.CatchupCommitRound != round { - panic(fmt.Sprintf( - "Conflicting CatchupCommitRound. Height: %v, - Orig: %v, - New: %v", - height, - ps.CatchupCommitRound, - round)) - } - */ - if ps.PRS.CatchupCommitRound == round { - return // Nothing to do! - } - ps.PRS.CatchupCommitRound = round - if round == ps.PRS.Round { - ps.PRS.CatchupCommit = ps.PRS.Precommits - } else { - ps.PRS.CatchupCommit = bits.NewBitArray(numValidators) - } -} - -// EnsureVoteBitArrays ensures the bit-arrays have been allocated for tracking -// what votes this peer has received. -// NOTE: It's important to make sure that numValidators actually matches -// what the node sees as the number of validators for height. -func (ps *PeerState) EnsureVoteBitArrays(height int64, numValidators int) { - ps.mtx.Lock() - defer ps.mtx.Unlock() - ps.ensureVoteBitArrays(height, numValidators) -} - -func (ps *PeerState) ensureVoteBitArrays(height int64, numValidators int) { - if ps.PRS.Height == height { - if ps.PRS.Prevotes == nil { - ps.PRS.Prevotes = bits.NewBitArray(numValidators) - } - if ps.PRS.Precommits == nil { - ps.PRS.Precommits = bits.NewBitArray(numValidators) - } - if ps.PRS.CatchupCommit == nil { - ps.PRS.CatchupCommit = bits.NewBitArray(numValidators) - } - if ps.PRS.ProposalPOL == nil { - ps.PRS.ProposalPOL = bits.NewBitArray(numValidators) - } - } else if ps.PRS.Height == height+1 { - if ps.PRS.LastCommit == nil { - ps.PRS.LastCommit = bits.NewBitArray(numValidators) - } - } -} - -// RecordVote increments internal votes related statistics for this peer. -// It returns the total number of added votes. -func (ps *PeerState) RecordVote() int { - ps.mtx.Lock() - defer ps.mtx.Unlock() - - ps.Stats.Votes++ - - return ps.Stats.Votes -} - -// VotesSent returns the number of blocks for which peer has been sending us -// votes. -func (ps *PeerState) VotesSent() int { - ps.mtx.Lock() - defer ps.mtx.Unlock() - - return ps.Stats.Votes -} - -// RecordBlockPart increments internal block part related statistics for this peer. -// It returns the total number of added block parts. -func (ps *PeerState) RecordBlockPart() int { - ps.mtx.Lock() - defer ps.mtx.Unlock() - - ps.Stats.BlockParts++ - return ps.Stats.BlockParts -} - -// BlockPartsSent returns the number of useful block parts the peer has sent us. -func (ps *PeerState) BlockPartsSent() int { - ps.mtx.Lock() - defer ps.mtx.Unlock() - - return ps.Stats.BlockParts -} - -// SetHasVote sets the given vote as known by the peer -func (ps *PeerState) SetHasVote(vote *types.Vote) { - ps.mtx.Lock() - defer ps.mtx.Unlock() - - ps.setHasVote(vote.Height, vote.Round, vote.Type, vote.ValidatorIndex) -} - -func (ps *PeerState) setHasVote(height int64, round int32, voteType cmtproto.SignedMsgType, index int32) { - logger := ps.logger.With( - "peerH/R", - fmt.Sprintf("%d/%d", ps.PRS.Height, ps.PRS.Round), - "H/R", - fmt.Sprintf("%d/%d", height, round)) - logger.Debug("setHasVote", "type", voteType, "index", index) - - // NOTE: some may be nil BitArrays -> no side effects. - psVotes := ps.getVoteBitArray(height, round, voteType) - if psVotes != nil { - psVotes.SetIndex(int(index), true) - } -} - -// ApplyNewRoundStepMessage updates the peer state for the new round. -func (ps *PeerState) ApplyNewRoundStepMessage(msg *cmtcon.NewRoundStepMessage) { - ps.mtx.Lock() - defer ps.mtx.Unlock() - - // Ignore duplicates or decreases - if CompareHRS(msg.Height, msg.Round, msg.Step, ps.PRS.Height, ps.PRS.Round, ps.PRS.Step) <= 0 { - return - } - - // Just remember these values. - psHeight := ps.PRS.Height - psRound := ps.PRS.Round - psCatchupCommitRound := ps.PRS.CatchupCommitRound - psCatchupCommit := ps.PRS.CatchupCommit - - startTime := cmttime.Now().Add(-1 * time.Duration(msg.SecondsSinceStartTime) * time.Second) - ps.PRS.Height = msg.Height - ps.PRS.Round = msg.Round - ps.PRS.Step = msg.Step - ps.PRS.StartTime = startTime - if psHeight != msg.Height || psRound != msg.Round { - ps.PRS.Proposal = false - ps.PRS.ProposalBlockPartSetHeader = types.PartSetHeader{} - ps.PRS.ProposalBlockParts = nil - ps.PRS.ProposalPOLRound = -1 - ps.PRS.ProposalPOL = nil - // We'll update the BitArray capacity later. - ps.PRS.Prevotes = nil - ps.PRS.Precommits = nil - } - if psHeight == msg.Height && psRound != msg.Round && msg.Round == psCatchupCommitRound { - // Peer caught up to CatchupCommitRound. - // Preserve psCatchupCommit! - // NOTE: We prefer to use prs.Precommits if - // pr.Round matches pr.CatchupCommitRound. - ps.PRS.Precommits = psCatchupCommit - } - if psHeight != msg.Height { - // Shift Precommits to LastCommit. - if psHeight+1 == msg.Height && psRound == msg.LastCommitRound { - ps.PRS.LastCommitRound = msg.LastCommitRound - ps.PRS.LastCommit = ps.PRS.Precommits - } else { - ps.PRS.LastCommitRound = msg.LastCommitRound - ps.PRS.LastCommit = nil - } - // We'll update the BitArray capacity later. - ps.PRS.CatchupCommitRound = -1 - ps.PRS.CatchupCommit = nil - } -} - -// ApplyNewValidBlockMessage updates the peer state for the new valid block. -func (ps *PeerState) ApplyNewValidBlockMessage(msg *cmtcon.NewValidBlockMessage) { - ps.mtx.Lock() - defer ps.mtx.Unlock() - - if ps.PRS.Height != msg.Height { - return - } - - if ps.PRS.Round != msg.Round && !msg.IsCommit { - return - } - - ps.PRS.ProposalBlockPartSetHeader = msg.BlockPartSetHeader - ps.PRS.ProposalBlockParts = msg.BlockParts -} - -// ApplyProposalPOLMessage updates the peer state for the new proposal POL. -func (ps *PeerState) ApplyProposalPOLMessage(msg *cmtcon.ProposalPOLMessage) { - ps.mtx.Lock() - defer ps.mtx.Unlock() - - if ps.PRS.Height != msg.Height { - return - } - if ps.PRS.ProposalPOLRound != msg.ProposalPOLRound { - return - } - - // TODO: Merge onto existing ps.PRS.ProposalPOL? - // We might have sent some prevotes in the meantime. - ps.PRS.ProposalPOL = msg.ProposalPOL -} - -// ApplyHasVoteMessage updates the peer state for the new vote. -func (ps *PeerState) ApplyHasVoteMessage(msg *cmtcon.HasVoteMessage) { - ps.mtx.Lock() - defer ps.mtx.Unlock() - - if ps.PRS.Height != msg.Height { - return - } - - ps.setHasVote(msg.Height, msg.Round, msg.Type, msg.Index) -} - -// ApplyVoteSetBitsMessage updates the peer state for the bit-array of votes -// it claims to have for the corresponding BlockID. -// `ourVotes` is a BitArray of votes we have for msg.BlockID -// NOTE: if ourVotes is nil (e.g. msg.Height < rs.Height), -// we conservatively overwrite ps's votes w/ msg.Votes. -func (ps *PeerState) ApplyVoteSetBitsMessage(msg *cmtcon.VoteSetBitsMessage, ourVotes *bits.BitArray) { - ps.mtx.Lock() - defer ps.mtx.Unlock() - - votes := ps.getVoteBitArray(msg.Height, msg.Round, msg.Type) - if votes != nil { - if ourVotes == nil { - votes.Update(msg.Votes) - } else { - otherVotes := votes.Sub(ourVotes) - hasVotes := otherVotes.Or(msg.Votes) - votes.Update(hasVotes) - } - } -} - -// String returns a string representation of the PeerState -func (ps *PeerState) String() string { - return ps.StringIndented("") -} - -// StringIndented returns a string representation of the PeerState -func (ps *PeerState) StringIndented(indent string) string { - ps.mtx.Lock() - defer ps.mtx.Unlock() - return fmt.Sprintf(`PeerState{ -%s Key %v -%s RoundState %v -%s Stats %v -%s}`, - indent, ps.peer.ID(), - indent, ps.PRS.StringIndented(indent+" "), - indent, ps.Stats, - indent) -} - -//----------------------------------------------------------------------------- - -// func init() { -// cmtjson.RegisterType(&NewRoundStepMessage{}, "tendermint/NewRoundStepMessage") -// cmtjson.RegisterType(&NewValidBlockMessage{}, "tendermint/NewValidBlockMessage") -// cmtjson.RegisterType(&ProposalMessage{}, "tendermint/Proposal") -// cmtjson.RegisterType(&ProposalPOLMessage{}, "tendermint/ProposalPOL") -// cmtjson.RegisterType(&BlockPartMessage{}, "tendermint/BlockPart") -// cmtjson.RegisterType(&VoteMessage{}, "tendermint/Vote") -// cmtjson.RegisterType(&HasVoteMessage{}, "tendermint/HasVote") -// cmtjson.RegisterType(&VoteSetMaj23Message{}, "tendermint/VoteSetMaj23") -// cmtjson.RegisterType(&VoteSetBitsMessage{}, "tendermint/VoteSetBits") -// } diff --git a/test/maverick/consensus/replay.go b/test/maverick/consensus/replay.go deleted file mode 100644 index ca863e0d0b..0000000000 --- a/test/maverick/consensus/replay.go +++ /dev/null @@ -1,535 +0,0 @@ -package consensus - -import ( - "bytes" - "fmt" - "hash/crc32" - "io" - "reflect" - "time" - - abci "github.com/tendermint/tendermint/abci/types" - cmtcon "github.com/tendermint/tendermint/consensus" - "github.com/tendermint/tendermint/crypto/merkle" - "github.com/tendermint/tendermint/libs/log" - "github.com/tendermint/tendermint/proxy" - sm "github.com/tendermint/tendermint/state" - "github.com/tendermint/tendermint/types" -) - -var crc32c = crc32.MakeTable(crc32.Castagnoli) - -// Functionality to replay blocks and messages on recovery from a crash. -// There are two general failure scenarios: -// -// 1. failure during consensus -// 2. failure while applying the block -// -// The former is handled by the WAL, the latter by the proxyApp Handshake on -// restart, which ultimately hands off the work to the WAL. - -//----------------------------------------- -// 1. Recover from failure during consensus -// (by replaying messages from the WAL) -//----------------------------------------- - -// Unmarshal and apply a single message to the consensus state as if it were -// received in receiveRoutine. Lines that start with "#" are ignored. -// NOTE: receiveRoutine should not be running. -func (cs *State) readReplayMessage(msg *cmtcon.TimedWALMessage, newStepSub types.Subscription) error { - // Skip meta messages which exist for demarcating boundaries. - if _, ok := msg.Msg.(cmtcon.EndHeightMessage); ok { - return nil - } - - // for logging - switch m := msg.Msg.(type) { - case types.EventDataRoundState: - cs.Logger.Info("Replay: New Step", "height", m.Height, "round", m.Round, "step", m.Step) - // these are playback checks - ticker := time.After(time.Second * 2) - if newStepSub != nil { - select { - case stepMsg := <-newStepSub.Out(): - m2 := stepMsg.Data().(types.EventDataRoundState) - if m.Height != m2.Height || m.Round != m2.Round || m.Step != m2.Step { - return fmt.Errorf("roundState mismatch. Got %v; Expected %v", m2, m) - } - case <-newStepSub.Cancelled(): - return fmt.Errorf("failed to read off newStepSub.Out(). newStepSub was cancelled") - case <-ticker: - return fmt.Errorf("failed to read off newStepSub.Out()") - } - } - case msgInfo: - peerID := m.PeerID - if peerID == "" { - peerID = "local" - } - switch msg := m.Msg.(type) { - case *cmtcon.ProposalMessage: - p := msg.Proposal - cs.Logger.Info("Replay: Proposal", "height", p.Height, "round", p.Round, "header", - p.BlockID.PartSetHeader, "pol", p.POLRound, "peer", peerID) - case *cmtcon.BlockPartMessage: - cs.Logger.Info("Replay: BlockPart", "height", msg.Height, "round", msg.Round, "peer", peerID) - case *cmtcon.VoteMessage: - v := msg.Vote - cs.Logger.Info("Replay: Vote", "height", v.Height, "round", v.Round, "type", v.Type, - "blockID", v.BlockID, "peer", peerID) - } - - cs.handleMsg(m) - case timeoutInfo: - cs.Logger.Info("Replay: Timeout", "height", m.Height, "round", m.Round, "step", m.Step, "dur", m.Duration) - cs.handleTimeout(m, cs.RoundState) - default: - return fmt.Errorf("replay: Unknown TimedWALMessage type: %v", reflect.TypeOf(msg.Msg)) - } - return nil -} - -// Replay only those messages since the last block. `timeoutRoutine` should -// run concurrently to read off tickChan. -func (cs *State) catchupReplay(csHeight int64) error { - - // Set replayMode to true so we don't log signing errors. - cs.replayMode = true - defer func() { cs.replayMode = false }() - - // Ensure that #ENDHEIGHT for this height doesn't exist. - // NOTE: This is just a sanity check. As far as we know things work fine - // without it, and Handshake could reuse State if it weren't for - // this check (since we can crash after writing #ENDHEIGHT). - // - // Ignore data corruption errors since this is a sanity check. - gr, found, err := cs.wal.SearchForEndHeight(csHeight, &cmtcon.WALSearchOptions{IgnoreDataCorruptionErrors: true}) - if err != nil { - return err - } - if gr != nil { - if err := gr.Close(); err != nil { - return err - } - } - if found { - return fmt.Errorf("wal should not contain #ENDHEIGHT %d", csHeight) - } - - // Search for last height marker. - // - // Ignore data corruption errors in previous heights because we only care about last height - if csHeight < cs.state.InitialHeight { - return fmt.Errorf("cannot replay height %v, below initial height %v", csHeight, cs.state.InitialHeight) - } - endHeight := csHeight - 1 - if csHeight == cs.state.InitialHeight { - endHeight = 0 - } - gr, found, err = cs.wal.SearchForEndHeight(endHeight, &cmtcon.WALSearchOptions{IgnoreDataCorruptionErrors: true}) - if err == io.EOF { - cs.Logger.Error("Replay: wal.group.Search returned EOF", "#ENDHEIGHT", endHeight) - } else if err != nil { - return err - } - if !found { - return fmt.Errorf("cannot replay height %d. WAL does not contain #ENDHEIGHT for %d", csHeight, endHeight) - } - defer gr.Close() - - cs.Logger.Info("Catchup by replaying consensus messages", "height", csHeight) - - var msg *cmtcon.TimedWALMessage - dec := WALDecoder{gr} - -LOOP: - for { - msg, err = dec.Decode() - switch { - case err == io.EOF: - break LOOP - case IsDataCorruptionError(err): - cs.Logger.Error("data has been corrupted in last height of consensus WAL", "err", err, "height", csHeight) - return err - case err != nil: - return err - } - - // NOTE: since the priv key is set when the msgs are received - // it will attempt to eg double sign but we can just ignore it - // since the votes will be replayed and we'll get to the next step - if err := cs.readReplayMessage(msg, nil); err != nil { - return err - } - } - cs.Logger.Info("Replay: Done") - return nil -} - -//-------------------------------------------------------------------------------- - -// Parses marker lines of the form: -// #ENDHEIGHT: 12345 -/* -func makeHeightSearchFunc(height int64) auto.SearchFunc { - return func(line string) (int, error) { - line = strings.TrimRight(line, "\n") - parts := strings.Split(line, " ") - if len(parts) != 2 { - return -1, errors.New("line did not have 2 parts") - } - i, err := strconv.Atoi(parts[1]) - if err != nil { - return -1, errors.New("failed to parse INFO: " + err.Error()) - } - if height < i { - return 1, nil - } else if height == i { - return 0, nil - } else { - return -1, nil - } - } -}*/ - -//--------------------------------------------------- -// 2. Recover from failure while applying the block. -// (by handshaking with the app to figure out where -// we were last, and using the WAL to recover there.) -//--------------------------------------------------- - -type Handshaker struct { - stateStore sm.Store - initialState sm.State - store sm.BlockStore - eventBus types.BlockEventPublisher - genDoc *types.GenesisDoc - logger log.Logger - - nBlocks int // number of blocks applied to the state -} - -func NewHandshaker(stateStore sm.Store, state sm.State, - store sm.BlockStore, genDoc *types.GenesisDoc) *Handshaker { - - return &Handshaker{ - stateStore: stateStore, - initialState: state, - store: store, - eventBus: types.NopEventBus{}, - genDoc: genDoc, - logger: log.NewNopLogger(), - nBlocks: 0, - } -} - -func (h *Handshaker) SetLogger(l log.Logger) { - h.logger = l -} - -// SetEventBus - sets the event bus for publishing block related events. -// If not called, it defaults to types.NopEventBus. -func (h *Handshaker) SetEventBus(eventBus types.BlockEventPublisher) { - h.eventBus = eventBus -} - -// NBlocks returns the number of blocks applied to the state. -func (h *Handshaker) NBlocks() int { - return h.nBlocks -} - -// TODO: retry the handshake/replay if it fails ? -func (h *Handshaker) Handshake(proxyApp proxy.AppConns) error { - - // Handshake is done via ABCI Info on the query conn. - res, err := proxyApp.Query().InfoSync(proxy.RequestInfo) - if err != nil { - return fmt.Errorf("error calling Info: %v", err) - } - - blockHeight := res.LastBlockHeight - if blockHeight < 0 { - return fmt.Errorf("got a negative last block height (%d) from the app", blockHeight) - } - appHash := res.LastBlockAppHash - - h.logger.Info("ABCI Handshake App Info", - "height", blockHeight, - "hash", appHash, - "software-version", res.Version, - "protocol-version", res.AppVersion, - ) - - // Only set the version if there is no existing state. - if h.initialState.LastBlockHeight == 0 { - h.initialState.Version.Consensus.App = res.AppVersion - } - - // Replay blocks up to the latest in the blockstore. - _, err = h.ReplayBlocks(h.initialState, appHash, blockHeight, proxyApp) - if err != nil { - return fmt.Errorf("error on replay: %v", err) - } - - h.logger.Info("Completed ABCI Handshake - CometBFT and App are synced", - "appHeight", blockHeight, "appHash", appHash) - - // TODO: (on restart) replay mempool - - return nil -} - -// ReplayBlocks replays all blocks since appBlockHeight and ensures the result -// matches the current state. -// Returns the final AppHash or an error. -func (h *Handshaker) ReplayBlocks( - state sm.State, - appHash []byte, - appBlockHeight int64, - proxyApp proxy.AppConns, -) ([]byte, error) { - storeBlockBase := h.store.Base() - storeBlockHeight := h.store.Height() - stateBlockHeight := state.LastBlockHeight - h.logger.Info( - "ABCI Replay Blocks", - "appHeight", - appBlockHeight, - "storeHeight", - storeBlockHeight, - "stateHeight", - stateBlockHeight) - - // If appBlockHeight == 0 it means that we are at genesis and hence should send InitChain. - if appBlockHeight == 0 { - validators := make([]*types.Validator, len(h.genDoc.Validators)) - for i, val := range h.genDoc.Validators { - validators[i] = types.NewValidator(val.PubKey, val.Power) - } - validatorSet := types.NewValidatorSet(validators) - nextVals := types.TM2PB.ValidatorUpdates(validatorSet) - csParams := types.TM2PB.ConsensusParams(h.genDoc.ConsensusParams) - req := abci.RequestInitChain{ - Time: h.genDoc.GenesisTime, - ChainId: h.genDoc.ChainID, - InitialHeight: h.genDoc.InitialHeight, - ConsensusParams: csParams, - Validators: nextVals, - AppStateBytes: h.genDoc.AppState, - } - res, err := proxyApp.Consensus().InitChainSync(req) - if err != nil { - return nil, err - } - - appHash = res.AppHash - - if stateBlockHeight == 0 { // we only update state when we are in initial state - // If the app did not return an app hash, we keep the one set from the genesis doc in - // the state. We don't set appHash since we don't want the genesis doc app hash - // recorded in the genesis block. We should probably just remove GenesisDoc.AppHash. - if len(res.AppHash) > 0 { - state.AppHash = res.AppHash - } - // If the app returned validators or consensus params, update the state. - if len(res.Validators) > 0 { - vals, err := types.PB2TM.ValidatorUpdates(res.Validators) - if err != nil { - return nil, err - } - state.Validators = types.NewValidatorSet(vals) - state.NextValidators = types.NewValidatorSet(vals).CopyIncrementProposerPriority(1) - } else if len(h.genDoc.Validators) == 0 { - // If validator set is not set in genesis and still empty after InitChain, exit. - return nil, fmt.Errorf("validator set is nil in genesis and still empty after InitChain") - } - - if res.ConsensusParams != nil { - state.ConsensusParams = types.UpdateConsensusParams(state.ConsensusParams, res.ConsensusParams) - state.Version.Consensus.App = state.ConsensusParams.Version.AppVersion - } - // We update the last results hash with the empty hash, to conform with RFC-6962. - state.LastResultsHash = merkle.HashFromByteSlices(nil) - if err := h.stateStore.Save(state); err != nil { - return nil, err - } - } - } - - // First handle edge cases and constraints on the storeBlockHeight and storeBlockBase. - switch { - case storeBlockHeight == 0: - assertAppHashEqualsOneFromState(appHash, state) - return appHash, nil - - case appBlockHeight == 0 && state.InitialHeight < storeBlockBase: - // the app has no state, and the block store is truncated above the initial height - return appHash, sm.ErrAppBlockHeightTooLow{AppHeight: appBlockHeight, StoreBase: storeBlockBase} - - case appBlockHeight > 0 && appBlockHeight < storeBlockBase-1: - // the app is too far behind truncated store (can be 1 behind since we replay the next) - return appHash, sm.ErrAppBlockHeightTooLow{AppHeight: appBlockHeight, StoreBase: storeBlockBase} - - case storeBlockHeight < appBlockHeight: - // the app should never be ahead of the store (but this is under app's control) - return appHash, sm.ErrAppBlockHeightTooHigh{CoreHeight: storeBlockHeight, AppHeight: appBlockHeight} - - case storeBlockHeight < stateBlockHeight: - // the state should never be ahead of the store (this is under CometBFT's control) - panic(fmt.Sprintf("StateBlockHeight (%d) > StoreBlockHeight (%d)", stateBlockHeight, storeBlockHeight)) - - case storeBlockHeight > stateBlockHeight+1: - // store should be at most one ahead of the state (this is under CometBFT's control) - panic(fmt.Sprintf("StoreBlockHeight (%d) > StateBlockHeight + 1 (%d)", storeBlockHeight, stateBlockHeight+1)) - } - - var err error - // Now either store is equal to state, or one ahead. - // For each, consider all cases of where the app could be, given app <= store - if storeBlockHeight == stateBlockHeight { - // CometBFT ran Commit and saved the state. - // Either the app is asking for replay, or we're all synced up. - if appBlockHeight < storeBlockHeight { - // the app is behind, so replay blocks, but no need to go through WAL (state is already synced to store) - return h.replayBlocks(state, proxyApp, appBlockHeight, storeBlockHeight, false) - - } else if appBlockHeight == storeBlockHeight { - // We're good! - assertAppHashEqualsOneFromState(appHash, state) - return appHash, nil - } - - } else if storeBlockHeight == stateBlockHeight+1 { - // We saved the block in the store but haven't updated the state, - // so we'll need to replay a block using the WAL. - switch { - case appBlockHeight < stateBlockHeight: - // the app is further behind than it should be, so replay blocks - // but leave the last block to go through the WAL - return h.replayBlocks(state, proxyApp, appBlockHeight, storeBlockHeight, true) - - case appBlockHeight == stateBlockHeight: - // We haven't run Commit (both the state and app are one block behind), - // so replayBlock with the real app. - // NOTE: We could instead use the cs.WAL on cs.Start, - // but we'd have to allow the WAL to replay a block that wrote it's #ENDHEIGHT - h.logger.Info("Replay last block using real app") - state, err = h.replayBlock(state, storeBlockHeight, proxyApp.Consensus()) - return state.AppHash, err - - case appBlockHeight == storeBlockHeight: - // We ran Commit, but didn't save the state, so replayBlock with mock app. - abciResponses, err := h.stateStore.LoadABCIResponses(storeBlockHeight) - if err != nil { - return nil, err - } - mockApp := newMockProxyApp(appHash, abciResponses) - h.logger.Info("Replay last block using mock app") - state, err = h.replayBlock(state, storeBlockHeight, mockApp) - return state.AppHash, err - } - - } - - panic(fmt.Sprintf("uncovered case! appHeight: %d, storeHeight: %d, stateHeight: %d", - appBlockHeight, storeBlockHeight, stateBlockHeight)) -} - -func (h *Handshaker) replayBlocks( - state sm.State, - proxyApp proxy.AppConns, - appBlockHeight, - storeBlockHeight int64, - mutateState bool) ([]byte, error) { - // App is further behind than it should be, so we need to replay blocks. - // We replay all blocks from appBlockHeight+1. - // - // Note that we don't have an old version of the state, - // so we by-pass state validation/mutation using sm.ExecCommitBlock. - // This also means we won't be saving validator sets if they change during this period. - // TODO: Load the historical information to fix this and just use state.ApplyBlock - // - // If mutateState == true, the final block is replayed with h.replayBlock() - - var appHash []byte - var err error - finalBlock := storeBlockHeight - if mutateState { - finalBlock-- - } - firstBlock := appBlockHeight + 1 - if firstBlock == 1 { - firstBlock = state.InitialHeight - } - for i := firstBlock; i <= finalBlock; i++ { - h.logger.Info("Applying block", "height", i) - block := h.store.LoadBlock(i) - // Extra check to ensure the app was not changed in a way it shouldn't have. - if len(appHash) > 0 { - assertAppHashEqualsOneFromBlock(appHash, block) - } - - appHash, err = sm.ExecCommitBlock(proxyApp.Consensus(), block, h.logger, h.stateStore, h.genDoc.InitialHeight) - if err != nil { - return nil, err - } - - h.nBlocks++ - } - - if mutateState { - // sync the final block - state, err = h.replayBlock(state, storeBlockHeight, proxyApp.Consensus()) - if err != nil { - return nil, err - } - appHash = state.AppHash - } - - assertAppHashEqualsOneFromState(appHash, state) - return appHash, nil -} - -// ApplyBlock on the proxyApp with the last block. -func (h *Handshaker) replayBlock(state sm.State, height int64, proxyApp proxy.AppConnConsensus) (sm.State, error) { - block := h.store.LoadBlock(height) - seenCommit := h.store.LoadSeenCommit(height) - meta := h.store.LoadBlockMeta(height) - - // Use stubs for both mempool and evidence pool since no transactions nor - // evidence are needed here - block already exists. - blockExec := sm.NewBlockExecutor(h.stateStore, h.logger, proxyApp, emptyMempool{}, sm.EmptyEvidencePool{}, sm.WithBlockStore(h.store)) - blockExec.SetEventBus(h.eventBus) - - var err error - state, _, err = blockExec.ApplyBlock(state, meta.BlockID, block, seenCommit) - if err != nil { - return sm.State{}, err - } - - h.nBlocks++ - - return state, nil -} - -func assertAppHashEqualsOneFromBlock(appHash []byte, block *types.Block) { - if !bytes.Equal(appHash, block.AppHash) { - panic(fmt.Sprintf(`block.AppHash does not match AppHash after replay. Got %X, expected %X. - -Block: %v -`, - appHash, block.AppHash, block)) - } -} - -func assertAppHashEqualsOneFromState(appHash []byte, state sm.State) { - if !bytes.Equal(appHash, state.AppHash) { - panic(fmt.Sprintf(`state.AppHash does not match AppHash after replay. Got -%X, expected %X. - -State: %v - -Did you reset CometBFT without resetting your application's data?`, - appHash, state.AppHash, state)) - } -} diff --git a/test/maverick/consensus/replay_file.go b/test/maverick/consensus/replay_file.go deleted file mode 100644 index c9f2b54aa5..0000000000 --- a/test/maverick/consensus/replay_file.go +++ /dev/null @@ -1,341 +0,0 @@ -package consensus - -import ( - "bufio" - "context" - "errors" - "fmt" - "io" - "os" - "strconv" - "strings" - - dbm "github.com/cometbft/cometbft-db" - - cfg "github.com/tendermint/tendermint/config" - cmtcon "github.com/tendermint/tendermint/consensus" - "github.com/tendermint/tendermint/libs/log" - cmtos "github.com/tendermint/tendermint/libs/os" - "github.com/tendermint/tendermint/proxy" - sm "github.com/tendermint/tendermint/state" - "github.com/tendermint/tendermint/store" - "github.com/tendermint/tendermint/types" -) - -const ( - // event bus subscriber - subscriber = "replay-file" -) - -//-------------------------------------------------------- -// replay messages interactively or all at once - -// replay the wal file -func RunReplayFile(config cfg.BaseConfig, csConfig *cfg.ConsensusConfig, console bool) { - consensusState := newConsensusStateForReplay(config, csConfig) - - if err := consensusState.ReplayFile(csConfig.WalFile(), console); err != nil { - cmtos.Exit(fmt.Sprintf("Error during consensus replay: %v", err)) - } -} - -// Replay msgs in file or start the console -func (cs *State) ReplayFile(file string, console bool) error { - - if cs.IsRunning() { - return errors.New("cs is already running, cannot replay") - } - if cs.wal != nil { - return errors.New("cs wal is open, cannot replay") - } - - cs.startForReplay() - - // ensure all new step events are regenerated as expected - - ctx := context.Background() - newStepSub, err := cs.eventBus.Subscribe(ctx, subscriber, types.EventQueryNewRoundStep) - if err != nil { - return fmt.Errorf("failed to subscribe %s to %v", subscriber, types.EventQueryNewRoundStep) - } - defer func() { - if err := cs.eventBus.Unsubscribe(ctx, subscriber, types.EventQueryNewRoundStep); err != nil { - cs.Logger.Error("Error unsubscribing to event bus", "err", err) - } - }() - - // just open the file for reading, no need to use wal - fp, err := os.OpenFile(file, os.O_RDONLY, 0600) - if err != nil { - return err - } - - pb := newPlayback(file, fp, cs, cs.state.Copy()) - defer pb.fp.Close() - - var nextN int // apply N msgs in a row - var msg *cmtcon.TimedWALMessage - for { - if nextN == 0 && console { - nextN = pb.replayConsoleLoop() - } - - msg, err = pb.dec.Decode() - if err == io.EOF { - return nil - } else if err != nil { - return err - } - - if err := pb.cs.readReplayMessage(msg, newStepSub); err != nil { - return err - } - - if nextN > 0 { - nextN-- - } - pb.count++ - } -} - -//------------------------------------------------ -// playback manager - -type playback struct { - cs *State - - fp *os.File - dec *WALDecoder - count int // how many lines/msgs into the file are we - - // replays can be reset to beginning - fileName string // so we can close/reopen the file - genesisState sm.State // so the replay session knows where to restart from -} - -func newPlayback(fileName string, fp *os.File, cs *State, genState sm.State) *playback { - return &playback{ - cs: cs, - fp: fp, - fileName: fileName, - genesisState: genState, - dec: NewWALDecoder(fp), - } -} - -// go back count steps by resetting the state and running (pb.count - count) steps -func (pb *playback) replayReset(count int, newStepSub types.Subscription) error { - if err := pb.cs.Stop(); err != nil { - return err - } - pb.cs.Wait() - - newCS := NewState(pb.cs.config, pb.genesisState.Copy(), pb.cs.blockExec, - pb.cs.blockStore, pb.cs.txNotifier, pb.cs.evpool, map[int64]Misbehavior{}) - newCS.SetEventBus(pb.cs.eventBus) - newCS.startForReplay() - - if err := pb.fp.Close(); err != nil { - return err - } - fp, err := os.OpenFile(pb.fileName, os.O_RDONLY, 0600) - if err != nil { - return err - } - pb.fp = fp - pb.dec = NewWALDecoder(fp) - count = pb.count - count - fmt.Printf("Reseting from %d to %d\n", pb.count, count) - pb.count = 0 - pb.cs = newCS - var msg *cmtcon.TimedWALMessage - for i := 0; i < count; i++ { - msg, err = pb.dec.Decode() - if err == io.EOF { - return nil - } else if err != nil { - return err - } - if err := pb.cs.readReplayMessage(msg, newStepSub); err != nil { - return err - } - pb.count++ - } - return nil -} - -func (cs *State) startForReplay() { - cs.Logger.Error("Replay commands are disabled until someone updates them and writes tests") - /* TODO:! - // since we replay tocks we just ignore ticks - go func() { - for { - select { - case <-cs.tickChan: - case <-cs.Quit: - return - } - } - }()*/ -} - -// console function for parsing input and running commands -func (pb *playback) replayConsoleLoop() int { - for { - fmt.Printf("> ") - bufReader := bufio.NewReader(os.Stdin) - line, more, err := bufReader.ReadLine() - if more { - cmtos.Exit("input is too long") - } else if err != nil { - cmtos.Exit(err.Error()) - } - - tokens := strings.Split(string(line), " ") - if len(tokens) == 0 { - continue - } - - switch tokens[0] { - case "next": - // "next" -> replay next message - // "next N" -> replay next N messages - - if len(tokens) == 1 { - return 0 - } - i, err := strconv.Atoi(tokens[1]) - if err != nil { - fmt.Println("next takes an integer argument") - } else { - return i - } - - case "back": - // "back" -> go back one message - // "back N" -> go back N messages - - // NOTE: "back" is not supported in the state machine design, - // so we restart and replay up to - - ctx := context.Background() - // ensure all new step events are regenerated as expected - - newStepSub, err := pb.cs.eventBus.Subscribe(ctx, subscriber, types.EventQueryNewRoundStep) - if err != nil { - cmtos.Exit(fmt.Sprintf("failed to subscribe %s to %v", subscriber, types.EventQueryNewRoundStep)) - } - defer func() { - if err := pb.cs.eventBus.Unsubscribe(ctx, subscriber, types.EventQueryNewRoundStep); err != nil { - pb.cs.Logger.Error("Error unsubscribing from eventBus", "err", err) - } - }() - - if len(tokens) == 1 { - if err := pb.replayReset(1, newStepSub); err != nil { - pb.cs.Logger.Error("Replay reset error", "err", err) - } - } else { - i, err := strconv.Atoi(tokens[1]) - if err != nil { - fmt.Println("back takes an integer argument") - } else if i > pb.count { - fmt.Printf("argument to back must not be larger than the current count (%d)\n", pb.count) - } else if err := pb.replayReset(i, newStepSub); err != nil { - pb.cs.Logger.Error("Replay reset error", "err", err) - } - } - - case "rs": - // "rs" -> print entire round state - // "rs short" -> print height/round/step - // "rs <field>" -> print another field of the round state - - rs := pb.cs.RoundState - if len(tokens) == 1 { - fmt.Println(rs) - } else { - switch tokens[1] { - case "short": - fmt.Printf("%v/%v/%v\n", rs.Height, rs.Round, rs.Step) - case "validators": - fmt.Println(rs.Validators) - case "proposal": - fmt.Println(rs.Proposal) - case "proposal_block": - fmt.Printf("%v %v\n", rs.ProposalBlockParts.StringShort(), rs.ProposalBlock.StringShort()) - case "locked_round": - fmt.Println(rs.LockedRound) - case "locked_block": - fmt.Printf("%v %v\n", rs.LockedBlockParts.StringShort(), rs.LockedBlock.StringShort()) - case "votes": - fmt.Println(rs.Votes.StringIndented(" ")) - - default: - fmt.Println("Unknown option", tokens[1]) - } - } - case "n": - fmt.Println(pb.count) - } - } -} - -//-------------------------------------------------------------------------------- - -// convenience for replay mode -func newConsensusStateForReplay(config cfg.BaseConfig, csConfig *cfg.ConsensusConfig) *State { - dbType := dbm.BackendType(config.DBBackend) - // Get BlockStore - blockStoreDB, err := dbm.NewDB("blockstore", dbType, config.DBDir()) - if err != nil { - cmtos.Exit(err.Error()) - } - blockStore := store.NewBlockStore(blockStoreDB) - - // Get State - stateDB, err := dbm.NewDB("state", dbType, config.DBDir()) - if err != nil { - cmtos.Exit(err.Error()) - } - stateStore := sm.NewStore(stateDB, sm.StoreOptions{ - DiscardABCIResponses: false, - }) - gdoc, err := sm.MakeGenesisDocFromFile(config.GenesisFile()) - if err != nil { - cmtos.Exit(err.Error()) - } - state, err := sm.MakeGenesisState(gdoc) - if err != nil { - cmtos.Exit(err.Error()) - } - - // Create proxyAppConn connection (consensus, mempool, query) - clientCreator := proxy.DefaultClientCreator(config.ProxyApp, config.ABCI, config.DBDir()) - proxyApp := proxy.NewAppConns(clientCreator) - err = proxyApp.Start() - if err != nil { - cmtos.Exit(fmt.Sprintf("Error starting proxy app conns: %v", err)) - } - - eventBus := types.NewEventBus() - if err := eventBus.Start(); err != nil { - cmtos.Exit(fmt.Sprintf("Failed to start event bus: %v", err)) - } - - handshaker := NewHandshaker(stateStore, state, blockStore, gdoc) - handshaker.SetEventBus(eventBus) - err = handshaker.Handshake(proxyApp) - if err != nil { - cmtos.Exit(fmt.Sprintf("Error on handshake: %v", err)) - } - - mempool, evpool := emptyMempool{}, sm.EmptyEvidencePool{} - blockExec := sm.NewBlockExecutor(stateStore, log.TestingLogger(), proxyApp.Consensus(), mempool, evpool) - - consensusState := NewState(csConfig, state.Copy(), blockExec, - blockStore, mempool, evpool, map[int64]Misbehavior{}) - - consensusState.SetEventBus(eventBus) - return consensusState -} diff --git a/test/maverick/consensus/replay_stubs.go b/test/maverick/consensus/replay_stubs.go deleted file mode 100644 index ea0d122bab..0000000000 --- a/test/maverick/consensus/replay_stubs.go +++ /dev/null @@ -1,95 +0,0 @@ -package consensus - -import ( - abci "github.com/tendermint/tendermint/abci/types" - "github.com/tendermint/tendermint/libs/clist" - mempl "github.com/tendermint/tendermint/mempool" - cmtstate "github.com/tendermint/tendermint/proto/tendermint/state" - "github.com/tendermint/tendermint/proxy" - "github.com/tendermint/tendermint/types" -) - -//----------------------------------------------------------------------------- - -type emptyMempool struct{} - -var _ mempl.Mempool = emptyMempool{} - -func (emptyMempool) Lock() {} -func (emptyMempool) Unlock() {} -func (emptyMempool) Size() int { return 0 } -func (emptyMempool) SizeBytes() int64 { return 0 } -func (emptyMempool) CheckTx(_ types.Tx, _ func(*abci.Response), _ mempl.TxInfo) error { - return nil -} -func (emptyMempool) RemoveTxByKey(txKey types.TxKey) error { return nil } -func (emptyMempool) ReapMaxBytesMaxGas(_, _ int64) types.Txs { return types.Txs{} } -func (emptyMempool) ReapMaxTxs(n int) types.Txs { return types.Txs{} } -func (emptyMempool) Update( - _ int64, - _ types.Txs, - _ []*abci.ResponseDeliverTx, - _ mempl.PreCheckFunc, - _ mempl.PostCheckFunc, -) error { - return nil -} -func (emptyMempool) Flush() {} -func (emptyMempool) FlushAppConn() error { return nil } -func (emptyMempool) TxsAvailable() <-chan struct{} { return make(chan struct{}) } -func (emptyMempool) EnableTxsAvailable() {} -func (emptyMempool) TxsBytes() int64 { return 0 } - -func (emptyMempool) GetTxByKey(txKey types.TxKey) (types.Tx, bool) { return nil, false } -func (emptyMempool) WasRecentlyEvicted(txKey types.TxKey) bool { return false } - -func (emptyMempool) TxsFront() *clist.CElement { return nil } -func (emptyMempool) TxsWaitChan() <-chan struct{} { return nil } - -func (emptyMempool) InitWAL() error { return nil } -func (emptyMempool) CloseWAL() {} - -//----------------------------------------------------------------------------- -// mockProxyApp uses ABCIResponses to give the right results. -// -// Useful because we don't want to call Commit() twice for the same block on -// the real app. - -func newMockProxyApp(appHash []byte, abciResponses *cmtstate.ABCIResponses) proxy.AppConnConsensus { - clientCreator := proxy.NewLocalClientCreator(&mockProxyApp{ - appHash: appHash, - abciResponses: abciResponses, - }) - cli, _ := clientCreator.NewABCIClient() - err := cli.Start() - if err != nil { - panic(err) - } - return proxy.NewAppConnConsensus(cli) -} - -type mockProxyApp struct { - abci.BaseApplication - - appHash []byte - txCount int - abciResponses *cmtstate.ABCIResponses -} - -func (mock *mockProxyApp) DeliverTx(req abci.RequestDeliverTx) abci.ResponseDeliverTx { - r := mock.abciResponses.DeliverTxs[mock.txCount] - mock.txCount++ - if r == nil { - return abci.ResponseDeliverTx{} - } - return *r -} - -func (mock *mockProxyApp) EndBlock(req abci.RequestEndBlock) abci.ResponseEndBlock { - mock.txCount = 0 - return *mock.abciResponses.EndBlock -} - -func (mock *mockProxyApp) Commit() abci.ResponseCommit { - return abci.ResponseCommit{Data: mock.appHash} -} diff --git a/test/maverick/consensus/state.go b/test/maverick/consensus/state.go deleted file mode 100644 index 8edab1b6e3..0000000000 --- a/test/maverick/consensus/state.go +++ /dev/null @@ -1,1996 +0,0 @@ -package consensus - -import ( - "bytes" - "errors" - "fmt" - "io" - "os" - "reflect" - "runtime/debug" - "sync" - "time" - - "github.com/gogo/protobuf/proto" - - cfg "github.com/tendermint/tendermint/config" - cmtcon "github.com/tendermint/tendermint/consensus" - cstypes "github.com/tendermint/tendermint/consensus/types" - "github.com/tendermint/tendermint/crypto" - cmtevents "github.com/tendermint/tendermint/libs/events" - "github.com/tendermint/tendermint/libs/fail" - cmtjson "github.com/tendermint/tendermint/libs/json" - "github.com/tendermint/tendermint/libs/log" - cmtmath "github.com/tendermint/tendermint/libs/math" - cmtos "github.com/tendermint/tendermint/libs/os" - "github.com/tendermint/tendermint/libs/service" - "github.com/tendermint/tendermint/p2p" - cmtproto "github.com/tendermint/tendermint/proto/tendermint/types" - sm "github.com/tendermint/tendermint/state" - "github.com/tendermint/tendermint/types" - cmttime "github.com/tendermint/tendermint/types/time" -) - -// State handles execution of the consensus algorithm. -// It processes votes and proposals, and upon reaching agreement, -// commits blocks to the chain and executes them against the application. -// The internal state machine receives input from peers, the internal validator, and from a timer. -type State struct { - service.BaseService - - // config details - config *cfg.ConsensusConfig - privValidator types.PrivValidator // for signing votes - - // store blocks and commits - blockStore sm.BlockStore - - // create and execute blocks - blockExec *sm.BlockExecutor - - // notify us if txs are available - txNotifier txNotifier - - // add evidence to the pool - // when it's detected - evpool evidencePool - - // internal state - mtx sync.RWMutex - cstypes.RoundState - state sm.State // State until height-1. - - // state changes may be triggered by: msgs from peers, - // msgs from ourself, or by timeouts - peerMsgQueue chan msgInfo - internalMsgQueue chan msgInfo - timeoutTicker TimeoutTicker - // privValidator pubkey, memoized for the duration of one block - // to avoid extra requests to HSM - privValidatorPubKey crypto.PubKey - - // information about about added votes and block parts are written on this channel - // so statistics can be computed by reactor - statsMsgQueue chan msgInfo - - // we use eventBus to trigger msg broadcasts in the reactor, - // and to notify external subscribers, eg. through a websocket - eventBus *types.EventBus - - // a Write-Ahead Log ensures we can recover from any kind of crash - // and helps us avoid signing conflicting votes - wal cmtcon.WAL - replayMode bool // so we don't log signing errors during replay - doWALCatchup bool // determines if we even try to do the catchup - - // for tests where we want to limit the number of transitions the state makes - nSteps int - - // some functions can be overwritten for testing - decideProposal func(height int64, round int32) - - // closed when we finish shutting down - done chan struct{} - - // synchronous pubsub between consensus state and reactor. - // state only emits EventNewRoundStep and EventVote - evsw cmtevents.EventSwitch - - // for reporting metrics - metrics *cmtcon.Metrics - - // misbehaviors mapped for each height (can't have more than one misbehavior per height) - misbehaviors map[int64]Misbehavior - - // the switch is passed to the state so that maveick misbehaviors can directly control which - // information they send to which nodes - sw *p2p.Switch -} - -// StateOption sets an optional parameter on the State. -type StateOption func(*State) - -// NewState returns a new State. -func NewState( - config *cfg.ConsensusConfig, - state sm.State, - blockExec *sm.BlockExecutor, - blockStore sm.BlockStore, - txNotifier txNotifier, - evpool evidencePool, - misbehaviors map[int64]Misbehavior, - options ...StateOption, -) *State { - cs := &State{ - config: config, - blockExec: blockExec, - blockStore: blockStore, - txNotifier: txNotifier, - peerMsgQueue: make(chan msgInfo, msgQueueSize), - internalMsgQueue: make(chan msgInfo, msgQueueSize), - timeoutTicker: NewTimeoutTicker(), - statsMsgQueue: make(chan msgInfo, msgQueueSize), - done: make(chan struct{}), - doWALCatchup: true, - wal: nilWAL{}, - evpool: evpool, - evsw: cmtevents.NewEventSwitch(), - metrics: cmtcon.NopMetrics(), - misbehaviors: misbehaviors, - } - // set function defaults (may be overwritten before calling Start) - cs.decideProposal = cs.defaultDecideProposal - - // We have no votes, so reconstruct LastCommit from SeenCommit. - if state.LastBlockHeight > 0 { - cs.reconstructLastCommit(state) - } - - cs.updateToState(state) - - // Don't call scheduleRound0 yet. - // We do that upon Start(). - - cs.BaseService = *service.NewBaseService(nil, "State", cs) - for _, option := range options { - option(cs) - } - return cs -} - -// I know this is not great but the maverick consensus state needs access to the peers -func (cs *State) SetSwitch(sw *p2p.Switch) { - cs.sw = sw -} - -// state transitions on complete-proposal, 2/3-any, 2/3-one -func (cs *State) handleMsg(mi msgInfo) { - cs.mtx.Lock() - defer cs.mtx.Unlock() - - var ( - added bool - err error - ) - msg, peerID := mi.Msg, mi.PeerID - switch msg := msg.(type) { - case *cmtcon.ProposalMessage: - // will not cause transition. - // once proposal is set, we can receive block parts - // err = cs.setProposal(msg.Proposal) - if b, ok := cs.misbehaviors[cs.Height]; ok { - err = b.ReceiveProposal(cs, msg.Proposal) - } else { - err = defaultReceiveProposal(cs, msg.Proposal) - } - case *cmtcon.BlockPartMessage: - // if the proposal is complete, we'll enterPrevote or tryFinalizeCommit - added, err = cs.addProposalBlockPart(msg, peerID) - if added { - cs.statsMsgQueue <- mi - } - - if err != nil && msg.Round != cs.Round { - cs.Logger.Debug( - "Received block part from wrong round", - "height", - cs.Height, - "csRound", - cs.Round, - "blockRound", - msg.Round) - err = nil - } - case *cmtcon.VoteMessage: - // attempt to add the vote and dupeout the validator if its a duplicate signature - // if the vote gives us a 2/3-any or 2/3-one, we transition - added, err = cs.tryAddVote(msg.Vote, peerID) - if added { - cs.statsMsgQueue <- mi - } - - // if err == ErrAddingVote { - // TODO: punish peer - // We probably don't want to stop the peer here. The vote does not - // necessarily comes from a malicious peer but can be just broadcasted by - // a typical peer. - // https://github.com/tendermint/tendermint/issues/1281 - // } - - // NOTE: the vote is broadcast to peers by the reactor listening - // for vote events - - // TODO: If rs.Height == vote.Height && rs.Round < vote.Round, - // the peer is sending us CatchupCommit precommits. - // We could make note of this and help filter in broadcastHasVoteMessage(). - default: - cs.Logger.Error("Unknown msg type", "type", reflect.TypeOf(msg)) - return - } - - if err != nil { - cs.Logger.Error("Error with msg", "height", cs.Height, "round", cs.Round, - "peer", peerID, "err", err, "msg", msg) - } -} - -// Enter (CreateEmptyBlocks): from enterNewRound(height,round) -// Enter (CreateEmptyBlocks, CreateEmptyBlocksInterval > 0 ): -// -// after enterNewRound(height,round), after timeout of CreateEmptyBlocksInterval -// -// Enter (!CreateEmptyBlocks) : after enterNewRound(height,round), once txs are in the mempool -func (cs *State) enterPropose(height int64, round int32) { - logger := cs.Logger.With("height", height, "round", round) - - if cs.Height != height || round < cs.Round || (cs.Round == round && cstypes.RoundStepPropose <= cs.Step) { - logger.Debug("enter propose", "msg", log.NewLazySprintf( - "enterPropose(%v/%v): Invalid args. Current step: %v/%v/%v", - height, - round, - cs.Height, - cs.Round, - cs.Step)) - return - } - logger.Info("enter propose", - "msg", - log.NewLazySprintf("enterPropose(%v/%v). Current: %v/%v/%v", height, round, cs.Height, cs.Round, cs.Step)) - - defer func() { - // Done enterPropose: - cs.updateRoundStep(round, cstypes.RoundStepPropose) - cs.newStep() - - // If we have the whole proposal + POL, then goto Prevote now. - // else, we'll enterPrevote when the rest of the proposal is received (in AddProposalBlockPart), - // or else after timeoutPropose - if cs.isProposalComplete() { - cs.enterPrevote(height, cs.Round) - } - }() - - if b, ok := cs.misbehaviors[cs.Height]; ok { - b.EnterPropose(cs, height, round) - } else { - defaultEnterPropose(cs, height, round) - } -} - -// Enter: `timeoutPropose` after entering Propose. -// Enter: proposal block and POL is ready. -// Prevote for LockedBlock if we're locked, or ProposalBlock if valid. -// Otherwise vote nil. -func (cs *State) enterPrevote(height int64, round int32) { - if cs.Height != height || round < cs.Round || (cs.Round == round && cstypes.RoundStepPrevote <= cs.Step) { - cs.Logger.Debug("enter prevote", "msg", log.NewLazySprintf( - "enterPrevote(%v/%v): Invalid args. Current step: %v/%v/%v", - height, - round, - cs.Height, - cs.Round, - cs.Step)) - return - } - - defer func() { - // Done enterPrevote: - cs.updateRoundStep(round, cstypes.RoundStepPrevote) - cs.newStep() - }() - - cs.Logger.Debug("enter prevote", - "msg", - log.NewLazySprintf("enterPrevote(%v/%v); current: %v/%v/%v", height, round, cs.Height, cs.Round, cs.Step)) - - // Sign and broadcast vote as necessary - if b, ok := cs.misbehaviors[cs.Height]; ok { - b.EnterPrevote(cs, height, round) - } else { - defaultEnterPrevote(cs, height, round) - } - - // Once `addVote` hits any +2/3 prevotes, we will go to PrevoteWait - // (so we have more time to try and collect +2/3 prevotes for a single block) -} - -// Enter: `timeoutPrevote` after any +2/3 prevotes. -// Enter: `timeoutPrecommit` after any +2/3 precommits. -// Enter: +2/3 precomits for block or nil. -// Lock & precommit the ProposalBlock if we have enough prevotes for it (a POL in this round) -// else, unlock an existing lock and precommit nil if +2/3 of prevotes were nil, -// else, precommit nil otherwise. -func (cs *State) enterPrecommit(height int64, round int32) { - logger := cs.Logger.With("height", height, "round", round) - - if cs.Height != height || round < cs.Round || (cs.Round == round && cstypes.RoundStepPrecommit <= cs.Step) { - logger.Debug("enter precommit", - "msg", - log.NewLazySprintf("enterPrecommit(%v/%v): Invalid args. Current step: %v/%v/%v", - height, - round, - cs.Height, - cs.Round, - cs.Step)) - return - } - - logger.Info("enter precommit", - "msg", - log.NewLazySprintf("enterPrecommit(%v/%v). Current: %v/%v/%v", height, round, cs.Height, cs.Round, cs.Step)) - - defer func() { - // Done enterPrecommit: - cs.updateRoundStep(round, cstypes.RoundStepPrecommit) - cs.newStep() - }() - - if b, ok := cs.misbehaviors[cs.Height]; ok { - b.EnterPrecommit(cs, height, round) - } else { - defaultEnterPrecommit(cs, height, round) - } -} - -func (cs *State) addVote( - vote *types.Vote, - peerID p2p.ID, -) (added bool, err error) { - cs.Logger.Debug( - "addVote", - "voteHeight", - vote.Height, - "voteType", - vote.Type, - "valIndex", - vote.ValidatorIndex, - "csHeight", - cs.Height, - ) - - // A precommit for the previous height? - // These come in while we wait timeoutCommit - if vote.Height+1 == cs.Height && vote.Type == cmtproto.PrecommitType { - if cs.Step != cstypes.RoundStepNewHeight { - // Late precommit at prior height is ignored - cs.Logger.Debug("Precommit vote came in after commit timeout and has been ignored", "vote", vote) - return - } - added, err = cs.LastCommit.AddVote(vote) - if !added { - return - } - - cs.Logger.Info("add vote", - "msg", - log.NewLazySprintf("Added to lastPrecommits: %v", cs.LastCommit.StringShort())) - _ = cs.eventBus.PublishEventVote(types.EventDataVote{Vote: vote}) - cs.evsw.FireEvent(types.EventVote, vote) - - // if we can skip timeoutCommit and have all the votes now, - if cs.config.SkipTimeoutCommit && cs.LastCommit.HasAll() { - // go straight to new round (skip timeout commit) - // cs.scheduleTimeout(time.Duration(0), cs.Height, 0, cstypes.RoundStepNewHeight) - cs.enterNewRound(cs.Height, 0) - } - - return - } - - // Height mismatch is ignored. - // Not necessarily a bad peer, but not favourable behaviour. - if vote.Height != cs.Height { - cs.Logger.Debug("vote ignored and not added", "voteHeight", vote.Height, "csHeight", cs.Height, "peerID", peerID) - return - } - - added, err = cs.Votes.AddVote(vote, peerID) - if !added { - // Either duplicate, or error upon cs.Votes.AddByIndex() - return - } - - _ = cs.eventBus.PublishEventVote(types.EventDataVote{Vote: vote}) - cs.evsw.FireEvent(types.EventVote, vote) - - switch vote.Type { - case cmtproto.PrevoteType: - if b, ok := cs.misbehaviors[cs.Height]; ok { - b.ReceivePrevote(cs, vote) - } else { - defaultReceivePrevote(cs, vote) - } - - case cmtproto.PrecommitType: - if b, ok := cs.misbehaviors[cs.Height]; ok { - b.ReceivePrecommit(cs, vote) - } - defaultReceivePrecommit(cs, vote) - - default: - panic(fmt.Sprintf("Unexpected vote type %v", vote.Type)) - } - - return added, err -} - -//----------------------------------------------------------------------------- -// Errors - -var ( - ErrInvalidProposalSignature = errors.New("error invalid proposal signature") - ErrInvalidProposalPOLRound = errors.New("error invalid proposal POL round") - ErrAddingVote = errors.New("error adding vote") - ErrSignatureFoundInPastBlocks = errors.New("found signature from the same key") - - errPubKeyIsNotSet = errors.New("pubkey is not set. Look for \"Can't get private validator pubkey\" errors") -) - -//----------------------------------------------------------------------------- - -var msgQueueSize = 1000 - -// msgs from the reactor which may update the state -type msgInfo struct { - Msg cmtcon.Message `json:"msg"` - PeerID p2p.ID `json:"peer_key"` -} - -// internally generated messages which may update the state -type timeoutInfo struct { - Duration time.Duration `json:"duration"` - Height int64 `json:"height"` - Round int32 `json:"round"` - Step cstypes.RoundStepType `json:"step"` -} - -func (ti *timeoutInfo) String() string { - return fmt.Sprintf("%v ; %d/%d %v", ti.Duration, ti.Height, ti.Round, ti.Step) -} - -// interface to the mempool -type txNotifier interface { - TxsAvailable() <-chan struct{} -} - -// interface to the evidence pool -type evidencePool interface { - // reports conflicting votes to the evidence pool to be processed into evidence - ReportConflictingVotes(voteA, voteB *types.Vote) -} - -//---------------------------------------- -// Public interface - -// SetLogger implements Service. -func (cs *State) SetLogger(l log.Logger) { - cs.BaseService.Logger = l - cs.timeoutTicker.SetLogger(l) -} - -// SetEventBus sets event bus. -func (cs *State) SetEventBus(b *types.EventBus) { - cs.eventBus = b - cs.blockExec.SetEventBus(b) -} - -// StateMetrics sets the metrics. -func StateMetrics(metrics *cmtcon.Metrics) StateOption { - return func(cs *State) { cs.metrics = metrics } -} - -// String returns a string. -func (cs *State) String() string { - // better not to access shared variables - return "ConsensusState" -} - -// GetState returns a copy of the chain state. -func (cs *State) GetState() sm.State { - cs.mtx.RLock() - defer cs.mtx.RUnlock() - return cs.state.Copy() -} - -// GetLastHeight returns the last height committed. -// If there were no blocks, returns 0. -func (cs *State) GetLastHeight() int64 { - cs.mtx.RLock() - defer cs.mtx.RUnlock() - return cs.RoundState.Height - 1 -} - -// GetRoundState returns a shallow copy of the internal consensus state. -func (cs *State) GetRoundState() *cstypes.RoundState { - cs.mtx.RLock() - rs := cs.RoundState // copy - cs.mtx.RUnlock() - return &rs -} - -// GetRoundStateJSON returns a json of RoundState. -func (cs *State) GetRoundStateJSON() ([]byte, error) { - cs.mtx.RLock() - defer cs.mtx.RUnlock() - return cmtjson.Marshal(cs.RoundState) -} - -// GetRoundStateSimpleJSON returns a json of RoundStateSimple -func (cs *State) GetRoundStateSimpleJSON() ([]byte, error) { - cs.mtx.RLock() - defer cs.mtx.RUnlock() - return cmtjson.Marshal(cs.RoundState.RoundStateSimple()) -} - -// GetValidators returns a copy of the current validators. -func (cs *State) GetValidators() (int64, []*types.Validator) { - cs.mtx.RLock() - defer cs.mtx.RUnlock() - return cs.state.LastBlockHeight, cs.state.Validators.Copy().Validators -} - -// SetPrivValidator sets the private validator account for signing votes. It -// immediately requests pubkey and caches it. -func (cs *State) SetPrivValidator(priv types.PrivValidator) { - cs.mtx.Lock() - defer cs.mtx.Unlock() - - cs.privValidator = priv - - if err := cs.updatePrivValidatorPubKey(); err != nil { - cs.Logger.Error("Can't get private validator pubkey", "err", err) - } -} - -// SetTimeoutTicker sets the local timer. It may be useful to overwrite for testing. -func (cs *State) SetTimeoutTicker(timeoutTicker TimeoutTicker) { - cs.mtx.Lock() - cs.timeoutTicker = timeoutTicker - cs.mtx.Unlock() -} - -// LoadCommit loads the commit for a given height. -func (cs *State) LoadCommit(height int64) *types.Commit { - cs.mtx.RLock() - defer cs.mtx.RUnlock() - if height == cs.blockStore.Height() { - return cs.blockStore.LoadSeenCommit(height) - } - return cs.blockStore.LoadBlockCommit(height) -} - -// OnStart loads the latest state via the WAL, and starts the timeout and -// receive routines. -func (cs *State) OnStart() error { - // We may set the WAL in testing before calling Start, so only OpenWAL if its - // still the nilWAL. - if _, ok := cs.wal.(nilWAL); ok { - if err := cs.loadWalFile(); err != nil { - return err - } - } - - // We may have lost some votes if the process crashed reload from consensus - // log to catchup. - if cs.doWALCatchup { - repairAttempted := false - LOOP: - for { - err := cs.catchupReplay(cs.Height) - switch { - case err == nil: - break LOOP - case !IsDataCorruptionError(err): - cs.Logger.Error("Error on catchup replay. Proceeding to start State anyway", "err", err) - break LOOP - case repairAttempted: - return err - } - - cs.Logger.Info("WAL file is corrupted. Attempting repair", "err", err) - - // 1) prep work - if err := cs.wal.Stop(); err != nil { - return err - } - repairAttempted = true - - // 2) backup original WAL file - corruptedFile := fmt.Sprintf("%s.CORRUPTED", cs.config.WalFile()) - if err := cmtos.CopyFile(cs.config.WalFile(), corruptedFile); err != nil { - return err - } - cs.Logger.Info("Backed up WAL file", "src", cs.config.WalFile(), "dst", corruptedFile) - - // 3) try to repair (WAL file will be overwritten!) - if err := repairWalFile(corruptedFile, cs.config.WalFile()); err != nil { - cs.Logger.Error("Repair failed", "err", err) - return err - } - cs.Logger.Info("Successful repair") - - // reload WAL file - if err := cs.loadWalFile(); err != nil { - return err - } - } - } - - if err := cs.evsw.Start(); err != nil { - return err - } - - // we need the timeoutRoutine for replay so - // we don't block on the tick chan. - // NOTE: we will get a build up of garbage go routines - // firing on the tockChan until the receiveRoutine is started - // to deal with them (by that point, at most one will be valid) - if err := cs.timeoutTicker.Start(); err != nil { - return err - } - - // Double Signing Risk Reduction - if err := cs.checkDoubleSigningRisk(cs.Height); err != nil { - return err - } - - // now start the receiveRoutine - go cs.receiveRoutine(0) - - // schedule the first round! - // use GetRoundState so we don't race the receiveRoutine for access - cs.scheduleRound0(cs.GetRoundState()) - - return nil -} - -// loadWalFile loads WAL data from file. It overwrites cs.wal. -func (cs *State) loadWalFile() error { - wal, err := cs.OpenWAL(cs.config.WalFile()) - if err != nil { - cs.Logger.Error("Error loading State wal", "err", err) - return err - } - cs.wal = wal - return nil -} - -// OnStop implements service.Service. -func (cs *State) OnStop() { - if err := cs.evsw.Stop(); err != nil { - cs.Logger.Error("error trying to stop eventSwitch", "error", err) - } - if err := cs.timeoutTicker.Stop(); err != nil { - cs.Logger.Error("error trying to stop timeoutTicket", "error", err) - } - // WAL is stopped in receiveRoutine. -} - -// Wait waits for the the main routine to return. -// NOTE: be sure to Stop() the event switch and drain -// any event channels or this may deadlock -func (cs *State) Wait() { - <-cs.done -} - -// OpenWAL opens a file to log all consensus messages and timeouts for -// deterministic accountability. -func (cs *State) OpenWAL(walFile string) (cmtcon.WAL, error) { - wal, err := NewWAL(walFile) - if err != nil { - cs.Logger.Error("Failed to open WAL", "file", walFile, "err", err) - return nil, err - } - wal.SetLogger(cs.Logger.With("wal", walFile)) - if err := wal.Start(); err != nil { - cs.Logger.Error("Failed to start WAL", "err", err) - return nil, err - } - return wal, nil -} - -//------------------------------------------------------------ -// Public interface for passing messages into the consensus state, possibly causing a state transition. -// If peerID == "", the msg is considered internal. -// Messages are added to the appropriate queue (peer or internal). -// If the queue is full, the function may block. -// TODO: should these return anything or let callers just use events? - -// AddVote inputs a vote. -func (cs *State) AddVote(vote *types.Vote, peerID p2p.ID) (added bool, err error) { - if peerID == "" { - cs.internalMsgQueue <- msgInfo{&cmtcon.VoteMessage{Vote: vote}, ""} - } else { - cs.peerMsgQueue <- msgInfo{&cmtcon.VoteMessage{Vote: vote}, peerID} - } - - // TODO: wait for event?! - return false, nil -} - -// SetProposal inputs a proposal. -func (cs *State) SetProposal(proposal *types.Proposal, peerID p2p.ID) error { - if peerID == "" { - cs.internalMsgQueue <- msgInfo{&cmtcon.ProposalMessage{Proposal: proposal}, ""} - } else { - cs.peerMsgQueue <- msgInfo{&cmtcon.ProposalMessage{Proposal: proposal}, peerID} - } - - // TODO: wait for event?! - return nil -} - -// AddProposalBlockPart inputs a part of the proposal block. -func (cs *State) AddProposalBlockPart(height int64, round int32, part *types.Part, peerID p2p.ID) error { - if peerID == "" { - cs.internalMsgQueue <- msgInfo{&cmtcon.BlockPartMessage{Height: height, Round: round, Part: part}, ""} - } else { - cs.peerMsgQueue <- msgInfo{&cmtcon.BlockPartMessage{Height: height, Round: round, Part: part}, peerID} - } - - // TODO: wait for event?! - return nil -} - -// SetProposalAndBlock inputs the proposal and all block parts. -func (cs *State) SetProposalAndBlock( - proposal *types.Proposal, - block *types.Block, - parts *types.PartSet, - peerID p2p.ID, -) error { - if err := cs.SetProposal(proposal, peerID); err != nil { - return err - } - for i := 0; i < int(parts.Total()); i++ { - part := parts.GetPart(i) - if err := cs.AddProposalBlockPart(proposal.Height, proposal.Round, part, peerID); err != nil { - return err - } - } - return nil -} - -//------------------------------------------------------------ -// internal functions for managing the state - -func (cs *State) updateHeight(height int64) { - cs.metrics.Height.Set(float64(height)) - cs.Height = height -} - -func (cs *State) updateRoundStep(round int32, step cstypes.RoundStepType) { - cs.Round = round - cs.Step = step -} - -// enterNewRound(height, 0) at cs.StartTime. -func (cs *State) scheduleRound0(rs *cstypes.RoundState) { - // cs.Logger.Info("scheduleRound0", "now", cmttime.Now(), "startTime", cs.StartTime) - sleepDuration := rs.StartTime.Sub(cmttime.Now()) - cs.scheduleTimeout(sleepDuration, rs.Height, 0, cstypes.RoundStepNewHeight) -} - -// Attempt to schedule a timeout (by sending timeoutInfo on the tickChan) -func (cs *State) scheduleTimeout(duration time.Duration, height int64, round int32, step cstypes.RoundStepType) { - cs.timeoutTicker.ScheduleTimeout(timeoutInfo{duration, height, round, step}) -} - -// send a msg into the receiveRoutine regarding our own proposal, block part, or vote -func (cs *State) sendInternalMessage(mi msgInfo) { - select { - case cs.internalMsgQueue <- mi: - default: - // NOTE: using the go-routine means our votes can - // be processed out of order. - // TODO: use CList here for strict determinism and - // attempt push to internalMsgQueue in receiveRoutine - cs.Logger.Info("Internal msg queue is full. Using a go-routine") - go func() { cs.internalMsgQueue <- mi }() - } -} - -// Reconstruct LastCommit from SeenCommit, which we saved along with the block, -// (which happens even before saving the state) -func (cs *State) reconstructLastCommit(state sm.State) { - seenCommit := cs.blockStore.LoadSeenCommit(state.LastBlockHeight) - if seenCommit == nil { - panic(fmt.Sprintf("Failed to reconstruct LastCommit: seen commit for height %v not found", - state.LastBlockHeight)) - } - - lastPrecommits := types.CommitToVoteSet(state.ChainID, seenCommit, state.LastValidators) - if !lastPrecommits.HasTwoThirdsMajority() { - panic("Failed to reconstruct LastCommit: Does not have +2/3 maj") - } - - cs.LastCommit = lastPrecommits -} - -// Updates State and increments height to match that of state. -// The round becomes 0 and cs.Step becomes cstypes.RoundStepNewHeight. -func (cs *State) updateToState(state sm.State) { - if cs.CommitRound > -1 && 0 < cs.Height && cs.Height != state.LastBlockHeight { - panic(fmt.Sprintf("updateToState() expected state height of %v but found %v", - cs.Height, state.LastBlockHeight)) - } - if !cs.state.IsEmpty() { - if cs.state.LastBlockHeight > 0 && cs.state.LastBlockHeight+1 != cs.Height { - // This might happen when someone else is mutating cs.state. - // Someone forgot to pass in state.Copy() somewhere?! - panic(fmt.Sprintf("Inconsistent cs.state.LastBlockHeight+1 %v vs cs.Height %v", - cs.state.LastBlockHeight+1, cs.Height)) - } - if cs.state.LastBlockHeight > 0 && cs.Height == cs.state.InitialHeight { - panic(fmt.Sprintf("Inconsistent cs.state.LastBlockHeight %v, expected 0 for initial height %v", - cs.state.LastBlockHeight, cs.state.InitialHeight)) - } - - // If state isn't further out than cs.state, just ignore. - // This happens when SwitchToConsensus() is called in the reactor. - // We don't want to reset e.g. the Votes, but we still want to - // signal the new round step, because other services (eg. txNotifier) - // depend on having an up-to-date peer state! - if state.LastBlockHeight <= cs.state.LastBlockHeight { - cs.Logger.Info( - "Ignoring updateToState()", - "newHeight", - state.LastBlockHeight+1, - "oldHeight", - cs.state.LastBlockHeight+1) - cs.newStep() - return - } - } - - // Reset fields based on state. - validators := state.Validators - - switch { - case state.LastBlockHeight == 0: // Very first commit should be empty. - cs.LastCommit = (*types.VoteSet)(nil) - case cs.CommitRound > -1 && cs.Votes != nil: // Otherwise, use cs.Votes - if !cs.Votes.Precommits(cs.CommitRound).HasTwoThirdsMajority() { - panic(fmt.Sprintf("Wanted to form a Commit, but Precommits (H/R: %d/%d) didn't have 2/3+: %v", - state.LastBlockHeight, - cs.CommitRound, - cs.Votes.Precommits(cs.CommitRound))) - } - cs.LastCommit = cs.Votes.Precommits(cs.CommitRound) - case cs.LastCommit == nil: - // NOTE: when CometBFT starts, it has no votes. reconstructLastCommit - // must be called to reconstruct LastCommit from SeenCommit. - panic(fmt.Sprintf("LastCommit cannot be empty after initial block (H:%d)", - state.LastBlockHeight+1, - )) - } - - // Next desired block height - height := state.LastBlockHeight + 1 - if height == 1 { - height = state.InitialHeight - } - - // RoundState fields - cs.updateHeight(height) - cs.updateRoundStep(0, cstypes.RoundStepNewHeight) - if cs.CommitTime.IsZero() { - // "Now" makes it easier to sync up dev nodes. - // We add timeoutCommit to allow transactions - // to be gathered for the first block. - // And alternative solution that relies on clocks: - // cs.StartTime = state.LastBlockTime.Add(timeoutCommit) - cs.StartTime = cs.config.Commit(cmttime.Now()) - } else { - cs.StartTime = cs.config.Commit(cs.CommitTime) - } - - cs.Validators = validators - cs.Proposal = nil - cs.ProposalBlock = nil - cs.ProposalBlockParts = nil - cs.LockedRound = -1 - cs.LockedBlock = nil - cs.LockedBlockParts = nil - cs.TwoThirdPrevoteRound = -1 - cs.TwoThirdPrevoteBlock = nil - cs.TwoThirdPrevoteBlockParts = nil - cs.Votes = cstypes.NewHeightVoteSet(state.ChainID, height, validators) - cs.CommitRound = -1 - cs.LastValidators = state.LastValidators - cs.TriggeredTimeoutPrecommit = false - - cs.state = state - - // Finally, broadcast RoundState - cs.newStep() -} - -func (cs *State) newStep() { - rs := cs.RoundStateEvent() - if err := cs.wal.Write(rs); err != nil { - cs.Logger.Error("Error writing to wal", "err", err) - } - cs.nSteps++ - // newStep is called by updateToState in NewState before the eventBus is set! - if cs.eventBus != nil { - if err := cs.eventBus.PublishEventNewRoundStep(rs); err != nil { - cs.Logger.Error("Error publishing new round step", "err", err) - } - cs.evsw.FireEvent(types.EventNewRoundStep, &cs.RoundState) - } -} - -//----------------------------------------- -// the main go routines - -// receiveRoutine handles messages which may cause state transitions. -// it's argument (n) is the number of messages to process before exiting - use 0 to run forever -// It keeps the RoundState and is the only thing that updates it. -// Updates (state transitions) happen on timeouts, complete proposals, and 2/3 majorities. -// State must be locked before any internal state is updated. -func (cs *State) receiveRoutine(maxSteps int) { - onExit := func(cs *State) { - // NOTE: the internalMsgQueue may have signed messages from our - // priv_val that haven't hit the WAL, but its ok because - // priv_val tracks LastSig - - // close wal now that we're done writing to it - if err := cs.wal.Stop(); err != nil { - cs.Logger.Error("error trying to stop wal", "error", err) - } - cs.wal.Wait() - - close(cs.done) - } - - defer func() { - if r := recover(); r != nil { - cs.Logger.Error("CONSENSUS FAILURE!!!", "err", r, "stack", string(debug.Stack())) - // stop gracefully - // - // NOTE: We most probably shouldn't be running any further when there is - // some unexpected panic. Some unknown error happened, and so we don't - // know if that will result in the validator signing an invalid thing. It - // might be worthwhile to explore a mechanism for manual resuming via - // some console or secure RPC system, but for now, halting the chain upon - // unexpected consensus bugs sounds like the better option. - onExit(cs) - } - }() - - for { - if maxSteps > 0 { - if cs.nSteps >= maxSteps { - cs.Logger.Info("reached max steps. exiting receive routine") - cs.nSteps = 0 - return - } - } - rs := cs.RoundState - var mi msgInfo - - select { - case <-cs.txNotifier.TxsAvailable(): - cs.handleTxsAvailable() - case mi = <-cs.peerMsgQueue: - if err := cs.wal.Write(mi); err != nil { - cs.Logger.Error("Error writing to wal", "err", err) - } - // handles proposals, block parts, votes - // may generate internal events (votes, complete proposals, 2/3 majorities) - cs.handleMsg(mi) - case mi = <-cs.internalMsgQueue: - err := cs.wal.WriteSync(mi) // NOTE: fsync - if err != nil { - panic(fmt.Sprintf("Failed to write %v msg to consensus wal due to %v. Check your FS and restart the node", mi, err)) - } - - if _, ok := mi.Msg.(*cmtcon.VoteMessage); ok { - // we actually want to simulate failing during - // the previous WriteSync, but this isn't easy to do. - // Equivalent would be to fail here and manually remove - // some bytes from the end of the wal. - fail.Fail() // XXX - } - - // handles proposals, block parts, votes - cs.handleMsg(mi) - case ti := <-cs.timeoutTicker.Chan(): // tockChan: - if err := cs.wal.Write(ti); err != nil { - cs.Logger.Error("Error writing to wal", "err", err) - } - // if the timeout is relevant to the rs - // go to the next step - cs.handleTimeout(ti, rs) - case <-cs.Quit(): - onExit(cs) - return - } - } -} - -func (cs *State) handleTimeout(ti timeoutInfo, rs cstypes.RoundState) { - cs.Logger.Debug("Received tock", "timeout", ti.Duration, "height", ti.Height, "round", ti.Round, "step", ti.Step) - - // timeouts must be for current height, round, step - if ti.Height != rs.Height || ti.Round < rs.Round || (ti.Round == rs.Round && ti.Step < rs.Step) { - cs.Logger.Debug("Ignoring tock because we're ahead", "height", rs.Height, "round", rs.Round, "step", rs.Step) - return - } - - // the timeout will now cause a state transition - cs.mtx.Lock() - defer cs.mtx.Unlock() - - switch ti.Step { - case cstypes.RoundStepNewHeight: - // NewRound event fired from enterNewRound. - // XXX: should we fire timeout here (for timeout commit)? - cs.enterNewRound(ti.Height, 0) - case cstypes.RoundStepNewRound: - cs.enterPropose(ti.Height, 0) - case cstypes.RoundStepPropose: - if err := cs.eventBus.PublishEventTimeoutPropose(cs.RoundStateEvent()); err != nil { - cs.Logger.Error("Error publishing timeout propose", "err", err) - } - cs.enterPrevote(ti.Height, ti.Round) - case cstypes.RoundStepPrevoteWait: - if err := cs.eventBus.PublishEventTimeoutWait(cs.RoundStateEvent()); err != nil { - cs.Logger.Error("Error publishing timeout wait", "err", err) - } - cs.enterPrecommit(ti.Height, ti.Round) - case cstypes.RoundStepPrecommitWait: - if err := cs.eventBus.PublishEventTimeoutWait(cs.RoundStateEvent()); err != nil { - cs.Logger.Error("Error publishing timeout wait", "err", err) - } - cs.enterPrecommit(ti.Height, ti.Round) - cs.enterNewRound(ti.Height, ti.Round+1) - default: - panic(fmt.Sprintf("Invalid timeout step: %v", ti.Step)) - } -} - -func (cs *State) handleTxsAvailable() { - cs.mtx.Lock() - defer cs.mtx.Unlock() - - // We only need to do this for round 0. - if cs.Round != 0 { - return - } - - switch cs.Step { - case cstypes.RoundStepNewHeight: // timeoutCommit phase - if cs.needProofBlock(cs.Height) { - // enterPropose will be called by enterNewRound - return - } - - // +1ms to ensure RoundStepNewRound timeout always happens after RoundStepNewHeight - timeoutCommit := cs.StartTime.Sub(cmttime.Now()) + 1*time.Millisecond - cs.scheduleTimeout(timeoutCommit, cs.Height, 0, cstypes.RoundStepNewRound) - case cstypes.RoundStepNewRound: // after timeoutCommit - cs.enterPropose(cs.Height, 0) - } -} - -//----------------------------------------------------------------------------- -// State functions -// Used internally by handleTimeout and handleMsg to make state transitions - -// Enter: `timeoutNewHeight` by startTime (commitTime+timeoutCommit), -// -// or, if SkipTimeoutCommit==true, after receiving all precommits from (height,round-1) -// -// Enter: `timeoutPrecommits` after any +2/3 precommits from (height,round-1) -// Enter: +2/3 precommits for nil at (height,round-1) -// Enter: +2/3 prevotes any or +2/3 precommits for block or any from (height, round) -// NOTE: cs.StartTime was already set for height. -func (cs *State) enterNewRound(height int64, round int32) { - logger := cs.Logger.With("height", height, "round", round) - - if cs.Height != height || round < cs.Round || (cs.Round == round && cs.Step != cstypes.RoundStepNewHeight) { - logger.Debug("enter new round", "msg", log.NewLazySprintf( - "enterNewRound(%v/%v): Invalid args. Current step: %v/%v/%v", - height, - round, - cs.Height, - cs.Round, - cs.Step)) - return - } - - if now := cmttime.Now(); cs.StartTime.After(now) { - logger.Debug("need to set a buffer and log message here for sanity", "startTime", cs.StartTime, "now", now) - } - - logger.Info("enter new round", - "msg", - log.NewLazySprintf("enterNewRound(%v/%v). Current: %v/%v/%v", height, round, cs.Height, cs.Round, cs.Step)) - - // Increment validators if necessary - validators := cs.Validators - if cs.Round < round { - validators = validators.Copy() - validators.IncrementProposerPriority(cmtmath.SafeSubInt32(round, cs.Round)) - } - - // Setup new round - // we don't fire newStep for this step, - // but we fire an event, so update the round step first - cs.updateRoundStep(round, cstypes.RoundStepNewRound) - cs.Validators = validators - if round == 0 { - // We've already reset these upon new height, - // and meanwhile we might have received a proposal - // for round 0. - } else { - logger.Info("Resetting Proposal info") - cs.Proposal = nil - cs.ProposalBlock = nil - cs.ProposalBlockParts = nil - } - cs.Votes.SetRound(cmtmath.SafeAddInt32(round, 1)) // also track next round (round+1) to allow round-skipping - cs.TriggeredTimeoutPrecommit = false - - if err := cs.eventBus.PublishEventNewRound(cs.NewRoundEvent()); err != nil { - cs.Logger.Error("Error publishing new round", "err", err) - } - cs.metrics.Rounds.Set(float64(round)) - - // Wait for txs to be available in the mempool - // before we enterPropose in round 0. If the last block changed the app hash, - // we may need an empty "proof" block, and enterPropose immediately. - waitForTxs := cs.config.WaitForTxs() && round == 0 && !cs.needProofBlock(height) - if waitForTxs { - if cs.config.CreateEmptyBlocksInterval > 0 { - cs.scheduleTimeout(cs.config.CreateEmptyBlocksInterval, height, round, - cstypes.RoundStepNewRound) - } - } else { - cs.enterPropose(height, round) - } -} - -// needProofBlock returns true on the first height (so the genesis app hash is signed right away) -// and where the last block (height-1) caused the app hash to change -func (cs *State) needProofBlock(height int64) bool { - if height == cs.state.InitialHeight { - return true - } - - lastBlockMeta := cs.blockStore.LoadBlockMeta(height - 1) - if lastBlockMeta == nil { - panic(fmt.Sprintf("needProofBlock: last block meta for height %d not found", height-1)) - } - return !bytes.Equal(cs.state.AppHash, lastBlockMeta.Header.AppHash) -} - -func (cs *State) isProposer(address []byte) bool { - return bytes.Equal(cs.Validators.GetProposer().Address, address) -} - -func (cs *State) defaultDecideProposal(height int64, round int32) { - var block *types.Block - var blockParts *types.PartSet - - // Decide on block - if cs.TwoThirdPrevoteBlock != nil { - // If there is valid block, choose that. - block, blockParts = cs.TwoThirdPrevoteBlock, cs.TwoThirdPrevoteBlockParts - } else { - // Create a new proposal block from state/txs from the mempool. - block, blockParts = cs.createProposalBlock() - if block == nil { - return - } - } - - // Flush the WAL. Otherwise, we may not recompute the same proposal to sign, - // and the privValidator will refuse to sign anything. - if err := cs.wal.FlushAndSync(); err != nil { - cs.Logger.Error("Error flushing to disk") - } - - // Make proposal - propBlockID := types.BlockID{Hash: block.Hash(), PartSetHeader: blockParts.Header()} - proposal := types.NewProposal(height, round, cs.TwoThirdPrevoteRound, propBlockID) - p := proposal.ToProto() - if err := cs.privValidator.SignProposal(cs.state.ChainID, p); err == nil { - proposal.Signature = p.Signature - - // send proposal and block parts on internal msg queue - cs.sendInternalMessage(msgInfo{&cmtcon.ProposalMessage{Proposal: proposal}, ""}) - for i := 0; i < int(blockParts.Total()); i++ { - part := blockParts.GetPart(i) - cs.sendInternalMessage(msgInfo{&cmtcon.BlockPartMessage{Height: cs.Height, Round: cs.Round, Part: part}, ""}) - } - cs.Logger.Info("Signed proposal", "height", height, "round", round, "proposal", proposal) - cs.Logger.Debug("default decide proposal", - "msg", - log.NewLazySprintf("Signed proposal block: %v", block)) - } else if !cs.replayMode { - cs.Logger.Error("enterPropose: Error signing proposal", "height", height, "round", round, "err", err) - } -} - -// Returns true if the proposal block is complete && -// (if POLRound was proposed, we have +2/3 prevotes from there). -func (cs *State) isProposalComplete() bool { - if cs.Proposal == nil || cs.ProposalBlock == nil { - return false - } - // we have the proposal. if there's a POLRound, - // make sure we have the prevotes from it too - if cs.Proposal.POLRound < 0 { - return true - } - // if this is false the proposer is lying or we haven't received the POL yet - return cs.Votes.Prevotes(cs.Proposal.POLRound).HasTwoThirdsMajority() -} - -// Create the next block to propose and return it. Returns nil block upon error. -// -// We really only need to return the parts, but the block is returned for -// convenience so we can log the proposal block. -// -// NOTE: keep it side-effect free for clarity. -// CONTRACT: cs.privValidator is not nil. -func (cs *State) createProposalBlock() (block *types.Block, blockParts *types.PartSet) { - if cs.privValidator == nil { - panic("entered createProposalBlock with privValidator being nil") - } - - var commit *types.Commit - switch { - case cs.Height == cs.state.InitialHeight: - // We're creating a proposal for the first block. - // The commit is empty, but not nil. - commit = types.NewCommit(0, 0, types.BlockID{}, nil) - case cs.LastCommit.HasTwoThirdsMajority(): - // Make the commit from LastCommit - commit = cs.LastCommit.MakeCommit() - default: // This shouldn't happen. - cs.Logger.Error("enterPropose: Cannot propose anything: No commit for the previous block") - return - } - - if cs.privValidatorPubKey == nil { - // If this node is a validator & proposer in the current round, it will - // miss the opportunity to create a block. - cs.Logger.Error(fmt.Sprintf("enterPropose: %v", errPubKeyIsNotSet)) - return - } - proposerAddr := cs.privValidatorPubKey.Address() - - block = cs.blockExec.CreateProposalBlock(cs.Height, cs.state, commit, proposerAddr) - blockParts = block.MakePartSet(types.BlockPartSizeBytes) - return -} - -// Enter: any +2/3 prevotes at next round. -func (cs *State) enterPrevoteWait(height int64, round int32) { - logger := cs.Logger.With("height", height, "round", round) - - if cs.Height != height || round < cs.Round || (cs.Round == round && cstypes.RoundStepPrevoteWait <= cs.Step) { - logger.Debug("enter prevote wait", - "msg", - log.NewLazySprintf( - "enterPrevoteWait(%v/%v): Invalid args. Current step: %v/%v/%v", - height, - round, - cs.Height, - cs.Round, - cs.Step)) - return - } - if !cs.Votes.Prevotes(round).HasTwoThirdsAny() { - panic(fmt.Sprintf("enterPrevoteWait(%v/%v), but Prevotes does not have any +2/3 votes", height, round)) - } - - logger.Debug("enter prevote wait", - "msg", - log.NewLazySprintf("enterPrevoteWait(%v/%v); current: %v/%v/%v", height, round, cs.Height, cs.Round, cs.Step)) - - defer func() { - // Done enterPrevoteWait: - cs.updateRoundStep(round, cstypes.RoundStepPrevoteWait) - cs.newStep() - }() - - // Wait for some more prevotes; enterPrecommit - cs.scheduleTimeout(cs.config.Prevote(round), height, round, cstypes.RoundStepPrevoteWait) -} - -// Enter: any +2/3 precommits for next round. -func (cs *State) enterPrecommitWait(height int64, round int32) { - logger := cs.Logger.With("height", height, "round", round) - - if cs.Height != height || round < cs.Round || (cs.Round == round && cs.TriggeredTimeoutPrecommit) { - logger.Debug("state enter precommit wait", - "msg", - log.NewLazySprintf( - "enterPrecommitWait(%v/%v): Invalid args. "+ - "Current state is Height/Round: %v/%v/, TriggeredTimeoutPrecommit:%v", - height, round, cs.Height, cs.Round, cs.TriggeredTimeoutPrecommit)) - return - } - if !cs.Votes.Precommits(round).HasTwoThirdsAny() { - panic(fmt.Sprintf("enterPrecommitWait(%v/%v), but Precommits does not have any +2/3 votes", height, round)) - } - logger.Info("enter precommit wait", - "msg", - log.NewLazySprintf("enterPrecommitWait(%v/%v). Current: %v/%v/%v", height, round, cs.Height, cs.Round, cs.Step)) - - defer func() { - // Done enterPrecommitWait: - cs.TriggeredTimeoutPrecommit = true - cs.newStep() - }() - - // Wait for some more precommits; enterNewRound - cs.scheduleTimeout(cs.config.Precommit(round), height, round, cstypes.RoundStepPrecommitWait) -} - -// Enter: +2/3 precommits for block -func (cs *State) enterCommit(height int64, commitRound int32) { - logger := cs.Logger.With("height", height, "commitRound", commitRound) - - if cs.Height != height || cstypes.RoundStepCommit <= cs.Step { - logger.Debug("enter commit", - "msg", - log.NewLazySprintf("enterCommit(%v/%v): Invalid args. Current step: %v/%v/%v", - height, - commitRound, - cs.Height, - cs.Round, - cs.Step)) - return - } - logger.Info("enter commit", - "msg", - log.NewLazySprintf("enterCommit(%v/%v). Current: %v/%v/%v", height, commitRound, cs.Height, cs.Round, cs.Step)) - - defer func() { - // Done enterCommit: - // keep cs.Round the same, commitRound points to the right Precommits set. - cs.updateRoundStep(cs.Round, cstypes.RoundStepCommit) - cs.CommitRound = commitRound - cs.CommitTime = cmttime.Now() - cs.newStep() - - // Maybe finalize immediately. - cs.tryFinalizeCommit(height) - }() - - blockID, ok := cs.Votes.Precommits(commitRound).TwoThirdsMajority() - if !ok { - panic("RunActionCommit() expects +2/3 precommits") - } - - // The Locked* fields no longer matter. - // Move them over to ProposalBlock if they match the commit hash, - // otherwise they'll be cleared in updateToState. - if cs.LockedBlock.HashesTo(blockID.Hash) { - logger.Info("Commit is for locked block. Set ProposalBlock=LockedBlock", "blockHash", blockID.Hash) - cs.ProposalBlock = cs.LockedBlock - cs.ProposalBlockParts = cs.LockedBlockParts - } - - // If we don't have the block being committed, set up to get it. - if !cs.ProposalBlock.HashesTo(blockID.Hash) { - if !cs.ProposalBlockParts.HasHeader(blockID.PartSetHeader) { - logger.Info( - "commit is for a block we do not know about; set ProposalBlock=nil", - "proposal", log.NewLazyBlockHash(cs.ProposalBlock), - "commit", blockID.Hash, - ) - - // We're getting the wrong block. - // Set up ProposalBlockParts and keep waiting. - cs.ProposalBlock = nil - cs.ProposalBlockParts = types.NewPartSetFromHeader(blockID.PartSetHeader) - if err := cs.eventBus.PublishEventValidBlock(cs.RoundStateEvent()); err != nil { - cs.Logger.Error("Error publishing valid block", "err", err) - } - cs.evsw.FireEvent(types.EventValidBlock, &cs.RoundState) - } - // else { - // We just need to keep waiting. - // } - } -} - -// If we have the block AND +2/3 commits for it, finalize. -func (cs *State) tryFinalizeCommit(height int64) { - logger := cs.Logger.With("height", height) - - if cs.Height != height { - panic(fmt.Sprintf("tryFinalizeCommit() cs.Height: %v vs height: %v", cs.Height, height)) - } - - blockID, ok := cs.Votes.Precommits(cs.CommitRound).TwoThirdsMajority() - if !ok || len(blockID.Hash) == 0 { - logger.Error("Attempt to finalize failed. There was no +2/3 majority, or +2/3 was for <nil>.") - return - } - if !cs.ProposalBlock.HashesTo(blockID.Hash) { - // TODO: this happens every time if we're not a validator (ugly logs) - // TODO: ^^ wait, why does it matter that we're a validator? - logger.Debug( - "attempt to finalize failed; we do not have the commit block", - "proposal-block", log.NewLazyBlockHash(cs.ProposalBlock), - "commit-block", blockID.Hash, - ) - return - } - - // go - cs.finalizeCommit(height) -} - -// Increment height and goto cstypes.RoundStepNewHeight -func (cs *State) finalizeCommit(height int64) { - if cs.Height != height || cs.Step != cstypes.RoundStepCommit { - cs.Logger.Debug("finalize commit", - "msg", - log.NewLazySprintf("finalizeCommit(%v): Invalid args. Current step: %v/%v/%v", - height, - cs.Height, - cs.Round, - cs.Step)) - return - } - - blockID, ok := cs.Votes.Precommits(cs.CommitRound).TwoThirdsMajority() - block, blockParts := cs.ProposalBlock, cs.ProposalBlockParts - - if !ok { - panic("Cannot finalizeCommit, commit does not have two thirds majority") - } - if !blockParts.HasHeader(blockID.PartSetHeader) { - panic("Expected ProposalBlockParts header to be commit header") - } - if !block.HashesTo(blockID.Hash) { - panic("Cannot finalizeCommit, ProposalBlock does not hash to commit hash") - } - if err := cs.blockExec.ValidateBlock(cs.state, block); err != nil { - panic(fmt.Errorf("+2/3 committed an invalid block: %w", err)) - } - - cs.Logger.Info("finalizing commit of block with N txs", - "height", block.Height, - "hash", block.Hash(), - "root", block.AppHash, - "N", len(block.Txs), - ) - cs.Logger.Debug("finalize commit", "msg", log.NewLazySprintf("%v", block)) - - fail.Fail() // XXX - - // Save to blockStore. - var seenCommit *types.Commit - if cs.blockStore.Height() < block.Height { - // NOTE: the seenCommit is local justification to commit this block, - // but may differ from the LastCommit included in the next block - precommits := cs.Votes.Precommits(cs.CommitRound) - seenCommit = precommits.MakeCommit() - cs.blockStore.SaveBlock(block, blockParts, seenCommit) - } else { - // Happens during replay if we already saved the block but didn't commit - cs.Logger.Debug("calling finalizeCommit on already stored block", "height", block.Height) - } - - fail.Fail() // XXX - - // Write EndHeightMessage{} for this height, implying that the blockstore - // has saved the block. - // - // If we crash before writing this EndHeightMessage{}, we will recover by - // running ApplyBlock during the ABCI handshake when we restart. If we - // didn't save the block to the blockstore before writing - // EndHeightMessage{}, we'd have to change WAL replay -- currently it - // complains about replaying for heights where an #ENDHEIGHT entry already - // exists. - // - // Either way, the State should not be resumed until we - // successfully call ApplyBlock (ie. later here, or in Handshake after - // restart). - endMsg := cmtcon.EndHeightMessage{Height: height} - if err := cs.wal.WriteSync(endMsg); err != nil { // NOTE: fsync - panic(fmt.Sprintf("Failed to write %v msg to consensus wal due to %v. Check your FS and restart the node", - endMsg, err)) - } - - fail.Fail() // XXX - - // Create a copy of the state for staging and an event cache for txs. - stateCopy := cs.state.Copy() - - // Execute and commit the block, update and save the state, and update the mempool. - // NOTE The block.AppHash wont reflect these txs until the next block. - var err error - var retainHeight int64 - stateCopy, retainHeight, err = cs.blockExec.ApplyBlock( - stateCopy, - types.BlockID{Hash: block.Hash(), PartSetHeader: blockParts.Header()}, - block, - seenCommit, - ) - if err != nil { - cs.Logger.Error("Error on ApplyBlock", "err", err) - return - } - - fail.Fail() // XXX - - // Prune old heights, if requested by ABCI app. - if retainHeight > 0 { - pruned, err := cs.pruneBlocks(retainHeight) - if err != nil { - cs.Logger.Error("Failed to prune blocks", "retainHeight", retainHeight, "err", err) - } else { - cs.Logger.Info("Pruned blocks", "pruned", pruned, "retainHeight", retainHeight) - } - } - - // must be called before we update state - cs.recordMetrics(height, block) - - // NewHeightStep! - cs.updateToState(stateCopy) - - fail.Fail() // XXX - - // Private validator might have changed it's key pair => refetch pubkey. - if err := cs.updatePrivValidatorPubKey(); err != nil { - cs.Logger.Error("Can't get private validator pubkey", "err", err) - } - - // cs.StartTime is already set. - // Schedule Round0 to start soon. - cs.scheduleRound0(&cs.RoundState) - - // By here, - // * cs.Height has been increment to height+1 - // * cs.Step is now cstypes.RoundStepNewHeight - // * cs.StartTime is set to when we will start round0. -} - -func (cs *State) pruneBlocks(retainHeight int64) (uint64, error) { - base := cs.blockStore.Base() - if retainHeight <= base { - return 0, nil - } - pruned, err := cs.blockStore.PruneBlocks(retainHeight) - if err != nil { - return 0, fmt.Errorf("failed to prune block store: %w", err) - } - err = cs.blockExec.Store().PruneStates(base, retainHeight) - if err != nil { - return 0, fmt.Errorf("failed to prune state database: %w", err) - } - return pruned, nil -} - -func (cs *State) recordMetrics(height int64, block *types.Block) { - cs.metrics.Validators.Set(float64(cs.Validators.Size())) - cs.metrics.ValidatorsPower.Set(float64(cs.Validators.TotalVotingPower())) - - var ( - missingValidators int - missingValidatorsPower int64 - ) - // height=0 -> MissingValidators and MissingValidatorsPower are both 0. - // Remember that the first LastCommit is intentionally empty, so it's not - // fair to increment missing validators number. - if height > cs.state.InitialHeight { - // Sanity check that commit size matches validator set size - only applies - // after first block. - var ( - commitSize = block.LastCommit.Size() - valSetLen = len(cs.LastValidators.Validators) - address types.Address - ) - if commitSize != valSetLen { - panic(fmt.Sprintf("commit size (%d) doesn't match valset length (%d) at height %d\n\n%v\n\n%v", - commitSize, valSetLen, block.Height, block.LastCommit.Signatures, cs.LastValidators.Validators)) - } - - if cs.privValidator != nil { - if cs.privValidatorPubKey == nil { - // Metrics won't be updated, but it's not critical. - cs.Logger.Error(fmt.Sprintf("recordMetrics: %v", errPubKeyIsNotSet)) - } else { - address = cs.privValidatorPubKey.Address() - } - } - - for i, val := range cs.LastValidators.Validators { - commitSig := block.LastCommit.Signatures[i] - if commitSig.Absent() { - missingValidators++ - missingValidatorsPower += val.VotingPower - } - - if bytes.Equal(val.Address, address) { - label := []string{ - "validator_address", val.Address.String(), - } - cs.metrics.ValidatorPower.With(label...).Set(float64(val.VotingPower)) - if commitSig.ForBlock() { - cs.metrics.ValidatorLastSignedHeight.With(label...).Set(float64(height)) - } else { - cs.metrics.ValidatorMissedBlocks.With(label...).Add(float64(1)) - } - } - - } - } - cs.metrics.MissingValidators.Set(float64(missingValidators)) - cs.metrics.MissingValidatorsPower.Set(float64(missingValidatorsPower)) - - // NOTE: byzantine validators power and count is only for consensus evidence i.e. duplicate vote - var ( - byzantineValidatorsPower = int64(0) - byzantineValidatorsCount = int64(0) - ) - for _, ev := range block.Evidence.Evidence { - if dve, ok := ev.(*types.DuplicateVoteEvidence); ok { - if _, val := cs.Validators.GetByAddress(dve.VoteA.ValidatorAddress); val != nil { - byzantineValidatorsCount++ - byzantineValidatorsPower += val.VotingPower - } - } - } - cs.metrics.ByzantineValidators.Set(float64(byzantineValidatorsCount)) - cs.metrics.ByzantineValidatorsPower.Set(float64(byzantineValidatorsPower)) - - if height > 1 { - lastBlockMeta := cs.blockStore.LoadBlockMeta(height - 1) - if lastBlockMeta != nil { - elapsedTime := block.Time.Sub(lastBlockMeta.Header.Time).Seconds() - cs.metrics.BlockIntervalSeconds.Observe(elapsedTime) - cs.metrics.BlockTimeSeconds.Set(elapsedTime) - - } - } - - cs.metrics.NumTxs.Set(float64(len(block.Data.Txs))) - cs.metrics.TotalTxs.Add(float64(len(block.Data.Txs))) - cs.metrics.BlockSizeBytes.Set(float64(block.Size())) - cs.metrics.CommittedHeight.Set(float64(block.Height)) -} - -//----------------------------------------------------------------------------- - -// NOTE: block is not necessarily valid. -// Asynchronously triggers either enterPrevote (before we timeout of propose) or tryFinalizeCommit, -// once we have the full block. -func (cs *State) addProposalBlockPart(msg *cmtcon.BlockPartMessage, peerID p2p.ID) (added bool, err error) { - height, round, part := msg.Height, msg.Round, msg.Part - - // Blocks might be reused, so round mismatch is OK - if cs.Height != height { - cs.Logger.Debug("Received block part from wrong height", "height", height, "round", round) - return false, nil - } - - // We're not expecting a block part. - if cs.ProposalBlockParts == nil { - // NOTE: this can happen when we've gone to a higher round and - // then receive parts from the previous round - not necessarily a bad peer. - cs.Logger.Info("Received a block part when we're not expecting any", - "height", height, "round", round, "index", part.Index, "peer", peerID) - return false, nil - } - - added, err = cs.ProposalBlockParts.AddPart(part) - if err != nil { - return added, err - } - if cs.ProposalBlockParts.ByteSize() > cs.state.ConsensusParams.Block.MaxBytes { - return added, fmt.Errorf("total size of proposal block parts exceeds maximum block bytes (%d > %d)", - cs.ProposalBlockParts.ByteSize(), cs.state.ConsensusParams.Block.MaxBytes, - ) - } - if added && cs.ProposalBlockParts.IsComplete() { - bz, err := io.ReadAll(cs.ProposalBlockParts.GetReader()) - if err != nil { - return added, err - } - - pbb := new(cmtproto.Block) - err = proto.Unmarshal(bz, pbb) - if err != nil { - return added, err - } - - block, err := types.BlockFromProto(pbb) - if err != nil { - return added, err - } - - cs.ProposalBlock = block - // NOTE: it's possible to receive complete proposal blocks for future rounds without having the proposal - cs.Logger.Info("Received complete proposal block", "height", cs.ProposalBlock.Height, "hash", cs.ProposalBlock.Hash()) - if err := cs.eventBus.PublishEventCompleteProposal(cs.CompleteProposalEvent()); err != nil { - cs.Logger.Error("Error publishing event complete proposal", "err", err) - } - - // Update Valid* if we can. - prevotes := cs.Votes.Prevotes(cs.Round) - blockID, hasTwoThirds := prevotes.TwoThirdsMajority() - if hasTwoThirds && !blockID.IsZero() && (cs.TwoThirdPrevoteRound < cs.Round) { - if cs.ProposalBlock.HashesTo(blockID.Hash) { - cs.Logger.Info("Updating valid block to new proposal block", - "valid-round", cs.Round, "valid-block-hash", cs.ProposalBlock.Hash()) - cs.TwoThirdPrevoteRound = cs.Round - cs.TwoThirdPrevoteBlock = cs.ProposalBlock - cs.TwoThirdPrevoteBlockParts = cs.ProposalBlockParts - } - // TODO: In case there is +2/3 majority in Prevotes set for some - // block and cs.ProposalBlock contains different block, either - // proposer is faulty or voting power of faulty processes is more - // than 1/3. We should trigger in the future accountability - // procedure at this point. - } - - if cs.Step <= cstypes.RoundStepPropose && cs.isProposalComplete() { - // Move onto the next step - cs.enterPrevote(height, cs.Round) - if hasTwoThirds { // this is optimisation as this will be triggered when prevote is added - cs.enterPrecommit(height, cs.Round) - } - } else if cs.Step == cstypes.RoundStepCommit { - // If we're waiting on the proposal block... - cs.tryFinalizeCommit(height) - } - return added, nil - } - return added, nil -} - -// Attempt to add the vote. if its a duplicate signature, dupeout the validator -func (cs *State) tryAddVote(vote *types.Vote, peerID p2p.ID) (bool, error) { - added, err := cs.addVote(vote, peerID) - if err != nil { - // If the vote height is off, we'll just ignore it, - // But if it's a conflicting sig, add it to the cs.evpool. - // If it's otherwise invalid, punish peer. - //nolint: gocritic - if voteErr, ok := err.(*types.ErrVoteConflictingVotes); ok { - if cs.privValidatorPubKey == nil { - return false, errPubKeyIsNotSet - } - - if bytes.Equal(vote.ValidatorAddress, cs.privValidatorPubKey.Address()) { - cs.Logger.Error( - "Found conflicting vote from ourselves. Did you unsafe_reset a validator?", - "height", - vote.Height, - "round", - vote.Round, - "type", - vote.Type) - return added, err - } - cs.evpool.ReportConflictingVotes(voteErr.VoteA, voteErr.VoteB) - return added, err - } else if err == types.ErrVoteNonDeterministicSignature { - cs.Logger.Debug("Vote has non-deterministic signature", "err", err) - } else { - // Either - // 1) bad peer OR - // 2) not a bad peer? this can also err sometimes with "Unexpected step" OR - // 3) tmkms use with multiple validators connecting to a single tmkms instance - // (https://github.com/tendermint/tendermint/issues/3839). - cs.Logger.Info("Error attempting to add vote", "err", err) - return added, ErrAddingVote - } - } - return added, nil -} - -//----------------------------------------------------------------------------- - -// CONTRACT: cs.privValidator is not nil. -func (cs *State) signVote( - msgType cmtproto.SignedMsgType, - hash []byte, - header types.PartSetHeader, -) (*types.Vote, error) { - // Flush the WAL. Otherwise, we may not recompute the same vote to sign, - // and the privValidator will refuse to sign anything. - if err := cs.wal.FlushAndSync(); err != nil { - return nil, err - } - - if cs.privValidatorPubKey == nil { - return nil, errPubKeyIsNotSet - } - addr := cs.privValidatorPubKey.Address() - valIdx, _ := cs.Validators.GetByAddress(addr) - - vote := &types.Vote{ - ValidatorAddress: addr, - ValidatorIndex: valIdx, - Height: cs.Height, - Round: cs.Round, - Timestamp: cs.voteTime(), - Type: msgType, - BlockID: types.BlockID{Hash: hash, PartSetHeader: header}, - } - v := vote.ToProto() - err := cs.privValidator.SignVote(cs.state.ChainID, v) - vote.Signature = v.Signature - - return vote, err -} - -func (cs *State) voteTime() time.Time { - now := cmttime.Now() - minVoteTime := now - // TODO: We should remove next line in case we don't vote for v in case cs.ProposalBlock == nil, - // even if cs.LockedBlock != nil. See https://github.com/tendermint/tendermint/tree/v0.34.x/spec/. - timeIota := time.Duration(cs.state.ConsensusParams.Block.TimeIotaMs) * time.Millisecond - if cs.LockedBlock != nil { - // See the BFT time spec - // https://github.com/tendermint/tendermint/blob/v0.34.x/spec/consensus/bft-time.md - minVoteTime = cs.LockedBlock.Time.Add(timeIota) - } else if cs.ProposalBlock != nil { - minVoteTime = cs.ProposalBlock.Time.Add(timeIota) - } - - if now.After(minVoteTime) { - return now - } - return minVoteTime -} - -// sign the vote and publish on internalMsgQueue -func (cs *State) signAddVote(msgType cmtproto.SignedMsgType, hash []byte, header types.PartSetHeader) *types.Vote { - if cs.privValidator == nil { // the node does not have a key - return nil - } - - if cs.privValidatorPubKey == nil { - // Vote won't be signed, but it's not critical. - cs.Logger.Error(fmt.Sprintf("signAddVote: %v", errPubKeyIsNotSet)) - return nil - } - - // If the node not in the validator set, do nothing. - if !cs.Validators.HasAddress(cs.privValidatorPubKey.Address()) { - return nil - } - - // TODO: pass pubKey to signVote - vote, err := cs.signVote(msgType, hash, header) - if err == nil { - cs.sendInternalMessage(msgInfo{&cmtcon.VoteMessage{Vote: vote}, ""}) - cs.Logger.Info("Signed and pushed vote", "height", cs.Height, "round", cs.Round, "vote", vote) - return vote - } - // if !cs.replayMode { - cs.Logger.Error("Error signing vote", "height", cs.Height, "round", cs.Round, "vote", vote, "err", err) - //} - return nil -} - -// updatePrivValidatorPubKey get's the private validator public key and -// memoizes it. This func returns an error if the private validator is not -// responding or responds with an error. -func (cs *State) updatePrivValidatorPubKey() error { - if cs.privValidator == nil { - return nil - } - - pubKey, err := cs.privValidator.GetPubKey() - if err != nil { - return err - } - cs.privValidatorPubKey = pubKey - return nil -} - -// look back to check existence of the node's consensus votes before joining consensus -func (cs *State) checkDoubleSigningRisk(height int64) error { - if cs.privValidator != nil && cs.privValidatorPubKey != nil && cs.config.DoubleSignCheckHeight > 0 && height > 0 { - valAddr := cs.privValidatorPubKey.Address() - doubleSignCheckHeight := cs.config.DoubleSignCheckHeight - if doubleSignCheckHeight > height { - doubleSignCheckHeight = height - } - for i := int64(1); i < doubleSignCheckHeight; i++ { - lastCommit := cs.blockStore.LoadSeenCommit(height - i) - if lastCommit != nil { - for sigIdx, s := range lastCommit.Signatures { - if s.BlockIDFlag == types.BlockIDFlagCommit && bytes.Equal(s.ValidatorAddress, valAddr) { - cs.Logger.Info("Found signature from the same key", "sig", s, "idx", sigIdx, "height", height-i) - return ErrSignatureFoundInPastBlocks - } - } - } - } - } - return nil -} - -//--------------------------------------------------------- - -func CompareHRS(h1 int64, r1 int32, s1 cstypes.RoundStepType, h2 int64, r2 int32, s2 cstypes.RoundStepType) int { - if h1 < h2 { - return -1 - } else if h1 > h2 { - return 1 - } - if r1 < r2 { - return -1 - } else if r1 > r2 { - return 1 - } - if s1 < s2 { - return -1 - } else if s1 > s2 { - return 1 - } - return 0 -} - -// repairWalFile decodes messages from src (until the decoder errors) and -// writes them to dst. -func repairWalFile(src, dst string) error { - in, err := os.Open(src) - if err != nil { - return err - } - defer in.Close() - - out, err := os.Open(dst) - if err != nil { - return err - } - defer out.Close() - - var ( - dec = NewWALDecoder(in) - enc = NewWALEncoder(out) - ) - - // best-case repair (until first error is encountered) - for { - msg, err := dec.Decode() - if err != nil { - break - } - - err = enc.Encode(msg) - if err != nil { - return fmt.Errorf("failed to encode msg: %w", err) - } - } - - return nil -} diff --git a/test/maverick/consensus/ticker.go b/test/maverick/consensus/ticker.go deleted file mode 100644 index fb3571ac86..0000000000 --- a/test/maverick/consensus/ticker.go +++ /dev/null @@ -1,134 +0,0 @@ -package consensus - -import ( - "time" - - "github.com/tendermint/tendermint/libs/log" - "github.com/tendermint/tendermint/libs/service" -) - -var ( - tickTockBufferSize = 10 -) - -// TimeoutTicker is a timer that schedules timeouts -// conditional on the height/round/step in the timeoutInfo. -// The timeoutInfo.Duration may be non-positive. -type TimeoutTicker interface { - Start() error - Stop() error - Chan() <-chan timeoutInfo // on which to receive a timeout - ScheduleTimeout(ti timeoutInfo) // reset the timer - - SetLogger(log.Logger) -} - -// timeoutTicker wraps time.Timer, -// scheduling timeouts only for greater height/round/step -// than what it's already seen. -// Timeouts are scheduled along the tickChan, -// and fired on the tockChan. -type timeoutTicker struct { - service.BaseService - - timer *time.Timer - tickChan chan timeoutInfo // for scheduling timeouts - tockChan chan timeoutInfo // for notifying about them -} - -// NewTimeoutTicker returns a new TimeoutTicker. -func NewTimeoutTicker() TimeoutTicker { - tt := &timeoutTicker{ - timer: time.NewTimer(0), - tickChan: make(chan timeoutInfo, tickTockBufferSize), - tockChan: make(chan timeoutInfo, tickTockBufferSize), - } - tt.BaseService = *service.NewBaseService(nil, "TimeoutTicker", tt) - tt.stopTimer() // don't want to fire until the first scheduled timeout - return tt -} - -// OnStart implements service.Service. It starts the timeout routine. -func (t *timeoutTicker) OnStart() error { - - go t.timeoutRoutine() - - return nil -} - -// OnStop implements service.Service. It stops the timeout routine. -func (t *timeoutTicker) OnStop() { - t.BaseService.OnStop() - t.stopTimer() -} - -// Chan returns a channel on which timeouts are sent. -func (t *timeoutTicker) Chan() <-chan timeoutInfo { - return t.tockChan -} - -// ScheduleTimeout schedules a new timeout by sending on the internal tickChan. -// The timeoutRoutine is always available to read from tickChan, so this won't block. -// The scheduling may fail if the timeoutRoutine has already scheduled a timeout for a later height/round/step. -func (t *timeoutTicker) ScheduleTimeout(ti timeoutInfo) { - t.tickChan <- ti -} - -//------------------------------------------------------------- - -// stop the timer and drain if necessary -func (t *timeoutTicker) stopTimer() { - // Stop() returns false if it was already fired or was stopped - if !t.timer.Stop() { - select { - case <-t.timer.C: - default: - t.Logger.Debug("Timer already stopped") - } - } -} - -// send on tickChan to start a new timer. -// timers are interupted and replaced by new ticks from later steps -// timeouts of 0 on the tickChan will be immediately relayed to the tockChan -func (t *timeoutTicker) timeoutRoutine() { - t.Logger.Debug("Starting timeout routine") - var ti timeoutInfo - for { - select { - case newti := <-t.tickChan: - t.Logger.Debug("Received tick", "old_ti", ti, "new_ti", newti) - - // ignore tickers for old height/round/step - if newti.Height < ti.Height { - continue - } else if newti.Height == ti.Height { - if newti.Round < ti.Round { - continue - } else if newti.Round == ti.Round { - if ti.Step > 0 && newti.Step <= ti.Step { - continue - } - } - } - - // stop the last timer - t.stopTimer() - - // update timeoutInfo and reset timer - // NOTE time.Timer allows duration to be non-positive - ti = newti - t.timer.Reset(ti.Duration) - t.Logger.Debug("Scheduled timeout", "dur", ti.Duration, "height", ti.Height, "round", ti.Round, "step", ti.Step) - case <-t.timer.C: - t.Logger.Info("Timed out", "dur", ti.Duration, "height", ti.Height, "round", ti.Round, "step", ti.Step) - // go routine here guarantees timeoutRoutine doesn't block. - // Determinism comes from playback in the receiveRoutine. - // We can eliminate it by merging the timeoutRoutine into receiveRoutine - // and managing the timeouts ourselves with a millisecond ticker - go func(toi timeoutInfo) { t.tockChan <- toi }(ti) - case <-t.Quit(): - return - } - } -} diff --git a/test/maverick/consensus/wal.go b/test/maverick/consensus/wal.go deleted file mode 100644 index 2c7ccc2a2e..0000000000 --- a/test/maverick/consensus/wal.go +++ /dev/null @@ -1,407 +0,0 @@ -package consensus - -import ( - "encoding/binary" - "errors" - "fmt" - "hash/crc32" - "io" - "path/filepath" - "time" - - "github.com/gogo/protobuf/proto" - - // cmtjson "github.com/tendermint/tendermint/libs/json" - cmtcon "github.com/tendermint/tendermint/consensus" - auto "github.com/tendermint/tendermint/libs/autofile" - "github.com/tendermint/tendermint/libs/log" - cmtos "github.com/tendermint/tendermint/libs/os" - "github.com/tendermint/tendermint/libs/service" - cmtcons "github.com/tendermint/tendermint/proto/tendermint/consensus" - cmttime "github.com/tendermint/tendermint/types/time" -) - -const ( - // time.Time + max consensus msg size - maxMsgSizeBytes = maxMsgSize + 24 - - // how often the WAL should be sync'd during period sync'ing - walDefaultFlushInterval = 2 * time.Second -) - -//-------------------------------------------------------- -// types and functions for savings consensus messages -// func init() { -// cmtjson.RegisterType(msgInfo{}, "tendermint/wal/MsgInfo") -// cmtjson.RegisterType(timeoutInfo{}, "tendermint/wal/TimeoutInfo") -// cmtjson.RegisterType(cmtcon.EndHeightMessage {}, "tendermint/wal/EndHeightMessage ") -// } - -// Write ahead logger writes msgs to disk before they are processed. -// Can be used for crash-recovery and deterministic replay. -// TODO: currently the wal is overwritten during replay catchup, give it a mode -// so it's either reading or appending - must read to end to start appending -// again. -type BaseWAL struct { - service.BaseService - - group *auto.Group - - enc *WALEncoder - - flushTicker *time.Ticker - flushInterval time.Duration -} - -var _ cmtcon.WAL = &BaseWAL{} - -// NewWAL returns a new write-ahead logger based on `baseWAL`, which implements -// WAL. It's flushed and synced to disk every 2s and once when stopped. -func NewWAL(walFile string, groupOptions ...func(*auto.Group)) (*BaseWAL, error) { - err := cmtos.EnsureDir(filepath.Dir(walFile), 0700) - if err != nil { - return nil, fmt.Errorf("failed to ensure WAL directory is in place: %w", err) - } - - group, err := auto.OpenGroup(walFile, groupOptions...) - if err != nil { - return nil, err - } - wal := &BaseWAL{ - group: group, - enc: NewWALEncoder(group), - flushInterval: walDefaultFlushInterval, - } - wal.BaseService = *service.NewBaseService(nil, "baseWAL", wal) - return wal, nil -} - -// SetFlushInterval allows us to override the periodic flush interval for the WAL. -func (wal *BaseWAL) SetFlushInterval(i time.Duration) { - wal.flushInterval = i -} - -func (wal *BaseWAL) Group() *auto.Group { - return wal.group -} - -func (wal *BaseWAL) SetLogger(l log.Logger) { - wal.BaseService.Logger = l - wal.group.SetLogger(l) -} - -func (wal *BaseWAL) OnStart() error { - size, err := wal.group.Head.Size() - if err != nil { - return err - } else if size == 0 { - if err := wal.WriteSync(cmtcon.EndHeightMessage{Height: 0}); err != nil { - return err - } - } - err = wal.group.Start() - if err != nil { - return err - } - wal.flushTicker = time.NewTicker(wal.flushInterval) - go wal.processFlushTicks() - return nil -} - -func (wal *BaseWAL) processFlushTicks() { - for { - select { - case <-wal.flushTicker.C: - if err := wal.FlushAndSync(); err != nil { - wal.Logger.Error("Periodic WAL flush failed", "err", err) - } - case <-wal.Quit(): - return - } - } -} - -// FlushAndSync flushes and fsync's the underlying group's data to disk. -// See auto#FlushAndSync -func (wal *BaseWAL) FlushAndSync() error { - return wal.group.FlushAndSync() -} - -// Stop the underlying autofile group. -// Use Wait() to ensure it's finished shutting down -// before cleaning up files. -func (wal *BaseWAL) OnStop() { - wal.flushTicker.Stop() - if err := wal.FlushAndSync(); err != nil { - wal.Logger.Error("error on flush data to disk", "error", err) - } - if err := wal.group.Stop(); err != nil { - wal.Logger.Error("error trying to stop wal", "error", err) - } - wal.group.Close() -} - -// Wait for the underlying autofile group to finish shutting down -// so it's safe to cleanup files. -func (wal *BaseWAL) Wait() { - wal.group.Wait() -} - -// Write is called in newStep and for each receive on the -// peerMsgQueue and the timeoutTicker. -// NOTE: does not call fsync() -func (wal *BaseWAL) Write(msg cmtcon.WALMessage) error { - if wal == nil { - return nil - } - - if err := wal.enc.Encode(&cmtcon.TimedWALMessage{Time: cmttime.Now(), Msg: msg}); err != nil { - wal.Logger.Error("Error writing msg to consensus wal. WARNING: recover may not be possible for the current height", - "err", err, "msg", msg) - return err - } - - return nil -} - -// WriteSync is called when we receive a msg from ourselves -// so that we write to disk before sending signed messages. -// NOTE: calls fsync() -func (wal *BaseWAL) WriteSync(msg cmtcon.WALMessage) error { - if wal == nil { - return nil - } - - if err := wal.Write(msg); err != nil { - return err - } - - if err := wal.FlushAndSync(); err != nil { - wal.Logger.Error(`WriteSync failed to flush consensus wal. - WARNING: may result in creating alternative proposals / votes for the current height iff the node restarted`, - "err", err) - return err - } - - return nil -} - -// WALSearchOptions are optional arguments to SearchForEndHeight. -type WALSearchOptions struct { - // IgnoreDataCorruptionErrors set to true will result in skipping data corruption errors. - IgnoreDataCorruptionErrors bool -} - -// SearchForEndHeight searches for the EndHeightMessage with the given height -// and returns an auto.GroupReader, whenever it was found or not and an error. -// Group reader will be nil if found equals false. -// -// CONTRACT: caller must close group reader. -func (wal *BaseWAL) SearchForEndHeight( - height int64, - options *cmtcon.WALSearchOptions) (rd io.ReadCloser, found bool, err error) { - var ( - msg *cmtcon.TimedWALMessage - gr *auto.GroupReader - ) - lastHeightFound := int64(-1) - - // NOTE: starting from the last file in the group because we're usually - // searching for the last height. See replay.go - min, max := wal.group.MinIndex(), wal.group.MaxIndex() - wal.Logger.Info("Searching for height", "height", height, "min", min, "max", max) - for index := max; index >= min; index-- { - gr, err = wal.group.NewReader(index) - if err != nil { - return nil, false, err - } - - dec := NewWALDecoder(gr) - for { - msg, err = dec.Decode() - if err == io.EOF { - // OPTIMISATION: no need to look for height in older files if we've seen h < height - if lastHeightFound > 0 && lastHeightFound < height { - gr.Close() - return nil, false, nil - } - // check next file - break - } - if options.IgnoreDataCorruptionErrors && IsDataCorruptionError(err) { - wal.Logger.Error("Corrupted entry. Skipping...", "err", err) - // do nothing - continue - } else if err != nil { - gr.Close() - return nil, false, err - } - - if m, ok := msg.Msg.(cmtcon.EndHeightMessage); ok { - lastHeightFound = m.Height - if m.Height == height { // found - wal.Logger.Info("Found", "height", height, "index", index) - return gr, true, nil - } - } - } - gr.Close() - } - - return nil, false, nil -} - -// ///////////////////////////////////////////////////////////////////////////// - -// A WALEncoder writes custom-encoded WAL messages to an output stream. -// -// Format: 4 bytes CRC sum + 4 bytes length + arbitrary-length value -type WALEncoder struct { - wr io.Writer -} - -// NewWALEncoder returns a new encoder that writes to wr. -func NewWALEncoder(wr io.Writer) *WALEncoder { - return &WALEncoder{wr} -} - -// Encode writes the custom encoding of v to the stream. It returns an error if -// the encoded size of v is greater than 1MB. Any error encountered -// during the write is also returned. -func (enc *WALEncoder) Encode(v *cmtcon.TimedWALMessage) error { - pbMsg, err := WALToProto(v.Msg) - if err != nil { - return err - } - pv := cmtcons.TimedWALMessage{ - Time: v.Time, - Msg: pbMsg, - } - - data, err := proto.Marshal(&pv) - if err != nil { - panic(fmt.Errorf("encode timed wall message failure: %w", err)) - } - - crc := crc32.Checksum(data, crc32c) - length := uint32(len(data)) - if length > maxMsgSizeBytes { - return fmt.Errorf("msg is too big: %d bytes, max: %d bytes", length, maxMsgSizeBytes) - } - totalLength := 8 + int(length) - - msg := make([]byte, totalLength) - binary.BigEndian.PutUint32(msg[0:4], crc) - binary.BigEndian.PutUint32(msg[4:8], length) - copy(msg[8:], data) - - _, err = enc.wr.Write(msg) - return err -} - -// ///////////////////////////////////////////////////////////////////////////// - -// IsDataCorruptionError returns true if data has been corrupted inside WAL. -func IsDataCorruptionError(err error) bool { - _, ok := err.(DataCorruptionError) - return ok -} - -// DataCorruptionError is an error that occures if data on disk was corrupted. -type DataCorruptionError struct { - cause error -} - -func (e DataCorruptionError) Error() string { - return fmt.Sprintf("DataCorruptionError[%v]", e.cause) -} - -func (e DataCorruptionError) Cause() error { - return e.cause -} - -// A WALDecoder reads and decodes custom-encoded WAL messages from an input -// stream. See WALEncoder for the format used. -// -// It will also compare the checksums and make sure data size is equal to the -// length from the header. If that is not the case, error will be returned. -type WALDecoder struct { - rd io.Reader -} - -// NewWALDecoder returns a new decoder that reads from rd. -func NewWALDecoder(rd io.Reader) *WALDecoder { - return &WALDecoder{rd} -} - -// Decode reads the next custom-encoded value from its reader and returns it. -func (dec *WALDecoder) Decode() (*cmtcon.TimedWALMessage, error) { - b := make([]byte, 4) - - _, err := dec.rd.Read(b) - if errors.Is(err, io.EOF) { - return nil, err - } - if err != nil { - return nil, DataCorruptionError{fmt.Errorf("failed to read checksum: %v", err)} - } - crc := binary.BigEndian.Uint32(b) - - b = make([]byte, 4) - _, err = dec.rd.Read(b) - if err != nil { - return nil, DataCorruptionError{fmt.Errorf("failed to read length: %v", err)} - } - length := binary.BigEndian.Uint32(b) - - if length > maxMsgSizeBytes { - return nil, DataCorruptionError{fmt.Errorf( - "length %d exceeded maximum possible value of %d bytes", - length, - maxMsgSizeBytes)} - } - - data := make([]byte, length) - n, err := dec.rd.Read(data) - if err != nil { - return nil, DataCorruptionError{fmt.Errorf("failed to read data: %v (read: %d, wanted: %d)", err, n, length)} - } - - // check checksum before decoding data - actualCRC := crc32.Checksum(data, crc32c) - if actualCRC != crc { - return nil, DataCorruptionError{fmt.Errorf("checksums do not match: read: %v, actual: %v", crc, actualCRC)} - } - - var res = new(cmtcons.TimedWALMessage) - err = proto.Unmarshal(data, res) - if err != nil { - return nil, DataCorruptionError{fmt.Errorf("failed to decode data: %v", err)} - } - - walMsg, err := WALFromProto(res.Msg) - if err != nil { - return nil, DataCorruptionError{fmt.Errorf("failed to convert from proto: %w", err)} - } - tMsgWal := &cmtcon.TimedWALMessage{ - Time: res.Time, - Msg: walMsg, - } - - return tMsgWal, err -} - -type nilWAL struct{} - -var _ cmtcon.WAL = nilWAL{} - -func (nilWAL) Write(m cmtcon.WALMessage) error { return nil } -func (nilWAL) WriteSync(m cmtcon.WALMessage) error { return nil } -func (nilWAL) FlushAndSync() error { return nil } -func (nilWAL) SearchForEndHeight(height int64, - options *cmtcon.WALSearchOptions) (rd io.ReadCloser, found bool, err error) { - return nil, false, nil -} -func (nilWAL) Start() error { return nil } -func (nilWAL) Stop() error { return nil } -func (nilWAL) Wait() {} diff --git a/test/maverick/consensus/wal_generator.go b/test/maverick/consensus/wal_generator.go deleted file mode 100644 index 64cd683282..0000000000 --- a/test/maverick/consensus/wal_generator.go +++ /dev/null @@ -1,232 +0,0 @@ -package consensus - -import ( - "bufio" - "bytes" - "fmt" - "io" - "path/filepath" - "testing" - "time" - - db "github.com/cometbft/cometbft-db" - - "github.com/tendermint/tendermint/abci/example/kvstore" - cfg "github.com/tendermint/tendermint/config" - cmtcon "github.com/tendermint/tendermint/consensus" - "github.com/tendermint/tendermint/libs/log" - cmtrand "github.com/tendermint/tendermint/libs/rand" - "github.com/tendermint/tendermint/privval" - "github.com/tendermint/tendermint/proxy" - sm "github.com/tendermint/tendermint/state" - "github.com/tendermint/tendermint/store" - "github.com/tendermint/tendermint/types" -) - -// WALGenerateNBlocks generates a consensus WAL. It does this by spinning up a -// stripped down version of node (proxy app, event bus, consensus state) with a -// persistent kvstore application and special consensus wal instance -// (byteBufferWAL) and waits until numBlocks are created. -// If the node fails to produce given numBlocks, it returns an error. -func WALGenerateNBlocks(t *testing.T, wr io.Writer, numBlocks int) (err error) { - config := getConfig(t) - - app := kvstore.NewPersistentKVStoreApplication(filepath.Join(config.DBDir(), "wal_generator")) - - logger := log.TestingLogger().With("wal_generator", "wal_generator") - logger.Info("generating WAL (last height msg excluded)", "numBlocks", numBlocks) - - // /////////////////////////////////////////////////////////////////////////// - // COPY PASTE FROM node.go WITH A FEW MODIFICATIONS - // NOTE: we can't import node package because of circular dependency. - // NOTE: we don't do handshake so need to set state.Version.Consensus.App directly. - privValidatorKeyFile := config.PrivValidatorKeyFile() - privValidatorStateFile := config.PrivValidatorStateFile() - privValidator := privval.LoadOrGenFilePV(privValidatorKeyFile, privValidatorStateFile) - genDoc, err := types.GenesisDocFromFile(config.GenesisFile()) - if err != nil { - return fmt.Errorf("failed to read genesis file: %w", err) - } - blockStoreDB := db.NewMemDB() - stateDB := blockStoreDB - stateStore := sm.NewStore(stateDB, sm.StoreOptions{ - DiscardABCIResponses: false, - }) - state, err := sm.MakeGenesisState(genDoc) - if err != nil { - return fmt.Errorf("failed to make genesis state: %w", err) - } - state.Version.Consensus.App = kvstore.ProtocolVersion - if err = stateStore.Save(state); err != nil { - t.Error(err) - } - - blockStore := store.NewBlockStore(blockStoreDB) - - proxyApp := proxy.NewAppConns(proxy.NewLocalClientCreator(app)) - proxyApp.SetLogger(logger.With("module", "proxy")) - if err := proxyApp.Start(); err != nil { - return fmt.Errorf("failed to start proxy app connections: %w", err) - } - t.Cleanup(func() { - if err := proxyApp.Stop(); err != nil { - t.Error(err) - } - }) - - eventBus := types.NewEventBus() - eventBus.SetLogger(logger.With("module", "events")) - if err := eventBus.Start(); err != nil { - return fmt.Errorf("failed to start event bus: %w", err) - } - t.Cleanup(func() { - if err := eventBus.Stop(); err != nil { - t.Error(err) - } - }) - mempool := emptyMempool{} - evpool := sm.EmptyEvidencePool{} - blockExec := sm.NewBlockExecutor(stateStore, log.TestingLogger(), proxyApp.Consensus(), mempool, evpool) - consensusState := NewState(config.Consensus, state.Copy(), - blockExec, blockStore, mempool, evpool, map[int64]Misbehavior{}) - consensusState.SetLogger(logger) - consensusState.SetEventBus(eventBus) - if privValidator != nil { - consensusState.SetPrivValidator(privValidator) - } - // END OF COPY PASTE - // /////////////////////////////////////////////////////////////////////////// - - // set consensus wal to buffered WAL, which will write all incoming msgs to buffer - numBlocksWritten := make(chan struct{}) - wal := newByteBufferWAL(logger, NewWALEncoder(wr), int64(numBlocks), numBlocksWritten) - // see wal.go#103 - if err := wal.Write(cmtcon.EndHeightMessage{Height: 0}); err != nil { - t.Error(err) - } - - consensusState.wal = wal - - if err := consensusState.Start(); err != nil { - return fmt.Errorf("failed to start consensus state: %w", err) - } - - select { - case <-numBlocksWritten: - if err := consensusState.Stop(); err != nil { - t.Error(err) - } - return nil - case <-time.After(1 * time.Minute): - if err := consensusState.Stop(); err != nil { - t.Error(err) - } - return fmt.Errorf("waited too long for CometBFT to produce %d blocks (grep logs for `wal_generator`)", numBlocks) - } -} - -// WALWithNBlocks returns a WAL content with numBlocks. -func WALWithNBlocks(t *testing.T, numBlocks int) (data []byte, err error) { - var b bytes.Buffer - wr := bufio.NewWriter(&b) - - if err := WALGenerateNBlocks(t, wr, numBlocks); err != nil { - return []byte{}, err - } - - wr.Flush() - return b.Bytes(), nil -} - -func randPort() int { - // returns between base and base + spread - base, spread := 20000, 20000 - return base + cmtrand.Intn(spread) -} - -func makeAddrs() (string, string, string) { - start := randPort() - return fmt.Sprintf("tcp://127.0.0.1:%d", start), - fmt.Sprintf("tcp://127.0.0.1:%d", start+1), - fmt.Sprintf("tcp://127.0.0.1:%d", start+2) -} - -// getConfig returns a config for test cases -func getConfig(t *testing.T) *cfg.Config { - c := cfg.ResetTestRoot(t.Name()) - - // and we use random ports to run in parallel - tm, rpc, grpc := makeAddrs() - c.P2P.ListenAddress = tm - c.RPC.ListenAddress = rpc - c.RPC.GRPCListenAddress = grpc - return c -} - -// byteBufferWAL is a WAL which writes all msgs to a byte buffer. Writing stops -// when the heightToStop is reached. Client will be notified via -// signalWhenStopsTo channel. -type byteBufferWAL struct { - enc *WALEncoder - stopped bool - heightToStop int64 - signalWhenStopsTo chan<- struct{} - - logger log.Logger -} - -// needed for determinism -var fixedTime, _ = time.Parse(time.RFC3339, "2017-01-02T15:04:05Z") - -func newByteBufferWAL(logger log.Logger, enc *WALEncoder, nBlocks int64, signalStop chan<- struct{}) *byteBufferWAL { - return &byteBufferWAL{ - enc: enc, - heightToStop: nBlocks, - signalWhenStopsTo: signalStop, - logger: logger, - } -} - -// Save writes message to the internal buffer except when heightToStop is -// reached, in which case it will signal the caller via signalWhenStopsTo and -// skip writing. -func (w *byteBufferWAL) Write(m cmtcon.WALMessage) error { - if w.stopped { - w.logger.Debug("WAL already stopped. Not writing message", "msg", m) - return nil - } - - if endMsg, ok := m.(cmtcon.EndHeightMessage); ok { - w.logger.Debug("WAL write end height message", "height", endMsg.Height, "stopHeight", w.heightToStop) - if endMsg.Height == w.heightToStop { - w.logger.Debug("Stopping WAL at height", "height", endMsg.Height) - w.signalWhenStopsTo <- struct{}{} - w.stopped = true - return nil - } - } - - w.logger.Debug("WAL Write Message", "msg", m) - err := w.enc.Encode(&cmtcon.TimedWALMessage{Time: fixedTime, Msg: m}) - if err != nil { - panic(fmt.Sprintf("failed to encode the msg %v", m)) - } - - return nil -} - -func (w *byteBufferWAL) WriteSync(m cmtcon.WALMessage) error { - return w.Write(m) -} - -func (w *byteBufferWAL) FlushAndSync() error { return nil } - -func (w *byteBufferWAL) SearchForEndHeight( - height int64, - options *cmtcon.WALSearchOptions) (rd io.ReadCloser, found bool, err error) { - return nil, false, nil -} - -func (w *byteBufferWAL) Start() error { return nil } -func (w *byteBufferWAL) Stop() error { return nil } -func (w *byteBufferWAL) Wait() {} diff --git a/test/maverick/main.go b/test/maverick/main.go deleted file mode 100644 index 50d3e18386..0000000000 --- a/test/maverick/main.go +++ /dev/null @@ -1,242 +0,0 @@ -package main - -import ( - "fmt" - "os" - "path/filepath" - - "github.com/spf13/cobra" - "github.com/spf13/viper" - - cmd "github.com/tendermint/tendermint/cmd/cometbft/commands" - "github.com/tendermint/tendermint/cmd/cometbft/commands/debug" - cfg "github.com/tendermint/tendermint/config" - "github.com/tendermint/tendermint/libs/cli" - cmtflags "github.com/tendermint/tendermint/libs/cli/flags" - "github.com/tendermint/tendermint/libs/log" - cmtos "github.com/tendermint/tendermint/libs/os" - cmtrand "github.com/tendermint/tendermint/libs/rand" - "github.com/tendermint/tendermint/p2p" - cs "github.com/tendermint/tendermint/test/maverick/consensus" - nd "github.com/tendermint/tendermint/test/maverick/node" - "github.com/tendermint/tendermint/types" - cmttime "github.com/tendermint/tendermint/types/time" -) - -var ( - config = cfg.DefaultConfig() - logger = log.NewTMLogger(log.NewSyncWriter(os.Stdout)) - misbehaviorFlag = "" -) - -func init() { - registerFlagsRootCmd(RootCmd) -} - -func registerFlagsRootCmd(command *cobra.Command) { - command.PersistentFlags().String("log_level", config.LogLevel, "Log level") -} - -func ParseConfig() (*cfg.Config, error) { - conf := cfg.DefaultConfig() - err := viper.Unmarshal(conf) - if err != nil { - return nil, err - } - conf.SetRoot(conf.RootDir) - cfg.EnsureRoot(conf.RootDir) - if err = conf.ValidateBasic(); err != nil { - return nil, fmt.Errorf("error in config file: %v", err) - } - return conf, err -} - -// RootCmd is the root command for CometBFT. -var RootCmd = &cobra.Command{ - Use: "maverick", - Short: "CometBFT Maverick Node", - Long: "CometBFT Maverick Node for testing with faulty consensus misbehaviors in a testnet. Contains " + - "all the functionality of a normal node but custom misbehaviors can be injected when running the node " + - "through a flag. See maverick node --help for how the misbehavior flag is constructured", - PersistentPreRunE: func(cmd *cobra.Command, args []string) (err error) { - fmt.Printf("use: %v, args: %v", cmd.Use, cmd.Args) - - config, err = ParseConfig() - if err != nil { - return err - } - - if config.LogFormat == cfg.LogFormatJSON { - logger = log.NewTMJSONLogger(log.NewSyncWriter(os.Stdout)) - } - - logger, err = cmtflags.ParseLogLevel(config.LogLevel, logger, cfg.DefaultLogLevel) - if err != nil { - return err - } - - if viper.GetBool(cli.TraceFlag) { - logger = log.NewTracingLogger(logger) - } - - logger = logger.With("module", "main") - return nil - }, -} - -func main() { - rootCmd := RootCmd - rootCmd.AddCommand( - ListMisbehaviorCmd, - cmd.GenValidatorCmd, - InitFilesCmd, - cmd.ProbeUpnpCmd, - cmd.ReplayCmd, - cmd.ReplayConsoleCmd, - cmd.ResetAllCmd, - cmd.ResetPrivValidatorCmd, - cmd.ShowValidatorCmd, - cmd.ShowNodeIDCmd, - cmd.GenNodeKeyCmd, - cmd.VersionCmd, - debug.DebugCmd, - cli.NewCompletionCmd(rootCmd, true), - ) - - nodeCmd := &cobra.Command{ - Use: "node", - Short: "Run the maverick node", - RunE: func(command *cobra.Command, args []string) error { - return startNode(config, logger, misbehaviorFlag) - }, - } - - cmd.AddNodeFlags(nodeCmd) - - // Create & start node - rootCmd.AddCommand(nodeCmd) - - // add special flag for misbehaviors - nodeCmd.Flags().StringVar( - &misbehaviorFlag, - "misbehaviors", - "", - "Select the misbehaviors of the node (comma-separated, no spaces in between): \n"+ - "e.g. --misbehaviors double-prevote,3\n"+ - "You can also have multiple misbehaviors: e.g. double-prevote,3,no-vote,5") - - cmd := cli.PrepareBaseCmd(rootCmd, "CMT", os.ExpandEnv(filepath.Join("$HOME", cfg.DefaultTendermintDir))) - if err := cmd.Execute(); err != nil { - panic(err) - } -} - -func startNode(config *cfg.Config, logger log.Logger, misbehaviorFlag string) error { - misbehaviors, err := nd.ParseMisbehaviors(misbehaviorFlag) - if err != nil { - return err - } - - node, err := nd.DefaultNewNode(config, logger, misbehaviors) - if err != nil { - return fmt.Errorf("failed to create node: %w", err) - } - - if err := node.Start(); err != nil { - return fmt.Errorf("failed to start node: %w", err) - } - - logger.Info("Started node", "nodeInfo", node.Switch().NodeInfo()) - - // Stop upon receiving SIGTERM or CTRL-C. - cmtos.TrapSignal(logger, func() { - if node.IsRunning() { - if err := node.Stop(); err != nil { - logger.Error("unable to stop the node", "error", err) - } - } - }) - - // Run forever. - select {} -} - -var InitFilesCmd = &cobra.Command{ - Use: "init", - Short: "Initialize CometBFT", - RunE: initFiles, -} - -func initFiles(cmd *cobra.Command, args []string) error { - return initFilesWithConfig(config) -} - -func initFilesWithConfig(config *cfg.Config) error { - // private validator - privValKeyFile := config.PrivValidatorKeyFile() - privValStateFile := config.PrivValidatorStateFile() - var pv *nd.FilePV - if cmtos.FileExists(privValKeyFile) { - pv = nd.LoadFilePV(privValKeyFile, privValStateFile) - logger.Info("Found private validator", "keyFile", privValKeyFile, - "stateFile", privValStateFile) - } else { - pv = nd.GenFilePV(privValKeyFile, privValStateFile) - pv.Save() - logger.Info("Generated private validator", "keyFile", privValKeyFile, - "stateFile", privValStateFile) - } - - nodeKeyFile := config.NodeKeyFile() - if cmtos.FileExists(nodeKeyFile) { - logger.Info("Found node key", "path", nodeKeyFile) - } else { - if _, err := p2p.LoadOrGenNodeKey(nodeKeyFile); err != nil { - return err - } - logger.Info("Generated node key", "path", nodeKeyFile) - } - - // genesis file - genFile := config.GenesisFile() - if cmtos.FileExists(genFile) { - logger.Info("Found genesis file", "path", genFile) - } else { - genDoc := types.GenesisDoc{ - ChainID: fmt.Sprintf("test-chain-%v", cmtrand.Str(6)), - GenesisTime: cmttime.Now(), - ConsensusParams: types.DefaultConsensusParams(), - } - pubKey, err := pv.GetPubKey() - if err != nil { - return fmt.Errorf("can't get pubkey: %w", err) - } - genDoc.Validators = []types.GenesisValidator{{ - Address: pubKey.Address(), - PubKey: pubKey, - Power: 10, - }} - - if err := genDoc.SaveAs(genFile); err != nil { - return err - } - logger.Info("Generated genesis file", "path", genFile) - } - - return nil -} - -var ListMisbehaviorCmd = &cobra.Command{ - Use: "misbehaviors", - Short: "Lists possible misbehaviors", - RunE: listMisbehaviors, -} - -func listMisbehaviors(cmd *cobra.Command, args []string) error { - str := "Currently registered misbehaviors: \n" - for key := range cs.MisbehaviorList { - str += fmt.Sprintf("- %s\n", key) - } - fmt.Println(str) - return nil -} diff --git a/test/maverick/node/node.go b/test/maverick/node/node.go deleted file mode 100644 index bcc198c668..0000000000 --- a/test/maverick/node/node.go +++ /dev/null @@ -1,1523 +0,0 @@ -package node - -import ( - "bytes" - "context" - "errors" - "fmt" - "net" - "net/http" - _ "net/http/pprof" //nolint: gosec // securely exposed on separate, optional port - "strconv" - "strings" - "time" - - "github.com/prometheus/client_golang/prometheus" - "github.com/prometheus/client_golang/prometheus/promhttp" - "github.com/rs/cors" - - dbm "github.com/cometbft/cometbft-db" - - abci "github.com/tendermint/tendermint/abci/types" - bcv0 "github.com/tendermint/tendermint/blockchain/v0" - bcv1 "github.com/tendermint/tendermint/blockchain/v1" - bcv2 "github.com/tendermint/tendermint/blockchain/v2" - cfg "github.com/tendermint/tendermint/config" - "github.com/tendermint/tendermint/consensus" - "github.com/tendermint/tendermint/crypto" - "github.com/tendermint/tendermint/evidence" - cmtjson "github.com/tendermint/tendermint/libs/json" - "github.com/tendermint/tendermint/libs/log" - cmtpubsub "github.com/tendermint/tendermint/libs/pubsub" - "github.com/tendermint/tendermint/libs/service" - "github.com/tendermint/tendermint/light" - mempl "github.com/tendermint/tendermint/mempool" - mempoolv2 "github.com/tendermint/tendermint/mempool/cat" - mempoolv0 "github.com/tendermint/tendermint/mempool/v0" - mempoolv1 "github.com/tendermint/tendermint/mempool/v1" - "github.com/tendermint/tendermint/p2p" - "github.com/tendermint/tendermint/p2p/pex" - "github.com/tendermint/tendermint/pkg/trace" - "github.com/tendermint/tendermint/privval" - "github.com/tendermint/tendermint/proxy" - rpccore "github.com/tendermint/tendermint/rpc/core" - grpccore "github.com/tendermint/tendermint/rpc/grpc" - rpcserver "github.com/tendermint/tendermint/rpc/jsonrpc/server" - sm "github.com/tendermint/tendermint/state" - "github.com/tendermint/tendermint/state/indexer" - blockidxkv "github.com/tendermint/tendermint/state/indexer/block/kv" - blockidxnull "github.com/tendermint/tendermint/state/indexer/block/null" - "github.com/tendermint/tendermint/state/txindex" - "github.com/tendermint/tendermint/state/txindex/kv" - "github.com/tendermint/tendermint/state/txindex/null" - "github.com/tendermint/tendermint/statesync" - "github.com/tendermint/tendermint/store" - cs "github.com/tendermint/tendermint/test/maverick/consensus" - "github.com/tendermint/tendermint/types" - cmttime "github.com/tendermint/tendermint/types/time" - "github.com/tendermint/tendermint/version" -) - -//------------------------------------------------------------------------------ - -// ParseMisbehaviors is a util function that converts a comma separated string into -// a map of misbehaviors to be executed by the maverick node -func ParseMisbehaviors(str string) (map[int64]cs.Misbehavior, error) { - // check if string is empty in which case we run a normal node - misbehaviors := make(map[int64]cs.Misbehavior) - if str == "" { - return misbehaviors, nil - } - strs := strings.Split(str, ",") - if len(strs)%2 != 0 { - return misbehaviors, errors.New("missing either height or misbehavior name in the misbehavior flag") - } -OUTER_LOOP: - for i := 0; i < len(strs); i += 2 { - height, err := strconv.ParseInt(strs[i+1], 10, 64) - if err != nil { - return misbehaviors, fmt.Errorf("failed to parse misbehavior height: %w", err) - } - for key, misbehavior := range cs.MisbehaviorList { - if key == strs[i] { - misbehaviors[height] = misbehavior - continue OUTER_LOOP - } - } - return misbehaviors, fmt.Errorf("received unknown misbehavior: %s. Did you forget to add it?", strs[i]) - } - - return misbehaviors, nil -} - -// DBContext specifies config information for loading a new DB. -type DBContext struct { - ID string - Config *cfg.Config -} - -// DBProvider takes a DBContext and returns an instantiated DB. -type DBProvider func(*DBContext) (dbm.DB, error) - -const readHeaderTimeout = 10 * time.Second - -// DefaultDBProvider returns a database using the DBBackend and DBDir -// specified in the ctx.Config. -func DefaultDBProvider(ctx *DBContext) (dbm.DB, error) { - dbType := dbm.BackendType(ctx.Config.DBBackend) - return dbm.NewDB(ctx.ID, dbType, ctx.Config.DBDir()) -} - -// GenesisDocProvider returns a GenesisDoc. -// It allows the GenesisDoc to be pulled from sources other than the -// filesystem, for instance from a distributed key-value store cluster. -type GenesisDocProvider func() (*types.GenesisDoc, error) - -// DefaultGenesisDocProviderFunc returns a GenesisDocProvider that loads -// the GenesisDoc from the config.GenesisFile() on the filesystem. -func DefaultGenesisDocProviderFunc(config *cfg.Config) GenesisDocProvider { - return func() (*types.GenesisDoc, error) { - return types.GenesisDocFromFile(config.GenesisFile()) - } -} - -// Provider takes a config and a logger and returns a ready to go Node. -type Provider func(*cfg.Config, log.Logger) (*Node, error) - -// DefaultNewNode returns a CometBFT node with default settings for the -// PrivValidator, ClientCreator, GenesisDoc, and DBProvider. -// It implements NodeProvider. -func DefaultNewNode(config *cfg.Config, logger log.Logger, misbehaviors map[int64]cs.Misbehavior) (*Node, error) { - nodeKey, err := p2p.LoadOrGenNodeKey(config.NodeKeyFile()) - if err != nil { - return nil, fmt.Errorf("failed to load or gen node key %s, err: %w", config.NodeKeyFile(), err) - } - - return NewNode(config, - LoadOrGenFilePV(config.PrivValidatorKeyFile(), config.PrivValidatorStateFile()), - nodeKey, - proxy.DefaultClientCreator(config.ProxyApp, config.ABCI, config.DBDir()), - DefaultGenesisDocProviderFunc(config), - DefaultDBProvider, - DefaultMetricsProvider(config.Instrumentation), - logger, - misbehaviors, - ) -} - -// MetricsProvider returns a consensus, p2p and mempool Metrics. -type MetricsProvider func(chainID string) (*consensus.Metrics, *p2p.Metrics, *mempl.Metrics, *sm.Metrics) - -// DefaultMetricsProvider returns Metrics build using Prometheus client library -// if Prometheus is enabled. Otherwise, it returns no-op Metrics. -func DefaultMetricsProvider(config *cfg.InstrumentationConfig) MetricsProvider { - return func(chainID string) (*consensus.Metrics, *p2p.Metrics, *mempl.Metrics, *sm.Metrics) { - if config.Prometheus { - return consensus.PrometheusMetrics(config.Namespace, "chain_id", chainID), - p2p.PrometheusMetrics(config.Namespace, "chain_id", chainID), - mempl.PrometheusMetrics(config.Namespace, "chain_id", chainID), - sm.PrometheusMetrics(config.Namespace, "chain_id", chainID) - } - return consensus.NopMetrics(), p2p.NopMetrics(), mempl.NopMetrics(), sm.NopMetrics() - } -} - -// Option sets a parameter for the node. -type Option func(*Node) - -// Temporary interface for switching to fast sync, we should get rid of v0 and v1 reactors. -// See: https://github.com/tendermint/tendermint/issues/4595 -type fastSyncReactor interface { - SwitchToFastSync(sm.State) error -} - -// CustomReactors allows you to add custom reactors (name -> p2p.Reactor) to -// the node's Switch. -// -// WARNING: using any name from the below list of the existing reactors will -// result in replacing it with the custom one. -// -// - MEMPOOL -// - BLOCKCHAIN -// - CONSENSUS -// - EVIDENCE -// - PEX -// - STATESYNC -func CustomReactors(reactors map[string]p2p.Reactor) Option { - return func(n *Node) { - for name, reactor := range reactors { - if existingReactor := n.sw.Reactor(name); existingReactor != nil { - n.sw.Logger.Info("Replacing existing reactor with a custom one", - "name", name, "existing", existingReactor, "custom", reactor) - n.sw.RemoveReactor(name, existingReactor) - } - n.sw.AddReactor(name, reactor) - } - } -} - -func CustomReactorsAsConstructors(reactors map[string]func(n *Node) p2p.Reactor) Option { - return func(n *Node) { - for name, customReactor := range reactors { - if existingReactor := n.sw.Reactor(name); existingReactor != nil { - n.sw.Logger.Info("Replacing existing reactor with a custom one", - "name", name) - n.sw.RemoveReactor(name, existingReactor) - } - n.sw.AddReactor(name, customReactor(n)) - } - } -} - -// StateProvider overrides the state provider used by state sync to retrieve trusted app hashes and -// build a State object for bootstrapping the node. -// WARNING: this interface is considered unstable and subject to change. -func StateProvider(stateProvider statesync.StateProvider) Option { - return func(n *Node) { - n.stateSyncProvider = stateProvider - } -} - -//------------------------------------------------------------------------------ - -// Node is the highest level interface to a full CometBFT node. -// It includes all configuration information and running services. -type Node struct { - service.BaseService - - // config - config *cfg.Config - genesisDoc *types.GenesisDoc // initial validator set - privValidator types.PrivValidator // local node's validator key - - // network - transport *p2p.MultiplexTransport - sw *p2p.Switch // p2p connections - addrBook pex.AddrBook // known peers - nodeInfo p2p.NodeInfo - nodeKey *p2p.NodeKey // our node privkey - isListening bool - - // services - eventBus *types.EventBus // pub/sub for services - stateStore sm.Store - blockStore *store.BlockStore // store the blockchain to disk - bcReactor p2p.Reactor // for fast-syncing - mempoolReactor p2p.Reactor // for gossipping transactions - mempool mempl.Mempool - stateSync bool // whether the node should state sync on startup - stateSyncReactor *statesync.Reactor // for hosting and restoring state sync snapshots - stateSyncProvider statesync.StateProvider // provides state data for bootstrapping a node - stateSyncGenesis sm.State // provides the genesis state for state sync - consensusState *cs.State // latest consensus state - consensusReactor *cs.Reactor // for participating in the consensus - pexReactor *pex.Reactor // for exchanging peer addresses - evidencePool *evidence.Pool // tracking evidence - proxyApp proxy.AppConns // connection to the application - rpcListeners []net.Listener // rpc servers - txIndexer txindex.TxIndexer - blockIndexer indexer.BlockIndexer - indexerService *txindex.IndexerService - prometheusSrv *http.Server -} - -func initDBs(config *cfg.Config, dbProvider DBProvider) (blockStore *store.BlockStore, stateDB dbm.DB, err error) { - var blockStoreDB dbm.DB - blockStoreDB, err = dbProvider(&DBContext{"blockstore", config}) - if err != nil { - return - } - blockStore = store.NewBlockStore(blockStoreDB) - - stateDB, err = dbProvider(&DBContext{"state", config}) - if err != nil { - return - } - - return -} - -func createAndStartProxyAppConns(clientCreator proxy.ClientCreator, logger log.Logger) (proxy.AppConns, error) { - proxyApp := proxy.NewAppConns(clientCreator) - proxyApp.SetLogger(logger.With("module", "proxy")) - if err := proxyApp.Start(); err != nil { - return nil, fmt.Errorf("error starting proxy app connections: %v", err) - } - return proxyApp, nil -} - -func createAndStartEventBus(logger log.Logger) (*types.EventBus, error) { - eventBus := types.NewEventBus() - eventBus.SetLogger(logger.With("module", "events")) - if err := eventBus.Start(); err != nil { - return nil, err - } - return eventBus, nil -} - -func createAndStartIndexerService( - config *cfg.Config, - dbProvider DBProvider, - eventBus *types.EventBus, - logger log.Logger, -) (*txindex.IndexerService, txindex.TxIndexer, indexer.BlockIndexer, error) { - var ( - txIndexer txindex.TxIndexer - blockIndexer indexer.BlockIndexer - ) - - switch config.TxIndex.Indexer { - case "kv": - store, err := dbProvider(&DBContext{"tx_index", config}) - if err != nil { - return nil, nil, nil, err - } - - txIndexer = kv.NewTxIndex(store) - blockIndexer = blockidxkv.New(dbm.NewPrefixDB(store, []byte("block_events"))) - default: - txIndexer = &null.TxIndex{} - blockIndexer = &blockidxnull.BlockerIndexer{} - } - - indexerService := txindex.NewIndexerService(txIndexer, blockIndexer, eventBus, false) - indexerService.SetLogger(logger.With("module", "txindex")) - - if err := indexerService.Start(); err != nil { - return nil, nil, nil, err - } - - return indexerService, txIndexer, blockIndexer, nil -} - -func doHandshake( - stateStore sm.Store, - state sm.State, - blockStore sm.BlockStore, - genDoc *types.GenesisDoc, - eventBus types.BlockEventPublisher, - proxyApp proxy.AppConns, - consensusLogger log.Logger, -) error { - handshaker := cs.NewHandshaker(stateStore, state, blockStore, genDoc) - handshaker.SetLogger(consensusLogger) - handshaker.SetEventBus(eventBus) - if err := handshaker.Handshake(proxyApp); err != nil { - return fmt.Errorf("error during handshake: %v", err) - } - return nil -} - -func logNodeStartupInfo(state sm.State, pubKey crypto.PubKey, logger, consensusLogger log.Logger) { - // Log the version info. - logger.Info("Version info", - "software", version.TMCoreSemVer, - "block", version.BlockProtocol, - "p2p", version.P2PProtocol, - ) - - // If the state and software differ in block version, at least log it. - if state.Version.Consensus.Block != version.BlockProtocol { - logger.Info("Software and state have different block protocols", - "software", version.BlockProtocol, - "state", state.Version.Consensus.Block, - ) - } - - addr := pubKey.Address() - // Log whether this node is a validator or an observer - if state.Validators.HasAddress(addr) { - consensusLogger.Info("This node is a validator", "addr", addr, "pubKey", pubKey) - } else { - consensusLogger.Info("This node is not a validator", "addr", addr, "pubKey", pubKey) - } -} - -func onlyValidatorIsUs(state sm.State, pubKey crypto.PubKey) bool { - if state.Validators.Size() > 1 { - return false - } - addr, _ := state.Validators.GetByIndex(0) - return bytes.Equal(pubKey.Address(), addr) -} - -func createMempoolAndMempoolReactor(config *cfg.Config, proxyApp proxy.AppConns, - state sm.State, memplMetrics *mempl.Metrics, logger log.Logger, -) (p2p.Reactor, mempl.Mempool) { - switch config.Mempool.Version { - case cfg.MempoolV2: - mp := mempoolv2.NewTxPool( - logger, - config.Mempool, - proxyApp.Mempool(), - state.LastBlockHeight, - mempoolv2.WithMetrics(memplMetrics), - mempoolv2.WithPreCheck(sm.TxPreCheck(state)), - mempoolv2.WithPostCheck(sm.TxPostCheck(state)), - ) - - reactor, err := mempoolv2.NewReactor( - mp, - &mempoolv2.ReactorOptions{ - ListenOnly: !config.Mempool.Broadcast, - MaxTxSize: config.Mempool.MaxTxBytes, - MaxGossipDelay: config.Mempool.MaxGossipDelay, - }, - ) - if err != nil { - // TODO: find a more polite way of handling this error - panic(err) - } - if config.Consensus.WaitForTxs() { - mp.EnableTxsAvailable() - } - - return reactor, mp - case cfg.MempoolV1: - mp := mempoolv1.NewTxMempool( - logger, - config.Mempool, - proxyApp.Mempool(), - state.LastBlockHeight, - mempoolv1.WithMetrics(memplMetrics), - mempoolv1.WithPreCheck(sm.TxPreCheck(state)), - mempoolv1.WithPostCheck(sm.TxPostCheck(state)), - ) - - reactor := mempoolv1.NewReactor( - config.Mempool, - mp, - trace.NoOpTracer(), - ) - if config.Consensus.WaitForTxs() { - mp.EnableTxsAvailable() - } - - return reactor, mp - - case cfg.MempoolV0: - mp := mempoolv0.NewCListMempool( - config.Mempool, - proxyApp.Mempool(), - state.LastBlockHeight, - mempoolv0.WithMetrics(memplMetrics), - mempoolv0.WithPreCheck(sm.TxPreCheck(state)), - mempoolv0.WithPostCheck(sm.TxPostCheck(state)), - ) - - mp.SetLogger(logger) - mp.SetLogger(logger) - - reactor := mempoolv0.NewReactor( - config.Mempool, - mp, - ) - if config.Consensus.WaitForTxs() { - mp.EnableTxsAvailable() - } - - return reactor, mp - - default: - return nil, nil - } -} - -func createEvidenceReactor(config *cfg.Config, dbProvider DBProvider, - stateDB dbm.DB, blockStore *store.BlockStore, logger log.Logger, -) (*evidence.Reactor, *evidence.Pool, error) { - evidenceDB, err := dbProvider(&DBContext{"evidence", config}) - if err != nil { - return nil, nil, err - } - stateStore := sm.NewStore(stateDB, sm.StoreOptions{ - DiscardABCIResponses: config.Storage.DiscardABCIResponses, - }) - evidenceLogger := logger.With("module", "evidence") - evidencePool, err := evidence.NewPool(evidenceDB, stateStore, blockStore) - if err != nil { - return nil, nil, err - } - evidenceReactor := evidence.NewReactor(evidencePool) - evidenceReactor.SetLogger(evidenceLogger) - return evidenceReactor, evidencePool, nil -} - -func createBlockchainReactor(config *cfg.Config, - state sm.State, - blockExec *sm.BlockExecutor, - blockStore *store.BlockStore, - fastSync bool, - logger log.Logger, -) (bcReactor p2p.Reactor, err error) { - switch config.FastSync.Version { - case "v0": - bcReactor = bcv0.NewBlockchainReactor(state.Copy(), blockExec, blockStore, fastSync) - case "v1": - bcReactor = bcv1.NewBlockchainReactor(state.Copy(), blockExec, blockStore, fastSync) - case "v2": - bcReactor = bcv2.NewBlockchainReactor(state.Copy(), blockExec, blockStore, fastSync) - default: - return nil, fmt.Errorf("unknown fastsync version %s", config.FastSync.Version) - } - - bcReactor.SetLogger(logger.With("module", "blockchain")) - return bcReactor, nil -} - -func createConsensusReactor(config *cfg.Config, - state sm.State, - blockExec *sm.BlockExecutor, - blockStore sm.BlockStore, - mempool mempl.Mempool, - evidencePool *evidence.Pool, - privValidator types.PrivValidator, - csMetrics *consensus.Metrics, - waitSync bool, - eventBus *types.EventBus, - consensusLogger log.Logger, - misbehaviors map[int64]cs.Misbehavior, -) (*cs.Reactor, *cs.State) { - consensusState := cs.NewState( - config.Consensus, - state.Copy(), - blockExec, - blockStore, - mempool, - evidencePool, - misbehaviors, - cs.StateMetrics(csMetrics), - ) - consensusState.SetLogger(consensusLogger) - if privValidator != nil { - consensusState.SetPrivValidator(privValidator) - } - consensusReactor := cs.NewReactor(consensusState, waitSync, cs.ReactorMetrics(csMetrics)) - consensusReactor.SetLogger(consensusLogger) - // services which will be publishing and/or subscribing for messages (events) - // consensusReactor will set it on consensusState and blockExecutor - consensusReactor.SetEventBus(eventBus) - return consensusReactor, consensusState -} - -func createTransport( - config *cfg.Config, - nodeInfo p2p.NodeInfo, - nodeKey *p2p.NodeKey, - proxyApp proxy.AppConns, -) ( - *p2p.MultiplexTransport, - []p2p.PeerFilterFunc, -) { - var ( - mConnConfig = p2p.MConnConfig(config.P2P) - transport = p2p.NewMultiplexTransport(nodeInfo, *nodeKey, mConnConfig, trace.NoOpTracer()) - connFilters = []p2p.ConnFilterFunc{} - peerFilters = []p2p.PeerFilterFunc{} - ) - - if !config.P2P.AllowDuplicateIP { - connFilters = append(connFilters, p2p.ConnDuplicateIPFilter()) - } - - // Filter peers by addr or pubkey with an ABCI query. - // If the query return code is OK, add peer. - if config.FilterPeers { - connFilters = append( - connFilters, - // ABCI query for address filtering. - func(_ p2p.ConnSet, c net.Conn, _ []net.IP) error { - res, err := proxyApp.Query().QuerySync(abci.RequestQuery{ - Path: fmt.Sprintf("/p2p/filter/addr/%s", c.RemoteAddr().String()), - }) - if err != nil { - return err - } - if res.IsErr() { - return fmt.Errorf("error querying abci app: %v", res) - } - - return nil - }, - ) - - peerFilters = append( - peerFilters, - // ABCI query for ID filtering. - func(_ p2p.IPeerSet, p p2p.Peer) error { - res, err := proxyApp.Query().QuerySync(abci.RequestQuery{ - Path: fmt.Sprintf("/p2p/filter/id/%s", p.ID()), - }) - if err != nil { - return err - } - if res.IsErr() { - return fmt.Errorf("error querying abci app: %v", res) - } - - return nil - }, - ) - } - - p2p.MultiplexTransportConnFilters(connFilters...)(transport) - - // Limit the number of incoming connections. - max := config.P2P.MaxNumInboundPeers + len(splitAndTrimEmpty(config.P2P.UnconditionalPeerIDs, ",", " ")) - p2p.MultiplexTransportMaxIncomingConnections(max)(transport) - - return transport, peerFilters -} - -func createSwitch(config *cfg.Config, - transport p2p.Transport, - p2pMetrics *p2p.Metrics, - peerFilters []p2p.PeerFilterFunc, - mempoolReactor p2p.Reactor, - bcReactor p2p.Reactor, - stateSyncReactor *statesync.Reactor, - consensusReactor *cs.Reactor, - evidenceReactor *evidence.Reactor, - nodeInfo p2p.NodeInfo, - nodeKey *p2p.NodeKey, - p2pLogger log.Logger, -) *p2p.Switch { - sw := p2p.NewSwitch( - config.P2P, - transport, - p2p.WithMetrics(p2pMetrics), - p2p.SwitchPeerFilters(peerFilters...), - ) - sw.SetLogger(p2pLogger) - sw.AddReactor("MEMPOOL", mempoolReactor) - sw.AddReactor("BLOCKCHAIN", bcReactor) - sw.AddReactor("CONSENSUS", consensusReactor) - sw.AddReactor("EVIDENCE", evidenceReactor) - sw.AddReactor("STATESYNC", stateSyncReactor) - - sw.SetNodeInfo(nodeInfo) - sw.SetNodeKey(nodeKey) - - p2pLogger.Info("P2P Node ID", "ID", nodeKey.ID(), "file", config.NodeKeyFile()) - return sw -} - -func createAddrBookAndSetOnSwitch(config *cfg.Config, sw *p2p.Switch, - p2pLogger log.Logger, nodeKey *p2p.NodeKey, -) (pex.AddrBook, error) { - addrBook := pex.NewAddrBook(config.P2P.AddrBookFile(), config.P2P.AddrBookStrict) - addrBook.SetLogger(p2pLogger.With("book", config.P2P.AddrBookFile())) - - // Add ourselves to addrbook to prevent dialing ourselves - if config.P2P.ExternalAddress != "" { - addr, err := p2p.NewNetAddressString(p2p.IDAddressString(nodeKey.ID(), config.P2P.ExternalAddress)) - if err != nil { - return nil, fmt.Errorf("p2p.external_address is incorrect: %w", err) - } - addrBook.AddOurAddress(addr) - } - if config.P2P.ListenAddress != "" { - addr, err := p2p.NewNetAddressString(p2p.IDAddressString(nodeKey.ID(), config.P2P.ListenAddress)) - if err != nil { - return nil, fmt.Errorf("p2p.laddr is incorrect: %w", err) - } - addrBook.AddOurAddress(addr) - } - - sw.SetAddrBook(addrBook) - - return addrBook, nil -} - -func createPEXReactorAndAddToSwitch(addrBook pex.AddrBook, config *cfg.Config, - sw *p2p.Switch, logger log.Logger, -) *pex.Reactor { - // TODO persistent peers ? so we can have their DNS addrs saved - pexReactor := pex.NewReactor(addrBook, - &pex.ReactorConfig{ - Seeds: splitAndTrimEmpty(config.P2P.Seeds, ",", " "), - SeedMode: config.P2P.SeedMode, - // See consensus/reactor.go: blocksToContributeToBecomeGoodPeer 10000 - // blocks assuming 10s blocks ~ 28 hours. - // TODO (melekes): make it dynamic based on the actual block latencies - // from the live network. - // https://github.com/tendermint/tendermint/issues/3523 - SeedDisconnectWaitPeriod: 28 * time.Hour, - PersistentPeersMaxDialPeriod: config.P2P.PersistentPeersMaxDialPeriod, - }) - pexReactor.SetLogger(logger.With("module", "pex")) - sw.AddReactor("PEX", pexReactor) - return pexReactor -} - -// startStateSync starts an asynchronous state sync process, then switches to fast sync mode. -func startStateSync(ssR *statesync.Reactor, bcR fastSyncReactor, conR *cs.Reactor, - stateProvider statesync.StateProvider, config *cfg.StateSyncConfig, fastSync bool, - stateStore sm.Store, blockStore *store.BlockStore, state sm.State, -) error { - ssR.Logger.Info("Starting state sync") - - if stateProvider == nil { - var err error - ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) - defer cancel() - stateProvider, err = statesync.NewLightClientStateProvider( - ctx, - state.ChainID, state.Version, state.InitialHeight, - config.RPCServers, light.TrustOptions{ - Period: config.TrustPeriod, - Height: config.TrustHeight, - Hash: config.TrustHashBytes(), - }, ssR.Logger.With("module", "light")) - if err != nil { - return fmt.Errorf("failed to set up light client state provider: %w", err) - } - } - - go func() { - state, commit, err := ssR.Sync(stateProvider, config.DiscoveryTime) - if err != nil { - ssR.Logger.Error("State sync failed", "err", err) - return - } - err = stateStore.Bootstrap(state) - if err != nil { - ssR.Logger.Error("Failed to bootstrap node with new state", "err", err) - return - } - err = blockStore.SaveSeenCommit(state.LastBlockHeight, commit) - if err != nil { - ssR.Logger.Error("Failed to store last seen commit", "err", err) - return - } - - if fastSync { - // FIXME Very ugly to have these metrics bleed through here. - conR.Metrics.StateSyncing.Set(0) - conR.Metrics.FastSyncing.Set(1) - err = bcR.SwitchToFastSync(state) - if err != nil { - ssR.Logger.Error("Failed to switch to fast sync", "err", err) - return - } - } else { - conR.SwitchToConsensus(state, true) - } - }() - return nil -} - -// NewNode returns a new, ready to go, CometBFT Node. -func NewNode(config *cfg.Config, - privValidator types.PrivValidator, - nodeKey *p2p.NodeKey, - clientCreator proxy.ClientCreator, - genesisDocProvider GenesisDocProvider, - dbProvider DBProvider, - metricsProvider MetricsProvider, - logger log.Logger, - misbehaviors map[int64]cs.Misbehavior, - options ...Option, -) (*Node, error) { - blockStore, stateDB, err := initDBs(config, dbProvider) - if err != nil { - return nil, err - } - - stateStore := sm.NewStore(stateDB, sm.StoreOptions{ - DiscardABCIResponses: false, - }) - - state, genDoc, err := LoadStateFromDBOrGenesisDocProvider(stateDB, genesisDocProvider) - if err != nil { - return nil, err - } - - // Create the proxyApp and establish connections to the ABCI app (consensus, mempool, query). - proxyApp, err := createAndStartProxyAppConns(clientCreator, logger) - if err != nil { - return nil, err - } - - // EventBus and IndexerService must be started before the handshake because - // we might need to index the txs of the replayed block as this might not have happened - // when the node stopped last time (i.e. the node stopped after it saved the block - // but before it indexed the txs, or, endblocker panicked) - eventBus, err := createAndStartEventBus(logger) - if err != nil { - return nil, err - } - - indexerService, txIndexer, blockIndexer, err := createAndStartIndexerService(config, dbProvider, eventBus, logger) - if err != nil { - return nil, err - } - - // If an address is provided, listen on the socket for a connection from an - // external signing process. - if config.PrivValidatorListenAddr != "" { - // FIXME: we should start services inside OnStart - privValidator, err = createAndStartPrivValidatorSocketClient(config.PrivValidatorListenAddr, genDoc.ChainID, logger) - if err != nil { - return nil, fmt.Errorf("error with private validator socket client: %w", err) - } - } - - pubKey, err := privValidator.GetPubKey() - if err != nil { - return nil, fmt.Errorf("can't get pubkey: %w", err) - } - - // Determine whether we should do state and/or fast sync. - // We don't fast-sync when the only validator is us. - fastSync := config.FastSyncMode && !onlyValidatorIsUs(state, pubKey) - stateSync := config.StateSync.Enable && !onlyValidatorIsUs(state, pubKey) - if stateSync && state.LastBlockHeight > 0 { - logger.Info("Found local state with non-zero height, skipping state sync") - stateSync = false - } - - // Create the handshaker, which calls RequestInfo, sets the AppVersion on the state, - // and replays any blocks as necessary to sync CometBFT with the app. - consensusLogger := logger.With("module", "consensus") - if !stateSync { - if err := doHandshake(stateStore, state, blockStore, genDoc, eventBus, proxyApp, consensusLogger); err != nil { - return nil, err - } - - // Reload the state. It will have the Version.Consensus.App set by the - // Handshake, and may have other modifications as well (ie. depending on - // what happened during block replay). - state, err = stateStore.Load() - if err != nil { - return nil, fmt.Errorf("cannot load state: %w", err) - } - } - - logNodeStartupInfo(state, pubKey, logger, consensusLogger) - - csMetrics, p2pMetrics, memplMetrics, smMetrics := metricsProvider(genDoc.ChainID) - - // Make MempoolReactor - mempoolReactor, mempool := createMempoolAndMempoolReactor(config, proxyApp, state, memplMetrics, logger) - - // Make Evidence Reactor - evidenceReactor, evidencePool, err := createEvidenceReactor(config, dbProvider, stateDB, blockStore, logger) - if err != nil { - return nil, err - } - - // make block executor for consensus and blockchain reactors to execute blocks - blockExec := sm.NewBlockExecutor( - stateStore, - logger.With("module", "state"), - proxyApp.Consensus(), - mempool, - evidencePool, - sm.BlockExecutorWithMetrics(smMetrics), - ) - - // Make BlockchainReactor. Don't start fast sync if we're doing a state sync first. - bcReactor, err := createBlockchainReactor(config, state, blockExec, blockStore, fastSync && !stateSync, logger) - if err != nil { - return nil, fmt.Errorf("could not create blockchain reactor: %w", err) - } - - // Make ConsensusReactor. Don't enable fully if doing a state sync and/or fast sync first. - // FIXME We need to update metrics here, since other reactors don't have access to them. - if stateSync { - csMetrics.StateSyncing.Set(1) - } else if fastSync { - csMetrics.FastSyncing.Set(1) - } - - logger.Info("Setting up maverick consensus reactor", "Misbehaviors", misbehaviors) - consensusReactor, consensusState := createConsensusReactor( - config, state, blockExec, blockStore, mempool, evidencePool, - privValidator, csMetrics, stateSync || fastSync, eventBus, consensusLogger, misbehaviors) - - // Set up state sync reactor, and schedule a sync if requested. - // FIXME The way we do phased startups (e.g. replay -> fast sync -> consensus) is very messy, - // we should clean this whole thing up. See: - // https://github.com/tendermint/tendermint/issues/4644 - stateSyncReactor := statesync.NewReactor( - *config.StateSync, - proxyApp.Snapshot(), - proxyApp.Query(), - config.StateSync.TempDir, - ) - stateSyncReactor.SetLogger(logger.With("module", "statesync")) - - nodeInfo, err := makeNodeInfo(config, nodeKey, txIndexer, genDoc, state) - if err != nil { - return nil, err - } - - // Setup Transport. - transport, peerFilters := createTransport(config, nodeInfo, nodeKey, proxyApp) - - // Setup Switch. - p2pLogger := logger.With("module", "p2p") - sw := createSwitch( - config, transport, p2pMetrics, peerFilters, mempoolReactor, bcReactor, - stateSyncReactor, consensusReactor, evidenceReactor, nodeInfo, nodeKey, p2pLogger, - ) - - err = sw.AddPersistentPeers(splitAndTrimEmpty(config.P2P.PersistentPeers, ",", " ")) - if err != nil { - return nil, fmt.Errorf("could not add peers from persistent_peers field: %w", err) - } - - err = sw.AddUnconditionalPeerIDs(splitAndTrimEmpty(config.P2P.UnconditionalPeerIDs, ",", " ")) - if err != nil { - return nil, fmt.Errorf("could not add peer ids from unconditional_peer_ids field: %w", err) - } - - addrBook, err := createAddrBookAndSetOnSwitch(config, sw, p2pLogger, nodeKey) - if err != nil { - return nil, fmt.Errorf("could not create addrbook: %w", err) - } - - // Optionally, start the pex reactor - // - // TODO: - // - // We need to set Seeds and PersistentPeers on the switch, - // since it needs to be able to use these (and their DNS names) - // even if the PEX is off. We can include the DNS name in the NetAddress, - // but it would still be nice to have a clear list of the current "PersistentPeers" - // somewhere that we can return with net_info. - // - // If PEX is on, it should handle dialing the seeds. Otherwise the switch does it. - // Note we currently use the addrBook regardless at least for AddOurAddress - var pexReactor *pex.Reactor - if config.P2P.PexReactor { - pexReactor = createPEXReactorAndAddToSwitch(addrBook, config, sw, logger) - } - - if config.RPC.PprofListenAddress != "" { - go func() { - logger.Info("Starting pprof server", "laddr", config.RPC.PprofListenAddress) - //nolint:gosec,nolintlint // G114: Use of net/http serve function that has no support for setting timeouts - logger.Error("pprof server error", "err", http.ListenAndServe(config.RPC.PprofListenAddress, nil)) - }() - } - - node := &Node{ - config: config, - genesisDoc: genDoc, - privValidator: privValidator, - - transport: transport, - sw: sw, - addrBook: addrBook, - nodeInfo: nodeInfo, - nodeKey: nodeKey, - - stateStore: stateStore, - blockStore: blockStore, - bcReactor: bcReactor, - mempoolReactor: mempoolReactor, - mempool: mempool, - consensusState: consensusState, - consensusReactor: consensusReactor, - stateSyncReactor: stateSyncReactor, - stateSync: stateSync, - stateSyncGenesis: state, // Shouldn't be necessary, but need a way to pass the genesis state - pexReactor: pexReactor, - evidencePool: evidencePool, - proxyApp: proxyApp, - txIndexer: txIndexer, - indexerService: indexerService, - blockIndexer: blockIndexer, - eventBus: eventBus, - } - node.BaseService = *service.NewBaseService(logger, "Node", node) - - for _, option := range options { - option(node) - } - - return node, nil -} - -// OnStart starts the Node. It implements service.Service. -func (n *Node) OnStart() error { - now := cmttime.Now() - genTime := n.genesisDoc.GenesisTime - if genTime.After(now) { - n.Logger.Info("Genesis time is in the future. Sleeping until then...", "genTime", genTime) - time.Sleep(genTime.Sub(now)) - } - - // Add private IDs to addrbook to block those peers being added - n.addrBook.AddPrivateIDs(splitAndTrimEmpty(n.config.P2P.PrivatePeerIDs, ",", " ")) - - // Start the RPC server before the P2P server - // so we can eg. receive txs for the first block - if n.config.RPC.ListenAddress != "" { - listeners, err := n.startRPC() - if err != nil { - return err - } - n.rpcListeners = listeners - } - - if n.config.Instrumentation.Prometheus && - n.config.Instrumentation.PrometheusListenAddr != "" { - n.prometheusSrv = n.startPrometheusServer(n.config.Instrumentation.PrometheusListenAddr) - } - - // Start the transport. - addr, err := p2p.NewNetAddressString(p2p.IDAddressString(n.nodeKey.ID(), n.config.P2P.ListenAddress)) - if err != nil { - return err - } - if err := n.transport.Listen(*addr); err != nil { - return err - } - - n.isListening = true - - // Start the switch (the P2P server). - err = n.sw.Start() - if err != nil { - return err - } - - // Always connect to persistent peers - err = n.sw.DialPeersAsync(splitAndTrimEmpty(n.config.P2P.PersistentPeers, ",", " ")) - if err != nil { - return fmt.Errorf("could not dial peers from persistent_peers field: %w", err) - } - - // Run state sync - if n.stateSync { - bcR, ok := n.bcReactor.(fastSyncReactor) - if !ok { - return fmt.Errorf("this blockchain reactor does not support switching from state sync") - } - err := startStateSync(n.stateSyncReactor, bcR, n.consensusReactor, n.stateSyncProvider, - n.config.StateSync, n.config.FastSyncMode, n.stateStore, n.blockStore, n.stateSyncGenesis) - if err != nil { - return fmt.Errorf("failed to start state sync: %w", err) - } - } - - return nil -} - -// OnStop stops the Node. It implements service.Service. -func (n *Node) OnStop() { - n.BaseService.OnStop() - - n.Logger.Info("Stopping Node") - - // first stop the non-reactor services - if err := n.eventBus.Stop(); err != nil { - n.Logger.Error("Error closing eventBus", "err", err) - } - if err := n.indexerService.Stop(); err != nil { - n.Logger.Error("Error closing indexerService", "err", err) - } - - // now stop the reactors - if err := n.sw.Stop(); err != nil { - n.Logger.Error("Error closing switch", "err", err) - } - - if err := n.transport.Close(); err != nil { - n.Logger.Error("Error closing transport", "err", err) - } - - n.isListening = false - - // finally stop the listeners / external services - for _, l := range n.rpcListeners { - n.Logger.Info("Closing rpc listener", "listener", l) - if err := l.Close(); err != nil { - n.Logger.Error("Error closing listener", "listener", l, "err", err) - } - } - - if pvsc, ok := n.privValidator.(service.Service); ok { - if err := pvsc.Stop(); err != nil { - n.Logger.Error("Error closing private validator", "err", err) - } - } - - if n.prometheusSrv != nil { - if err := n.prometheusSrv.Shutdown(context.Background()); err != nil { - // Error from closing listeners, or context timeout: - n.Logger.Error("Prometheus HTTP server Shutdown", "err", err) - } - } -} - -// ConfigureRPC makes sure RPC has all the objects it needs to operate. -func (n *Node) ConfigureRPC() error { - pubKey, err := n.privValidator.GetPubKey() - if err != nil { - return fmt.Errorf("can't get pubkey: %w", err) - } - rpccore.SetEnvironment(&rpccore.Environment{ - ProxyAppQuery: n.proxyApp.Query(), - ProxyAppMempool: n.proxyApp.Mempool(), - - StateStore: n.stateStore, - BlockStore: n.blockStore, - EvidencePool: n.evidencePool, - ConsensusState: n.consensusState, - P2PPeers: n.sw, - P2PTransport: n, - - PubKey: pubKey, - GenDoc: n.genesisDoc, - TxIndexer: n.txIndexer, - BlockIndexer: n.blockIndexer, - ConsensusReactor: &consensus.Reactor{}, - EventBus: n.eventBus, - Mempool: n.mempool, - - Logger: n.Logger.With("module", "rpc"), - - Config: *n.config.RPC, - }) - return nil -} - -func (n *Node) startRPC() ([]net.Listener, error) { - err := n.ConfigureRPC() - if err != nil { - return nil, err - } - - listenAddrs := splitAndTrimEmpty(n.config.RPC.ListenAddress, ",", " ") - - if n.config.RPC.Unsafe { - rpccore.AddUnsafeRoutes() - } - - config := rpcserver.DefaultConfig() - config.MaxBodyBytes = n.config.RPC.MaxBodyBytes - config.MaxHeaderBytes = n.config.RPC.MaxHeaderBytes - config.MaxOpenConnections = n.config.RPC.MaxOpenConnections - // If necessary adjust global WriteTimeout to ensure it's greater than - // TimeoutBroadcastTxCommit. - // See https://github.com/tendermint/tendermint/issues/3435 - if config.WriteTimeout <= n.config.RPC.TimeoutBroadcastTxCommit { - config.WriteTimeout = n.config.RPC.TimeoutBroadcastTxCommit + 1*time.Second - } - - // we may expose the rpc over both a unix and tcp socket - listeners := make([]net.Listener, len(listenAddrs)) - for i, listenAddr := range listenAddrs { - mux := http.NewServeMux() - rpcLogger := n.Logger.With("module", "rpc-server") - wmLogger := rpcLogger.With("protocol", "websocket") - wm := rpcserver.NewWebsocketManager(rpccore.Routes, - rpcserver.OnDisconnect(func(remoteAddr string) { - err := n.eventBus.UnsubscribeAll(context.Background(), remoteAddr) - if err != nil && err != cmtpubsub.ErrSubscriptionNotFound { - wmLogger.Error("Failed to unsubscribe addr from events", "addr", remoteAddr, "err", err) - } - }), - rpcserver.ReadLimit(config.MaxBodyBytes), - ) - wm.SetLogger(wmLogger) - mux.HandleFunc("/websocket", wm.WebsocketHandler) - rpcserver.RegisterRPCFuncs(mux, rpccore.Routes, rpcLogger) - listener, err := rpcserver.Listen( - listenAddr, - config, - ) - if err != nil { - return nil, err - } - - var rootHandler http.Handler = mux - if n.config.RPC.IsCorsEnabled() { - corsMiddleware := cors.New(cors.Options{ - AllowedOrigins: n.config.RPC.CORSAllowedOrigins, - AllowedMethods: n.config.RPC.CORSAllowedMethods, - AllowedHeaders: n.config.RPC.CORSAllowedHeaders, - }) - rootHandler = corsMiddleware.Handler(mux) - } - if n.config.RPC.IsTLSEnabled() { - go func() { - if err := rpcserver.ServeTLS( - listener, - rootHandler, - n.config.RPC.CertFile(), - n.config.RPC.KeyFile(), - rpcLogger, - config, - ); err != nil { - n.Logger.Error("Error serving server with TLS", "err", err) - } - }() - } else { - go func() { - if err := rpcserver.Serve( - listener, - rootHandler, - rpcLogger, - config, - ); err != nil { - n.Logger.Error("Error serving server", "err", err) - } - }() - } - - listeners[i] = listener - } - - // we expose a simplified api over grpc for convenience to app devs - grpcListenAddr := n.config.RPC.GRPCListenAddress - if grpcListenAddr != "" { - config := rpcserver.DefaultConfig() - config.MaxBodyBytes = n.config.RPC.MaxBodyBytes - config.MaxHeaderBytes = n.config.RPC.MaxHeaderBytes - // NOTE: GRPCMaxOpenConnections is used, not MaxOpenConnections - config.MaxOpenConnections = n.config.RPC.GRPCMaxOpenConnections - // If necessary adjust global WriteTimeout to ensure it's greater than - // TimeoutBroadcastTxCommit. - // See https://github.com/tendermint/tendermint/issues/3435 - if config.WriteTimeout <= n.config.RPC.TimeoutBroadcastTxCommit { - config.WriteTimeout = n.config.RPC.TimeoutBroadcastTxCommit + 1*time.Second - } - listener, err := rpcserver.Listen(grpcListenAddr, config) - if err != nil { - return nil, err - } - go func() { - if err := grpccore.StartGRPCServer(listener); err != nil { - n.Logger.Error("Error starting gRPC server", "err", err) - } - }() - listeners = append(listeners, listener) - } - - return listeners, nil -} - -// startPrometheusServer starts a Prometheus HTTP server, listening for metrics -// collectors on addr. -func (n *Node) startPrometheusServer(addr string) *http.Server { - srv := &http.Server{ - Addr: addr, - Handler: promhttp.InstrumentMetricHandler( - prometheus.DefaultRegisterer, promhttp.HandlerFor( - prometheus.DefaultGatherer, - promhttp.HandlerOpts{MaxRequestsInFlight: n.config.Instrumentation.MaxOpenConnections}, - ), - ), - ReadHeaderTimeout: readHeaderTimeout, - } - go func() { - if err := srv.ListenAndServe(); err != http.ErrServerClosed { - // Error starting or closing listener: - n.Logger.Error("Prometheus HTTP server ListenAndServe", "err", err) - } - }() - return srv -} - -// Switch returns the Node's Switch. -func (n *Node) Switch() *p2p.Switch { - return n.sw -} - -// BlockStore returns the Node's BlockStore. -func (n *Node) BlockStore() *store.BlockStore { - return n.blockStore -} - -// ConsensusState returns the Node's ConsensusState. -func (n *Node) ConsensusState() *cs.State { - return n.consensusState -} - -// ConsensusReactor returns the Node's ConsensusReactor. -func (n *Node) ConsensusReactor() *cs.Reactor { - return n.consensusReactor -} - -// MempoolReactor returns the Node's mempool reactor. -func (n *Node) MempoolReactor() p2p.Reactor { - return n.mempoolReactor -} - -// Mempool returns the Node's mempool. -func (n *Node) Mempool() mempl.Mempool { - return n.mempool -} - -// PEXReactor returns the Node's PEXReactor. It returns nil if PEX is disabled. -func (n *Node) PEXReactor() *pex.Reactor { - return n.pexReactor -} - -// EvidencePool returns the Node's EvidencePool. -func (n *Node) EvidencePool() *evidence.Pool { - return n.evidencePool -} - -// EventBus returns the Node's EventBus. -func (n *Node) EventBus() *types.EventBus { - return n.eventBus -} - -// PrivValidator returns the Node's PrivValidator. -// XXX: for convenience only! -func (n *Node) PrivValidator() types.PrivValidator { - return n.privValidator -} - -// GenesisDoc returns the Node's GenesisDoc. -func (n *Node) GenesisDoc() *types.GenesisDoc { - return n.genesisDoc -} - -// ProxyApp returns the Node's AppConns, representing its connections to the ABCI application. -func (n *Node) ProxyApp() proxy.AppConns { - return n.proxyApp -} - -// Config returns the Node's config. -func (n *Node) Config() *cfg.Config { - return n.config -} - -//------------------------------------------------------------------------------ - -func (n *Node) Listeners() []string { - return []string{ - fmt.Sprintf("Listener(@%v)", n.config.P2P.ExternalAddress), - } -} - -func (n *Node) IsListening() bool { - return n.isListening -} - -// NodeInfo returns the Node's Info from the Switch. -func (n *Node) NodeInfo() p2p.NodeInfo { - return n.nodeInfo -} - -func makeNodeInfo( - config *cfg.Config, - nodeKey *p2p.NodeKey, - txIndexer txindex.TxIndexer, - genDoc *types.GenesisDoc, - state sm.State, -) (p2p.NodeInfo, error) { - txIndexerStatus := "on" - if _, ok := txIndexer.(*null.TxIndex); ok { - txIndexerStatus = "off" - } - - var bcChannel byte - switch config.FastSync.Version { - case "v0": - bcChannel = bcv0.BlockchainChannel - case "v1": - bcChannel = bcv1.BlockchainChannel - case "v2": - bcChannel = bcv2.BlockchainChannel - default: - return nil, fmt.Errorf("unknown fastsync version %s", config.FastSync.Version) - } - - nodeInfo := p2p.DefaultNodeInfo{ - ProtocolVersion: p2p.NewProtocolVersion( - version.P2PProtocol, // global - state.Version.Consensus.Block, - state.Version.Consensus.App, - ), - DefaultNodeID: nodeKey.ID(), - Network: genDoc.ChainID, - Version: version.TMCoreSemVer, - Channels: []byte{ - bcChannel, - cs.StateChannel, cs.DataChannel, cs.VoteChannel, cs.VoteSetBitsChannel, - mempl.MempoolChannel, - evidence.EvidenceChannel, - statesync.SnapshotChannel, statesync.ChunkChannel, - }, - Moniker: config.Moniker, - Other: p2p.DefaultNodeInfoOther{ - TxIndex: txIndexerStatus, - RPCAddress: config.RPC.ListenAddress, - }, - } - - if config.P2P.PexReactor { - nodeInfo.Channels = append(nodeInfo.Channels, pex.PexChannel) - } - - lAddr := config.P2P.ExternalAddress - - if lAddr == "" { - lAddr = config.P2P.ListenAddress - } - - nodeInfo.ListenAddr = lAddr - - err := nodeInfo.Validate() - return nodeInfo, err -} - -//------------------------------------------------------------------------------ - -var genesisDocKey = []byte("genesisDoc") - -// LoadStateFromDBOrGenesisDocProvider attempts to load the state from the -// database, or creates one using the given genesisDocProvider and persists the -// result to the database. On success this also returns the genesis doc loaded -// through the given provider. -func LoadStateFromDBOrGenesisDocProvider( - stateDB dbm.DB, - genesisDocProvider GenesisDocProvider, -) (sm.State, *types.GenesisDoc, error) { - // Get genesis doc - genDoc, err := loadGenesisDoc(stateDB) - if err != nil { - genDoc, err = genesisDocProvider() - if err != nil { - return sm.State{}, nil, err - } - // save genesis doc to prevent a certain class of user errors (e.g. when it - // was changed, accidentally or not). Also good for audit trail. - saveGenesisDoc(stateDB, genDoc) - } - stateStore := sm.NewStore(stateDB, sm.StoreOptions{ - DiscardABCIResponses: false, - }) - state, err := stateStore.LoadFromDBOrGenesisDoc(genDoc) - if err != nil { - return sm.State{}, nil, err - } - return state, genDoc, nil -} - -// panics if failed to unmarshal bytes -func loadGenesisDoc(db dbm.DB) (*types.GenesisDoc, error) { - b, err := db.Get(genesisDocKey) - if err != nil { - panic(err) - } - if len(b) == 0 { - return nil, errors.New("genesis doc not found") - } - var genDoc *types.GenesisDoc - err = cmtjson.Unmarshal(b, &genDoc) - if err != nil { - panic(fmt.Sprintf("Failed to load genesis doc due to unmarshaling error: %v (bytes: %X)", err, b)) - } - return genDoc, nil -} - -// panics if failed to marshal the given genesis document -func saveGenesisDoc(db dbm.DB, genDoc *types.GenesisDoc) { - b, err := cmtjson.Marshal(genDoc) - if err != nil { - panic(fmt.Sprintf("Failed to save genesis doc due to marshaling error: %v", err)) - } - if err := db.SetSync(genesisDocKey, b); err != nil { - panic(fmt.Sprintf("Failed to save genesis doc: %v", err)) - } -} - -func createAndStartPrivValidatorSocketClient( - listenAddr, - chainID string, - logger log.Logger, -) (types.PrivValidator, error) { - pve, err := privval.NewSignerListener(listenAddr, logger) - if err != nil { - return nil, fmt.Errorf("failed to start private validator: %w", err) - } - - pvsc, err := privval.NewSignerClient(pve, chainID) - if err != nil { - return nil, fmt.Errorf("failed to start private validator: %w", err) - } - - // try to get a pubkey from private validate first time - _, err = pvsc.GetPubKey() - if err != nil { - return nil, fmt.Errorf("can't get pubkey: %w", err) - } - - const ( - retries = 50 // 50 * 100ms = 5s total - timeout = 100 * time.Millisecond - ) - pvscWithRetries := privval.NewRetrySignerClient(pvsc, retries, timeout) - - return pvscWithRetries, nil -} - -// splitAndTrimEmpty slices s into all subslices separated by sep and returns a -// slice of the string s with all leading and trailing Unicode code points -// contained in cutset removed. If sep is empty, SplitAndTrim splits after each -// UTF-8 sequence. First part is equivalent to strings.SplitN with a count of -// -1. also filter out empty strings, only return non-empty strings. -func splitAndTrimEmpty(s, sep, cutset string) []string { - if s == "" { - return []string{} - } - - spl := strings.Split(s, sep) - nonEmptyStrings := make([]string, 0, len(spl)) - for i := 0; i < len(spl); i++ { - element := strings.Trim(spl[i], cutset) - if element != "" { - nonEmptyStrings = append(nonEmptyStrings, element) - } - } - return nonEmptyStrings -} diff --git a/test/maverick/node/privval.go b/test/maverick/node/privval.go deleted file mode 100644 index 094b164fd4..0000000000 --- a/test/maverick/node/privval.go +++ /dev/null @@ -1,356 +0,0 @@ -package node - -import ( - "errors" - "fmt" - "os" - - "github.com/tendermint/tendermint/crypto" - "github.com/tendermint/tendermint/crypto/ed25519" - cmtbytes "github.com/tendermint/tendermint/libs/bytes" - cmtjson "github.com/tendermint/tendermint/libs/json" - cmtos "github.com/tendermint/tendermint/libs/os" - "github.com/tendermint/tendermint/libs/tempfile" - cmtproto "github.com/tendermint/tendermint/proto/tendermint/types" - "github.com/tendermint/tendermint/types" -) - -// ******************************************************************************************************************* -// -// WARNING: FOR TESTING ONLY. DO NOT USE THIS FILE OUTSIDE MAVERICK -// -// ******************************************************************************************************************* - -const ( - stepNone int8 = 0 // Used to distinguish the initial state - stepPropose int8 = 1 - stepPrevote int8 = 2 - stepPrecommit int8 = 3 -) - -// A vote is either stepPrevote or stepPrecommit. -func voteToStep(vote *cmtproto.Vote) int8 { - switch vote.Type { - case cmtproto.PrevoteType: - return stepPrevote - case cmtproto.PrecommitType: - return stepPrecommit - default: - panic(fmt.Sprintf("Unknown vote type: %v", vote.Type)) - } -} - -//------------------------------------------------------------------------------- - -// FilePVKey stores the immutable part of PrivValidator. -type FilePVKey struct { - Address types.Address `json:"address"` - PubKey crypto.PubKey `json:"pub_key"` - PrivKey crypto.PrivKey `json:"priv_key"` - - filePath string -} - -// Save persists the FilePVKey to its filePath. -func (pvKey FilePVKey) Save() { - outFile := pvKey.filePath - if outFile == "" { - panic("cannot save PrivValidator key: filePath not set") - } - - jsonBytes, err := cmtjson.MarshalIndent(pvKey, "", " ") - if err != nil { - panic(err) - } - err = tempfile.WriteFileAtomic(outFile, jsonBytes, 0o600) - if err != nil { - panic(err) - } -} - -//------------------------------------------------------------------------------- - -// FilePVLastSignState stores the mutable part of PrivValidator. -type FilePVLastSignState struct { - Height int64 `json:"height"` - Round int32 `json:"round"` - Step int8 `json:"step"` - Signature []byte `json:"signature,omitempty"` - SignBytes cmtbytes.HexBytes `json:"signbytes,omitempty"` - - filePath string -} - -// CheckHRS checks the given height, round, step (HRS) against that of the -// FilePVLastSignState. It returns an error if the arguments constitute a regression, -// or if they match but the SignBytes are empty. -// The returned boolean indicates whether the last Signature should be reused - -// it returns true if the HRS matches the arguments and the SignBytes are not empty (indicating -// we have already signed for this HRS, and can reuse the existing signature). -// It panics if the HRS matches the arguments, there's a SignBytes, but no Signature. -func (lss *FilePVLastSignState) CheckHRS(height int64, round int32, step int8) (bool, error) { - if lss.Height > height { - return false, fmt.Errorf("height regression. Got %v, last height %v", height, lss.Height) - } - - if lss.Height == height { - if lss.Round > round { - return false, fmt.Errorf("round regression at height %v. Got %v, last round %v", height, round, lss.Round) - } - - if lss.Round == round { - if lss.Step > step { - return false, fmt.Errorf( - "step regression at height %v round %v. Got %v, last step %v", - height, - round, - step, - lss.Step, - ) - } else if lss.Step == step { - if lss.SignBytes != nil { - if lss.Signature == nil { - panic("pv: Signature is nil but SignBytes is not!") - } - return true, nil - } - return false, errors.New("no SignBytes found") - } - } - } - return false, nil -} - -// Save persists the FilePvLastSignState to its filePath. -func (lss *FilePVLastSignState) Save() { - outFile := lss.filePath - if outFile == "" { - panic("cannot save FilePVLastSignState: filePath not set") - } - jsonBytes, err := cmtjson.MarshalIndent(lss, "", " ") - if err != nil { - panic(err) - } - err = tempfile.WriteFileAtomic(outFile, jsonBytes, 0o600) - if err != nil { - panic(err) - } -} - -//------------------------------------------------------------------------------- - -// FilePV implements PrivValidator using data persisted to disk -// to prevent double signing. -// NOTE: the directories containing pv.Key.filePath and pv.LastSignState.filePath must already exist. -// It includes the LastSignature and LastSignBytes so we don't lose the signature -// if the process crashes after signing but before the resulting consensus message is processed. -type FilePV struct { - Key FilePVKey - LastSignState FilePVLastSignState -} - -// GenFilePV generates a new validator with randomly generated private key -// and sets the filePaths, but does not call Save(). -func GenFilePV(keyFilePath, stateFilePath string) *FilePV { - privKey := ed25519.GenPrivKey() - - return &FilePV{ - Key: FilePVKey{ - Address: privKey.PubKey().Address(), - PubKey: privKey.PubKey(), - PrivKey: privKey, - filePath: keyFilePath, - }, - LastSignState: FilePVLastSignState{ - Step: stepNone, - filePath: stateFilePath, - }, - } -} - -// LoadFilePV loads a FilePV from the filePaths. The FilePV handles double -// signing prevention by persisting data to the stateFilePath. If either file path -// does not exist, the program will exit. -func LoadFilePV(keyFilePath, stateFilePath string) *FilePV { - return loadFilePV(keyFilePath, stateFilePath, true) -} - -// LoadFilePVEmptyState loads a FilePV from the given keyFilePath, with an empty LastSignState. -// If the keyFilePath does not exist, the program will exit. -func LoadFilePVEmptyState(keyFilePath, stateFilePath string) *FilePV { - return loadFilePV(keyFilePath, stateFilePath, false) -} - -// If loadState is true, we load from the stateFilePath. Otherwise, we use an empty LastSignState. -func loadFilePV(keyFilePath, stateFilePath string, loadState bool) *FilePV { - keyJSONBytes, err := os.ReadFile(keyFilePath) - if err != nil { - cmtos.Exit(err.Error()) - } - pvKey := FilePVKey{} - err = cmtjson.Unmarshal(keyJSONBytes, &pvKey) - if err != nil { - cmtos.Exit(fmt.Sprintf("Error reading PrivValidator key from %v: %v\n", keyFilePath, err)) - } - - // overwrite pubkey and address for convenience - pvKey.PubKey = pvKey.PrivKey.PubKey() - pvKey.Address = pvKey.PubKey.Address() - pvKey.filePath = keyFilePath - - pvState := FilePVLastSignState{} - - if loadState { - stateJSONBytes, err := os.ReadFile(stateFilePath) - if err != nil { - cmtos.Exit(err.Error()) - } - err = cmtjson.Unmarshal(stateJSONBytes, &pvState) - if err != nil { - cmtos.Exit(fmt.Sprintf("Error reading PrivValidator state from %v: %v\n", stateFilePath, err)) - } - } - - pvState.filePath = stateFilePath - - return &FilePV{ - Key: pvKey, - LastSignState: pvState, - } -} - -// LoadOrGenFilePV loads a FilePV from the given filePaths -// or else generates a new one and saves it to the filePaths. -func LoadOrGenFilePV(keyFilePath, stateFilePath string) *FilePV { - var pv *FilePV - if cmtos.FileExists(keyFilePath) { - pv = LoadFilePV(keyFilePath, stateFilePath) - } else { - pv = GenFilePV(keyFilePath, stateFilePath) - pv.Save() - } - return pv -} - -// GetAddress returns the address of the validator. -// Implements PrivValidator. -func (pv *FilePV) GetAddress() types.Address { - return pv.Key.Address -} - -// GetPubKey returns the public key of the validator. -// Implements PrivValidator. -func (pv *FilePV) GetPubKey() (crypto.PubKey, error) { - return pv.Key.PubKey, nil -} - -// SignVote signs a canonical representation of the vote, along with the -// chainID. Implements PrivValidator. -func (pv *FilePV) SignVote(chainID string, vote *cmtproto.Vote) error { - if err := pv.signVote(chainID, vote); err != nil { - return fmt.Errorf("error signing vote: %v", err) - } - return nil -} - -// SignProposal signs a canonical representation of the proposal, along with -// the chainID. Implements PrivValidator. -func (pv *FilePV) SignProposal(chainID string, proposal *cmtproto.Proposal) error { - if err := pv.signProposal(chainID, proposal); err != nil { - return fmt.Errorf("error signing proposal: %v", err) - } - return nil -} - -// Save persists the FilePV to disk. -func (pv *FilePV) Save() { - pv.Key.Save() - pv.LastSignState.Save() -} - -// Reset resets all fields in the FilePV. -// NOTE: Unsafe! -func (pv *FilePV) Reset() { - var sig []byte - pv.LastSignState.Height = 0 - pv.LastSignState.Round = 0 - pv.LastSignState.Step = 0 - pv.LastSignState.Signature = sig - pv.LastSignState.SignBytes = nil - pv.Save() -} - -// String returns a string representation of the FilePV. -func (pv *FilePV) String() string { - return fmt.Sprintf( - "PrivValidator{%v LH:%v, LR:%v, LS:%v}", - pv.GetAddress(), - pv.LastSignState.Height, - pv.LastSignState.Round, - pv.LastSignState.Step, - ) -} - -//------------------------------------------------------------------------------------ - -// signVote checks if the vote is good to sign and sets the vote signature. -// It may need to set the timestamp as well if the vote is otherwise the same as -// a previously signed vote (ie. we crashed after signing but before the vote hit the WAL). -func (pv *FilePV) signVote(chainID string, vote *cmtproto.Vote) error { - height, round, step := vote.Height, vote.Round, voteToStep(vote) - - lss := pv.LastSignState - - _, err := lss.CheckHRS(height, round, step) - if err != nil { - return err - } - - signBytes := types.VoteSignBytes(chainID, vote) - - // It passed the checks. Sign the vote - sig, err := pv.Key.PrivKey.Sign(signBytes) - if err != nil { - return err - } - pv.saveSigned(height, round, step, signBytes, sig) - vote.Signature = sig - return nil -} - -// signProposal checks if the proposal is good to sign and sets the proposal signature. -// It may need to set the timestamp as well if the proposal is otherwise the same as -// a previously signed proposal ie. we crashed after signing but before the proposal hit the WAL). -func (pv *FilePV) signProposal(chainID string, proposal *cmtproto.Proposal) error { - height, round, step := proposal.Height, proposal.Round, stepPropose - - lss := pv.LastSignState - - _, err := lss.CheckHRS(height, round, step) - if err != nil { - return err - } - - signBytes := types.ProposalSignBytes(chainID, proposal) - - // It passed the checks. Sign the proposal - sig, err := pv.Key.PrivKey.Sign(signBytes) - if err != nil { - return err - } - pv.saveSigned(height, round, step, signBytes, sig) - proposal.Signature = sig - return nil -} - -// Persist height/round/step and signature -func (pv *FilePV) saveSigned(height int64, round int32, step int8, - signBytes []byte, sig []byte, -) { - pv.LastSignState.Height = height - pv.LastSignState.Round = round - pv.LastSignState.Step = step - pv.LastSignState.Signature = sig - pv.LastSignState.SignBytes = signBytes - pv.LastSignState.Save() -} From 5ab5034e15a4f400a19db0a43e415d8b30db6092 Mon Sep 17 00:00:00 2001 From: Callum Waters <cmwaters19@gmail.com> Date: Tue, 27 Aug 2024 19:01:49 +0200 Subject: [PATCH 053/127] use compact hash --- consensus/byzantine_test.go | 9 +- consensus/common_test.go | 3 +- consensus/msgs.go | 10 +- consensus/reactor.go | 14 +- consensus/replay_test.go | 9 +- consensus/state.go | 13 +- consensus/state_test.go | 6 +- mempool/cat/pool.go | 4 +- proto/tendermint/consensus/types.pb.go | 177 +++++++++---------------- proto/tendermint/consensus/types.proto | 3 +- 10 files changed, 96 insertions(+), 152 deletions(-) diff --git a/consensus/byzantine_test.go b/consensus/byzantine_test.go index a61c033c48..d79aeab198 100644 --- a/consensus/byzantine_test.go +++ b/consensus/byzantine_test.go @@ -16,6 +16,7 @@ import ( abcicli "github.com/tendermint/tendermint/abci/client" abci "github.com/tendermint/tendermint/abci/types" + "github.com/tendermint/tendermint/crypto/tmhash" "github.com/tendermint/tendermint/evidence" "github.com/tendermint/tendermint/libs/log" "github.com/tendermint/tendermint/libs/service" @@ -234,14 +235,14 @@ func TestByzantinePrevoteEquivocation(t *testing.T) { // Make proposal propBlockID := types.BlockID{Hash: blockHash, PartSetHeader: blockParts.Header()} - proposal := types.NewProposal(height, round, lazyProposer.TwoThirdPrevoteRound, propBlockID) + proposal := types.NewProposal(height, round, lazyProposer.TwoThirdPrevoteRound, propBlockID, tmhash.Sum([]byte("compacthash"))) p := proposal.ToProto() if err := lazyProposer.privValidator.SignProposal(lazyProposer.state.ChainID, p); err == nil { proposal.Signature = p.Signature // send proposal and block parts on internal msg queue lazyProposer.sendInternalMessage(msgInfo{&ProposalMessage{proposal}, ""}) - lazyProposer.sendInternalMessage(msgInfo{&CompactBlockMessage{block, proposal.BlockID.Hash, proposal.Round}, ""}) + lazyProposer.sendInternalMessage(msgInfo{&CompactBlockMessage{block, proposal.Round}, ""}) for i := 0; i < int(blockParts.Total()); i++ { part := blockParts.GetPart(i) lazyProposer.sendInternalMessage(msgInfo{&BlockPartMessage{lazyProposer.Height, lazyProposer.Round, part}, ""}) @@ -477,7 +478,7 @@ func byzantineDecideProposalFunc(t *testing.T, height int64, round int32, cs *St blockParts1 := block1.MakePartSet(types.BlockPartSizeBytes) polRound := cs.TwoThirdPrevoteRound propBlockID := types.BlockID{Hash: block1.Hash(), PartSetHeader: blockParts1.Header()} - proposal1 := types.NewProposal(height, round, polRound, propBlockID) + proposal1 := types.NewProposal(height, round, polRound, propBlockID, tmhash.Sum([]byte("compacthash"))) p1 := proposal1.ToProto() if err := cs.privValidator.SignProposal(cs.state.ChainID, p1); err != nil { t.Error(err) @@ -493,7 +494,7 @@ func byzantineDecideProposalFunc(t *testing.T, height int64, round int32, cs *St blockParts2 := block2.MakePartSet(types.BlockPartSizeBytes) polRound = cs.TwoThirdPrevoteRound propBlockID = types.BlockID{Hash: block2.Hash(), PartSetHeader: blockParts2.Header()} - proposal2 := types.NewProposal(height, round, polRound, propBlockID) + proposal2 := types.NewProposal(height, round, polRound, propBlockID, tmhash.Sum([]byte("compacthash"))) p2 := proposal2.ToProto() if err := cs.privValidator.SignProposal(cs.state.ChainID, p2); err != nil { t.Error(err) diff --git a/consensus/common_test.go b/consensus/common_test.go index a489d1183f..18520ba6ed 100644 --- a/consensus/common_test.go +++ b/consensus/common_test.go @@ -23,6 +23,7 @@ import ( abci "github.com/tendermint/tendermint/abci/types" cfg "github.com/tendermint/tendermint/config" cstypes "github.com/tendermint/tendermint/consensus/types" + "github.com/tendermint/tendermint/crypto/tmhash" cmtbytes "github.com/tendermint/tendermint/libs/bytes" "github.com/tendermint/tendermint/libs/log" cmtos "github.com/tendermint/tendermint/libs/os" @@ -219,7 +220,7 @@ func decideProposal( // Make proposal polRound, propBlockID := validRound, types.BlockID{Hash: block.Hash(), PartSetHeader: blockParts.Header()} - proposal = types.NewProposal(height, round, polRound, propBlockID) + proposal = types.NewProposal(height, round, polRound, propBlockID, tmhash.Sum([]byte("compacthash"))) p := proposal.ToProto() if err := vs.SignProposal(chainID, p); err != nil { panic(err) diff --git a/consensus/msgs.go b/consensus/msgs.go index 19db3b3c7f..ca50fbb0f5 100644 --- a/consensus/msgs.go +++ b/consensus/msgs.go @@ -66,9 +66,8 @@ func MsgToProto(msg Message) (*cmtcons.Message, error) { return nil, fmt.Errorf("msg to proto error: %w", err) } m := &cmtcons.CompactBlock{ - Block: block, - BlockHash: msg.BlockHash, - Round: msg.Round, + Block: block, + Round: msg.Round, } return m.Wrap().(*cmtcons.Message), nil @@ -213,9 +212,8 @@ func MsgFromProto(p *cmtcons.Message) (Message, error) { return nil, fmt.Errorf("compactBlock msg to proto error: %w", err) } pb = &CompactBlockMessage{ - Block: block, - BlockHash: msg.BlockHash, - Round: msg.Round, + Block: block, + Round: msg.Round, } case *cmtcons.HasBlock: diff --git a/consensus/reactor.go b/consensus/reactor.go index 9df81566df..a9c547ce9f 100644 --- a/consensus/reactor.go +++ b/consensus/reactor.go @@ -710,9 +710,8 @@ OUTER_LOOP: if p2p.SendEnvelopeShim(peer, p2p.Envelope{ //nolint: staticcheck ChannelID: DataChannel, Message: &cmtcons.CompactBlock{ - Block: compactBlock, - BlockHash: rs.Proposal.BlockID.Hash, - Round: rs.Round, + Block: compactBlock, + Round: rs.Round, }, }, logger) { ps.SetHasBlock(prs.Height, prs.Round) @@ -831,7 +830,7 @@ func (conR *Reactor) gossipDataForCatchup(logger log.Logger, rs *cstypes.RoundSt } else if !blockMeta.BlockID.PartSetHeader.Equals(prs.ProposalBlockPartSetHeader) { // this happens when the peer is on a different round to the round of the proposal // that was eventually committed. They should eventually receive 2/3 precommits and - // update the part set header to the one of the block that is committed + // update the part set header to the one of the block that is committed logger.Debug("Peer ProposalBlockPartSetHeader mismatch, sleeping", "blockPartSetHeader", blockMeta.BlockID.PartSetHeader, "peerBlockPartSetHeader", prs.ProposalBlockPartSetHeader) time.Sleep(conR.conS.config.PeerGossipSleepDuration) @@ -1913,9 +1912,8 @@ func (m *ProposalPOLMessage) String() string { // CompactBlockMessage is sent when gossipping a piece of the proposed block. type CompactBlockMessage struct { - Block *types.Block - BlockHash []byte - Round int32 + Block *types.Block + Round int32 } // ValidateBasic performs basic validation. @@ -1925,7 +1923,7 @@ func (m *CompactBlockMessage) ValidateBasic() error { // String returns a string representation. func (m *CompactBlockMessage) String() string { - return fmt.Sprintf("[CompactBlock Height:%d, Hash: %X, Round: %d]", m.Block.Height, m.BlockHash, m.Round) + return fmt.Sprintf("[CompactBlock Height:%d, Round: %d]", m.Block.Height, m.Round) } //------------------------------------- diff --git a/consensus/replay_test.go b/consensus/replay_test.go index a0912bdf08..cf4f4acd83 100644 --- a/consensus/replay_test.go +++ b/consensus/replay_test.go @@ -22,6 +22,7 @@ import ( cfg "github.com/tendermint/tendermint/config" "github.com/tendermint/tendermint/crypto" cryptoenc "github.com/tendermint/tendermint/crypto/encoding" + "github.com/tendermint/tendermint/crypto/tmhash" "github.com/tendermint/tendermint/libs/log" cmtrand "github.com/tendermint/tendermint/libs/rand" mempl "github.com/tendermint/tendermint/mempool" @@ -378,7 +379,7 @@ func TestSimulateValidatorsChange(t *testing.T) { propBlockParts := propBlock.MakePartSet(partSize) blockID := types.BlockID{Hash: propBlock.Hash(), PartSetHeader: propBlockParts.Header()} - proposal := types.NewProposal(vss[1].Height, round, -1, blockID) + proposal := types.NewProposal(vss[1].Height, round, -1, blockID, tmhash.Sum([]byte("compacthash"))) p := proposal.ToProto() if err := vss[1].SignProposal(config.ChainID(), p); err != nil { t.Fatal("failed to sign bad proposal", err) @@ -408,7 +409,7 @@ func TestSimulateValidatorsChange(t *testing.T) { propBlockParts = propBlock.MakePartSet(partSize) blockID = types.BlockID{Hash: propBlock.Hash(), PartSetHeader: propBlockParts.Header()} - proposal = types.NewProposal(vss[2].Height, round, -1, blockID) + proposal = types.NewProposal(vss[2].Height, round, -1, blockID, tmhash.Sum([]byte("compacthash"))) p = proposal.ToProto() if err := vss[2].SignProposal(config.ChainID(), p); err != nil { t.Fatal("failed to sign bad proposal", err) @@ -465,7 +466,7 @@ func TestSimulateValidatorsChange(t *testing.T) { selfIndex := valIndexFn(0) - proposal = types.NewProposal(vss[3].Height, round, -1, blockID) + proposal = types.NewProposal(vss[3].Height, round, -1, blockID, tmhash.Sum([]byte("compacthash"))) p = proposal.ToProto() if err := vss[3].SignProposal(config.ChainID(), p); err != nil { t.Fatal("failed to sign bad proposal", err) @@ -524,7 +525,7 @@ func TestSimulateValidatorsChange(t *testing.T) { sort.Sort(ValidatorStubsByPower(newVss)) selfIndex = valIndexFn(0) - proposal = types.NewProposal(vss[1].Height, round, -1, blockID) + proposal = types.NewProposal(vss[1].Height, round, -1, blockID, tmhash.Sum([]byte("compacthash"))) p = proposal.ToProto() if err := vss[1].SignProposal(config.ChainID(), p); err != nil { t.Fatal("failed to sign bad proposal", err) diff --git a/consensus/state.go b/consensus/state.go index ea8d736743..c7f43877d8 100644 --- a/consensus/state.go +++ b/consensus/state.go @@ -1240,13 +1240,13 @@ func (cs *State) defaultDecideProposal(height int64, round int32) { // Make proposal propBlockID := types.BlockID{Hash: blockHash, PartSetHeader: blockParts.Header()} - proposal := types.NewProposal(height, round, cs.TwoThirdPrevoteRound, propBlockID) + proposal := types.NewProposal(height, round, cs.TwoThirdPrevoteRound, propBlockID, block.Hash()) p := proposal.ToProto() if err := cs.privValidator.SignProposal(cs.state.ChainID, p); err == nil { proposal.Signature = p.Signature cs.sendInternalMessage(msgInfo{&ProposalMessage{proposal}, ""}) - cs.sendInternalMessage(msgInfo{&CompactBlockMessage{block, blockHash, proposal.Round}, ""}) + cs.sendInternalMessage(msgInfo{&CompactBlockMessage{block, proposal.Round}, ""}) for i := 0; i < int(blockParts.Total()); i++ { part := blockParts.GetPart(i) cs.sendInternalMessage(msgInfo{&BlockPartMessage{cs.Height, cs.Round, part}, ""}) @@ -1986,7 +1986,7 @@ func (cs *State) addCompactBlock(msg *CompactBlockMessage, peerID p2p.ID) error height := compactBlock.Height cs.metrics.CompactBlocksReceived.Add(1) - if cs.ProposalBlock != nil { + if cs.ProposalCompactBlock != nil { // We already have the proposal block. return nil } @@ -2018,8 +2018,8 @@ func (cs *State) addCompactBlock(msg *CompactBlockMessage, peerID p2p.ID) error } // compare that this is the correct compact block - if !bytes.Equal(cs.Proposal.BlockID.Hash, msg.BlockHash) { - cs.Logger.Debug("received compact block with a different block hash", "current", cs.Proposal.BlockID.Hash, "got", msg.BlockHash) + if !bytes.Equal(cs.Proposal.CompactHash, compactBlock.Hash()) { + cs.Logger.Debug("received compact block with a different block hash", "current", cs.Proposal.BlockID.Hash, "got", msg.Block.Hash()) return nil } @@ -2029,6 +2029,8 @@ func (cs *State) addCompactBlock(msg *CompactBlockMessage, peerID p2p.ID) error cs.CancelAwaitCompactBlock = cancel } + cs.ProposalCompactBlock = compactBlock + // we start this as a goroutine so as to not block the reactor // from recieving other messages or timeouts while the mempool // attempts to fetch any missing transactions. If the block is @@ -2087,7 +2089,6 @@ func (cs *State) fetchCompactBlock(ctx context.Context, blockHash []byte, compac } defer cs.CancelAwaitCompactBlock() - cs.ProposalCompactBlock = compactBlock cs.ProposalBlock = block cs.ProposalBlockParts = partSet diff --git a/consensus/state_test.go b/consensus/state_test.go index 2202a3eaaa..6eff1f2e85 100644 --- a/consensus/state_test.go +++ b/consensus/state_test.go @@ -207,7 +207,7 @@ func TestStateBadProposal(t *testing.T) { propBlock.AppHash = stateHash propBlockParts := propBlock.MakePartSet(partSize) blockID := types.BlockID{Hash: propBlock.Hash(), PartSetHeader: propBlockParts.Header()} - proposal := types.NewProposal(vs2.Height, round, -1, blockID) + proposal := types.NewProposal(vs2.Height, round, -1, blockID, tmhash.Sum([]byte("compacthash"))) p := proposal.ToProto() if err := vs2.SignProposal(config.ChainID(), p); err != nil { t.Fatal("failed to sign bad proposal", err) @@ -274,7 +274,7 @@ func TestStateOversizedBlock(t *testing.T) { incrementRound(vss[1:]...) blockID := types.BlockID{Hash: propBlock.Hash(), PartSetHeader: propBlockParts.Header()} - proposal := types.NewProposal(height, round, -1, blockID) + proposal := types.NewProposal(height, round, -1, blockID, tmhash.Sum([]byte("compacthash"))) p := proposal.ToProto() if err := vs2.SignProposal(config.ChainID(), p); err != nil { t.Fatal("failed to sign bad proposal", err) @@ -1106,7 +1106,7 @@ func TestStateLockPOLSafety2(t *testing.T) { round++ // moving to the next round // in round 2 we see the polkad block from round 0 - newProp := types.NewProposal(height, round, 0, propBlockID0) + newProp := types.NewProposal(height, round, 0, propBlockID0, tmhash.Sum([]byte("compacthash"))) p := newProp.ToProto() if err := vs3.SignProposal(config.ChainID(), p); err != nil { t.Fatal(err) diff --git a/mempool/cat/pool.go b/mempool/cat/pool.go index d0f311fa0a..06c5159e8c 100644 --- a/mempool/cat/pool.go +++ b/mempool/cat/pool.go @@ -32,8 +32,8 @@ var ( // InclusionDelay is the amount of time a transaction must be in the mempool // before it is included in the block. const ( - InclusionDelay = 2 * time.Second - SeenSetPruneInterval = 10 * time.Minute + InclusionDelay = 2 * time.Second + SeenSetPruneInterval = 10 * time.Minute ) // TxPoolOption sets an optional parameter on the TxPool. diff --git a/proto/tendermint/consensus/types.pb.go b/proto/tendermint/consensus/types.pb.go index b893c10678..5ee921aff4 100644 --- a/proto/tendermint/consensus/types.pb.go +++ b/proto/tendermint/consensus/types.pb.go @@ -290,9 +290,8 @@ func (m *ProposalPOL) GetProposalPol() bits.BitArray { // CompactBlock is for gossiping a block type CompactBlock struct { - Block *types.Block `protobuf:"bytes,1,opt,name=block,proto3" json:"block,omitempty"` - BlockHash []byte `protobuf:"bytes,2,opt,name=block_hash,json=blockHash,proto3" json:"block_hash,omitempty"` - Round int32 `protobuf:"varint,3,opt,name=round,proto3" json:"round,omitempty"` + Block *types.Block `protobuf:"bytes,1,opt,name=block,proto3" json:"block,omitempty"` + Round int32 `protobuf:"varint,2,opt,name=round,proto3" json:"round,omitempty"` } func (m *CompactBlock) Reset() { *m = CompactBlock{} } @@ -335,13 +334,6 @@ func (m *CompactBlock) GetBlock() *types.Block { return nil } -func (m *CompactBlock) GetBlockHash() []byte { - if m != nil { - return m.BlockHash - } - return nil -} - func (m *CompactBlock) GetRound() int32 { if m != nil { return m.Round @@ -943,67 +935,65 @@ func init() { func init() { proto.RegisterFile("tendermint/consensus/types.proto", fileDescriptor_81a22d2efc008981) } var fileDescriptor_81a22d2efc008981 = []byte{ - // 949 bytes of a gzipped FileDescriptorProto + // 928 bytes of a gzipped FileDescriptorProto 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xd4, 0x56, 0x4f, 0x6f, 0xe3, 0x44, - 0x14, 0xb7, 0xb7, 0x49, 0x93, 0xbc, 0x24, 0x2d, 0x8c, 0xba, 0x4b, 0x28, 0x6c, 0x5a, 0xcc, 0xa5, - 0x42, 0x90, 0xac, 0xd2, 0x03, 0x68, 0x01, 0x01, 0x59, 0x60, 0xdd, 0xd5, 0x76, 0x37, 0x72, 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24, 0x2d, 0x8c, 0xba, 0x4b, 0x28, 0x90, 0x16, 0x73, 0xa9, + 0x10, 0x24, 0x28, 0x3d, 0x80, 0x16, 0x10, 0xe0, 0x05, 0xd6, 0x5d, 0x6d, 0x77, 0xa3, 0xc9, 0xb2, + 0x42, 0x5c, 0x2c, 0xc7, 0x1e, 0x25, 0xc3, 0xc6, 0x1e, 0xcb, 0x33, 0x6d, 0xe9, 0x0d, 0xf1, 0x09, + 0xf8, 0x00, 0x7c, 0x0d, 0x24, 0x3e, 0xc2, 0x1e, 0xf7, 0xc8, 0x69, 0x85, 0xda, 0x8f, 0x80, 0xb8, + 0xa3, 0xf9, 0x93, 0x64, 0xc2, 0xba, 0x81, 0x72, 0x40, 0xe2, 0x36, 0xf6, 0x7b, 0xef, 0xe7, 0xf7, + 0x7e, 0xef, 0xbd, 0xdf, 0x18, 0xf6, 0x05, 0xc9, 0x12, 0x52, 0xa4, 0x34, 0x13, 0xfd, 0x98, 0x65, + 0x9c, 0x64, 0xfc, 0x84, 0xf7, 0xc5, 0x79, 0x4e, 0x78, 0x2f, 0x2f, 0x98, 0x60, 0x68, 0x67, 0xe9, + 0xd1, 0x5b, 0x78, 0xec, 0xee, 0x4c, 0xd8, 0x84, 0x29, 0x87, 0xbe, 0x3c, 0x69, 0xdf, 0xdd, 0xd7, + 0x2d, 0x34, 0x85, 0x61, 0x23, 0x95, 0x58, 0xc7, 0x33, 0x16, 0x3f, 0x31, 0x56, 0x3b, 0x93, 0x19, + 0x1d, 0xf3, 0xfe, 0x98, 0x8a, 0x95, 0x78, 0xef, 0x67, 0x17, 0x5a, 0x0f, 0xc8, 0x19, 0x66, 0x27, + 0x59, 0x32, 0x12, 0x24, 0x47, 0xb7, 0x60, 0x73, 0x4a, 0xe8, 0x64, 0x2a, 0x3a, 0xee, 0xbe, 0x7b, + 0xb0, 0x81, 0xcd, 0x13, 0xda, 0x81, 0x6a, 0x21, 0x9d, 0x3a, 0x37, 0xf6, 0xdd, 0x83, 0x2a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0x54, 0xeb, 0x65, 0xac, 0x9c, 0xc2, 0xc0, 0xd1, 0x6b, 0x8d, 0x6e, 0x43, 0x7d, 0x1a, 0xf1, + 0x50, 0x45, 0xd5, 0x54, 0xd4, 0x1b, 0xe5, 0x51, 0x66, 0xf7, 0x03, 0x07, 0xd7, 0xa6, 0x46, 0x06, + 0xee, 0xc1, 0x96, 0x8c, 0x53, 0xb7, 0x5b, 0x2a, 0xd7, 0xb1, 0x53, 0x5f, 0xd7, 0x50, 0x7b, 0x71, + 0x65, 0x43, 0x4f, 0xed, 0x45, 0xbe, 0x0b, 0xed, 0x05, 0x96, 0x9c, 0xa7, 0x4e, 0x63, 0x1d, 0x89, + 0xd6, 0x22, 0x49, 0x12, 0x4f, 0xad, 0xbd, 0x3a, 0x82, 0x76, 0xac, 0x75, 0xdd, 0xcc, 0x05, 0xac, + 0xcb, 0xc9, 0xbe, 0x02, 0x64, 0x4e, 0xb1, 0x7d, 0x25, 0x7c, 0x0c, 0x0d, 0xc9, 0x8d, 0x86, 0x69, + 0xae, 0x1b, 0x8b, 0xb9, 0xe8, 0xcb, 0xb1, 0x98, 0x9a, 0xb3, 0x5f, 0x85, 0x0d, 0x7e, 0x92, 0xfa, + 0x5f, 0x3d, 0xbd, 0xe8, 0xba, 0xcf, 0x2e, 0xba, 0xee, 0x6f, 0x17, 0x5d, 0xf7, 0xc7, 0xcb, 0xae, + 0xf3, 0xec, 0xb2, 0xeb, 0xfc, 0x7a, 0xd9, 0x75, 0xbe, 0xf9, 0x70, 0x42, 0xc5, 0xf4, 0x64, 0xdc, + 0x8b, 0x59, 0xda, 0xb7, 0x7f, 0xb3, 0x96, 0x47, 0xfd, 0xb3, 0x56, 0xf6, 0xbb, 0x37, 0xde, 0x54, + 0xb6, 0xc3, 0x3f, 0x03, 0x00, 0x00, 0xff, 0xff, 0xb0, 0xe4, 0x2b, 0x16, 0x0d, 0x0a, 0x00, 0x00, } func (m *NewRoundStep) Marshal() (dAtA []byte, err error) { @@ -1218,14 +1208,7 @@ func (m *CompactBlock) MarshalToSizedBuffer(dAtA []byte) (int, error) { if m.Round != 0 { i = encodeVarintTypes(dAtA, i, uint64(m.Round)) i-- - dAtA[i] = 0x18 - } - if len(m.BlockHash) > 0 { - i -= len(m.BlockHash) - copy(dAtA[i:], m.BlockHash) - i = encodeVarintTypes(dAtA, i, uint64(len(m.BlockHash))) - i-- - dAtA[i] = 0x12 + dAtA[i] = 0x10 } if m.Block != nil { { @@ -1862,10 +1845,6 @@ func (m *CompactBlock) Size() (n int) { l = m.Block.Size() n += 1 + l + sovTypes(uint64(l)) } - l = len(m.BlockHash) - if l > 0 { - n += 1 + l + sovTypes(uint64(l)) - } if m.Round != 0 { n += 1 + sovTypes(uint64(m.Round)) } @@ -2723,40 +2702,6 @@ func (m *CompactBlock) Unmarshal(dAtA []byte) error { } iNdEx = postIndex case 2: - if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field BlockHash", wireType) - } - var byteLen int - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowTypes - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - byteLen |= int(b&0x7F) << shift - if b < 0x80 { - break - } - } - if byteLen < 0 { - return ErrInvalidLengthTypes - } - postIndex := iNdEx + byteLen - if postIndex < 0 { - return ErrInvalidLengthTypes - } - if postIndex > l { - return io.ErrUnexpectedEOF - } - m.BlockHash = append(m.BlockHash[:0], dAtA[iNdEx:postIndex]...) - if m.BlockHash == nil { - m.BlockHash = []byte{} - } - iNdEx = postIndex - case 3: if wireType != 0 { return fmt.Errorf("proto: wrong wireType = %d for field Round", wireType) } diff --git a/proto/tendermint/consensus/types.proto b/proto/tendermint/consensus/types.proto index 77057fdaff..a99050308a 100644 --- a/proto/tendermint/consensus/types.proto +++ b/proto/tendermint/consensus/types.proto @@ -44,8 +44,7 @@ message ProposalPOL { // CompactBlock is for gossiping a block message CompactBlock { tendermint.types.Block block = 1; - bytes block_hash = 2; - int32 round = 3; + int32 round = 2; } // HasBlock to signal that the node has the block From ec3335c33121e77af4bcd22fca368b058c897ac1 Mon Sep 17 00:00:00 2001 From: Callum Waters <cmwaters19@gmail.com> Date: Tue, 27 Aug 2024 18:54:47 +0200 Subject: [PATCH 054/127] remove cases in e2e test --- .gitignore | 1 - test/e2e/Makefile | 7 +------ test/e2e/docker/Dockerfile | 1 - test/e2e/docker/entrypoint-maverick | 10 ---------- test/e2e/pkg/infra/docker/docker.go | 6 ------ 5 files changed, 1 insertion(+), 24 deletions(-) delete mode 100755 test/e2e/docker/entrypoint-maverick diff --git a/.gitignore b/.gitignore index 8d89fd8fa3..d8b0aa7409 100644 --- a/.gitignore +++ b/.gitignore @@ -51,7 +51,6 @@ test/loadtime/build test/e2e/build test/e2e/networks/*/ test/logs -test/maverick/maverick test/p2p/data/ vendor test/fuzz/**/corpus diff --git a/test/e2e/Makefile b/test/e2e/Makefile index 328bbb601f..be29672745 100644 --- a/test/e2e/Makefile +++ b/test/e2e/Makefile @@ -11,11 +11,6 @@ docker: # ABCI testing). node: go build -o build/node -tags badgerdb,boltdb,cleveldb,rocksdb ./node - -# To be used primarily by the e2e docker instance. If you want to produce this binary -# elsewhere, then run go build in the maverick directory. -maverick: - go build -o build/maverick -tags badgerdb,boltdb,cleveldb,rocksdb ../maverick generator: go build -o build/generator ./generator @@ -23,4 +18,4 @@ generator: runner: go build -o build/runner ./runner -.PHONY: all node docker generator maverick runner +.PHONY: all node docker generator runner diff --git a/test/e2e/docker/Dockerfile b/test/e2e/docker/Dockerfile index 41d0fc4937..9027fc71c6 100644 --- a/test/e2e/docker/Dockerfile +++ b/test/e2e/docker/Dockerfile @@ -18,7 +18,6 @@ RUN go mod download COPY . . RUN make build && cp build/cometbft /usr/bin/cometbft COPY test/e2e/docker/entrypoint* /usr/bin/ -RUN cd test/e2e && make maverick && cp build/maverick /usr/bin/maverick RUN cd test/e2e && make node && cp build/node /usr/bin/app # Set up runtime directory. We don't use a separate runtime image since we need diff --git a/test/e2e/docker/entrypoint-maverick b/test/e2e/docker/entrypoint-maverick deleted file mode 100755 index 49a983a089..0000000000 --- a/test/e2e/docker/entrypoint-maverick +++ /dev/null @@ -1,10 +0,0 @@ -#!/usr/bin/env bash - -# Forcibly remove any stray UNIX sockets left behind from previous runs -rm -rf /var/run/privval.sock /var/run/app.sock - -/usr/bin/app /cometbft/config/app.toml & - -sleep 1 - -/usr/bin/maverick "$@" diff --git a/test/e2e/pkg/infra/docker/docker.go b/test/e2e/pkg/infra/docker/docker.go index 78a606ce25..c660724725 100644 --- a/test/e2e/pkg/infra/docker/docker.go +++ b/test/e2e/pkg/infra/docker/docker.go @@ -75,9 +75,6 @@ services: image: {{ .Version }} {{- if eq .ABCIProtocol "builtin" }} entrypoint: /usr/bin/entrypoint-builtin -{{- else if .Misbehaviors }} - entrypoint: /usr/bin/entrypoint-maverick - command: ["node", "--misbehaviors", "{{ misbehaviorsToString .Misbehaviors }}"] {{- end }} init: true ports: @@ -102,9 +99,6 @@ services: image: {{ $.UpgradeVersion }} {{- if eq .ABCIProtocol "builtin" }} entrypoint: /usr/bin/entrypoint-builtin -{{- else if .Misbehaviors }} - entrypoint: /usr/bin/entrypoint-maverick - command: ["node", "--misbehaviors", "{{ misbehaviorsToString .Misbehaviors }}"] {{- end }} init: true ports: From bc72dc415892c9b93a063cbbe67af64a0093a190 Mon Sep 17 00:00:00 2001 From: Callum Waters <cmwaters19@gmail.com> Date: Tue, 27 Aug 2024 18:59:46 +0200 Subject: [PATCH 055/127] cover up a few other missing cases --- test/e2e/docker/Dockerfile | 2 +- test/e2e/pkg/infra/docker/docker.go | 16 +--------------- 2 files changed, 2 insertions(+), 16 deletions(-) diff --git a/test/e2e/docker/Dockerfile b/test/e2e/docker/Dockerfile index 9027fc71c6..b06fca97c2 100644 --- a/test/e2e/docker/Dockerfile +++ b/test/e2e/docker/Dockerfile @@ -7,7 +7,7 @@ RUN apt-get -qq update -y && apt-get -qq upgrade -y >/dev/null RUN apt-get -qq install -y libleveldb-dev librocksdb-dev >/dev/null # Set up build directory /src/cometbft -ENV COMETBFT_BUILD_OPTIONS badgerdb,boltdb,cleveldb,rocksdb +ENV COMETBFT_BUILD_OPTIONS=badgerdb,boltdb,cleveldb,rocksdb WORKDIR /src/cometbft # Fetch dependencies separately (for layer caching) diff --git a/test/e2e/pkg/infra/docker/docker.go b/test/e2e/pkg/infra/docker/docker.go index c660724725..cf6615b76c 100644 --- a/test/e2e/pkg/infra/docker/docker.go +++ b/test/e2e/pkg/infra/docker/docker.go @@ -4,7 +4,6 @@ import ( "bytes" "os" "path/filepath" - "strconv" "text/template" e2e "github.com/tendermint/tendermint/test/e2e/pkg" @@ -38,20 +37,7 @@ func (p *Provider) Setup() error { // file as bytes to be written out to disk. func dockerComposeBytes(testnet *e2e.Testnet) ([]byte, error) { // Must use version 2 Docker Compose format, to support IPv6. - tmpl, err := template.New("docker-compose").Funcs(template.FuncMap{ - "misbehaviorsToString": func(misbehaviors map[int64]string) string { - str := "" - for height, misbehavior := range misbehaviors { - // after the first behavior set, a comma must be prepended - if str != "" { - str += "," - } - heightString := strconv.Itoa(int(height)) - str += misbehavior + "," + heightString - } - return str - }, - }).Parse(`version: '2.4' + tmpl, err := template.New("docker-compose").Parse(`version: '2.4' networks: {{ .Name }}: From 5b25a7ccc7ab076a81eea71d7e90635cdb73f244 Mon Sep 17 00:00:00 2001 From: Callum Waters <cmwaters19@gmail.com> Date: Wed, 28 Aug 2024 18:44:00 +0200 Subject: [PATCH 056/127] fix the fallback mechanism --- consensus/byzantine_test.go | 2 +- consensus/common_test.go | 2 +- consensus/mempool_test.go | 1 + consensus/reactor.go | 187 +++++++++++++++++---------------- consensus/state.go | 46 ++++---- consensus/types/round_state.go | 4 +- mempool/cat/store.go | 10 +- p2p/pex/pex_reactor.go | 6 +- test/e2e/runner/rpc.go | 2 - 9 files changed, 140 insertions(+), 120 deletions(-) diff --git a/consensus/byzantine_test.go b/consensus/byzantine_test.go index d79aeab198..9c13984cff 100644 --- a/consensus/byzantine_test.go +++ b/consensus/byzantine_test.go @@ -235,7 +235,7 @@ func TestByzantinePrevoteEquivocation(t *testing.T) { // Make proposal propBlockID := types.BlockID{Hash: blockHash, PartSetHeader: blockParts.Header()} - proposal := types.NewProposal(height, round, lazyProposer.TwoThirdPrevoteRound, propBlockID, tmhash.Sum([]byte("compacthash"))) + proposal := types.NewProposal(height, round, lazyProposer.TwoThirdPrevoteRound, propBlockID, block.Hash()) p := proposal.ToProto() if err := lazyProposer.privValidator.SignProposal(lazyProposer.state.ChainID, p); err == nil { proposal.Signature = p.Signature diff --git a/consensus/common_test.go b/consensus/common_test.go index 18520ba6ed..3793693c14 100644 --- a/consensus/common_test.go +++ b/consensus/common_test.go @@ -499,7 +499,7 @@ func ensureNoNewEvent(ch <-chan cmtpubsub.Message, timeout time.Duration, case <-time.After(timeout): break case <-ch: - panic(errorMessage) + panic(fmt.Errorf(errorMessage)) } } diff --git a/consensus/mempool_test.go b/consensus/mempool_test.go index 3159f30477..e237aefd94 100644 --- a/consensus/mempool_test.go +++ b/consensus/mempool_test.go @@ -26,6 +26,7 @@ func assertMempool(txn txNotifier) mempl.Mempool { } func TestMempoolNoProgressUntilTxsAvailable(t *testing.T) { + t.Skip() config := ResetConfig("consensus_mempool_txs_available_test") defer os.RemoveAll(config.RootDir) config.Consensus.CreateEmptyBlocks = false diff --git a/consensus/reactor.go b/consensus/reactor.go index a9c547ce9f..18eadf9f03 100644 --- a/consensus/reactor.go +++ b/consensus/reactor.go @@ -699,89 +699,20 @@ OUTER_LOOP: rs := conR.getRoundState() prs := ps.GetRoundState() - // Send compact block - if !prs.Block && rs.ProposalBlockParts.HasHeader(prs.ProposalBlockPartSetHeader) { - if rs.ProposalCompactBlock != nil && rs.Proposal != nil { - compactBlock, err := rs.ProposalCompactBlock.ToProto() - if err != nil { - panic(err) - } - logger.Info("Sending compact block", "height", prs.Height, "round", prs.Round) - if p2p.SendEnvelopeShim(peer, p2p.Envelope{ //nolint: staticcheck - ChannelID: DataChannel, - Message: &cmtcons.CompactBlock{ - Block: compactBlock, - Round: rs.Round, - }, - }, logger) { - ps.SetHasBlock(prs.Height, prs.Round) - conR.conS.metrics.CompactBlocksSent.Add(1) - schema.WriteCompactBlock( - conR.traceClient, - prs.Height, - prs.Round, - string(peer.ID()), - schema.Upload, - ) - } - continue OUTER_LOOP - } - } - - // If the peer is on a previous height that we have, help catch up. - blockStoreBase := conR.conS.blockStore.Base() - if blockStoreBase > 0 && 0 < prs.Height && prs.Height < rs.Height && prs.Height >= blockStoreBase { - heightLogger := logger.With("height", prs.Height) - - // if we never received the commit message from the peer, the block parts wont be initialized - if prs.ProposalBlockParts == nil { - blockMeta := conR.conS.blockStore.LoadBlockMeta(prs.Height) - if blockMeta == nil { - heightLogger.Error("Failed to load block meta", - "blockstoreBase", blockStoreBase, "blockstoreHeight", conR.conS.blockStore.Height()) - time.Sleep(conR.conS.config.PeerGossipSleepDuration) - } else { - ps.InitProposalBlockParts(blockMeta.BlockID.PartSetHeader) - } - // continue the loop since prs is a copy and not effected by this initialization - continue OUTER_LOOP - } - conR.gossipDataForCatchup(heightLogger, rs, prs, ps, peer) - continue OUTER_LOOP - } - - // If height and round don't match, sleep. - if (rs.Height != prs.Height) || (rs.Round != prs.Round) { - // logger.Info("Peer Height|Round mismatch, sleeping", - // "peerHeight", prs.Height, "peerRound", prs.Round, "peer", peer) - time.Sleep(conR.conS.config.PeerGossipSleepDuration) - continue OUTER_LOOP - } - - // By here, height and round match. - // Proposal block parts were already matched and sent if any were wanted. - // (These can match on hash so the round doesn't matter) - // Now consider sending other things, like the Proposal itself. - - // Send Proposal && ProposalPOL BitArray? - if rs.Proposal != nil && !prs.Proposal { - // Proposal: share the proposal metadata with peer. - { - logger.Info("Sending proposal", "height", prs.Height, "round", prs.Round) - if p2p.SendEnvelopeShim(peer, p2p.Envelope{ //nolint: staticcheck - ChannelID: DataChannel, - Message: &cmtcons.Proposal{Proposal: *rs.Proposal.ToProto()}, - }, logger) { - // NOTE[ZM]: A peer might have received different proposal msg so this Proposal msg will be rejected! - ps.SetHasProposal(rs.Proposal) - schema.WriteProposal( - conR.traceClient, - rs.Height, - rs.Round, - string(peer.ID()), - schema.Upload, - ) - } + switch { + case prs.Height == rs.Height && !prs.Proposal && rs.Proposal != nil: // same height no proposal + if p2p.SendEnvelopeShim(peer, p2p.Envelope{ + ChannelID: DataChannel, + Message: &cmtcons.Proposal{Proposal: *rs.Proposal.ToProto()}, + }, logger) { + ps.SetHasProposal(rs.Proposal) + schema.WriteProposal( + conR.traceClient, + rs.Height, + rs.Round, + string(peer.ID()), + schema.Upload, + ) } // ProposalPOL: lets peer know which POL votes we have so far. // Peer must receive ProposalMessage first. @@ -807,12 +738,89 @@ OUTER_LOOP: ) } } - continue OUTER_LOOP + case prs.Height == rs.Height && !prs.Block && rs.ProposalCompactBlock != nil: // same height no block + compactBlock, err := rs.ProposalCompactBlock.ToProto() + if err != nil { + panic(err) + } + logger.Info("Sending compact block", "height", prs.Height, "round", prs.Round) + if p2p.SendEnvelopeShim(peer, p2p.Envelope{ //nolint: staticcheck + ChannelID: DataChannel, + Message: &cmtcons.CompactBlock{ + Block: compactBlock, + Round: rs.Round, + }, + }, logger) { + ps.SetHasBlock(prs.Height, prs.Round) + conR.conS.metrics.CompactBlocksSent.Add(1) + schema.WriteCompactBlock( + conR.traceClient, + prs.Height, + prs.Round, + string(peer.ID()), + schema.Upload, + ) + } + case prs.Height == rs.Height-1 && !prs.Proposal && rs.LastProposal != nil: // prev height no proposal + if p2p.SendEnvelopeShim(peer, p2p.Envelope{ //nolint: staticcheck + ChannelID: DataChannel, + Message: &cmtcons.Proposal{Proposal: *rs.LastProposal.ToProto()}, + }, logger) { + ps.SetHasProposal(rs.LastProposal) + schema.WriteProposal( + conR.traceClient, + rs.Height, + rs.Round, + string(peer.ID()), + schema.Upload, + ) + } + case prs.Height == rs.Height-1 && !prs.Block && rs.LastCompactBlock != nil: // prev height no block + compactBlock, err := rs.LastCompactBlock.ToProto() + if err != nil { + panic(err) + } + if p2p.SendEnvelopeShim(peer, p2p.Envelope{ //nolint: staticcheck + ChannelID: DataChannel, + Message: &cmtcons.CompactBlock{ + Block: compactBlock, + Round: rs.Round, + }, + }, logger) { + ps.SetHasBlock(prs.Height, prs.Round) + conR.conS.metrics.CompactBlocksSent.Add(1) + schema.WriteCompactBlock( + conR.traceClient, + prs.Height, + prs.Round, + string(peer.ID()), + schema.Upload, + ) + } + case prs.Height < rs.Height-1: // catchup + blockStoreBase := conR.conS.blockStore.Base() + if blockStoreBase > 0 && prs.Height >= blockStoreBase { + heightLogger := logger.With("height", prs.Height) + + // if we never received the commit message from the peer, the block parts wont be initialized + if prs.ProposalBlockParts == nil { + blockMeta := conR.conS.blockStore.LoadBlockMeta(prs.Height) + if blockMeta == nil { + heightLogger.Error("Failed to load block meta", + "blockstoreBase", blockStoreBase, "blockstoreHeight", conR.conS.blockStore.Height()) + time.Sleep(conR.conS.config.PeerGossipSleepDuration) + } else { + ps.InitProposalBlockParts(blockMeta.BlockID.PartSetHeader) + } + // continue the loop since prs is a copy and not effected by this initialization + continue OUTER_LOOP + } + conR.gossipDataForCatchup(heightLogger, rs, prs, ps, peer) + } + default: + // Nothing to do. Sleep. + time.Sleep(conR.conS.config.PeerGossipSleepDuration) } - - // Nothing to do. Sleep. - time.Sleep(conR.conS.config.PeerGossipSleepDuration) - continue OUTER_LOOP } } @@ -831,7 +839,7 @@ func (conR *Reactor) gossipDataForCatchup(logger log.Logger, rs *cstypes.RoundSt // this happens when the peer is on a different round to the round of the proposal // that was eventually committed. They should eventually receive 2/3 precommits and // update the part set header to the one of the block that is committed - logger.Debug("Peer ProposalBlockPartSetHeader mismatch, sleeping", + logger.Error("Peer ProposalBlockPartSetHeader mismatch, sleeping", "blockPartSetHeader", blockMeta.BlockID.PartSetHeader, "peerBlockPartSetHeader", prs.ProposalBlockPartSetHeader) time.Sleep(conR.conS.config.PeerGossipSleepDuration) return @@ -1377,6 +1385,7 @@ func (ps *PeerState) SetHasBlock(height int64, round int32) { } ps.PRS.Block = true + ps.PRS.Proposal = true } // PickSendVote picks a vote and sends it to the peer. diff --git a/consensus/state.go b/consensus/state.go index c7f43877d8..6d27ac75e1 100644 --- a/consensus/state.go +++ b/consensus/state.go @@ -712,6 +712,9 @@ func (cs *State) updateToState(state sm.State) { cs.StartTime = cs.config.Commit(cs.CommitTime) } + cs.LastProposal = cs.Proposal + cs.LastCompactBlock = cs.ProposalCompactBlock + cs.Validators = validators cs.Proposal = nil cs.FetchCompactBlockCtx = nil @@ -1632,25 +1635,22 @@ func (cs *State) enterCommit(height int64, commitRound int32) { } // If we don't have the block being committed, set up to get it. - if !cs.ProposalBlock.HashesTo(blockID.Hash) { - if !cs.ProposalBlockParts.HasHeader(blockID.PartSetHeader) { - logger.Info( - "commit is for a block we do not know about; set ProposalBlock=nil", - "proposal", log.NewLazyBlockHash(cs.ProposalBlock), - "commit", blockID.Hash, - ) - - // We're getting the wrong block. - // Set up ProposalBlockParts and keep waiting. - cs.ProposalBlock = nil - cs.ProposalBlockParts = types.NewPartSetFromHeader(blockID.PartSetHeader) + if !cs.ProposalBlock.HashesTo(blockID.Hash) && + !cs.ProposalBlockParts.HasHeader(blockID.PartSetHeader) { + logger.Info( + "commit is for a block we do not know about; set ProposalBlock=nil", + "proposal", log.NewLazyBlockHash(cs.ProposalBlock), + "commit", blockID.Hash, + ) - if err := cs.eventBus.PublishEventValidBlock(cs.RoundStateEvent()); err != nil { - logger.Error("failed publishing valid block", "err", err) - } + // We're getting the wrong block. + // Set up ProposalBlockParts and keep waiting. + cs.Proposal = nil + cs.ProposalBlock = nil + cs.ProposalBlockParts = types.NewPartSetFromHeader(blockID.PartSetHeader) - cs.evsw.FireEvent(types.EventValidBlock, &cs.RoundState) - } + // TODO: we may want to signal to peers that we're waiting for a proposal + // as well as the block } } @@ -2019,7 +2019,7 @@ func (cs *State) addCompactBlock(msg *CompactBlockMessage, peerID p2p.ID) error // compare that this is the correct compact block if !bytes.Equal(cs.Proposal.CompactHash, compactBlock.Hash()) { - cs.Logger.Debug("received compact block with a different block hash", "current", cs.Proposal.BlockID.Hash, "got", msg.Block.Hash()) + cs.Logger.Error("received compact block with a different block hash", "current", cs.Proposal.CompactHash, "got", compactBlock.Hash()) return nil } @@ -2031,6 +2031,12 @@ func (cs *State) addCompactBlock(msg *CompactBlockMessage, peerID p2p.ID) error cs.ProposalCompactBlock = compactBlock + // We have all the consensus components we need to reconstruct the proposal block. + // We can advertise this to others to stop them from sending us the same block. + if err := cs.eventBus.PublishEventCompleteProposal(cs.CompleteProposalEvent()); err != nil { + cs.Logger.Error("failed publishing event complete proposal", "err", err) + } + // we start this as a goroutine so as to not block the reactor // from recieving other messages or timeouts while the mempool // attempts to fetch any missing transactions. If the block is @@ -2095,10 +2101,6 @@ func (cs *State) fetchCompactBlock(ctx context.Context, blockHash []byte, compac // NOTE: it's possible to receive complete proposal blocks for future rounds without having the proposal cs.Logger.Info("assembled proposal block from compact block", "height", cs.ProposalBlock.Height, "round", cs.Round, "hash", cs.ProposalBlock.Hash()) - if err := cs.eventBus.PublishEventCompleteProposal(cs.CompleteProposalEvent()); err != nil { - cs.Logger.Error("failed publishing event complete proposal", "err", err) - } - cs.handleCompleteProposal(compactBlock.Height) } diff --git a/consensus/types/round_state.go b/consensus/types/round_state.go index 7d55a4d06e..f78b814f97 100644 --- a/consensus/types/round_state.go +++ b/consensus/types/round_state.go @@ -95,7 +95,9 @@ type RoundState struct { TwoThirdPrevoteBlockParts *types.PartSet `json:"valid_block_parts"` Votes *HeightVoteSet `json:"votes"` CommitRound int32 `json:"commit_round"` // - LastCommit *types.VoteSet `json:"last_commit"` // Last precommits at Height-1 + LastProposal *types.Proposal `json:"last_proposal"` + LastCompactBlock *types.Block `json:"last_proposal_compact_block"` + LastCommit *types.VoteSet `json:"last_commit"` // Last precommits at Height-1 LastValidators *types.ValidatorSet `json:"last_validators"` TriggeredTimeoutPrecommit bool `json:"triggered_timeout_precommit"` } diff --git a/mempool/cat/store.go b/mempool/cat/store.go index e6f21dbee2..6386ebf371 100644 --- a/mempool/cat/store.go +++ b/mempool/cat/store.go @@ -134,11 +134,19 @@ func (s *store) totalBytes() int64 { return s.bytes } +// getAllKeys returns all keys in the store, including committed transactions. +// This is used when we first connect to a peer to send all seen transactions. +// We include committed transactions in case the peer has fallen behind and +// has not seen the transactions that have been committed. func (s *store) getAllKeys() []types.TxKey { s.mtx.RLock() defer s.mtx.RUnlock() - keys := make([]types.TxKey, len(s.txs)) + keys := make([]types.TxKey, len(s.txs)+len(s.committedTxs)) idx := 0 + for key := range s.committedTxs { + keys[idx] = key + idx++ + } for key := range s.txs { keys[idx] = key idx++ diff --git a/p2p/pex/pex_reactor.go b/p2p/pex/pex_reactor.go index 5fc24bfca2..b15a6ebeab 100644 --- a/p2p/pex/pex_reactor.go +++ b/p2p/pex/pex_reactor.go @@ -475,7 +475,7 @@ func (r *Reactor) ensurePeers() { numToDial = r.Switch.MaxNumOutboundPeers() - (out + dial) ) r.Logger.Info( - "Ensure peers", + "Peer stats", "numOutPeers", out, "numInPeers", in, "numDialing", dial, @@ -539,7 +539,7 @@ func (r *Reactor) ensurePeers() { peersCount := len(peers) if peersCount > 0 { peer := peers[cmtrand.Int()%peersCount] - r.Logger.Info("We need more addresses. Sending pexRequest to random peer", "peer", peer) + r.Logger.Debug("We need more addresses. Sending pexRequest to random peer", "peer", peer) r.RequestAddrs(peer) } @@ -547,7 +547,7 @@ func (r *Reactor) ensurePeers() { // This is done in addition to asking a peer for addresses to work-around // peers not participating in PEX. if len(toDial) == 0 { - r.Logger.Info("No addresses to dial. Falling back to seeds") + r.Logger.Debug("No addresses to dial. Falling back to seeds") r.dialSeeds() } } diff --git a/test/e2e/runner/rpc.go b/test/e2e/runner/rpc.go index 1a21c61fcc..0848bea559 100644 --- a/test/e2e/runner/rpc.go +++ b/test/e2e/runner/rpc.go @@ -86,8 +86,6 @@ func waitForNode(node *e2e.Node, height int64, timeout time.Duration) (*rpctypes return status, nil } lastHeight = status.SyncInfo.LatestBlockHeight - default: - return nil, err } time.Sleep(300 * time.Millisecond) From cf354c5176b36079631ae12eac9944754d8c959e Mon Sep 17 00:00:00 2001 From: Callum Waters <cmwaters19@gmail.com> Date: Fri, 30 Aug 2024 15:44:40 +0200 Subject: [PATCH 057/127] don't evict transcactions that have been proposed --- consensus/state.go | 2 -- mempool/cat/block_builder.go | 1 + mempool/cat/pool.go | 38 +++++++++++++----------------------- mempool/cat/reactor.go | 8 +++++--- mempool/cat/store.go | 4 ++-- mempool/cat/store_test.go | 2 +- mempool/cat/tx.go | 3 +++ 7 files changed, 26 insertions(+), 32 deletions(-) diff --git a/consensus/state.go b/consensus/state.go index 6d27ac75e1..e93bc2b97e 100644 --- a/consensus/state.go +++ b/consensus/state.go @@ -2048,9 +2048,7 @@ func (cs *State) addCompactBlock(msg *CompactBlockMessage, peerID p2p.ID) error func (cs *State) fetchCompactBlock(ctx context.Context, blockHash []byte, compactBlock *types.Block, round int32) { txs, err := cs.txFetcher.FetchTxsFromKeys(ctx, blockHash, compactBlock.Data.Txs.ToSliceOfBytes()) - cs.Logger.Info("requesting lock") cs.mtx.Lock() - cs.Logger.Info("got lock") defer cs.mtx.Unlock() if err != nil { cs.metrics.CompactBlocksFailed.Add(1) diff --git a/mempool/cat/block_builder.go b/mempool/cat/block_builder.go index 44908ec01c..d7f6f01352 100644 --- a/mempool/cat/block_builder.go +++ b/mempool/cat/block_builder.go @@ -111,6 +111,7 @@ func (memR *Reactor) FetchKeysFromTxs(ctx context.Context, txs [][]byte) ([][]by // We don't set the priority, gasWanted or sender fields because we // don't know them. wtx := newWrappedTx(tx, key, memR.mempool.Height(), 0, 0, "") + wtx.evictable = false memR.broadcastNewTx(wtx) // For safety we also store this transaction in the mempool (ignoring // all size limits) so that we can retrieve it later if needed. Note diff --git a/mempool/cat/pool.go b/mempool/cat/pool.go index 06c5159e8c..75f16c83ed 100644 --- a/mempool/cat/pool.go +++ b/mempool/cat/pool.go @@ -4,13 +4,10 @@ import ( "errors" "fmt" "path/filepath" - "runtime" "sort" "sync" "time" - "github.com/creachadair/taskgroup" - abci "github.com/tendermint/tendermint/abci/types" "github.com/tendermint/tendermint/config" "github.com/tendermint/tendermint/libs/log" @@ -466,7 +463,7 @@ func (txmp *TxPool) ReapMaxBytesMaxGas(maxBytes, maxGas int64) types.Txs { // skip transactions that have been in the mempool for less than the inclusion delay // This gives time for the transaction to be broadcast to all peers if currentTime.Sub(w.timestamp) < InclusionDelay { - break + continue } // N.B. When computing byte size, we need to include the overhead for @@ -478,6 +475,7 @@ func (txmp *TxPool) ReapMaxBytesMaxGas(maxBytes, maxGas int64) types.Txs { } totalBytes += txBytes totalGas += w.gasWanted + w.evictable = false keep = append(keep, w.tx) } return keep @@ -599,7 +597,7 @@ func (txmp *TxPool) addNewTransaction(wtx *wrappedTx, checkTxRes *abci.ResponseC // of them as necessary to make room for tx. If no such items exist, we // discard tx. if !txmp.canAddTx(wtx.size()) { - victims, victimBytes := txmp.store.getTxsBelowPriority(wtx.priority) + victims, victimBytes := txmp.store.getEvictableTxsBelowPriority(wtx.priority) // If there are no suitable eviction candidates, or the total size of // those candidates is not enough to make room for the new transaction, @@ -721,34 +719,26 @@ func (txmp *TxPool) recheckTransactions() { ) // Collect transactions currently in the mempool requiring recheck. - wtxs := txmp.store.getAllTxs() + wtxs := txmp.allEntriesSorted() // Issue CheckTx calls for each remaining transaction, and when all the // rechecks are complete signal watchers that transactions may be available. go func() { - g, start := taskgroup.New(nil).Limit(2 * runtime.NumCPU()) - for _, wtx := range wtxs { wtx := wtx - start(func() error { - // The response for this CheckTx is handled by the default recheckTxCallback. - rsp, err := txmp.proxyAppConn.CheckTxSync(abci.RequestCheckTx{ - Tx: wtx.tx, - Type: abci.CheckTxType_Recheck, - }) - if err != nil { - txmp.logger.Error("failed to execute CheckTx during recheck", - "err", err, "key", fmt.Sprintf("%x", wtx.key)) - } else { - txmp.handleRecheckResult(wtx, rsp) - } - return nil + // The response for this CheckTx is handled by the default recheckTxCallback. + rsp, err := txmp.proxyAppConn.CheckTxSync(abci.RequestCheckTx{ + Tx: wtx.tx, + Type: abci.CheckTxType_Recheck, }) + if err != nil { + txmp.logger.Error("failed to execute CheckTx during recheck", + "err", err, "key", fmt.Sprintf("%x", wtx.key)) + } else { + txmp.handleRecheckResult(wtx, rsp) + } } _ = txmp.proxyAppConn.FlushAsync() - - // When recheck is complete, trigger a notification for more transactions. - _ = g.Wait() txmp.notifyTxsAvailable() }() } diff --git a/mempool/cat/reactor.go b/mempool/cat/reactor.go index 00f8998444..6989e67c34 100644 --- a/mempool/cat/reactor.go +++ b/mempool/cat/reactor.go @@ -266,6 +266,11 @@ func (memR *Reactor) ReceiveEnvelope(e p2p.Envelope) { memR.mempool.PeerHasTx(peerID, key) memR.Logger.Debug("received new trasaction", "peerID", peerID, "txKey", key) } + // If a block has been proposed with this transaction and + // consensus was waiting for it, it will now be published. + memR.blockFetcher.TryAddMissingTx(key, tx) + + // Now attempt to add the tx to the mempool. _, err = memR.mempool.TryAddNewTx(ntx, key, txInfo) if err != nil && err != ErrTxInMempool && err != ErrTxRecentlyCommitted { if memR.blockFetcher.IsMissingTx(key) { @@ -275,9 +280,6 @@ func (memR *Reactor) ReceiveEnvelope(e p2p.Envelope) { memR.Logger.Info("Could not add tx from peer", "peerID", peerID, "txKey", key, "err", err) return } - // If a block has been proposed with this transaction and - // consensus was waiting for it, it will now be published. - memR.blockFetcher.TryAddMissingTx(key, tx) if !memR.opts.ListenOnly { // We broadcast only transactions that we deem valid and actually have in our mempool. memR.broadcastSeenTx(key) diff --git a/mempool/cat/store.go b/mempool/cat/store.go index 6386ebf371..239ac69c91 100644 --- a/mempool/cat/store.go +++ b/mempool/cat/store.go @@ -166,13 +166,13 @@ func (s *store) getAllTxs() []*wrappedTx { return txs } -func (s *store) getTxsBelowPriority(priority int64) ([]*wrappedTx, int64) { +func (s *store) getEvictableTxsBelowPriority(priority int64) ([]*wrappedTx, int64) { s.mtx.RLock() defer s.mtx.RUnlock() txs := make([]*wrappedTx, 0, len(s.txs)) bytes := int64(0) for _, tx := range s.txs { - if tx.priority < priority { + if tx.priority < priority && tx.evictable { txs = append(txs, tx) bytes += tx.size() } diff --git a/mempool/cat/store_test.go b/mempool/cat/store_test.go index 4397f239ea..10470a62f5 100644 --- a/mempool/cat/store_test.go +++ b/mempool/cat/store_test.go @@ -153,7 +153,7 @@ func TestStoreGetTxs(t *testing.T) { require.Equal(t, numTxs, len(keys)) // get txs below a certain priority - txs, bz := store.getTxsBelowPriority(int64(numTxs / 2)) + txs, bz := store.getEvictableTxsBelowPriority(int64(numTxs / 2)) require.Equal(t, numTxs/2, len(txs)) var actualBz int64 for _, tx := range txs { diff --git a/mempool/cat/tx.go b/mempool/cat/tx.go index 8de425cee4..ad9bd15399 100644 --- a/mempool/cat/tx.go +++ b/mempool/cat/tx.go @@ -18,6 +18,8 @@ type wrappedTx struct { gasWanted int64 // app: gas required to execute this transaction priority int64 // app: priority value for this transaction sender string // app: assigned sender label + evictable bool // whether this transaction can be evicted from the mempool. This is false when the transaction + // is a part of a proposed block } func newWrappedTx(tx types.Tx, key types.TxKey, height, gasWanted, priority int64, sender string) *wrappedTx { @@ -29,6 +31,7 @@ func newWrappedTx(tx types.Tx, key types.TxKey, height, gasWanted, priority int6 gasWanted: gasWanted, priority: priority, sender: sender, + evictable: true, } } From dddd61e91665855c24b1658fc42b508edfaa86c8 Mon Sep 17 00:00:00 2001 From: Callum Waters <cmwaters19@gmail.com> Date: Fri, 30 Aug 2024 16:08:09 +0200 Subject: [PATCH 058/127] mark peer as having sent compact proposal --- consensus/reactor.go | 1 + 1 file changed, 1 insertion(+) diff --git a/consensus/reactor.go b/consensus/reactor.go index 18eadf9f03..49f3a9f556 100644 --- a/consensus/reactor.go +++ b/consensus/reactor.go @@ -728,6 +728,7 @@ OUTER_LOOP: ProposalPol: *rs.Votes.Prevotes(rs.Proposal.POLRound).BitArray().ToProto(), }, }, logger) { + ps.SetHasBlock(rs.Height, rs.Round) schema.WriteConsensusState( conR.traceClient, rs.Height, From 92e20ded371c038014a4349acb311051faf115ae Mon Sep 17 00:00:00 2001 From: Callum Waters <cmwaters19@gmail.com> Date: Fri, 30 Aug 2024 16:12:06 +0200 Subject: [PATCH 059/127] don't evict transactions in a proposal --- mempool/cat/block_builder.go | 1 + 1 file changed, 1 insertion(+) diff --git a/mempool/cat/block_builder.go b/mempool/cat/block_builder.go index d7f6f01352..4d3b240710 100644 --- a/mempool/cat/block_builder.go +++ b/mempool/cat/block_builder.go @@ -35,6 +35,7 @@ func (memR *Reactor) FetchTxsFromKeys(ctx context.Context, blockID []byte, compa wtx := memR.mempool.store.get(txKey) if wtx != nil { txs[i] = wtx.tx + wtx.evictable = false } else { missingKeys[i] = txKey } From 0a5adc0ea0be304f88ee07cdb7b4f4a32a43cb77 Mon Sep 17 00:00:00 2001 From: Callum Waters <cmwaters19@gmail.com> Date: Fri, 30 Aug 2024 16:16:13 +0200 Subject: [PATCH 060/127] make sure the lock is correctly used --- mempool/cat/block_builder.go | 2 +- mempool/cat/pool.go | 2 +- mempool/cat/store.go | 8 ++++++++ 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/mempool/cat/block_builder.go b/mempool/cat/block_builder.go index 4d3b240710..491b2e916a 100644 --- a/mempool/cat/block_builder.go +++ b/mempool/cat/block_builder.go @@ -35,7 +35,7 @@ func (memR *Reactor) FetchTxsFromKeys(ctx context.Context, blockID []byte, compa wtx := memR.mempool.store.get(txKey) if wtx != nil { txs[i] = wtx.tx - wtx.evictable = false + memR.mempool.store.markAsUnevictable(txKey) } else { missingKeys[i] = txKey } diff --git a/mempool/cat/pool.go b/mempool/cat/pool.go index 75f16c83ed..095e1dd6d8 100644 --- a/mempool/cat/pool.go +++ b/mempool/cat/pool.go @@ -475,7 +475,7 @@ func (txmp *TxPool) ReapMaxBytesMaxGas(maxBytes, maxGas int64) types.Txs { } totalBytes += txBytes totalGas += w.gasWanted - w.evictable = false + txmp.store.markAsUnevictable(w.key) keep = append(keep, w.tx) } return keep diff --git a/mempool/cat/store.go b/mempool/cat/store.go index 239ac69c91..334a9587d2 100644 --- a/mempool/cat/store.go +++ b/mempool/cat/store.go @@ -64,6 +64,14 @@ func (s *store) has(txKey types.TxKey) bool { return has } +func (s *store) markAsUnevictable(txKey types.TxKey) { + s.mtx.Lock() + defer s.mtx.Unlock() + if tx, exists := s.txs[txKey]; exists { + tx.evictable = false + } +} + func (s *store) markAsCommitted(txKeys []types.TxKey) { s.mtx.Lock() defer s.mtx.Unlock() From 398c7ce145e0af711890b7081536da89ef143c5d Mon Sep 17 00:00:00 2001 From: evan-forbes <evan.samuel.forbes@gmail.com> Date: Tue, 17 Sep 2024 14:38:27 -0500 Subject: [PATCH 061/127] feat: pipeline block prop --- mempool/cat/pool.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/mempool/cat/pool.go b/mempool/cat/pool.go index 095e1dd6d8..224b427a8b 100644 --- a/mempool/cat/pool.go +++ b/mempool/cat/pool.go @@ -499,6 +499,14 @@ func (txmp *TxPool) ReapMaxTxs(max int) types.Txs { if max >= 0 && len(keep) >= max { break } + // don't include txs unless they've been in the mempool for a 1 minute + // or more. This hack is a crude, and slow, form of pipelining block + // propagation. The full vacuum! protocol handles this elegantly while + // still maintaining reliable and usable throughput, so if you make a + // tweet criticizing this be prepared to be relentlessly dunked on. + if w.timestamp.After(time.Now().Add(time.Minute * -1)) { + continue + } keep = append(keep, w.tx) } return keep From 5a07b9da1ce3f04e269f096e22075ffd2129d59c Mon Sep 17 00:00:00 2001 From: evan-forbes <evan.samuel.forbes@gmail.com> Date: Tue, 17 Sep 2024 16:03:32 -0500 Subject: [PATCH 062/127] fix: max block size go boom --- types/params.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/params.go b/types/params.go index 2fc704f115..6d4c475f25 100644 --- a/types/params.go +++ b/types/params.go @@ -12,7 +12,7 @@ import ( const ( // MaxBlockSizeBytes is the maximum permitted size of the blocks. - MaxBlockSizeBytes = 104857600 // 100MB + MaxBlockSizeBytes = 1_000_000_000 // 100MB // BlockPartSizeBytes is the size of one block part. BlockPartSizeBytes uint32 = 65536 // 64kB From 327c7d620295b7d93c60ef9573edcdeb9004cad7 Mon Sep 17 00:00:00 2001 From: evan-forbes <evan.samuel.forbes@gmail.com> Date: Tue, 17 Sep 2024 18:19:50 -0500 Subject: [PATCH 063/127] fix: actual inclusion delay --- mempool/cat/pool.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/mempool/cat/pool.go b/mempool/cat/pool.go index 224b427a8b..4123b30d58 100644 --- a/mempool/cat/pool.go +++ b/mempool/cat/pool.go @@ -29,8 +29,12 @@ var ( // InclusionDelay is the amount of time a transaction must be in the mempool // before it is included in the block. const ( - InclusionDelay = 2 * time.Second - SeenSetPruneInterval = 10 * time.Minute + // InclusionDelay = 2 * time.Second + SeenSetPruneInterval = 100 * time.Minute +) + +var ( + InclusionDelay = 60 * time.Second ) // TxPoolOption sets an optional parameter on the TxPool. From f4f0ef195d490d4d44d8b0164b3818d110a591d9 Mon Sep 17 00:00:00 2001 From: evan-forbes <evan.samuel.forbes@gmail.com> Date: Tue, 17 Sep 2024 19:37:32 -0500 Subject: [PATCH 064/127] chore: bump settings --- mempool/cat/pool.go | 2 +- mempool/cat/reactor.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/mempool/cat/pool.go b/mempool/cat/pool.go index 4123b30d58..8d3978abda 100644 --- a/mempool/cat/pool.go +++ b/mempool/cat/pool.go @@ -34,7 +34,7 @@ const ( ) var ( - InclusionDelay = 60 * time.Second + InclusionDelay = 120 * time.Second ) // TxPoolOption sets an optional parameter on the TxPool. diff --git a/mempool/cat/reactor.go b/mempool/cat/reactor.go index 6989e67c34..a8c57068a1 100644 --- a/mempool/cat/reactor.go +++ b/mempool/cat/reactor.go @@ -172,14 +172,14 @@ func (memR *Reactor) GetChannels() []*p2p.ChannelDescriptor { { ID: mempool.MempoolChannel, Priority: 4, - SendQueueCapacity: 50, + SendQueueCapacity: 5000, RecvMessageCapacity: txMsg.Size(), MessageType: &protomem.Message{}, }, { ID: MempoolStateChannel, Priority: 5, - SendQueueCapacity: 500, + SendQueueCapacity: 5000, RecvMessageCapacity: stateMsg.Size(), MessageType: &protomem.Message{}, }, From 933ff1226f9300b22cd163eb3b281dcb5fb0ed79 Mon Sep 17 00:00:00 2001 From: evan-forbes <evan.samuel.forbes@gmail.com> Date: Wed, 18 Sep 2024 08:55:12 -0500 Subject: [PATCH 065/127] chore: add some logs --- mempool/cat/pool.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/mempool/cat/pool.go b/mempool/cat/pool.go index 8d3978abda..f3995f66f1 100644 --- a/mempool/cat/pool.go +++ b/mempool/cat/pool.go @@ -34,7 +34,8 @@ const ( ) var ( - InclusionDelay = 120 * time.Second + // set the default to 5, but this value can be changed in an init func + InclusionDelay = 5 * time.Second ) // TxPoolOption sets an optional parameter on the TxPool. @@ -404,6 +405,7 @@ func (txmp *TxPool) TryAddNewTx(tx types.Tx, key types.TxKey, txInfo mempool.TxI func (txmp *TxPool) RemoveTxByKey(txKey types.TxKey) error { txmp.removeTxByKey(txKey) txmp.metrics.EvictedTxs.Add(1) + txmp.logger.Error("tx evicted via remove by tx key") return nil } @@ -616,6 +618,7 @@ func (txmp *TxPool) addNewTransaction(wtx *wrappedTx, checkTxRes *abci.ResponseC // drop the new one. if len(victims) == 0 || victimBytes < wtx.size() { txmp.metrics.EvictedTxs.Add(1) + txmp.logger.Error("tx evicted cause we ran out of room") txmp.evictedTxCache.Push(wtx.key) checkTxRes.MempoolError = fmt.Sprintf("rejected valid incoming transaction; mempool is full (%X)", wtx.key) @@ -672,6 +675,7 @@ func (txmp *TxPool) addNewTransaction(wtx *wrappedTx, checkTxRes *abci.ResponseC func (txmp *TxPool) evictTx(wtx *wrappedTx) { txmp.store.remove(wtx.key) txmp.evictedTxCache.Push(wtx.key) + txmp.logger.Error("tx evicted") txmp.metrics.EvictedTxs.Add(1) txmp.logger.Debug( "evicted valid existing transaction; mempool full", From b7bc6e47be98370bbf5f6d20709b16f267302653 Mon Sep 17 00:00:00 2001 From: evan-forbes <evan.samuel.forbes@gmail.com> Date: Mon, 23 Sep 2024 07:00:35 -0500 Subject: [PATCH 066/127] feat: simplify seen logic and wait for enough seens --- mempool/cat/cache.go | 22 ++++++++++++++++----- mempool/cat/pool.go | 13 ++++++++++--- mempool/cat/reactor.go | 44 ++++++++++++++++++++++++------------------ 3 files changed, 52 insertions(+), 27 deletions(-) diff --git a/mempool/cat/cache.go b/mempool/cat/cache.go index 7540f79126..7cff9e5ddf 100644 --- a/mempool/cat/cache.go +++ b/mempool/cat/cache.go @@ -2,9 +2,9 @@ package cat import ( "container/list" + "sync" "time" - tmsync "github.com/tendermint/tendermint/libs/sync" "github.com/tendermint/tendermint/types" ) @@ -15,7 +15,7 @@ import ( type LRUTxCache struct { staticSize int - mtx tmsync.Mutex + mtx sync.RWMutex // cacheMap is used as a quick look up table cacheMap map[types.TxKey]*list.Element // list is a doubly linked list used to capture the FIFO nature of the cache @@ -98,7 +98,7 @@ func (c *LRUTxCache) Has(txKey types.TxKey) bool { // SeenTxSet records transactions that have been // seen by other peers but not yet by us type SeenTxSet struct { - mtx tmsync.Mutex + mtx sync.RWMutex set map[types.TxKey]timestampedPeerSet } @@ -182,8 +182,8 @@ func (s *SeenTxSet) Has(txKey types.TxKey, peer uint16) bool { } func (s *SeenTxSet) Get(txKey types.TxKey) map[uint16]struct{} { - s.mtx.Lock() - defer s.mtx.Unlock() + s.mtx.RLock() + defer s.mtx.RUnlock() seenSet, exists := s.set[txKey] if !exists { return nil @@ -196,6 +196,18 @@ func (s *SeenTxSet) Get(txKey types.TxKey) map[uint16]struct{} { return peers } +func (s *SeenTxSet) GetSeenCount(txKey types.TxKey) int { + s.mtx.RLock() + defer s.mtx.RUnlock() + + seenSet, exists := s.set[txKey] + if !exists { + return 0 + } + + return len(seenSet.peers) +} + // Len returns the amount of cached items. Mostly used for testing. func (s *SeenTxSet) Len() int { s.mtx.Lock() diff --git a/mempool/cat/pool.go b/mempool/cat/pool.go index f3995f66f1..e550bd83a7 100644 --- a/mempool/cat/pool.go +++ b/mempool/cat/pool.go @@ -6,6 +6,7 @@ import ( "path/filepath" "sort" "sync" + "sync/atomic" "time" abci "github.com/tendermint/tendermint/abci/types" @@ -36,6 +37,7 @@ const ( var ( // set the default to 5, but this value can be changed in an init func InclusionDelay = 5 * time.Second + peerCount = atomic.Int32{} ) // TxPoolOption sets an optional parameter on the TxPool. @@ -432,7 +434,7 @@ func (txmp *TxPool) Flush() { // PeerHasTx marks that the transaction has been seen by a peer. func (txmp *TxPool) PeerHasTx(peer uint16, txKey types.TxKey) { - txmp.logger.Debug("peer has tx", "peer", peer, "txKey", fmt.Sprintf("%X", txKey)) + // txmp.logger.Debug("peer has tx", "peer", peer, "txKey", fmt.Sprintf("%X", txKey)) txmp.seenByPeersSet.Add(txKey, peer) } @@ -462,13 +464,18 @@ func (txmp *TxPool) allEntriesSorted() []*wrappedTx { // constraints, the result will also be empty. func (txmp *TxPool) ReapMaxBytesMaxGas(maxBytes, maxGas int64) types.Txs { var totalGas, totalBytes int64 - currentTime := time.Now() + // currentTime := time.Now() var keep []types.Tx //nolint:prealloc for _, w := range txmp.allEntriesSorted() { // skip transactions that have been in the mempool for less than the inclusion delay // This gives time for the transaction to be broadcast to all peers - if currentTime.Sub(w.timestamp) < InclusionDelay { + // if currentTime.Sub(w.timestamp) < InclusionDelay { + // continue + // } + + seenCount := txmp.seenByPeersSet.GetSeenCount(w.key) + if seenCount < (2 * (int(peerCount.Load()) / 3)) { continue } diff --git a/mempool/cat/reactor.go b/mempool/cat/reactor.go index a8c57068a1..54ed706e8b 100644 --- a/mempool/cat/reactor.go +++ b/mempool/cat/reactor.go @@ -198,6 +198,7 @@ func (memR *Reactor) AddPeer(peer p2p.Peer) { for _, key := range keys { memR.broadcastSeenTx(key) } + peerCount.Add(1) } // RemovePeer implements Reactor. For all current outbound requests to this @@ -213,6 +214,11 @@ func (memR *Reactor) RemovePeer(peer p2p.Peer, reason interface{}) { for key := range outboundRequests { memR.findNewPeerToRequestTx(key) } + n := peerCount.Add(-1) + if n < 0 { + memR.Logger.Error("seen req went below one, resetting") + peerCount.Store(0) + } } func (memR *Reactor) Receive(chID byte, peer p2p.Peer, msgBytes []byte) { @@ -350,13 +356,13 @@ func (memR *Reactor) ReceiveEnvelope(e p2p.Envelope) { // before we receive a WantTx message from them. In this case we might // ignore the request if we know it's no longer valid. if has && !memR.opts.ListenOnly { - peerID := memR.ids.GetIDForPeer(e.Src.ID()) - memR.Logger.Debug("sending a transaction in response to a want msg", "peer", peerID, "txKey", txKey) + // peerID := memR.ids.GetIDForPeer(e.Src.ID()) + // memR.Logger.Debug("sending a transaction in response to a want msg", "peer", peerID, "txKey", txKey) if p2p.SendEnvelopeShim(e.Src, p2p.Envelope{ //nolint:staticcheck ChannelID: mempool.MempoolChannel, Message: &protomem.Txs{Txs: [][]byte{tx}}, }, memR.Logger) { - memR.mempool.PeerHasTx(peerID, txKey) + // memR.mempool.PeerHasTx(peerID, txKey) schema.WriteMempoolTx( memR.traceClient, string(e.Src.ID()), @@ -394,28 +400,28 @@ func (memR *Reactor) broadcastSeenTx(txKey types.TxKey) { panic(err) } - for id, peer := range memR.ids.GetAll() { - if p, ok := peer.Get(types.PeerStateKey).(PeerState); ok { - // make sure peer isn't too far behind. This can happen - // if the peer is blocksyncing still and catching up - // in which case we just skip sending the transaction - if p.GetHeight() < memR.mempool.Height()-peerHeightDiff { - memR.Logger.Debug("peer is too far behind us. Skipping broadcast of seen tx", "peerID", peer.ID(), - "peerHeight", p.GetHeight(), "ourHeight", memR.mempool.Height()) - continue - } - } + for _, peer := range memR.ids.GetAll() { + // if p, ok := peer.Get(types.PeerStateKey).(PeerState); ok { + // // make sure peer isn't too far behind. This can happen + // // if the peer is blocksyncing still and catching up + // // in which case we just skip sending the transaction + // if p.GetHeight() < memR.mempool.Height()-peerHeightDiff { + // memR.Logger.Debug("peer is too far behind us. Skipping broadcast of seen tx", "peerID", peer.ID(), + // "peerHeight", p.GetHeight(), "ourHeight", memR.mempool.Height()) + // continue + // } + // } // no need to send a seen tx message to a peer that already // has that tx. - if memR.mempool.seenByPeersSet.Has(txKey, id) { - continue - } + // if memR.mempool.seenByPeersSet.Has(txKey, id) { + // continue + // } if !peer.Send(MempoolStateChannel, bz) { memR.Logger.Error("failed to send seen tx to peer", "peerID", peer.ID(), "txKey", txKey) } } - memR.Logger.Debug("broadcasted seen tx to all peers", "tx_key", txKey.String()) + // memR.Logger.Debug("broadcasted seen tx to all peers", "tx_key", txKey.String()) } // broadcastNewTx broadcast new transaction to all peers unless we are already sure they have seen the tx. @@ -448,7 +454,7 @@ func (memR *Reactor) broadcastNewTx(wtx *wrappedTx) { } if peer.Send(mempool.MempoolChannel, bz) { //nolint:staticcheck - memR.mempool.PeerHasTx(id, wtx.key) + // memR.mempool.PeerHasTx(id, wtx.key) } else { memR.Logger.Error("failed to send new tx to peer", "peerID", peer.ID(), "txKey", wtx.key) } From 660ab64a5625622b3c8c7c1f66acdacebb54506e Mon Sep 17 00:00:00 2001 From: evan-forbes <evan.samuel.forbes@gmail.com> Date: Mon, 23 Sep 2024 16:54:25 -0500 Subject: [PATCH 067/127] feat: sort by fee, then by seen --- mempool/cat/pool.go | 17 ++++++++++++++--- mempool/cat/tx.go | 1 + 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/mempool/cat/pool.go b/mempool/cat/pool.go index e550bd83a7..bb8087a761 100644 --- a/mempool/cat/pool.go +++ b/mempool/cat/pool.go @@ -452,6 +452,18 @@ func (txmp *TxPool) allEntriesSorted() []*wrappedTx { return txs } +func (txmp *TxPool) allEntriesMostSeen() []*wrappedTx { + txs := txmp.allEntriesSorted() + for i, tx := range txs { + seen := txmp.seenByPeersSet.GetSeenCount(tx.key) + txs[i].seenCount = seen + } + sort.SliceStable(txs, func(i, j int) bool { + return txs[i].seenCount > txs[j].seenCount + }) + return txs +} + // ReapMaxBytesMaxGas returns a slice of valid transactions that fit within the // size and gas constraints. The results are ordered by nonincreasing priority, // with ties broken by increasing order of arrival. Reaping transactions does @@ -467,15 +479,14 @@ func (txmp *TxPool) ReapMaxBytesMaxGas(maxBytes, maxGas int64) types.Txs { // currentTime := time.Now() var keep []types.Tx //nolint:prealloc - for _, w := range txmp.allEntriesSorted() { + for _, w := range txmp.allEntriesMostSeen() { // skip transactions that have been in the mempool for less than the inclusion delay // This gives time for the transaction to be broadcast to all peers // if currentTime.Sub(w.timestamp) < InclusionDelay { // continue // } - seenCount := txmp.seenByPeersSet.GetSeenCount(w.key) - if seenCount < (2 * (int(peerCount.Load()) / 3)) { + if w.seenCount < (2 * (int(peerCount.Load()) / 3)) { continue } diff --git a/mempool/cat/tx.go b/mempool/cat/tx.go index ad9bd15399..7d4b269d0c 100644 --- a/mempool/cat/tx.go +++ b/mempool/cat/tx.go @@ -20,6 +20,7 @@ type wrappedTx struct { sender string // app: assigned sender label evictable bool // whether this transaction can be evicted from the mempool. This is false when the transaction // is a part of a proposed block + seenCount int } func newWrappedTx(tx types.Tx, key types.TxKey, height, gasWanted, priority int64, sender string) *wrappedTx { From 3b69305836ed00ac14f73d22946f3a85ec9099f4 Mon Sep 17 00:00:00 2001 From: evan-forbes <evan.samuel.forbes@gmail.com> Date: Mon, 23 Sep 2024 17:22:38 -0500 Subject: [PATCH 068/127] feat: don't include txs that originated from us in a block --- mempool/cat/block_builder.go | 2 +- mempool/cat/pool.go | 7 ++++++- mempool/cat/store_test.go | 10 +++++----- mempool/cat/tx.go | 9 ++++++--- 4 files changed, 18 insertions(+), 10 deletions(-) diff --git a/mempool/cat/block_builder.go b/mempool/cat/block_builder.go index 491b2e916a..0de67dd367 100644 --- a/mempool/cat/block_builder.go +++ b/mempool/cat/block_builder.go @@ -111,7 +111,7 @@ func (memR *Reactor) FetchKeysFromTxs(ctx context.Context, txs [][]byte) ([][]by // // We don't set the priority, gasWanted or sender fields because we // don't know them. - wtx := newWrappedTx(tx, key, memR.mempool.Height(), 0, 0, "") + wtx := newWrappedTx(tx, key, memR.mempool.Height(), 0, 0, "", false) wtx.evictable = false memR.broadcastNewTx(wtx) // For safety we also store this transaction in the mempool (ignoring diff --git a/mempool/cat/pool.go b/mempool/cat/pool.go index bb8087a761..df531ec913 100644 --- a/mempool/cat/pool.go +++ b/mempool/cat/pool.go @@ -381,7 +381,7 @@ func (txmp *TxPool) TryAddNewTx(tx types.Tx, key types.TxKey, txInfo mempool.TxI // Create wrapped tx wtx := newWrappedTx( - tx, key, txmp.Height(), rsp.GasWanted, rsp.Priority, rsp.Sender, + tx, key, txmp.Height(), rsp.GasWanted, rsp.Priority, rsp.Sender, txInfo.SenderID == 0, ) // Perform the post check @@ -486,6 +486,11 @@ func (txmp *TxPool) ReapMaxBytesMaxGas(maxBytes, maxGas int64) types.Txs { // continue // } + // skip our own txs to force them to be distributed before being included. + if w.selfTx { + continue + } + if w.seenCount < (2 * (int(peerCount.Load()) / 3)) { continue } diff --git a/mempool/cat/store_test.go b/mempool/cat/store_test.go index 10470a62f5..e64aa67977 100644 --- a/mempool/cat/store_test.go +++ b/mempool/cat/store_test.go @@ -16,7 +16,7 @@ func TestStoreSimple(t *testing.T) { tx := types.Tx("tx1") key := tx.Key() - wtx := newWrappedTx(tx, key, 1, 1, 1, "") + wtx := newWrappedTx(tx, key, 1, 1, 1, "", false) // asset zero state require.Nil(t, store.get(key)) @@ -47,7 +47,7 @@ func TestStoreReservingTxs(t *testing.T) { tx := types.Tx("tx1") key := tx.Key() - wtx := newWrappedTx(tx, key, 1, 1, 1, "") + wtx := newWrappedTx(tx, key, 1, 1, 1, "", false) // asset zero state store.release(key) @@ -105,7 +105,7 @@ func TestStoreConcurrentAccess(t *testing.T) { for range ticker.C { tx := types.Tx(fmt.Sprintf("tx%d", i%(numTxs/10))) key := tx.Key() - wtx := newWrappedTx(tx, key, 1, 1, 1, "") + wtx := newWrappedTx(tx, key, 1, 1, 1, "", false) existingTx := store.get(key) if existingTx != nil && bytes.Equal(existingTx.tx, tx) { // tx has already been added @@ -138,7 +138,7 @@ func TestStoreGetTxs(t *testing.T) { for i := 0; i < numTxs; i++ { tx := types.Tx(fmt.Sprintf("tx%d", i)) key := tx.Key() - wtx := newWrappedTx(tx, key, 1, 1, int64(i), "") + wtx := newWrappedTx(tx, key, 1, 1, int64(i), "", false) store.set(wtx) } @@ -168,7 +168,7 @@ func TestStoreExpiredTxs(t *testing.T) { for i := 0; i < numTxs; i++ { tx := types.Tx(fmt.Sprintf("tx%d", i)) key := tx.Key() - wtx := newWrappedTx(tx, key, int64(i), 1, 1, "") + wtx := newWrappedTx(tx, key, int64(i), 1, 1, "", false) store.set(wtx) } diff --git a/mempool/cat/tx.go b/mempool/cat/tx.go index 7d4b269d0c..90dab5299e 100644 --- a/mempool/cat/tx.go +++ b/mempool/cat/tx.go @@ -18,12 +18,14 @@ type wrappedTx struct { gasWanted int64 // app: gas required to execute this transaction priority int64 // app: priority value for this transaction sender string // app: assigned sender label - evictable bool // whether this transaction can be evicted from the mempool. This is false when the transaction - // is a part of a proposed block + evictable bool // whether this transaction can be evicted from the mempool. This is false when the transaction is a part of a proposed block nolint:lll + + selfTx bool // keeps track of if this tx originated from this node. If so, then it will not be included in a block. This is a hack. + // temporary var only used for sorting when reaping seenCount int } -func newWrappedTx(tx types.Tx, key types.TxKey, height, gasWanted, priority int64, sender string) *wrappedTx { +func newWrappedTx(tx types.Tx, key types.TxKey, height, gasWanted, priority int64, sender string, self bool) *wrappedTx { return &wrappedTx{ tx: tx, key: key, @@ -33,6 +35,7 @@ func newWrappedTx(tx types.Tx, key types.TxKey, height, gasWanted, priority int6 priority: priority, sender: sender, evictable: true, + selfTx: self, } } From 684abfa26cd99aee9575af42d612093c88c7c470 Mon Sep 17 00:00:00 2001 From: evan-forbes <evan.samuel.forbes@gmail.com> Date: Wed, 25 Sep 2024 00:13:24 -0500 Subject: [PATCH 069/127] feat: insanely hacky version of starling and another part of vacuum --- mempool/cat/block_builder.go | 2 +- mempool/cat/cache.go | 4 +- mempool/cat/peers.go | 14 +++- mempool/cat/pool.go | 20 +---- mempool/cat/reactor.go | 121 ++++++++++++++++++++------- mempool/cat/requests.go | 4 +- mempool/cat/requests_test.go | 14 ++-- mempool/cat/store.go | 8 +- mempool/cat/wants.go | 35 ++++++++ node/node.go | 4 +- proto/tendermint/mempool/types.pb.go | 89 +++++++++++++++----- proto/tendermint/mempool/types.proto | 3 +- 12 files changed, 227 insertions(+), 91 deletions(-) create mode 100644 mempool/cat/wants.go diff --git a/mempool/cat/block_builder.go b/mempool/cat/block_builder.go index 0de67dd367..211a8c6b43 100644 --- a/mempool/cat/block_builder.go +++ b/mempool/cat/block_builder.go @@ -82,7 +82,7 @@ func (memR *Reactor) FetchTxsFromKeys(ctx context.Context, blockID []byte, compa for _, key := range missingKeys { numHaveTx := len(memR.mempool.seenByPeersSet.Get(key)) memR.Logger.Info("searching for missing tx", "numHaveTx", numHaveTx) - memR.findNewPeerToRequestTx(key) + memR.findNewPeerToRequestTx(key, 5) } // Wait for the reactor to retrieve and post all transactions. diff --git a/mempool/cat/cache.go b/mempool/cat/cache.go index 7cff9e5ddf..0029a2746d 100644 --- a/mempool/cat/cache.go +++ b/mempool/cat/cache.go @@ -171,8 +171,8 @@ func (s *SeenTxSet) Prune(limit time.Time) { } func (s *SeenTxSet) Has(txKey types.TxKey, peer uint16) bool { - s.mtx.Lock() - defer s.mtx.Unlock() + s.mtx.RLock() + defer s.mtx.RUnlock() seenSet, exists := s.set[txKey] if !exists { return false diff --git a/mempool/cat/peers.go b/mempool/cat/peers.go index 86a6f4c010..71313c9614 100644 --- a/mempool/cat/peers.go +++ b/mempool/cat/peers.go @@ -17,12 +17,14 @@ type mempoolIDs struct { peerMap map[p2p.ID]uint16 // quick lookup table for peer ID to short ID nextID uint16 // assumes that a node will never have over 65536 active peers activeIDs map[uint16]p2p.Peer // used to check if a given peerID key is used, the value doesn't matter + knownIDs map[uint16]p2p.ID } func newMempoolIDs() *mempoolIDs { return &mempoolIDs{ peerMap: make(map[p2p.ID]uint16), activeIDs: make(map[uint16]p2p.Peer), + knownIDs: make(map[uint16]p2p.ID), nextID: firstPeerID, // reserve unknownPeerID(0) for mempoolReactor.BroadcastTx } } @@ -40,19 +42,20 @@ func (ids *mempoolIDs) ReserveForPeer(peer p2p.Peer) { curID := ids.nextPeerID() ids.peerMap[peer.ID()] = curID ids.activeIDs[curID] = peer + ids.knownIDs[curID] = peer.ID() } // nextPeerID returns the next unused peer ID to use. // This assumes that ids's mutex is already locked. func (ids *mempoolIDs) nextPeerID() uint16 { - if len(ids.activeIDs) == mempool.MaxActiveIDs { + if (len(ids.activeIDs) + len(ids.knownIDs)) == mempool.MaxActiveIDs { panic(fmt.Sprintf("node has maximum %d active IDs and wanted to get one more", mempool.MaxActiveIDs)) } - _, idExists := ids.activeIDs[ids.nextID] + _, idExists := ids.knownIDs[ids.nextID] for idExists { ids.nextID++ - _, idExists = ids.activeIDs[ids.nextID] + _, idExists = ids.knownIDs[ids.nextID] } curID := ids.nextID ids.nextID++ @@ -80,7 +83,10 @@ func (ids *mempoolIDs) GetIDForPeer(peerID p2p.ID) uint16 { id, exists := ids.peerMap[peerID] if !exists { - return 0 + id = ids.nextPeerID() + ids.peerMap[peerID] = id + ids.knownIDs[id] = peerID + peerCount.Add(1) } return id } diff --git a/mempool/cat/pool.go b/mempool/cat/pool.go index df531ec913..39220612c4 100644 --- a/mempool/cat/pool.go +++ b/mempool/cat/pool.go @@ -216,16 +216,6 @@ func (txmp *TxPool) WasRecentlyEvicted(txKey types.TxKey) bool { return txmp.evictedTxCache.Has(txKey) } -// GetCommitted retrieves a committed transaction based on the key. -// It returns the transaction and a bool indicating if the transaction exists or not. -func (txmp *TxPool) GetCommitted(txKey types.TxKey) (types.Tx, bool) { - wtx := txmp.store.getCommitted(txKey) - if wtx != nil { - return wtx.tx, true - } - return types.Tx{}, false -} - // IsRejectedTx returns true if the transaction was recently rejected and is // currently within the cache func (txmp *TxPool) IsRejectedTx(txKey types.TxKey) bool { @@ -479,6 +469,7 @@ func (txmp *TxPool) ReapMaxBytesMaxGas(maxBytes, maxGas int64) types.Txs { // currentTime := time.Now() var keep []types.Tx //nolint:prealloc + for _, w := range txmp.allEntriesMostSeen() { // skip transactions that have been in the mempool for less than the inclusion delay // This gives time for the transaction to be broadcast to all peers @@ -486,11 +477,6 @@ func (txmp *TxPool) ReapMaxBytesMaxGas(maxBytes, maxGas int64) types.Txs { // continue // } - // skip our own txs to force them to be distributed before being included. - if w.selfTx { - continue - } - if w.seenCount < (2 * (int(peerCount.Load()) / 3)) { continue } @@ -520,11 +506,7 @@ func (txmp *TxPool) ReapMaxBytesMaxGas(maxBytes, maxGas int64) types.Txs { // does not have that many transactions available. func (txmp *TxPool) ReapMaxTxs(max int) types.Txs { var keep []types.Tx //nolint:prealloc - currentTime := time.Now() for _, w := range txmp.allEntriesSorted() { - if currentTime.Sub(w.timestamp) < InclusionDelay { - break - } if max >= 0 && len(keep) >= max { break } diff --git a/mempool/cat/reactor.go b/mempool/cat/reactor.go index 54ed706e8b..8f4e8553b2 100644 --- a/mempool/cat/reactor.go +++ b/mempool/cat/reactor.go @@ -42,6 +42,8 @@ type Reactor struct { requests *requestScheduler blockFetcher *blockFetcher traceClient trace.Tracer + self p2p.ID + wantState *wantState } type ReactorOptions struct { @@ -58,6 +60,8 @@ type ReactorOptions struct { // TraceClient is the trace client for collecting trace level events TraceClient trace.Tracer + + Self p2p.ID } func (opts *ReactorOptions) VerifyAndComplete() error { @@ -97,6 +101,7 @@ func NewReactor(mempool *TxPool, opts *ReactorOptions) (*Reactor, error) { requests: newRequestScheduler(opts.MaxGossipDelay, defaultGlobalRequestTimeout), blockFetcher: newBlockFetcher(), traceClient: opts.TraceClient, + wantState: NewWantState(), } memR.BaseReactor = *p2p.NewBaseReactor("Mempool", memR) return memR, nil @@ -164,6 +169,7 @@ func (memR *Reactor) GetChannels() []*p2p.ChannelDescriptor { Sum: &protomem.Message_SeenTx{ SeenTx: &protomem.SeenTx{ TxKey: make([]byte, tmhash.Size), + Peer: "5fb79350bd5d78c07607aefadcc35ed45bbd9101", }, }, } @@ -196,7 +202,20 @@ func (memR *Reactor) InitPeer(peer p2p.Peer) p2p.Peer { func (memR *Reactor) AddPeer(peer p2p.Peer) { keys := memR.mempool.store.getAllKeys() for _, key := range keys { - memR.broadcastSeenTx(key) + // memR.broadcastSeenTx(key, string(memR.self)) + msg := &protomem.Message{ + Sum: &protomem.Message_SeenTx{ + SeenTx: &protomem.SeenTx{ + TxKey: key[:], + Peer: string(memR.self), + }, + }, + } + bz, err := msg.Marshal() + if err != nil { + panic(err) + } + peer.Send(MempoolStateChannel, bz) } peerCount.Add(1) } @@ -212,7 +231,7 @@ func (memR *Reactor) RemovePeer(peer p2p.Peer, reason interface{}) { // we won't receive any responses from them. outboundRequests := memR.requests.ClearAllRequestsFrom(peerID) for key := range outboundRequests { - memR.findNewPeerToRequestTx(key) + memR.findNewPeerToRequestTx(key, 5) } n := peerCount.Add(-1) if n < 0 { @@ -288,8 +307,33 @@ func (memR *Reactor) ReceiveEnvelope(e p2p.Envelope) { } if !memR.opts.ListenOnly { // We broadcast only transactions that we deem valid and actually have in our mempool. - memR.broadcastSeenTx(key) + memR.broadcastSeenTx(key, string(memR.self)) } + + go func(tx []byte, key types.TxKey) { + wants, has := memR.wantState.GetWants(key) + if has { + for peer := range wants { + p := memR.ids.GetPeer(peer) + if p == nil { + return + } + if p2p.SendEnvelopeShim(e.Src, p2p.Envelope{ //nolint:staticcheck + ChannelID: mempool.MempoolChannel, + Message: &protomem.Txs{Txs: [][]byte{tx}}, + }, memR.Logger) { + // memR.mempool.PeerHasTx(peerID, txKey) + schema.WriteMempoolTx( + memR.traceClient, + string(p.ID()), + key[:], + schema.Upload, + ) + } + } + } + + }(tx, key) } // A peer has indicated to us that it has a transaction. We first verify the txkey and @@ -313,23 +357,25 @@ func (memR *Reactor) ReceiveEnvelope(e p2p.Envelope) { txKey[:], schema.Download, ) - peerID := memR.ids.GetIDForPeer(e.Src.ID()) + peerID := memR.ids.GetIDForPeer(p2p.ID(msg.Peer)) memR.mempool.PeerHasTx(peerID, txKey) // Check if we don't already have the transaction and that it was recently rejected if memR.mempool.Has(txKey) || memR.mempool.IsRejectedTx(txKey) || memR.mempool.store.hasCommitted(txKey) { - memR.Logger.Debug("received a seen tx for a tx we already have", "txKey", txKey) + // memR.Logger.Debug("received a seen tx for a tx we already have", "txKey", txKey) return } // If we are already requesting that tx, then we don't need to go any further. if memR.requests.ForTx(txKey) != 0 { - memR.Logger.Debug("received a SeenTx message for a transaction we are already requesting", "txKey", txKey) + // memR.Logger.Debug("received a SeenTx message for a transaction we are already requesting", "txKey", txKey) return } // We don't have the transaction, nor are we requesting it so we send the node // a want msg - memR.requestTx(txKey, e.Src) + memR.requestTx(txKey, e.Src, 5) + + memR.broadcastSeenTx(types.TxKey(msg.TxKey), msg.Peer) // A peer is requesting a transaction that we have claimed to have. Find the specified // transaction and broadcast it to the peer. We may no longer have the transaction @@ -348,10 +394,6 @@ func (memR *Reactor) ReceiveEnvelope(e p2p.Envelope) { schema.Download, ) tx, has := memR.mempool.GetTxByKey(txKey) - if !has { - // see if the tx was recently committed - tx, has = memR.mempool.GetCommitted(txKey) - } // TODO: consider handling the case where we receive a HasTx message from a peer // before we receive a WantTx message from them. In this case we might // ignore the request if we know it's no longer valid. @@ -370,6 +412,11 @@ func (memR *Reactor) ReceiveEnvelope(e p2p.Envelope) { schema.Upload, ) } + } else { + peer, has := memR.ids.peerMap[e.Src.ID()] + if has { + memR.wantState.Add(txKey, peer) + } } default: @@ -386,12 +433,13 @@ type PeerState interface { // broadcastSeenTx broadcasts a SeenTx message to all peers unless we // know they have already seen the transaction -func (memR *Reactor) broadcastSeenTx(txKey types.TxKey) { +func (memR *Reactor) broadcastSeenTx(txKey types.TxKey, from string) { memR.Logger.Debug("broadcasting seen tx to all peers", "tx_key", txKey.String()) msg := &protomem.Message{ Sum: &protomem.Message_SeenTx{ SeenTx: &protomem.SeenTx{ TxKey: txKey[:], + Peer: from, }, }, } @@ -438,20 +486,20 @@ func (memR *Reactor) broadcastNewTx(wtx *wrappedTx) { panic(err) } - for id, peer := range memR.ids.GetAll() { - if p, ok := peer.Get(types.PeerStateKey).(PeerState); ok { - // make sure peer isn't too far behind. This can happen - // if the peer is blocksyncing still and catching up - // in which case we just skip sending the transaction - if p.GetHeight() < wtx.height-peerHeightDiff { - memR.Logger.Debug("peer is too far behind us. Skipping broadcast of seen tx") - continue - } - } + for _, peer := range memR.ids.GetAll() { + // if p, ok := peer.Get(types.PeerStateKey).(PeerState); ok { + // // make sure peer isn't too far behind. This can happen + // // if the peer is blocksyncing still and catching up + // // in which case we just skip sending the transaction + // if p.GetHeight() < wtx.height-peerHeightDiff { + // memR.Logger.Debug("peer is too far behind us. Skipping broadcast of seen tx") + // continue + // } + // } - if memR.mempool.seenByPeersSet.Has(wtx.key, id) { - continue - } + // if memR.mempool.seenByPeersSet.Has(wtx.key, id) { + // continue + // } if peer.Send(mempool.MempoolChannel, bz) { //nolint:staticcheck // memR.mempool.PeerHasTx(id, wtx.key) @@ -463,7 +511,7 @@ func (memR *Reactor) broadcastNewTx(wtx *wrappedTx) { // requestTx requests a transaction from a peer and tracks it, // requesting it from another peer if the first peer does not respond. -func (memR *Reactor) requestTx(txKey types.TxKey, peer p2p.Peer) { +func (memR *Reactor) requestTx(txKey types.TxKey, peer p2p.Peer, tries int) { if peer == nil { // we have disconnected from the peer return @@ -482,7 +530,7 @@ func (memR *Reactor) requestTx(txKey types.TxKey, peer p2p.Peer) { if success { memR.Logger.Debug("requested transaction", "txKey", txKey, "peerID", peer.ID()) memR.mempool.metrics.RequestedTxs.Add(1) - requested := memR.requests.Add(txKey, memR.ids.GetIDForPeer(peer.ID()), memR.findNewPeerToRequestTx) + requested := memR.requests.Add(txKey, memR.ids.GetIDForPeer(peer.ID()), memR.findNewPeerToRequestTx, tries) if !requested { memR.Logger.Debug("have already marked a tx as requested", "txKey", txKey, "peerID", peer.ID()) } @@ -493,18 +541,29 @@ func (memR *Reactor) requestTx(txKey types.TxKey, peer p2p.Peer) { // findNewPeerToSendTx finds a new peer that has already seen the transaction to // request a transaction from. -func (memR *Reactor) findNewPeerToRequestTx(txKey types.TxKey) { +func (memR *Reactor) findNewPeerToRequestTx(txKey types.TxKey, tries int) { + if tries == 0 { + return + } + // ensure that we are connected to peers if memR.ids.Len() == 0 { return } + if committed := memR.mempool.store.hasCommitted(txKey); committed { + return + } + // get the next peer in the list of remaining peers that have seen the tx // and does not already have an outbound request for that tx seenMap := memR.mempool.seenByPeersSet.Get(txKey) var peerID uint16 for possiblePeer := range seenMap { if !memR.requests.Has(possiblePeer, txKey) { + if _, active := memR.ids.activeIDs[possiblePeer]; !active { + continue + } peerID = possiblePeer break } @@ -514,7 +573,7 @@ func (memR *Reactor) findNewPeerToRequestTx(txKey types.TxKey) { // No other free peer has the transaction we are looking for. // We give up 🤷♂️ and hope either a peer responds late or the tx // is gossiped again - memR.Logger.Debug("no other peer has the tx we are looking for", "txKey", txKey) + // memR.Logger.Error("no other peer has the tx we are looking for", "txKey", txKey) // TODO: should add a metric to see how common this is return } @@ -522,9 +581,9 @@ func (memR *Reactor) findNewPeerToRequestTx(txKey types.TxKey) { if peer == nil { // we disconnected from that peer, retry again until we exhaust the list memR.mempool.seenByPeersSet.Remove(txKey, peerID) - memR.findNewPeerToRequestTx(txKey) + memR.findNewPeerToRequestTx(txKey, tries-1) } else { memR.mempool.metrics.RerequestedTxs.Add(1) - memR.requestTx(txKey, peer) + memR.requestTx(txKey, peer, tries-1) } } diff --git a/mempool/cat/requests.go b/mempool/cat/requests.go index ea78ff5027..a37e90bf1f 100644 --- a/mempool/cat/requests.go +++ b/mempool/cat/requests.go @@ -43,7 +43,7 @@ func newRequestScheduler(responseTime, globalTimeout time.Duration) *requestSche } } -func (r *requestScheduler) Add(key types.TxKey, peer uint16, onTimeout func(key types.TxKey)) bool { +func (r *requestScheduler) Add(key types.TxKey, peer uint16, onTimeout func(key types.TxKey, tries int), tries int) bool { if peer == 0 { return false } @@ -62,7 +62,7 @@ func (r *requestScheduler) Add(key types.TxKey, peer uint16, onTimeout func(key // trigger callback. Callback can `Add` the tx back to the scheduler if onTimeout != nil { - onTimeout(key) + onTimeout(key, tries) } // We set another timeout because the peer could still send diff --git a/mempool/cat/requests_test.go b/mempool/cat/requests_test.go index 16065e0a34..e07fccc3a9 100644 --- a/mempool/cat/requests_test.go +++ b/mempool/cat/requests_test.go @@ -29,21 +29,21 @@ func TestRequestSchedulerRerequest(t *testing.T) { // create a request closeCh := make(chan struct{}) - require.True(t, requests.Add(key, peerA, func(key types.TxKey) { + require.True(t, requests.Add(key, peerA, func(key types.TxKey, tries int) { require.Equal(t, key, key) // the first peer times out to respond so we ask the second peer - require.True(t, requests.Add(key, peerB, func(key types.TxKey) { + require.True(t, requests.Add(key, peerB, func(key types.TxKey, tries int) { t.Fatal("did not expect to timeout") - })) + }, 100)) close(closeCh) - })) + }, 100)) // check that the request was added require.Equal(t, peerA, requests.ForTx(key)) require.True(t, requests.Has(peerA, key)) // should not be able to add the same request again - require.False(t, requests.Add(key, peerA, nil)) + require.False(t, requests.Add(key, peerA, nil, 100)) // wait for the scheduler to invoke the timeout <-closeCh @@ -72,7 +72,7 @@ func TestRequestSchedulerNonResponsivePeer(t *testing.T) { peerA uint16 = 1 // should be non-zero ) - require.True(t, requests.Add(key, peerA, nil)) + require.True(t, requests.Add(key, peerA, nil, 100)) require.Eventually(t, func() bool { return requests.ForTx(key) == 0 }, 100*time.Millisecond, 5*time.Millisecond) @@ -97,7 +97,7 @@ func TestRequestSchedulerConcurrencyAddsAndReads(t *testing.T) { addWg.Add(1) go func(peer uint16) { defer addWg.Done() - requests.Add(keys[int(peer)%N], peer, nil) + requests.Add(keys[int(peer)%N], peer, nil, 100) }(uint16(i)) } for i := 1; i < N*N; i++ { diff --git a/mempool/cat/store.go b/mempool/cat/store.go index 334a9587d2..92590a277c 100644 --- a/mempool/cat/store.go +++ b/mempool/cat/store.go @@ -12,7 +12,7 @@ type store struct { mtx sync.RWMutex bytes int64 txs map[types.TxKey]*wrappedTx - committedTxs map[types.TxKey]*wrappedTx + committedTxs map[types.TxKey]struct{} reservedTxs map[types.TxKey]struct{} } @@ -44,7 +44,7 @@ func (s *store) get(txKey types.TxKey) *wrappedTx { return s.txs[txKey] } -func (s *store) getCommitted(txKey types.TxKey) *wrappedTx { +func (s *store) getCommitted(txKey types.TxKey) struct{} { s.mtx.RLock() defer s.mtx.RUnlock() return s.committedTxs[txKey] @@ -79,7 +79,7 @@ func (s *store) markAsCommitted(txKeys []types.TxKey) { if tx, exists := s.txs[key]; exists { s.bytes -= tx.size() delete(s.txs, key) - s.committedTxs[key] = tx + s.committedTxs[key] = struct{}{} } } } @@ -87,7 +87,7 @@ func (s *store) markAsCommitted(txKeys []types.TxKey) { func (s *store) clearCommitted() { s.mtx.Lock() defer s.mtx.Unlock() - s.committedTxs = make(map[types.TxKey]*wrappedTx) + s.committedTxs = make(map[types.TxKey]struct{}) } func (s *store) remove(txKey types.TxKey) bool { diff --git a/mempool/cat/wants.go b/mempool/cat/wants.go new file mode 100644 index 0000000000..198acf8c66 --- /dev/null +++ b/mempool/cat/wants.go @@ -0,0 +1,35 @@ +package cat + +import ( + "sync" + + "github.com/tendermint/tendermint/types" +) + +type wantState struct { + mtx *sync.RWMutex + wants map[types.TxKey]map[uint16]struct{} +} + +func NewWantState() *wantState { + return &wantState{ + wants: make(map[types.TxKey]map[uint16]struct{}), + mtx: &sync.RWMutex{}, + } +} + +func (f *wantState) GetWants(tx types.TxKey) (map[uint16]struct{}, bool) { + f.mtx.RLock() + defer f.mtx.RUnlock() + out, has := f.wants[tx] + return out, has +} + +func (f *wantState) Add(tx types.TxKey, peer uint16) { + f.mtx.Lock() + defer f.mtx.Unlock() + if _, exists := f.wants[tx]; !exists { + f.wants[tx] = make(map[uint16]struct{}) + } + f.wants[tx][peer] = struct{}{} +} diff --git a/node/node.go b/node/node.go index 9c2ece5872..dbdaabd059 100644 --- a/node/node.go +++ b/node/node.go @@ -378,6 +378,7 @@ func createMempoolAndMempoolReactor( memplMetrics *mempl.Metrics, logger log.Logger, traceClient trace.Tracer, + self p2p.ID, ) (mempl.Mempool, *cat.Reactor) { mp := cat.NewTxPool( logger, @@ -396,6 +397,7 @@ func createMempoolAndMempoolReactor( MaxTxSize: config.Mempool.MaxTxBytes, TraceClient: traceClient, MaxGossipDelay: config.Mempool.MaxGossipDelay, + Self: self, }, ) if err != nil { @@ -820,7 +822,7 @@ func NewNode(config *cfg.Config, } // Make MempoolReactor - mempool, mempoolReactor := createMempoolAndMempoolReactor(config, proxyApp, state, memplMetrics, logger, tracer) + mempool, mempoolReactor := createMempoolAndMempoolReactor(config, proxyApp, state, memplMetrics, logger, tracer, nodeKey.ID()) // Make Evidence Reactor evidenceReactor, evidencePool, err := createEvidenceReactor(config, dbProvider, stateDB, blockStore, logger) diff --git a/proto/tendermint/mempool/types.pb.go b/proto/tendermint/mempool/types.pb.go index 14d1197ef9..feb8ee2de3 100644 --- a/proto/tendermint/mempool/types.pb.go +++ b/proto/tendermint/mempool/types.pb.go @@ -68,6 +68,7 @@ func (m *Txs) GetTxs() [][]byte { type SeenTx struct { TxKey []byte `protobuf:"bytes,1,opt,name=tx_key,json=txKey,proto3" json:"tx_key,omitempty"` + Peer string `protobuf:"bytes,2,opt,name=peer,proto3" json:"peer,omitempty"` } func (m *SeenTx) Reset() { *m = SeenTx{} } @@ -110,6 +111,13 @@ func (m *SeenTx) GetTxKey() []byte { return nil } +func (m *SeenTx) GetPeer() string { + if m != nil { + return m.Peer + } + return "" +} + type WantTx struct { TxKey []byte `protobuf:"bytes,1,opt,name=tx_key,json=txKey,proto3" json:"tx_key,omitempty"` } @@ -156,7 +164,6 @@ func (m *WantTx) GetTxKey() []byte { type Message struct { // Types that are valid to be assigned to Sum: - // // *Message_Txs // *Message_SeenTx // *Message_WantTx @@ -263,24 +270,25 @@ func init() { func init() { proto.RegisterFile("tendermint/mempool/types.proto", fileDescriptor_2af51926fdbcbc05) } var fileDescriptor_2af51926fdbcbc05 = []byte{ - // 268 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x92, 0x2b, 0x49, 0xcd, 0x4b, - 0x49, 0x2d, 0xca, 0xcd, 0xcc, 0x2b, 0xd1, 0xcf, 0x4d, 0xcd, 0x2d, 0xc8, 0xcf, 0xcf, 0xd1, 0x2f, - 0xa9, 0x2c, 0x48, 0x2d, 0xd6, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x12, 0x42, 0xc8, 0xeb, 0x41, - 0xe5, 0x95, 0xc4, 0xb9, 0x98, 0x43, 0x2a, 0x8a, 0x85, 0x04, 0xb8, 0x98, 0x4b, 0x2a, 0x8a, 0x25, - 0x18, 0x15, 0x98, 0x35, 0x78, 0x82, 0x40, 0x4c, 0x25, 0x79, 0x2e, 0xb6, 0xe0, 0xd4, 0xd4, 0xbc, - 0x90, 0x0a, 0x21, 0x51, 0x2e, 0xb6, 0x92, 0x8a, 0xf8, 0xec, 0xd4, 0x4a, 0x09, 0x46, 0x05, 0x46, - 0x0d, 0x9e, 0x20, 0xd6, 0x92, 0x0a, 0xef, 0xd4, 0x4a, 0x90, 0x82, 0xf0, 0xc4, 0xbc, 0x12, 0xdc, - 0x0a, 0x56, 0x33, 0x72, 0xb1, 0xfb, 0xa6, 0x16, 0x17, 0x27, 0xa6, 0xa7, 0x0a, 0x69, 0xc3, 0xcc, - 0x67, 0xd4, 0xe0, 0x36, 0x12, 0xd7, 0xc3, 0x74, 0x88, 0x5e, 0x48, 0x45, 0xb1, 0x07, 0x03, 0xd8, - 0x6a, 0x21, 0x53, 0x2e, 0xf6, 0xe2, 0xd4, 0xd4, 0xbc, 0xf8, 0x92, 0x0a, 0x09, 0x26, 0xb0, 0x06, - 0x29, 0x6c, 0x1a, 0x20, 0xae, 0xf3, 0x60, 0x08, 0x62, 0x2b, 0x86, 0xb8, 0xd3, 0x94, 0x8b, 0xbd, - 0x3c, 0x31, 0xaf, 0x04, 0xa4, 0x8d, 0x19, 0xb7, 0x36, 0x88, 0x9b, 0x41, 0xda, 0xca, 0xc1, 0x2c, - 0x27, 0x56, 0x2e, 0xe6, 0xe2, 0xd2, 0x5c, 0xa7, 0xe0, 0x13, 0x8f, 0xe4, 0x18, 0x2f, 0x3c, 0x92, - 0x63, 0x7c, 0xf0, 0x48, 0x8e, 0x71, 0xc2, 0x63, 0x39, 0x86, 0x0b, 0x8f, 0xe5, 0x18, 0x6e, 0x3c, - 0x96, 0x63, 0x88, 0xb2, 0x4c, 0xcf, 0x2c, 0xc9, 0x28, 0x4d, 0xd2, 0x4b, 0xce, 0xcf, 0xd5, 0x47, - 0x0a, 0x61, 0x24, 0x26, 0x38, 0x78, 0xf5, 0x31, 0x43, 0x3f, 0x89, 0x0d, 0x2c, 0x63, 0x0c, 0x08, - 0x00, 0x00, 0xff, 0xff, 0x3b, 0xd2, 0x5d, 0x18, 0x9a, 0x01, 0x00, 0x00, + // 286 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x74, 0x91, 0xb1, 0x4a, 0xc3, 0x50, + 0x14, 0x86, 0x73, 0x8d, 0x4d, 0xf1, 0xb4, 0x83, 0x5c, 0x90, 0x16, 0x87, 0x6b, 0xc8, 0x14, 0x10, + 0x12, 0x68, 0xe9, 0xe0, 0xda, 0xa9, 0x20, 0x2e, 0x69, 0x40, 0x70, 0x29, 0xa9, 0x1e, 0x6a, 0xd1, + 0xdc, 0x84, 0xdc, 0x53, 0x7a, 0xf3, 0x16, 0xbe, 0x87, 0x2f, 0xe2, 0xd8, 0xd1, 0x51, 0x92, 0x17, + 0x91, 0xdc, 0x28, 0x16, 0x6a, 0xb7, 0x1f, 0x7e, 0x3e, 0xce, 0xc7, 0x7f, 0x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98, 0xa5, 0xe1, 0xde, 0xea, 0x7b, 0xd1, 0x4c, 0x1e, 0x1e, 0x7e, 0x64, 0xe9, 0x98, 0x66, + 0xfc, 0x1d, 0x00, 0x00, 0xff, 0xff, 0x37, 0x79, 0xee, 0x21, 0xae, 0x01, 0x00, 0x00, } func (m *Txs) Marshal() (dAtA []byte, err error) { @@ -335,6 +343,13 @@ func (m *SeenTx) MarshalToSizedBuffer(dAtA []byte) (int, error) { _ = i var l int _ = l + if len(m.Peer) > 0 { + i -= len(m.Peer) + copy(dAtA[i:], m.Peer) + i = encodeVarintTypes(dAtA, i, uint64(len(m.Peer))) + i-- + dAtA[i] = 0x12 + } if len(m.TxKey) > 0 { i -= len(m.TxKey) copy(dAtA[i:], m.TxKey) @@ -506,6 +521,10 @@ func (m *SeenTx) Size() (n int) { if l > 0 { n += 1 + l + sovTypes(uint64(l)) } + l = len(m.Peer) + if l > 0 { + n += 1 + l + sovTypes(uint64(l)) + } return n } @@ -722,6 +741,38 @@ func (m *SeenTx) Unmarshal(dAtA []byte) error { m.TxKey = []byte{} } iNdEx = postIndex + case 2: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Peer", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTypes + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLengthTypes + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthTypes + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.Peer = string(dAtA[iNdEx:postIndex]) + iNdEx = postIndex default: iNdEx = preIndex skippy, err := skipTypes(dAtA[iNdEx:]) diff --git a/proto/tendermint/mempool/types.proto b/proto/tendermint/mempool/types.proto index 75602411d0..b719ff76f8 100644 --- a/proto/tendermint/mempool/types.proto +++ b/proto/tendermint/mempool/types.proto @@ -8,7 +8,8 @@ message Txs { } message SeenTx { - bytes tx_key = 1; + bytes tx_key = 1; + string peer = 2; } message WantTx { From f36f1b3e9f0d6b8039b26fc9cc8690c33594e025 Mon Sep 17 00:00:00 2001 From: evan-forbes <evan.samuel.forbes@gmail.com> Date: Wed, 25 Sep 2024 01:12:35 -0500 Subject: [PATCH 070/127] fix: only broadcast seentxs the first time we've seen that tx --- mempool/cat/cache.go | 10 ++++++++-- mempool/cat/pool.go | 4 ++-- mempool/cat/reactor.go | 12 ++++++++---- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/mempool/cat/cache.go b/mempool/cat/cache.go index 0029a2746d..39188b9748 100644 --- a/mempool/cat/cache.go +++ b/mempool/cat/cache.go @@ -113,9 +113,9 @@ func NewSeenTxSet() *SeenTxSet { } } -func (s *SeenTxSet) Add(txKey types.TxKey, peer uint16) { +func (s *SeenTxSet) Add(txKey types.TxKey, peer uint16) bool { if peer == 0 { - return + return false } s.mtx.Lock() defer s.mtx.Unlock() @@ -125,8 +125,14 @@ func (s *SeenTxSet) Add(txKey types.TxKey, peer uint16) { peers: map[uint16]struct{}{peer: {}}, time: time.Now().UTC(), } + return true } else { + _, seen := seenSet.peers[peer] + if seen { + return false + } seenSet.peers[peer] = struct{}{} + return true } } diff --git a/mempool/cat/pool.go b/mempool/cat/pool.go index 39220612c4..1eef51d85c 100644 --- a/mempool/cat/pool.go +++ b/mempool/cat/pool.go @@ -423,9 +423,9 @@ func (txmp *TxPool) Flush() { } // PeerHasTx marks that the transaction has been seen by a peer. -func (txmp *TxPool) PeerHasTx(peer uint16, txKey types.TxKey) { +func (txmp *TxPool) PeerHasTx(peer uint16, txKey types.TxKey) bool { // txmp.logger.Debug("peer has tx", "peer", peer, "txKey", fmt.Sprintf("%X", txKey)) - txmp.seenByPeersSet.Add(txKey, peer) + return txmp.seenByPeersSet.Add(txKey, peer) } // allEntriesSorted returns a slice of all the transactions currently in the diff --git a/mempool/cat/reactor.go b/mempool/cat/reactor.go index 8f4e8553b2..25c5e54472 100644 --- a/mempool/cat/reactor.go +++ b/mempool/cat/reactor.go @@ -177,14 +177,14 @@ func (memR *Reactor) GetChannels() []*p2p.ChannelDescriptor { return []*p2p.ChannelDescriptor{ { ID: mempool.MempoolChannel, - Priority: 4, + Priority: 8, SendQueueCapacity: 5000, RecvMessageCapacity: txMsg.Size(), MessageType: &protomem.Message{}, }, { ID: MempoolStateChannel, - Priority: 5, + Priority: 10, SendQueueCapacity: 5000, RecvMessageCapacity: stateMsg.Size(), MessageType: &protomem.Message{}, @@ -358,7 +358,11 @@ func (memR *Reactor) ReceiveEnvelope(e p2p.Envelope) { schema.Download, ) peerID := memR.ids.GetIDForPeer(p2p.ID(msg.Peer)) - memR.mempool.PeerHasTx(peerID, txKey) + success := memR.mempool.PeerHasTx(peerID, txKey) + // return as this peer has already seen this tx + if !success { + return + } // Check if we don't already have the transaction and that it was recently rejected if memR.mempool.Has(txKey) || memR.mempool.IsRejectedTx(txKey) || memR.mempool.store.hasCommitted(txKey) { // memR.Logger.Debug("received a seen tx for a tx we already have", "txKey", txKey) @@ -434,7 +438,7 @@ type PeerState interface { // broadcastSeenTx broadcasts a SeenTx message to all peers unless we // know they have already seen the transaction func (memR *Reactor) broadcastSeenTx(txKey types.TxKey, from string) { - memR.Logger.Debug("broadcasting seen tx to all peers", "tx_key", txKey.String()) + memR.Logger.Info("broadcasting seen tx to all peers", "tx_key", txKey.String(), "from", from) msg := &protomem.Message{ Sum: &protomem.Message_SeenTx{ SeenTx: &protomem.SeenTx{ From 5116182fd77efaad4da4d72dba2a80d113715e1a Mon Sep 17 00:00:00 2001 From: evan-forbes <evan.samuel.forbes@gmail.com> Date: Wed, 25 Sep 2024 02:00:18 -0500 Subject: [PATCH 071/127] fix: prune peer count --- mempool/cat/peers.go | 3 ++- mempool/cat/pool.go | 1 + mempool/cat/reactor.go | 3 +-- p2p/conn/connection.go | 4 +++- p2p/peer.go | 2 ++ 5 files changed, 9 insertions(+), 4 deletions(-) diff --git a/mempool/cat/peers.go b/mempool/cat/peers.go index 71313c9614..e59779d5c8 100644 --- a/mempool/cat/peers.go +++ b/mempool/cat/peers.go @@ -71,6 +71,7 @@ func (ids *mempoolIDs) Reclaim(peerID p2p.ID) uint16 { if ok { delete(ids.activeIDs, removedID) delete(ids.peerMap, peerID) + peerCount.Add(-1) return removedID } return 0 @@ -86,7 +87,7 @@ func (ids *mempoolIDs) GetIDForPeer(peerID p2p.ID) uint16 { id = ids.nextPeerID() ids.peerMap[peerID] = id ids.knownIDs[id] = peerID - peerCount.Add(1) + fmt.Println("known IDs:", len(ids.knownIDs)) } return id } diff --git a/mempool/cat/pool.go b/mempool/cat/pool.go index 1eef51d85c..cc3701d1e5 100644 --- a/mempool/cat/pool.go +++ b/mempool/cat/pool.go @@ -478,6 +478,7 @@ func (txmp *TxPool) ReapMaxBytesMaxGas(maxBytes, maxGas int64) types.Txs { // } if w.seenCount < (2 * (int(peerCount.Load()) / 3)) { + txmp.logger.Error("too few seen to add to block!!", "peerCount", peerCount.Load(), "seen count", w.seenCount) continue } diff --git a/mempool/cat/reactor.go b/mempool/cat/reactor.go index 25c5e54472..776526f45b 100644 --- a/mempool/cat/reactor.go +++ b/mempool/cat/reactor.go @@ -217,7 +217,6 @@ func (memR *Reactor) AddPeer(peer p2p.Peer) { } peer.Send(MempoolStateChannel, bz) } - peerCount.Add(1) } // RemovePeer implements Reactor. For all current outbound requests to this @@ -438,7 +437,7 @@ type PeerState interface { // broadcastSeenTx broadcasts a SeenTx message to all peers unless we // know they have already seen the transaction func (memR *Reactor) broadcastSeenTx(txKey types.TxKey, from string) { - memR.Logger.Info("broadcasting seen tx to all peers", "tx_key", txKey.String(), "from", from) + memR.Logger.Debug("broadcasting seen tx to all peers", "tx_key", txKey.String(), "from", from) msg := &protomem.Message{ Sum: &protomem.Message_SeenTx{ SeenTx: &protomem.SeenTx{ diff --git a/p2p/conn/connection.go b/p2p/conn/connection.go index 87ab2ed28a..aced95e914 100644 --- a/p2p/conn/connection.go +++ b/p2p/conn/connection.go @@ -350,10 +350,11 @@ func (c *MConnection) stopForError(r interface{}) { // Queues a message to be sent to channel. func (c *MConnection) Send(chID byte, msgBytes []byte) bool { if !c.IsRunning() { + c.Logger.Error("mconn is not running") return false } - c.Logger.Debug("Send", "channel", chID, "conn", c, "msgBytes", log.NewLazySprintf("%X", msgBytes)) + // c.Logger.Debug("Send", "channel", chID, "conn", c, "msgBytes", log.NewLazySprintf("%X", msgBytes)) // Send message to channel. channel, ok := c.channelsIdx[chID] @@ -370,6 +371,7 @@ func (c *MConnection) Send(chID byte, msgBytes []byte) bool { default: } } else { + c.Logger.Error("mconn is clogged") c.Logger.Debug("Send failed", "channel", chID, "conn", c, "msgBytes", log.NewLazySprintf("%X", msgBytes)) } return success diff --git a/p2p/peer.go b/p2p/peer.go index f43cff9d51..04a5c2107d 100644 --- a/p2p/peer.go +++ b/p2p/peer.go @@ -356,8 +356,10 @@ func (p *peer) SendEnvelope(e Envelope) bool { // SendEnvelope replaces Send which will be deprecated in a future release. func (p *peer) Send(chID byte, msgBytes []byte) bool { if !p.IsRunning() { + p.Logger.Error("peer is not running") return false } else if !p.hasChannel(chID) { + p.Logger.Error("peer has no channel") return false } res := p.mconn.Send(chID, msgBytes) From 4c7124f40a1b8eef626356e39184fde1b897a9fe Mon Sep 17 00:00:00 2001 From: evan-forbes <evan.samuel.forbes@gmail.com> Date: Wed, 25 Sep 2024 02:06:03 -0500 Subject: [PATCH 072/127] fix: concurrent write --- mempool/cat/peers.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mempool/cat/peers.go b/mempool/cat/peers.go index e59779d5c8..e975ba9d2b 100644 --- a/mempool/cat/peers.go +++ b/mempool/cat/peers.go @@ -79,8 +79,8 @@ func (ids *mempoolIDs) Reclaim(peerID p2p.ID) uint16 { // GetIDForPeer returns the shorthand ID reserved for the peer. func (ids *mempoolIDs) GetIDForPeer(peerID p2p.ID) uint16 { - ids.mtx.RLock() - defer ids.mtx.RUnlock() + ids.mtx.Lock() + defer ids.mtx.Unlock() id, exists := ids.peerMap[peerID] if !exists { From 127c520a3361b62de8228c7ef1067b790186d32f Mon Sep 17 00:00:00 2001 From: evan-forbes <evan.samuel.forbes@gmail.com> Date: Wed, 25 Sep 2024 06:33:59 -0500 Subject: [PATCH 073/127] fix: bunch of other fixes --- mempool/cat/peers.go | 6 +++--- mempool/cat/pool.go | 9 +++++---- mempool/cat/reactor.go | 4 +++- mempool/cat/wants.go | 18 ++++++++++++++++++ p2p/conn/connection.go | 1 - 5 files changed, 29 insertions(+), 9 deletions(-) diff --git a/mempool/cat/peers.go b/mempool/cat/peers.go index e975ba9d2b..2738bdc7f7 100644 --- a/mempool/cat/peers.go +++ b/mempool/cat/peers.go @@ -35,9 +35,9 @@ func (ids *mempoolIDs) ReserveForPeer(peer p2p.Peer) { ids.mtx.Lock() defer ids.mtx.Unlock() - if _, ok := ids.peerMap[peer.ID()]; ok { - panic("duplicate peer added to mempool") - } + // if _, ok := ids.peerMap[peer.ID()]; ok { + // panic("duplicate peer added to mempool") + // } curID := ids.nextPeerID() ids.peerMap[peer.ID()] = curID diff --git a/mempool/cat/pool.go b/mempool/cat/pool.go index cc3701d1e5..ffa0f3bea9 100644 --- a/mempool/cat/pool.go +++ b/mempool/cat/pool.go @@ -338,7 +338,7 @@ func (txmp *TxPool) TryAddNewTx(tx types.Tx, key types.TxKey, txInfo mempool.TxI // reserve the key if !txmp.store.reserve(key) { txmp.logger.Debug("mempool already attempting to verify and add transaction", "txKey", fmt.Sprintf("%X", key)) - txmp.PeerHasTx(txInfo.SenderID, key) + // txmp.PeerHasTx(txInfo.SenderID, key) return nil, ErrTxInMempool } defer txmp.store.release(key) @@ -424,8 +424,9 @@ func (txmp *TxPool) Flush() { // PeerHasTx marks that the transaction has been seen by a peer. func (txmp *TxPool) PeerHasTx(peer uint16, txKey types.TxKey) bool { - // txmp.logger.Debug("peer has tx", "peer", peer, "txKey", fmt.Sprintf("%X", txKey)) - return txmp.seenByPeersSet.Add(txKey, peer) + success := txmp.seenByPeersSet.Add(txKey, peer) + // txmp.logger.Info("peer has tx", "success", success, "peer", peer, "txKey", fmt.Sprintf("%X", txKey)) + return success } // allEntriesSorted returns a slice of all the transactions currently in the @@ -477,7 +478,7 @@ func (txmp *TxPool) ReapMaxBytesMaxGas(maxBytes, maxGas int64) types.Txs { // continue // } - if w.seenCount < (2 * (int(peerCount.Load()) / 3)) { + if w.seenCount < int(2*peerCount.Load()/3) { txmp.logger.Error("too few seen to add to block!!", "peerCount", peerCount.Load(), "seen count", w.seenCount) continue } diff --git a/mempool/cat/reactor.go b/mempool/cat/reactor.go index 776526f45b..ace9abd516 100644 --- a/mempool/cat/reactor.go +++ b/mempool/cat/reactor.go @@ -103,6 +103,7 @@ func NewReactor(mempool *TxPool, opts *ReactorOptions) (*Reactor, error) { traceClient: opts.TraceClient, wantState: NewWantState(), } + memR.self = opts.Self memR.BaseReactor = *p2p.NewBaseReactor("Mempool", memR) return memR, nil } @@ -321,6 +322,7 @@ func (memR *Reactor) ReceiveEnvelope(e p2p.Envelope) { ChannelID: mempool.MempoolChannel, Message: &protomem.Txs{Txs: [][]byte{tx}}, }, memR.Logger) { + memR.wantState.Delete(key, peer) // memR.mempool.PeerHasTx(peerID, txKey) schema.WriteMempoolTx( memR.traceClient, @@ -377,7 +379,7 @@ func (memR *Reactor) ReceiveEnvelope(e p2p.Envelope) { // We don't have the transaction, nor are we requesting it so we send the node // a want msg memR.requestTx(txKey, e.Src, 5) - + memR.Logger.Info("broadcasting seentx from peer", "peer", msg.Peer) memR.broadcastSeenTx(types.TxKey(msg.TxKey), msg.Peer) // A peer is requesting a transaction that we have claimed to have. Find the specified diff --git a/mempool/cat/wants.go b/mempool/cat/wants.go index 198acf8c66..41f76c3391 100644 --- a/mempool/cat/wants.go +++ b/mempool/cat/wants.go @@ -33,3 +33,21 @@ func (f *wantState) Add(tx types.TxKey, peer uint16) { } f.wants[tx][peer] = struct{}{} } + +func (f *wantState) Delete(tx types.TxKey, peer uint16) { + f.mtx.Lock() + defer f.mtx.Unlock() + ws, has := f.wants[tx] + if !has { + return + } + _, has = ws[peer] + if !has { + return + } + delete(ws, peer) + f.wants[tx] = ws + if len(f.wants[tx]) == 0 { + delete(f.wants, tx) + } +} diff --git a/p2p/conn/connection.go b/p2p/conn/connection.go index aced95e914..01b141c0eb 100644 --- a/p2p/conn/connection.go +++ b/p2p/conn/connection.go @@ -371,7 +371,6 @@ func (c *MConnection) Send(chID byte, msgBytes []byte) bool { default: } } else { - c.Logger.Error("mconn is clogged") c.Logger.Debug("Send failed", "channel", chID, "conn", c, "msgBytes", log.NewLazySprintf("%X", msgBytes)) } return success From cc8a88a8b44719d9b0be9241cd729978fd60ce34 Mon Sep 17 00:00:00 2001 From: evan-forbes <evan.samuel.forbes@gmail.com> Date: Wed, 25 Sep 2024 06:35:13 -0500 Subject: [PATCH 074/127] chore: remove log --- mempool/cat/reactor.go | 1 - 1 file changed, 1 deletion(-) diff --git a/mempool/cat/reactor.go b/mempool/cat/reactor.go index ace9abd516..38f5e7e5be 100644 --- a/mempool/cat/reactor.go +++ b/mempool/cat/reactor.go @@ -379,7 +379,6 @@ func (memR *Reactor) ReceiveEnvelope(e p2p.Envelope) { // We don't have the transaction, nor are we requesting it so we send the node // a want msg memR.requestTx(txKey, e.Src, 5) - memR.Logger.Info("broadcasting seentx from peer", "peer", msg.Peer) memR.broadcastSeenTx(types.TxKey(msg.TxKey), msg.Peer) // A peer is requesting a transaction that we have claimed to have. Find the specified From 82da5ed44ccdc24bff6bc48c126a7f6f60479c04 Mon Sep 17 00:00:00 2001 From: evan-forbes <evan.samuel.forbes@gmail.com> Date: Wed, 25 Sep 2024 06:56:39 -0500 Subject: [PATCH 075/127] hack: just hardcode the number of required seen count --- mempool/cat/pool.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mempool/cat/pool.go b/mempool/cat/pool.go index ffa0f3bea9..77c2b22196 100644 --- a/mempool/cat/pool.go +++ b/mempool/cat/pool.go @@ -478,7 +478,7 @@ func (txmp *TxPool) ReapMaxBytesMaxGas(maxBytes, maxGas int64) types.Txs { // continue // } - if w.seenCount < int(2*peerCount.Load()/3) { + if w.seenCount < 60 { txmp.logger.Error("too few seen to add to block!!", "peerCount", peerCount.Load(), "seen count", w.seenCount) continue } From 93f79fc54e4298baff8694b966da405df58703da Mon Sep 17 00:00:00 2001 From: evan-forbes <evan.samuel.forbes@gmail.com> Date: Wed, 25 Sep 2024 07:18:56 -0500 Subject: [PATCH 076/127] chore: manually bump value --- mempool/cat/pool.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mempool/cat/pool.go b/mempool/cat/pool.go index 77c2b22196..f39493d8a0 100644 --- a/mempool/cat/pool.go +++ b/mempool/cat/pool.go @@ -478,7 +478,7 @@ func (txmp *TxPool) ReapMaxBytesMaxGas(maxBytes, maxGas int64) types.Txs { // continue // } - if w.seenCount < 60 { + if w.seenCount < 73 { txmp.logger.Error("too few seen to add to block!!", "peerCount", peerCount.Load(), "seen count", w.seenCount) continue } From e5beebc8f0fd630297fb13106bfc79ab020eb43d Mon Sep 17 00:00:00 2001 From: evan-forbes <evan.samuel.forbes@gmail.com> Date: Wed, 25 Sep 2024 07:20:38 -0500 Subject: [PATCH 077/127] chore: add logs --- mempool/cat/pool.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/mempool/cat/pool.go b/mempool/cat/pool.go index f39493d8a0..457b4c3492 100644 --- a/mempool/cat/pool.go +++ b/mempool/cat/pool.go @@ -481,6 +481,8 @@ func (txmp *TxPool) ReapMaxBytesMaxGas(maxBytes, maxGas int64) types.Txs { if w.seenCount < 73 { txmp.logger.Error("too few seen to add to block!!", "peerCount", peerCount.Load(), "seen count", w.seenCount) continue + } else { + txmp.logger.Info("including tx in block", "seen", w.seenCount) } // N.B. When computing byte size, we need to include the overhead for From 24e29218c7ce13198a001f9d5c96c2dc5c8da942 Mon Sep 17 00:00:00 2001 From: evan-forbes <evan.samuel.forbes@gmail.com> Date: Wed, 25 Sep 2024 07:50:09 -0500 Subject: [PATCH 078/127] chore: change value --- mempool/cat/pool.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mempool/cat/pool.go b/mempool/cat/pool.go index 457b4c3492..b4153bb3fa 100644 --- a/mempool/cat/pool.go +++ b/mempool/cat/pool.go @@ -478,7 +478,7 @@ func (txmp *TxPool) ReapMaxBytesMaxGas(maxBytes, maxGas int64) types.Txs { // continue // } - if w.seenCount < 73 { + if w.seenCount < 25 { txmp.logger.Error("too few seen to add to block!!", "peerCount", peerCount.Load(), "seen count", w.seenCount) continue } else { From ca18d892081c18553873593d11d82dbe74d02bf8 Mon Sep 17 00:00:00 2001 From: evan-forbes <evan.samuel.forbes@gmail.com> Date: Wed, 25 Sep 2024 10:07:05 -0500 Subject: [PATCH 079/127] feat: enable the seen limit to manually change --- mempool/cat/pool.go | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/mempool/cat/pool.go b/mempool/cat/pool.go index b4153bb3fa..f8bacbdd65 100644 --- a/mempool/cat/pool.go +++ b/mempool/cat/pool.go @@ -3,8 +3,10 @@ package cat import ( "errors" "fmt" + "os" "path/filepath" "sort" + "strconv" "sync" "sync/atomic" "time" @@ -36,8 +38,9 @@ const ( var ( // set the default to 5, but this value can be changed in an init func - InclusionDelay = 5 * time.Second - peerCount = atomic.Int32{} + InclusionDelay = 5 * time.Second + peerCount = atomic.Int32{} + defaultSeenLimit = 40 ) // TxPoolOption sets an optional parameter on the TxPool. @@ -471,6 +474,8 @@ func (txmp *TxPool) ReapMaxBytesMaxGas(maxBytes, maxGas int64) types.Txs { var keep []types.Tx //nolint:prealloc + seenLimit := getSeenLimit() + for _, w := range txmp.allEntriesMostSeen() { // skip transactions that have been in the mempool for less than the inclusion delay // This gives time for the transaction to be broadcast to all peers @@ -478,8 +483,8 @@ func (txmp *TxPool) ReapMaxBytesMaxGas(maxBytes, maxGas int64) types.Txs { // continue // } - if w.seenCount < 25 { - txmp.logger.Error("too few seen to add to block!!", "peerCount", peerCount.Load(), "seen count", w.seenCount) + if w.seenCount < seenLimit { + txmp.logger.Error("too few seen to add to block!!", "seen limit", seenLimit, "seen count", w.seenCount) continue } else { txmp.logger.Info("including tx in block", "seen", w.seenCount) @@ -527,6 +532,18 @@ func (txmp *TxPool) ReapMaxTxs(max int) types.Txs { return keep } +func getSeenLimit() int { + evar := os.Getenv("SEEN_LIMIT") + if evar == "" { + return defaultSeenLimit + } + parsed, err := strconv.ParseInt(evar, 10, 64) + if err != nil { + return defaultSeenLimit + } + return int(parsed) +} + // Update removes all the given transactions from the mempool and the cache, // and updates the current block height. The blockTxs and deliverTxResponses // must have the same length with each response corresponding to the tx at the From b00c1d0262e927f2a0f4bcadce607144d9bd5813 Mon Sep 17 00:00:00 2001 From: evan-forbes <evan.samuel.forbes@gmail.com> Date: Wed, 25 Sep 2024 17:54:49 -0500 Subject: [PATCH 080/127] fix: set the proposal even if the peer state appears to be messed up --- consensus/reactor.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/consensus/reactor.go b/consensus/reactor.go index 49f3a9f556..85d0c02316 100644 --- a/consensus/reactor.go +++ b/consensus/reactor.go @@ -1330,9 +1330,9 @@ func (ps *PeerState) SetHasProposal(proposal *types.Proposal) { ps.mtx.Lock() defer ps.mtx.Unlock() - if ps.PRS.Height != proposal.Height || ps.PRS.Round != proposal.Round { - return - } + // if ps.PRS.Height != proposal.Height || ps.PRS.Round != proposal.Round { + // return + // } if ps.PRS.Proposal { return From 257481996032b76f74895d5707bd541bdfb51944 Mon Sep 17 00:00:00 2001 From: evan-forbes <evan.samuel.forbes@gmail.com> Date: Wed, 25 Sep 2024 18:14:51 -0500 Subject: [PATCH 081/127] fix: broadcast async even if we already have that tx --- mempool/cat/pool.go | 2 +- mempool/cat/reactor.go | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/mempool/cat/pool.go b/mempool/cat/pool.go index f8bacbdd65..1e91ef3a61 100644 --- a/mempool/cat/pool.go +++ b/mempool/cat/pool.go @@ -40,7 +40,7 @@ var ( // set the default to 5, but this value can be changed in an init func InclusionDelay = 5 * time.Second peerCount = atomic.Int32{} - defaultSeenLimit = 40 + defaultSeenLimit = 60 ) // TxPoolOption sets an optional parameter on the TxPool. diff --git a/mempool/cat/reactor.go b/mempool/cat/reactor.go index 38f5e7e5be..5f7e1fcce1 100644 --- a/mempool/cat/reactor.go +++ b/mempool/cat/reactor.go @@ -364,6 +364,8 @@ func (memR *Reactor) ReceiveEnvelope(e p2p.Envelope) { if !success { return } + go memR.broadcastSeenTx(types.TxKey(msg.TxKey), msg.Peer) + // Check if we don't already have the transaction and that it was recently rejected if memR.mempool.Has(txKey) || memR.mempool.IsRejectedTx(txKey) || memR.mempool.store.hasCommitted(txKey) { // memR.Logger.Debug("received a seen tx for a tx we already have", "txKey", txKey) @@ -379,7 +381,6 @@ func (memR *Reactor) ReceiveEnvelope(e p2p.Envelope) { // We don't have the transaction, nor are we requesting it so we send the node // a want msg memR.requestTx(txKey, e.Src, 5) - memR.broadcastSeenTx(types.TxKey(msg.TxKey), msg.Peer) // A peer is requesting a transaction that we have claimed to have. Find the specified // transaction and broadcast it to the peer. We may no longer have the transaction @@ -417,7 +418,9 @@ func (memR *Reactor) ReceiveEnvelope(e p2p.Envelope) { ) } } else { + memR.ids.mtx.RLock() peer, has := memR.ids.peerMap[e.Src.ID()] + memR.ids.mtx.RUnlock() if has { memR.wantState.Add(txKey, peer) } From ac1e37ce613aa29e16a58351e5c15270f4bf1d67 Mon Sep 17 00:00:00 2001 From: evan-forbes <evan.samuel.forbes@gmail.com> Date: Wed, 25 Sep 2024 19:42:53 -0500 Subject: [PATCH 082/127] fix: crank queue for mempool state --- mempool/cat/reactor.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/mempool/cat/reactor.go b/mempool/cat/reactor.go index 5f7e1fcce1..7285815ecd 100644 --- a/mempool/cat/reactor.go +++ b/mempool/cat/reactor.go @@ -178,15 +178,15 @@ func (memR *Reactor) GetChannels() []*p2p.ChannelDescriptor { return []*p2p.ChannelDescriptor{ { ID: mempool.MempoolChannel, - Priority: 8, + Priority: 9, SendQueueCapacity: 5000, RecvMessageCapacity: txMsg.Size(), MessageType: &protomem.Message{}, }, { ID: MempoolStateChannel, - Priority: 10, - SendQueueCapacity: 5000, + Priority: 20, + SendQueueCapacity: 33000, RecvMessageCapacity: stateMsg.Size(), MessageType: &protomem.Message{}, }, From f7c12c77c989914f85afc3308d112f30622192e9 Mon Sep 17 00:00:00 2001 From: evan-forbes <evan.samuel.forbes@gmail.com> Date: Sun, 29 Sep 2024 16:11:13 -0500 Subject: [PATCH 083/127] chore: stop redundantly hashing the txs during block creation --- consensus/byzantine_test.go | 6 +++--- consensus/common_test.go | 2 +- consensus/mempool_test.go | 2 +- consensus/replay_stubs.go | 6 ++++-- consensus/replay_test.go | 8 ++++---- consensus/state.go | 18 +++++++++--------- consensus/state_test.go | 2 +- mempool/cat/block_builder_test.go | 2 +- mempool/cat/pool.go | 10 ++++++---- mempool/cat/pool_test.go | 8 ++++---- mempool/mempool.go | 2 +- mempool/mock/mempool.go | 8 +++++--- mempool/v0/clist_mempool.go | 10 ++++++---- mempool/v0/clist_mempool_test.go | 4 ++-- mempool/v1/mempool.go | 8 +++++--- mempool/v1/mempool_test.go | 8 ++++---- node/node_test.go | 4 ++-- state/execution.go | 6 +++--- 18 files changed, 62 insertions(+), 52 deletions(-) diff --git a/consensus/byzantine_test.go b/consensus/byzantine_test.go index 9c13984cff..b0fadeb523 100644 --- a/consensus/byzantine_test.go +++ b/consensus/byzantine_test.go @@ -217,7 +217,7 @@ func TestByzantinePrevoteEquivocation(t *testing.T) { } proposerAddr := lazyProposer.privValidatorPubKey.Address() - block := lazyProposer.blockExec.CreateProposalBlock( + block, _ := lazyProposer.blockExec.CreateProposalBlock( lazyProposer.Height, lazyProposer.state, commit, proposerAddr, ) blockHash := block.Hash() @@ -474,7 +474,7 @@ func byzantineDecideProposalFunc(t *testing.T, height int64, round int32, cs *St // Avoid sending on internalMsgQueue and running consensus state. // Create a new proposal block from state/txs from the mempool. - block1 := cs.createProposalBlock() + block1, _ := cs.createProposalBlock() blockParts1 := block1.MakePartSet(types.BlockPartSizeBytes) polRound := cs.TwoThirdPrevoteRound propBlockID := types.BlockID{Hash: block1.Hash(), PartSetHeader: blockParts1.Header()} @@ -490,7 +490,7 @@ func byzantineDecideProposalFunc(t *testing.T, height int64, round int32, cs *St deliverTxsRange(cs, 0, 1) // Create a new proposal block from state/txs from the mempool. - block2 := cs.createProposalBlock() + block2, _ := cs.createProposalBlock() blockParts2 := block2.MakePartSet(types.BlockPartSizeBytes) polRound = cs.TwoThirdPrevoteRound propBlockID = types.BlockID{Hash: block2.Hash(), PartSetHeader: blockParts2.Header()} diff --git a/consensus/common_test.go b/consensus/common_test.go index 3793693c14..1daf093918 100644 --- a/consensus/common_test.go +++ b/consensus/common_test.go @@ -208,7 +208,7 @@ func decideProposal( round int32, ) (proposal *types.Proposal, block *types.Block) { cs1.mtx.Lock() - block = cs1.createProposalBlock() + block, _ = cs1.createProposalBlock() blockParts := block.MakePartSet(types.BlockPartSizeBytes) validRound := cs1.TwoThirdPrevoteRound diff --git a/consensus/mempool_test.go b/consensus/mempool_test.go index e237aefd94..4b7fa99a53 100644 --- a/consensus/mempool_test.go +++ b/consensus/mempool_test.go @@ -167,7 +167,7 @@ func TestMempoolRmBadTx(t *testing.T) { // check for the tx for { - txs := assertMempool(cs.txNotifier).ReapMaxBytesMaxGas(int64(len(txBytes)), -1) + txs, _ := assertMempool(cs.txNotifier).ReapMaxBytesMaxGas(int64(len(txBytes)), -1) if len(txs) == 0 { emptyMempoolCh <- struct{}{} return diff --git a/consensus/replay_stubs.go b/consensus/replay_stubs.go index d4f438c6ca..3fb15af350 100644 --- a/consensus/replay_stubs.go +++ b/consensus/replay_stubs.go @@ -27,8 +27,10 @@ func (txmp emptyMempool) RemoveTxByKey(txKey types.TxKey) error { return nil } -func (emptyMempool) ReapMaxBytesMaxGas(_, _ int64) types.Txs { return types.Txs{} } -func (emptyMempool) ReapMaxTxs(n int) types.Txs { return types.Txs{} } +func (emptyMempool) ReapMaxBytesMaxGas(_, _ int64) (types.Txs, []types.TxKey) { + return types.Txs{}, []types.TxKey{} +} +func (emptyMempool) ReapMaxTxs(n int) types.Txs { return types.Txs{} } func (emptyMempool) Update( _ int64, _ types.Txs, diff --git a/consensus/replay_test.go b/consensus/replay_test.go index cf4f4acd83..56602cb920 100644 --- a/consensus/replay_test.go +++ b/consensus/replay_test.go @@ -375,7 +375,7 @@ func TestSimulateValidatorsChange(t *testing.T) { newValidatorTx1 := kvstore.MakeValSetChangeTx(valPubKey1ABCI, testMinPower) err = assertMempool(css[0].txNotifier).CheckTx(newValidatorTx1, nil, mempl.TxInfo{}) assert.Nil(t, err) - propBlock := css[0].createProposalBlock() // changeProposer(t, cs1, vs2) + propBlock, _ := css[0].createProposalBlock() // changeProposer(t, cs1, vs2) propBlockParts := propBlock.MakePartSet(partSize) blockID := types.BlockID{Hash: propBlock.Hash(), PartSetHeader: propBlockParts.Header()} @@ -405,7 +405,7 @@ func TestSimulateValidatorsChange(t *testing.T) { updateValidatorTx1 := kvstore.MakeValSetChangeTx(updatePubKey1ABCI, 25) err = assertMempool(css[0].txNotifier).CheckTx(updateValidatorTx1, nil, mempl.TxInfo{}) assert.Nil(t, err) - propBlock = css[0].createProposalBlock() // changeProposer(t, cs1, vs2) + propBlock, _ = css[0].createProposalBlock() // changeProposer(t, cs1, vs2) propBlockParts = propBlock.MakePartSet(partSize) blockID = types.BlockID{Hash: propBlock.Hash(), PartSetHeader: propBlockParts.Header()} @@ -442,7 +442,7 @@ func TestSimulateValidatorsChange(t *testing.T) { newValidatorTx3 := kvstore.MakeValSetChangeTx(newVal3ABCI, testMinPower) err = assertMempool(css[0].txNotifier).CheckTx(newValidatorTx3, nil, mempl.TxInfo{}) assert.Nil(t, err) - propBlock = css[0].createProposalBlock() // changeProposer(t, cs1, vs2) + propBlock, _ = css[0].createProposalBlock() // changeProposer(t, cs1, vs2) propBlockParts = propBlock.MakePartSet(partSize) blockID = types.BlockID{Hash: propBlock.Hash(), PartSetHeader: propBlockParts.Header()} newVss := make([]*validatorStub, nVals+1) @@ -517,7 +517,7 @@ func TestSimulateValidatorsChange(t *testing.T) { removeValidatorTx3 := kvstore.MakeValSetChangeTx(newVal3ABCI, 0) err = assertMempool(css[0].txNotifier).CheckTx(removeValidatorTx3, nil, mempl.TxInfo{}) assert.Nil(t, err) - propBlock = css[0].createProposalBlock() // changeProposer(t, cs1, vs2) + propBlock, _ = css[0].createProposalBlock() // changeProposer(t, cs1, vs2) propBlockParts = propBlock.MakePartSet(partSize) blockID = types.BlockID{Hash: propBlock.Hash(), PartSetHeader: propBlockParts.Header()} newVss = make([]*validatorStub, nVals+3) diff --git a/consensus/state.go b/consensus/state.go index e93bc2b97e..94a1d5e3d5 100644 --- a/consensus/state.go +++ b/consensus/state.go @@ -1205,6 +1205,7 @@ func (cs *State) defaultDecideProposal(height int64, round int32) { var block *types.Block var blockParts *types.PartSet var blockHash []byte + var hashes []types.TxKey // Decide on block if cs.TwoThirdPrevoteBlock != nil { @@ -1219,7 +1220,7 @@ func (cs *State) defaultDecideProposal(height int64, round int32) { } else { // Create a new proposal block from state/txs from the mempool. schema.WriteABCI(cs.traceClient, schema.PrepareProposalStart, height, round) - block = cs.createProposalBlock() + block, hashes = cs.createProposalBlock() schema.WriteABCI(cs.traceClient, schema.PrepareProposalEnd, height, round) if block == nil { return @@ -1228,12 +1229,11 @@ func (cs *State) defaultDecideProposal(height int64, round int32) { blockHash = block.Hash() } - keys, err := cs.txFetcher.FetchKeysFromTxs(context.Background(), block.Txs.ToSliceOfBytes()) - if err != nil { - cs.Logger.Error("failed to fetch tx keys", "err", err) - return + keyTxs := make([]types.Tx, len(hashes)) + for i, h := range hashes { + keyTxs[i] = types.Tx(h[:]) } - block.Data.Txs = types.ToTxs(keys) + block.Data.Txs = keyTxs // Flush the WAL. Otherwise, we may not recompute the same proposal to sign, // and the privValidator will refuse to sign anything. @@ -1281,7 +1281,7 @@ func (cs *State) isProposalComplete() bool { // // NOTE: keep it side-effect free for clarity. // CONTRACT: cs.privValidator is not nil. -func (cs *State) createProposalBlock() *types.Block { +func (cs *State) createProposalBlock() (*types.Block, []types.TxKey) { if cs.privValidator == nil { panic("entered createProposalBlock with privValidator being nil") } @@ -1299,14 +1299,14 @@ func (cs *State) createProposalBlock() *types.Block { default: // This shouldn't happen. cs.Logger.Error("propose step; cannot propose anything without commit for the previous block") - return nil + return nil, nil } if cs.privValidatorPubKey == nil { // If this node is a validator & proposer in the current round, it will // miss the opportunity to create a block. cs.Logger.Error("propose step; empty priv validator public key", "err", errPubKeyIsNotSet) - return nil + return nil, nil } proposerAddr := cs.privValidatorPubKey.Address() diff --git a/consensus/state_test.go b/consensus/state_test.go index 6eff1f2e85..aaad77f1bd 100644 --- a/consensus/state_test.go +++ b/consensus/state_test.go @@ -192,7 +192,7 @@ func TestStateBadProposal(t *testing.T) { proposalCh := subscribe(cs1.eventBus, types.EventQueryCompleteProposal) voteCh := subscribe(cs1.eventBus, types.EventQueryVote) - propBlock := cs1.createProposalBlock() // changeProposer(t, cs1, vs2) + propBlock, _ := cs1.createProposalBlock() // changeProposer(t, cs1, vs2) // make the second validator the proposer by incrementing round round++ diff --git a/mempool/cat/block_builder_test.go b/mempool/cat/block_builder_test.go index a1b9b3ef50..291238cb88 100644 --- a/mempool/cat/block_builder_test.go +++ b/mempool/cat/block_builder_test.go @@ -186,7 +186,7 @@ func TestFetchTxsFromKeys(t *testing.T) { defer cancel() reactor, pool := setupReactor(t) - numTxs := 10 + numTxs := 100 txs := make([][]byte, numTxs) keys := make([][]byte, numTxs) peer := genPeer() diff --git a/mempool/cat/pool.go b/mempool/cat/pool.go index 1e91ef3a61..20de8075fd 100644 --- a/mempool/cat/pool.go +++ b/mempool/cat/pool.go @@ -40,7 +40,7 @@ var ( // set the default to 5, but this value can be changed in an init func InclusionDelay = 5 * time.Second peerCount = atomic.Int32{} - defaultSeenLimit = 60 + defaultSeenLimit = 72 ) // TxPoolOption sets an optional parameter on the TxPool. @@ -468,11 +468,12 @@ func (txmp *TxPool) allEntriesMostSeen() []*wrappedTx { // // If the mempool is empty or has no transactions fitting within the given // constraints, the result will also be empty. -func (txmp *TxPool) ReapMaxBytesMaxGas(maxBytes, maxGas int64) types.Txs { +func (txmp *TxPool) ReapMaxBytesMaxGas(maxBytes, maxGas int64) (types.Txs, []types.TxKey) { var totalGas, totalBytes int64 // currentTime := time.Now() - var keep []types.Tx //nolint:prealloc + var keep []types.Tx //nolint:prealloc + var keys []types.TxKey //nolint:prealloc seenLimit := getSeenLimit() @@ -501,8 +502,9 @@ func (txmp *TxPool) ReapMaxBytesMaxGas(maxBytes, maxGas int64) types.Txs { totalGas += w.gasWanted txmp.store.markAsUnevictable(w.key) keep = append(keep, w.tx) + keys = append(keys, w.key) } - return keep + return keep, keys } // ReapMaxTxs returns up to max transactions from the mempool. The results are diff --git a/mempool/cat/pool_test.go b/mempool/cat/pool_test.go index 2c7ee4a8fc..81e28fbc06 100644 --- a/mempool/cat/pool_test.go +++ b/mempool/cat/pool_test.go @@ -364,14 +364,14 @@ func TestTxPool_ReapMaxBytesMaxGas(t *testing.T) { } // reap by gas capacity only - reapedTxs := txmp.ReapMaxBytesMaxGas(-1, 50) + reapedTxs, _ := txmp.ReapMaxBytesMaxGas(-1, 50) ensurePrioritized(reapedTxs) require.Equal(t, len(tTxs), txmp.Size()) require.Equal(t, int64(5800), txmp.SizeBytes()) require.Len(t, reapedTxs, 50) // reap by transaction bytes only - reapedTxs = txmp.ReapMaxBytesMaxGas(1200, -1) + reapedTxs, _ = txmp.ReapMaxBytesMaxGas(1200, -1) ensurePrioritized(reapedTxs) require.Equal(t, len(tTxs), txmp.Size()) require.Equal(t, int64(5800), txmp.SizeBytes()) @@ -380,7 +380,7 @@ func TestTxPool_ReapMaxBytesMaxGas(t *testing.T) { // Reap by both transaction bytes and gas, where the size yields 31 reaped // transactions and the gas limit reaps 25 transactions. - reapedTxs = txmp.ReapMaxBytesMaxGas(2000, 25) + reapedTxs, _ = txmp.ReapMaxBytesMaxGas(2000, 25) ensurePrioritized(reapedTxs) require.Equal(t, len(tTxs), txmp.Size()) require.Equal(t, int64(5800), txmp.SizeBytes()) @@ -404,7 +404,7 @@ func TestTxMempoolTxLargerThanMaxBytes(t *testing.T) { require.NoError(t, txmp.CheckTx(smallTx, nil, mempool.TxInfo{SenderID: 1})) // reap by max bytes less than the large tx - reapedTxs := txmp.ReapMaxBytesMaxGas(100, -1) + reapedTxs, _ := txmp.ReapMaxBytesMaxGas(100, -1) require.Len(t, reapedTxs, 1) require.Equal(t, types.Tx(smallTx), reapedTxs[0]) } diff --git a/mempool/mempool.go b/mempool/mempool.go index cbbf1e2d4b..c8d14716cd 100644 --- a/mempool/mempool.go +++ b/mempool/mempool.go @@ -42,7 +42,7 @@ type Mempool interface { // // If both maxes are negative, there is no cap on the size of all returned // transactions (~ all available transactions). - ReapMaxBytesMaxGas(maxBytes, maxGas int64) types.Txs + ReapMaxBytesMaxGas(maxBytes, maxGas int64) (types.Txs, []types.TxKey) // ReapMaxTxs reaps up to max transactions from the mempool. If max is // negative, there is no cap on the size of all returned transactions diff --git a/mempool/mock/mempool.go b/mempool/mock/mempool.go index 7084712912..e0db61a48d 100644 --- a/mempool/mock/mempool.go +++ b/mempool/mock/mempool.go @@ -18,9 +18,11 @@ func (Mempool) Size() int { return 0 } func (Mempool) CheckTx(_ types.Tx, _ func(*abci.Response), _ mempool.TxInfo) error { return nil } -func (Mempool) RemoveTxByKey(txKey types.TxKey) error { return nil } -func (Mempool) ReapMaxBytesMaxGas(_, _ int64) types.Txs { return types.Txs{} } -func (Mempool) ReapMaxTxs(n int) types.Txs { return types.Txs{} } +func (Mempool) RemoveTxByKey(txKey types.TxKey) error { return nil } +func (Mempool) ReapMaxBytesMaxGas(_, _ int64) (types.Txs, []types.TxKey) { + return types.Txs{}, []types.TxKey{} +} +func (Mempool) ReapMaxTxs(n int) types.Txs { return types.Txs{} } func (Mempool) Update( _ int64, _ types.Txs, diff --git a/mempool/v0/clist_mempool.go b/mempool/v0/clist_mempool.go index 8d8b637303..8e3720195b 100644 --- a/mempool/v0/clist_mempool.go +++ b/mempool/v0/clist_mempool.go @@ -553,7 +553,7 @@ func (mem *CListMempool) notifyTxsAvailable() { } // Safe for concurrent use by multiple goroutines. -func (mem *CListMempool) ReapMaxBytesMaxGas(maxBytes, maxGas int64) types.Txs { +func (mem *CListMempool) ReapMaxBytesMaxGas(maxBytes, maxGas int64) (types.Txs, []types.TxKey) { mem.updateMtx.RLock() defer mem.updateMtx.RUnlock() @@ -566,16 +566,18 @@ func (mem *CListMempool) ReapMaxBytesMaxGas(maxBytes, maxGas int64) types.Txs { // size per tx, and set the initial capacity based off of that. // txs := make([]types.Tx, 0, cmtmath.MinInt(mem.txs.Len(), max/mem.avgTxSize)) txs := make([]types.Tx, 0, mem.txs.Len()) + keys := make([]types.TxKey, 0, mem.txs.Len()) for e := mem.txs.Front(); e != nil; e = e.Next() { memTx := e.Value.(*mempoolTx) txs = append(txs, memTx.tx) + keys = append(keys, memTx.tx.Key()) dataSize := types.ComputeProtoSizeForTxs([]types.Tx{memTx.tx}) // Check total size requirement if maxBytes > -1 && runningSize+dataSize > maxBytes { - return txs[:len(txs)-1] + return txs[:len(txs)-1], keys } runningSize += dataSize @@ -586,11 +588,11 @@ func (mem *CListMempool) ReapMaxBytesMaxGas(maxBytes, maxGas int64) types.Txs { // must be non-negative, it follows that this won't overflow. newTotalGas := totalGas + memTx.gasWanted if maxGas > -1 && newTotalGas > maxGas { - return txs[:len(txs)-1] + return txs[:len(txs)-1], keys[:len(txs)-1] } totalGas = newTotalGas } - return txs + return txs, keys } // Safe for concurrent use by multiple goroutines. diff --git a/mempool/v0/clist_mempool_test.go b/mempool/v0/clist_mempool_test.go index fd15ea659b..a5c13bb114 100644 --- a/mempool/v0/clist_mempool_test.go +++ b/mempool/v0/clist_mempool_test.go @@ -164,7 +164,7 @@ func TestReapMaxBytesMaxGas(t *testing.T) { } for tcIndex, tt := range tests { checkTxs(t, mp, tt.numTxsToCreate, mempool.UnknownPeerID) - got := mp.ReapMaxBytesMaxGas(tt.maxBytes, tt.maxGas) + got, _ := mp.ReapMaxBytesMaxGas(tt.maxBytes, tt.maxGas) assert.Equal(t, tt.expectedNumTxs, len(got), "Got %d txs, expected %d, tc #%d", len(got), tt.expectedNumTxs, tcIndex) mp.Flush() @@ -430,7 +430,7 @@ func TestSerialReap(t *testing.T) { } reapCheck := func(exp int) { - txs := mp.ReapMaxBytesMaxGas(-1, -1) + txs, _ := mp.ReapMaxBytesMaxGas(-1, -1) require.Equal(t, len(txs), exp, fmt.Sprintf("Expected to reap %v txs but got %v", exp, len(txs))) } diff --git a/mempool/v1/mempool.go b/mempool/v1/mempool.go index 7cfd60f336..184b1ec1dd 100644 --- a/mempool/v1/mempool.go +++ b/mempool/v1/mempool.go @@ -354,10 +354,11 @@ func (txmp *TxMempool) allEntriesSorted() []*WrappedTx { // // If the mempool is empty or has no transactions fitting within the given // constraints, the result will also be empty. -func (txmp *TxMempool) ReapMaxBytesMaxGas(maxBytes, maxGas int64) types.Txs { +func (txmp *TxMempool) ReapMaxBytesMaxGas(maxBytes, maxGas int64) (types.Txs, []types.TxKey) { var totalGas, totalBytes int64 - var keep []types.Tx //nolint:prealloc + var keep []types.Tx //nolint:prealloc + var keys []types.TxKey //nolint:prealloc for _, w := range txmp.allEntriesSorted() { // N.B. When computing byte size, we need to include the overhead for // encoding as protobuf to send to the application. This actually overestimates it @@ -369,8 +370,9 @@ func (txmp *TxMempool) ReapMaxBytesMaxGas(maxBytes, maxGas int64) types.Txs { totalBytes += txBytes totalGas += w.gasWanted keep = append(keep, w.tx) + keys = append(keys, w.hash) } - return keep + return keep, keys } // TxsWaitChan returns a channel that is closed when there is at least one diff --git a/mempool/v1/mempool_test.go b/mempool/v1/mempool_test.go index 8db9a3c3a6..25246d85ea 100644 --- a/mempool/v1/mempool_test.go +++ b/mempool/v1/mempool_test.go @@ -347,14 +347,14 @@ func TestTxMempool_ReapMaxBytesMaxGas(t *testing.T) { } // reap by gas capacity only - reapedTxs := txmp.ReapMaxBytesMaxGas(-1, 50) + reapedTxs, _ := txmp.ReapMaxBytesMaxGas(-1, 50) ensurePrioritized(reapedTxs) require.Equal(t, len(tTxs), txmp.Size()) require.Equal(t, totalSizeBytes, txmp.SizeBytes()) require.Len(t, reapedTxs, 50) // reap by transaction bytes only - reapedTxs = txmp.ReapMaxBytesMaxGas(1000, -1) + reapedTxs, _ = txmp.ReapMaxBytesMaxGas(1000, -1) ensurePrioritized(reapedTxs) require.Equal(t, len(tTxs), txmp.Size()) require.Equal(t, totalSizeBytes, txmp.SizeBytes()) @@ -362,7 +362,7 @@ func TestTxMempool_ReapMaxBytesMaxGas(t *testing.T) { // Reap by both transaction bytes and gas, where the size yields 31 reaped // transactions and the gas limit reaps 25 transactions. - reapedTxs = txmp.ReapMaxBytesMaxGas(1500, 30) + reapedTxs, _ = txmp.ReapMaxBytesMaxGas(1500, 30) ensurePrioritized(reapedTxs) require.Equal(t, len(tTxs), txmp.Size()) require.Equal(t, totalSizeBytes, txmp.SizeBytes()) @@ -386,7 +386,7 @@ func TestTxMempoolTxLargerThanMaxBytes(t *testing.T) { require.NoError(t, txmp.CheckTx(smallTx, nil, mempool.TxInfo{SenderID: 1})) // reap by max bytes less than the large tx - reapedTxs := txmp.ReapMaxBytesMaxGas(100, -1) + reapedTxs, _ := txmp.ReapMaxBytesMaxGas(100, -1) require.Len(t, reapedTxs, 1) require.Equal(t, types.Tx(smallTx), reapedTxs[0]) } diff --git a/node/node_test.go b/node/node_test.go index 55a178babf..db486b80c1 100644 --- a/node/node_test.go +++ b/node/node_test.go @@ -318,7 +318,7 @@ func TestCreateProposalBlock(t *testing.T) { ) commit := types.NewCommit(height-1, 0, types.BlockID{}, nil) - block := blockExec.CreateProposalBlock( + block, _ := blockExec.CreateProposalBlock( height, state, commit, proposerAddr, @@ -408,7 +408,7 @@ func TestMaxProposalBlockSize(t *testing.T) { ) commit := types.NewCommit(height-1, 0, types.BlockID{}, nil) - block := blockExec.CreateProposalBlock( + block, _ := blockExec.CreateProposalBlock( height, state, commit, proposerAddr, diff --git a/state/execution.go b/state/execution.go index c8f04483c1..6b6117f72e 100644 --- a/state/execution.go +++ b/state/execution.go @@ -107,7 +107,7 @@ func (blockExec *BlockExecutor) CreateProposalBlock( height int64, state State, commit *types.Commit, proposerAddr []byte, -) *types.Block { +) (*types.Block, []types.TxKey) { maxBytes := state.ConsensusParams.Block.MaxBytes maxGas := state.ConsensusParams.Block.MaxGas @@ -116,7 +116,7 @@ func (blockExec *BlockExecutor) CreateProposalBlock( maxDataBytes := types.MaxDataBytes(maxBytes, evSize, state.Validators.Size()) - txs := blockExec.mempool.ReapMaxBytesMaxGas(maxDataBytes, maxGas) + txs, hashes := blockExec.mempool.ReapMaxBytesMaxGas(maxDataBytes, maxGas) var timestamp time.Time if height == state.InitialHeight { @@ -168,7 +168,7 @@ func (blockExec *BlockExecutor) CreateProposalBlock( evidence, proposerAddr, ) - return block + return block, hashes } func (blockExec *BlockExecutor) ProcessProposal( From d50d579c8f781f936ec02b63aa05bbbe3839602d Mon Sep 17 00:00:00 2001 From: evan-forbes <evan.samuel.forbes@gmail.com> Date: Sun, 29 Sep 2024 16:42:39 -0500 Subject: [PATCH 084/127] fix: add the data root in comet instead of the app --- state/execution.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/state/execution.go b/state/execution.go index 6b6117f72e..024f2bb569 100644 --- a/state/execution.go +++ b/state/execution.go @@ -7,6 +7,7 @@ import ( abci "github.com/tendermint/tendermint/abci/types" cryptoenc "github.com/tendermint/tendermint/crypto/encoding" + "github.com/tendermint/tendermint/crypto/merkle" "github.com/tendermint/tendermint/libs/fail" "github.com/tendermint/tendermint/libs/log" mempl "github.com/tendermint/tendermint/mempool" @@ -155,6 +156,13 @@ func (blockExec *BlockExecutor) CreateProposalBlock( } } + hashBytes := make([][]byte, len(hashes)) + for i, h := range hashes { + hashBytes[i] = h[:] + } + + rawNewData.Hash = merkle.HashFromByteSlices(hashBytes) + newData, err := types.DataFromProto(rawNewData) if err != nil { // todo(evan): see if we can get rid of this panic From 46b3c3a34cd5f1cfe3a46c24fed1a497d86d3588 Mon Sep 17 00:00:00 2001 From: evan-forbes <evan.samuel.forbes@gmail.com> Date: Sun, 29 Sep 2024 18:36:25 -0500 Subject: [PATCH 085/127] hack: try to only send block parts for catchup if they're really behind --- consensus/reactor.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/consensus/reactor.go b/consensus/reactor.go index 85d0c02316..782ecfc5fc 100644 --- a/consensus/reactor.go +++ b/consensus/reactor.go @@ -798,7 +798,7 @@ OUTER_LOOP: schema.Upload, ) } - case prs.Height < rs.Height-1: // catchup + case prs.Height < rs.Height-2: // catchup blockStoreBase := conR.conS.blockStore.Base() if blockStoreBase > 0 && prs.Height >= blockStoreBase { heightLogger := logger.With("height", prs.Height) From 59d12d9252da441457146fba8c9f74799d5e14f1 Mon Sep 17 00:00:00 2001 From: evan-forbes <evan.samuel.forbes@gmail.com> Date: Mon, 30 Sep 2024 00:01:55 -0500 Subject: [PATCH 086/127] chore: optimize sorting seen txs from the mempool --- mempool/cat/pool.go | 25 ++++--------- mempool/cat/store.go | 15 ++++++++ state/execution.go | 89 +++++++++++++++++++++++--------------------- types/block.go | 4 ++ 4 files changed, 73 insertions(+), 60 deletions(-) diff --git a/mempool/cat/pool.go b/mempool/cat/pool.go index 20de8075fd..c5f67d5e44 100644 --- a/mempool/cat/pool.go +++ b/mempool/cat/pool.go @@ -446,13 +446,9 @@ func (txmp *TxPool) allEntriesSorted() []*wrappedTx { return txs } -func (txmp *TxPool) allEntriesMostSeen() []*wrappedTx { - txs := txmp.allEntriesSorted() - for i, tx := range txs { - seen := txmp.seenByPeersSet.GetSeenCount(tx.key) - txs[i].seenCount = seen - } - sort.SliceStable(txs, func(i, j int) bool { +func (txmp *TxPool) seenEntries(seenLimit int) []*wrappedTx { + txs := txmp.store.getAllSeenTxs(seenLimit) + sort.Slice(txs, func(i, j int) bool { return txs[i].seenCount > txs[j].seenCount }) return txs @@ -477,30 +473,23 @@ func (txmp *TxPool) ReapMaxBytesMaxGas(maxBytes, maxGas int64) (types.Txs, []typ seenLimit := getSeenLimit() - for _, w := range txmp.allEntriesMostSeen() { + for _, w := range txmp.seenEntries(seenLimit) { // skip transactions that have been in the mempool for less than the inclusion delay // This gives time for the transaction to be broadcast to all peers // if currentTime.Sub(w.timestamp) < InclusionDelay { // continue // } - if w.seenCount < seenLimit { - txmp.logger.Error("too few seen to add to block!!", "seen limit", seenLimit, "seen count", w.seenCount) - continue - } else { - txmp.logger.Info("including tx in block", "seen", w.seenCount) - } - // N.B. When computing byte size, we need to include the overhead for // encoding as protobuf to send to the application. This actually overestimates it // as we add the proto overhead to each transaction - txBytes := types.ComputeProtoSizeForTxs([]types.Tx{w.tx}) + txBytes := int64(len(w.tx) + 16) if (maxGas >= 0 && totalGas+w.gasWanted > maxGas) || (maxBytes >= 0 && totalBytes+txBytes > maxBytes) { continue } - totalBytes += txBytes + totalBytes += int64(txBytes) totalGas += w.gasWanted - txmp.store.markAsUnevictable(w.key) + // txmp.store.markAsUnevictable(w.key) keep = append(keep, w.tx) keys = append(keys, w.key) } diff --git a/mempool/cat/store.go b/mempool/cat/store.go index 92590a277c..1b47345c79 100644 --- a/mempool/cat/store.go +++ b/mempool/cat/store.go @@ -174,6 +174,21 @@ func (s *store) getAllTxs() []*wrappedTx { return txs } +func (s *store) getAllSeenTxs(seenLimit int) []*wrappedTx { + s.mtx.RLock() + defer s.mtx.RUnlock() + txs := make([]*wrappedTx, len(s.txs)) + idx := 0 + for _, tx := range s.txs { + if tx.seenCount < seenLimit { + continue + } + txs[idx] = tx + idx++ + } + return txs +} + func (s *store) getEvictableTxsBelowPriority(priority int64) ([]*wrappedTx, int64) { s.mtx.RLock() defer s.mtx.RUnlock() diff --git a/state/execution.go b/state/execution.go index 024f2bb569..c42be9c43b 100644 --- a/state/execution.go +++ b/state/execution.go @@ -119,59 +119,64 @@ func (blockExec *BlockExecutor) CreateProposalBlock( txs, hashes := blockExec.mempool.ReapMaxBytesMaxGas(maxDataBytes, maxGas) - var timestamp time.Time - if height == state.InitialHeight { - timestamp = state.LastBlockTime // genesis time - } else { - timestamp = MedianTime(commit, state.LastValidators) - } - - preparedProposal, err := blockExec.proxyApp.PrepareProposalSync( - abci.RequestPrepareProposal{ - BlockData: &cmtproto.Data{Txs: txs.ToSliceOfBytes()}, - BlockDataSize: maxDataBytes, - ChainId: state.ChainID, - Height: height, - Time: timestamp, - }, - ) - if err != nil { - // The App MUST ensure that only valid (and hence 'processable') transactions - // enter the mempool. Hence, at this point, we can't have any non-processable - // transaction causing an error. - // - // Also, the App can simply skip any transaction that could cause any kind of trouble. - // Either way, we can not recover in a meaningful way, unless we skip proposing - // this block, repair what caused the error and try again. Hence, we panic on - // purpose for now. - panic(err) - } - rawNewData := preparedProposal.GetBlockData() - var blockDataSize int - for _, tx := range rawNewData.GetTxs() { - blockDataSize += len(tx) - - if maxDataBytes < int64(blockDataSize) { - panic("block data exceeds max amount of allowed bytes") - } - } + // var timestamp time.Time + // if height == state.InitialHeight { + // timestamp = state.LastBlockTime // genesis time + // } else { + // timestamp = MedianTime(commit, state.LastValidators) + // } + + // preparedProposal, err := blockExec.proxyApp.PrepareProposalSync( + // abci.RequestPrepareProposal{ + // BlockData: &cmtproto.Data{Txs: txs.ToSliceOfBytes()}, + // BlockDataSize: maxDataBytes, + // ChainId: state.ChainID, + // Height: height, + // Time: timestamp, + // }, + // ) + // if err != nil { + // // The App MUST ensure that only valid (and hence 'processable') transactions + // // enter the mempool. Hence, at this point, we can't have any non-processable + // // transaction causing an error. + // // + // // Also, the App can simply skip any transaction that could cause any kind of trouble. + // // Either way, we can not recover in a meaningful way, unless we skip proposing + // // this block, repair what caused the error and try again. Hence, we panic on + // // purpose for now. + // panic(err) + // } + // rawNewData := preparedProposal.GetBlockData() + // var blockDataSize int + // for _, tx := range rawNewData.GetTxs() { + // blockDataSize += len(tx) + + // if maxDataBytes < int64(blockDataSize) { + // panic("block data exceeds max amount of allowed bytes") + // } + // } hashBytes := make([][]byte, len(hashes)) for i, h := range hashes { hashBytes[i] = h[:] } - rawNewData.Hash = merkle.HashFromByteSlices(hashBytes) + // rawNewData.Hash = merkle.HashFromByteSlices(hashBytes) - newData, err := types.DataFromProto(rawNewData) - if err != nil { - // todo(evan): see if we can get rid of this panic - panic(err) + // newData, err := types.DataFromProto(rawNewData) + // if err != nil { + // // todo(evan): see if we can get rid of this panic + // panic(err) + // } + + data := types.Data{ + Txs: txs, } + data.SetHash(merkle.HashFromByteSlices(hashBytes)) block, _ := state.MakeBlock( height, - newData, + data, commit, evidence, proposerAddr, diff --git a/types/block.go b/types/block.go index 8c235ff0a0..39c1b89c9b 100644 --- a/types/block.go +++ b/types/block.go @@ -1038,6 +1038,10 @@ type Data struct { hash cmtbytes.HexBytes } +func (data *Data) SetHash(h []byte) { + data.hash = cmtbytes.HexBytes(h) +} + // Hash returns the hash of the data func (data *Data) Hash() cmtbytes.HexBytes { if data == nil { From 0739a85bcf1f239ca41a740ee054c6e32d7241f9 Mon Sep 17 00:00:00 2001 From: evan-forbes <evan.samuel.forbes@gmail.com> Date: Mon, 30 Sep 2024 00:26:10 -0500 Subject: [PATCH 087/127] chore: revert catchup hack --- consensus/reactor.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/consensus/reactor.go b/consensus/reactor.go index 782ecfc5fc..8d6dfb3fb4 100644 --- a/consensus/reactor.go +++ b/consensus/reactor.go @@ -153,7 +153,7 @@ func (conR *Reactor) GetChannels() []*p2p.ChannelDescriptor { return []*p2p.ChannelDescriptor{ { ID: StateChannel, - Priority: 5, + Priority: 20, SendQueueCapacity: 100, RecvMessageCapacity: maxMsgSize, MessageType: &cmtcons.Message{}, @@ -161,7 +161,7 @@ func (conR *Reactor) GetChannels() []*p2p.ChannelDescriptor { { ID: DataChannel, // maybe split between gossiping current block and catchup stuff // once we gossip the whole block there's nothing left to send until next height or round - Priority: 10, + Priority: 20, SendQueueCapacity: 100, RecvBufferCapacity: 50 * 4096, RecvMessageCapacity: maxMsgSize, @@ -798,7 +798,7 @@ OUTER_LOOP: schema.Upload, ) } - case prs.Height < rs.Height-2: // catchup + case prs.Height < rs.Height-1: // catchup blockStoreBase := conR.conS.blockStore.Base() if blockStoreBase > 0 && prs.Height >= blockStoreBase { heightLogger := logger.With("height", prs.Height) From f78269c31903664c2d65f364db251cec507ad8d1 Mon Sep 17 00:00:00 2001 From: evan-forbes <evan.samuel.forbes@gmail.com> Date: Mon, 30 Sep 2024 01:02:13 -0500 Subject: [PATCH 088/127] fix: don't allocated empty txs! --- mempool/cat/store.go | 2 +- rpc/core/mocks/mempool.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/mempool/cat/store.go b/mempool/cat/store.go index 1b47345c79..b1861282e4 100644 --- a/mempool/cat/store.go +++ b/mempool/cat/store.go @@ -177,7 +177,7 @@ func (s *store) getAllTxs() []*wrappedTx { func (s *store) getAllSeenTxs(seenLimit int) []*wrappedTx { s.mtx.RLock() defer s.mtx.RUnlock() - txs := make([]*wrappedTx, len(s.txs)) + txs := make([]*wrappedTx, 0) //nolint:prealloc idx := 0 for _, tx := range s.txs { if tx.seenCount < seenLimit { diff --git a/rpc/core/mocks/mempool.go b/rpc/core/mocks/mempool.go index bdf127f437..68cd15d2f6 100644 --- a/rpc/core/mocks/mempool.go +++ b/rpc/core/mocks/mempool.go @@ -116,11 +116,11 @@ func (mr *MockMempoolMockRecorder) Lock() *gomock.Call { } // ReapMaxBytesMaxGas mocks base method. -func (m *MockMempool) ReapMaxBytesMaxGas(maxBytes, maxGas int64) types0.Txs { +func (m *MockMempool) ReapMaxBytesMaxGas(maxBytes, maxGas int64) (types0.Txs, []types0.TxKey) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "ReapMaxBytesMaxGas", maxBytes, maxGas) ret0, _ := ret[0].(types0.Txs) - return ret0 + return ret0, []types0.TxKey{} } // ReapMaxBytesMaxGas indicates an expected call of ReapMaxBytesMaxGas. From cd0b52027839b6b3e050437fbbbcaca910543c2a Mon Sep 17 00:00:00 2001 From: evan-forbes <evan.samuel.forbes@gmail.com> Date: Mon, 30 Sep 2024 01:18:01 -0500 Subject: [PATCH 089/127] chore: revert avoiding call to app during prepare --- state/execution.go | 75 ++++++++++++++++++++++------------------------ 1 file changed, 36 insertions(+), 39 deletions(-) diff --git a/state/execution.go b/state/execution.go index c42be9c43b..7f3103a3a3 100644 --- a/state/execution.go +++ b/state/execution.go @@ -126,35 +126,35 @@ func (blockExec *BlockExecutor) CreateProposalBlock( // timestamp = MedianTime(commit, state.LastValidators) // } - // preparedProposal, err := blockExec.proxyApp.PrepareProposalSync( - // abci.RequestPrepareProposal{ - // BlockData: &cmtproto.Data{Txs: txs.ToSliceOfBytes()}, - // BlockDataSize: maxDataBytes, - // ChainId: state.ChainID, - // Height: height, - // Time: timestamp, - // }, - // ) - // if err != nil { - // // The App MUST ensure that only valid (and hence 'processable') transactions - // // enter the mempool. Hence, at this point, we can't have any non-processable - // // transaction causing an error. - // // - // // Also, the App can simply skip any transaction that could cause any kind of trouble. - // // Either way, we can not recover in a meaningful way, unless we skip proposing - // // this block, repair what caused the error and try again. Hence, we panic on - // // purpose for now. - // panic(err) - // } - // rawNewData := preparedProposal.GetBlockData() - // var blockDataSize int - // for _, tx := range rawNewData.GetTxs() { - // blockDataSize += len(tx) - - // if maxDataBytes < int64(blockDataSize) { - // panic("block data exceeds max amount of allowed bytes") - // } - // } + preparedProposal, err := blockExec.proxyApp.PrepareProposalSync( + abci.RequestPrepareProposal{ + BlockData: &cmtproto.Data{Txs: txs.ToSliceOfBytes()}, + BlockDataSize: maxDataBytes, + ChainId: state.ChainID, + Height: height, + // Time: timestamp, + }, + ) + if err != nil { + // The App MUST ensure that only valid (and hence 'processable') transactions + // enter the mempool. Hence, at this point, we can't have any non-processable + // transaction causing an error. + // + // Also, the App can simply skip any transaction that could cause any kind of trouble. + // Either way, we can not recover in a meaningful way, unless we skip proposing + // this block, repair what caused the error and try again. Hence, we panic on + // purpose for now. + panic(err) + } + rawNewData := preparedProposal.GetBlockData() + var blockDataSize int + for _, tx := range rawNewData.GetTxs() { + blockDataSize += len(tx) + + if maxDataBytes < int64(blockDataSize) { + panic("block data exceeds max amount of allowed bytes") + } + } hashBytes := make([][]byte, len(hashes)) for i, h := range hashes { @@ -163,20 +163,17 @@ func (blockExec *BlockExecutor) CreateProposalBlock( // rawNewData.Hash = merkle.HashFromByteSlices(hashBytes) - // newData, err := types.DataFromProto(rawNewData) - // if err != nil { - // // todo(evan): see if we can get rid of this panic - // panic(err) - // } - - data := types.Data{ - Txs: txs, + newData, err := types.DataFromProto(rawNewData) + if err != nil { + // todo(evan): see if we can get rid of this panic + panic(err) } - data.SetHash(merkle.HashFromByteSlices(hashBytes)) + + newData.SetHash(merkle.HashFromByteSlices(hashBytes)) block, _ := state.MakeBlock( height, - data, + newData, commit, evidence, proposerAddr, From 9b30c532de802ba6a7286105a1c4dfb339c160b8 Mon Sep 17 00:00:00 2001 From: evan-forbes <evan.samuel.forbes@gmail.com> Date: Mon, 30 Sep 2024 02:15:07 -0500 Subject: [PATCH 090/127] Revert "chore: revert avoiding call to app during prepare" This reverts commit cd0b52027839b6b3e050437fbbbcaca910543c2a. --- state/execution.go | 75 ++++++++++++++++++++++++---------------------- 1 file changed, 39 insertions(+), 36 deletions(-) diff --git a/state/execution.go b/state/execution.go index 7f3103a3a3..c42be9c43b 100644 --- a/state/execution.go +++ b/state/execution.go @@ -126,35 +126,35 @@ func (blockExec *BlockExecutor) CreateProposalBlock( // timestamp = MedianTime(commit, state.LastValidators) // } - preparedProposal, err := blockExec.proxyApp.PrepareProposalSync( - abci.RequestPrepareProposal{ - BlockData: &cmtproto.Data{Txs: txs.ToSliceOfBytes()}, - BlockDataSize: maxDataBytes, - ChainId: state.ChainID, - Height: height, - // Time: timestamp, - }, - ) - if err != nil { - // The App MUST ensure that only valid (and hence 'processable') transactions - // enter the mempool. Hence, at this point, we can't have any non-processable - // transaction causing an error. - // - // Also, the App can simply skip any transaction that could cause any kind of trouble. - // Either way, we can not recover in a meaningful way, unless we skip proposing - // this block, repair what caused the error and try again. Hence, we panic on - // purpose for now. - panic(err) - } - rawNewData := preparedProposal.GetBlockData() - var blockDataSize int - for _, tx := range rawNewData.GetTxs() { - blockDataSize += len(tx) - - if maxDataBytes < int64(blockDataSize) { - panic("block data exceeds max amount of allowed bytes") - } - } + // preparedProposal, err := blockExec.proxyApp.PrepareProposalSync( + // abci.RequestPrepareProposal{ + // BlockData: &cmtproto.Data{Txs: txs.ToSliceOfBytes()}, + // BlockDataSize: maxDataBytes, + // ChainId: state.ChainID, + // Height: height, + // Time: timestamp, + // }, + // ) + // if err != nil { + // // The App MUST ensure that only valid (and hence 'processable') transactions + // // enter the mempool. Hence, at this point, we can't have any non-processable + // // transaction causing an error. + // // + // // Also, the App can simply skip any transaction that could cause any kind of trouble. + // // Either way, we can not recover in a meaningful way, unless we skip proposing + // // this block, repair what caused the error and try again. Hence, we panic on + // // purpose for now. + // panic(err) + // } + // rawNewData := preparedProposal.GetBlockData() + // var blockDataSize int + // for _, tx := range rawNewData.GetTxs() { + // blockDataSize += len(tx) + + // if maxDataBytes < int64(blockDataSize) { + // panic("block data exceeds max amount of allowed bytes") + // } + // } hashBytes := make([][]byte, len(hashes)) for i, h := range hashes { @@ -163,17 +163,20 @@ func (blockExec *BlockExecutor) CreateProposalBlock( // rawNewData.Hash = merkle.HashFromByteSlices(hashBytes) - newData, err := types.DataFromProto(rawNewData) - if err != nil { - // todo(evan): see if we can get rid of this panic - panic(err) - } + // newData, err := types.DataFromProto(rawNewData) + // if err != nil { + // // todo(evan): see if we can get rid of this panic + // panic(err) + // } - newData.SetHash(merkle.HashFromByteSlices(hashBytes)) + data := types.Data{ + Txs: txs, + } + data.SetHash(merkle.HashFromByteSlices(hashBytes)) block, _ := state.MakeBlock( height, - newData, + data, commit, evidence, proposerAddr, From 41402dcc98aa003e91d39293bb4d00ed8e110a09 Mon Sep 17 00:00:00 2001 From: evan-forbes <evan.samuel.forbes@gmail.com> Date: Mon, 30 Sep 2024 02:28:24 -0500 Subject: [PATCH 091/127] chore: fix reaping txs from the mempool --- mempool/cat/pool.go | 2 +- mempool/cat/store.go | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/mempool/cat/pool.go b/mempool/cat/pool.go index c5f67d5e44..079950c777 100644 --- a/mempool/cat/pool.go +++ b/mempool/cat/pool.go @@ -40,7 +40,7 @@ var ( // set the default to 5, but this value can be changed in an init func InclusionDelay = 5 * time.Second peerCount = atomic.Int32{} - defaultSeenLimit = 72 + defaultSeenLimit = 79 ) // TxPoolOption sets an optional parameter on the TxPool. diff --git a/mempool/cat/store.go b/mempool/cat/store.go index b1861282e4..aa72e9318d 100644 --- a/mempool/cat/store.go +++ b/mempool/cat/store.go @@ -178,13 +178,11 @@ func (s *store) getAllSeenTxs(seenLimit int) []*wrappedTx { s.mtx.RLock() defer s.mtx.RUnlock() txs := make([]*wrappedTx, 0) //nolint:prealloc - idx := 0 for _, tx := range s.txs { if tx.seenCount < seenLimit { continue } - txs[idx] = tx - idx++ + txs = append(txs, tx) } return txs } From 4cd0efaaee06176182eaba00a9d45c66675cbc26 Mon Sep 17 00:00:00 2001 From: evan-forbes <evan.samuel.forbes@gmail.com> Date: Mon, 30 Sep 2024 03:30:32 -0500 Subject: [PATCH 092/127] fix: forget to set the seen txs --- mempool/cat/pool.go | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/mempool/cat/pool.go b/mempool/cat/pool.go index 079950c777..c9d27aa647 100644 --- a/mempool/cat/pool.go +++ b/mempool/cat/pool.go @@ -447,11 +447,27 @@ func (txmp *TxPool) allEntriesSorted() []*wrappedTx { } func (txmp *TxPool) seenEntries(seenLimit int) []*wrappedTx { - txs := txmp.store.getAllSeenTxs(seenLimit) - sort.Slice(txs, func(i, j int) bool { - return txs[i].seenCount > txs[j].seenCount + // Get all transactions from the store + txs := txmp.store.getAllTxs() + + // Preallocate a slice to avoid reallocations + prunedTxs := make([]*wrappedTx, 0, len(txs)) + + // Prune transactions that don't exceed the seenLimit + for _, tx := range txs { + seen := txmp.seenByPeersSet.GetSeenCount(tx.key) + if seen > seenLimit { + tx.seenCount = seen + prunedTxs = append(prunedTxs, tx) + } + } + + // Sort the remaining transactions by seenCount in descending order + sort.Slice(prunedTxs, func(i, j int) bool { + return prunedTxs[i].seenCount > prunedTxs[j].seenCount }) - return txs + + return prunedTxs } // ReapMaxBytesMaxGas returns a slice of valid transactions that fit within the From 723fd7849348f979718f73761292bcb20f3a8c7b Mon Sep 17 00:00:00 2001 From: evan-forbes <evan.samuel.forbes@gmail.com> Date: Mon, 30 Sep 2024 03:55:02 -0500 Subject: [PATCH 093/127] fix: app tests --- mempool/cat/pool.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/mempool/cat/pool.go b/mempool/cat/pool.go index c9d27aa647..cc6b407b65 100644 --- a/mempool/cat/pool.go +++ b/mempool/cat/pool.go @@ -456,7 +456,7 @@ func (txmp *TxPool) seenEntries(seenLimit int) []*wrappedTx { // Prune transactions that don't exceed the seenLimit for _, tx := range txs { seen := txmp.seenByPeersSet.GetSeenCount(tx.key) - if seen > seenLimit { + if seen >= seenLimit { tx.seenCount = seen prunedTxs = append(prunedTxs, tx) } @@ -487,9 +487,7 @@ func (txmp *TxPool) ReapMaxBytesMaxGas(maxBytes, maxGas int64) (types.Txs, []typ var keep []types.Tx //nolint:prealloc var keys []types.TxKey //nolint:prealloc - seenLimit := getSeenLimit() - - for _, w := range txmp.seenEntries(seenLimit) { + for _, w := range txmp.seenEntries(getSeenLimit()) { // skip transactions that have been in the mempool for less than the inclusion delay // This gives time for the transaction to be broadcast to all peers // if currentTime.Sub(w.timestamp) < InclusionDelay { From 4f2e3e573105ed3394c283a4c0c36df06313ff53 Mon Sep 17 00:00:00 2001 From: evan-forbes <evan.samuel.forbes@gmail.com> Date: Mon, 30 Sep 2024 16:59:46 -0500 Subject: [PATCH 094/127] chore: adjust state channel priority --- consensus/reactor.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/consensus/reactor.go b/consensus/reactor.go index 8d6dfb3fb4..bcb056a6b9 100644 --- a/consensus/reactor.go +++ b/consensus/reactor.go @@ -153,7 +153,7 @@ func (conR *Reactor) GetChannels() []*p2p.ChannelDescriptor { return []*p2p.ChannelDescriptor{ { ID: StateChannel, - Priority: 20, + Priority: 7, SendQueueCapacity: 100, RecvMessageCapacity: maxMsgSize, MessageType: &cmtcons.Message{}, From 686cfd8c4b13049334f365fc3283459c892ab2d9 Mon Sep 17 00:00:00 2001 From: evan-forbes <evan.samuel.forbes@gmail.com> Date: Mon, 30 Sep 2024 22:30:45 -0500 Subject: [PATCH 095/127] chore: increase blockpart size --- types/params.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/params.go b/types/params.go index 6d4c475f25..6c5f1041c5 100644 --- a/types/params.go +++ b/types/params.go @@ -15,7 +15,7 @@ const ( MaxBlockSizeBytes = 1_000_000_000 // 100MB // BlockPartSizeBytes is the size of one block part. - BlockPartSizeBytes uint32 = 65536 // 64kB + BlockPartSizeBytes uint32 = 4000000 // 64kB // MaxBlockPartsCount is the maximum number of block parts. MaxBlockPartsCount = (MaxBlockSizeBytes / BlockPartSizeBytes) + 1 From 587af80832ef653e2f55e0dd37cc2f02ec052a04 Mon Sep 17 00:00:00 2001 From: evan-forbes <evan.samuel.forbes@gmail.com> Date: Mon, 30 Sep 2024 23:02:30 -0500 Subject: [PATCH 096/127] fix: inrease the max message size and turn off the wal entirely --- consensus/reactor.go | 2 +- consensus/state.go | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/consensus/reactor.go b/consensus/reactor.go index bcb056a6b9..c7e07f75fc 100644 --- a/consensus/reactor.go +++ b/consensus/reactor.go @@ -30,7 +30,7 @@ const ( VoteChannel = byte(0x22) VoteSetBitsChannel = byte(0x23) - maxMsgSize = 1048576 // 1MB; NOTE/TODO: keep in sync with types.PartSet sizes. + maxMsgSize = 100048576 // 1MB; NOTE/TODO: keep in sync with types.PartSet sizes. blocksToContributeToBecomeGoodPeer = 10000 votesToContributeToBecomeGoodPeer = 10000 diff --git a/consensus/state.go b/consensus/state.go index 94a1d5e3d5..323ce4fa28 100644 --- a/consensus/state.go +++ b/consensus/state.go @@ -344,11 +344,11 @@ func (cs *State) LoadCommit(height int64) *types.Commit { func (cs *State) OnStart() error { // We may set the WAL in testing before calling Start, so only OpenWAL if its // still the nilWAL. - if _, ok := cs.wal.(nilWAL); ok { - if err := cs.loadWalFile(); err != nil { - return err - } - } + // if _, ok := cs.wal.(nilWAL); ok { + // if err := cs.loadWalFile(); err != nil { + // return err + // } + // } cs.metrics.StartHeight.Set(float64(cs.Height)) From a55b875d4efc24974a199c6dce916eea7da3a3a6 Mon Sep 17 00:00:00 2001 From: evan-forbes <evan.samuel.forbes@gmail.com> Date: Mon, 30 Sep 2024 23:27:47 -0500 Subject: [PATCH 097/127] fix: logger panic --- consensus/state.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/consensus/state.go b/consensus/state.go index 323ce4fa28..b5154fefb5 100644 --- a/consensus/state.go +++ b/consensus/state.go @@ -2081,14 +2081,14 @@ func (cs *State) fetchCompactBlock(ctx context.Context, blockHash []byte, compac } if !bytes.Equal(block.Hash(), cs.Proposal.BlockID.Hash) { - cs.Logger.Error("received compact block with header hash [%v] that does not match proposal [%v]", block.Hash(), cs.Proposal.BlockID.Hash) + cs.Logger.Error("received compact block with header hash that does not match proposal", "hash", block.Hash(), "block id hash", cs.Proposal.BlockID.Hash) return } // check that the part set header matched that of the partSet := block.MakePartSet(types.BlockPartSizeBytes) if !partSet.HasHeader(cs.Proposal.BlockID.PartSetHeader) { - cs.Logger.Error("received compact block with part set header [%v] that does not match proposal [%v]", partSet.Header(), cs.Proposal.BlockID.PartSetHeader) + cs.Logger.Error("received compact block with part set header that does not match proposal", "header", partSet.Header(), "psh", cs.Proposal.BlockID.PartSetHeader) return } From 1c765718d0e0b4931b03ecec2dea6a17b554ec6c Mon Sep 17 00:00:00 2001 From: evan-forbes <evan.samuel.forbes@gmail.com> Date: Tue, 1 Oct 2024 07:40:59 -0500 Subject: [PATCH 098/127] chore: use a middleground value for block parts --- types/params.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/params.go b/types/params.go index 6c5f1041c5..ddd3322230 100644 --- a/types/params.go +++ b/types/params.go @@ -15,7 +15,7 @@ const ( MaxBlockSizeBytes = 1_000_000_000 // 100MB // BlockPartSizeBytes is the size of one block part. - BlockPartSizeBytes uint32 = 4000000 // 64kB + BlockPartSizeBytes uint32 = 1000000 // 64kB // MaxBlockPartsCount is the maximum number of block parts. MaxBlockPartsCount = (MaxBlockSizeBytes / BlockPartSizeBytes) + 1 From 3c1d0c1674221e6c114ae317df191b1c0e251324 Mon Sep 17 00:00:00 2001 From: evan-forbes <evan.samuel.forbes@gmail.com> Date: Tue, 1 Oct 2024 09:35:43 -0500 Subject: [PATCH 099/127] fix: remove useless consensus state message --- consensus/reactor.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/consensus/reactor.go b/consensus/reactor.go index c7e07f75fc..d68050b48a 100644 --- a/consensus/reactor.go +++ b/consensus/reactor.go @@ -542,12 +542,12 @@ func (conR *Reactor) subscribeToBroadcastEvents() { conR.Logger.Error("Error adding listener for events", "err", err) } - if err := conR.conS.evsw.AddListenerForEvent(subscriber, types.EventVote, - func(data cmtevents.EventData) { - conR.broadcastHasVoteMessage(data.(*types.Vote)) - }); err != nil { - conR.Logger.Error("Error adding listener for events", "err", err) - } + // if err := conR.conS.evsw.AddListenerForEvent(subscriber, types.EventVote, + // func(data cmtevents.EventData) { + // conR.broadcastHasVoteMessage(data.(*types.Vote)) + // }); err != nil { + // conR.Logger.Error("Error adding listener for events", "err", err) + // } } From 2e43d1f47e7ea175b548bc8d25a6891d83dcf74d Mon Sep 17 00:00:00 2001 From: evan-forbes <evan.samuel.forbes@gmail.com> Date: Tue, 1 Oct 2024 14:22:47 -0500 Subject: [PATCH 100/127] chore: switch the block part size back --- types/params.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/params.go b/types/params.go index ddd3322230..6d4c475f25 100644 --- a/types/params.go +++ b/types/params.go @@ -15,7 +15,7 @@ const ( MaxBlockSizeBytes = 1_000_000_000 // 100MB // BlockPartSizeBytes is the size of one block part. - BlockPartSizeBytes uint32 = 1000000 // 64kB + BlockPartSizeBytes uint32 = 65536 // 64kB // MaxBlockPartsCount is the maximum number of block parts. MaxBlockPartsCount = (MaxBlockSizeBytes / BlockPartSizeBytes) + 1 From 23930d23d7f154011be8a258dd3fa013804f94e6 Mon Sep 17 00:00:00 2001 From: evan-forbes <evan.samuel.forbes@gmail.com> Date: Tue, 1 Oct 2024 15:56:02 -0500 Subject: [PATCH 101/127] feat!: use the data root as the part set header hash :eyes: --- consensus/state_test.go | 2 +- crypto/merkle/proof.go | 5 +++++ mempool/cat/pool.go | 2 +- types/block.go | 2 +- types/part_set.go | 41 +++++++++++++++++++++++------------------ types/part_set_test.go | 8 ++++---- 6 files changed, 35 insertions(+), 25 deletions(-) diff --git a/consensus/state_test.go b/consensus/state_test.go index aaad77f1bd..1f20451201 100644 --- a/consensus/state_test.go +++ b/consensus/state_test.go @@ -1830,7 +1830,7 @@ func TestStateOutputsBlockPartsStats(t *testing.T) { peer := p2pmock.NewPeer(nil) // 1) new block part - parts := types.NewPartSetFromData(cmtrand.Bytes(100), 10) + parts := types.NewPartSetFromData(cmtrand.Bytes(100), []byte{}, 10) msg := &BlockPartMessage{ Height: 1, Round: 0, diff --git a/crypto/merkle/proof.go b/crypto/merkle/proof.go index 1084bdf7c1..8ded6b3653 100644 --- a/crypto/merkle/proof.go +++ b/crypto/merkle/proof.go @@ -111,6 +111,11 @@ func (sp *Proof) StringIndented(indent string) string { // NOTE: it expects the LeafHash and the elements of Aunts to be of size tmhash.Size, // and it expects at most MaxAunts elements in Aunts. func (sp *Proof) ValidateBasic() error { + // hack: todo remove proofs from the block parts that now rely on compact + // block hashes and the data root + if sp == nil { + return nil + } if sp.Total < 0 { return errors.New("negative Total") } diff --git a/mempool/cat/pool.go b/mempool/cat/pool.go index cc6b407b65..8f7a001faf 100644 --- a/mempool/cat/pool.go +++ b/mempool/cat/pool.go @@ -40,7 +40,7 @@ var ( // set the default to 5, but this value can be changed in an init func InclusionDelay = 5 * time.Second peerCount = atomic.Int32{} - defaultSeenLimit = 79 + defaultSeenLimit = 82 ) // TxPoolOption sets an optional parameter on the TxPool. diff --git a/types/block.go b/types/block.go index 39c1b89c9b..e627cadde8 100644 --- a/types/block.go +++ b/types/block.go @@ -158,7 +158,7 @@ func (b *Block) MakePartSet(partSize uint32) *PartSet { if err != nil { panic(err) } - return NewPartSetFromData(bz, partSize) + return NewPartSetFromData(bz, b.DataHash, partSize) } // HashesTo is a convenience function that checks if a block hashes to the given argument. diff --git a/types/part_set.go b/types/part_set.go index 01fcec06ae..ed6b6aaffb 100644 --- a/types/part_set.go +++ b/types/part_set.go @@ -31,9 +31,11 @@ func (part *Part) ValidateBasic() error { if len(part.Bytes) > int(BlockPartSizeBytes) { return fmt.Errorf("too big: %d bytes, max: %d", len(part.Bytes), BlockPartSizeBytes) } - if err := part.Proof.ValidateBasic(); err != nil { - return fmt.Errorf("wrong Proof: %w", err) - } + // assume we're using a single commitment over the block data and relying on + // the signed hashes from the compact block + // if err := part.Proof.ValidateBasic(); err != nil { + // return fmt.Errorf("wrong Proof: %w", err) + // } return nil } @@ -78,13 +80,13 @@ func PartFromProto(pb *cmtproto.Part) (*Part, error) { } part := new(Part) - proof, err := merkle.ProofFromProto(&pb.Proof) - if err != nil { - return nil, err - } + // proof, err := merkle.ProofFromProto(&pb.Proof) + // if err != nil { + // return nil, err + // } part.Index = pb.Index part.Bytes = pb.Bytes - part.Proof = *proof + // part.Proof = *proof return part, part.ValidateBasic() } @@ -163,7 +165,7 @@ type PartSet struct { // Returns an immutable, full PartSet from the data bytes. // The data bytes are split into "partSize" chunks, and merkle tree computed. // CONTRACT: partSize is greater than zero. -func NewPartSetFromData(data []byte, partSize uint32) *PartSet { +func NewPartSetFromData(data []byte, dataRoot []byte, partSize uint32) *PartSet { // divide data into 4kb parts. total := (uint32(len(data)) + partSize - 1) / partSize parts := make([]*Part, total) @@ -178,14 +180,17 @@ func NewPartSetFromData(data []byte, partSize uint32) *PartSet { partsBytes[i] = part.Bytes partsBitArray.SetIndex(int(i), true) } + // assume we're using a single commitment over the block data and relying on + // the signed hashes from the compact block + // // Compute merkle proofs - root, proofs := merkle.ProofsFromByteSlices(partsBytes) - for i := uint32(0); i < total; i++ { - parts[i].Proof = *proofs[i] - } + // root, proofs := merkle.ProofsFromByteSlices(partsBytes) for i := uint32(0); i < total; + // i++ { + // parts[i].Proof = *proofs[i] + // } return &PartSet{ total: total, - hash: root, + hash: dataRoot, parts: parts, partsBitArray: partsBitArray, count: total, @@ -280,10 +285,10 @@ func (ps *PartSet) AddPart(part *Part) (bool, error) { return false, nil } - // Check hash proof - if part.Proof.Verify(ps.Hash(), part.Bytes) != nil { - return false, ErrPartSetInvalidProof - } + // // Check hash proof + // if part.Proof.Verify(ps.Hash(), part.Bytes) != nil { + // return false, ErrPartSetInvalidProof + // } // Add part ps.parts[part.Index] = part diff --git a/types/part_set_test.go b/types/part_set_test.go index ec55fed694..c85e6a4fd9 100644 --- a/types/part_set_test.go +++ b/types/part_set_test.go @@ -19,7 +19,7 @@ func TestBasicPartSet(t *testing.T) { // Construct random data of size partSize * 100 nParts := 100 data := cmtrand.Bytes(testPartSize * nParts) - partSet := NewPartSetFromData(data, testPartSize) + partSet := NewPartSetFromData(data, []byte{}, testPartSize) assert.NotEmpty(t, partSet.Hash()) assert.EqualValues(t, nParts, partSet.Total()) @@ -66,7 +66,7 @@ func TestBasicPartSet(t *testing.T) { func TestWrongProof(t *testing.T) { // Construct random data of size partSize * 100 data := cmtrand.Bytes(testPartSize * 100) - partSet := NewPartSetFromData(data, testPartSize) + partSet := NewPartSetFromData(data, []byte{}, testPartSize) // Test adding a part with wrong data. partSet2 := NewPartSetFromHeader(partSet.Header()) @@ -101,7 +101,7 @@ func TestPartSetHeaderValidateBasic(t *testing.T) { tc := tc t.Run(tc.testName, func(t *testing.T) { data := cmtrand.Bytes(testPartSize * 100) - ps := NewPartSetFromData(data, testPartSize) + ps := NewPartSetFromData(data, []byte{}, testPartSize) psHeader := ps.Header() tc.malleatePartSetHeader(&psHeader) assert.Equal(t, tc.expectErr, psHeader.ValidateBasic() != nil, "Validate Basic had an unexpected result") @@ -130,7 +130,7 @@ func TestPartValidateBasic(t *testing.T) { tc := tc t.Run(tc.testName, func(t *testing.T) { data := cmtrand.Bytes(testPartSize * 100) - ps := NewPartSetFromData(data, testPartSize) + ps := NewPartSetFromData(data, []byte{}, testPartSize) part := ps.GetPart(0) tc.malleatePart(part) assert.Equal(t, tc.expectErr, part.ValidateBasic() != nil, "Validate Basic had an unexpected result") From 77103e94492e695b9f45970894c30f2acc75bd7f Mon Sep 17 00:00:00 2001 From: evan-forbes <evan.samuel.forbes@gmail.com> Date: Tue, 1 Oct 2024 18:29:16 -0500 Subject: [PATCH 102/127] chore: minor issue with gossiping txs perfectly --- mempool/cat/reactor.go | 45 ++++++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/mempool/cat/reactor.go b/mempool/cat/reactor.go index 7285815ecd..96aac321d7 100644 --- a/mempool/cat/reactor.go +++ b/mempool/cat/reactor.go @@ -20,7 +20,7 @@ import ( const ( // default duration to wait before considering a peer non-responsive // and searching for the tx from a new peer - DefaultGossipDelay = 200 * time.Millisecond + DefaultGossipDelay = 12000 * time.Millisecond // Content Addressable Tx Pool gossips state based messages (SeenTx and WantTx) on a separate channel // for cross compatibility @@ -311,27 +311,30 @@ func (memR *Reactor) ReceiveEnvelope(e p2p.Envelope) { } go func(tx []byte, key types.TxKey) { - wants, has := memR.wantState.GetWants(key) - if has { - for peer := range wants { - p := memR.ids.GetPeer(peer) - if p == nil { - return - } - if p2p.SendEnvelopeShim(e.Src, p2p.Envelope{ //nolint:staticcheck - ChannelID: mempool.MempoolChannel, - Message: &protomem.Txs{Txs: [][]byte{tx}}, - }, memR.Logger) { - memR.wantState.Delete(key, peer) - // memR.mempool.PeerHasTx(peerID, txKey) - schema.WriteMempoolTx( - memR.traceClient, - string(p.ID()), - key[:], - schema.Upload, - ) + for i := 0; i < 5; i++ { + wants, has := memR.wantState.GetWants(key) + if has { + for peer := range wants { + p := memR.ids.GetPeer(peer) + if p == nil { + continue + } + if p2p.SendEnvelopeShim(e.Src, p2p.Envelope{ //nolint:staticcheck + ChannelID: mempool.MempoolChannel, + Message: &protomem.Txs{Txs: [][]byte{tx}}, + }, memR.Logger) { + memR.wantState.Delete(key, peer) + // memR.mempool.PeerHasTx(peerID, txKey) + schema.WriteMempoolTx( + memR.traceClient, + string(p.ID()), + key[:], + schema.Upload, + ) + } } } + time.Sleep(time.Second) } }(tx, key) @@ -380,7 +383,7 @@ func (memR *Reactor) ReceiveEnvelope(e p2p.Envelope) { // We don't have the transaction, nor are we requesting it so we send the node // a want msg - memR.requestTx(txKey, e.Src, 5) + memR.requestTx(txKey, e.Src, 10) // A peer is requesting a transaction that we have claimed to have. Find the specified // transaction and broadcast it to the peer. We may no longer have the transaction From 89ef034f2b6a5a23fbe70a9ee834609fe4f42277 Mon Sep 17 00:00:00 2001 From: evan-forbes <evan.samuel.forbes@gmail.com> Date: Tue, 1 Oct 2024 22:08:14 -0500 Subject: [PATCH 103/127] feat: periodically rebroadcast seentxs --- mempool/cat/block_builder.go | 11 ++++++-- mempool/cat/pool.go | 25 ++++++++++++++--- mempool/cat/pool_test.go | 2 +- mempool/cat/reactor.go | 53 +++++++++++++++++++++++++----------- mempool/cat/tx.go | 7 ++--- 5 files changed, 71 insertions(+), 27 deletions(-) diff --git a/mempool/cat/block_builder.go b/mempool/cat/block_builder.go index 211a8c6b43..3a6a73458e 100644 --- a/mempool/cat/block_builder.go +++ b/mempool/cat/block_builder.go @@ -2,6 +2,7 @@ package cat import ( "context" + "crypto/sha256" "fmt" "sync" "time" @@ -99,7 +100,13 @@ func (memR *Reactor) FetchKeysFromTxs(ctx context.Context, txs [][]byte) ([][]by if ctx.Err() != nil { return nil, ctx.Err() } - key := types.Tx(tx).Key() + key := [32]byte{} + blobTx, isBlobTx := types.UnmarshalBlobTx(tx) + if isBlobTx { + key = sha256.Sum256(blobTx.Tx) + } else { + key = sha256.Sum256(tx) + } keys[idx] = key[:] has := memR.mempool.store.has(key) if !has { @@ -111,7 +118,7 @@ func (memR *Reactor) FetchKeysFromTxs(ctx context.Context, txs [][]byte) ([][]by // // We don't set the priority, gasWanted or sender fields because we // don't know them. - wtx := newWrappedTx(tx, key, memR.mempool.Height(), 0, 0, "", false) + wtx := newWrappedTx(tx, key, memR.mempool.Height(), 0, 0, "", isBlobTx) wtx.evictable = false memR.broadcastNewTx(wtx) // For safety we also store this transaction in the mempool (ignoring diff --git a/mempool/cat/pool.go b/mempool/cat/pool.go index 8f7a001faf..95dc187171 100644 --- a/mempool/cat/pool.go +++ b/mempool/cat/pool.go @@ -1,6 +1,7 @@ package cat import ( + "crypto/sha256" "errors" "fmt" "os" @@ -256,8 +257,14 @@ func (txmp *TxPool) CheckTx(tx types.Tx, cb func(*abci.Response), txInfo mempool // This is a new transaction that we haven't seen before. Verify it against the app and attempt // to add it to the transaction pool. - key := tx.Key() - rsp, err := txmp.TryAddNewTx(tx, key, txInfo) + key := [32]byte{} + blobTx, isBlobTx := types.UnmarshalBlobTx(tx) + if isBlobTx { + key = sha256.Sum256(blobTx.Tx) + } else { + key = sha256.Sum256(tx) + } + rsp, err := txmp.TryAddNewTx(tx, key, txInfo, isBlobTx) if err != nil { return err } @@ -319,7 +326,7 @@ func (txmp *TxPool) markToBeBroadcast(key types.TxKey) { // to avoid races with the same tx. It then call `CheckTx` so that the application can validate it. // If it passes `CheckTx`, the new transaction is added to the mempool as long as it has // sufficient priority and space else if evicted it will return an error -func (txmp *TxPool) TryAddNewTx(tx types.Tx, key types.TxKey, txInfo mempool.TxInfo) (*abci.ResponseCheckTx, error) { +func (txmp *TxPool) TryAddNewTx(tx types.Tx, key types.TxKey, txInfo mempool.TxInfo, isBlob bool) (*abci.ResponseCheckTx, error) { // First check any of the caches to see if we can conclude early. We may have already seen and processed // the transaction, or it may have already been committed. if txmp.store.hasCommitted(key) { @@ -374,7 +381,7 @@ func (txmp *TxPool) TryAddNewTx(tx types.Tx, key types.TxKey, txInfo mempool.TxI // Create wrapped tx wtx := newWrappedTx( - tx, key, txmp.Height(), rsp.GasWanted, rsp.Priority, rsp.Sender, txInfo.SenderID == 0, + tx, key, txmp.Height(), rsp.GasWanted, rsp.Priority, rsp.Sender, isBlob, ) // Perform the post check @@ -459,6 +466,12 @@ func (txmp *TxPool) seenEntries(seenLimit int) []*wrappedTx { if seen >= seenLimit { tx.seenCount = seen prunedTxs = append(prunedTxs, tx) + // treat non-blob txs as special since they propagate a lot faster and + // we need to get txsim off the ground + } else if !tx.isBlob { + if seen >= 2*(seenLimit/3) { + prunedTxs = append(prunedTxs, tx) + } } } @@ -470,6 +483,10 @@ func (txmp *TxPool) seenEntries(seenLimit int) []*wrappedTx { return prunedTxs } +func (txmp *TxPool) GetAllTxs() []*wrappedTx { + return txmp.store.getAllTxs() +} + // ReapMaxBytesMaxGas returns a slice of valid transactions that fit within the // size and gas constraints. The results are ordered by nonincreasing priority, // with ties broken by increasing order of arrival. Reaping transactions does diff --git a/mempool/cat/pool_test.go b/mempool/cat/pool_test.go index 81e28fbc06..5c422cb6de 100644 --- a/mempool/cat/pool_test.go +++ b/mempool/cat/pool_test.go @@ -734,7 +734,7 @@ func TestTxPool_ConcurrentlyAddingTx(t *testing.T) { wg.Add(1) go func(sender uint16) { defer wg.Done() - _, err := txmp.TryAddNewTx(tx, tx.Key(), mempool.TxInfo{SenderID: sender}) + _, err := txmp.TryAddNewTx(tx, tx.Key(), mempool.TxInfo{SenderID: sender}, false) errCh <- err }(uint16(i + 1)) } diff --git a/mempool/cat/reactor.go b/mempool/cat/reactor.go index 96aac321d7..8b60eb5772 100644 --- a/mempool/cat/reactor.go +++ b/mempool/cat/reactor.go @@ -1,6 +1,7 @@ package cat import ( + "crypto/sha256" "fmt" "time" @@ -133,19 +134,20 @@ func (memR *Reactor) OnStart() error { memR.Logger.Info("Tx broadcasting is disabled") } // run a separate go routine to check for time based TTLs - if memR.mempool.config.TTLDuration > 0 { - go func() { - ticker := time.NewTicker(memR.mempool.config.TTLDuration) - for { - select { - case <-ticker.C: - memR.mempool.CheckToPurgeExpiredTxs() - case <-memR.Quit(): - return - } - } - }() - } + // if memR.mempool.config.TTLDuration > 0 { + // go func() { + // ticker := time.NewTicker(memR.mempool.config.TTLDuration) + // for { + // select { + // case <-ticker.C: + // memR.mempool.CheckToPurgeExpiredTxs() + // case <-memR.Quit(): + // return + // } + // } + // }() + // } + go memR.PeriodicallyBroadcastSeenTxs(time.Second) return nil } @@ -279,7 +281,14 @@ func (memR *Reactor) ReceiveEnvelope(e p2p.Envelope) { var err error for _, tx := range protoTxs { ntx := types.Tx(tx) - key := ntx.Key() + key := [32]byte{} + // manually calculate the key to determine is its a blobtx while we're at it + blobTx, isBlobTx := types.UnmarshalBlobTx(tx) + if isBlobTx { + key = sha256.Sum256(blobTx.Tx) + } else { + key = sha256.Sum256(tx) + } schema.WriteMempoolTx(memR.traceClient, string(e.Src.ID()), key[:], schema.Download) // If we requested the transaction we mark it as received. if memR.requests.Has(peerID, key) { @@ -296,7 +305,7 @@ func (memR *Reactor) ReceiveEnvelope(e p2p.Envelope) { memR.blockFetcher.TryAddMissingTx(key, tx) // Now attempt to add the tx to the mempool. - _, err = memR.mempool.TryAddNewTx(ntx, key, txInfo) + _, err = memR.mempool.TryAddNewTx(ntx, key, txInfo, isBlobTx) if err != nil && err != ErrTxInMempool && err != ErrTxRecentlyCommitted { if memR.blockFetcher.IsMissingTx(key) { memR.Logger.Error("tx in block is not valid by mempool") @@ -436,6 +445,18 @@ func (memR *Reactor) ReceiveEnvelope(e p2p.Envelope) { } } +// PeriodicallyBroadcastSeenTxs will rebroadcast a seenTx for a given tx. It +// cycles through all txs, and waits the provided duration between each +// broadcast. +func (memR *Reactor) PeriodicallyBroadcastSeenTxs(dur time.Duration) { + for { + for _, tx := range memR.mempool.GetAllTxs() { + memR.broadcastSeenTx(tx.key, string(memR.self)) + time.Sleep(dur) + } + } +} + // PeerState describes the state of a peer. type PeerState interface { GetHeight() int64 @@ -444,7 +465,7 @@ type PeerState interface { // broadcastSeenTx broadcasts a SeenTx message to all peers unless we // know they have already seen the transaction func (memR *Reactor) broadcastSeenTx(txKey types.TxKey, from string) { - memR.Logger.Debug("broadcasting seen tx to all peers", "tx_key", txKey.String(), "from", from) + // memR.Logger.Debug("broadcasting seen tx to all peers", "tx_key", txKey.String(), "from", from) msg := &protomem.Message{ Sum: &protomem.Message_SeenTx{ SeenTx: &protomem.SeenTx{ diff --git a/mempool/cat/tx.go b/mempool/cat/tx.go index 90dab5299e..92470f22fc 100644 --- a/mempool/cat/tx.go +++ b/mempool/cat/tx.go @@ -20,12 +20,11 @@ type wrappedTx struct { sender string // app: assigned sender label evictable bool // whether this transaction can be evicted from the mempool. This is false when the transaction is a part of a proposed block nolint:lll - selfTx bool // keeps track of if this tx originated from this node. If so, then it will not be included in a block. This is a hack. - // temporary var only used for sorting when reaping seenCount int + isBlob bool } -func newWrappedTx(tx types.Tx, key types.TxKey, height, gasWanted, priority int64, sender string, self bool) *wrappedTx { +func newWrappedTx(tx types.Tx, key types.TxKey, height, gasWanted, priority int64, sender string, isBlob bool) *wrappedTx { return &wrappedTx{ tx: tx, key: key, @@ -35,7 +34,7 @@ func newWrappedTx(tx types.Tx, key types.TxKey, height, gasWanted, priority int6 priority: priority, sender: sender, evictable: true, - selfTx: self, + isBlob: isBlob, } } From 0a7bb1531169025a67054c5a10e42a97d281300e Mon Sep 17 00:00:00 2001 From: evan-forbes <evan.samuel.forbes@gmail.com> Date: Wed, 2 Oct 2024 17:39:52 -0500 Subject: [PATCH 104/127] fix: stop peer id leak --- mempool/cat/peers.go | 39 ++++++++++++++++++++++++++++++--------- mempool/cat/pool.go | 5 +---- 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/mempool/cat/peers.go b/mempool/cat/peers.go index 2738bdc7f7..af26990c8c 100644 --- a/mempool/cat/peers.go +++ b/mempool/cat/peers.go @@ -32,17 +32,41 @@ func newMempoolIDs() *mempoolIDs { // ReserveForPeer searches for the next unused ID and assigns it to the // peer. func (ids *mempoolIDs) ReserveForPeer(peer p2p.Peer) { - ids.mtx.Lock() - defer ids.mtx.Unlock() // if _, ok := ids.peerMap[peer.ID()]; ok { // panic("duplicate peer added to mempool") // } - curID := ids.nextPeerID() - ids.peerMap[peer.ID()] = curID + curID := ids.getIDSafe(peer.ID()) + + ids.mtx.Lock() + defer ids.mtx.Unlock() + ids.activeIDs[curID] = peer - ids.knownIDs[curID] = peer.ID() +} + +// getIDSafe ensures that all known ids are accountted for +func (ids *mempoolIDs) getIDSafe(id p2p.ID) uint16 { + ids.mtx.Lock() + defer ids.mtx.Unlock() + var newid uint16 + var seen bool + + for sid, pid := range ids.knownIDs { + if id == pid { + newid = sid + seen = true + break + } + } + if !seen { + newid = ids.nextPeerID() + } + + ids.peerMap[id] = newid + ids.knownIDs[newid] = id + + return newid } // nextPeerID returns the next unused peer ID to use. @@ -84,10 +108,7 @@ func (ids *mempoolIDs) GetIDForPeer(peerID p2p.ID) uint16 { id, exists := ids.peerMap[peerID] if !exists { - id = ids.nextPeerID() - ids.peerMap[peerID] = id - ids.knownIDs[id] = peerID - fmt.Println("known IDs:", len(ids.knownIDs)) + id = ids.getIDSafe(peerID) } return id } diff --git a/mempool/cat/pool.go b/mempool/cat/pool.go index 95dc187171..39be8155aa 100644 --- a/mempool/cat/pool.go +++ b/mempool/cat/pool.go @@ -264,6 +264,7 @@ func (txmp *TxPool) CheckTx(tx types.Tx, cb func(*abci.Response), txInfo mempool } else { key = sha256.Sum256(tx) } + rsp, err := txmp.TryAddNewTx(tx, key, txInfo, isBlobTx) if err != nil { return err @@ -468,10 +469,6 @@ func (txmp *TxPool) seenEntries(seenLimit int) []*wrappedTx { prunedTxs = append(prunedTxs, tx) // treat non-blob txs as special since they propagate a lot faster and // we need to get txsim off the ground - } else if !tx.isBlob { - if seen >= 2*(seenLimit/3) { - prunedTxs = append(prunedTxs, tx) - } } } From 6d9ef081f89c4e9cd2cdf9d4fd29fd9c10fac649 Mon Sep 17 00:00:00 2001 From: evan-forbes <evan.samuel.forbes@gmail.com> Date: Wed, 2 Oct 2024 18:52:23 -0500 Subject: [PATCH 105/127] fix: mutex got hit twice --- mempool/cat/peers.go | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/mempool/cat/peers.go b/mempool/cat/peers.go index af26990c8c..2456cdce0a 100644 --- a/mempool/cat/peers.go +++ b/mempool/cat/peers.go @@ -94,8 +94,7 @@ func (ids *mempoolIDs) Reclaim(peerID p2p.ID) uint16 { removedID, ok := ids.peerMap[peerID] if ok { delete(ids.activeIDs, removedID) - delete(ids.peerMap, peerID) - peerCount.Add(-1) + // delete(ids.peerMap, peerID) return removedID } return 0 @@ -103,10 +102,9 @@ func (ids *mempoolIDs) Reclaim(peerID p2p.ID) uint16 { // GetIDForPeer returns the shorthand ID reserved for the peer. func (ids *mempoolIDs) GetIDForPeer(peerID p2p.ID) uint16 { - ids.mtx.Lock() - defer ids.mtx.Unlock() - + ids.mtx.RLock() id, exists := ids.peerMap[peerID] + ids.mtx.RUnlock() if !exists { id = ids.getIDSafe(peerID) } From c5ada6b4b0659cf52a86d484b7905ef964cc70c2 Mon Sep 17 00:00:00 2001 From: evan-forbes <evan.samuel.forbes@gmail.com> Date: Wed, 2 Oct 2024 19:43:25 -0500 Subject: [PATCH 106/127] chore: don't clear the seenBySet if a peer doesn't respond --- mempool/cat/pool.go | 4 +++- mempool/cat/reactor.go | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/mempool/cat/pool.go b/mempool/cat/pool.go index 39be8155aa..fee24522f8 100644 --- a/mempool/cat/pool.go +++ b/mempool/cat/pool.go @@ -34,7 +34,7 @@ var ( // before it is included in the block. const ( // InclusionDelay = 2 * time.Second - SeenSetPruneInterval = 100 * time.Minute + SeenSetPruneInterval = 30 * time.Minute ) var ( @@ -609,6 +609,8 @@ func (txmp *TxPool) Update( keys := make([]types.TxKey, len(blockTxs)) for idx, tx := range blockTxs { keys[idx] = tx.Key() + // txmp.seenByPeersSet.RemoveKey(keys[idx]) + txmp.rejectedTxCache.Push(keys[idx]) } txmp.store.markAsCommitted(keys) diff --git a/mempool/cat/reactor.go b/mempool/cat/reactor.go index 8b60eb5772..e2e8de105d 100644 --- a/mempool/cat/reactor.go +++ b/mempool/cat/reactor.go @@ -611,7 +611,7 @@ func (memR *Reactor) findNewPeerToRequestTx(txKey types.TxKey, tries int) { peer := memR.ids.GetPeer(peerID) if peer == nil { // we disconnected from that peer, retry again until we exhaust the list - memR.mempool.seenByPeersSet.Remove(txKey, peerID) + // memR.mempool.seenByPeersSet.Remove(txKey, peerID) memR.findNewPeerToRequestTx(txKey, tries-1) } else { memR.mempool.metrics.RerequestedTxs.Add(1) From 5a82599f39ff8d4c09ccd7df11dac6d776179770 Mon Sep 17 00:00:00 2001 From: evan-forbes <evan.samuel.forbes@gmail.com> Date: Thu, 3 Oct 2024 00:25:20 -0500 Subject: [PATCH 107/127] fix: add upload traces for seen and want txs --- mempool/cat/pool.go | 1 + mempool/cat/reactor.go | 17 +++++++++++------ mempool/cat/store.go | 3 +++ pkg/trace/schema/mempool.go | 3 +++ 4 files changed, 18 insertions(+), 6 deletions(-) diff --git a/mempool/cat/pool.go b/mempool/cat/pool.go index fee24522f8..c64fecfa4d 100644 --- a/mempool/cat/pool.go +++ b/mempool/cat/pool.go @@ -720,6 +720,7 @@ func (txmp *TxPool) addNewTransaction(wtx *wrappedTx, checkTxRes *abci.ResponseC } func (txmp *TxPool) evictTx(wtx *wrappedTx) { + fmt.Println("EVICTING TX NO NO NO NO BAD STOP") txmp.store.remove(wtx.key) txmp.evictedTxCache.Push(wtx.key) txmp.logger.Error("tx evicted") diff --git a/mempool/cat/reactor.go b/mempool/cat/reactor.go index e2e8de105d..75ca2422c9 100644 --- a/mempool/cat/reactor.go +++ b/mempool/cat/reactor.go @@ -369,6 +369,7 @@ func (memR *Reactor) ReceiveEnvelope(e p2p.Envelope) { schema.SeenTx, txKey[:], schema.Download, + msg.Peer, ) peerID := memR.ids.GetIDForPeer(p2p.ID(msg.Peer)) success := memR.mempool.PeerHasTx(peerID, txKey) @@ -409,6 +410,7 @@ func (memR *Reactor) ReceiveEnvelope(e p2p.Envelope) { schema.WantTx, txKey[:], schema.Download, + "", ) tx, has := memR.mempool.GetTxByKey(txKey) // TODO: consider handling the case where we receive a HasTx message from a peer @@ -498,6 +500,8 @@ func (memR *Reactor) broadcastSeenTx(txKey types.TxKey, from string) { if !peer.Send(MempoolStateChannel, bz) { memR.Logger.Error("failed to send seen tx to peer", "peerID", peer.ID(), "txKey", txKey) + } else { + schema.WriteMempoolPeerState(memR.traceClient, string(peer.ID()), schema.SeenTx, txKey[:], schema.Upload, from) } } // memR.Logger.Debug("broadcasted seen tx to all peers", "tx_key", txKey.String()) @@ -559,12 +563,13 @@ func (memR *Reactor) requestTx(txKey types.TxKey, peer p2p.Peer, tries int) { success := peer.Send(MempoolStateChannel, bz) //nolint:staticcheck if success { - memR.Logger.Debug("requested transaction", "txKey", txKey, "peerID", peer.ID()) - memR.mempool.metrics.RequestedTxs.Add(1) - requested := memR.requests.Add(txKey, memR.ids.GetIDForPeer(peer.ID()), memR.findNewPeerToRequestTx, tries) - if !requested { - memR.Logger.Debug("have already marked a tx as requested", "txKey", txKey, "peerID", peer.ID()) - } + // memR.Logger.Debug("requested transaction", "txKey", txKey, "peerID", peer.ID()) + // memR.mempool.metrics.RequestedTxs.Add(1) + schema.WriteMempoolPeerState(memR.traceClient, string(peer.ID()), schema.WantTx, txKey[:], schema.Upload, "") + memR.requests.Add(txKey, memR.ids.GetIDForPeer(peer.ID()), memR.findNewPeerToRequestTx, tries) + // if !requested { + // memR.Logger.Debug("have already marked a tx as requested", "txKey", txKey, "peerID", peer.ID()) + // } } else { memR.Logger.Error("failed to send message to request transaction", "txKey", txKey, "peerID", peer.ID()) } diff --git a/mempool/cat/store.go b/mempool/cat/store.go index aa72e9318d..5d7918695e 100644 --- a/mempool/cat/store.go +++ b/mempool/cat/store.go @@ -1,6 +1,7 @@ package cat import ( + "fmt" "sync" "time" @@ -98,6 +99,7 @@ func (s *store) remove(txKey types.TxKey) bool { return false } s.bytes -= tx.size() + fmt.Println("DELETING TX IS THIS ALLOWED?") delete(s.txs, txKey) return true } @@ -213,6 +215,7 @@ func (s *store) purgeExpiredTxs(expirationHeight int64, expirationAge time.Time) for key, tx := range s.txs { if tx.height < expirationHeight || tx.timestamp.Before(expirationAge) { s.bytes -= tx.size() + fmt.Println("DELETING TX EXCUSE ME NO NO NO NO NO") delete(s.txs, key) purgedTxs = append(purgedTxs, tx) counter++ diff --git a/pkg/trace/schema/mempool.go b/pkg/trace/schema/mempool.go index 118a91d3b4..f8115934ac 100644 --- a/pkg/trace/schema/mempool.go +++ b/pkg/trace/schema/mempool.go @@ -71,6 +71,7 @@ type MempoolPeerState struct { StateUpdate MempoolStateUpdateType `json:"state_update"` TxHash string `json:"tx_hash"` TransferType TransferType `json:"transfer_type"` + Validator string `json:"validator,omitempty"` // originating valiator for seenTxs } // Table returns the table name for the MempoolPeerState struct. @@ -86,6 +87,7 @@ func WriteMempoolPeerState( stateUpdate MempoolStateUpdateType, txHash []byte, transferType TransferType, + validator string, ) { // this check is redundant to what is checked during client.Write, although it // is an optimization to avoid allocations from creating the map of fields. @@ -97,6 +99,7 @@ func WriteMempoolPeerState( StateUpdate: stateUpdate, TransferType: transferType, TxHash: bytes.HexBytes(txHash).String(), + Validator: validator, }) } From 7d9fce855dffcf5a441c88df811e79a2ad841ff9 Mon Sep 17 00:00:00 2001 From: evan-forbes <evan.samuel.forbes@gmail.com> Date: Thu, 3 Oct 2024 00:32:21 -0500 Subject: [PATCH 108/127] chore: add some stuff --- mempool/cat/reactor.go | 1 + 1 file changed, 1 insertion(+) diff --git a/mempool/cat/reactor.go b/mempool/cat/reactor.go index 75ca2422c9..a4d0aa900c 100644 --- a/mempool/cat/reactor.go +++ b/mempool/cat/reactor.go @@ -377,6 +377,7 @@ func (memR *Reactor) ReceiveEnvelope(e p2p.Envelope) { if !success { return } + fmt.Println("peer has tx", msg.Peer, types.TxKey(msg.TxKey).String()) go memR.broadcastSeenTx(types.TxKey(msg.TxKey), msg.Peer) // Check if we don't already have the transaction and that it was recently rejected From a9ab5a697f01c47b21bd6ceff23f8e4d9827e7fa Mon Sep 17 00:00:00 2001 From: evan-forbes <evan.samuel.forbes@gmail.com> Date: Thu, 3 Oct 2024 02:02:38 -0500 Subject: [PATCH 109/127] chore: revert changes --- mempool/cat/pool.go | 2 +- mempool/cat/reactor.go | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/mempool/cat/pool.go b/mempool/cat/pool.go index c64fecfa4d..4fd12cbde8 100644 --- a/mempool/cat/pool.go +++ b/mempool/cat/pool.go @@ -609,7 +609,7 @@ func (txmp *TxPool) Update( keys := make([]types.TxKey, len(blockTxs)) for idx, tx := range blockTxs { keys[idx] = tx.Key() - // txmp.seenByPeersSet.RemoveKey(keys[idx]) + txmp.seenByPeersSet.RemoveKey(keys[idx]) txmp.rejectedTxCache.Push(keys[idx]) } txmp.store.markAsCommitted(keys) diff --git a/mempool/cat/reactor.go b/mempool/cat/reactor.go index a4d0aa900c..50f8236f17 100644 --- a/mempool/cat/reactor.go +++ b/mempool/cat/reactor.go @@ -377,7 +377,6 @@ func (memR *Reactor) ReceiveEnvelope(e p2p.Envelope) { if !success { return } - fmt.Println("peer has tx", msg.Peer, types.TxKey(msg.TxKey).String()) go memR.broadcastSeenTx(types.TxKey(msg.TxKey), msg.Peer) // Check if we don't already have the transaction and that it was recently rejected @@ -617,7 +616,7 @@ func (memR *Reactor) findNewPeerToRequestTx(txKey types.TxKey, tries int) { peer := memR.ids.GetPeer(peerID) if peer == nil { // we disconnected from that peer, retry again until we exhaust the list - // memR.mempool.seenByPeersSet.Remove(txKey, peerID) + memR.mempool.seenByPeersSet.Remove(txKey, peerID) memR.findNewPeerToRequestTx(txKey, tries-1) } else { memR.mempool.metrics.RerequestedTxs.Add(1) From 1d3f20560fe62420666ec00f8c6a2a2a9c8ce130 Mon Sep 17 00:00:00 2001 From: evan-forbes <evan.samuel.forbes@gmail.com> Date: Thu, 3 Oct 2024 18:46:41 -0500 Subject: [PATCH 110/127] feat: trace all missing tx hashes --- mempool/cat/block_builder.go | 6 ++++-- mempool/cat/pool.go | 1 + pkg/trace/schema/mempool.go | 18 +++++++++++------- 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/mempool/cat/block_builder.go b/mempool/cat/block_builder.go index 3a6a73458e..1f39cb47cb 100644 --- a/mempool/cat/block_builder.go +++ b/mempool/cat/block_builder.go @@ -52,7 +52,7 @@ func (memR *Reactor) FetchTxsFromKeys(ctx context.Context, blockID []byte, compa 0, len(compactData), 0, - blockID, + nil, ) return txs, nil } @@ -68,13 +68,15 @@ func (memR *Reactor) FetchTxsFromKeys(ctx context.Context, blockID []byte, compa defer func() { timeTaken := request.TimeTaken() memR.Logger.Info("fetched txs", "timeTaken", timeTaken, "numRetrieved", initialNumMissing-len(request.missingKeys), "numMissing", len(request.missingKeys)) + missingKeys := make([][]byte, len(request.missingKeys)) + schema.WriteMempoolRecoveryStats( memR.traceClient, initialNumMissing, initialNumMissing-len(request.missingKeys), len(compactData), timeTaken, - blockID, + missingKeys, ) memR.mempool.metrics.RecoveryRate.Observe(float64(initialNumMissing-len(request.missingKeys)) / float64(initialNumMissing)) }() diff --git a/mempool/cat/pool.go b/mempool/cat/pool.go index 4fd12cbde8..f51c6fca4a 100644 --- a/mempool/cat/pool.go +++ b/mempool/cat/pool.go @@ -373,6 +373,7 @@ func (txmp *TxPool) TryAddNewTx(tx types.Tx, key types.TxKey, txInfo mempool.TxI if rsp.Code != abci.CodeTypeOK { if txmp.config.KeepInvalidTxsInCache { txmp.rejectedTxCache.Push(key) + fmt.Println("tx rejected!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!") } txmp.metrics.FailedTxs.Add(1) // we don't return an error when there has been a fail code. Instead the diff --git a/pkg/trace/schema/mempool.go b/pkg/trace/schema/mempool.go index f8115934ac..6d56cdf63c 100644 --- a/pkg/trace/schema/mempool.go +++ b/pkg/trace/schema/mempool.go @@ -108,11 +108,11 @@ const ( ) type MempoolRecovery struct { - Missing int `json:"missing"` - Recovered int `json:"recovered"` - Total int `json:"total"` - TimeTaken uint64 `json:"time_taken"` - BlockID string `json:"block_id"` + Missing int `json:"missing"` + Recovered int `json:"recovered"` + Total int `json:"total"` + TimeTaken uint64 `json:"time_taken"` + Hashes []string `json:"hashes"` } func (m MempoolRecovery) Table() string { @@ -125,13 +125,17 @@ func WriteMempoolRecoveryStats( recovered int, total int, timeTaken uint64, - blockID []byte, + hashes [][]byte, ) { + txs := make([]string, len(hashes)) + for i, hash := range hashes { + txs[i] = bytes.HexBytes(hash).String() + } client.Write(MempoolRecovery{ Missing: missing, Recovered: recovered, Total: total, TimeTaken: timeTaken, - BlockID: bytes.HexBytes(blockID).String(), + Hashes: txs, }) } From 852602b7ac2e5fe0a166bdbab745b9ed3842d851 Mon Sep 17 00:00:00 2001 From: evan-forbes <evan.samuel.forbes@gmail.com> Date: Thu, 3 Oct 2024 18:47:12 -0500 Subject: [PATCH 111/127] fix: don't gossip a tx as seen if we don't add it to the mempool --- mempool/cat/reactor.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mempool/cat/reactor.go b/mempool/cat/reactor.go index 50f8236f17..838d95bec8 100644 --- a/mempool/cat/reactor.go +++ b/mempool/cat/reactor.go @@ -314,7 +314,7 @@ func (memR *Reactor) ReceiveEnvelope(e p2p.Envelope) { memR.Logger.Info("Could not add tx from peer", "peerID", peerID, "txKey", key, "err", err) return } - if !memR.opts.ListenOnly { + if !memR.opts.ListenOnly && err == nil { // We broadcast only transactions that we deem valid and actually have in our mempool. memR.broadcastSeenTx(key, string(memR.self)) } From 12da7e1686c2e5cde391ddd23b876e388a2fcd69 Mon Sep 17 00:00:00 2001 From: evan-forbes <evan.samuel.forbes@gmail.com> Date: Thu, 3 Oct 2024 19:17:33 -0500 Subject: [PATCH 112/127] chore: trace rejected txs --- mempool/cat/reactor.go | 20 ++++++++------------ pkg/trace/schema/mempool.go | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 12 deletions(-) diff --git a/mempool/cat/reactor.go b/mempool/cat/reactor.go index 838d95bec8..cbff72abe0 100644 --- a/mempool/cat/reactor.go +++ b/mempool/cat/reactor.go @@ -7,6 +7,7 @@ import ( "github.com/gogo/protobuf/proto" + abci "github.com/tendermint/tendermint/abci/types" cfg "github.com/tendermint/tendermint/config" "github.com/tendermint/tendermint/crypto/tmhash" "github.com/tendermint/tendermint/libs/log" @@ -278,7 +279,6 @@ func (memR *Reactor) ReceiveEnvelope(e p2p.Envelope) { txInfo := mempool.TxInfo{SenderID: peerID} txInfo.SenderP2PID = e.Src.ID() - var err error for _, tx := range protoTxs { ntx := types.Tx(tx) key := [32]byte{} @@ -305,20 +305,16 @@ func (memR *Reactor) ReceiveEnvelope(e p2p.Envelope) { memR.blockFetcher.TryAddMissingTx(key, tx) // Now attempt to add the tx to the mempool. - _, err = memR.mempool.TryAddNewTx(ntx, key, txInfo, isBlobTx) - if err != nil && err != ErrTxInMempool && err != ErrTxRecentlyCommitted { - if memR.blockFetcher.IsMissingTx(key) { - memR.Logger.Error("tx in block is not valid by mempool") - } - - memR.Logger.Info("Could not add tx from peer", "peerID", peerID, "txKey", key, "err", err) - return - } - if !memR.opts.ListenOnly && err == nil { + rsp, err := memR.mempool.TryAddNewTx(ntx, key, txInfo, isBlobTx) + if err != nil || rsp.Code != abci.CodeTypeOK { + memR.Logger.Error("Could not add tx from peer", "peerID", peerID, "txKey", key, "err", err) // We broadcast only transactions that we deem valid and actually have in our mempool. - memR.broadcastSeenTx(key, string(memR.self)) + schema.WriteMempoolRejected(memR.traceClient, string(e.Src.ID()), key[:], uint64(rsp.Code), err) + return } + memR.broadcastSeenTx(key, string(memR.self)) + go func(tx []byte, key types.TxKey) { for i := 0; i < 5; i++ { wants, has := memR.wantState.GetWants(key) diff --git a/pkg/trace/schema/mempool.go b/pkg/trace/schema/mempool.go index 6d56cdf63c..3dcaab1ed1 100644 --- a/pkg/trace/schema/mempool.go +++ b/pkg/trace/schema/mempool.go @@ -11,6 +11,7 @@ func MempoolTables() []string { MempoolTxTable, MempoolPeerStateTable, MempoolRecoveryTable, + MempoolRejectedTable, } } @@ -105,6 +106,7 @@ func WriteMempoolPeerState( const ( MempoolRecoveryTable = "mempool_recovery" + MempoolRejectedTable = "mempool_rejected" ) type MempoolRecovery struct { @@ -139,3 +141,38 @@ func WriteMempoolRecoveryStats( Hashes: txs, }) } + +type MempoolRejected struct { + Code uint64 `json:"code"` + Error string `json:"error"` + TxHash string `json:"tx_hash"` + Peer string `json:"peer"` +} + +func (m MempoolRejected) Table() string { + return MempoolRejectedTable +} + +func WriteMempoolRejected( + client trace.Tracer, + peer string, + txHash []byte, + code uint64, + err error, +) { + // this check is redundant to what is checked during client.Write, although it + // is an optimization to avoid allocations from creating the map of fields. + if !client.IsCollecting(MempoolRejectedTable) { + return + } + var errS string + if err != nil { + errS = err.Error() + } + client.Write(MempoolRejected{ + Peer: peer, + Code: code, + TxHash: bytes.HexBytes(txHash).String(), + Error: errS, + }) +} From dc5a2ab1f34922541a9e1ff8757106dc8b318bcd Mon Sep 17 00:00:00 2001 From: evan-forbes <evan.samuel.forbes@gmail.com> Date: Thu, 3 Oct 2024 19:42:01 -0500 Subject: [PATCH 113/127] fix: panic --- mempool/cat/reactor.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/mempool/cat/reactor.go b/mempool/cat/reactor.go index cbff72abe0..c4d2ed0e29 100644 --- a/mempool/cat/reactor.go +++ b/mempool/cat/reactor.go @@ -309,7 +309,11 @@ func (memR *Reactor) ReceiveEnvelope(e p2p.Envelope) { if err != nil || rsp.Code != abci.CodeTypeOK { memR.Logger.Error("Could not add tx from peer", "peerID", peerID, "txKey", key, "err", err) // We broadcast only transactions that we deem valid and actually have in our mempool. - schema.WriteMempoolRejected(memR.traceClient, string(e.Src.ID()), key[:], uint64(rsp.Code), err) + code := uint32(69) + if rsp != nil { + code = rsp.Code + } + schema.WriteMempoolRejected(memR.traceClient, string(e.Src.ID()), key[:], uint64(code), err) return } From 3d652d0d02015a6a04e4b9b0360b2eca1b7fea15 Mon Sep 17 00:00:00 2001 From: evan-forbes <evan.samuel.forbes@gmail.com> Date: Fri, 4 Oct 2024 01:34:15 -0500 Subject: [PATCH 114/127] fix: tracing and stop broadcasting on every checktx call --- mempool/cat/block_builder.go | 65 ++++++++++++++++++++++++++++++++++-- mempool/cat/pool.go | 6 ++-- mempool/cat/reactor.go | 7 ++-- mempool/v1/reactor.go | 2 ++ pkg/trace/schema/mempool.go | 12 +++---- pkg/trace/schema/schema.go | 3 ++ 6 files changed, 80 insertions(+), 15 deletions(-) diff --git a/mempool/cat/block_builder.go b/mempool/cat/block_builder.go index 1f39cb47cb..8af17baffb 100644 --- a/mempool/cat/block_builder.go +++ b/mempool/cat/block_builder.go @@ -7,6 +7,7 @@ import ( "sync" "time" + "github.com/tendermint/tendermint/libs/bytes" "github.com/tendermint/tendermint/pkg/trace/schema" "github.com/tendermint/tendermint/types" ) @@ -65,10 +66,14 @@ func (memR *Reactor) FetchTxsFromKeys(ctx context.Context, blockID []byte, compa missingKeys, txs, ) + defer func() { timeTaken := request.TimeTaken() - memR.Logger.Info("fetched txs", "timeTaken", timeTaken, "numRetrieved", initialNumMissing-len(request.missingKeys), "numMissing", len(request.missingKeys)) - missingKeys := make([][]byte, len(request.missingKeys)) + + missingTxs := make([]string, len(request.missingKeys)) + for i, tx := range missingKeys { + missingTxs[i] = bytes.HexBytes(tx[:]).String() + } schema.WriteMempoolRecoveryStats( memR.traceClient, @@ -76,8 +81,10 @@ func (memR *Reactor) FetchTxsFromKeys(ctx context.Context, blockID []byte, compa initialNumMissing-len(request.missingKeys), len(compactData), timeTaken, - missingKeys, + missingTxs, ) + + memR.Logger.Info("fetched txs", "timeTaken", timeTaken, "numRetrieved", initialNumMissing-len(request.missingKeys), "numMissing", len(request.missingKeys)) memR.mempool.metrics.RecoveryRate.Observe(float64(initialNumMissing-len(request.missingKeys)) / float64(initialNumMissing)) }() @@ -92,6 +99,58 @@ func (memR *Reactor) FetchTxsFromKeys(ctx context.Context, blockID []byte, compa return request.WaitForBlock(ctx) } +func (memR *Reactor) FetchTxsFromKeysSync(compactData [][]byte) ([][]byte, error) { + txs := make([][]byte, len(compactData)) + missingKeys := make(map[int]types.TxKey, len(compactData)) + + // iterate through the keys to know what transactions we have and what are missing + for i, key := range compactData { + txKey, err := types.TxKeyFromBytes(key) + if err != nil { + return nil, fmt.Errorf("incorrect compact blocks format: %w", err) + } + wtx := memR.mempool.store.get(txKey) + if wtx != nil { + txs[i] = wtx.tx + memR.mempool.store.markAsUnevictable(txKey) + } else { + missingKeys[i] = txKey + } + } + memR.Logger.Info("fetching transactions from peers", "numTxs", len(txs), "numMissing", len(missingKeys)) + memR.mempool.metrics.MissingTxs.Add(float64(len(missingKeys))) + + // Check if we got lucky and already had all the transactions. + if len(missingKeys) == 0 { + schema.WriteMempoolRecoveryStats( + memR.traceClient, + 0, + 0, + len(compactData), + 0, + nil, + ) + return txs, nil + } + + missingTxs := make([]string, len(missingKeys)) + for i, tx := range missingKeys { + missingTxs[i] = bytes.HexBytes(tx[:]).String() + } + + schema.WriteMempoolRecoveryStats( + memR.traceClient, + len(missingKeys), + len(missingKeys), + len(compactData), + 0, + missingTxs, + ) + + // Wait for the reactor to retrieve and post all transactions. + return nil, fmt.Errorf("missing transaction: %d", len(missingTxs)) +} + // FetchKeysFromTxs is in many ways the opposite method. It takes a full block generated by the application // and reduces it to the set of keys that need to be gossiped from one mempool to another nodes mempool // in order to recreate the full block. diff --git a/mempool/cat/pool.go b/mempool/cat/pool.go index f51c6fca4a..c7fb9c0571 100644 --- a/mempool/cat/pool.go +++ b/mempool/cat/pool.go @@ -41,7 +41,7 @@ var ( // set the default to 5, but this value can be changed in an init func InclusionDelay = 5 * time.Second peerCount = atomic.Int32{} - defaultSeenLimit = 82 + defaultSeenLimit = 2 ) // TxPoolOption sets an optional parameter on the TxPool. @@ -277,7 +277,9 @@ func (txmp *TxPool) CheckTx(tx types.Tx, cb func(*abci.Response), txInfo mempool }() // push to the broadcast queue that a new transaction is ready - txmp.markToBeBroadcast(key) + if txInfo.SenderID == 0 { + txmp.markToBeBroadcast(key) + } return nil } diff --git a/mempool/cat/reactor.go b/mempool/cat/reactor.go index c4d2ed0e29..d2b2deee25 100644 --- a/mempool/cat/reactor.go +++ b/mempool/cat/reactor.go @@ -281,7 +281,7 @@ func (memR *Reactor) ReceiveEnvelope(e p2p.Envelope) { for _, tx := range protoTxs { ntx := types.Tx(tx) - key := [32]byte{} + var key types.TxKey // manually calculate the key to determine is its a blobtx while we're at it blobTx, isBlobTx := types.UnmarshalBlobTx(tx) if isBlobTx { @@ -289,7 +289,7 @@ func (memR *Reactor) ReceiveEnvelope(e p2p.Envelope) { } else { key = sha256.Sum256(tx) } - schema.WriteMempoolTx(memR.traceClient, string(e.Src.ID()), key[:], schema.Download) + schema.WriteMempoolTx(memR.traceClient, string(e.Src.ID()), key[:], len(ntx), schema.Download) // If we requested the transaction we mark it as received. if memR.requests.Has(peerID, key) { memR.requests.MarkReceived(peerID, key) @@ -338,6 +338,7 @@ func (memR *Reactor) ReceiveEnvelope(e p2p.Envelope) { memR.traceClient, string(p.ID()), key[:], + len(tx), schema.Upload, ) } @@ -428,6 +429,7 @@ func (memR *Reactor) ReceiveEnvelope(e p2p.Envelope) { memR.traceClient, string(e.Src.ID()), txKey[:], + len(tx), schema.Upload, ) } @@ -538,6 +540,7 @@ func (memR *Reactor) broadcastNewTx(wtx *wrappedTx) { if peer.Send(mempool.MempoolChannel, bz) { //nolint:staticcheck // memR.mempool.PeerHasTx(id, wtx.key) + schema.WriteMempoolTx(memR.traceClient, string(peer.ID()), wtx.key[:], len(wtx.tx), schema.UploadBroadcast) } else { memR.Logger.Error("failed to send new tx to peer", "peerID", peer.ID(), "txKey", wtx.key) } diff --git a/mempool/v1/reactor.go b/mempool/v1/reactor.go index 2ecfc51a5f..d85adc414c 100644 --- a/mempool/v1/reactor.go +++ b/mempool/v1/reactor.go @@ -197,6 +197,7 @@ func (memR *Reactor) ReceiveEnvelope(e p2p.Envelope) { memR.traceClient, string(e.Src.ID()), ntx.Hash(), + len(ntx), schema.Download, ) err = memR.mempool.CheckTx(ntx, nil, txInfo) @@ -303,6 +304,7 @@ func (memR *Reactor) broadcastTxRoutine(peer p2p.Peer) { memR.traceClient, string(peer.ID()), memTx.tx.Hash(), + len(memTx.tx), schema.Upload, ) } diff --git a/pkg/trace/schema/mempool.go b/pkg/trace/schema/mempool.go index 3dcaab1ed1..064ab606b0 100644 --- a/pkg/trace/schema/mempool.go +++ b/pkg/trace/schema/mempool.go @@ -37,7 +37,7 @@ func (m MempoolTx) Table() string { // WriteMempoolTx writes a tracing point for a tx using the predetermined // schema for mempool tracing. -func WriteMempoolTx(client trace.Tracer, peer string, txHash []byte, transferType TransferType) { +func WriteMempoolTx(client trace.Tracer, peer string, txHash []byte, size int, transferType TransferType) { // this check is redundant to what is checked during client.Write, although it // is an optimization to avoid allocations from the map of fields. if !client.IsCollecting(MempoolTxTable) { @@ -46,7 +46,7 @@ func WriteMempoolTx(client trace.Tracer, peer string, txHash []byte, transferTyp client.Write(MempoolTx{ TxHash: bytes.HexBytes(txHash).String(), Peer: peer, - Size: len(txHash), + Size: size, TransferType: transferType, }) } @@ -127,18 +127,14 @@ func WriteMempoolRecoveryStats( recovered int, total int, timeTaken uint64, - hashes [][]byte, + hashes []string, ) { - txs := make([]string, len(hashes)) - for i, hash := range hashes { - txs[i] = bytes.HexBytes(hash).String() - } client.Write(MempoolRecovery{ Missing: missing, Recovered: recovered, Total: total, TimeTaken: timeTaken, - Hashes: txs, + Hashes: hashes, }) } diff --git a/pkg/trace/schema/schema.go b/pkg/trace/schema/schema.go index c0f2316787..132c94ff50 100644 --- a/pkg/trace/schema/schema.go +++ b/pkg/trace/schema/schema.go @@ -28,6 +28,7 @@ type TransferType int const ( Download TransferType = iota Upload + UploadBroadcast ) func (t TransferType) String() string { @@ -36,6 +37,8 @@ func (t TransferType) String() string { return "download" case Upload: return "upload" + case UploadBroadcast: + return "broadcast" default: return "unknown" } From 6fe30d4716ba4303602b5e552841345e715df020 Mon Sep 17 00:00:00 2001 From: evan-forbes <evan.samuel.forbes@gmail.com> Date: Fri, 4 Oct 2024 01:49:48 -0500 Subject: [PATCH 115/127] chore: make the default seen limit higher --- mempool/cat/pool.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/mempool/cat/pool.go b/mempool/cat/pool.go index c7fb9c0571..e077cddb26 100644 --- a/mempool/cat/pool.go +++ b/mempool/cat/pool.go @@ -41,7 +41,7 @@ var ( // set the default to 5, but this value can be changed in an init func InclusionDelay = 5 * time.Second peerCount = atomic.Int32{} - defaultSeenLimit = 2 + defaultSeenLimit = 82 ) // TxPoolOption sets an optional parameter on the TxPool. @@ -470,8 +470,6 @@ func (txmp *TxPool) seenEntries(seenLimit int) []*wrappedTx { if seen >= seenLimit { tx.seenCount = seen prunedTxs = append(prunedTxs, tx) - // treat non-blob txs as special since they propagate a lot faster and - // we need to get txsim off the ground } } @@ -520,7 +518,7 @@ func (txmp *TxPool) ReapMaxBytesMaxGas(maxBytes, maxGas int64) (types.Txs, []typ } totalBytes += int64(txBytes) totalGas += w.gasWanted - // txmp.store.markAsUnevictable(w.key) + txmp.store.markAsUnevictable(w.key) keep = append(keep, w.tx) keys = append(keys, w.key) } From 7f98e686a2c3d35032783cc3c2911055a5f5190e Mon Sep 17 00:00:00 2001 From: evan-forbes <evan.samuel.forbes@gmail.com> Date: Fri, 4 Oct 2024 18:35:09 -0500 Subject: [PATCH 116/127] fix: try to fix some tracing redundant txs and recovery --- mempool/cat/block_builder.go | 13 ++++++------- mempool/cat/pool.go | 2 +- mempool/cat/reactor.go | 19 +++++++++++++------ 3 files changed, 20 insertions(+), 14 deletions(-) diff --git a/mempool/cat/block_builder.go b/mempool/cat/block_builder.go index 8af17baffb..cc3b618e80 100644 --- a/mempool/cat/block_builder.go +++ b/mempool/cat/block_builder.go @@ -58,6 +58,10 @@ func (memR *Reactor) FetchTxsFromKeys(ctx context.Context, blockID []byte, compa return txs, nil } initialNumMissing := len(missingKeys) + missingTxs := make([]string, len(missingKeys)) + for i, tx := range missingKeys { + missingTxs[i] = bytes.HexBytes(tx[:]).String() + } // setup a request for this block and begin to track and retrieve all missing transactions request := memR.blockFetcher.newRequest( @@ -67,14 +71,9 @@ func (memR *Reactor) FetchTxsFromKeys(ctx context.Context, blockID []byte, compa txs, ) - defer func() { + defer func(missingTxs []string) { timeTaken := request.TimeTaken() - missingTxs := make([]string, len(request.missingKeys)) - for i, tx := range missingKeys { - missingTxs[i] = bytes.HexBytes(tx[:]).String() - } - schema.WriteMempoolRecoveryStats( memR.traceClient, initialNumMissing, @@ -86,7 +85,7 @@ func (memR *Reactor) FetchTxsFromKeys(ctx context.Context, blockID []byte, compa memR.Logger.Info("fetched txs", "timeTaken", timeTaken, "numRetrieved", initialNumMissing-len(request.missingKeys), "numMissing", len(request.missingKeys)) memR.mempool.metrics.RecoveryRate.Observe(float64(initialNumMissing-len(request.missingKeys)) / float64(initialNumMissing)) - }() + }(missingTxs) // request the missing transactions if we haven't already for _, key := range missingKeys { diff --git a/mempool/cat/pool.go b/mempool/cat/pool.go index e077cddb26..ecec9644e6 100644 --- a/mempool/cat/pool.go +++ b/mempool/cat/pool.go @@ -41,7 +41,7 @@ var ( // set the default to 5, but this value can be changed in an init func InclusionDelay = 5 * time.Second peerCount = atomic.Int32{} - defaultSeenLimit = 82 + defaultSeenLimit = 0 ) // TxPoolOption sets an optional parameter on the TxPool. diff --git a/mempool/cat/reactor.go b/mempool/cat/reactor.go index d2b2deee25..aeff58d247 100644 --- a/mempool/cat/reactor.go +++ b/mempool/cat/reactor.go @@ -378,10 +378,15 @@ func (memR *Reactor) ReceiveEnvelope(e p2p.Envelope) { if !success { return } + + if memR.mempool.IsRejectedTx(txKey) || memR.mempool.store.hasCommitted(txKey) { + return + } + go memR.broadcastSeenTx(types.TxKey(msg.TxKey), msg.Peer) // Check if we don't already have the transaction and that it was recently rejected - if memR.mempool.Has(txKey) || memR.mempool.IsRejectedTx(txKey) || memR.mempool.store.hasCommitted(txKey) { + if memR.mempool.Has(txKey) { // memR.Logger.Debug("received a seen tx for a tx we already have", "txKey", txKey) return } @@ -434,11 +439,13 @@ func (memR *Reactor) ReceiveEnvelope(e p2p.Envelope) { ) } } else { - memR.ids.mtx.RLock() - peer, has := memR.ids.peerMap[e.Src.ID()] - memR.ids.mtx.RUnlock() - if has { - memR.wantState.Add(txKey, peer) + if !memR.mempool.IsRejectedTx(txKey) && !memR.mempool.store.hasCommitted(txKey) { + memR.ids.mtx.RLock() + peer, has := memR.ids.peerMap[e.Src.ID()] + memR.ids.mtx.RUnlock() + if has { + memR.wantState.Add(txKey, peer) + } } } From 33ef7c1bb229f9cc528ff39b7ae116a9e4d2da31 Mon Sep 17 00:00:00 2001 From: evan-forbes <evan.samuel.forbes@gmail.com> Date: Fri, 4 Oct 2024 21:18:20 -0500 Subject: [PATCH 117/127] fix: sending the same tx repeatedly to many folks --- mempool/cat/block_builder.go | 11 +++-- mempool/cat/peers.go | 11 +++++ mempool/cat/pool.go | 2 +- mempool/cat/reactor.go | 86 ++++++++++++++++++++++-------------- mempool/cat/wants.go | 13 +++--- types/tx.go | 15 +++++-- 6 files changed, 92 insertions(+), 46 deletions(-) diff --git a/mempool/cat/block_builder.go b/mempool/cat/block_builder.go index cc3b618e80..b53fdab2f4 100644 --- a/mempool/cat/block_builder.go +++ b/mempool/cat/block_builder.go @@ -26,12 +26,15 @@ func (memR *Reactor) FetchTxsFromKeys(ctx context.Context, blockID []byte, compa } txs := make([][]byte, len(compactData)) - missingKeys := make(map[int]types.TxKey, len(compactData)) + missingKeys := make(map[int]types.TxKey) // iterate through the keys to know what transactions we have and what are missing for i, key := range compactData { txKey, err := types.TxKeyFromBytes(key) if err != nil { + fmt.Println("INCORRECT COMPLACT BLONKS ERROOR!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!") + fmt.Println("INCORRECT COMPLACT BLONKS ERROOR!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!") + panic(err) return nil, fmt.Errorf("incorrect compact blocks format: %w", err) } wtx := memR.mempool.store.get(txKey) @@ -59,8 +62,10 @@ func (memR *Reactor) FetchTxsFromKeys(ctx context.Context, blockID []byte, compa } initialNumMissing := len(missingKeys) missingTxs := make([]string, len(missingKeys)) - for i, tx := range missingKeys { - missingTxs[i] = bytes.HexBytes(tx[:]).String() + cursor := 0 + for _, tx := range missingKeys { + missingTxs[cursor] = bytes.HexBytes(tx[:]).String() + cursor++ } // setup a request for this block and begin to track and retrieve all missing transactions diff --git a/mempool/cat/peers.go b/mempool/cat/peers.go index 2456cdce0a..b475d5c888 100644 --- a/mempool/cat/peers.go +++ b/mempool/cat/peers.go @@ -29,6 +29,17 @@ func newMempoolIDs() *mempoolIDs { } } +func (ids *mempoolIDs) getPeerFromID(id p2p.ID) (p2p.Peer, bool) { + ids.mtx.RLock() + defer ids.mtx.RUnlock() + for _, peer := range ids.activeIDs { + if peer.ID() == id { + return peer, true + } + } + return nil, false +} + // ReserveForPeer searches for the next unused ID and assigns it to the // peer. func (ids *mempoolIDs) ReserveForPeer(peer p2p.Peer) { diff --git a/mempool/cat/pool.go b/mempool/cat/pool.go index ecec9644e6..e077cddb26 100644 --- a/mempool/cat/pool.go +++ b/mempool/cat/pool.go @@ -41,7 +41,7 @@ var ( // set the default to 5, but this value can be changed in an init func InclusionDelay = 5 * time.Second peerCount = atomic.Int32{} - defaultSeenLimit = 0 + defaultSeenLimit = 82 ) // TxPoolOption sets an optional parameter on the TxPool. diff --git a/mempool/cat/reactor.go b/mempool/cat/reactor.go index aeff58d247..aa8f9e468d 100644 --- a/mempool/cat/reactor.go +++ b/mempool/cat/reactor.go @@ -148,7 +148,7 @@ func (memR *Reactor) OnStart() error { // } // }() // } - go memR.PeriodicallyBroadcastSeenTxs(time.Second) + go memR.PeriodicallyClearWants(time.Second * 3) return nil } @@ -234,7 +234,7 @@ func (memR *Reactor) RemovePeer(peer p2p.Peer, reason interface{}) { // we won't receive any responses from them. outboundRequests := memR.requests.ClearAllRequestsFrom(peerID) for key := range outboundRequests { - memR.findNewPeerToRequestTx(key, 5) + memR.findNewPeerToRequestTx(key, 7) } n := peerCount.Add(-1) if n < 0 { @@ -320,33 +320,29 @@ func (memR *Reactor) ReceiveEnvelope(e p2p.Envelope) { memR.broadcastSeenTx(key, string(memR.self)) go func(tx []byte, key types.TxKey) { - for i := 0; i < 5; i++ { - wants, has := memR.wantState.GetWants(key) - if has { - for peer := range wants { - p := memR.ids.GetPeer(peer) - if p == nil { - continue - } - if p2p.SendEnvelopeShim(e.Src, p2p.Envelope{ //nolint:staticcheck - ChannelID: mempool.MempoolChannel, - Message: &protomem.Txs{Txs: [][]byte{tx}}, - }, memR.Logger) { - memR.wantState.Delete(key, peer) - // memR.mempool.PeerHasTx(peerID, txKey) - schema.WriteMempoolTx( - memR.traceClient, - string(p.ID()), - key[:], - len(tx), - schema.Upload, - ) - } + wants, has := memR.wantState.GetWants(key) + if has { + for peer := range wants { + p, has := memR.ids.getPeerFromID(peer) + if !has || p == nil { + continue + } + if p2p.SendEnvelopeShim(e.Src, p2p.Envelope{ //nolint:staticcheck + ChannelID: mempool.MempoolChannel, + Message: &protomem.Txs{Txs: [][]byte{tx}}, + }, memR.Logger) { + // memR.mempool.PeerHasTx(peerID, txKey) + memR.wantState.Delete(key, peer) + schema.WriteMempoolTx( + memR.traceClient, + string(p.ID()), + key[:], + len(tx), + schema.Upload, + ) } } - time.Sleep(time.Second) } - }(tx, key) } @@ -399,7 +395,7 @@ func (memR *Reactor) ReceiveEnvelope(e p2p.Envelope) { // We don't have the transaction, nor are we requesting it so we send the node // a want msg - memR.requestTx(txKey, e.Src, 10) + memR.requestTx(txKey, e.Src, 3) // A peer is requesting a transaction that we have claimed to have. Find the specified // transaction and broadcast it to the peer. We may no longer have the transaction @@ -441,10 +437,10 @@ func (memR *Reactor) ReceiveEnvelope(e p2p.Envelope) { } else { if !memR.mempool.IsRejectedTx(txKey) && !memR.mempool.store.hasCommitted(txKey) { memR.ids.mtx.RLock() - peer, has := memR.ids.peerMap[e.Src.ID()] + _, has := memR.ids.peerMap[e.Src.ID()] memR.ids.mtx.RUnlock() if has { - memR.wantState.Add(txKey, peer) + memR.wantState.Add(txKey, e.Src.ID()) } } } @@ -459,11 +455,37 @@ func (memR *Reactor) ReceiveEnvelope(e p2p.Envelope) { // PeriodicallyBroadcastSeenTxs will rebroadcast a seenTx for a given tx. It // cycles through all txs, and waits the provided duration between each // broadcast. -func (memR *Reactor) PeriodicallyBroadcastSeenTxs(dur time.Duration) { +func (memR *Reactor) PeriodicallyClearWants(dur time.Duration) { for { for _, tx := range memR.mempool.GetAllTxs() { - memR.broadcastSeenTx(tx.key, string(memR.self)) - time.Sleep(dur) + memR.ClearWant(tx.key, tx.tx) + } + time.Sleep(dur) + } +} + +func (memR *Reactor) ClearWant(key types.TxKey, tx types.Tx) { + wants, has := memR.wantState.GetWants(key) + if has { + for peer := range wants { + p, has := memR.ids.getPeerFromID(peer) + if !has || p == nil { + continue + } + if p2p.SendEnvelopeShim(p, p2p.Envelope{ //nolint:staticcheck + ChannelID: mempool.MempoolChannel, + Message: &protomem.Txs{Txs: [][]byte{tx}}, + }, memR.Logger) { + // memR.mempool.PeerHasTx(peerID, txKey) + memR.wantState.Delete(key, peer) + schema.WriteMempoolTx( + memR.traceClient, + string(p.ID()), + key[:], + len(tx), + schema.Upload, + ) + } } } } diff --git a/mempool/cat/wants.go b/mempool/cat/wants.go index 41f76c3391..fd71ef32e9 100644 --- a/mempool/cat/wants.go +++ b/mempool/cat/wants.go @@ -3,38 +3,39 @@ package cat import ( "sync" + "github.com/tendermint/tendermint/p2p" "github.com/tendermint/tendermint/types" ) type wantState struct { mtx *sync.RWMutex - wants map[types.TxKey]map[uint16]struct{} + wants map[types.TxKey]map[p2p.ID]struct{} } func NewWantState() *wantState { return &wantState{ - wants: make(map[types.TxKey]map[uint16]struct{}), + wants: make(map[types.TxKey]map[p2p.ID]struct{}), mtx: &sync.RWMutex{}, } } -func (f *wantState) GetWants(tx types.TxKey) (map[uint16]struct{}, bool) { +func (f *wantState) GetWants(tx types.TxKey) (map[p2p.ID]struct{}, bool) { f.mtx.RLock() defer f.mtx.RUnlock() out, has := f.wants[tx] return out, has } -func (f *wantState) Add(tx types.TxKey, peer uint16) { +func (f *wantState) Add(tx types.TxKey, peer p2p.ID) { f.mtx.Lock() defer f.mtx.Unlock() if _, exists := f.wants[tx]; !exists { - f.wants[tx] = make(map[uint16]struct{}) + f.wants[tx] = make(map[p2p.ID]struct{}) } f.wants[tx][peer] = struct{}{} } -func (f *wantState) Delete(tx types.TxKey, peer uint16) { +func (f *wantState) Delete(tx types.TxKey, peer p2p.ID) { f.mtx.Lock() defer f.mtx.Unlock() ws, has := f.wants[tx] diff --git a/types/tx.go b/types/tx.go index e6d750564f..39ed00f4be 100644 --- a/types/tx.go +++ b/types/tx.go @@ -60,12 +60,19 @@ func (key TxKey) String() string { return fmt.Sprintf("TxKey{%X}", key[:]) } -func TxKeyFromBytes(bytes []byte) (TxKey, error) { - if len(bytes) != TxKeySize { - return TxKey{}, fmt.Errorf("incorrect tx key size. Expected %d bytes, got %d", TxKeySize, len(bytes)) +var ( + empty = bytes.Repeat([]byte{0}, TxKeySize) +) + +func TxKeyFromBytes(bz []byte) (TxKey, error) { + if len(bz) != TxKeySize { + return TxKey{}, fmt.Errorf("incorrect tx key size. Expected %d bytes, got %d", TxKeySize, len(bz)) + } + if bytes.Equal(bz, empty) { + return TxKey{}, fmt.Errorf("empty tx key") } var key TxKey - copy(key[:], bytes) + copy(key[:], bz) return key, nil } From b999df6b31c671ef8558f47822a4ba33ca2269ac Mon Sep 17 00:00:00 2001 From: evan-forbes <evan.samuel.forbes@gmail.com> Date: Fri, 4 Oct 2024 23:38:53 -0500 Subject: [PATCH 118/127] chore: actually remove the problematic code --- mempool/cat/reactor.go | 35 ++++++++--------------------------- mempool/cat/wants.go | 8 +++++--- pkg/trace/schema/schema.go | 3 +++ 3 files changed, 16 insertions(+), 30 deletions(-) diff --git a/mempool/cat/reactor.go b/mempool/cat/reactor.go index aa8f9e468d..d1eba53896 100644 --- a/mempool/cat/reactor.go +++ b/mempool/cat/reactor.go @@ -319,31 +319,7 @@ func (memR *Reactor) ReceiveEnvelope(e p2p.Envelope) { memR.broadcastSeenTx(key, string(memR.self)) - go func(tx []byte, key types.TxKey) { - wants, has := memR.wantState.GetWants(key) - if has { - for peer := range wants { - p, has := memR.ids.getPeerFromID(peer) - if !has || p == nil { - continue - } - if p2p.SendEnvelopeShim(e.Src, p2p.Envelope{ //nolint:staticcheck - ChannelID: mempool.MempoolChannel, - Message: &protomem.Txs{Txs: [][]byte{tx}}, - }, memR.Logger) { - // memR.mempool.PeerHasTx(peerID, txKey) - memR.wantState.Delete(key, peer) - schema.WriteMempoolTx( - memR.traceClient, - string(p.ID()), - key[:], - len(tx), - schema.Upload, - ) - } - } - } - }(tx, key) + go memR.ClearWant(key, tx) } // A peer has indicated to us that it has a transaction. We first verify the txkey and @@ -457,6 +433,7 @@ func (memR *Reactor) ReceiveEnvelope(e p2p.Envelope) { // broadcast. func (memR *Reactor) PeriodicallyClearWants(dur time.Duration) { for { + fmt.Println("clearing wants --------------------") for _, tx := range memR.mempool.GetAllTxs() { memR.ClearWant(tx.key, tx.tx) } @@ -468,6 +445,7 @@ func (memR *Reactor) ClearWant(key types.TxKey, tx types.Tx) { wants, has := memR.wantState.GetWants(key) if has { for peer := range wants { + fmt.Println("peer wanted tx", peer, key.String()) p, has := memR.ids.getPeerFromID(peer) if !has || p == nil { continue @@ -477,13 +455,16 @@ func (memR *Reactor) ClearWant(key types.TxKey, tx types.Tx) { Message: &protomem.Txs{Txs: [][]byte{tx}}, }, memR.Logger) { // memR.mempool.PeerHasTx(peerID, txKey) - memR.wantState.Delete(key, peer) + err := memR.wantState.Delete(key, peer) + if err != nil { + fmt.Println("faaaaaack", err) + } schema.WriteMempoolTx( memR.traceClient, string(p.ID()), key[:], len(tx), - schema.Upload, + schema.UploadClear, ) } } diff --git a/mempool/cat/wants.go b/mempool/cat/wants.go index fd71ef32e9..1a15da6846 100644 --- a/mempool/cat/wants.go +++ b/mempool/cat/wants.go @@ -1,6 +1,7 @@ package cat import ( + "fmt" "sync" "github.com/tendermint/tendermint/p2p" @@ -35,20 +36,21 @@ func (f *wantState) Add(tx types.TxKey, peer p2p.ID) { f.wants[tx][peer] = struct{}{} } -func (f *wantState) Delete(tx types.TxKey, peer p2p.ID) { +func (f *wantState) Delete(tx types.TxKey, peer p2p.ID) error { f.mtx.Lock() defer f.mtx.Unlock() ws, has := f.wants[tx] if !has { - return + return fmt.Errorf("no wants for transaction found", peer, tx.String()) } _, has = ws[peer] if !has { - return + return fmt.Errorf("peer didn't want the transaction", peer, tx.String()) } delete(ws, peer) f.wants[tx] = ws if len(f.wants[tx]) == 0 { delete(f.wants, tx) } + return nil } diff --git a/pkg/trace/schema/schema.go b/pkg/trace/schema/schema.go index 132c94ff50..57d1a8282c 100644 --- a/pkg/trace/schema/schema.go +++ b/pkg/trace/schema/schema.go @@ -29,6 +29,7 @@ const ( Download TransferType = iota Upload UploadBroadcast + UploadClear ) func (t TransferType) String() string { @@ -39,6 +40,8 @@ func (t TransferType) String() string { return "upload" case UploadBroadcast: return "broadcast" + case UploadClear: + return "clear" default: return "unknown" } From 5c034db17d0b0bea76e302d9f3b6c1258fd91a87 Mon Sep 17 00:00:00 2001 From: evan-forbes <evan.samuel.forbes@gmail.com> Date: Sat, 5 Oct 2024 00:35:33 -0500 Subject: [PATCH 119/127] fix: don't automatically broadcast new transactions --- mempool/cat/reactor.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/mempool/cat/reactor.go b/mempool/cat/reactor.go index d1eba53896..15495090a4 100644 --- a/mempool/cat/reactor.go +++ b/mempool/cat/reactor.go @@ -127,7 +127,8 @@ func (memR *Reactor) OnStart() error { // listen in for any newly verified tx via RPC, then immediately // broadcast it to all connected peers. case nextTx := <-memR.mempool.next(): - memR.broadcastNewTx(nextTx) + memR.broadcastSeenTx(nextTx.key, string(memR.self)) + // memR.broadcastNewTx(nextTx) } } }() @@ -394,7 +395,7 @@ func (memR *Reactor) ReceiveEnvelope(e p2p.Envelope) { // TODO: consider handling the case where we receive a HasTx message from a peer // before we receive a WantTx message from them. In this case we might // ignore the request if we know it's no longer valid. - if has && !memR.opts.ListenOnly { + if has { // peerID := memR.ids.GetIDForPeer(e.Src.ID()) // memR.Logger.Debug("sending a transaction in response to a want msg", "peer", peerID, "txKey", txKey) if p2p.SendEnvelopeShim(e.Src, p2p.Envelope{ //nolint:staticcheck From c4b8c857bb24ba374e0a8e2a7a8e669714dec51b Mon Sep 17 00:00:00 2001 From: evan-forbes <evan.samuel.forbes@gmail.com> Date: Sat, 5 Oct 2024 16:41:56 -0500 Subject: [PATCH 120/127] chore: stop hashing the tx three times during apply block --- consensus/reactor.go | 2 +- consensus/state.go | 4 ++++ mempool/cat/pool.go | 18 +++++++++--------- mempool/cat/reactor.go | 1 - state/execution.go | 12 +++++++++++- store/store.go | 5 +++-- types/block.go | 21 +++++++++++++++++++++ 7 files changed, 49 insertions(+), 14 deletions(-) diff --git a/consensus/reactor.go b/consensus/reactor.go index d68050b48a..7c532956bd 100644 --- a/consensus/reactor.go +++ b/consensus/reactor.go @@ -744,7 +744,7 @@ OUTER_LOOP: if err != nil { panic(err) } - logger.Info("Sending compact block", "height", prs.Height, "round", prs.Round) + // logger.Info("Sending compact block", "height", prs.Height, "round", prs.Round) if p2p.SendEnvelopeShim(peer, p2p.Envelope{ //nolint: staticcheck ChannelID: DataChannel, Message: &cmtcons.CompactBlock{ diff --git a/consensus/state.go b/consensus/state.go index b5154fefb5..e2a444e8df 100644 --- a/consensus/state.go +++ b/consensus/state.go @@ -1698,6 +1698,10 @@ func (cs *State) finalizeCommit(height int64) { blockID, ok := cs.Votes.Precommits(cs.CommitRound).TwoThirdsMajority() block, blockParts := cs.ProposalBlock, cs.ProposalBlockParts + compactBlock := cs.ProposalCompactBlock + if block != nil && compactBlock != nil { + block.SetTxHashes(compactBlock.Txs) + } if !ok { panic("cannot finalize commit; commit does not have 2/3 majority") diff --git a/mempool/cat/pool.go b/mempool/cat/pool.go index e077cddb26..ec0e652fe3 100644 --- a/mempool/cat/pool.go +++ b/mempool/cat/pool.go @@ -41,7 +41,7 @@ var ( // set the default to 5, but this value can be changed in an init func InclusionDelay = 5 * time.Second peerCount = atomic.Int32{} - defaultSeenLimit = 82 + defaultSeenLimit = 84 ) // TxPoolOption sets an optional parameter on the TxPool. @@ -577,17 +577,17 @@ func getSeenLimit() int { // calling Update. func (txmp *TxPool) Update( blockHeight int64, - blockTxs types.Txs, + blockHashes types.Txs, // switched to hashes hackily to avoid re-hashing deliverTxResponses []*abci.ResponseDeliverTx, newPreFn mempool.PreCheckFunc, newPostFn mempool.PostCheckFunc, ) error { // Safety check: Transactions and responses must match in number. - if len(blockTxs) != len(deliverTxResponses) { + if len(blockHashes) != len(deliverTxResponses) { panic(fmt.Sprintf("mempool: got %d transactions but %d DeliverTx responses", - len(blockTxs), len(deliverTxResponses))) + len(blockHashes), len(deliverTxResponses))) } - txmp.logger.Debug("updating mempool", "height", blockHeight, "txs", len(blockTxs)) + txmp.logger.Debug("updating mempool", "height", blockHeight, "txs", len(blockHashes)) txmp.updateMtx.Lock() txmp.height = blockHeight @@ -602,14 +602,14 @@ func (txmp *TxPool) Update( txmp.lastPurgeTime = time.Now() txmp.updateMtx.Unlock() - txmp.metrics.SuccessfulTxs.Add(float64(len(blockTxs))) + txmp.metrics.SuccessfulTxs.Add(float64(len(blockHashes))) txmp.store.clearCommitted() // add the recently committed transactions to the cache - keys := make([]types.TxKey, len(blockTxs)) - for idx, tx := range blockTxs { - keys[idx] = tx.Key() + keys := make([]types.TxKey, len(blockHashes)) + for idx, tx := range blockHashes { + keys[idx] = types.TxKey(tx) txmp.seenByPeersSet.RemoveKey(keys[idx]) txmp.rejectedTxCache.Push(keys[idx]) } diff --git a/mempool/cat/reactor.go b/mempool/cat/reactor.go index 15495090a4..16502c3a1c 100644 --- a/mempool/cat/reactor.go +++ b/mempool/cat/reactor.go @@ -434,7 +434,6 @@ func (memR *Reactor) ReceiveEnvelope(e p2p.Envelope) { // broadcast. func (memR *Reactor) PeriodicallyClearWants(dur time.Duration) { for { - fmt.Println("clearing wants --------------------") for _, tx := range memR.mempool.GetAllTxs() { memR.ClearWant(tx.key, tx.tx) } diff --git a/state/execution.go b/state/execution.go index c42be9c43b..f53759f1eb 100644 --- a/state/execution.go +++ b/state/execution.go @@ -347,10 +347,20 @@ func (blockExec *BlockExecutor) Commit( "app_hash", fmt.Sprintf("%X", res.Data), ) + hashes := block.GetTxHashes() + if len(hashes) == 0 && len(block.Txs) != 0 { + fmt.Println("weird we didn't set the txs ==========================") + hashes = make([]types.Tx, 0, len(block.Txs)) + for _, tx := range block.Txs { + key := tx.Key() + hashes = append(hashes, types.Tx(key[:])) + } + } + // Update mempool. err = blockExec.mempool.Update( block.Height, - block.Txs, + hashes, deliverTxResponses, TxPreCheck(state), TxPostCheck(state), diff --git a/store/store.go b/store/store.go index f225f2a21a..ab8701ba70 100644 --- a/store/store.go +++ b/store/store.go @@ -460,7 +460,8 @@ func (bs *BlockStore) SaveTxInfo(block *types.Block, txResponseCodes []uint32) e batch := bs.db.NewBatch() // Batch and save txs from the block - for i, tx := range block.Txs { + hashes := block.GetTxHashes() + for i := range block.Txs { txInfo := cmtstore.TxInfo{ Height: block.Height, Index: uint32(i), @@ -470,7 +471,7 @@ func (bs *BlockStore) SaveTxInfo(block *types.Block, txResponseCodes []uint32) e if err != nil { return fmt.Errorf("unable to marshal tx: %w", err) } - if err := batch.Set(calcTxHashKey(tx.Hash()), txInfoBytes); err != nil { + if err := batch.Set(calcTxHashKey(hashes[i]), txInfoBytes); err != nil { return err } } diff --git a/types/block.go b/types/block.go index e627cadde8..ba00d0c90c 100644 --- a/types/block.go +++ b/types/block.go @@ -47,6 +47,27 @@ type Block struct { Data `json:"data"` Evidence EvidenceData `json:"evidence"` LastCommit *Commit `json:"last_commit"` + + // cached data + txHashes []Tx +} + +func (b *Block) GetTxHashes() []Tx { + hashes := b.txHashes + if len(hashes) != len(b.Txs) { + fmt.Println("shoot we have to calculate the hashes of the txs oh noooooooooooooooo") + hashes = make([]Tx, 0, len(b.Txs)) + for _, tx := range b.Txs { + k := tx.Key() + hashes = append(hashes, k[:]) + } + } + b.txHashes = hashes + return hashes +} + +func (b *Block) SetTxHashes(hashes []Tx) { + b.txHashes = hashes } // ValidateBasic performs basic validation that doesn't involve state data. It From 61f0f66cf3fa03a1ee5045124950936d43e8c339 Mon Sep 17 00:00:00 2001 From: evan-forbes <evan.samuel.forbes@gmail.com> Date: Sat, 5 Oct 2024 17:05:37 -0500 Subject: [PATCH 121/127] chore: remove panic --- mempool/cat/block_builder.go | 1 - 1 file changed, 1 deletion(-) diff --git a/mempool/cat/block_builder.go b/mempool/cat/block_builder.go index b53fdab2f4..77e306514a 100644 --- a/mempool/cat/block_builder.go +++ b/mempool/cat/block_builder.go @@ -34,7 +34,6 @@ func (memR *Reactor) FetchTxsFromKeys(ctx context.Context, blockID []byte, compa if err != nil { fmt.Println("INCORRECT COMPLACT BLONKS ERROOR!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!") fmt.Println("INCORRECT COMPLACT BLONKS ERROOR!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!") - panic(err) return nil, fmt.Errorf("incorrect compact blocks format: %w", err) } wtx := memR.mempool.store.get(txKey) From 8b18717a4707fb3e64b348c1bbba1e26a4567a1c Mon Sep 17 00:00:00 2001 From: evan-forbes <evan.samuel.forbes@gmail.com> Date: Sat, 5 Oct 2024 18:17:50 -0500 Subject: [PATCH 122/127] dramatically reduce backoff to continually dial peers --- p2p/pex/pex_reactor.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/p2p/pex/pex_reactor.go b/p2p/pex/pex_reactor.go index b15a6ebeab..51763e655e 100644 --- a/p2p/pex/pex_reactor.go +++ b/p2p/pex/pex_reactor.go @@ -33,15 +33,15 @@ const ( maxMsgSize = maxAddressSize * maxGetSelection // ensure we have enough peers - defaultEnsurePeersPeriod = 30 * time.Second + defaultEnsurePeersPeriod = 5 * time.Second // Seed/Crawler constants // minTimeBetweenCrawls is a minimum time between attempts to crawl a peer. - minTimeBetweenCrawls = 2 * time.Minute + minTimeBetweenCrawls = 1 * time.Minute // check some peers every this - crawlPeerPeriod = 30 * time.Second + crawlPeerPeriod = 15 * time.Second maxAttemptsToDial = 16 // ~ 35h in total (last attempt - 18h) @@ -51,7 +51,7 @@ const ( biasToSelectNewPeers = 30 // 70 to select good peers // if a peer is marked bad, it will be banned for at least this time period - defaultBanTime = 24 * time.Hour + defaultBanTime = 1 * time.Second ) type errMaxAttemptsToDial struct { @@ -572,7 +572,7 @@ func (r *Reactor) dialPeer(addr *p2p.NetAddress) error { // exponential backoff if it's not our first attempt to dial given address if attempts > 0 { jitter := time.Duration(cmtrand.Float64() * float64(time.Second)) // 1s == (1e9 ns) - backoffDuration := jitter + ((1 << uint(attempts)) * time.Second) + backoffDuration := jitter + (time.Duration(attempts) * time.Second) backoffDuration = r.maxBackoffDurationForPeer(addr, backoffDuration) sinceLastDialed := time.Since(lastDialed) if sinceLastDialed < backoffDuration { From 5ef2f9090f1e3312b95cd7cc5a2d666ac462e2a6 Mon Sep 17 00:00:00 2001 From: evan-forbes <evan.samuel.forbes@gmail.com> Date: Sun, 6 Oct 2024 17:27:07 -0500 Subject: [PATCH 123/127] Revert "dramatically reduce backoff to continually dial peers" This reverts commit 8b18717a4707fb3e64b348c1bbba1e26a4567a1c. --- p2p/pex/pex_reactor.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/p2p/pex/pex_reactor.go b/p2p/pex/pex_reactor.go index 51763e655e..b15a6ebeab 100644 --- a/p2p/pex/pex_reactor.go +++ b/p2p/pex/pex_reactor.go @@ -33,15 +33,15 @@ const ( maxMsgSize = maxAddressSize * maxGetSelection // ensure we have enough peers - defaultEnsurePeersPeriod = 5 * time.Second + defaultEnsurePeersPeriod = 30 * time.Second // Seed/Crawler constants // minTimeBetweenCrawls is a minimum time between attempts to crawl a peer. - minTimeBetweenCrawls = 1 * time.Minute + minTimeBetweenCrawls = 2 * time.Minute // check some peers every this - crawlPeerPeriod = 15 * time.Second + crawlPeerPeriod = 30 * time.Second maxAttemptsToDial = 16 // ~ 35h in total (last attempt - 18h) @@ -51,7 +51,7 @@ const ( biasToSelectNewPeers = 30 // 70 to select good peers // if a peer is marked bad, it will be banned for at least this time period - defaultBanTime = 1 * time.Second + defaultBanTime = 24 * time.Hour ) type errMaxAttemptsToDial struct { @@ -572,7 +572,7 @@ func (r *Reactor) dialPeer(addr *p2p.NetAddress) error { // exponential backoff if it's not our first attempt to dial given address if attempts > 0 { jitter := time.Duration(cmtrand.Float64() * float64(time.Second)) // 1s == (1e9 ns) - backoffDuration := jitter + (time.Duration(attempts) * time.Second) + backoffDuration := jitter + ((1 << uint(attempts)) * time.Second) backoffDuration = r.maxBackoffDurationForPeer(addr, backoffDuration) sinceLastDialed := time.Since(lastDialed) if sinceLastDialed < backoffDuration { From 5cca85dd56c937a89b829bfb2bc3ffe13221ec68 Mon Sep 17 00:00:00 2001 From: evan-forbes <evan.samuel.forbes@gmail.com> Date: Sun, 6 Oct 2024 21:56:12 -0500 Subject: [PATCH 124/127] feat: prioritize outgoing txs --- mempool/cat/block_builder.go | 2 +- mempool/cat/pool.go | 48 +++-- mempool/cat/pool_test.go | 2 +- mempool/cat/reactor.go | 74 +++---- mempool/cat/sorted_queue.go | 192 +++++++++++++++++ mempool/cat/sorted_queue_test.go | 307 +++++++++++++++++++++++++++ mempool/cat/store_test.go | 10 +- mempool/cat/tx.go | 11 +- mempool/cat/wants.go | 4 +- proto/tendermint/mempool/types.pb.go | 76 +++++-- proto/tendermint/mempool/types.proto | 3 +- types/tx.go | 2 +- 12 files changed, 637 insertions(+), 94 deletions(-) create mode 100644 mempool/cat/sorted_queue.go create mode 100644 mempool/cat/sorted_queue_test.go diff --git a/mempool/cat/block_builder.go b/mempool/cat/block_builder.go index 77e306514a..8c5568761d 100644 --- a/mempool/cat/block_builder.go +++ b/mempool/cat/block_builder.go @@ -182,7 +182,7 @@ func (memR *Reactor) FetchKeysFromTxs(ctx context.Context, txs [][]byte) ([][]by // // We don't set the priority, gasWanted or sender fields because we // don't know them. - wtx := newWrappedTx(tx, key, memR.mempool.Height(), 0, 0, "", isBlobTx) + wtx := newWrappedTx(tx, key, memR.mempool.Height(), 0, 0, "", 0) wtx.evictable = false memR.broadcastNewTx(wtx) // For safety we also store this transaction in the mempool (ignoring diff --git a/mempool/cat/pool.go b/mempool/cat/pool.go index ec0e652fe3..bb85d24201 100644 --- a/mempool/cat/pool.go +++ b/mempool/cat/pool.go @@ -40,8 +40,7 @@ const ( var ( // set the default to 5, but this value can be changed in an init func InclusionDelay = 5 * time.Second - peerCount = atomic.Int32{} - defaultSeenLimit = 84 + defaultSeenLimit = 0 ) // TxPoolOption sets an optional parameter on the TxPool. @@ -95,6 +94,8 @@ type TxPool struct { broadcastCh chan *wrappedTx broadcastMtx sync.Mutex txsToBeBroadcast []types.TxKey + + valPrio atomic.Uint64 } // NewTxPool constructs a new, empty content addressable txpool at the specified @@ -126,6 +127,7 @@ func NewTxPool( store: newStore(), broadcastCh: make(chan *wrappedTx), txsToBeBroadcast: make([]types.TxKey, 0), + valPrio: atomic.Uint64{}, } for _, opt := range options { @@ -214,6 +216,16 @@ func (txmp *TxPool) GetTxByKey(txKey types.TxKey) (types.Tx, bool) { return types.Tx{}, false } +// GetWrappedTxByKey retrieves a transaction based on the key. It returns a bool +// indicating whether transaction was found in the cache. +func (txmp *TxPool) GetWrappedTxByKey(txKey types.TxKey) (*wrappedTx, bool) { + wtx := txmp.store.get(txKey) + if wtx != nil { + return wtx, true + } + return wtx, false +} + // WasRecentlyEvicted returns a bool indicating whether the transaction with // the specified key was recently evicted and is currently within the cache. func (txmp *TxPool) WasRecentlyEvicted(txKey types.TxKey) bool { @@ -265,10 +277,14 @@ func (txmp *TxPool) CheckTx(tx types.Tx, cb func(*abci.Response), txInfo mempool key = sha256.Sum256(tx) } - rsp, err := txmp.TryAddNewTx(tx, key, txInfo, isBlobTx) + valPrio := txmp.valPrio.Load() + txmp.valPrio.Add(1) + + rsp, _, err := txmp.TryAddNewTx(tx, key, txInfo, valPrio) if err != nil { return err } + defer func() { // call the callback if it is set if cb != nil { @@ -329,48 +345,48 @@ func (txmp *TxPool) markToBeBroadcast(key types.TxKey) { // to avoid races with the same tx. It then call `CheckTx` so that the application can validate it. // If it passes `CheckTx`, the new transaction is added to the mempool as long as it has // sufficient priority and space else if evicted it will return an error -func (txmp *TxPool) TryAddNewTx(tx types.Tx, key types.TxKey, txInfo mempool.TxInfo, isBlob bool) (*abci.ResponseCheckTx, error) { +func (txmp *TxPool) TryAddNewTx(tx types.Tx, key types.TxKey, txInfo mempool.TxInfo, valPrio uint64) (*abci.ResponseCheckTx, *wrappedTx, error) { // First check any of the caches to see if we can conclude early. We may have already seen and processed // the transaction, or it may have already been committed. if txmp.store.hasCommitted(key) { - return nil, ErrTxRecentlyCommitted + return nil, nil, ErrTxRecentlyCommitted } if txmp.IsRejectedTx(key) { // The peer has sent us a transaction that we have previously marked as invalid. Since `CheckTx` can // be non-deterministic, we don't punish the peer but instead just ignore the tx - return nil, ErrTxAlreadyRejected + return nil, nil, ErrTxAlreadyRejected } if txmp.Has(key) { txmp.metrics.AlreadySeenTxs.Add(1) // The peer has sent us a transaction that we have already seen - return nil, ErrTxInMempool + return nil, nil, ErrTxInMempool } // reserve the key if !txmp.store.reserve(key) { txmp.logger.Debug("mempool already attempting to verify and add transaction", "txKey", fmt.Sprintf("%X", key)) // txmp.PeerHasTx(txInfo.SenderID, key) - return nil, ErrTxInMempool + return nil, nil, ErrTxInMempool } defer txmp.store.release(key) // If a precheck hook is defined, call it before invoking the application. if err := txmp.preCheck(tx); err != nil { txmp.metrics.FailedTxs.Add(1) - return nil, mempool.ErrPreCheck{Reason: err} + return nil, nil, mempool.ErrPreCheck{Reason: err} } // Early exit if the proxy connection has an error. if err := txmp.proxyAppConn.Error(); err != nil { - return nil, err + return nil, nil, err } // Invoke an ABCI CheckTx for this transaction. rsp, err := txmp.proxyAppConn.CheckTxSync(abci.RequestCheckTx{Tx: tx}) if err != nil { - return rsp, err + return rsp, nil, err } if rsp.Code != abci.CodeTypeOK { if txmp.config.KeepInvalidTxsInCache { @@ -380,12 +396,12 @@ func (txmp *TxPool) TryAddNewTx(tx types.Tx, key types.TxKey, txInfo mempool.TxI txmp.metrics.FailedTxs.Add(1) // we don't return an error when there has been a fail code. Instead the // client is expected to read the error code and the raw log - return rsp, nil + return rsp, nil, nil } // Create wrapped tx wtx := newWrappedTx( - tx, key, txmp.Height(), rsp.GasWanted, rsp.Priority, rsp.Sender, isBlob, + tx, key, txmp.Height(), rsp.GasWanted, rsp.Priority, rsp.Sender, valPrio, ) // Perform the post check @@ -395,15 +411,15 @@ func (txmp *TxPool) TryAddNewTx(tx types.Tx, key types.TxKey, txInfo mempool.TxI txmp.rejectedTxCache.Push(key) } txmp.metrics.FailedTxs.Add(1) - return rsp, fmt.Errorf("rejected bad transaction after post check: %w", err) + return rsp, nil, fmt.Errorf("rejected bad transaction after post check: %w", err) } // Now we consider the transaction to be valid. Once a transaction is valid, it // can only become invalid if recheckTx is enabled and RecheckTx returns a non zero code if err := txmp.addNewTransaction(wtx, rsp); err != nil { - return nil, err + return nil, nil, err } - return rsp, nil + return rsp, wtx, nil } // RemoveTxByKey removes the transaction with the specified key from the diff --git a/mempool/cat/pool_test.go b/mempool/cat/pool_test.go index 5c422cb6de..3707bd3256 100644 --- a/mempool/cat/pool_test.go +++ b/mempool/cat/pool_test.go @@ -734,7 +734,7 @@ func TestTxPool_ConcurrentlyAddingTx(t *testing.T) { wg.Add(1) go func(sender uint16) { defer wg.Done() - _, err := txmp.TryAddNewTx(tx, tx.Key(), mempool.TxInfo{SenderID: sender}, false) + _, _, err := txmp.TryAddNewTx(tx, tx.Key(), mempool.TxInfo{SenderID: sender}, 0) errCh <- err }(uint16(i + 1)) } diff --git a/mempool/cat/reactor.go b/mempool/cat/reactor.go index 16502c3a1c..bdab8fbb43 100644 --- a/mempool/cat/reactor.go +++ b/mempool/cat/reactor.go @@ -1,8 +1,10 @@ package cat import ( + "context" "crypto/sha256" "fmt" + "math" "time" "github.com/gogo/protobuf/proto" @@ -46,6 +48,7 @@ type Reactor struct { traceClient trace.Tracer self p2p.ID wantState *wantState + txPrio *TxPrioritizor } type ReactorOptions struct { @@ -96,6 +99,7 @@ func NewReactor(mempool *TxPool, opts *ReactorOptions) (*Reactor, error) { if err != nil { return nil, err } + memR := &Reactor{ opts: opts, mempool: mempool, @@ -104,6 +108,7 @@ func NewReactor(mempool *TxPool, opts *ReactorOptions) (*Reactor, error) { blockFetcher: newBlockFetcher(), traceClient: opts.TraceClient, wantState: NewWantState(), + txPrio: NewTxPrioritizor(mempool.logger, opts.TraceClient), } memR.self = opts.Self memR.BaseReactor = *p2p.NewBaseReactor("Mempool", memR) @@ -166,7 +171,7 @@ func (memR *Reactor) GetChannels() []*p2p.ChannelDescriptor { largestTx := make([]byte, memR.opts.MaxTxSize) txMsg := protomem.Message{ Sum: &protomem.Message_Txs{ - Txs: &protomem.Txs{Txs: [][]byte{largestTx}}, + Txs: &protomem.Txs{Txs: [][]byte{largestTx}, Valprio: math.MaxUint64}, }, } @@ -183,7 +188,7 @@ func (memR *Reactor) GetChannels() []*p2p.ChannelDescriptor { { ID: mempool.MempoolChannel, Priority: 9, - SendQueueCapacity: 5000, + SendQueueCapacity: 2, RecvMessageCapacity: txMsg.Size(), MessageType: &protomem.Message{}, }, @@ -205,6 +210,7 @@ func (memR *Reactor) InitPeer(peer p2p.Peer) p2p.Peer { // AddPeer broadcasts all the transactions that this node has seen func (memR *Reactor) AddPeer(peer p2p.Peer) { + memR.txPrio.AddPeer(peer) keys := memR.mempool.store.getAllKeys() for _, key := range keys { // memR.broadcastSeenTx(key, string(memR.self)) @@ -237,11 +243,12 @@ func (memR *Reactor) RemovePeer(peer p2p.Peer, reason interface{}) { for key := range outboundRequests { memR.findNewPeerToRequestTx(key, 7) } - n := peerCount.Add(-1) - if n < 0 { - memR.Logger.Error("seen req went below one, resetting") - peerCount.Store(0) - } + + memR.txPrio.RemovePeer(peer.ID()) +} + +func (memR *Reactor) FeedPeer(ctx context.Context, peer p2p.Peer) { + } func (memR *Reactor) Receive(chID byte, peer p2p.Peer, msgBytes []byte) { @@ -306,7 +313,7 @@ func (memR *Reactor) ReceiveEnvelope(e p2p.Envelope) { memR.blockFetcher.TryAddMissingTx(key, tx) // Now attempt to add the tx to the mempool. - rsp, err := memR.mempool.TryAddNewTx(ntx, key, txInfo, isBlobTx) + rsp, wtx, err := memR.mempool.TryAddNewTx(ntx, key, txInfo, msg.Valprio) if err != nil || rsp.Code != abci.CodeTypeOK { memR.Logger.Error("Could not add tx from peer", "peerID", peerID, "txKey", key, "err", err) // We broadcast only transactions that we deem valid and actually have in our mempool. @@ -320,7 +327,7 @@ func (memR *Reactor) ReceiveEnvelope(e p2p.Envelope) { memR.broadcastSeenTx(key, string(memR.self)) - go memR.ClearWant(key, tx) + go memR.ClearWant(key, wtx) } // A peer has indicated to us that it has a transaction. We first verify the txkey and @@ -391,26 +398,12 @@ func (memR *Reactor) ReceiveEnvelope(e p2p.Envelope) { schema.Download, "", ) - tx, has := memR.mempool.GetTxByKey(txKey) + wtx, has := memR.mempool.GetWrappedTxByKey(txKey) // TODO: consider handling the case where we receive a HasTx message from a peer // before we receive a WantTx message from them. In this case we might // ignore the request if we know it's no longer valid. if has { - // peerID := memR.ids.GetIDForPeer(e.Src.ID()) - // memR.Logger.Debug("sending a transaction in response to a want msg", "peer", peerID, "txKey", txKey) - if p2p.SendEnvelopeShim(e.Src, p2p.Envelope{ //nolint:staticcheck - ChannelID: mempool.MempoolChannel, - Message: &protomem.Txs{Txs: [][]byte{tx}}, - }, memR.Logger) { - // memR.mempool.PeerHasTx(peerID, txKey) - schema.WriteMempoolTx( - memR.traceClient, - string(e.Src.ID()), - txKey[:], - len(tx), - schema.Upload, - ) - } + memR.txPrio.Send(e.Src, wtx) } else { if !memR.mempool.IsRejectedTx(txKey) && !memR.mempool.store.hasCommitted(txKey) { memR.ids.mtx.RLock() @@ -435,13 +428,14 @@ func (memR *Reactor) ReceiveEnvelope(e p2p.Envelope) { func (memR *Reactor) PeriodicallyClearWants(dur time.Duration) { for { for _, tx := range memR.mempool.GetAllTxs() { - memR.ClearWant(tx.key, tx.tx) + memR.ClearWant(tx.key, tx) } time.Sleep(dur) } } -func (memR *Reactor) ClearWant(key types.TxKey, tx types.Tx) { +// TODO: this is really bad for sending the same tx to the same peer multiple times due to read +func (memR *Reactor) ClearWant(key types.TxKey, wtx *wrappedTx) { wants, has := memR.wantState.GetWants(key) if has { for peer := range wants { @@ -450,23 +444,13 @@ func (memR *Reactor) ClearWant(key types.TxKey, tx types.Tx) { if !has || p == nil { continue } - if p2p.SendEnvelopeShim(p, p2p.Envelope{ //nolint:staticcheck - ChannelID: mempool.MempoolChannel, - Message: &protomem.Txs{Txs: [][]byte{tx}}, - }, memR.Logger) { - // memR.mempool.PeerHasTx(peerID, txKey) - err := memR.wantState.Delete(key, peer) - if err != nil { - fmt.Println("faaaaaack", err) - } - schema.WriteMempoolTx( - memR.traceClient, - string(p.ID()), - key[:], - len(tx), - schema.UploadClear, - ) + err := memR.wantState.Delete(key, peer) + if err != nil { + // hack hack !! we've already sent this tx in a different call + // so just continue + continue } + memR.txPrio.Send(p, wtx) } } } @@ -524,10 +508,12 @@ func (memR *Reactor) broadcastNewTx(wtx *wrappedTx) { msg := &protomem.Message{ Sum: &protomem.Message_Txs{ Txs: &protomem.Txs{ - Txs: [][]byte{wtx.tx}, + Txs: [][]byte{wtx.tx}, + Valprio: memR.mempool.valPrio.Load(), }, }, } + memR.mempool.valPrio.Add(1) bz, err := msg.Marshal() if err != nil { panic(err) diff --git a/mempool/cat/sorted_queue.go b/mempool/cat/sorted_queue.go new file mode 100644 index 0000000000..8029b1e647 --- /dev/null +++ b/mempool/cat/sorted_queue.go @@ -0,0 +1,192 @@ +package cat + +import ( + "container/heap" + "context" + "sync" + + "github.com/tendermint/tendermint/libs/log" + "github.com/tendermint/tendermint/mempool" + "github.com/tendermint/tendermint/p2p" + "github.com/tendermint/tendermint/pkg/trace" + "github.com/tendermint/tendermint/pkg/trace/schema" + protomem "github.com/tendermint/tendermint/proto/tendermint/mempool" +) + +type TxPrioritizor struct { + ctx context.Context + rootCancel context.CancelFunc + logger log.Logger + traceClient trace.Tracer + + mtx *sync.RWMutex + outgoing map[p2p.ID]*SortedQueue + outgoingCancels map[p2p.ID]context.CancelFunc +} + +func NewTxPrioritizor(logger log.Logger, traceClient trace.Tracer) *TxPrioritizor { + ctx, cancel := context.WithCancel(context.Background()) + + tp := &TxPrioritizor{ + outgoing: make(map[p2p.ID]*SortedQueue), + outgoingCancels: make(map[p2p.ID]context.CancelFunc), + mtx: &sync.RWMutex{}, + ctx: ctx, + rootCancel: cancel, + logger: logger, + traceClient: traceClient, + } + + return tp +} + +func (tp *TxPrioritizor) AddPeer(peer p2p.Peer) { + tp.mtx.Lock() + defer tp.mtx.Unlock() + + sq := NewSortedQueue() + tp.outgoing[peer.ID()] = sq + + ctx, cancel := context.WithCancel(tp.ctx) + tp.outgoingCancels[peer.ID()] = cancel + + go func(ctx context.Context, peer p2p.Peer, sq *SortedQueue) { + for { + select { + case <-ctx.Done(): + return + case <-sq.ready: + otx, has := sq.Pop() + if !has || otx == nil { + panic("nil tx popped when we were supposed to have txs to send") + } + + wtx := otx.(*wrappedTx) // btw, this is only required cause the heap package doesn't yet use generics + + if p2p.SendEnvelopeShim(peer, p2p.Envelope{ //nolint:staticcheck + ChannelID: mempool.MempoolChannel, + Message: &protomem.Txs{Txs: [][]byte{wtx.tx}, Valprio: wtx.valPrio}, + }, tp.logger) { + // memR.mempool.PeerHasTx(peerID, txKey) + schema.WriteMempoolTx( + tp.traceClient, + string(peer.ID()), + wtx.key[:], + len(wtx.tx), + schema.Upload, + ) + } else { + // reinsert the tx if we are unable to send it. + sq.Insert(wtx) + } + + } + } + }(ctx, peer, sq) +} + +func (tp *TxPrioritizor) RemovePeer(peer p2p.ID) { + tp.mtx.Lock() + defer tp.mtx.Unlock() + if cancel, has := tp.outgoingCancels[peer]; has { + cancel() + } +} + +func (tp *TxPrioritizor) Send(peer p2p.Peer, wtx *wrappedTx) { + tp.mtx.RLock() + sq, has := tp.outgoing[peer.ID()] + if !has { + tp.logger.Error("yo there's no outgoing peer") + } + tp.mtx.RUnlock() + sq.Insert(wtx) +} + +// Ordered is an interface that requires a LessThan method for comparison +type Ordered interface { + LessThan(other Ordered) bool +} + +// SortedQueue represents a thread-safe priority queue for elements implementing the Ordered interface +type SortedQueue struct { + mu sync.RWMutex // Mutex for concurrency control + heap priorityQueue // Internal min-heap for sorted queue + ready chan struct{} +} + +// NewSortedQueue initializes a new SortedQueue +func NewSortedQueue() *SortedQueue { + sq := &SortedQueue{ + heap: priorityQueue{}, + ready: make(chan struct{}, 6000), + } + heap.Init(&sq.heap) + return sq +} + +// Insert adds a new element to the sorted queue and maintains order +func (sq *SortedQueue) Insert(value Ordered) { + sq.mu.Lock() + defer sq.mu.Unlock() + + heap.Push(&sq.heap, value) + sq.ready <- struct{}{} // the popper that things can be popped +} + +// Pop removes and returns the smallest element from the queue +func (sq *SortedQueue) Pop() (Ordered, bool) { + sq.mu.Lock() + defer sq.mu.Unlock() + + if sq.heap.Len() == 0 { + return nil, false // Queue is empty + } + item := heap.Pop(&sq.heap).(Ordered) + return item, true +} + +// Peek returns the smallest element without removing it +func (sq *SortedQueue) Peek() (Ordered, bool) { + sq.mu.RLock() + defer sq.mu.RUnlock() + + if sq.heap.Len() == 0 { + return nil, false // Queue is empty + } + return sq.heap[0], true +} + +// Len returns the number of elements in the queue +func (sq *SortedQueue) Len() int { + sq.mu.RLock() + defer sq.mu.RUnlock() + + return sq.heap.Len() +} + +// priorityQueue is the internal heap structure that implements heap.Interface +type priorityQueue []Ordered + +func (pq priorityQueue) Len() int { return len(pq) } + +func (pq priorityQueue) Less(i, j int) bool { + // Use the LessThan method for comparison + return pq[i].LessThan(pq[j]) +} + +func (pq priorityQueue) Swap(i, j int) { + pq[i], pq[j] = pq[j], pq[i] +} + +func (pq *priorityQueue) Push(x any) { + *pq = append(*pq, x.(Ordered)) +} + +func (pq *priorityQueue) Pop() any { + old := *pq + n := len(old) + item := old[n-1] + *pq = old[0 : n-1] + return item +} diff --git a/mempool/cat/sorted_queue_test.go b/mempool/cat/sorted_queue_test.go new file mode 100644 index 0000000000..ddf83b2091 --- /dev/null +++ b/mempool/cat/sorted_queue_test.go @@ -0,0 +1,307 @@ +package cat + +import ( + "container/heap" + "sync" + "testing" +) + +// Item is a custom type that implements the Ordered interface for testing purposes +type Item struct { + value int +} + +// LessThan implements the Ordered interface for the Item type +func (i *Item) LessThan(other Ordered) bool { + return i.value < other.(*Item).value +} + +// TestPriorityQueue_PushAndPop is the table-driven test for priorityQueue's Push and Pop methods +func TestPriorityQueue_PushAndPop(t *testing.T) { + tests := []struct { + name string + input []*Item + popOrder []int // Expected pop order + finalLen int // Expected length after pops + }{ + { + name: "Basic insertion and pop", + input: []*Item{ + {value: 5}, + {value: 3}, + {value: 8}, + {value: 1}, + }, + popOrder: []int{1, 3, 5, 8}, + finalLen: 0, + }, + { + name: "Insert single element", + input: []*Item{ + {value: 42}, + }, + popOrder: []int{42}, + finalLen: 0, + }, + { + name: "Insert already sorted input", + input: []*Item{ + {value: 1}, + {value: 2}, + {value: 3}, + {value: 4}, + {value: 5}, + }, + popOrder: []int{1, 2, 3, 4, 5}, + finalLen: 0, + }, + { + name: "Insert reverse sorted input", + input: []*Item{ + {value: 5}, + {value: 4}, + {value: 3}, + {value: 2}, + {value: 1}, + }, + popOrder: []int{1, 2, 3, 4, 5}, + finalLen: 0, + }, + { + name: "All identical values", + input: []*Item{ + {value: 7}, + {value: 7}, + {value: 7}, + {value: 7}, + }, + popOrder: []int{7, 7, 7, 7}, + finalLen: 0, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + var pq priorityQueue + + // Initialize priorityQueue as a heap + heap.Init(&pq) + + // Push all input items into the priority queue + for _, item := range tt.input { + heap.Push(&pq, item) + } + + // Ensure the queue length matches the input size + if pq.Len() != len(tt.input) { + t.Errorf("Expected queue length %d, got %d", len(tt.input), pq.Len()) + } + + // Pop items and verify the pop order + for i, expectedValue := range tt.popOrder { + poppedItem := heap.Pop(&pq).(*Item) + + if poppedItem.value != expectedValue { + t.Errorf("Pop at index %d: expected %d, got %d", i, expectedValue, poppedItem.value) + } + } + + // Ensure the queue length matches the expected final length + if pq.Len() != tt.finalLen { + t.Errorf("Expected final queue length %d, got %d", tt.finalLen, pq.Len()) + } + }) + } +} + +// TestPriorityQueue_PointerBehavior ensures that pointers and references are correctly handled +func TestPriorityQueue_PointerBehavior(t *testing.T) { + // Create two distinct items + item1 := &Item{value: 10} + item2 := &Item{value: 5} + + var pq priorityQueue + heap.Init(&pq) + + // Push the items + heap.Push(&pq, item1) + heap.Push(&pq, item2) + + // Pop the first item and ensure it's the smaller one + popped := heap.Pop(&pq).(*Item) + if popped.value != 5 { + t.Errorf("Expected item with value 5, but got %d", popped.value) + } + + // Modify the original item2 and ensure that the queue still behaves correctly + item2.value = 100 + + // Push the modified item back into the queue + heap.Push(&pq, item2) + + // Pop the next item and ensure it's the original item1 (value 10) + popped = heap.Pop(&pq).(*Item) + if popped.value != 10 { + t.Errorf("Expected item with value 10, but got %d", popped.value) + } + + // Pop the modified item2 (which now has value 100) + popped = heap.Pop(&pq).(*Item) + if popped.value != 100 { + t.Errorf("Expected item with value 100, but got %d", popped.value) + } +} + +// TestSortedQueue_Concurrency tests concurrent insertions and pops in SortedQueue +func TestSortedQueue_Concurrency(t *testing.T) { + // Initialize the sorted queue + sq := NewSortedQueue() + + var wg sync.WaitGroup + numGoroutines := 10 + numItemsPerGoroutine := 100 + + // Concurrently insert elements into the queue + for i := 0; i < numGoroutines; i++ { + wg.Add(1) + go func(base int) { + defer wg.Done() + for j := 0; j < numItemsPerGoroutine; j++ { + sq.Insert(&Item{value: base + j}) + } + }(i * numItemsPerGoroutine) + } + + wg.Wait() + + // Concurrently pop elements from the queue + poppedItems := make([]int, 0, numGoroutines*numItemsPerGoroutine) + + for i := 0; i < numGoroutines; i++ { + for j := 0; j < numItemsPerGoroutine; j++ { + item, ok := sq.Pop() + if ok { + poppedItems = append(poppedItems, item.(*Item).value) + + } + } + } + + // Check that the popped elements are in sorted order + for i := 1; i < len(poppedItems); i++ { + if poppedItems[i] < poppedItems[i-1] { + t.Errorf("Popped items are not in sorted order: %d < %d at index %d", poppedItems[i], poppedItems[i-1], i) + } + } +} + +// TestSortedQueue_SimpleInsertAndPop tests sequential inserts and pops +func TestSortedQueue_SimpleInsertAndPop(t *testing.T) { + sq := NewSortedQueue() + + // Insert items + sq.Insert(&Item{value: 5}) + sq.Insert(&Item{value: 2}) + sq.Insert(&Item{value: 9}) + sq.Insert(&Item{value: 1}) + + // Pop and verify the order is correct + expectedOrder := []int{1, 2, 5, 9} + for _, expected := range expectedOrder { + item, ok := sq.Pop() + if !ok { + t.Fatalf("Expected to pop an item, but got none") + } + if item.(*Item).value != expected { + t.Errorf("Expected %d, but got %d", expected, item.(*Item).value) + } + } + + // Pop from empty queue + _, ok := sq.Pop() + if ok { + t.Errorf("Expected no item from empty queue, but got one") + } +} + +// TestSortedQueue_ConcurrentInsert tests concurrent inserts without pops +func TestSortedQueue_ConcurrentInsert(t *testing.T) { + sq := NewSortedQueue() + + var wg sync.WaitGroup + numGoroutines := 5 + numItemsPerGoroutine := 1000 + + // Concurrently insert elements + for i := 0; i < numGoroutines; i++ { + wg.Add(1) + go func(base int) { + defer wg.Done() + for j := 0; j < numItemsPerGoroutine; j++ { + sq.Insert(&Item{value: base + j}) + } + }(i * numItemsPerGoroutine) + } + + // Wait for all insertions to complete + wg.Wait() + + // Verify queue size after all inserts + expectedLen := numGoroutines * numItemsPerGoroutine + if sq.Len() != expectedLen { + t.Errorf("Expected queue length %d, but got %d", expectedLen, sq.Len()) + } + + // Verify that the smallest element is correctly ordered + firstItem, ok := sq.Pop() + if !ok || firstItem.(*Item).value != 0 { + t.Errorf("Expected first item to be 0, but got %d", firstItem.(*Item).value) + } +} + +// TestSortedQueue_ConcurrentPop ensures that concurrent pops behave correctly +func TestSortedQueue_ConcurrentPop(t *testing.T) { + sq := NewSortedQueue() + + // Insert some elements sequentially + for i := 0; i < 1000; i++ { + sq.Insert(&Item{value: i}) + } + + var wg sync.WaitGroup + numGoroutines := 5 + numItemsPerGoroutine := 200 + var popMu sync.Mutex + poppedItems := make([]int, 0, 1000) + + // Concurrently pop elements + for i := 0; i < numGoroutines; i++ { + wg.Add(1) + go func() { + defer wg.Done() + for j := 0; j < numItemsPerGoroutine; j++ { + item, ok := sq.Pop() + if ok { + popMu.Lock() + poppedItems = append(poppedItems, item.(*Item).value) + popMu.Unlock() + } + } + }() + } + + // Wait for all pops to finish + wg.Wait() + + // Verify that we popped the correct number of items + if len(poppedItems) != numGoroutines*numItemsPerGoroutine { + t.Errorf("Expected %d popped items, but got %d", numGoroutines*numItemsPerGoroutine, len(poppedItems)) + } + + // Ensure that the popped items are in sorted order + for i := 1; i < len(poppedItems); i++ { + if poppedItems[i] < poppedItems[i-1] { + t.Errorf("Popped items are not in sorted order: %d < %d at index %d", poppedItems[i], poppedItems[i-1], i) + } + } +} diff --git a/mempool/cat/store_test.go b/mempool/cat/store_test.go index e64aa67977..6068ca21ad 100644 --- a/mempool/cat/store_test.go +++ b/mempool/cat/store_test.go @@ -16,7 +16,7 @@ func TestStoreSimple(t *testing.T) { tx := types.Tx("tx1") key := tx.Key() - wtx := newWrappedTx(tx, key, 1, 1, 1, "", false) + wtx := newWrappedTx(tx, key, 1, 1, 1, "", 0) // asset zero state require.Nil(t, store.get(key)) @@ -47,7 +47,7 @@ func TestStoreReservingTxs(t *testing.T) { tx := types.Tx("tx1") key := tx.Key() - wtx := newWrappedTx(tx, key, 1, 1, 1, "", false) + wtx := newWrappedTx(tx, key, 1, 1, 1, "", 0) // asset zero state store.release(key) @@ -105,7 +105,7 @@ func TestStoreConcurrentAccess(t *testing.T) { for range ticker.C { tx := types.Tx(fmt.Sprintf("tx%d", i%(numTxs/10))) key := tx.Key() - wtx := newWrappedTx(tx, key, 1, 1, 1, "", false) + wtx := newWrappedTx(tx, key, 1, 1, 1, "", 0) existingTx := store.get(key) if existingTx != nil && bytes.Equal(existingTx.tx, tx) { // tx has already been added @@ -138,7 +138,7 @@ func TestStoreGetTxs(t *testing.T) { for i := 0; i < numTxs; i++ { tx := types.Tx(fmt.Sprintf("tx%d", i)) key := tx.Key() - wtx := newWrappedTx(tx, key, 1, 1, int64(i), "", false) + wtx := newWrappedTx(tx, key, 1, 1, int64(i), "", 0) store.set(wtx) } @@ -168,7 +168,7 @@ func TestStoreExpiredTxs(t *testing.T) { for i := 0; i < numTxs; i++ { tx := types.Tx(fmt.Sprintf("tx%d", i)) key := tx.Key() - wtx := newWrappedTx(tx, key, int64(i), 1, 1, "", false) + wtx := newWrappedTx(tx, key, int64(i), 1, 1, "", 0) store.set(wtx) } diff --git a/mempool/cat/tx.go b/mempool/cat/tx.go index 92470f22fc..983392b47a 100644 --- a/mempool/cat/tx.go +++ b/mempool/cat/tx.go @@ -21,10 +21,10 @@ type wrappedTx struct { evictable bool // whether this transaction can be evicted from the mempool. This is false when the transaction is a part of a proposed block nolint:lll seenCount int - isBlob bool + valPrio uint64 // the validator where the tx originated from's priority } -func newWrappedTx(tx types.Tx, key types.TxKey, height, gasWanted, priority int64, sender string, isBlob bool) *wrappedTx { +func newWrappedTx(tx types.Tx, key types.TxKey, height, gasWanted, priority int64, sender string, valPrio uint64) *wrappedTx { return &wrappedTx{ tx: tx, key: key, @@ -34,9 +34,14 @@ func newWrappedTx(tx types.Tx, key types.TxKey, height, gasWanted, priority int6 priority: priority, sender: sender, evictable: true, - isBlob: isBlob, + valPrio: valPrio, } } // Size reports the size of the raw transaction in bytes. func (w *wrappedTx) size() int64 { return int64(len(w.tx)) } + +func (w *wrappedTx) LessThan(b Ordered) bool { + bb := b.(*wrappedTx) + return w.valPrio < bb.valPrio +} diff --git a/mempool/cat/wants.go b/mempool/cat/wants.go index 1a15da6846..dc2c0b01da 100644 --- a/mempool/cat/wants.go +++ b/mempool/cat/wants.go @@ -41,11 +41,11 @@ func (f *wantState) Delete(tx types.TxKey, peer p2p.ID) error { defer f.mtx.Unlock() ws, has := f.wants[tx] if !has { - return fmt.Errorf("no wants for transaction found", peer, tx.String()) + return fmt.Errorf("no wants for transaction found: %v %v", peer, tx.String()) } _, has = ws[peer] if !has { - return fmt.Errorf("peer didn't want the transaction", peer, tx.String()) + return fmt.Errorf("peer didn't want the transaction %v %v", peer, tx.String()) } delete(ws, peer) f.wants[tx] = ws diff --git a/proto/tendermint/mempool/types.pb.go b/proto/tendermint/mempool/types.pb.go index feb8ee2de3..d772028b7d 100644 --- a/proto/tendermint/mempool/types.pb.go +++ b/proto/tendermint/mempool/types.pb.go @@ -23,7 +23,8 @@ var _ = math.Inf const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package type Txs struct { - Txs [][]byte `protobuf:"bytes,1,rep,name=txs,proto3" json:"txs,omitempty"` + Txs [][]byte `protobuf:"bytes,1,rep,name=txs,proto3" json:"txs,omitempty"` + Valprio uint64 `protobuf:"varint,2,opt,name=valprio,proto3" json:"valprio,omitempty"` } func (m *Txs) Reset() { *m = Txs{} } @@ -66,6 +67,13 @@ func (m *Txs) GetTxs() [][]byte { return nil } +func (m *Txs) GetValprio() uint64 { + if m != nil { + return m.Valprio + } + return 0 +} + type SeenTx struct { TxKey []byte `protobuf:"bytes,1,opt,name=tx_key,json=txKey,proto3" json:"tx_key,omitempty"` Peer string `protobuf:"bytes,2,opt,name=peer,proto3" json:"peer,omitempty"` @@ -270,25 +278,26 @@ func init() { func init() { proto.RegisterFile("tendermint/mempool/types.proto", fileDescriptor_2af51926fdbcbc05) } var fileDescriptor_2af51926fdbcbc05 = []byte{ - // 286 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x74, 0x91, 0xb1, 0x4a, 0xc3, 0x50, - 0x14, 0x86, 0x73, 0x8d, 0x4d, 0xf1, 0xb4, 0x83, 0x5c, 0x90, 0x16, 0x87, 0x6b, 0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0xdd, 0x26, 0x92, 0x1a, 0xcc, 0x3e, 0x8e, 0xb5, 0xce, 0x0d, 0xb6, 0x35, 0x69, - 0xda, 0x01, 0x5b, 0x6d, 0xd2, 0xe9, 0xfc, 0xa3, 0x12, 0x6c, 0x57, 0x09, 0xf6, 0x55, 0x09, 0xf6, - 0x56, 0x0b, 0x6b, 0x57, 0x0b, 0xeb, 0xb3, 0x16, 0xd6, 0xc3, 0xcd, 0x6a, 0x4d, 0xcf, 0x9b, 0x65, - 0xf0, 0x98, 0xa5, 0xe1, 0xde, 0xea, 0x7b, 0xd1, 0x4c, 0x1e, 0x1e, 0x7e, 0x64, 0xe9, 0x98, 0x66, - 0xfc, 0x1d, 0x00, 0x00, 0xff, 0xff, 0x37, 0x79, 0xee, 0x21, 0xae, 0x01, 0x00, 0x00, + // 299 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x74, 0x91, 0xc1, 0x4a, 0xc3, 0x40, + 0x14, 0x45, 0x33, 0xa6, 0x4d, 0xf0, 0xb5, 0x0b, 0x19, 0x10, 0x83, 0x8b, 0xb1, 0x64, 0x15, 0x10, + 0x12, 0x6c, 0xe9, 0xc2, 0x6d, 0x57, 0x05, 0x71, 0x93, 0x16, 0x04, 0x37, 0x25, 0xd5, 0x47, 0x2d, + 0x36, 0x33, 0x21, 0x33, 0xb5, 0x93, 0xbf, 0xf0, 0x3f, 0xfc, 0x11, 0x97, 0x5d, 0xba, 0x94, 0xe4, + 0x47, 0x24, 0x13, 0x8b, 0x85, 0xda, 0xdd, 0x7d, 0x5c, 0x0e, 0xef, 0xc0, 0x05, 0xa6, 0x90, 0x3f, + 0x63, 0x9e, 0x2e, 0xb9, 0x8a, 0x52, 0x4c, 0x33, 0x21, 0x56, 0x91, 0x2a, 0x32, 0x94, 0x61, 0x96, + 0x0b, 0x25, 0x28, 0xfd, 0xeb, 0xc3, 0xdf, 0xde, 0xbf, 0x01,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0x13, 0x00, + 0x00, 0xff, 0xff, 0x7c, 0x11, 0x90, 0x62, 0xc8, 0x01, 0x00, 0x00, } func (m *Txs) Marshal() (dAtA []byte, err error) { @@ -311,6 +320,11 @@ func (m *Txs) MarshalToSizedBuffer(dAtA []byte) (int, error) { _ = i var l int _ = l + if m.Valprio != 0 { + i = encodeVarintTypes(dAtA, i, uint64(m.Valprio)) + i-- + dAtA[i] = 0x10 + } if len(m.Txs) > 0 { for iNdEx := len(m.Txs) - 1; iNdEx >= 0; iNdEx-- { i -= len(m.Txs[iNdEx]) @@ -508,6 +522,9 @@ func (m *Txs) Size() (n int) { n += 1 + l + sovTypes(uint64(l)) } } + if m.Valprio != 0 { + n += 1 + sovTypes(uint64(m.Valprio)) + } return n } @@ -657,6 +674,25 @@ func (m *Txs) Unmarshal(dAtA []byte) error { m.Txs = append(m.Txs, make([]byte, postIndex-iNdEx)) copy(m.Txs[len(m.Txs)-1], dAtA[iNdEx:postIndex]) iNdEx = postIndex + case 2: + if wireType != 0 { + return fmt.Errorf("proto: wrong wireType = %d for field Valprio", wireType) + } + m.Valprio = 0 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTypes + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + m.Valprio |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } default: iNdEx = preIndex skippy, err := skipTypes(dAtA[iNdEx:]) diff --git a/proto/tendermint/mempool/types.proto b/proto/tendermint/mempool/types.proto index b719ff76f8..e27b10c8ec 100644 --- a/proto/tendermint/mempool/types.proto +++ b/proto/tendermint/mempool/types.proto @@ -4,7 +4,8 @@ package tendermint.mempool; option go_package = "github.com/tendermint/tendermint/proto/tendermint/mempool"; message Txs { - repeated bytes txs = 1; + repeated bytes txs = 1; + uint64 valprio = 2; } message SeenTx { diff --git a/types/tx.go b/types/tx.go index 39ed00f4be..87cc06f6a7 100644 --- a/types/tx.go +++ b/types/tx.go @@ -69,7 +69,7 @@ func TxKeyFromBytes(bz []byte) (TxKey, error) { return TxKey{}, fmt.Errorf("incorrect tx key size. Expected %d bytes, got %d", TxKeySize, len(bz)) } if bytes.Equal(bz, empty) { - return TxKey{}, fmt.Errorf("empty tx key") + return TxKey{}, fmt.Errorf("empty tx key. Expected %d bytes, got %d", TxKeySize, len(bz)) } var key TxKey copy(key[:], bz) From 48d1c756f4d778d295b3feb39fdc1581853dbcdb Mon Sep 17 00:00:00 2001 From: evan-forbes <evan.samuel.forbes@gmail.com> Date: Sun, 6 Oct 2024 22:04:03 -0500 Subject: [PATCH 125/127] feat: add the validator priority to traces --- mempool/cat/reactor.go | 7 ++++--- mempool/cat/sorted_queue.go | 1 + mempool/v1/reactor.go | 2 ++ pkg/trace/schema/mempool.go | 4 +++- 4 files changed, 10 insertions(+), 4 deletions(-) diff --git a/mempool/cat/reactor.go b/mempool/cat/reactor.go index bdab8fbb43..4e2ea9f892 100644 --- a/mempool/cat/reactor.go +++ b/mempool/cat/reactor.go @@ -297,7 +297,7 @@ func (memR *Reactor) ReceiveEnvelope(e p2p.Envelope) { } else { key = sha256.Sum256(tx) } - schema.WriteMempoolTx(memR.traceClient, string(e.Src.ID()), key[:], len(ntx), schema.Download) + schema.WriteMempoolTx(memR.traceClient, string(e.Src.ID()), key[:], len(ntx), msg.Valprio, schema.Download) // If we requested the transaction we mark it as received. if memR.requests.Has(peerID, key) { memR.requests.MarkReceived(peerID, key) @@ -505,11 +505,12 @@ func (memR *Reactor) broadcastSeenTx(txKey types.TxKey, from string) { // broadcastNewTx broadcast new transaction to all peers unless we are already sure they have seen the tx. func (memR *Reactor) broadcastNewTx(wtx *wrappedTx) { + valprio := memR.mempool.valPrio.Load() msg := &protomem.Message{ Sum: &protomem.Message_Txs{ Txs: &protomem.Txs{ Txs: [][]byte{wtx.tx}, - Valprio: memR.mempool.valPrio.Load(), + Valprio: valprio, }, }, } @@ -536,7 +537,7 @@ func (memR *Reactor) broadcastNewTx(wtx *wrappedTx) { if peer.Send(mempool.MempoolChannel, bz) { //nolint:staticcheck // memR.mempool.PeerHasTx(id, wtx.key) - schema.WriteMempoolTx(memR.traceClient, string(peer.ID()), wtx.key[:], len(wtx.tx), schema.UploadBroadcast) + schema.WriteMempoolTx(memR.traceClient, string(peer.ID()), wtx.key[:], len(wtx.tx), valprio, schema.UploadBroadcast) } else { memR.Logger.Error("failed to send new tx to peer", "peerID", peer.ID(), "txKey", wtx.key) } diff --git a/mempool/cat/sorted_queue.go b/mempool/cat/sorted_queue.go index 8029b1e647..95c405f6aa 100644 --- a/mempool/cat/sorted_queue.go +++ b/mempool/cat/sorted_queue.go @@ -73,6 +73,7 @@ func (tp *TxPrioritizor) AddPeer(peer p2p.Peer) { string(peer.ID()), wtx.key[:], len(wtx.tx), + wtx.valPrio, schema.Upload, ) } else { diff --git a/mempool/v1/reactor.go b/mempool/v1/reactor.go index d85adc414c..74ee0441a3 100644 --- a/mempool/v1/reactor.go +++ b/mempool/v1/reactor.go @@ -198,6 +198,7 @@ func (memR *Reactor) ReceiveEnvelope(e p2p.Envelope) { string(e.Src.ID()), ntx.Hash(), len(ntx), + 0, schema.Download, ) err = memR.mempool.CheckTx(ntx, nil, txInfo) @@ -305,6 +306,7 @@ func (memR *Reactor) broadcastTxRoutine(peer p2p.Peer) { string(peer.ID()), memTx.tx.Hash(), len(memTx.tx), + 0, schema.Upload, ) } diff --git a/pkg/trace/schema/mempool.go b/pkg/trace/schema/mempool.go index 064ab606b0..d9e7d5eab7 100644 --- a/pkg/trace/schema/mempool.go +++ b/pkg/trace/schema/mempool.go @@ -28,6 +28,7 @@ type MempoolTx struct { Peer string `json:"peer"` Size int `json:"size"` TransferType TransferType `json:"transfer_type"` + ValPrio uint64 `json:"valprio"` } // Table returns the table name for the MempoolTx struct. @@ -37,7 +38,7 @@ func (m MempoolTx) Table() string { // WriteMempoolTx writes a tracing point for a tx using the predetermined // schema for mempool tracing. -func WriteMempoolTx(client trace.Tracer, peer string, txHash []byte, size int, transferType TransferType) { +func WriteMempoolTx(client trace.Tracer, peer string, txHash []byte, size int, valprio uint64, transferType TransferType) { // this check is redundant to what is checked during client.Write, although it // is an optimization to avoid allocations from the map of fields. if !client.IsCollecting(MempoolTxTable) { @@ -48,6 +49,7 @@ func WriteMempoolTx(client trace.Tracer, peer string, txHash []byte, size int, t Peer: peer, Size: size, TransferType: transferType, + ValPrio: valprio, }) } From e8db88b571b9f2413eeab74944729f67bb80fc64 Mon Sep 17 00:00:00 2001 From: evan-forbes <evan.samuel.forbes@gmail.com> Date: Mon, 7 Oct 2024 01:09:06 -0500 Subject: [PATCH 126/127] fix: rare block part issue --- mempool/cat/block_builder.go | 57 ++---------------------------------- mempool/cat/pool.go | 5 ++-- mempool/cat/reactor.go | 10 +++++-- mempool/cat/store.go | 13 +++++--- mempool/cat/store_test.go | 12 ++++---- pkg/trace/schema/mempool.go | 24 +++++++++++++++ types/part_set.go | 4 +-- 7 files changed, 54 insertions(+), 71 deletions(-) diff --git a/mempool/cat/block_builder.go b/mempool/cat/block_builder.go index 8c5568761d..22a98c1735 100644 --- a/mempool/cat/block_builder.go +++ b/mempool/cat/block_builder.go @@ -36,7 +36,8 @@ func (memR *Reactor) FetchTxsFromKeys(ctx context.Context, blockID []byte, compa fmt.Println("INCORRECT COMPLACT BLONKS ERROOR!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!") return nil, fmt.Errorf("incorrect compact blocks format: %w", err) } - wtx := memR.mempool.store.get(txKey) + wtx, _ := memR.mempool.GetWrappedTxByKey(txKey) + // wtx := memR.mempool.store.get(txKey) if wtx != nil { txs[i] = wtx.tx memR.mempool.store.markAsUnevictable(txKey) @@ -95,65 +96,13 @@ func (memR *Reactor) FetchTxsFromKeys(ctx context.Context, blockID []byte, compa for _, key := range missingKeys { numHaveTx := len(memR.mempool.seenByPeersSet.Get(key)) memR.Logger.Info("searching for missing tx", "numHaveTx", numHaveTx) - memR.findNewPeerToRequestTx(key, 5) + memR.findNewPeerToRequestTx(key, 2) } // Wait for the reactor to retrieve and post all transactions. return request.WaitForBlock(ctx) } -func (memR *Reactor) FetchTxsFromKeysSync(compactData [][]byte) ([][]byte, error) { - txs := make([][]byte, len(compactData)) - missingKeys := make(map[int]types.TxKey, len(compactData)) - - // iterate through the keys to know what transactions we have and what are missing - for i, key := range compactData { - txKey, err := types.TxKeyFromBytes(key) - if err != nil { - return nil, fmt.Errorf("incorrect compact blocks format: %w", err) - } - wtx := memR.mempool.store.get(txKey) - if wtx != nil { - txs[i] = wtx.tx - memR.mempool.store.markAsUnevictable(txKey) - } else { - missingKeys[i] = txKey - } - } - memR.Logger.Info("fetching transactions from peers", "numTxs", len(txs), "numMissing", len(missingKeys)) - memR.mempool.metrics.MissingTxs.Add(float64(len(missingKeys))) - - // Check if we got lucky and already had all the transactions. - if len(missingKeys) == 0 { - schema.WriteMempoolRecoveryStats( - memR.traceClient, - 0, - 0, - len(compactData), - 0, - nil, - ) - return txs, nil - } - - missingTxs := make([]string, len(missingKeys)) - for i, tx := range missingKeys { - missingTxs[i] = bytes.HexBytes(tx[:]).String() - } - - schema.WriteMempoolRecoveryStats( - memR.traceClient, - len(missingKeys), - len(missingKeys), - len(compactData), - 0, - missingTxs, - ) - - // Wait for the reactor to retrieve and post all transactions. - return nil, fmt.Errorf("missing transaction: %d", len(missingTxs)) -} - // FetchKeysFromTxs is in many ways the opposite method. It takes a full block generated by the application // and reduces it to the set of keys that need to be gossiped from one mempool to another nodes mempool // in order to recreate the full block. diff --git a/mempool/cat/pool.go b/mempool/cat/pool.go index bb85d24201..f70558ee1a 100644 --- a/mempool/cat/pool.go +++ b/mempool/cat/pool.go @@ -124,7 +124,7 @@ func NewTxPool( height: height, preCheckFn: func(_ types.Tx) error { return nil }, postCheckFn: func(_ types.Tx, _ *abci.ResponseCheckTx) error { return nil }, - store: newStore(), + store: newStore(logger), broadcastCh: make(chan *wrappedTx), txsToBeBroadcast: make([]types.TxKey, 0), valPrio: atomic.Uint64{}, @@ -603,8 +603,7 @@ func (txmp *TxPool) Update( panic(fmt.Sprintf("mempool: got %d transactions but %d DeliverTx responses", len(blockHashes), len(deliverTxResponses))) } - txmp.logger.Debug("updating mempool", "height", blockHeight, "txs", len(blockHashes)) - + txmp.logger.Info("updating mempool", "height", blockHeight, "txs", len(blockHashes), "size", txmp.Size(), "bytes", txmp.SizeBytes()) txmp.updateMtx.Lock() txmp.height = blockHeight txmp.notifiedTxsAvailable = false diff --git a/mempool/cat/reactor.go b/mempool/cat/reactor.go index 4e2ea9f892..d8947c8d39 100644 --- a/mempool/cat/reactor.go +++ b/mempool/cat/reactor.go @@ -430,6 +430,8 @@ func (memR *Reactor) PeriodicallyClearWants(dur time.Duration) { for _, tx := range memR.mempool.GetAllTxs() { memR.ClearWant(tx.key, tx) } + // just added this here to avoid writing a new function that occasionally records this + schema.WriteMempoolSize(memR.traceClient, memR.mempool.Size(), int(memR.mempool.SizeBytes())) time.Sleep(dur) } } @@ -446,8 +448,8 @@ func (memR *Reactor) ClearWant(key types.TxKey, wtx *wrappedTx) { } err := memR.wantState.Delete(key, peer) if err != nil { - // hack hack !! we've already sent this tx in a different call - // so just continue + // we've already sent this tx in a different call + // so just continue and don't send continue } memR.txPrio.Send(p, wtx) @@ -578,6 +580,10 @@ func (memR *Reactor) requestTx(txKey types.TxKey, peer p2p.Peer, tries int) { // findNewPeerToSendTx finds a new peer that has already seen the transaction to // request a transaction from. func (memR *Reactor) findNewPeerToRequestTx(txKey types.TxKey, tries int) { + if memR.mempool.Has(txKey) { + return + } + if tries == 0 { return } diff --git a/mempool/cat/store.go b/mempool/cat/store.go index 5d7918695e..6a081b8042 100644 --- a/mempool/cat/store.go +++ b/mempool/cat/store.go @@ -1,10 +1,10 @@ package cat import ( - "fmt" "sync" "time" + "github.com/tendermint/tendermint/libs/log" "github.com/tendermint/tendermint/types" ) @@ -15,13 +15,15 @@ type store struct { txs map[types.TxKey]*wrappedTx committedTxs map[types.TxKey]struct{} reservedTxs map[types.TxKey]struct{} + logger log.Logger } -func newStore() *store { +func newStore(logger log.Logger) *store { return &store{ bytes: 0, txs: make(map[types.TxKey]*wrappedTx), reservedTxs: make(map[types.TxKey]struct{}), + logger: logger, } } @@ -81,6 +83,11 @@ func (s *store) markAsCommitted(txKeys []types.TxKey) { s.bytes -= tx.size() delete(s.txs, key) s.committedTxs[key] = struct{}{} + } else { + if s.logger != nil { + s.logger.Error("oh shit no tx was removed when committing") + } + } } } @@ -99,7 +106,6 @@ func (s *store) remove(txKey types.TxKey) bool { return false } s.bytes -= tx.size() - fmt.Println("DELETING TX IS THIS ALLOWED?") delete(s.txs, txKey) return true } @@ -215,7 +221,6 @@ func (s *store) purgeExpiredTxs(expirationHeight int64, expirationAge time.Time) for key, tx := range s.txs { if tx.height < expirationHeight || tx.timestamp.Before(expirationAge) { s.bytes -= tx.size() - fmt.Println("DELETING TX EXCUSE ME NO NO NO NO NO") delete(s.txs, key) purgedTxs = append(purgedTxs, tx) counter++ diff --git a/mempool/cat/store_test.go b/mempool/cat/store_test.go index 6068ca21ad..f6d5853f50 100644 --- a/mempool/cat/store_test.go +++ b/mempool/cat/store_test.go @@ -12,7 +12,7 @@ import ( ) func TestStoreSimple(t *testing.T) { - store := newStore() + store := newStore(nil) tx := types.Tx("tx1") key := tx.Key() @@ -43,7 +43,7 @@ func TestStoreSimple(t *testing.T) { } func TestStoreReservingTxs(t *testing.T) { - store := newStore() + store := newStore(nil) tx := types.Tx("tx1") key := tx.Key() @@ -81,7 +81,7 @@ func TestStoreReservingTxs(t *testing.T) { } func TestReadReserved(t *testing.T) { - store := newStore() + store := newStore(nil) tx := types.Tx("tx1") store.reserve(tx.Key()) @@ -92,7 +92,7 @@ func TestReadReserved(t *testing.T) { } func TestStoreConcurrentAccess(t *testing.T) { - store := newStore() + store := newStore(nil) numTxs := 100 @@ -132,7 +132,7 @@ func TestStoreConcurrentAccess(t *testing.T) { } func TestStoreGetTxs(t *testing.T) { - store := newStore() + store := newStore(nil) numTxs := 100 for i := 0; i < numTxs; i++ { @@ -163,7 +163,7 @@ func TestStoreGetTxs(t *testing.T) { } func TestStoreExpiredTxs(t *testing.T) { - store := newStore() + store := newStore(nil) numTxs := 100 for i := 0; i < numTxs; i++ { tx := types.Tx(fmt.Sprintf("tx%d", i)) diff --git a/pkg/trace/schema/mempool.go b/pkg/trace/schema/mempool.go index d9e7d5eab7..8e78e9831f 100644 --- a/pkg/trace/schema/mempool.go +++ b/pkg/trace/schema/mempool.go @@ -174,3 +174,27 @@ func WriteMempoolRejected( Error: errS, }) } + +const ( + MempoolSizeTable = "mempool_size" +) + +type MempoolSize struct { + Size int `json:"size"` + Bytes int `json:"bytes"` +} + +func (m MempoolSize) Table() string { + return MempoolSizeTable +} + +func WriteMempoolSize( + client trace.Tracer, + size, + bytes int, +) { + client.Write(MempoolSize{ + Size: size, + Bytes: bytes, + }) +} diff --git a/types/part_set.go b/types/part_set.go index ed6b6aaffb..f66c602d92 100644 --- a/types/part_set.go +++ b/types/part_set.go @@ -65,11 +65,11 @@ func (part *Part) ToProto() (*cmtproto.Part, error) { return nil, errors.New("nil part") } pb := new(cmtproto.Part) - proof := part.Proof.ToProto() + // proof := part.Proof.ToProto() pb.Index = part.Index pb.Bytes = part.Bytes - pb.Proof = *proof + // pb.Proof = *proof return pb, nil } From be72d877c09458d62b7622c15eccca0359057eff Mon Sep 17 00:00:00 2001 From: evan-forbes <evan.samuel.forbes@gmail.com> Date: Mon, 7 Oct 2024 02:56:02 -0500 Subject: [PATCH 127/127] feat: trace memool seens directly --- mempool/cat/cache.go | 10 ++++----- mempool/cat/pool.go | 45 +++++++++++++++++++++++-------------- mempool/cat/reactor.go | 4 +++- mempool/cat/sorted_queue.go | 2 +- pkg/trace/schema/mempool.go | 34 ++++++++++++++++++++++++++++ 5 files changed, 71 insertions(+), 24 deletions(-) diff --git a/mempool/cat/cache.go b/mempool/cat/cache.go index 39188b9748..87c3968620 100644 --- a/mempool/cat/cache.go +++ b/mempool/cat/cache.go @@ -113,9 +113,9 @@ func NewSeenTxSet() *SeenTxSet { } } -func (s *SeenTxSet) Add(txKey types.TxKey, peer uint16) bool { +func (s *SeenTxSet) Add(txKey types.TxKey, peer uint16) (bool, int) { if peer == 0 { - return false + return false, 0 } s.mtx.Lock() defer s.mtx.Unlock() @@ -125,14 +125,14 @@ func (s *SeenTxSet) Add(txKey types.TxKey, peer uint16) bool { peers: map[uint16]struct{}{peer: {}}, time: time.Now().UTC(), } - return true + return true, 1 } else { _, seen := seenSet.peers[peer] if seen { - return false + return false, 0 } seenSet.peers[peer] = struct{}{} - return true + return true, len(seenSet.peers) } } diff --git a/mempool/cat/pool.go b/mempool/cat/pool.go index f70558ee1a..ccf47f6e98 100644 --- a/mempool/cat/pool.go +++ b/mempool/cat/pool.go @@ -41,8 +41,31 @@ var ( // set the default to 5, but this value can be changed in an init func InclusionDelay = 5 * time.Second defaultSeenLimit = 0 + SeenLimit = defaultSeenLimit ) +func init() { + sl := os.Getenv("SEEN_LIMIT") + if sl == "" { + return + } + parsed, err := strconv.ParseInt(sl, 10, 64) + if err != nil { + return + } + SeenLimit = int(parsed) + + id := os.Getenv("INCLUSION_DELAY") + if id == "" { + return + } + idp, err := strconv.ParseInt(id, 10, 64) + if err != nil { + return + } + InclusionDelay = time.Second * time.Duration(idp) +} + // TxPoolOption sets an optional parameter on the TxPool. type TxPoolOption func(*TxPool) @@ -453,10 +476,10 @@ func (txmp *TxPool) Flush() { } // PeerHasTx marks that the transaction has been seen by a peer. -func (txmp *TxPool) PeerHasTx(peer uint16, txKey types.TxKey) bool { - success := txmp.seenByPeersSet.Add(txKey, peer) +func (txmp *TxPool) PeerHasTx(peer uint16, txKey types.TxKey) (bool, int) { + success, seen := txmp.seenByPeersSet.Add(txKey, peer) // txmp.logger.Info("peer has tx", "success", success, "peer", peer, "txKey", fmt.Sprintf("%X", txKey)) - return success + return success, seen } // allEntriesSorted returns a slice of all the transactions currently in the @@ -480,7 +503,7 @@ func (txmp *TxPool) seenEntries(seenLimit int) []*wrappedTx { // Preallocate a slice to avoid reallocations prunedTxs := make([]*wrappedTx, 0, len(txs)) - // Prune transactions that don't exceed the seenLimit + // Prune transactions that exceed the seenLimit for _, tx := range txs { seen := txmp.seenByPeersSet.GetSeenCount(tx.key) if seen >= seenLimit { @@ -518,7 +541,7 @@ func (txmp *TxPool) ReapMaxBytesMaxGas(maxBytes, maxGas int64) (types.Txs, []typ var keep []types.Tx //nolint:prealloc var keys []types.TxKey //nolint:prealloc - for _, w := range txmp.seenEntries(getSeenLimit()) { + for _, w := range txmp.seenEntries(SeenLimit) { // skip transactions that have been in the mempool for less than the inclusion delay // This gives time for the transaction to be broadcast to all peers // if currentTime.Sub(w.timestamp) < InclusionDelay { @@ -568,18 +591,6 @@ func (txmp *TxPool) ReapMaxTxs(max int) types.Txs { return keep } -func getSeenLimit() int { - evar := os.Getenv("SEEN_LIMIT") - if evar == "" { - return defaultSeenLimit - } - parsed, err := strconv.ParseInt(evar, 10, 64) - if err != nil { - return defaultSeenLimit - } - return int(parsed) -} - // Update removes all the given transactions from the mempool and the cache, // and updates the current block height. The blockTxs and deliverTxResponses // must have the same length with each response corresponding to the tx at the diff --git a/mempool/cat/reactor.go b/mempool/cat/reactor.go index d8947c8d39..d89750e0f3 100644 --- a/mempool/cat/reactor.go +++ b/mempool/cat/reactor.go @@ -353,7 +353,8 @@ func (memR *Reactor) ReceiveEnvelope(e p2p.Envelope) { msg.Peer, ) peerID := memR.ids.GetIDForPeer(p2p.ID(msg.Peer)) - success := memR.mempool.PeerHasTx(peerID, txKey) + + success, seen := memR.mempool.PeerHasTx(peerID, txKey) // return as this peer has already seen this tx if !success { return @@ -363,6 +364,7 @@ func (memR *Reactor) ReceiveEnvelope(e p2p.Envelope) { return } + schema.WriteMempoolSeen(memR.traceClient, msg.TxKey, seen) go memR.broadcastSeenTx(types.TxKey(msg.TxKey), msg.Peer) // Check if we don't already have the transaction and that it was recently rejected diff --git a/mempool/cat/sorted_queue.go b/mempool/cat/sorted_queue.go index 95c405f6aa..99681e910c 100644 --- a/mempool/cat/sorted_queue.go +++ b/mempool/cat/sorted_queue.go @@ -58,7 +58,7 @@ func (tp *TxPrioritizor) AddPeer(peer p2p.Peer) { case <-sq.ready: otx, has := sq.Pop() if !has || otx == nil { - panic("nil tx popped when we were supposed to have txs to send") + // panic("nil tx popped when we were supposed to have txs to send") } wtx := otx.(*wrappedTx) // btw, this is only required cause the heap package doesn't yet use generics diff --git a/pkg/trace/schema/mempool.go b/pkg/trace/schema/mempool.go index 8e78e9831f..8a5faef6f3 100644 --- a/pkg/trace/schema/mempool.go +++ b/pkg/trace/schema/mempool.go @@ -12,6 +12,8 @@ func MempoolTables() []string { MempoolPeerStateTable, MempoolRecoveryTable, MempoolRejectedTable, + MempoolSizeTable, + MempoolSeenTable, } } @@ -193,8 +195,40 @@ func WriteMempoolSize( size, bytes int, ) { + if !client.IsCollecting(MempoolSizeTable) { + return + } client.Write(MempoolSize{ Size: size, Bytes: bytes, }) } + +const ( + MempoolSeenTable = "mempool_seen" +) + +type MempoolSeen struct { + Tx string `json:"tx"` + Count int `json:"count"` +} + +func (m MempoolSeen) Table() string { + return MempoolSeenTable +} + +func WriteMempoolSeen( + client trace.Tracer, + txHash []byte, + count int, +) { + // this check is redundant to what is checked during client.Write, although it + // is an optimization to avoid allocations from creating the map of fields. + if !client.IsCollecting(MempoolSeenTable) { + return + } + client.Write(MempoolSeen{ + Tx: bytes.HexBytes(txHash).String(), + Count: count, + }) +}