Skip to content

Commit

Permalink
Calculate witness commitment when needed
Browse files Browse the repository at this point in the history
  • Loading branch information
luke-jr committed Jan 29, 2016
1 parent c7b02df commit c355da4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
10 changes: 10 additions & 0 deletions bitcoin/txn.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,16 @@ def idhash(self):
del self.txid
raise NotImplementedError
self.txid = dblsha(self.data)
if hasattr(self, 'witness_hash'):
del self.witness_hash

def withash(self):
self.witness_hash = dblsha(self.data)

def get_witness_hash(self):
if not hasattr(self, 'witness_hash'):
self.withash()
return self.witness_hash

# Txn tests
def _test():
Expand Down
18 changes: 17 additions & 1 deletion merklemaker.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@
_makeCoinbase = [0, 0]
_filecounter = 0

def CalculateWitnessCommitment(txnobjs, nonce):
gentx_withash = b'\0' * 0x20
withashes = (gentx_withash,) + tuple(a.get_witness_hash() for a in txnobjs[1:])
txids = (gentx_withash,) + tuple(a.txid for a in txnobjs[1:])
if withashes == txids:
# Unnecessary
return None

wmr = MerkleTree(data=withashes).merkleRoot()
commitment = dblsha(wmr + nonce)
return commitment

def MakeBlockHeader(MRD, BlockVersionBytes):
(merkleRoot, merkleTree, coinbase, prevBlock, bits) = MRD[:5]
timestamp = pack('<L', int(time()))
Expand Down Expand Up @@ -82,6 +94,7 @@ def __init__(self, *a, **k):
self.currentBlock = (None, None, None)
self.lastBlock = (None, None, None)
self.SubsidyAlgo = lambda height: 5000000000 >> (height // 210000)
self.WitnessNonce = b'\0' * 0x20

def _prepare(self):
self.UseTemplateChecks = True
Expand Down Expand Up @@ -166,7 +179,9 @@ def createClearMerkleTree(self, height):
cbtxn = self.makeCoinbaseTxn(subsidy, False)
cbtxn.setCoinbase(b'\0\0') # necessary to avoid triggering segwit marker+flags
cbtxn.assemble()
return MerkleTree([cbtxn])
mt = MerkleTree([cbtxn])
mt.witness_commitment = None
return mt

def updateBlock(self, newBlock, height = None, bits = None, _HBH = None):
if newBlock == self.currentBlock[0]:
Expand Down Expand Up @@ -402,6 +417,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)

return newMerkleTree

Expand Down

0 comments on commit c355da4

Please sign in to comment.