Dash Core  0.12.2.1
P2P Digital Currency
blocktools.py
Go to the documentation of this file.
1 # blocktools.py - utilities for manipulating blocks and transactions
2 #
3 # Distributed under the MIT/X11 software license, see the accompanying
4 # file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 #
6 
7 from .mininode import *
8 from .script import CScript, OP_TRUE, OP_CHECKSIG
9 
10 # Create a block (with regtest difficulty)
11 def create_block(hashprev, coinbase, nTime=None):
12  block = CBlock()
13  if nTime is None:
14  import time
15  block.nTime = int(time.time()+600)
16  else:
17  block.nTime = nTime
18  block.hashPrevBlock = hashprev
19  block.nBits = 0x207fffff # Will break after a difficulty adjustment...
20  block.vtx.append(coinbase)
21  block.hashMerkleRoot = block.calc_merkle_root()
22  block.calc_sha256()
23  return block
24 
26  r = bytearray(0)
27  if value == 0:
28  return r
29  neg = value < 0
30  absvalue = -value if neg else value
31  while (absvalue):
32  r.append(int(absvalue & 0xff))
33  absvalue >>= 8
34  if r[-1] & 0x80:
35  r.append(0x80 if neg else 0)
36  elif neg:
37  r[-1] |= 0x80
38  return r
39 
40 # Create a coinbase transaction, assuming no miner fees.
41 # If pubkey is passed in, the coinbase output will be a P2PK output;
42 # otherwise an anyone-can-spend output.
43 def create_coinbase(height, pubkey = None):
44  coinbase = CTransaction()
45  coinbase.vin.append(CTxIn(COutPoint(0, 0xffffffff),
46  ser_string(serialize_script_num(height)), 0xffffffff))
47  coinbaseoutput = CTxOut()
48  coinbaseoutput.nValue = 500 * COIN
49  halvings = int(height/150) # regtest
50  coinbaseoutput.nValue >>= halvings
51  if (pubkey != None):
52  coinbaseoutput.scriptPubKey = CScript([pubkey, OP_CHECKSIG])
53  else:
54  coinbaseoutput.scriptPubKey = CScript([OP_TRUE])
55  coinbase.vout = [ coinbaseoutput ]
56  coinbase.calc_sha256()
57  return coinbase
58 
59 # Create a transaction with an anyone-can-spend output, that spends the
60 # nth output of prevtx.
61 def create_transaction(prevtx, n, sig, value):
62  tx = CTransaction()
63  assert(n < len(prevtx.vout))
64  tx.vin.append(CTxIn(COutPoint(prevtx.sha256, n), sig, 0xffffffff))
65  tx.vout.append(CTxOut(value, b""))
66  tx.calc_sha256()
67  return tx
def serialize_script_num(value)
Definition: blocktools.py:25
def create_block(hashprev, coinbase, nTime=None)
Definition: blocktools.py:11
def create_coinbase(height, pubkey=None)
Definition: blocktools.py:43
def create_transaction(prevtx, n, sig, value)
Definition: blocktools.py:61