Skip to content

Commit 05dcd6b

Browse files
committed
fixed the last test of ch12
1 parent 327719a commit 05dcd6b

File tree

5 files changed

+63
-156
lines changed

5 files changed

+63
-156
lines changed

appa.asciidoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -707,5 +707,5 @@ include::code-ch12/answers.py[tag=exercise6,indent=0]
707707

708708
[source,python]
709709
----
710-
include::code-ch12/broadcast.py[tag=answer6,indent=0]
710+
include::code-ch12/answers.py[tag=answer6,indent=0]
711711
----

code-ch12/Chapter12.ipynb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@
321321
"\n",
322322
"\n",
323323
"# connect to tbtc.programmingblockchain.com in testnet mode\n",
324-
"# create a bloom filter of size 30 and 5 functions. Add a tweak that you like\n",
324+
"# create a bloom filter of size 30 and 5 functions. Add a tweak.\n",
325325
"# add the h160 to the bloom filter\n",
326326
"# complete the handshake\n",
327327
"# load the bloom filter with the filterload command\n",
@@ -349,7 +349,7 @@
349349
" # set the tx's testnet to be True\n",
350350
" # loop through the tx outs\n",
351351
" # if our output has the same address as our address we found it\n",
352-
" # we found our utxo. set prev_tx, prev_index, and transaction\n",
352+
" # we found our utxo. set prev_tx, prev_index, and tx\n",
353353
"# create the TxIn\n",
354354
"# calculate the output amount (previous amount minus the fee)\n",
355355
"# create a new TxOut to the target script with the output amount\n",

code-ch12/answers.py

Lines changed: 58 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
... little_endian_to_int,
6161
... read_varint,
6262
... SIGHASH_ALL,
63-
>>> )
63+
... )
6464
>>> from merkleblock import MerkleBlock
6565
>>> from network import (
6666
... GetDataMessage,
@@ -70,69 +70,113 @@
7070
... SimpleNode,
7171
... TX_DATA_TYPE,
7272
... FILTERED_BLOCK_DATA_TYPE,
73-
>>> )
73+
... )
7474
>>> from script import p2pkh_script, Script
7575
>>> from tx import Tx, TxIn, TxOut, TxFetcher
76-
>>> last_block_hex = '00000000000538d5c2246336644f9a4956551afb44ba47278759ec55\
77-
ea912e19'
78-
>>> secret = little_endian_to_int(hash256(b''))
76+
>>> last_block_hex = '00000000000000a03f9432ac63813c6710bfe41712ac5ef6faab093f\
77+
e2917636'
78+
>>> secret = little_endian_to_int(hash256(b'Jimmy Song'))
7979
>>> private_key = PrivateKey(secret=secret)
8080
>>> addr = private_key.point.address(testnet=True)
8181
>>> h160 = decode_base58(addr)
8282
>>> target_address = 'mwJn1YPMq7y5F8J3LkC5Hxg9PHyZ5K4cFv'
8383
>>> target_h160 = decode_base58(target_address)
8484
>>> target_script = p2pkh_script(target_h160)
8585
>>> fee = 5000 # fee in satoshis
86+
>>> # connect to tbtc.programmingblockchain.com in testnet mode
8687
>>> node = SimpleNode('tbtc.programmingblockchain.com', testnet=True, logging=\
87-
True)
88+
False)
89+
>>> # create a bloom filter of size 30 and 5 functions. Add a tweak.
8890
>>> bf = BloomFilter(30, 5, 90210)
91+
>>> # add the h160 to the bloom filter
8992
>>> bf.add(h160)
93+
>>> # complete the handshake
9094
>>> node.handshake()
95+
>>> # load the bloom filter with the filterload command
9196
>>> node.send(bf.filterload())
97+
>>> # set start block to last_block from above
9298
>>> start_block = bytes.fromhex(last_block_hex)
99+
>>> # send a getheaders message with the starting block
93100
>>> getheaders = GetHeadersMessage(start_block=start_block)
94101
>>> node.send(getheaders)
102+
>>> # wait for the headers message
95103
>>> headers = node.wait_for(HeadersMessage)
104+
>>> # store the last block as None
96105
>>> last_block = None
106+
>>> # initialize the GetDataMessage
97107
>>> getdata = GetDataMessage()
108+
>>> # loop through the blocks in the headers
98109
>>> for b in headers.blocks:
110+
... # check that the proof of work on the block is valid
99111
... if not b.check_pow():
100112
... raise RuntimeError('proof of work is invalid')
113+
... # check that this block's prev_block is the last block
101114
... if last_block is not None and b.prev_block != last_block:
102115
... raise RuntimeError('chain broken')
116+
... # add a new item to the getdata message
117+
... # should be FILTERED_BLOCK_DATA_TYPE and block hash
103118
... getdata.add_data(FILTERED_BLOCK_DATA_TYPE, b.hash())
119+
... # set the last block to the current hash
104120
... last_block = b.hash()
121+
>>> # send the getdata message
105122
>>> node.send(getdata)
106-
>>> prev_tx, prev_index = None, None
123+
>>> # initialize prev_tx, prev_index and prev_amount to None
124+
>>> prev_tx, prev_index, prev_amount = None, None, None
125+
>>> # loop while prev_tx is None
107126
>>> while prev_tx is None:
127+
... # wait for the merkleblock or tx commands
108128
... message = node.wait_for(MerkleBlock, Tx)
129+
... # if we have the merkleblock command
109130
... if message.command == b'merkleblock':
131+
... # check that the MerkleBlock is valid
110132
... if not message.is_valid():
111133
... raise RuntimeError('invalid merkle proof')
134+
... # else we have the tx command
112135
... else:
136+
... # set the tx's testnet to be True
113137
... message.testnet = True
138+
... # loop through the tx outs
114139
... for i, tx_out in enumerate(message.tx_outs):
140+
... # if our output has the same address as our address we found it
115141
... if tx_out.script_pubkey.address(testnet=True) == addr:
142+
... # we found our utxo. set prev_tx, prev_index, and tx
116143
... prev_tx = message.hash()
117144
... prev_index = i
145+
... prev_amount = tx_out.amount
118146
... print('found: {}:{}'.format(prev_tx.hex(), prev_index))
147+
found: b2cddd41d18d00910f88c31aa58c6816a190b8fc30fe7c665e1cd2ec60efdf3f:7
148+
>>> # create the TxIn
119149
>>> tx_in = TxIn(prev_tx, prev_index)
150+
>>> # calculate the output amount (previous amount minus the fee)
120151
>>> output_amount = prev_amount - fee
121-
>>> tx_outs = TxOut(output_amount, target_script)
152+
>>> # create a new TxOut to the target script with the output amount
153+
>>> tx_out = TxOut(output_amount, target_script)
154+
>>> # create a new transaction with the one input and one output
122155
>>> tx_obj = Tx(1, [tx_in], [tx_out], 0, testnet=True)
123-
>>> tx_obj.sign_input(0, private_key)
156+
>>> # sign the only input of the transaction
157+
>>> print(tx_obj.sign_input(0, private_key))
158+
True
159+
>>> # serialize and hex to see what it looks like
124160
>>> print(tx_obj.serialize().hex())
125-
010000000194e631abb9e1079ec72a1616a3aa0111c614e65b96a6a4420e2cc6af9e6cc96e0000\
126-
00006a47304402203cc8c56abe1c0dd043afa9eb125dafbebdde2dd4cd7abf0fb1aae0667a2200\
127-
6e02203c95b74d0f0735bbf1b261d36e077515b6939fc088b9d7c1b7030a5e494596330121021c\
128-
dd761c7eb1c90c0af0a5963e94bf0203176b4662778d32bd6d7ab5d8628b32ffffffff01f88298\
129-
00000000001976a914ad346f8eb57dee9a37981716e498120ae80e44f788ac00000000
161+
01000000013fdfef60ecd21c5e667cfe30fcb890a116688ca51ac3880f91008dd141ddcdb20700\
162+
00006b483045022100ff77d2559261df5490ed00d231099c4b8ea867e6ccfe8e3e6d077313ed4f\
163+
1428022033a1db8d69eb0dc376f89684d1ed1be75719888090388a16f1e8eedeb8067768012103\
164+
dc585d46cfca73f3a75ba1ef0c5756a21c1924587480700c6eb64e3f75d22083ffffffff019334\
165+
e500000000001976a914ad346f8eb57dee9a37981716e498120ae80e44f788ac00000000
166+
>>> # send this signed transaction on the network
130167
>>> node.send(tx_obj)
168+
>>> # wait a sec so this message goes through with time.sleep(1)
131169
>>> time.sleep(1)
170+
>>> # now ask for this transaction from the other node
171+
>>> # create a GetDataMessage
132172
>>> getdata = GetDataMessage()
173+
>>> # ask for our transaction by adding it to the message
133174
>>> getdata.add_data(TX_DATA_TYPE, tx_obj.hash())
175+
>>> # send the message
134176
>>> node.send(getdata)
177+
>>> # now wait for a Tx response
135178
>>> received_tx = node.wait_for(Tx)
179+
>>> # if the received tx has the same id as our tx, we are done!
136180
>>> if received_tx.id() == tx_obj.id():
137181
... print('success!')
138182
success!

code-ch12/broadcast.py

Lines changed: 0 additions & 137 deletions
This file was deleted.

code-ch12/jupyter.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ fee = 5000 # fee in satoshis
8888

8989

9090
# connect to tbtc.programmingblockchain.com in testnet mode
91-
# create a bloom filter of size 30 and 5 functions. Add a tweak that you like
91+
# create a bloom filter of size 30 and 5 functions. Add a tweak.
9292
# add the h160 to the bloom filter
9393
# complete the handshake
9494
# load the bloom filter with the filterload command
@@ -116,7 +116,7 @@ fee = 5000 # fee in satoshis
116116
# set the tx's testnet to be True
117117
# loop through the tx outs
118118
# if our output has the same address as our address we found it
119-
# we found our utxo. set prev_tx, prev_index, and transaction
119+
# we found our utxo. set prev_tx, prev_index, and tx
120120
# create the TxIn
121121
# calculate the output amount (previous amount minus the fee)
122122
# create a new TxOut to the target script with the output amount

0 commit comments

Comments
 (0)