Skip to content

Commit 6e2383a

Browse files
author
Ross Nicoll
committed
Move MAX_MONEY into a core chain parameter
Enables support for blockchains whose MAX_MONEY does not match Bitcoin.
1 parent 2d4b68a commit 6e2383a

2 files changed

Lines changed: 25 additions & 5 deletions

File tree

bitcoin/core/__init__.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,15 @@
2424

2525
# Core definitions
2626
COIN = 100000000
27-
MAX_MONEY = 21000000 * COIN
2827
MAX_BLOCK_SIZE = 1000000
2928
MAX_BLOCK_SIGOPS = MAX_BLOCK_SIZE/50
3029

31-
def MoneyRange(nValue):
32-
return 0 <= nValue <= MAX_MONEY
30+
def MoneyRange(nValue, params=None):
31+
global coreparams
32+
if not params:
33+
params = coreparams
34+
35+
return 0 <= nValue <= params.MAX_MONEY
3336

3437
def _py2_x(h):
3538
"""Convert a hex string to bytes"""
@@ -537,12 +540,14 @@ def GetHash(self):
537540

538541
class CoreChainParams(object):
539542
"""Define consensus-critical parameters of a given instance of the Bitcoin system"""
543+
MAX_MONEY = None
540544
GENESIS_BLOCK = None
541545
PROOF_OF_WORK_LIMIT = None
542546
SUBSIDY_HALVING_INTERVAL = None
543547
NAME = None
544548

545549
class CoreMainParams(CoreChainParams):
550+
MAX_MONEY = 21000000 * COIN
546551
NAME = 'mainnet'
547552
GENESIS_BLOCK = CBlock.deserialize(x('0100000000000000000000000000000000000000000000000000000000000000000000003ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a29ab5f49ffff001d1dac2b7c0101000000010000000000000000000000000000000000000000000000000000000000000000ffffffff4d04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73ffffffff0100f2052a01000000434104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac00000000'))
548553
SUBSIDY_HALVING_INTERVAL = 210000
@@ -586,6 +591,7 @@ def CheckTransaction(tx):
586591
587592
Raises CheckTransactionError
588593
"""
594+
global coreparams
589595

590596
if not tx.vin:
591597
raise CheckTransactionError("CheckTransaction() : vin empty")
@@ -601,7 +607,7 @@ def CheckTransaction(tx):
601607
for txout in tx.vout:
602608
if txout.nValue < 0:
603609
raise CheckTransactionError("CheckTransaction() : txout.nValue negative")
604-
if txout.nValue > MAX_MONEY:
610+
if txout.nValue > coreparams.MAX_MONEY:
605611
raise CheckTransactionError("CheckTransaction() : txout.nValue too high")
606612
nValueOut += txout.nValue
607613
if not MoneyRange(nValueOut):
@@ -737,7 +743,6 @@ def CheckBlock(block, fCheckPoW = True, fCheckMerkleRoot = True, cur_time=None):
737743
'Hash',
738744
'Hash160',
739745
'COIN',
740-
'MAX_MONEY',
741746
'MAX_BLOCK_SIZE',
742747
'MAX_BLOCK_SIGOPS',
743748
'MoneyRange',

bitcoin/tests/test_core.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,21 @@ def T(value, expected):
3232
T(1001000000, '10.01')
3333
T(1012345678, '10.12345678')
3434

35+
class Test_Money(unittest.TestCase):
36+
def test_MoneyRange(self):
37+
self.assertFalse(MoneyRange(-1))
38+
self.assertTrue(MoneyRange(0))
39+
self.assertTrue(MoneyRange(100000))
40+
self.assertTrue(MoneyRange(21000000 * COIN)) # Maximum money on Bitcoin network
41+
self.assertFalse(MoneyRange(21000001 * COIN))
42+
43+
def test_MoneyRangeCustomParams(self):
44+
highMaxParamsType = type(str('CoreHighMainParams'), (CoreMainParams,object), {'MAX_MONEY': 22000000 * COIN })
45+
highMaxParams = highMaxParamsType()
46+
self.assertTrue(MoneyRange(21000001 * COIN, highMaxParams))
47+
self.assertTrue(MoneyRange(22000000 * COIN, highMaxParams))
48+
self.assertFalse(MoneyRange(22000001 * COIN, highMaxParams))
49+
3550
class Test_CBlockHeader(unittest.TestCase):
3651
def test_serialization(self):
3752
genesis = CBlockHeader(nVersion=1,

0 commit comments

Comments
 (0)