Skip to content
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

Add disable tx gossip option #13

Merged
merged 3 commits into from
Jun 27, 2023
Merged

Conversation

pcw109550
Copy link
Member

@pcw109550 pcw109550 commented Jun 15, 2023

Definition of Transaction Gossip Disabling

Tx gossip disabling means there is no p2p communication related with transactions. More specifically, there must be no broadcasting and announcement. See ethereum/EIPs#2465 for more details about broadcasting and announcement. This is not erigon specifc, but an Ethereum spec.

This duality allows nodes to do the high-bandwidth broadcasting (10s-100s KB) for a logarithmic number of peers; and the low-bandwidth announcing (10s-100s B) for the remaining linear number of peers.

Erigon-lib Txpool Implementation Structure

One liner: fetch ➡️ pool ➡️send.

Txpool must

  1. Receive remote transaction via p2p tx gossiping. Done at txpool/fetch.go::handleInboundMessage method.
    • Called Periodically when Fetch struct is initialized.
    • The method calls txpool/pool.go::AddRemoteTxs method defined at Pool interface.
  2. Receive local transaction via RPC and create announcements(newTxs <-) for local tx. Done at txpool/pool.go::AddLocalTxs method.
    • Called only when sendTransaction RPC is called.
  3. Create announcements(newTxs <-) for remote transaction. Done at txpool/pool.go::processRemoteTxs method.
    • Called Periodically(case <-processRemoteTxsEvery.C) when txpool's MainLoop method is activated. It checks if there are received remote transactions at step 1.
  4. Gossip local/remote transaction to current peers. Done at txpool/send.go::BroadcastPooledTxs, txpool.send.go::AnnouncePooledTxs.
    • Called when txpool's MainLoop method is activated and announcements are ready from channel(case announcements := <-newTxs).
  5. Gossip local/remote transaction to new peers. Done at txpool/send.go::PropagatePooledTxsToPeersList.
    • Called when new peer is added.

Adding Disable Tx Gossip Option

To disable tx gossiping, I must turn off Step 1, 4, 5; to not listen remote tx and stop outbound p2p traffic.

To achieve this, I added field NoTxGossip boolean at txpoolCfg struct.
This value will be injected from initializing txpool by calling txpool/txpooluitl/all_components.go::AllComponents.

When NoTxGossip flag is on,Pool interface will be initialized using newly added TxPoolDropRemote struct.
TxPoolDropRemote inherits TxPool but overrides AddRemoteTxs.

AddRemoteTxs method from TxPool implementation fills in unprocessedRemoteByHash, unprocessedRemoteTx. This unprocessed remote information will be consumed at processRemoteTxs method.

Stop Digesting Remote Transactions: Disabling Step 1 and Step 3

I disabled AddRemoteTxs to do nothing(empty implementation). Therefore unprocessedRemoteByHash, unprocessedRemoteTx will be never filled. So even if we call processRemoteTxs in txpool/pool.go::MainLoop, it will have no effect. This change explicitly stops step 1, and implicitly stops step 3. There is no remote transactions stacked so no remote transactions to be processed.

Stop Outbound P2P Traffic: Disabling Step 4

I patched txpool/pool.go::MainLoop to avoid callingtxpool/send.go::BroadcastPooledTxs, txpool.send.go::AnnouncePooledTxs when announcement was generated. By this way, newTx channel will be emptied, not stacked forever. Even though I disabled announcement creation for remote transactions, there will be announcements created when calling AddLocalTx method so emptying is necessary.

		case announcements := <-newTxs:
			go func() {
				...
				if p.cfg.NoTxGossip {
					// drain newTxs for emptying newTx channel
					// newTx channel will be filled only with local transactions
					// early return to avoid outbound transaction propagation
					log.Debug("[txpool] tx gossip disabled", "state", "drain new transactions")
					return
				}
...

Stop Outbound P2P Traffic: Disabling Step 5

I patched not to call txpool/send.go::PropagatePooledTxsToPeersList. We must clean new peers, so recentlyConnectedPeers.GetAndClean method must be called first.

case <-syncToNewPeersEvery.C: // new peer
			newPeers := p.recentlyConnectedPeers.GetAndClean()
			if len(newPeers) == 0 {
				continue
			}
			if p.cfg.NoTxGossip {
				// avoid transaction gossiping for new peers
				log.Debug("[txpool] tx gossip disabled", "state", "sync new peers")
				continue
			}
...

Testing

Added txpool/pool_test.go::TestDropRemote. It initializes TxPoolDropRemote struct and checks below two conditions:

  • Local transactions are received and announcement is created.
  • Remote transactions are received but announcement is not created. This is because I overrided AddRemoteTxs method to do nothing.

@pcw109550 pcw109550 self-assigned this Jun 15, 2023
@pcw109550 pcw109550 changed the title Add flag to disable tx gossip Add disable tx gossip option Jun 16, 2023
@pcw109550 pcw109550 merged commit a6b80ff into op-erigon Jun 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants