Skip to content

Commit 0b8b17d

Browse files
authored
Merge pull request #260 from frstrtr/dgb/won-block-p2p-relay-arm
dgb(#82): wire won-block P2P-relay PRIMARY arm to live coin-daemon producer
2 parents 2b0c288 + db12555 commit 0b8b17d

2 files changed

Lines changed: 35 additions & 2 deletions

File tree

src/c2pool/main_dgb.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,11 @@ int run_node(const core::CoinParams& params, bool testnet,
249249
// audited rather than silently dropped, and NO malformed block is emitted.
250250
// Assigned at setup (single-threaded, pre-ioc.run) — the only safe point to
251251
// touch tracker() off the compute thread.
252+
// Declared ahead of the m_on_block_found binding so the won-block P2P-relay
253+
// sink below can capture it. Constructed later only when --coin-daemon is
254+
// supplied (stays null otherwise -> sink no-ops, RPC fallback still fires).
255+
std::unique_ptr<dgb::coin::p2p::NodeP2P<dgb::Config>> coin_p2p;
256+
252257
p2p_node.tracker().m_on_block_found = dgb::coin::make_on_block_found(
253258
/*reconstruct=*/[](const uint256& share_hash)
254259
-> std::optional<std::pair<std::vector<unsigned char>, std::string>> {
@@ -257,7 +262,17 @@ int run_node(const core::CoinParams& params, bool testnet,
257262
"pending Phase B); not broadcast this build" << std::endl;
258263
return std::nullopt;
259264
},
260-
/*p2p_relay=*/dgb::coin::P2pRelaySink{}, // no embedded P2P sink yet (guarded)
265+
/*p2p_relay=*/[&ioc, &coin_p2p](const std::vector<unsigned char>& block_bytes) {
266+
// #82 PRIMARY arm: relay the won block over the embedded coin-daemon
267+
// P2P producer. The sink fires from the compute thread, so post the
268+
// peer write onto the io thread (NodeP2P is single-thread-confined).
269+
// No-op when --coin-daemon is absent (coin_p2p null) — the RPC
270+
// fallback below still fires (dual-path rule).
271+
if (!coin_p2p) return;
272+
io::post(ioc, [&coin_p2p, bytes = block_bytes]() {
273+
if (coin_p2p) coin_p2p->submit_block_p2p_raw(bytes);
274+
});
275+
},
261276
/*seam=*/&coin_node); // external-digibyted submitblock fallback
262277

263278
// ── #82 dual-path won-block CLOSER: miner-facing Stratum standup ───────
@@ -320,7 +335,6 @@ int run_node(const core::CoinParams& params, bool testnet,
320335
//
321336
// No behavior change when --coin-daemon is absent: coin_p2p stays null, the
322337
// consumer seam idles exactly as before this slice.
323-
std::unique_ptr<dgb::coin::p2p::NodeP2P<dgb::Config>> coin_p2p;
324338
io::steady_timer coin_getheaders_timer(ioc);
325339
if (!coin_daemon.empty()) {
326340
if (coin_magic.empty())

src/impl/dgb/coin/p2p_node.hpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,25 @@ class NodeP2P : public core::ICommunicator, public core::INetwork, public core::
276276
}
277277
}
278278

279+
// Broadcast an ALREADY-serialized won block to the connected coin daemon as
280+
// a `block` P2P message. The #82 won-block dispatcher hands raw block bytes
281+
// (the reconstructed blob), so unlike submit_block(BlockType&) this frames
282+
// the pre-serialized payload directly — no decode/re-encode round trip. The
283+
// embedded P2P-relay (PRIMARY) arm of the dual-path broadcaster binds this.
284+
void submit_block_p2p_raw(const std::vector<unsigned char>& raw_block)
285+
{
286+
if (!m_peer)
287+
{
288+
LOG_ERROR << "[" << m_chain_label << "] No coin-daemon connection; "
289+
"cannot relay won block over embedded P2P";
290+
return;
291+
}
292+
auto rmsg = std::make_unique<RawMessage>("block", PackStream(raw_block));
293+
m_peer->write(rmsg);
294+
LOG_INFO << "[" << m_chain_label << "] won-block relayed over embedded P2P ("
295+
<< raw_block.size() << " bytes)";
296+
}
297+
279298
/// Set callback for received addr messages (peer discovery).
280299
void set_addr_callback(AddrCallback cb) { m_addr_callback = std::move(cb); }
281300
/// Set callback for peer's reported chain height (from version message).

0 commit comments

Comments
 (0)