Skip to content

Commit

Permalink
Insert witness commitments in generation transaction output
Browse files Browse the repository at this point in the history
  • Loading branch information
luke-jr committed Jan 29, 2016
1 parent 662f2d8 commit 5120ab0
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
7 changes: 7 additions & 0 deletions bitcoin/script.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ def toAddress(cls, addr):
elif ver == 5 or ver == 196:
return b'\xa9\x14' + pubkeyhash + b'\x87'
raise ValueError('invalid address version')

@classmethod
def commitment(cls, commitment):
clen = len(commitment)
if clen > 0x4b:
raise NotImplementedError
return b'\x6a' + bytes((clen,)) + commitment

def countSigOps(s):
# FIXME: don't count data as ops
Expand Down
9 changes: 8 additions & 1 deletion eloipool.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,10 @@ def RaiseRedFlags(reason):
import subprocess
from time import time

def makeCoinbaseTxn(coinbaseValue, useCoinbaser = True, prevBlockHex = None):
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:
Expand Down Expand Up @@ -128,6 +131,10 @@ def makeCoinbaseTxn(coinbaseValue, useCoinbaser = True, prevBlockHex = None):
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
Expand Down
12 changes: 7 additions & 5 deletions merklemaker.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ def URI2Access(uri):

def createClearMerkleTree(self, height):
subsidy = self.SubsidyAlgo(height)
cbtxn = self.makeCoinbaseTxn(subsidy, False)
cbtxn = self.makeCoinbaseTxn(subsidy, False, witness_commitment=None)
cbtxn.setCoinbase(b'\0\0') # necessary to avoid triggering segwit marker+flags
cbtxn.assemble()
mt = MerkleTree([cbtxn])
Expand Down Expand Up @@ -406,7 +406,9 @@ def _ProcessGBT(self, MP, TS = None):
ka['txid'] = iinfo['txid']
txnobjs.append(Txn(data=txnlist[i], **ka))

cbtxn = self.makeCoinbaseTxn(MP['coinbasevalue'], prevBlockHex = MP['previousblockhash'])
witness_commitment = CalculateWitnessCommitment(txnobjs, self.WitnessNonce)

cbtxn = self.makeCoinbaseTxn(MP['coinbasevalue'], prevBlockHex = MP['previousblockhash'], witness_commitment=witness_commitment)
cbtxn.setCoinbase(b'\0\0')
cbtxn.assemble()
txnobjs[0] = cbtxn
Expand All @@ -416,7 +418,7 @@ def _ProcessGBT(self, MP, TS = None):
newMerkleTree.POTInfo = MP.get('POTInfo')
newMerkleTree.MP = MP
newMerkleTree.oMP = oMP
newMerkleTree.witness_commitment = CalculateWitnessCommitment(txnobjs, self.WitnessNonce)
newMerkleTree.witness_commitment = witness_commitment

return newMerkleTree

Expand Down Expand Up @@ -823,9 +825,9 @@ def MBS(LO = 0):
txninfo[2]['fee'] = 0
assert MBS(1) == (MP, txnlist, txninfo)
# _ProcessGBT tests
def makeCoinbaseTxn(coinbaseValue, useCoinbaser = True, prevBlockHex = None):
def makeCoinbaseTxn(coinbaseValue, useCoinbaser = True, prevBlockHex = None, witness_commitment=None):
txn = Txn.new()
txn.addOutput(coinbaseValue, b'')
txn.addOutput(coinbaseValue, BitcoinScript.commitment(witness_commitment) if witness_commitment else b'')
return txn
MM.makeCoinbaseTxn = makeCoinbaseTxn
MM.updateBlock = lambda *a, **ka: None
Expand Down

0 comments on commit 5120ab0

Please sign in to comment.