<commit_before>// Copyright (c) 2009-2010 <NAME>
// 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 "core.h"
#include "main.h"
#include "tx.h"
#include "net.h"
#ifdef ENABLE_WALLET
#include "wallet.h"
#endif
//////////////////////////////////////////////////////////////////////////////
//
// BitcoinMiner
//

int static FormatHashBlocks(void* pbuffer, unsigned int len) {
	unsigned char* pdata = (unsigned char*) pbuffer;
	unsigned int blocks = 1 + ((len + 8) / 64);
	unsigned char* pend = pdata + 64 * blocks;
	memset(pdata + len, 0, 64 * blocks - len);
	pdata[len] = 0x80;
	unsigned int bits = len * 8;
	pend[-1] = (bits >> 0) & 0xff;
	pend[-2] = (bits >> 8) & 0xff;
	pend[-3] = (bits >> 16) & 0xff;
	pend[-4] = (bits >> 24) & 0xff;
	return blocks;
}

static const unsigned int pSHA256InitState[8] = { 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f,
		0x9b05688c, 0x1f83d9ab, 0x5be0cd19 };

void SHA256Transform(void* pstate, void* pinput, const void* pinit) {
	SHA256_CTX ctx;
	unsigned char data[64];

	SHA256_Init(&ctx);

	for (int i = 0; i < 16; i++)
		((uint32_t*) data)[i] = ByteReverse(((uint32_t*) pinput)[i]);

	for (int i = 0; i < 8; i++)
		ctx.h[i] = ((uint32_t*) pinit)[i];

	SHA256_Update(&ctx, data, sizeof(data));
	for (int i = 0; i < 8; i++)
		((uint32_t*) pstate)[i] = ctx.h[i];
}

// Some explaining would be appreciated
class COrphan {
public:
	const CTransaction* ptx;
	set<uint256> setDependsOn;
	double dPriority;
	double dFeePerKb;

	COrphan(const CTransaction* ptxIn) {
		ptx = ptxIn;
		dPriority = dFeePerKb = 0;
	}

	void print() const {
		LogPrint("INFO","COrphan(hash=%s, dPriority=%.1f, dFeePerKb=%.1f)\n", ptx->GetHash().ToString(), dPriority,
				dFeePerKb);
		for (const auto& hash : setDependsOn)
			LogPrint("INFO", "   setDependsOn %s\n", hash.ToString());
	}
};

uint64_t nLastBlockTx = 0;
uint64_t nLastBlockSize = 0;

// We want to sort transactions by priority and fee, so:

void GetPriorityTx(vector<TxPriority> &vecPriority, map<uint256, vector<COrphan*> > &mapDependers) {
	vecPriority.reserve(mempool.mapTx.size());
	CBlockIndex* pindexPrev = chainActive.Tip();

	// Priority order to process transactions
	list<COrphan> vOrphan; // list memory doesn't move
	double dPriority = 0;
	for (map<uint256, CTxMemPoolEntry>::iterator mi = mempool.mapTx.begin(); mi != mempool.mapTx.end(); ++mi) {
		int nTxHeight = mi->second.GetHeight();
		CBaseTransaction *pBaseTx = mi->second.GetTx().get();

		if (!pTxCacheTip->IsContainTx(pBaseTx->GetHash())) {
			if (pBaseTx->nTxType == APPEAL_TX) {
				const CAppealTransaction *patx = (const CAppealTransaction *) pBaseTx;
				if (!pTxCacheTip->IsContainTx(patx->preTxHash)) {
					continue;
				}
			} else {

			}
			unsigned int nTxSize = ::GetSerializeSize(pBaseTx->GetNewInstance(), SER_NETWORK, PROTOCOL_VERSION);
			double dFeePerKb = double(pBaseTx->GetFee()) / (double(nTxSize) / 1000.0);
			dPriority = 1000.0 / double(nTxSize);
			vecPriority.push_back(TxPriority(dPriority, dFeePerKb, mi->second.GetTx()));
		}

	}
}

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
//
//    // Create coinbase tx
//    CTransaction txNew;
//    txNew.vin.resize(1);
//    txNew.vin[0].prevout.SetNull();
//    txNew.vout.resize(1);
//    txNew.vout[0].scriptPubKey = scriptPubKeyIn;
//
//    // Add our coinbase tx as first transaction
//    pblock->vtx.push_back(txNew);
//    pblocktemplate->vTxFees.push_back(-1); // updated at end
//    pblocktemplate->vTxSigOps.push_back(-1); // updated at end
//
//    // Largest block you're willing to create:
//    unsigned int nBlockMaxSize = GetArg("-blockmaxsize", DEFAULT_BLOCK_MAX_SIZE);
//    // Limit to betweeen 1K and MAX_BLOCK_SIZE-1K for sanity:
//    nBlockMaxSize = max((unsigned int)1000, min((unsigned int)(MAX_BLOCK_SIZE-1000), nBlockMaxSize));<commit_msg>add the function of burn fee for some tx<commit_after>// Copyright (c) 2009-2010 <NAME>
// 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 "core.h"
#include "main.h"
#include "tx.h"
#include "net.h"
#ifdef ENABLE_WALLET
#include "wallet.h"
#endif
//////////////////////////////////////////////////////////////////////////////
//
// BitcoinMiner
//

int static FormatHashBlocks(void* pbuffer, unsigned int len) {
	unsigned char* pdata = (unsigned char*) pbuffer;
	unsigned int blocks = 1 + ((len + 8) / 64);
	unsigned char* pend = pdata + 64 * blocks;
	memset(pdata + len, 0, 64 * blocks - len);
	pdata[len] = 0x80;
	unsigned int bits = len * 8;
	pend[-1] = (bits >> 0) & 0xff;
	pend[-2] = (bits >> 8) & 0xff;
	pend[-3] = (bits >> 16) & 0xff;
	pend[-4] = (bits >> 24) & 0xff;
	return blocks;
}

static const unsigned int pSHA256InitState[8] = { 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f,
		0x9b05688c, 0x1f83d9ab, 0x5be0cd19 };

void SHA256Transform(void* pstate, void* pinput, const void* pinit) {
	SHA256_CTX ctx;
	unsigned char data[64];

	SHA256_Init(&ctx);

	for (int i = 0; i < 16; i++)
		((uint32_t*) data)[i] = ByteReverse(((uint32_t*) pinput)[i]);

	for (int i = 0; i < 8; i++)
		ctx.h[i] = ((uint32_t*) pinit)[i];

	SHA256_Update(&ctx, data, sizeof(data));
	for (int i = 0; i < 8; i++)
		((uint32_t*) pstate)[i] = ctx.h[i];
}

// Some explaining would be appreciated
class COrphan {
public:
	const CTransaction* ptx;
	set<uint256> setDependsOn;
	double dPriority;
	double dFeePerKb;

	COrphan(const CTransaction* ptxIn) {
		ptx = ptxIn;
		dPriority = dFeePerKb = 0;
	}

	void print() const {
		LogPrint("INFO","COrphan(hash=%s, dPriority=%.1f, dFeePerKb=%.1f)\n", ptx->GetHash().ToString(), dPriority,
				dFeePerKb);
		for (const auto& hash : setDependsOn)
			LogPrint("INFO", "   setDependsOn %s\n", hash.ToString());
	}
};

uint64_t nLastBlockTx = 0;
uint64_t nLastBlockSize = 0;

//base on the last 500 blocks
uint64_t GetElementForBurn(void)
{
	CBlockIndex* pindexPrev = chainActive.Tip();

	return 0;
}

// We want to sort transactions by priority and fee, so:

void GetPriorityTx(vector<TxPriority> &vecPriority, map<uint256, vector<COrphan*> > &mapDependers) {
	vecPriority.reserve(mempool.mapTx.size());
	CBlockIndex* pindexPrev = chainActive.Tip();

	// Priority order to process transactions
	list<COrphan> vOrphan; // list memory doesn't move
	double dPriority = 0;
	for (map<uint256, CTxMemPoolEntry>::iterator mi = mempool.mapTx.begin(); mi != mempool.mapTx.end(); ++mi) {
		int nTxHeight = mi->second.GetHeight();//get Chain height when the tx entering the mempool
		CBaseTransaction *pBaseTx = mi->second.GetTx().get();

		if (!pTxCacheTip->IsContainTx(pBaseTx->GetHash())) {
			if (pBaseTx->nTxType == APPEAL_TX) {
				const CAppealTransaction *patx = (const CAppealTransaction *) pBaseTx;
				if (!pTxCacheTip->IsContainTx(patx->preTxHash)) {
					continue;
				}
			} else {

			}
			unsigned int nTxSize = ::GetSerializeSize(pBaseTx->GetNewInstance(), SER_NETWORK, PROTOCOL_VERSION);
#if 0
			{
				uint64_t element = GetElementForBurn();
				uint64_t burnfee = xx(element, pBaseTx);
				if(pBaseTx->GetFee() < burnfee)
				{
					LogPrint("INFO","the pBaseTx->GetFee() < burnfee\n");
					assert(0);
				}
				double dFeePerKb = double(pBaseTx->GetFee() - ) / (double(nTxSize) / 1000.0);
			}
#else
			double dFeePerKb = double(pBaseTx->GetFee()) / (double(nTxSize) / 1000.0);
#endif
			dPriority = 1000.0 / double(nTxSize);
			vecPriority.push_back(TxPriority(dPriority, dFeePerKb, mi->second.GetTx()));
		}

	}
}

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
//
//    // Create coinbase tx
//    CTransaction txNew;
//    txNew.vin.resize(1);
//    txNew.vin[0].prevout.SetNull();
//    txNew.vout.resize(1);
//    txNew.vout[0].scriptPubKey = scriptPubKeyIn;
//
//    // Add our coinbase tx as first transaction
//    pblock->vtx.push_back(txNew);
//    pblocktemplate->vTxFees.push_back(-1); // updated at end
//    pblocktemplate->vTxSigOps.push_back(-1); // updated at end
//
//    // Largest block you're willing to create:
//    unsigned int nBlockMaxSize = GetArg("-blockmaxsize", DEFAULT_BLOCK_MAX_SIZE);
//    // Limit to betweeen 1K and MAX_BLOCK_SIZE-1K for sanity:
//    nBlockMaxSize = max((unsigned int)1000, min((unsigned int)(MAX_BLOCK_SIZE-1000), nBlockMaxSize));
{'fraction_non_alphanumeric': 0.17150218438445167, 'fraction_numerical': 0.03999103842276241, 'mean_word_length': 3.6572769953051645, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 68, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4174388', 'n_tokens_mistral': 3650, 'n_tokens_neox': 3275, 'n_words': 766}