Developping BitCoin Yourself-altcoin generate, make altcoin, bitcoin, coingen!
Developping BitCoin Yourself-altcoin generate, make altcoin, bitcoin, coingen!
It can support any algorithm (sha256, scrypt, X11, x13, NIST, grostel, and these algorithms in series) and parameter customization.
At present, bitcoin SHA256 and Wright currency scypt and SHA3-256 (KECCAK) are built in advance.
Online compilation currently provides three installation packages:
1, linux/unix's daemon program (with purse function)
2, Mac wallet installation package
3, windows wallet installation package
- Preview of the screenshot
(1) installation diagram 1
(2) installation diagram 2
(3) installation success
(4) miner
(5) Transaction record
(6)transaction details
(7) copy right
()
(8) debug window
(9) p2p network
(10) console cmd
(11) cmd test
(12) address info
2、Some of the modifications are code fragments. Please use eclipse to open out the code and check it.
=================source file chainparams.cpp Modifying the creation block
/**
* Build the genesis block. Note that the output of the genesis coinbase cannot
* be spent as it did not originally exist in the database.
*
* CBlock(hash=000000000019d6, ver=1, hashPrevBlock=00000000000000, hashMerkleRoot=4a5e1e, nTime=1231006505, nBits=1d00ffff, nNonce=2083236893, vtx=1)
* CTransaction(hash=4a5e1e, ver=1, vin.size=1, vout.size=1, nLockTime=0)
* CTxIn(COutPoint(000000, -1), coinbase 04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73)
* CTxOut(nValue=50.00000000, scriptPubKey=0x5F1DF16B2B704C8A578D0B)
* vMerkleTree: 4a5e1e
*/
// HarryWu, generate genesis block by genesis.py as following:
//
// localhost genesis # python genesis.py \
// -t $(date +%s) \
// -z "shanghai stock index closed at 2343.57, on 24th Sept., 2014" \
// -a SHA256 \
// -p 049e02fa9aa3c19a3b112a58bab503c5caf797972f5cfe1006275aa5485a01b48f9f648bc5380ee1e82dc6f474c8e0f7e2f6bbd0de9355f92496e3ea327ccb19cc \
// -v 10000000000
// Raw block data: 04ffff001d01043b7368616e676861692073746f636b20696e64657820636c6f73656420617420323334332e35372c206f6e203234746820536570742e2c2032303134
// algorithm: SHA256
// merkle hash: 1c395aad7fab156523a095a869d3fcdf3249a8a97c8d7337adb4f33d826da32b
// pszTimestamp: shanghai stock index closed at 2343.57, on 24th Sept., 2014
// pubkey: 049e02fa9aa3c19a3b112a58bab503c5caf797972f5cfe1006275aa5485a01b48f9f648bc5380ee1e82dc6f474c8e0f7e2f6bbd0de9355f92496e3ea327ccb19cc
// time: 1411650667
// bits: 0x1d00ffff
// Searching for genesis hash..
//
// nonce: 1456993276
// genesis hash: 000000004df0288b461e17d9a20e557fd296861c604f1944eb9e2cca866af0a5
const char* pszTimestamp = "shanghai stock index closed at 2343.57, on 24th Sept., 2014";
CMutableTransaction txNew;
txNew.vin.resize(1);
txNew.vout.resize(1);
txNew.vin[0].scriptSig = CScript() << 0x1d00ffff << CScriptNum(4) << vector<unsigned char>((const unsigned char*)pszTimestamp, (const unsigned char*)pszTimestamp + strlen(pszTimestamp));
txNew.vout[0].nValue = nGenesisSubsidy * COIN;
txNew.vout[0].scriptPubKey = CScript() << ParseHex("049e02fa9aa3c19a3b112a58bab503c5caf797972f5cfe1006275aa5485a01b48f9f648bc5380ee1e82dc6f474c8e0f7e2f6bbd0de9355f92496e3ea327ccb19cc") << OP_CHECKSIG;
genesis.vtx.push_back(txNew);
genesis.hashPrevBlock = 0;
genesis.hashMerkleRoot = genesis.BuildMerkleTree();
genesis.nVersion = 1;
genesis.nTime = 1411666331;
genesis.nBits = 0x1d00ffff;
genesis.nNonce = 2056985438;
hashGenesisBlock = genesis.GetHash();
assert(hashGenesisBlock == uint256("0x0000000061b1aca334b059920fed7bace2336ea4d23d63428c7aee04da49e942"));
assert(genesis.hashMerkleRoot == uint256("0x7bf229f629a6666596c1ce57117c28d1d29299e8a5303347929bd70847c49adb"));
=================source file chainparams.cpp Modifying the magic number of the network protocol
class CMainParams : public CChainParams {
public:
CMainParams() {
networkID = CBaseChainParams::MAIN;
strNetworkID = "main";
/**
* The message start string is designed to be unlikely to occur in normal data.
* The characters are rarely used upper ASCII, not valid as UTF-8, and produce
* a large 4-byte int at any alignment.
*/
pchMessageStart[0] = 0x90;
pchMessageStart[1] = 0x0d;
pchMessageStart[2] = 0xf0;
pchMessageStart[3] = 0x0d;
=================source file chainparams.cpp Modify the address prefix
base58Prefixes[PUBKEY_ADDRESS] = list_of(35); // F prefix
base58Prefixes[SCRIPT_ADDRESS] = list_of(65); // T prefix
base58Prefixes[SECRET_KEY] = list_of(45); // 7 prefix
base58Prefixes[EXT_PUBLIC_KEY] = list_of(0x04)(0x88)(0xEE)(0x35);
base58Prefixes[EXT_SECRET_KEY] = list_of(0x04)(0x88)(0xEE)(0x45);
=================source file miner.cpp Modifying the workload mechanism
Bitcoin searches for random numbers in function ScanHash ().
//
// ScanHash scans nonces looking for a hash with at least some zero bits.
// The nonce is usually preserved between calls, but periodically or if the
// nonce is 0xffff0000 or above, the block is rebuilt and nNonce starts over at
// zero.
//
bool static ScanHash(const CBlockHeader *pblock, uint32_t& nNonce, uint256 *phash)
{
// Write the first 76 bytes of the block header to a double-SHA256 state.
//CHash256 hasher;
//CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
//ss << pblock;
//assert(ss.size() == 80);
//hasher.Write((unsigned char)&ss[0], 76);
while (true) {
nNonce++;
// Write the last 4 bytes of the block header (the nonce) to a copy of
// the double-SHA256 state, and compute the result.
//CHash256(hasher).Write((unsigned char*)&nNonce, 4).Finalize((unsigned char*)phash);
*phash = pblock->ComputePowHash(nNonce);
// Return the nonce if the hash has at least some zero bits,
// caller will check if it has enough to reach the target
if (((uint16_t*)phash)[15] == 0)
return true;
// If nothing found after trying for a while, return -1
if ((nNonce & 0xffff) == 0)
return false;
if ((nNonce & 0xfff) == 0)
boost::this_thread::interruption_point();
}
}
In the function BitcoinMiner (), create a new block, set the timestamp (disturbance 1), get the latest transaction in MemPool (disturbance 2), call the ScanHash () search random number (disturbance 3) until the hash that satisfies the condition is found.
In addition, the workload proved by the bit of bitcoin is used as the index of the block, and the 2 concepts in litecoin are treated distinctions (the hash of the sha256 as the index, and the pow hash from scrypt as a proof of the workload).
Note: set the conditions for independent mining (set in chainparams.cpp).
fMiningRequiresPeers = !true; // See BitcoinMiner() for details.
Mining code:
void static BitcoinMiner(CWallet *pwallet)
{
LogPrintf("FastCoinMiner started\n");
SetThreadPriority(THREAD_PRIORITY_LOWEST);
RenameThread("fastcoin-miner");
// Each thread has its own key and counter
CReserveKey reservekey(pwallet);
unsigned int nExtraNonce = 0;
try {
while (true) {
if (Params().MiningRequiresPeers()) {
// Busy-wait for the network to come online so we don't waste time mining
// on an obsolete chain. In regtest mode we expect to fly solo.
while (vNodes.empty())
MilliSleep(1000);
}
//
// Create new block
//
unsigned int nTransactionsUpdatedLast = mempool.GetTransactionsUpdated();
CBlockIndex* pindexPrev = chainActive.Tip();
auto_ptr<CBlockTemplate> pblocktemplate(CreateNewBlockWithKey(reservekey));
if (!pblocktemplate.get())
{
LogPrintf("Error in FastCoinMiner: Keypool ran out, please call keypoolrefill before restarting the mining thread\n");
return;
}
CBlock *pblock = &pblocktemplate->block;
IncrementExtraNonce(pblock, pindexPrev, nExtraNonce);
LogPrintf("Running FastCoinMiner with %u transactions in block (%u bytes)\n", pblock->vtx.size(),
::GetSerializeSize(*pblock, SER_NETWORK, PROTOCOL_VERSION));
With a slight modification, several preset hash algorithms (sha256, scrypt, keccak (SHA3)) can be distinguished by the version number of block, so that when ComputePowHash is called in the scanhash, different POW algorithms can be selected, and you can even string these algorithms together to increase the square attack that the algorithm is cracked.
uint256 CBlockHeader::ComputePowHash(uint32_t nNonce) const
{
if (nVersion == 1 || nVersion == 2){
/**
* Use SHA256+SHA256 to make PoW
/
// Write the first 76 bytes of the block header to a double-SHA256 state.
CDoubleSHA256Pow hasher; // TODO: Create a new PowHasher named CPowHash256
CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
ss << this;
assert(ss.size() == 80);
hasher.Write((unsigned char)&ss[0], 76);
uint256 powHash;
CDoubleSHA256Pow(hasher).Write((unsigned char)&nNonce, 4).Finalize((unsigned char)&powHash);
return powHash;
}else if (nVersion == 3){
/*
* Scrypt PoW
/
CScryptHash256Pow hasher;
CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
ss << this;
assert(ss.size() == 80);
hasher.Write((unsigned char)&ss[0], 76);
uint256 powHash;
CScryptHash256Pow(hasher).Write((unsigned char)&nNonce, 4).Finalize((unsigned char)&powHash);
return powHash;
}else if (nVersion == 4){
/*
* Scrypt+SHA256 PoW
/
}else if (nVersion == 5){
/*
* Keccak(1088,512,256) or Known as SHA3-256(fips202draft) Pow
/
CKeccak_256 hasher;
CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
ss << this;
assert(ss.size() == 80);
hasher.Write((unsigned char)&ss[0], 76);
uint256 powHash;
CKeccak_256(hasher).Write((unsigned char)&nNonce, 4).Finalize((unsigned char*)&powHash);
return powHash;
}else{
// Abort, unknown block version.
assert(false);
return ~(uint256)0;
}
}
- Amend the incentive mechanism
=================source file :main.h
/** Coinbase transaction outputs can only be spent after this number of new blocks (network rule) */
static const int COINBASE_MATURITY = 14;
================= Revise transaction confirmation block number recommended value
Source code qt/transactionrecord.h
/** UI model for a transaction. A core transaction can be represented by multiple UI transactions if it has
multiple outputs.
*/
class TransactionRecord
{
public:
enum Type
{
Other,
Generated,
SendToAddress,
SendToOther,
RecvWithAddress,
RecvFromOther,
SendToSelf
};
/** Number of confirmation recommended for accepting a transaction */
static const int RecommendedNumConfirmations = 2;
==========================Modifying the difficulty configuration
Source file chainparams.cpp
class CMainParams : public CChainParams {
public:
CMainParams() {
networkID = CBaseChainParams::MAIN;
strNetworkID = "main";
/**
* The message start string is designed to be unlikely to occur in normal data.
* The characters are rarely used upper ASCII, not valid as UTF-8, and produce
* a large 4-byte int at any alignment.
*/
pchMessageStart[0] = 0x90;
pchMessageStart[1] = 0x0d;
pchMessageStart[2] = 0xf0;
pchMessageStart[3] = 0x0d;
vAlertPubKey = ParseHex("04e01590abdc5967eb550413fcf04bbd7cead46f13579b58d52ea2f08d71a1a94196c476cd4fa60c30b51737fe3d9c8c88a04a6bec2282ebb1f22286130a153b85");
nDefaultPort = 9999;
bnProofOfWorkLimit = ~uint256(0) >> 32;
nSubsidyHalvingInterval = 210000;
nEnforceBlockUpgradeMajority = 750;
nRejectBlockOutdatedMajority = 950;
nToCheckBlockUpgradeMajority = 1000;
nMinerThreads = 1; // 0 for all available cpus.
nTargetTimespan = 60 * 60; // re-targeting every one hour
nTargetSpacing = 1 * 60; // do new pow every 1 minutes.
nGenesisSubsidy = 100;
A) bnProofOfWorkLimit=~uint256 (0) > > 32;
It is a large integer (256bit) representation, the first 32 digits of 0, which represent the minimum difficulty allowed by the whole network, and the block below this difficulty will not be excavated.
B) nGenesisSubsidy = 100;
The initial allowance, the first block of bitcoin, is awarded 50 BTC.
C) nSubsidyHavlingInterval = 210000;
This parameter determines the number of block after which bitcoin rewards (subsidies, mining Awards) will be halved. This parameter combines initial rewards (such as bitcoin 50) to estimate the overall currency output of the whole network (such as 21 million of bitcoin), and this initial allowance is also configurable, as shown in 2. For example, bitcoin, a formula of equal ratio sequence sum can calculate the total amount of money 50 (1/ (1 - 0.5)) *210000=2100 million BTC.
The above 2 parameters are used when the query area is rewarded. Please check the GetBlockValue (height, fees) of main.cpp for details.
CAmount GetBlockValue(int nHeight, const CAmount& nFees)
{
CAmount nSubsidy = Params().GenesisSubsidy() * COIN;
int halvings = nHeight / Params().SubsidyHalvingInterval();
// Force block reward to zero when right shift is undefined.
if (halvings >= 64)
return nFees;
// Subsidy is cut in half every 210,000 blocks which will occur approximately every 4 years.
nSubsidy >>= halvings;
return nSubsidy + nFees;
}
A lot of Shanzhai is also here to modify the distribution of money, such as nHeight==1, when the award of 10 million, the other blocks are not rewarded, this even predug mode of the bar.
In addition, with regard to the distribution of money, the Satoshi students here may have considered a lot of why he had been set up for many years before the ore source was exhausted, and why the decline was taken, probably out of the purpose of protecting the block chain (timestampserver).
D) difficulty adjustment cycle
NTargetTimespan = 60 * 60; / / re-targeting every one hour, 60 blocks
NTargetSpacing = 1 * 60; / / / expect 1 block/minute
The setting here is 60 minutes (3600 seconds, because expected 60 seconds 1 block, then 60 blocks to calculate new difficulty, otherwise the difficulty of using the last block), reevaluate the difficulty, the next block of mining may have to use new difficulty set.
Bitcoin adjustment cycle is 2 weeks / 10 minutes, that is 2 * 7 * 24 * 6 = 2016
More Info go to www.1687820.com