MCPcopy Index your code
hub / github.com/jimmysong/programmingbitcoin / fetch

Method fetch

code-ch13/tx.py:30–48  ·  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 tx = Tx.parse(BytesIO(raw), testnet=testnet)
39 # make sure the tx we got matches to the hash we requested
40 if tx.segwit:
41 computed = tx.id()
42 else:
43 computed = hash256(raw)[::-1].hex()
44 if computed != tx_id:
45 raise RuntimeError('server lied: {} vs {}'.format(computed, tx_id))
46 cls.cache[tx_id] = tx
47 cls.cache[tx_id].testnet = testnet
48 return cls.cache[tx_id]
49
50 @classmethod
51 def load_cache(cls, filename):

Callers 8

fetch_txMethod · 0.45
test_sig_hashMethod · 0.45
test_verify_p2pkhMethod · 0.45
test_verify_p2shMethod · 0.45
test_verify_p2wpkhMethod · 0.45
test_verify_p2wshMethod · 0.45

Calls 5

hash256Function · 0.90
get_urlMethod · 0.45
parseMethod · 0.45
idMethod · 0.45
hexMethod · 0.45

Tested by 7

test_sig_hashMethod · 0.36
test_verify_p2pkhMethod · 0.36
test_verify_p2shMethod · 0.36
test_verify_p2wpkhMethod · 0.36
test_verify_p2wshMethod · 0.36