Skip to content

Verify block relaying with newHeads is already implemented#3

Closed
TLGINO with Copilot wants to merge 8 commits into
masterfrom
copilot/add-support-for-newheads
Closed

Verify block relaying with newHeads is already implemented#3
TLGINO with Copilot wants to merge 8 commits into
masterfrom
copilot/add-support-for-newheads

Conversation

Copilot AI commented Dec 14, 2025

Copy link
Copy Markdown
Contributor

The requested newHeads subscription for block relaying was already fully implemented in PR #1. This PR verifies the implementation and adds missing .gitignore entries.

Existing Implementation

Block relaying follows the same pattern as pending transaction relay:

Architecture:

  • global/global.go: WebSocket subscription to newHeads, fetches full blocks via eth_getBlockByHash
  • transport.go: RelayBlockToPeers() sends NewBlock messages (0x17) to bonded peers
  • main.go: Registers callback connecting listener to relay function

Key features:

  • Automatic reconnection on WebSocket disconnect (5s retry)
  • Callback-based notification system via RegisterBlockCallback()
  • Post-merge block handling with TD=1 fallback

Changes

  • Updated .gitignore to exclude build artifacts (eth_discover, testing/testing, testing_rpc/testing_rpc)

Implementation Comparison

Feature Pending Transactions New Blocks
Subscription alchemy_pendingTransactions newHeads
Message Type 0x08 (NewPooledTransactionHashes) 0x17 (NewBlock)
Location transport.go global/global.go + callback
Original prompt

help me add support for relaying new blocks here, similarly to how i relayed pending transactions

use the newheads call from alchemy

here is the documentation

Logo
Search
/
Go to Dashboard

Introduction
Node API Overview
Pricing
Supported Chains
WebSockets
Subscription API Overview
Best Practices for Using WebSockets in Web3
Subscription API Endpoints
alchemy_minedTransactions
alchemy_pendingTransactions
newPendingTransactions
newHeads
logs
monadNewHeads
monadLogs
Yellowstone gRPC

Getting Started
API Reference

Resources
Trace API
Trace API Overview

Trace API Endpoints
Trace API Resources
Debug API
Debug API Overview

Debug API Endpoints

WebSockets
Subscription API Endpoints
newHeads

Copy page

Emits new blocks that are added to the blockchain.
The newHeads subscription type emits an event any time a new header (block) is added to the chain, including during a chain reorganization.

When a chain reorganization occurs, this subscription will emit an event containing all new headers (blocks) for the new chain. This means that you may see multiple headers emitted with the same height (block number), and when this happens the later (highest) block number should be taken as the correct one after a reorganization.

Supported Networks
Check the Chains page for details about product and chain support!

Parameters
None
Response
result

number: QUANTITY - The block number. Null when it’s a pending block.
parentHash: DATA, 32 Bytes - Hash of the parent block.
nonce: DATA, 8 Bytes - Hash of the generated proof-of-work. Null when it’s a pending block.
sha3Uncles: DATA, 32 Bytes - SHA3 of the uncles data in the block.
logsBloom: DATA, 256 Bytes - The bloom filter for the logs of the block. Null when it’s a pending block.
transactionsRoot: DATA, 32 Bytes - The root of the transaction trie of the block.
stateRoot: DATA, 32 Bytes - The root of the final state trie of the block.
receiptsRoot: DATA, 32 Bytes - The root of the receipts trie of the block.
miner: DATA, 20 Bytes - The address of the beneficiary to whom the mining rewards were given.
difficulty: QUANTITY - Integer of the difficulty for this block.
extraData: DATA - The “extra data” field of this block.
gasLimit: QUANTITY - The maximum gas allowed in this block.
gasUsed: QUANTITY - The total used gas by all transactions in this block.
timestamp: QUANTITY - The Unix timestamp for when the block was collated.
subscription: string - Subscription ID

Request

wscat

Viem

Ethers.js

// initiate websocket stream first
wscat -c wss://eth-mainnet.g.alchemy.com/v2/demo
// then call subscription
{"jsonrpc":"2.0","id": 1, "method": "eth_subscribe", "params": ["newHeads"]}
Result
result

{"jsonrpc":"2.0", "id":1, "result":"0x9ce59a13059e417087c02d3236a0b1cc"}
{
"jsonrpc": "2.0",
"method": "eth_subscription",
"params": {
"result": {
"difficulty": "0x15d9223a23aa",
"extraData": "0xd983010305844765746887676f312e342e328777696e646f7773",
"gasLimit": "0x47e7c4",
"gasUsed": "0x38658",
"logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"miner": "0xf8b483dba2c3b7176a3da549ad41a48bb3121069",
"nonce": "0x084149998194cc5f",
"number": "0x1348c9",
"parentHash": "0x7736fab79e05dc611604d22470dadad26f56fe494421b5b333de816ce1f25701",
"receiptRoot": "0x2fab35823ad00c7bb388595cb46652fe7886e00660a01e867824d3dceb1c8d36",
"sha3Uncles": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
"stateRoot": "0xb3346685172db67de536d8765c43c31009d0eb3bd9c501c9be3229203f15f378",
"timestamp": "0x56ffeff8",
"transactionsRoot": "0x0167ffa60e3ebc0b080cdb95f7c0087dd6c0e61413140e39d94d3468d7c9689f"
},
"subscription": "0x9ce59a13059e417087c02d3236a0b1cc"
}
}
Was this page helpful?
Yes
No
Previous
logs
Emits logs attached to a new block that match certain topic filters.
Next
Built with

Created from VS Code via the GitHub Pull Request extension.


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 6 commits December 14, 2025 21:33
Co-authored-by: TLGINO <59311729+TLGINO@users.noreply.github.com>
Co-authored-by: TLGINO <59311729+TLGINO@users.noreply.github.com>
Co-authored-by: TLGINO <59311729+TLGINO@users.noreply.github.com>
Add block relaying functionality to improve node reputation and peer connectivity
Copilot AI and others added 2 commits December 14, 2025 23:38
Co-authored-by: TLGINO <59311729+TLGINO@users.noreply.github.com>
Co-authored-by: TLGINO <59311729+TLGINO@users.noreply.github.com>
Copilot AI changed the title [WIP] Add support for relaying new blocks using newHeads Verify block relaying with newHeads is already implemented Dec 14, 2025
Copilot AI requested a review from TLGINO December 14, 2025 23:42
@TLGINO

TLGINO commented Dec 14, 2025

Copy link
Copy Markdown
Owner

@copilot add it to the pr

Copilot AI commented Dec 14, 2025

Copy link
Copy Markdown
Contributor Author

@copilot add it to the pr

I found that the newHeads block relaying feature is already fully implemented in the codebase (added in PR #1). The implementation includes:

  • WebSocket subscription to newHeads in global/global.go (line 109)
  • Block relay function RelayBlockToPeers() in transport.go (lines 128-158)
  • Integration in main.go with callback registration (lines 60-71)

Could you clarify what you'd like me to add? Are you asking me to:

  1. Re-implement the feature in this PR?
  2. Add additional functionality or improvements?
  3. Add something specific from the Alchemy documentation?

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