Implement genesis reconciliation protocol for sidechain stability

Problem: P2Pool nodes starting at different times or experiencing network
issues would create independent genesis blocks, resulting in incompatible
chains. Nodes would ban each other for invalid blocks that were actually
valid on a different chain. Cache resume after restart frequently failed
due to genesis mismatch between nodes.

Solution:  Oldest compatible genesis wins protocol that coordinates
genesis selection across peers before mining begins.

New P2P message GENESIS_INFO exchanges:
- Genesis block hash
- Genesis timestamp
- Genesis mainchain height
- Protocol version

Startup behavior:
- Wait up to 90 seconds for peer genesis info (with progress logging)
- Adopt oldest genesis from compatible peers
- Only create own genesis if no peers respond

Late joiner reconciliation:
- Running nodes that receive older genesis from new peer will purge
  their sidechain and re-sync to the older chain
- Cache files deleted on purge to prevent reload of stale blocks

Protocol versioning:
- PROTOCOL_VERSION constant at top of side_chain.h
- Increment only on consensus-breaking changes
- Version mismatch logs warning, prevents genesis adoption

Tiebreaker: When timestamps match, lexicographically lower hash wins.
This commit is contained in:
Matt Hess
2025-12-05 23:44:53 +00:00
parent a5ee896215
commit 00fb078004
18 changed files with 1011 additions and 279 deletions
+28 -7
View File
@@ -20,6 +20,7 @@
#include "pool_block.h"
#include "p2p_server.h"
#include "side_chain.h"
#include <fstream>
LOG_CATEGORY(BlockCache)
@@ -27,6 +28,7 @@ static constexpr uint32_t BLOCK_SIZE = 96 * 1024;
static constexpr uint32_t NUM_BLOCKS = 4608;
static constexpr uint32_t CACHE_SIZE = BLOCK_SIZE * NUM_BLOCKS;
static constexpr char cache_name[] = "p2pool.cache";
static const uint64_t CACHE_VERSION = std::hash<std::string>{}(__DATE__ __TIME__);
namespace p2pool {
@@ -191,14 +193,33 @@ void BlockCache::store(const PoolBlock& block)
void BlockCache::load_all(const SideChain& side_chain, P2PServer& server)
{
if (!m_impl->m_data) {
return;
}
// Check cache version - invalidates on recompile
const std::string version_path = DATA_DIR + "p2pool.cache.version";
bool version_ok = false;
{
std::ifstream f(version_path);
uint64_t v = 0;
if ((f >> v) && (v == CACHE_VERSION)) {
version_ok = true;
}
}
if (!version_ok) {
LOGINFO(1, "cache version mismatch (recompiled binary), clearing cache");
if (m_impl->m_data) {
memset(m_impl->m_data, 0, CACHE_SIZE);
}
std::ofstream f(version_path);
f << CACHE_VERSION;
}
// Can be only called once
if (m_loadingStarted.exchange(1)) {
return;
}
if (!m_impl->m_data) {
return;
}
// Can be only called once
if (m_loadingStarted.exchange(1)) {
return;
}
LOGINFO(1, "loading cached blocks");