Repository navigation
Add validator to pubsub and stop gossiping transactions twice #97
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
257da78
cae6a77
46f82de
d88c5fa
533add6
6dc8e73
4097620
ac95d91
fd1a422
0b62bb4
5ca6741
696a3c0
609e8c1
80fb98b
57f1f1b
14c27f1
0d00994
7ecbbba
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -61,8 +61,8 @@ func (ids *mempoolIDs) Reclaim(peer peer.ID) { | |
|
|
||
| // GetForPeer returns an ID for the peer. ID is generated if required. | ||
| func (ids *mempoolIDs) GetForPeer(peer peer.ID) uint16 { | ||
| ids.mtx.RLock() | ||
| defer ids.mtx.RUnlock() | ||
| ids.mtx.Lock() | ||
| defer ids.mtx.Unlock() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice find. It's obviously not just read here. |
||
|
|
||
| id, ok := ids.peerMap[peer] | ||
| if !ok { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,12 +2,14 @@ package node | |
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
|
|
||
| "github.com/libp2p/go-libp2p-core/crypto" | ||
| "github.com/libp2p/go-libp2p-core/peer" | ||
| pubsub "github.com/libp2p/go-libp2p-pubsub" | ||
| abci "github.com/tendermint/tendermint/abci/types" | ||
| llcfg "github.com/tendermint/tendermint/config" | ||
| "github.com/tendermint/tendermint/libs/clist" | ||
| "github.com/tendermint/tendermint/libs/log" | ||
| "github.com/tendermint/tendermint/libs/service" | ||
| corep2p "github.com/tendermint/tendermint/p2p" | ||
|
|
@@ -91,6 +93,10 @@ func NewNode(ctx context.Context, conf config.NodeConfig, nodeKey crypto.PrivKey | |
| } | ||
|
|
||
| mp := mempool.NewCListMempool(llcfg.DefaultMempoolConfig(), proxyApp.Mempool(), 0) | ||
| mpIDs := newMempoolIDs() | ||
|
|
||
| txValidator := newTxValidator(mp, mpIDs, logger) | ||
| client.SetTxValidator(txValidator) | ||
|
|
||
| var aggregator *aggregator = nil | ||
| if conf.Aggregator { | ||
|
|
@@ -109,7 +115,7 @@ func NewNode(ctx context.Context, conf config.NodeConfig, nodeKey crypto.PrivKey | |
| aggregator: aggregator, | ||
| dalc: dalc, | ||
| Mempool: mp, | ||
| mempoolIDs: newMempoolIDs(), | ||
| mempoolIDs: mpIDs, | ||
| incomingTxCh: make(chan *p2p.GossipMessage), | ||
| Store: store, | ||
| ctx: ctx, | ||
|
|
@@ -119,68 +125,6 @@ func NewNode(ctx context.Context, conf config.NodeConfig, nodeKey crypto.PrivKey | |
| return node, nil | ||
| } | ||
|
|
||
| func (n *Node) mempoolReadLoop(ctx context.Context) { | ||
| for { | ||
| select { | ||
| case tx := <-n.incomingTxCh: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My comment was discarded somehow O_o I think it's reasonable, to keep handler and validator separate (see #100 (comment)).
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. regarding the initial question: |
||
| n.Logger.Debug("tx received", "from", tx.From, "bytes", len(tx.Data)) | ||
| err := n.Mempool.CheckTx(tx.Data, func(resp *abci.Response) {}, mempool.TxInfo{ | ||
| SenderID: n.mempoolIDs.GetForPeer(tx.From), | ||
| SenderP2PID: corep2p.ID(tx.From), | ||
| }) | ||
| if err != nil { | ||
| n.Logger.Error("failed to execute CheckTx", "error", err) | ||
| } | ||
| case <-ctx.Done(): | ||
| return | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func (n *Node) mempoolPublishLoop(ctx context.Context) { | ||
| rawMempool := n.Mempool.(*mempool.CListMempool) | ||
| var next *clist.CElement | ||
|
|
||
| for { | ||
| // wait for transactions | ||
| if next == nil { | ||
| select { | ||
| case <-rawMempool.TxsWaitChan(): | ||
| if next = rawMempool.TxsFront(); next != nil { | ||
| continue | ||
| } | ||
| case <-ctx.Done(): | ||
| return | ||
| } | ||
| } | ||
|
|
||
| // send transactions | ||
| for { | ||
| memTx := next.Value.(*mempool.MempoolTx) | ||
| tx := memTx.Tx | ||
|
|
||
| err := n.P2P.GossipTx(ctx, tx) | ||
| if err != nil { | ||
| n.Logger.Error("failed to gossip transaction", "error", err) | ||
| continue | ||
| } | ||
|
|
||
| nx := next.Next() | ||
| if nx == nil { | ||
| break | ||
| } | ||
| next = nx | ||
| } | ||
|
|
||
| select { | ||
| case <-next.NextWaitChan(): | ||
| next = next.Next() | ||
| case <-ctx.Done(): | ||
| return | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // OnStart is a part of Service interface. | ||
| func (n *Node) OnStart() error { | ||
| n.Logger.Info("starting P2P client") | ||
|
|
@@ -192,14 +136,9 @@ func (n *Node) OnStart() error { | |
| if err != nil { | ||
| return fmt.Errorf("error while starting data availability layer client: %w", err) | ||
| } | ||
| go n.mempoolReadLoop(n.ctx) | ||
| go n.mempoolPublishLoop(n.ctx) | ||
| if n.conf.Aggregator { | ||
| go n.aggregator.aggregationLoop(n.ctx) | ||
| } | ||
| n.P2P.SetTxHandler(func(tx *p2p.GossipMessage) { | ||
| n.incomingTxCh <- tx | ||
| }) | ||
|
|
||
| return nil | ||
| } | ||
|
|
@@ -235,3 +174,33 @@ func (n *Node) EventBus() *lltypes.EventBus { | |
| func (n *Node) ProxyApp() proxy.AppConns { | ||
| return n.proxyApp | ||
| } | ||
|
|
||
| // newTxValidator creates a pubsub validator that uses the node's mempool to check the | ||
| // transaction. If the transaction is valid, then it is added to the mempool | ||
| func newTxValidator(pool mempool.Mempool, poolIDs *mempoolIDs, logger log.Logger) pubsub.Validator { | ||
| return func(ctx context.Context, i peer.ID, m *pubsub.Message) bool { | ||
| logger.Debug("transaction of size %d recieved", "bytes", len(m.Data)) | ||
| checkTxResCh := make(chan *abci.Response, 1) | ||
| err := pool.CheckTx(m.Data, func(resp *abci.Response) { | ||
| checkTxResCh <- resp | ||
| }, mempool.TxInfo{ | ||
| SenderID: poolIDs.GetForPeer(m.GetFrom()), | ||
| SenderP2PID: corep2p.ID(m.GetFrom()), | ||
| }) | ||
| switch { | ||
| case errors.Is(err, mempool.ErrTxInCache): | ||
| return true | ||
| case errors.Is(err, mempool.ErrMempoolIsFull{}): | ||
| return true | ||
| case errors.Is(err, mempool.ErrTxTooLarge{}): | ||
| return false | ||
| case errors.Is(err, mempool.ErrPreCheck{}): | ||
|
Comment on lines
+191
to
+197
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Orthogonal to this PR but the errors should consistently be vars and wrapped using |
||
| return false | ||
| default: | ||
| } | ||
| res := <-checkTxResCh | ||
| checkTxResp := res.GetCheckTx() | ||
|
|
||
| return checkTxResp.Code == abci.CodeTypeOK | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -101,7 +101,7 @@ func (l *Local) BroadcastTxCommit(ctx context.Context, tx types.Tx) (*ctypes.Res | |
| } | ||
| }() | ||
|
|
||
| // Broadcast tx and wait for CheckTx result | ||
| // add to mempool and wait for CheckTx result | ||
| checkTxResCh := make(chan *abci.Response, 1) | ||
| err = l.node.Mempool.CheckTx(tx, func(res *abci.Response) { | ||
| checkTxResCh <- res | ||
|
|
@@ -120,6 +120,12 @@ func (l *Local) BroadcastTxCommit(ctx context.Context, tx types.Tx) (*ctypes.Res | |
| }, nil | ||
| } | ||
|
|
||
| // broadcast tx | ||
| err = l.node.P2P.GossipTx(ctx, tx) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("tx added to local mempool but failure to broadcast: %w", err) | ||
| } | ||
|
|
||
|
Comment on lines
+123
to
+128
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I still think it's weird that the p2p gossiping is now triggered in rpcclient but yeah let's fix this in a followup PR. |
||
| // Wait for the tx to be included in a block or timeout. | ||
| select { | ||
| case msg := <-deliverTxSub.Out(): // The tx was included in a block. | ||
|
|
@@ -160,10 +166,14 @@ func (l *Local) BroadcastTxCommit(ctx context.Context, tx types.Tx) (*ctypes.Res | |
| // More: https://docs.tendermint.com/master/rpc/#/Tx/broadcast_tx_async | ||
| func (l *Local) BroadcastTxAsync(ctx context.Context, tx types.Tx) (*ctypes.ResultBroadcastTx, error) { | ||
| err := l.node.Mempool.CheckTx(tx, nil, mempool.TxInfo{}) | ||
|
|
||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| // gossipTx optimistically | ||
| err = l.node.P2P.GossipTx(ctx, tx) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("tx added to local mempool but failed to gossip: %w", err) | ||
| } | ||
| return &ctypes.ResultBroadcastTx{Hash: tx.Hash()}, nil | ||
| } | ||
|
|
||
|
|
@@ -180,6 +190,22 @@ func (l *Local) BroadcastTxSync(ctx context.Context, tx types.Tx) (*ctypes.Resul | |
| } | ||
| res := <-resCh | ||
| r := res.GetCheckTx() | ||
|
|
||
| // gossip the transaction if it's in the mempool. | ||
| // Note: we have to do this here because, unlike the tendermint mempool reactor, there | ||
| // is no routine that gossips transactions after they enter the pool | ||
| if r.Code == abci.CodeTypeOK { | ||
| err = l.node.P2P.GossipTx(ctx, tx) | ||
| if err != nil { | ||
| // the transaction must be removed from the mempool if it cannot be gossiped. | ||
| // if this does not occur, then the user will not be able to try again using | ||
| // this node, as the CheckTx call above will return an error indicating that | ||
| // the tx is already in the mempool | ||
| l.node.Mempool.RemoveTxByKey(mempool.TxKey(tx), true) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. now the transaction is removed from the mempool if there's a gossiping error
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, I see. This now means that the app said this Tx is valid but no-one will ever see it because gossiping failed (and we out self did not receive it via gossiping but locally)? Is that right?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah, that's correct
Comment on lines
+194
to
+204
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added some docs |
||
| return nil, fmt.Errorf("valid tra: %w", err) | ||
| } | ||
| } | ||
|
|
||
| return &ctypes.ResultBroadcastTx{ | ||
| Code: r.Code, | ||
| Data: r.Data, | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.