Skip to content

Commit

Permalink
Merge pull request BitcoinUnlimited#30 from bissias/no_more_cdeltablock
Browse files Browse the repository at this point in the history
Remove references to CDeltaBlock outside deltablocks files
  • Loading branch information
bissias authored Jun 2, 2020
2 parents e5028b2 + d935012 commit 1651a45
Show file tree
Hide file tree
Showing 11 changed files with 252 additions and 176 deletions.
299 changes: 179 additions & 120 deletions src/blockrelay/netdeltablocks.cpp

Large diffs are not rendered by default.

20 changes: 9 additions & 11 deletions src/bobtail/dag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,29 +89,27 @@ void CDagForrest::Clear()
_dag.clear();
}

bool CDagForrest::Find(const uint256 &hash, CDagNode* node)
CDagNode* CDagForrest::Find(const uint256 &hash)
{
// TODO : replace with std::find
node = nullptr;
auto iter = _dag.begin();
while (iter != _dag.end())
{
CDagNode* temp = *iter;
if (temp->hash == hash)
{
node = *iter;
return true;
return *iter;
}
++iter;
}
return false;
return nullptr;
}

bool CDagForrest::Insert(const CSubBlock &sub_block)
{
uint256 sub_block_hash = sub_block.GetHash();
CDagNode* temp;
if (Find(sub_block_hash, temp))
CDagNode* temp = Find(sub_block_hash);
if (temp != nullptr)
{
// we have this node.
return false;
Expand All @@ -122,8 +120,8 @@ bool CDagForrest::Insert(const CSubBlock &sub_block)
std::set<int16_t> merge_list;
for (auto &hash : sub_block.GetAncestorHashes())
{
CDagNode* ancestor = nullptr;
if (Find(hash, ancestor) == false)
CDagNode* ancestor = Find(hash);
if (ancestor == nullptr)
{
// TODO : A subblock is missing, try to re-request it or something
continue;
Expand Down Expand Up @@ -174,8 +172,8 @@ bool CDagForrest::IsSubgraphValid(std::set<uint256> sgHashes)
std::set<CDagNode*> subgraph;
for (auto &hash : sgHashes)
{
CDagNode* node;
if(Find(hash, node) == false)
CDagNode* node = Find(hash);
if(node == nullptr)
{
// missing a subblock, we need to request it
// TODO : request missing subblock or throw an error or something
Expand Down
2 changes: 1 addition & 1 deletion src/bobtail/dag.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class CDagForrest

void Clear();

bool Find(const uint256 &hash, CDagNode* node);
CDagNode* Find(const uint256 &hash);

bool Insert(const CSubBlock &sub_block);
void TemporalSort();
Expand Down
6 changes: 0 additions & 6 deletions src/bobtail/subblock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,3 @@ std::vector<uint256> CSubBlock::GetTxHashes() const

return hashes;
}

const CSubBlockRef CSubBlock::SubBlockByHash(const uint256& hash)
{
//TODO: ADD DAG LOOKUP LOGIC HERE!!!
return nullptr;
}
2 changes: 0 additions & 2 deletions src/bobtail/subblock.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ class CSubBlock : public CBlockHeader
std::set<uint256> GetAncestorHashes() const;

std::vector<uint256> GetTxHashes() const;

static const CSubBlockRef SubBlockByHash(const uint256& hash);
};


Expand Down
2 changes: 2 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "blockrelay/thinblock.h"
#include "blockstorage/blockstorage.h"
#include "blockstorage/sequential_files.h"
#include "bobtail/dag.h"
#include "chainparams.h"
#include "checkpoints.h"
#include "checkqueue.h"
Expand Down Expand Up @@ -76,6 +77,7 @@
*/

/*! Known, complete delta blocks. */
CDagForrest bobtailDag;
std::map<uint256, ConstCDeltaBlockRef> known_dbs;

std::atomic<bool> fImporting{false};
Expand Down
41 changes: 27 additions & 14 deletions src/miner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,13 @@ CTransactionRef BlockAssembler::coinbaseTx(const CScript &scriptPubKeyIn, int _n
// merge with delta template's coinbase if available
if (best_delta_template != nullptr)
{
//TODO: ADD SUBBLOCK COINBASE CONTRUCTION LOGIC HERE
/*
// LOG(WB, "Delta template available. Constructing coinbase for new block template from delta block.\n");
for (auto ancptr_opret : best_delta_template->coinbase()->vout)
// _anc estor _poin_t_r _OPRET URN
tx.vout.emplace_back(ancptr_opret);
*/
}
tx.vin[0].scriptSig = CScript() << _nHeight << OP_0;

Expand Down Expand Up @@ -232,8 +235,10 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript &sc
assert(pindexPrev); // can't make a new block if we don't even have the previous block
const uint256 prevBlockHash = pindexPrev->GetBlockHash();

best_delta_template =
CDeltaBlock::isEnabled(Params(), pindexPrev) ? CDeltaBlock::bestTemplate(prevBlockHash) : nullptr;
//TODO: ADD CODE TO CREATE SUBBLOCK TEMPLATE HERE
//best_delta_template =
// CDeltaBlock::isEnabled(Params(), pindexPrev) ? CDeltaBlock::bestTemplate(prevBlockHash) : nullptr;
best_delta_template = nullptr;

if (best_delta_template == nullptr)
{
Expand Down Expand Up @@ -282,11 +287,14 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript &sc
if (pblocktemplate->delta_block != nullptr)
{
LOG(WB, "Pre-adding %d transactions (and excluding the coinbase) from delta block.\n",
best_delta_template->numTransactions() - 1);
LOG(WB, "Current delta template delta set size: %d\n", best_delta_template->deltaSet().size());
for (auto biter = best_delta_template->begin_past_coinbase(); biter != best_delta_template->end(); ++biter)
best_delta_template->vtx.size() - 1);
LOG(WB, "Current delta template delta set size: %d\n", best_delta_template->vtx.size());
//for (auto biter = best_delta_template->begin_past_coinbase(); biter != best_delta_template->end(); ++biter)
for (auto txr : best_delta_template->vtx)
{
auto txr = *biter;
if (txr->IsCoinBase())
continue;
//auto txr = *biter;
bool done = false;
{
READLOCK(mempool.cs_txmempool);
Expand Down Expand Up @@ -405,7 +413,7 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript &sc
LOG(WB, "Size of vtxe set: %d\n", vtxe.size());
size_t i = 0;
size_t start_delta_set =
(pblocktemplate->delta_block != nullptr ? pblocktemplate->delta_block->numTransactions() - 1 : 0);
(pblocktemplate->delta_block != nullptr ? pblocktemplate->delta_block->vtx.size() - 1 : 0);
LOG(WB, "Start of delta set in delta block: %d\n", start_delta_set);

std::vector<CTransactionRef> add_to_delta;
Expand All @@ -428,7 +436,7 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript &sc
std::random_shuffle(add_to_delta.begin(), add_to_delta.end());
for (auto txref : add_to_delta)
{
pblocktemplate->delta_block->add(txref);
pblocktemplate->delta_block->vtx.push_back(txref);
// COZ_PROGRESS_NAMED("add to delta from vector");
}
}
Expand All @@ -448,10 +456,13 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript &sc
// pblock->numTransactions());
if (pblocktemplate->delta_block != nullptr)
{
//TODO ADD SUBBLOCK COINBASE
/*
LOG(WB, "Delta block num txn before adding CB: %d\n", pblocktemplate->delta_block->numTransactions());
pblocktemplate->delta_block->setCoinbase(final_cb);
LOG(WB, "Delta block num txn after adding CB: %d\n", pblocktemplate->delta_block->numTransactions());
LOG(WB, "Delta block delta set size: %d\n", pblocktemplate->delta_block->deltaSet().size());
*/
}
pblocktemplate->vTxFees[0] = -nFees;

Expand All @@ -466,7 +477,7 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript &sc
UpdateTime(pblocktemplate->delta_block.get(), chainparams.GetConsensus(), pindexPrev);
pblocktemplate->delta_block->nBits = pblock->nBits;
pblocktemplate->delta_block->nNonce = pblock->nNonce;
pblocktemplate->delta_block->setAllTransactionsKnown();
//pblocktemplate->delta_block->setAllTransactionsKnown();

// sanity check: both blocks should have the same merkle root
// uint256 regular_hashMerkleRoot = BlockMerkleRoot(*pblock);
Expand All @@ -481,8 +492,8 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript &sc
*/
// LOG(WB, "Miner constructed regular block max depth: %d, for size: %d\n", pblock->treeMaxDepth(),
// pblock->numTransactions());
LOG(WB, "Miner constructed delta block max depth: %d, for size: %d\n",
pblocktemplate->delta_block->treeMaxDepth(), pblocktemplate->delta_block->numTransactions());
LOG(WB, "Miner constructed delta block max depth: ???, for size: %d\n",
pblocktemplate->delta_block->vtx.size());

// LOG(WB, "Delta block merkle root: %s, regular merkle root: %s\n", delta_hashMerkleRoot.GetHex(),
// regular_hashMerkleRoot.GetHex());
Expand All @@ -503,7 +514,8 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript &sc
if (pblocktemplate->delta_block != nullptr)
{
pblocktemplate->delta_block->fXVal = true;
testblock = pblocktemplate->delta_block.get();
//TODO: SUBBLOCK CANNOT BE CONVERTED TO REGULAR BLOCK
//testblock = pblocktemplate->delta_block.get();
}

if (blockstreamCoreCompatible)
Expand Down Expand Up @@ -658,12 +670,13 @@ bool BlockAssembler::TestForBlock(CTxMemPool::txiter iter)
tx->GetHash().GetHex());*/
return false;
}
//TODO: NECESSARY FOR SUBBLOCKS?
/*
if (best_delta_template->spendsOutput(inp.prevout))
{
/*LOG(WB, "Transaction %s would be double-spending one included in delta block template, dropping.\n",
tx->GetHash().GetHex());*/
return false;
}
*/
}
}
return true;
Expand Down
5 changes: 3 additions & 2 deletions src/miner.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#ifndef BITCOIN_MINER_H
#define BITCOIN_MINER_H

#include "bobtail/subblock.h"
#include "deltablocks.h"
#include "primitives/block.h"
#include "txmempool.h"
Expand Down Expand Up @@ -41,7 +42,7 @@ static const bool DEFAULT_PRINTPRIORITY = false;
struct CBlockTemplate
{
CBlockRef block;
CDeltaBlockRef delta_block;
CSubBlockRef delta_block;
std::vector<CAmount> vTxFees;
std::vector<int64_t> vTxSigOps;
CBlockTemplate() : block(new CBlock()) {}
Expand Down Expand Up @@ -104,7 +105,7 @@ class BlockAssembler

private:
// delta block template which is used to create the block
CDeltaBlockRef best_delta_template;
CSubBlockRef best_delta_template;

// utility functions
/** Clear the block's state and prepare for assembling a new block */
Expand Down
22 changes: 12 additions & 10 deletions src/rpc/mining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "blockrelay/netdeltablocks.h"
#include "blockstorage/blockstorage.h"
#include "bobtail/bobtail.h"
#include "bobtail/dag.h"
#include "chain.h"
#include "chainparams.h"
#include "consensus/consensus.h"
Expand Down Expand Up @@ -39,6 +40,7 @@
#include <univalue.h>

using namespace std;
extern CDagForrest bobtailDag;

/**
* Return average network hashes per second based on the last 'lookup' blocks,
Expand Down Expand Up @@ -122,15 +124,16 @@ UniValue generateBlocks(boost::shared_ptr<CReserveScript> coinbaseScript,
while (numblocks < nGenerate)
{
std::unique_ptr<CBlockTemplate> pblocktemplate;
CDeltaBlockRef pblock;
{
TxAdmissionPause lock; // flush any tx waiting to enter the mempool
pblocktemplate = BlockAssembler(Params()).CreateNewBlock(coinbaseScript->reserveScript);
}
if (!pblocktemplate.get())
throw JSONRPCError(RPC_INTERNAL_ERROR, "Couldn't create new block");
LOG(WB, "Using delta block for RPC generate.\n");
pblock = pblocktemplate->delta_block;
CBlockRef pblock;
//TODO: MAKE SUBBLOCK COMPATIBLE
//pblock = pblocktemplate->delta_block;

{
// LOCK(cs_main);
Expand All @@ -150,18 +153,17 @@ UniValue generateBlocks(boost::shared_ptr<CReserveScript> coinbaseScript,
continue;

LOG(WB, "PROCESS NEW WEAK\n");
CDeltaBlock::processNew(pblock);
//CDeltaBlock::processNew(pblock);
//TODO: PROCESS BLOCK
//CDeltaBlock::tryRegister(pblock);
//TODO: ACTUALLY CALL sendDeltaBlock here
CDeltaBlock::tryRegister(pblock);
//TODO: REGISTER BLOCK

// Now check if Bobtail PoW is also satisfied
if (CheckBobtailPoW(*pblock, pblock->allAncestorHashes(), Params().GetConsensus(), BOBTAIL_K))
// TODO: CheckBobtailPoW NEEDS TO TAKE A DAG NOT VECTOR OF HASHES
std::vector<uint256> ancestors; //DELETE ME
if (CheckBobtailPoW(*pblock, ancestors, Params().GetConsensus(), BOBTAIL_K))
{
LOG(WB, "PROCESS NEW STRONG %s\n", pblock->GetHash().ToString());
for (int i = 0; i < pblock->allAncestorHashes().size(); i++)
{
LOG(WB, "\t%s\n", pblock->allAncestorHashes()[i].ToString());
}
// In we are mining our own block or not running in parallel for any reason
// we must terminate any block validation threads that are currently running,
// Unless they have more work than our own block or are processing a chain
Expand Down
16 changes: 10 additions & 6 deletions src/unlimited.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,7 @@ void static BitcoinMiner(const CChainParams &chainparams)
int64_t micros_after = GetTimeMicros();
if (pblocktemplate->delta_block != nullptr)
LOG(WB, "Time for building and checking delta block template %s with %d txn: %d\n",
pblocktemplate->delta_block->GetHash().GetHex(), pblocktemplate->delta_block->numTransactions(),
pblocktemplate->delta_block->GetHash().GetHex(), pblocktemplate->delta_block->vtx.size(),
micros_after - micros_before);

// COZ_END("CreateNewBlock");
Expand All @@ -703,7 +703,9 @@ void static BitcoinMiner(const CChainParams &chainparams)
if (pblocktemplate->delta_block != nullptr)
{
LOG(WB, "Using delta block for mining.\n");
pblock = pblocktemplate->delta_block;
//pblock = pblocktemplate->delta_block;
//TODO: NEED MINING TEMPLATE FOR BOBTAIL
//pblock = pblocktemplate->delta_block;
}
else
{
Expand All @@ -724,7 +726,7 @@ void static BitcoinMiner(const CChainParams &chainparams)
uint256 hash;
uint32_t nNonce = 0;

ConstCDeltaBlockRef latest_delta(CDeltaBlock::latestForStrong(pblock->hashPrevBlock));
//ConstCDeltaBlockRef latest_delta(CDeltaBlock::latestForStrong(pblock->hashPrevBlock));
while (true)
{
// Check if something found
Expand All @@ -742,7 +744,8 @@ void static BitcoinMiner(const CChainParams &chainparams)
bool is_strong = UintToArith256(hash) <= hashStrongTarget;
if (pblocktemplate->delta_block != nullptr)
{
CDeltaBlock::processNew(pblocktemplate->delta_block);
//CDeltaBlock::processNew(pblocktemplate->delta_block);
//TODO: PROCESS NEW BLOCK HERE
//TODO: ACTUALLY CALL sendDeltaBlock here
}
if (is_strong)
Expand All @@ -768,8 +771,9 @@ void static BitcoinMiner(const CChainParams &chainparams)
boost::this_thread::interruption_point();

// rebuild when a new delta block arrived
if (CDeltaBlock::latestForStrong(pblock->hashPrevBlock) != latest_delta)
break;
//if (CDeltaBlock::latestForStrong(pblock->hashPrevBlock) != latest_delta)
// break;
// TODO: WHAT IS THIS DOING? DO WE NEED IT?
// Regtest mode doesn't require peers
if (vNodes.empty() && chainparams.MiningRequiresPeers())
break;
Expand Down
Loading

0 comments on commit 1651a45

Please sign in to comment.