16
16
#raw_txn_str = '01000000012f03082d300efd92837d3f6d910a21d9d19e868242cfebb21198beed7b440999000000004a493046022100c0f693e024f966dc5f834324baa38426bba05460a2b3f9920989d38322176460022100c523a3aa62da26db1fc1902a93741dce3489629df18be11ba68ff9586041821601ffffffff0100f2052a010000001976a9148773ec867e322378e216eefe55bfcede5263059b88ac00000000'
17
17
#raw_txn_str = '01000000017f950ab790838e0c05e79856d25d586823fe139e1807405a3f207ff33f9b7663010000006b483045022100d8629403cd3b49950da9293653c6279149c029e6b7b15371342d0d2ce286c8f2022078787985a644e94fd9246f6c25733336c94af5f00d9d34a07dc2f9e0987ef990012102b726d7eae11a6d5cf3b2362e773e116a6140347dcee1b2943f4a2897351e5d90ffffffff021bf03c000000000017a91469f3757380a56820abc7052867216599e575cddd8777c1ca1c000000001976a914d5f950abe0b559b2b7a7ab3d18a507ea1c3e4ac688ac00000000'
18
18
#txn_hash = '4269fdc239d027922dcec96f1ae283dbaff10e2d1bd49605661d091e79714956'
19
- txn_hash = '8ca45aed169b0434ad5a117804cdf6eec715208d57f13396c0ba18fb5a327e30'
19
+ #txn_hash = '8ca45aed169b0434ad5a117804cdf6eec715208d57f13396c0ba18fb5a327e30' # P2PKH
20
+ #txn_hash = '40eee3ae1760e3a8532263678cdf64569e6ad06abc133af64f735e52562bccc8'
21
+ txn_hash = '7edb32d4ffd7a385b763c7a8e56b6358bcd729e747290624e18acdbe6209fc45' # P2SH
20
22
block_hash = '0000000000000000009a8aa7b36b0e37a28bf98956097b7b844e172692e604e1'
21
23
22
24
@@ -592,6 +594,11 @@ def scriptParser(txn: dict, input_index: int):
592
594
else : # Any non assigned opcode
593
595
return False
594
596
597
+ print ('stack = %s' % stack )
598
+ for item in stack :
599
+ if type (item ) == bytes :
600
+ print ('stack item = %s' % bytes .decode (binascii .hexlify (item )))
601
+
595
602
return stack .pop ()
596
603
597
604
def convertPKHToAddress (prefix , addr ):
@@ -721,10 +728,11 @@ def unlockTxn(mptr: mmap):
721
728
print ('Network fees = %d' % (input_satoshis - out_satoshis ))
722
729
for index in range (input_count ):
723
730
status = scriptParser (txn , index )
731
+ print ('status = %s' % status )
724
732
if status == False :
725
733
print ('Invalid Transaction' )
726
- return False
727
- return True
734
+ return ( False , 0 )
735
+ return ( True , input_satoshis - out_satoshis )
728
736
729
737
g_block_header_size = 80
730
738
@@ -747,20 +755,19 @@ def validate_all_transactions_of_block(block_hash_bigendian_b: bytes):
747
755
getBlockHeader (mptr )
748
756
txn_count = getTransactionCount (mptr )
749
757
print ('txn_count = %d' % txn_count )
750
- skip_coinbase_txn = getCoinbaseTransaction (mptr )
758
+ coinbase_txn = getCoinbaseTransaction (mptr )
759
+ net_fees = 0
751
760
for index in range (1 , txn_count ):
752
761
print ('XXXXXXXXXXXXXXXX txn index = %d' % index )
753
- isValid = unlockTxn (mptr )
762
+ isValid , satoshis = unlockTxn (mptr )
754
763
if isValid == False :
755
764
print ('Invalid Transaction' )
756
765
exit ()
757
766
else :
758
767
print ('Valid Transaction' )
759
- # block = getBlock(mptr, start - 8)
760
- # print('magic number = %s' % (block['block_pre_header']['magic_number']))
761
- # next_block_hash = block['block_header']['prev_block_hash']
762
- # print('next block hash = %s, n_file = %d, height = %d' % (next_block_hash, jsonobj['n_file'], jsonobj['height']))
763
- # next_block_hash_bigendian_b = binascii.unhexlify(next_block_hash)[::-1]
768
+ net_fees += satoshis
769
+ print ('mining reward = %.8f' % (sum (out ['satoshis' ] for out in coinbase_txn ['out' ]) / 100000000.0 ))
770
+ print ('net_fees = %.8f' % (net_fees / 100000000.0 ))
764
771
765
772
if __name__ == '__main__' :
766
773
txn_hash_bigendian = binascii .unhexlify (txn_hash )[::- 1 ]
@@ -774,13 +781,11 @@ def validate_all_transactions_of_block(block_hash_bigendian_b: bytes):
774
781
mptr = mmap .mmap (block_file .fileno (), 0 , prot = mmap .PROT_READ ) #File is open read-only
775
782
776
783
mptr .seek (block_offset + g_block_header_size + txn_offset )
777
- # print(bytes.decode(binascii.hexlify(mptr.read(1000))))
778
- # print(raw_txn_str)
779
784
isValid = unlockTxn (mptr )
780
785
if isValid == False :
781
786
print ('Invalid Transaction' )
782
787
else :
783
788
print ('Valid Transaction' )
784
789
785
- block_hash_bigendian_b = binascii .unhexlify (block_hash )[::- 1 ]
786
- validate_all_transactions_of_block (block_hash_bigendian_b )
790
+ # block_hash_bigendian_b = binascii.unhexlify(block_hash)[::-1]
791
+ # validate_all_transactions_of_block(block_hash_bigendian_b)
0 commit comments