MCPcopy
hub / github.com/jimmysong/programmingbitcoin / fetch

Method fetch

code-ch08/tx.py:30–49  ·  view source on GitHub ↗
(cls, tx_id, testnet=False, fresh=False)

Source from the content-addressed store, hash-verified

28
29 @classmethod
30 def fetch(cls, tx_id, testnet=False, fresh=False):
31 if fresh or (tx_id not in cls.cache):
32 url = '{}/tx/{}.hex'.format(cls.get_url(testnet), tx_id)
33 response = requests.get(url)
34 try:
35 raw = bytes.fromhex(response.text.strip())
36 except ValueError:
37 raise ValueError('unexpected response: {}'.format(response.text))
38 # make sure the tx we got matches to the hash we requested
39 if raw[4] == 0:
40 raw = raw[:4] + raw[6:]
41 tx = Tx.parse(BytesIO(raw), testnet=testnet)
42 tx.locktime = little_endian_to_int(raw[-4:])
43 else:
44 tx = Tx.parse(BytesIO(raw), testnet=testnet)
45 if tx.id() != tx_id:
46 raise ValueError('not the same id: {} vs {}'.format(tx.id(), tx_id))
47 cls.cache[tx_id] = tx
48 cls.cache[tx_id].testnet = testnet
49 return cls.cache[tx_id]
50
51 @classmethod
52 def load_cache(cls, filename):

Callers 4

fetch_txMethod · 0.45
test_sig_hashMethod · 0.45
test_verify_p2pkhMethod · 0.45
test_verify_p2shMethod · 0.45

Calls 4

little_endian_to_intFunction · 0.90
get_urlMethod · 0.45
parseMethod · 0.45
idMethod · 0.45

Tested by 3

test_sig_hashMethod · 0.36
test_verify_p2pkhMethod · 0.36
test_verify_p2shMethod · 0.36