This is the third part of my research into crypto currency. This portion of research will cover mining software. There are different forms of mining and different forms of software used to mine. Some mining is hardware based and some software based. Hardware based mining is still software driven.
Crypto currencies use 2 things: Proof-of-work and Proof-of-stake. Some coins lean more towards proof-of-stake and others towards proof-of-work. There is a big difference between the 2.
The same is said for mining software. Some use one, others use the other. There are ways to set up mining software to reference proof-of-work or stake, but otherwise bypass them altogether and not actually use either of them. For example, bitcoin uses valid proof-of-work as a reference, but bypasses actually using it.
// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-2013 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or
http://www.opensource.org/licenses/mit-license.php.
#ifndef BITCOIN_MINER_H
#define BITCOIN_MINER_H
#include "primitives/block.h"
#include <stdint.h>
class CBlockIndex;
class CReserveKey;
class CScript;
class CWallet;
struct CBlockTemplate
{
CBlock block;
std::vector<CAmount> vTxFees;
std::vector<int64_t> vTxSigOps;
};
/** Run the miner threads */
void GenerateBitcoins(bool fGenerate, CWallet* pwallet, int nThreads);
/** Generate a new block, without valid proof-of-work */
CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn);
CBlockTemplate* CreateNewBlockWithKey(CReserveKey& reservekey);
/** Modify the extranonce in a block */
void IncrementExtraNonce(CBlock* pblock, CBlockIndex* pindexPrev, unsigned int& nExtraNonce);
void UpdateTime(CBlockHeader* block, const CBlockIndex* pindexPrev);
#endif // BITCOIN_MINER_H
There is nothing wrong with this portion of the software. Generating blocks without p-o-w will save on resource consumption. The small amount saved is negligable and only amounts to a fraction of a fraction of processing power. But if you notice, this particular miner uses fees. Calculating a fee based on the p-o-w per block generated will consume the resources previously saved in bypassing p-o-w for generating new blocks. Because fees are determined on a byte-for-byte basis, the more data you send/receive, the larger the fee, and the more resources needed to calculate the fee. The byte-for-byte fee is not based on the amount of coins in the transaction. It is only based on the total amount of data, or size of the packets/datagrams, sent and received during the transaction(s).
Bitcoin, for example, determines transactions by highest priority. Priority being determined by the fee rate.
// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-2014 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or
http://www.opensource.org/licenses/mit-license.php.
#include "miner.h"
#include "amount.h"
#include "primitives/transaction.h"
#include "hash.h"
#include "main.h"
#include "net.h"
#include "pow.h"
#include "timedata.h"
#include "util.h"
#include "utilmoneystr.h"
#ifdef ENABLE_WALLET
#include "wallet/wallet.h"
#endif
#include <boost/thread.hpp>
#include <boost/tuple/tuple.hpp>
using namespace std;
//////////////////////////////////////////////////////////////////////////////
//
// BitcoinMiner
//
//
// Unconfirmed transactions in the memory pool often depend on other
// transactions in the memory pool. When we select transactions from the
// pool, we select by highest priority or fee rate, so we might consider
// transactions that depend on transactions that aren't yet in the block.
// The COrphan class keeps track of these 'temporary orphans' while
// CreateBlock is figuring out which transactions to include.
//
class COrphan
{
public:
const CTransaction* ptx;
set<uint256> setDependsOn;
CFeeRate feeRate;
double dPriority;
COrphan(const CTransaction* ptxIn) : ptx(ptxIn), feeRate(0), dPriority(0)
{
}
};
uint64_t nLastBlockTx = 0;
uint64_t nLastBlockSize = 0;
// We want to sort transactions by priority and fee rate, so:
typedef boost::tuple<double, CFeeRate, const CTransaction*> TxPriority;
class TxPriorityCompare
{
bool byFee;
public:
TxPriorityCompare(bool _byFee) : byFee(_byFee) { }
bool operator()(const TxPriority& a, const TxPriority& b)
{
if (byFee)
{
if (a.get<1>() == b.get<1>())
return a.get<0>() < b.get<0>();
return a.get<1>() < b.get<1>();
}
else
{
if (a.get<0>() == b.get<0>())
return a.get<1>() < b.get<1>();
return a.get<0>() < b.get<0>();
}
}
};
Bitcoin does have free transactions based on the minimum block size. But it also ignores free transactions and sets a fee anyway.
// Skip free transactions if we're past the minimum block size:
const uint256& hash = tx.GetHash();
double dPriorityDelta = 0;
CAmount nFeeDelta = 0;
mempool.ApplyDeltas(hash, dPriorityDelta, nFeeDelta);
if (fSortedByFee && (dPriorityDelta <= 0) && (nFeeDelta <= 0) && (feeRate < ::minRelayTxFee) && (nBlockSize + nTxSize >= nBlockMinSize))
continue;
// Prioritise by fee once past the priority size or we run out of high-priority
// transactions:
if (!fSortedByFee &&
((nBlockSize + nTxSize >= nBlockPrioritySize) || !AllowFree(dPriority)))
{
fSortedByFee = true;
comparer = TxPriorityCompare(fSortedByFee);
std::make_heap(vecPriority.begin(), vecPriority.end(), comparer);
}
if (!view.HaveInputs(tx))
continue;
CAmount nTxFees = view.GetValueIn(tx)-tx.GetValueOut();
nTxSigOps += GetP2SHSigOpCount(tx, view);
if (nBlockSigOps + nTxSigOps >= MAX_BLOCK_SIGOPS)
continue;
// Note that flags: we don't want to set mempool/IsStandard()
// policy here, but we still have to ensure that the block we
// create only contains transactions that are valid in new blocks.
CValidationState state;
if (!CheckInputs(tx, state, view, true, MANDATORY_SCRIPT_VERIFY_FLAGS, true))
continue;
UpdateCoins(tx, state, view, nHeight);
// Added
pblock->vtx.push_back(tx);
pblocktemplate->vTxFees.push_back(nTxFees);
pblocktemplate->vTxSigOps.push_back(nTxSigOps);
nBlockSize += nTxSize;
++nBlockTx;
nBlockSigOps += nTxSigOps;
nFees += nTxFees;
if (fPrintPriority)
{
LogPrintf("priority %.1f fee %s txid %s\n",
dPriority, feeRate.ToString(), tx.GetHash().ToString());
}
// Add transactions that depend on this one to the priority queue
if (mapDependers.count(hash))
{
BOOST_FOREACH(COrphan* porphan, mapDependers[hash])
{
if (!porphan->setDependsOn.empty())
{
porphan->setDependsOn.erase(hash);
if (porphan->setDependsOn.empty())
{
vecPriority.push_back(TxPriority(porphan->dPriority, porphan->feeRate, porphan->ptx));
std::push_heap(vecPriority.begin(), vecPriority.end(), comparer);
}
}
}
}
}
The miner also uses not just the block chain, but also uses the memory pool, where it also dumps to and leaves the left over data there, to fill up available resources with unused data. This data is left over from the blocks and contains all the information contained within the block, or blocks, and any unencrypted data also remains. Windows uses the hard drive swap file for the memory pool, so anyone who can access the swap file can get all of the information on a user they need to perform malicious activity. Not necessarily easy to do, but a possibility that exists.
// Has to wait for dependencies
if (!porphan)
{
// Use list for automatic deletion
vOrphan.push_back(COrphan(&tx));
porphan = &vOrphan.back();
}
As you can see by this snippet of code, there is supposed to be an automatic deletion of orphans, but there is no actual execution of deleting orphans or unused data. So orphaned blocks continue to take up space, leaving less space for legitimate blocks. It will list the orphaned blocks, but do nothing about them.
There is still a lot of research to complete for mining software. Most of the source is fine. It does what it is intended to do and has checks and balances in place where they should be to ensure valid blocks. The only discrepencies are where fees are concerned and the amount of resources consumed and wasted during transactions and generating blocks. The total amount of resource waste has not yet been determined. On-going tests have not yet been completed in this area. These tests currently cover proof-of-work blocks.
Proof-of-stake blocks have a lot of similarities and also a lot of differences. P-o-s can be bypassed just as easily as p-o-w, and can also have the same type of resource waste. Fees can also be incurred and enforced on transactions without a user's knowledge. The security of proof-of-work is the same as proof-of-stake. The crypto used is the same. The hasher class/chunker is the same, with a few differences. Some hash out to 64 bits, but chunk it out to 60. Others hash out to 64 bits and chunk out to the same.
Proof-of-stake's major difference from proof-of-work is the time it takes to mint blocks. Staking relies on the time table used for minting and confirming blocks. Proof-of-work has no such use of a time table for minting or confirming and thus can have any time table the developer chose to use, if any at all. Most proof-of-work seems to only require the fact that some form of "work" was done, but has no need to know when it was done, how long it took or whether it was a valid block or not. Staking requires a valid block, confirmed within a reasonable amount of time. Proof-of-work has little to do with the stake and is usually referenced, but bypassed altogether. Staking is slightly more efficient than proof-of-work. But both come with the same vulnerabilities and the same wasted resources.
Testing proof-of-stake is also on-going. The amount of resource waste is still yet to be determined.
Bitcoin miners do not actually run a check on the block, whereas Litecoin does. They have similar headers, with the exception of included libraries and the one actually checking the block and the other does not.
// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-2013 The Bitcoin developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or
http://www.opensource.org/licenses/mit-license.php.
#ifndef BITCOIN_MINER_H
#define BITCOIN_MINER_H
#include <stdint.h>
class CBlock;
class CBlockHeader;
class CBlockIndex;
class CReserveKey;
class CScript;
class CWallet;
struct CBlockTemplate;
/** Run the miner threads */
void GenerateBitcoins(bool fGenerate, CWallet* pwallet, int nThreads);
/** Generate a new block, without valid proof-of-work */
CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn);
CBlockTemplate* CreateNewBlockWithKey(CReserveKey& reservekey);
/** Modify the extranonce in a block */
void IncrementExtraNonce(CBlock* pblock, CBlockIndex* pindexPrev, unsigned int& nExtraNonce);
/** Check mined block */
bool CheckWork(CBlock* pblock, CWallet& wallet, CReserveKey& reservekey);
void UpdateTime(CBlockHeader* block, const CBlockIndex* pindexPrev);
extern double dHashesPerSec;
extern int64_t nHPSTimerStart;
#endif // BITCOIN_MINER_H
Bitcoin and Litecoin both use the exact same source for their miners. Again, with the exception of which libraries are used and one does a check, the other doesn't. This snippet of Litecoin is the exact same for Bitcoin. Minus the included libraries have a one line difference.
// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-2014 The Bitcoin developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or
http://www.opensource.org/licenses/mit-license.php.
#include "miner.h"
#include "amount.h"
#include "primitives/block.h"
#include "primitives/transaction.h"
#include "hash.h"
#include "crypto/scrypt.h"
#include "main.h"
#include "net.h"
#include "pow.h"
#include "timedata.h"
#include "util.h"
#include "utilmoneystr.h"
#ifdef ENABLE_WALLET
#include "wallet.h"
#endif
#include <boost/thread.hpp>
#include <boost/tuple/tuple.hpp>
using namespace std;
//////////////////////////////////////////////////////////////////////////////
//
// BitcoinMiner
//
//
// Unconfirmed transactions in the memory pool often depend on other
// transactions in the memory pool. When we select transactions from the
// pool, we select by highest priority or fee rate, so we might consider
// transactions that depend on transactions that aren't yet in the block.
// The COrphan class keeps track of these 'temporary orphans' while
// CreateBlock is figuring out which transactions to include.
//
class COrphan
{
public:
const CTransaction* ptx;
set<uint256> setDependsOn;
CFeeRate feeRate;
double dPriority;
COrphan(const CTransaction* ptxIn) : ptx(ptxIn), feeRate(0), dPriority(0)
{
}
};
uint64_t nLastBlockTx = 0;
uint64_t nLastBlockSize = 0;
// We want to sort transactions by priority and fee rate, so:
typedef boost::tuple<double, CFeeRate, const CTransaction*> TxPriority;
class TxPriorityCompare
{
bool byFee;
public:
TxPriorityCompare(bool _byFee) : byFee(_byFee) { }
bool operator()(const TxPriority& a, const TxPriority& b)
{
if (byFee)
{
if (a.get<1>() == b.get<1>())
return a.get<0>() < b.get<0>();
return a.get<1>() < b.get<1>();
}
else
{
if (a.get<0>() == b.get<0>())
return a.get<1>() < b.get<1>();
return a.get<0>() < b.get<0>();
}
}
};
void UpdateTime(CBlockHeader* pblock, const CBlockIndex* pindexPrev)
{
pblock->nTime = std::max(pindexPrev->GetMedianTimePast()+1, GetAdjustedTime());
// Updating time can change work required on testnet:
if (Params().AllowMinDifficultyBlocks())
pblock->nBits = GetNextWorkRequired(pindexPrev, pblock);
}
CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn)
{
// Create new block
auto_ptr<CBlockTemplate> pblocktemplate(new CBlockTemplate());
if(!pblocktemplate.get())
return NULL;
CBlock *pblock = &pblocktemplate->block; // pointer for convenience
// -regtest only: allow overriding block.nVersion with
// -blockversion=N to test forking scenarios
if (Params().MineBlocksOnDemand())
pblock->nVersion = GetArg("-blockversion", pblock->nVersion);
// Create coinbase tx
CMutableTransaction txNew;
txNew.vin.resize(1);
txNew.vin[0].prevout.SetNull();
txNew.vout.resize(1);
txNew.vout[0].scriptPubKey = scriptPubKeyIn;
// Add dummy coinbase tx as first transaction
pblock->vtx.push_back(CTransaction());
pblocktemplate->vTxFees.push_back(-1); // updated at end
pblocktemplate->vTxSigOps.push_back(-1); // updated at end
In a line-by-line comparison, most code blocks were exactly identical, even down to the commenting. Exact word for word comments and source. So any vulnerabilities and flaws found within Bitcoin, are exactly the same in Litecoin. At least within the mining software. Performing your own line-by-line comparison to source found on Github will verify these results.