def makeCoinbaseTxn(coinbaseValue, useCoinbaser=True, prevBlockHex=None, witness_commitment=NotImplemented): if witness_commitment is NotImplemented: raise NotImplementedError txn = Txn.new() #todo needs get dev rewared from MP pkScript = BitcoinScript.toAddress(config.TrackerAddr) devfeeScript = BitcoinScript.toAddress(config.Coinbaser_fee) txn.addOutput(coinbaseValue - config.Coinbaser_value, pkScript) txn.addOutput(config.Coinbaser_value, devfeeScript) txn.addOutput(config.devreward_value, a2b_hex(config.devreward_pubkey)) # SegWit commitment if not witness_commitment is None: txn.addOutput( 0, BitcoinScript.commitment(WitnessMagic + witness_commitment)) # TODO # TODO: red flag on dupe coinbase return txn
def makeCoinbaseTxn(coinbaseValue, useCoinbaser=True, prevBlockHex=None, witness_commitment=None): txn = Txn.new() txn.addOutput( coinbaseValue, BitcoinScript.commitment(witness_commitment) if witness_commitment else b'') return txn
def makeCoinbaseTxn(coinbaseValue, useCoinbaser=True, prevBlockHex=None, witness_commitment=NotImplemented, rsk_blockhash=None): if witness_commitment is NotImplemented: raise NotImplementedError txn = Txn.new() if useCoinbaser and hasattr(config, 'CoinbaserCmd') and config.CoinbaserCmd: coinbased = 0 try: cmd = config.CoinbaserCmd cmd = cmd.replace('%d', str(coinbaseValue)) cmd = cmd.replace('%p', prevBlockHex or '""') p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE) nout = int(p.stdout.readline()) for i in range(nout): amount = int(p.stdout.readline()) addr = p.stdout.readline().rstrip(b'\n').decode('utf8') pkScript = BitcoinScript.toAddress(addr) txn.addOutput(amount, pkScript) coinbased += amount except: coinbased = coinbaseValue + 1 if coinbased >= coinbaseValue: logging.getLogger('makeCoinbaseTxn').error('Coinbaser failed!') txn.outputs = [] else: coinbaseValue -= coinbased pkScript = BitcoinScript.toAddress(config.TrackerAddr) txn.addOutput(coinbaseValue, pkScript) # SegWit commitment if not witness_commitment is None: txn.addOutput( 0, BitcoinScript.commitment(WitnessMagic + witness_commitment)) # RSK merged mining if rsk_blockhash is not None: txn.addOutput(0, rootstock.getRSKTag() + rsk_blockhash) # TODO # TODO: red flag on dupe coinbase return txn
def makeCoinbaseTxn(coinbaseValue, useCoinbaser = True, prevBlockHex = None, witness_commitment = NotImplemented): if witness_commitment is NotImplemented: raise NotImplementedError txn = Txn.new() if useCoinbaser and hasattr(config, 'CoinbaserCmd') and config.CoinbaserCmd: coinbased = 0 try: cmd = config.CoinbaserCmd cmd = cmd.replace('%d', str(coinbaseValue)) cmd = cmd.replace('%p', prevBlockHex or '""') p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE) nout = int(p.stdout.readline()) for i in range(nout): amount = int(p.stdout.readline()) addr = p.stdout.readline().rstrip(b'\n').decode('utf8') pkScript = BitcoinScript.toAddress(addr) txn.addOutput(amount, pkScript) coinbased += amount except: coinbased = coinbaseValue + 1 if coinbased >= coinbaseValue: logging.getLogger('makeCoinbaseTxn').error('Coinbaser failed!') txn.outputs = [] else: coinbaseValue -= coinbased pkScript = BitcoinScript.toAddress(config.TrackerAddr) txn.addOutput(coinbaseValue, pkScript) # SegWit commitment if not witness_commitment is None: txn.addOutput(0, BitcoinScript.commitment(WitnessMagic + witness_commitment)) # TODO # TODO: red flag on dupe coinbase return txn
def doJSON_getblocktemplate(self, params): if 'mode' in params and params['mode'] != 'template': raise AttributeError('getblocktemplate mode "%s" not supported' % (params['mode'],)) if 'longpollid' in params: self.processLP(params['longpollid']) RequestedTarget = None try: RequestedTarget = int(params['target'], 16) except: pass rv = dict(self.getblocktemplate_rv_template) p_magic = [False] (MC, wld, target) = self.server.getBlockTemplate(self.Username, p_magic=p_magic, RequestedTarget=RequestedTarget) (height, merkleTree, cb, prevBlock, bits) = MC[:5] rv['height'] = height rv['previousblockhash'] = b2a_hex(prevBlock[::-1]).decode('ascii') if p_magic[0]: rv['longpollid'] = 'bootstrap' else: rv['longpollid'] = str(self.server.LPId) tl = [] SegwitTemplate = False for rule in merkleTree.MP['rules']: if rule == 'segwit' or rule == '!segwit': SegwitTemplate = True break for txn in merkleTree.data[1:]: txno = {} txno['data'] = b2a_hex(txn.data).decode('ascii') if SegwitTemplate: txno['txid'] = b2a_hex(txn.txid[::-1]).decode('ascii') tl.append(txno) rv['transactions'] = tl now = int(time()) rv['time'] = now # FIXME: ensure mintime is always >= real mintime, both here and in share acceptance rv['mintime'] = now - 180 rv['curtime'] = now rv['maxtime'] = now + self.server.StaleWorkTimeout rv['expires'] = self.server.StaleWorkTimeout rv['bits'] = b2a_hex(bits[::-1]).decode('ascii') rv['target'] = '%064x' % (target,) t = deepcopy(merkleTree.data[0]) t.setCoinbase(cb) if not merkleTree.witness_commitment is None: assert t.outputs[-1] == (0, BitcoinScript.commitment(WitnessMagic + merkleTree.witness_commitment)) t.outputs.pop() t.assemble() txno = {} txno['data'] = b2a_hex(t.data).decode('ascii') rv['coinbasetxn'] = txno rv['version'] = merkleTree.MP['version'] rv['rules'] = merkleTree.MP['rules'] rv['vbavailable'] = merkleTree.MP['_filtered_vbavailable'] rv['vbrequired'] = rv['version'] & 0x1fffffff return rv
def makeCoinbaseTxn(coinbaseValue, useCoinbaser = True, prevBlockHex = None, witness_commitment=None): txn = Txn.new() txn.addOutput(coinbaseValue, BitcoinScript.commitment(witness_commitment) if witness_commitment else b'') return txn
def doJSON_getblocktemplate(self, params): if 'mode' in params and params['mode'] != 'template': raise AttributeError('getblocktemplate mode "%s" not supported' % (params['mode'], )) if 'longpollid' in params: self.processLP(params['longpollid']) RequestedTarget = None try: RequestedTarget = int(params['target'], 16) except: pass rv = dict(self.getblocktemplate_rv_template) p_magic = [False] (MC, wld, target) = self.server.getBlockTemplate( self.Username, p_magic=p_magic, RequestedTarget=RequestedTarget) (height, merkleTree, cb, prevBlock, bits) = MC[:5] rv['height'] = height rv['previousblockhash'] = b2a_hex(prevBlock[::-1]).decode('ascii') if p_magic[0]: rv['longpollid'] = 'bootstrap' else: rv['longpollid'] = str(self.server.LPId) tl = [] SegwitTemplate = False for rule in merkleTree.MP['rules']: if rule == 'segwit' or rule == '!segwit': SegwitTemplate = True break for txn in merkleTree.data[1:]: txno = {} txno['data'] = b2a_hex(txn.data).decode('ascii') if SegwitTemplate: txno['txid'] = b2a_hex(txn.txid[::-1]).decode('ascii') tl.append(txno) rv['transactions'] = tl now = int(time()) rv['time'] = now # FIXME: ensure mintime is always >= real mintime, both here and in share acceptance rv['mintime'] = now - 180 rv['curtime'] = now rv['maxtime'] = now + self.server.StaleWorkTimeout rv['expires'] = self.server.StaleWorkTimeout rv['bits'] = b2a_hex(bits[::-1]).decode('ascii') rv['target'] = '%064x' % (target, ) t = deepcopy(merkleTree.data[0]) t.setCoinbase(cb) if not merkleTree.witness_commitment is None: assert t.outputs[-1] == ( 0, BitcoinScript.commitment(WitnessMagic + merkleTree.witness_commitment)) t.outputs.pop() t.assemble() txno = {} txno['data'] = b2a_hex(t.data).decode('ascii') rv['coinbasetxn'] = txno rv['version'] = merkleTree.MP['version'] rv['rules'] = merkleTree.MP['rules'] rv['vbavailable'] = merkleTree.MP['_filtered_vbavailable'] rv['vbrequired'] = rv['version'] & 0x1fffffff return rv