上海丙赟信息科技有限责任公司


  • 首页

  • 关于

  • 归档

比特币源码阅读(0.16)(三)

发表于 2018-04-16 | 阅读次数:

chainparams.cpp::CMainParams

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/**
* CChainParams defines various tweak-able parameters of a given instance of the
* Bitcoin system. There are three: the main network on which people trade goods
* and services, the public test network which gets reset from time to time and
* a regression test mode which is intended for private networks only. It has
* minimal difficulty to ensure that blocks can be found instantly.
*/
class CMainParams : public CChainParams {
public:
CMainParams() {
//工作网络,还可能是测试或者回归测试网络等,不需要修改
strNetworkID = "main";

//区块奖励减半间隔,每隔210000个块减半一次,按每10分钟出一个块计算,大约间隔为4年
consensus.nSubsidyHalvingInterval = 210000;

consensus.BIP16Height = 173805; // 00000000000000ce80a7e057163a4db1d5ad7b20fb6f598c9597b9665c8fb0d4 - April 1, 2012

// BIP34Height and BIP34Hash are just the historical height and block hash at which BIP34 activated.
// They are an optimization. Earlier versions of the code implemented the exact rules from the BIP34, checking
// whether the transition criteria were satisfied for every block. This was relatively expensive, and as BIP34's
// activation is a long time ago now, that check has been replaced with a simpler one that just looks at height and
// hash of the chain at a certain height.
// If you create a new chain from scratch, you can just set this to 0/genesis to have BIP34 be active from the start.
consensus.BIP34Height = 227931;
consensus.BIP34Hash = uint256S("0x000000000000024b89b42a942fe0d9fea3bb44ab7bd1b19115dd6a759c0808b8");

consensus.BIP65Height = 388381; // 000000000000000004c2b624ed5d7756c508d90fd0da2c7c679febfa6c4735f0
consensus.BIP66Height = 363725; // 00000000000000000379eaa19dce8c9b722d46ae6a57c2f1a988119488b50931

// 算力极限值,最低难度值(difficult = 1)时所对应的256位的二进制数值
//consensus.powLimit is the absolute highest target (lowest difficulty) allowed. But in practice, it is identical to genesis.nBits because the difficulty target is canonically stored in compact form. If you take consensus.powLimit, convert to compact form and then convert back, you get genesis.nBits
consensus.powLimit = uint256S("00000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
// 算力修改间隔,每两周调整一次难度
consensus.nPowTargetTimespan = 14 * 24 * 60 * 60; // two weeks
// 平均每600秒出一个块(10分钟)
consensus.nPowTargetSpacing = 10 * 60;
// 不允许在当前难度周期挖低难度的块,仅在网上测试时才允许
consensus.fPowAllowMinDifficultyBlocks = false;
// 每个难度周期内(2016个块)不允许调整难度值
consensus.fPowNoRetargeting = false;

//rule changes require 95% agreement, measured across 1 retargeting periods.
consensus.nRuleChangeActivationThreshold = 1916; // 95% of 2016
consensus.nMinerConfirmationWindow = 2016; // nPowTargetTimespan / nPowTargetSpacing

consensus.vDeployments[Consensus::DEPLOYMENT_TESTDUMMY].bit = 28;
consensus.vDeployments[Consensus::DEPLOYMENT_TESTDUMMY].nStartTime = 1199145601; // January 1, 2008
consensus.vDeployments[Consensus::DEPLOYMENT_TESTDUMMY].nTimeout = 1230767999; // December 31, 2008

// Deployment of BIP68, BIP112, and BIP113.
consensus.vDeployments[Consensus::DEPLOYMENT_CSV].bit = 0;
consensus.vDeployments[Consensus::DEPLOYMENT_CSV].nStartTime = 1462060800; // May 1st, 2016
consensus.vDeployments[Consensus::DEPLOYMENT_CSV].nTimeout = 1493596800; // May 1st, 2017

// Deployment of SegWit (BIP141, BIP143, and BIP147)
consensus.vDeployments[Consensus::DEPLOYMENT_SEGWIT].bit = 1;
consensus.vDeployments[Consensus::DEPLOYMENT_SEGWIT].nStartTime = 1479168000; // November 15th, 2016.
consensus.vDeployments[Consensus::DEPLOYMENT_SEGWIT].nTimeout = 1510704000; // November 15th, 2017.

// The best chain should have at least this much work.
// The minimum amount of chain work that a client must have before it will consider itself synchronized.
// 改成全0
consensus.nMinimumChainWork = uint256S("0x000000000000000000000000000000000000000000f91c579d57cad4bc5278cc");

// By default assume that the signatures in ancestors of this block are valid.
// 改成全0
consensus.defaultAssumeValid = uint256S("0x0000000000000000005214481d2d96f898e3d5416e43359c145944a909d242e0"); //506067

/*These are the network ‘magic bytes'; They’re a series of four bytes that identify messages as belonging to a particular protocol. 0xf9, 0xbe, 0xb4, and 0xd9 are the ones Bitcoin uses. You want to use something else, so that nobody can ever trick your newcoin client into connecting to the Bitcoin network, or vice versa. It doesn’t matter what these are except that they shouldn’t match the ones used in other protocols (particularly other cryptocurrency block chains). They have to be values between 0 and 255.,需要修改掉
*/
pchMessageStart[0] = 0xf9;
pchMessageStart[1] = 0xbe;
pchMessageStart[2] = 0xb4;
pchMessageStart[3] = 0xd9;

//端口号,需要修改掉
nDefaultPort = 8333;

nPruneAfterHeight = 100000;

//第一个块的奖励为50个比特币
genesis = CreateGenesisBlock(1231006505, 2083236893, 0x1d00ffff, 1, 50 * COIN);
consensus.hashGenesisBlock = genesis.GetHash();
assert(consensus.hashGenesisBlock == uint256S("0x000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f"));
assert(genesis.hashMerkleRoot == uint256S("0x4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b"));

// Note that of those which support the service bits prefix, most only support a subset of
// possible options.
// This is fine at runtime as we'll fall back to using them as a oneshot if they dont support the
// service bits we want, but we should get them updated to support all service bits wanted by any
// release ASAP to avoid it where possible.
vSeeds.emplace_back("seed.bitcoin.sipa.be"); // Pieter Wuille, only supports x1, x5, x9, and xd
vSeeds.emplace_back("dnsseed.bluematt.me"); // Matt Corallo, only supports x9
vSeeds.emplace_back("dnsseed.bitcoin.dashjr.org"); // Luke Dashjr
vSeeds.emplace_back("seed.bitcoinstats.com"); // Christian Decker, supports x1 - xf
vSeeds.emplace_back("seed.bitcoin.jonasschnelli.ch"); // Jonas Schnelli, only supports x1, x5, x9, and xd
vSeeds.emplace_back("seed.btc.petertodd.org"); // Peter Todd, only supports x1, x5, x9, and xd

//[https://en.bitcoin.it/wiki/List_of_address_prefixes](http://)
//Blockchain-based currencies use encoded strings, which are a Base58Check encoding of some hash,
//typically that of a public key. The encoding includes a prefix (traditionally a single version byte),
//which affects the leading symbol(s) in the encoded result.
//Pubkey hash (P2PKH address)
base58Prefixes[PUBKEY_ADDRESS] = std::vector<unsigned char>(1,0);
//Script hash (P2SH address)
base58Prefixes[SCRIPT_ADDRESS] = std::vector<unsigned char>(1,5);
//Private key (WIF, uncompressed pubkey)
base58Prefixes[SECRET_KEY] = std::vector<unsigned char>(1,128);
//BIP32 pubkey
base58Prefixes[EXT_PUBLIC_KEY] = {0x04, 0x88, 0xB2, 0x1E};
//BIP32 private key
base58Prefixes[EXT_SECRET_KEY] = {0x04, 0x88, 0xAD, 0xE4};

//短名,需要修改掉
bech32_hrp = "bc";

vFixedSeeds = std::vector<SeedSpec6>(pnSeed6_main, pnSeed6_main + ARRAYLEN(pnSeed6_main));

fDefaultConsistencyChecks = false;
fRequireStandard = true;
fMineBlocksOnDemand = false;

//The Bitcoin client uses these as it verifies the Bitcoin block chain.
checkpointData = {
{
{ 11111, uint256S("0x0000000069e244f73d78e8fd29ba2fd2ed618bd6fa2ee92559f542fdb26e7c1d")},
{ 33333, uint256S("0x000000002dd5588a74784eaa7ab0507a18ad16a236e7b1ce69f00d7ddfb5d0a6")},
{ 74000, uint256S("0x0000000000573993a3c9e41ce34471c079dcf5f52a0e824a81e7f953b8661a20")},
{105000, uint256S("0x00000000000291ce28027faea320c8d2b054b2e0fe44a773f3eefb151d6bdc97")},
{134444, uint256S("0x00000000000005b12ffd4cd315cd34ffd4a594f430ac814c91184a0d42d2b0fe")},
{168000, uint256S("0x000000000000099e61ea72015e79632f216fe6cb33d7899acb35b75c8303b763")},
{193000, uint256S("0x000000000000059f452a5f7340de6682a977387c17010ff6e6c3bd83ca8b1317")},
{210000, uint256S("0x000000000000048b95347e83192f69cf0366076336c639f9b7228e9ba171342e")},
{216116, uint256S("0x00000000000001b4f4b433e81ee46494af945cf96014816a4e2370f11b23df4e")},
{225430, uint256S("0x00000000000001c108384350f74090433e7fcf79a606b8e797f065b130575932")},
{250000, uint256S("0x000000000000003887df1f29024b06fc2200b55f8af8f35453d7be294df2d214")},
{279000, uint256S("0x0000000000000001ae8c72a0b0c301f67e3afca10e819efa9041e458e9bd7e40")},
{295000, uint256S("0x00000000000000004d9b4ef50f0f9d686fd69db2e03af35a100370c64632a983")},
}
};

chainTxData = ChainTxData{
// Data as of block 0000000000000000002d6cca6761c99b3c2e936f9a0e304b7c7651a993f461de (height 506081).
1516903077, // * UNIX timestamp of last known number of transactions
295363220, // * total number of transactions between genesis and that timestamp
// (the tx=... number in the SetBestChain debug.log lines)
3.5 // * estimated number of transactions per second after that timestamp
};
}
};

比特币源码阅读(0.16)(九)

发表于 2018-04-16 | 分类于 区块链 | 阅读次数:

CBlock

区块是一种被包含在公开账簿(区块链)里的聚合了交易信息的容器数据结构。它由一个包含元数据的区块头和紧跟其后的构成区块主体的一长串交易列表组成。区块头是80字节,平均每个交易至少是250字节,平均每个区块至少包含超过500个交易。因此,一个包含所有交易的完整区块比区块头大1000倍。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class CBlockHeader
{
public:
// 版本号,通常是1
int32_t nVersion;
// 父区块哈希值
uint256 hashPrevBlock;
//merkle树根(一种用来有效地总结区块中所有交易的数据结构)
uint256 hashMerkleRoot;
//时间戳
uint32_t nTime;
//难度目标,这个标记的值被存为系数/指数格式,前两位十六进制数字为幂(exponent),接下来得六位为系数(coefficient)。
uint32_t nBits;
//工作量证明算法的计数器
uint32_t nNonce;
};

class CBlock : public CBlockHeader
{
public:
// network and disk
std::vector<CTransactionRef> vtx;

// memory only
mutable bool fChecked;
};

比特币源码阅读(0.16)(八)

发表于 2018-04-16 | 阅读次数:

uint256和arith_uint256

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class arith_uint256 : public base_uint<256> {
public:
arith_uint256() {}
arith_uint256(const base_uint<256>& b) : base_uint<256>(b) {}
arith_uint256(uint64_t b) : base_uint<256>(b) {}
explicit arith_uint256(const std::string& str) : base_uint<256>(str) {}

/**
* The "compact" format is a representation of a whole
* number N using an unsigned 32bit number similar to a
* floating point format.
* The most significant 8 bits are the unsigned exponent of base 256.
* This exponent can be thought of as "number of bytes of N".
* The lower 23 bits are the mantissa.
* Bit number 24 (0x800000) represents the sign of N.
* N = (-1^sign) * mantissa * 256^(exponent-3)
*
* Satoshi's original implementation used BN_bn2mpi() and BN_mpi2bn().
* MPI uses the most significant bit of the first byte as sign.
* Thus 0x1234560000 is compact (0x05123456)
* and 0xc0de000000 is compact (0x0600c0de)
*
* Bitcoin only uses this "compact" format for encoding difficulty
* targets, which are unsigned 256bit quantities. Thus, all the
* complexities of the sign bit and using base 256 are probably an
* implementation accident.
*/
arith_uint256& SetCompact(uint32_t nCompact, bool *pfNegative = nullptr, bool *pfOverflow = nullptr);
uint32_t GetCompact(bool fNegative = false) const;

friend uint256 ArithToUint256(const arith_uint256 &);
friend arith_uint256 UintToArith256(const uint256 &);
};
1…34

李蔚海

33 日志
1 分类
1 标签
RSS
E-Mail
© 2018 李蔚海
由 Hexo 强力驱动
|
主题 — NexT.Mist v5.1.4