Removed redundant m_sideChainData

This commit is contained in:
SChernykh 2022-10-04 15:44:57 +02:00
parent 32ea0f2cf6
commit eabf856dbd
8 changed files with 50 additions and 36 deletions

View file

@ -166,9 +166,10 @@ BlockCache::~BlockCache()
void BlockCache::store(const PoolBlock& block)
{
const std::vector<uint8_t> mainchain_data = block.serialize_mainchain_data();
const std::vector<uint8_t> sidechain_data = block.serialize_sidechain_data();
const size_t n1 = mainchain_data.size();
const size_t n2 = block.m_sideChainData.size();
const size_t n2 = sidechain_data.size();
if (!m_impl->m_data || (sizeof(uint32_t) + n1 + n2 > BLOCK_SIZE)) {
return;
@ -178,7 +179,7 @@ void BlockCache::store(const PoolBlock& block)
*reinterpret_cast<uint32_t*>(data) = static_cast<uint32_t>(n1 + n2);
memcpy(data + sizeof(uint32_t), mainchain_data.data(), n1);
memcpy(data + sizeof(uint32_t) + n1, block.m_sideChainData.data(), n2);
memcpy(data + sizeof(uint32_t) + n1, sidechain_data.data(), n2);
}
void BlockCache::load_all(SideChain& side_chain, P2PServer& server)

View file

@ -530,6 +530,7 @@ void BlockTemplate::update(const MinerData& data, const Mempool& mempool, Wallet
#if POOL_BLOCK_DEBUG
const std::vector<uint8_t> mainchain_data = m_poolBlockTemplate->serialize_mainchain_data();
const std::vector<uint8_t> sidechain_data = m_poolBlockTemplate->serialize_sidechain_data();
if (mainchain_data != m_blockTemplateBlob) {
LOGERR(1, "serialize_mainchain_data() has a bug, fix it! ");
@ -545,7 +546,7 @@ void BlockTemplate::update(const MinerData& data, const Mempool& mempool, Wallet
{
std::vector<uint8_t> buf = m_blockTemplateBlob;
buf.insert(buf.end(), m_poolBlockTemplate->m_sideChainData.begin(), m_poolBlockTemplate->m_sideChainData.end());
buf.insert(buf.end(), sidechain_data.begin(), sidechain_data.end());
PoolBlock check;
const int result = check.deserialize(buf.data(), buf.size(), m_pool->side_chain(), nullptr);
@ -784,8 +785,9 @@ hash BlockTemplate::calc_sidechain_hash() const
const int blob_size = static_cast<int>(m_blockTemplateBlob.size());
const std::vector<uint8_t>& consensus_id = m_pool->side_chain().consensus_id();
const std::vector<uint8_t> sidechain_data = m_poolBlockTemplate->serialize_sidechain_data();
keccak_custom([this, sidechain_hash_offset, blob_size, consensus_id](int offset) -> uint8_t {
keccak_custom([this, sidechain_hash_offset, blob_size, consensus_id, &sidechain_data](int offset) -> uint8_t {
uint32_t k = static_cast<uint32_t>(offset - static_cast<int>(m_nonceOffset));
if (k < NONCE_SIZE) {
return 0;
@ -806,15 +808,15 @@ hash BlockTemplate::calc_sidechain_hash() const
}
const int side_chain_data_offsset = offset - blob_size;
const int side_chain_data_size = static_cast<int>(m_poolBlockTemplate->m_sideChainData.size());
const int side_chain_data_size = static_cast<int>(sidechain_data.size());
if (side_chain_data_offsset < side_chain_data_size) {
return m_poolBlockTemplate->m_sideChainData[side_chain_data_offsset];
return sidechain_data[side_chain_data_offsset];
}
const int consensus_id_offset = side_chain_data_offsset - side_chain_data_size;
return consensus_id[consensus_id_offset];
},
static_cast<int>(m_blockTemplateBlob.size() + m_poolBlockTemplate->m_sideChainData.size() + consensus_id.size()), sidechain_hash.h, HASH_SIZE);
static_cast<int>(m_blockTemplateBlob.size() + sidechain_data.size() + consensus_id.size()), sidechain_hash.h, HASH_SIZE);
return sidechain_hash;
}
@ -1073,11 +1075,12 @@ void BlockTemplate::submit_sidechain_block(uint32_t template_id, uint32_t nonce,
#if POOL_BLOCK_DEBUG
{
std::vector<uint8_t> buf = m_poolBlockTemplate->serialize_mainchain_data();
const std::vector<uint8_t> sidechain_data = m_poolBlockTemplate->serialize_sidechain_data();
memcpy(buf.data() + m_nonceOffset, &nonce, NONCE_SIZE);
memcpy(buf.data() + m_extraNonceOffsetInTemplate, &extra_nonce, EXTRA_NONCE_SIZE);
buf.insert(buf.end(), m_poolBlockTemplate->m_sideChainData.begin(), m_poolBlockTemplate->m_sideChainData.end());
buf.insert(buf.end(), sidechain_data.begin(), sidechain_data.end());
PoolBlock check;
const int result = check.deserialize(buf.data(), buf.size(), side_chain, nullptr);

View file

@ -749,12 +749,13 @@ void P2PServer::broadcast(const PoolBlock& block)
int outputs_offset, outputs_blob_size;
const std::vector<uint8_t> mainchain_data = block.serialize_mainchain_data(nullptr, nullptr, &outputs_offset, &outputs_blob_size);
const std::vector<uint8_t> sidechain_data = block.serialize_sidechain_data();
data->blob.reserve(mainchain_data.size() + block.m_sideChainData.size());
data->blob.reserve(mainchain_data.size() + sidechain_data.size());
data->blob = mainchain_data;
data->blob.insert(data->blob.end(), block.m_sideChainData.begin(), block.m_sideChainData.end());
data->blob.insert(data->blob.end(), sidechain_data.begin(), sidechain_data.end());
data->pruned_blob.reserve(mainchain_data.size() + block.m_sideChainData.size() + 16 - outputs_blob_size);
data->pruned_blob.reserve(mainchain_data.size() + sidechain_data.size() + 16 - outputs_blob_size);
data->pruned_blob.assign(mainchain_data.begin(), mainchain_data.begin() + outputs_offset);
// 0 outputs in the pruned blob
@ -770,7 +771,7 @@ void P2PServer::broadcast(const PoolBlock& block)
writeVarint(outputs_blob_size, data->pruned_blob);
data->pruned_blob.insert(data->pruned_blob.end(), mainchain_data.begin() + outputs_offset + outputs_blob_size, mainchain_data.end());
data->pruned_blob.insert(data->pruned_blob.end(), block.m_sideChainData.begin(), block.m_sideChainData.end());
data->pruned_blob.insert(data->pruned_blob.end(), sidechain_data.begin(), sidechain_data.end());
data->ancestor_hashes.reserve(block.m_uncles.size() + 1);
data->ancestor_hashes = block.m_uncles;

View file

@ -56,7 +56,6 @@ PoolBlock::PoolBlock()
m_outputs.reserve(2048);
m_transactions.reserve(256);
m_sideChainData.reserve(512);
m_uncles.reserve(8);
}
@ -80,6 +79,7 @@ PoolBlock& PoolBlock::operator=(const PoolBlock& b)
#if POOL_BLOCK_DEBUG
m_mainChainDataDebug = b.m_mainChainDataDebug;
m_sideChainDataDebug = b.m_sideChainDataDebug;
#endif
m_majorVersion = b.m_majorVersion;
@ -93,7 +93,6 @@ PoolBlock& PoolBlock::operator=(const PoolBlock& b)
m_extraNonceSize = b.m_extraNonceSize;
m_extraNonce = b.m_extraNonce;
m_transactions = b.m_transactions;
m_sideChainData = b.m_sideChainData;
m_minerWallet = b.m_minerWallet;
m_txkeySec = b.m_txkeySec;
m_parent = b.m_parent;
@ -225,34 +224,44 @@ std::vector<uint8_t> PoolBlock::serialize_mainchain_data_nolock(size_t* header_s
return data;
}
void PoolBlock::serialize_sidechain_data()
std::vector<uint8_t> PoolBlock::serialize_sidechain_data() const
{
std::vector<uint8_t> data;
MutexLock lock(m_lock);
m_sideChainData.clear();
m_sideChainData.reserve((m_uncles.size() + 4) * HASH_SIZE + 11);
data.reserve((m_uncles.size() + 4) * HASH_SIZE + 20);
const hash& spend = m_minerWallet.spend_public_key();
const hash& view = m_minerWallet.view_public_key();
m_sideChainData.insert(m_sideChainData.end(), spend.h, spend.h + HASH_SIZE);
m_sideChainData.insert(m_sideChainData.end(), view.h, view.h + HASH_SIZE);
m_sideChainData.insert(m_sideChainData.end(), m_txkeySec.h, m_txkeySec.h + HASH_SIZE);
m_sideChainData.insert(m_sideChainData.end(), m_parent.h, m_parent.h + HASH_SIZE);
data.insert(data.end(), spend.h, spend.h + HASH_SIZE);
data.insert(data.end(), view.h, view.h + HASH_SIZE);
data.insert(data.end(), m_txkeySec.h, m_txkeySec.h + HASH_SIZE);
data.insert(data.end(), m_parent.h, m_parent.h + HASH_SIZE);
writeVarint(m_uncles.size(), m_sideChainData);
writeVarint(m_uncles.size(), data);
for (const hash& id : m_uncles) {
m_sideChainData.insert(m_sideChainData.end(), id.h, id.h + HASH_SIZE);
data.insert(data.end(), id.h, id.h + HASH_SIZE);
}
writeVarint(m_sidechainHeight, m_sideChainData);
writeVarint(m_sidechainHeight, data);
writeVarint(m_difficulty.lo, m_sideChainData);
writeVarint(m_difficulty.hi, m_sideChainData);
writeVarint(m_difficulty.lo, data);
writeVarint(m_difficulty.hi, data);
writeVarint(m_cumulativeDifficulty.lo, m_sideChainData);
writeVarint(m_cumulativeDifficulty.hi, m_sideChainData);
writeVarint(m_cumulativeDifficulty.lo, data);
writeVarint(m_cumulativeDifficulty.hi, data);
#if POOL_BLOCK_DEBUG
if (!m_sideChainDataDebug.empty() && (data != m_sideChainDataDebug)) {
LOGERR(1, "serialize_sidechain_data() has a bug, fix it!");
panic();
}
#endif
return data;
}
void PoolBlock::reset_offchain_data()

View file

@ -65,6 +65,7 @@ struct PoolBlock
#if POOL_BLOCK_DEBUG
std::vector<uint8_t> m_mainChainDataDebug;
std::vector<uint8_t> m_sideChainDataDebug;
#endif
// Monero block template
@ -97,9 +98,6 @@ struct PoolBlock
// All block transaction hashes including the miner transaction hash at index 0
std::vector<hash> m_transactions;
// Side-chain data
std::vector<uint8_t> m_sideChainData;
// Miner's wallet
Wallet m_minerWallet{ nullptr };
@ -134,7 +132,7 @@ struct PoolBlock
std::vector<uint8_t> serialize_mainchain_data(size_t* header_size = nullptr, size_t* miner_tx_size = nullptr, int* outputs_offset = nullptr, int* outputs_blob_size = nullptr) const;
std::vector<uint8_t> serialize_mainchain_data_nolock(size_t* header_size, size_t* miner_tx_size, int* outputs_offset, int* outputs_blob_size) const;
void serialize_sidechain_data();
std::vector<uint8_t> serialize_sidechain_data() const;
int deserialize(const uint8_t* data, size_t size, const SideChain& sidechain, uv_loop_t* loop);
void reset_offchain_data();

View file

@ -207,9 +207,9 @@ int PoolBlock::deserialize(const uint8_t* data, size_t size, const SideChain& si
m_mainChainDataDebug.assign(data_begin, data_begin + outputs_offset);
m_mainChainDataDebug.insert(m_mainChainDataDebug.end(), outputs_blob_size, 0);
m_mainChainDataDebug.insert(m_mainChainDataDebug.end(), data_begin + outputs_offset + outputs_actual_blob_size, data);
#endif
const uint8_t* sidechain_data_begin = data;
#endif
hash spend_pub_key;
hash view_pub_key;
@ -323,7 +323,9 @@ int PoolBlock::deserialize(const uint8_t* data, size_t size, const SideChain& si
return __LINE__;
}
m_sideChainData.assign(sidechain_data_begin, data_end);
#if POOL_BLOCK_DEBUG
m_sideChainDataDebug.assign(sidechain_data_begin, data_end);
#endif
}
catch (std::exception& e) {
const char* msg = e.what();

View file

@ -677,7 +677,8 @@ bool SideChain::get_block_blob(const hash& id, std::vector<uint8_t>& blob) const
}
blob = block->serialize_mainchain_data();
blob.insert(blob.end(), block->m_sideChainData.begin(), block->m_sideChainData.end());
const std::vector<uint8_t> sidechain_data = block->serialize_sidechain_data();
blob.insert(blob.end(), sidechain_data.begin(), sidechain_data.end());
return true;
}

View file

@ -70,7 +70,6 @@ TEST(pool_block, deserialize)
ASSERT_EQ(b.m_extraNonceSize, 4);
ASSERT_EQ(b.m_extraNonce, 28);
ASSERT_EQ(b.m_transactions.size(), 159);
ASSERT_EQ(b.m_sideChainData.size(), 146);
ASSERT_EQ(b.m_uncles.size(), 0);
ASSERT_EQ(b.m_sidechainHeight, 53450);
ASSERT_EQ(b.m_difficulty.lo, 319296691);