Skip to content

Commit

Permalink
feat(btc_api): integrate into chain proofs
Browse files Browse the repository at this point in the history
  • Loading branch information
lucidLuckylee committed May 11, 2023
1 parent aa42d6a commit 056c96d
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 52 deletions.
2 changes: 1 addition & 1 deletion src/block/block_header.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func fetch_block_header(block_height) -> felt* {
let (raw_block_header) = alloc();
%{
block_hex = BTC_API.get_bock_header_raw(ids.block_height)
block_hex = BTC_API.get_block_header_raw(ids.block_height)
from_hex(block_hex, ids.raw_block_header)
%}

Expand Down
14 changes: 1 addition & 13 deletions src/chain_proof/main.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,7 @@ func fetch_block(block_height) -> felt* {
let (block_data) = alloc();
%{
import urllib3
import json
http = urllib3.PoolManager()
url = 'https://blockstream.info/api/block-height/' + str(ids.block_height)
r = http.request('GET', url)
block_hash = str(r.data, 'utf-8')
url = f'https://blockstream.info/api/block/{ block_hash }/raw'
r = http.request('GET', url)
block_hex = r.data.hex()
block_hex = BTC_API.get_bock_raw(ids.block_height)
from_hex(block_hex, ids.block_data)
%}

Expand Down
14 changes: 1 addition & 13 deletions src/headers_chain_proof/main.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,7 @@ func fetch_block(block_height) -> felt* {
let (block_data) = alloc();
%{
import urllib3
import json
http = urllib3.PoolManager()
url = 'https://blockstream.info/api/block-height/' + str(ids.block_height)
r = http.request('GET', url)
block_hash = str(r.data, 'utf-8')
url = f'https://blockstream.info/api/block/{ block_hash }/raw'
r = http.request('GET', url)
block_hex = r.data.hex()
block_hex = BTC_API.get_block_header_raw(ids.block_height)
from_hex(block_hex, ids.block_data)
%}

Expand Down
6 changes: 4 additions & 2 deletions src/transaction/transaction.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ func fetch_transaction(block_height, tx_index) -> felt* {
%{
# BTC_API is defined in src/utils/python_utils.cairo
tx_hex = BTC_API.get_transaction(ids.block_height, ids.tx_index)
tx_hex = BTC_API.get_transaction_raw(ids.block_height, ids.tx_index)
from_hex(tx_hex, ids.raw_transaction)
%}
return raw_transaction;
Expand Down Expand Up @@ -337,7 +337,9 @@ func validate_output{range_check_ptr, utreexo_roots: felt*, hash_ptr: HashBuilti
// See also:
// - https://developer.bitcoin.org/devguide/transactions.html#signature-hash-types
//
func write_transaction{writer: Writer, range_check_ptr, bitwise_ptr: BitwiseBuiltin*}(transaction: Transaction) {
func write_transaction{writer: Writer, range_check_ptr, bitwise_ptr: BitwiseBuiltin*}(
transaction: Transaction
) {
alloc_locals;
write_uint32(transaction.version);

Expand Down
29 changes: 18 additions & 11 deletions src/utils/btc_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,16 @@ def get_transaction_raw(self, block_height, tx_index):
return tx_hex

# Expecting bitcoind client with -txindex
@lru_cache(maxsize=CACHE_SIZE_LARGE_DATA)
def get_transaction(self, block_height, tx_index):
block_hash = self.get_block_hash(block_height)
tx_id = self.rpc.getblock(block_hash)['tx'][tx_index]
tx_json = self.rpc.getrawtransaction(f'{tx_id}', True)
return tx_json

def get_transaction_by_id(self, txid):
tx_json = self.rpc.getrawtransaction(f'{tx_id}', True)
return tx_json


class EsplorerAPI(BTCAPI):
def __init__(self, base_url):
Expand Down Expand Up @@ -203,30 +206,34 @@ def get_transaction_raw(self, block_height, tx_index):
r.data.decode('utf-8'))
exit(-1)
return tx_hex

@lru_cache(maxsize=CACHE_SIZE_LARGE_DATA)
def get_transaction(self, block_height, tx_index):
block_hash = self.get_block_hash(block_height)
url = self.base_url + f'block/{block_hash}/txid/' + str(tx_index)
def get_transaction_by_id(self, txid):
url = self.base_url + f'tx/{txid}'
r = self.pool_manager.request('GET', url)
txid = r.data.decode('utf-8')
tx = json.loads(r.data)
if r.status != 200:
print(
f'ERROR: get_transaction({block_height}, {tx_index}) could not retrieve tx_id from the remote API: ',
f'ERROR: get_transaction_by_id({txid}) could not retrieve tx_id from the remote API: ',
r.status,
r.data.decode('utf-8'))
exit(-1)
url = self.base_url + f'tx/{txid}'
return tx


@lru_cache(maxsize=CACHE_SIZE_LARGE_DATA)
def get_transaction(self, block_height, tx_index):
block_hash = self.get_block_hash(block_height)
url = self.base_url + f'block/{block_hash}/txid/' + str(tx_index)
r = self.pool_manager.request('GET', url)
tx = json.loads(r.data)
txid = r.data.decode('utf-8')
if r.status != 200:
print(
f'ERROR: get_transaction({block_height}, {tx_index}) could not retrieve tx_id from the remote API: ',
r.status,
r.data.decode('utf-8'))
exit(-1)
return tx

return self.get_transaction_by_id(txid)

if __name__ == '__main__':
API = BTCAPI.make_BTCAPI()
Expand Down
3 changes: 2 additions & 1 deletion src/utils/python_utils.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ func setup_python_defs() {
# Note: This import requires $PYTHONPATH to include the zerosync path.
# Currently appended in Makefile.
from src.utils.btc_api import BTCAPI
global BTC_API = BTCAPI.makeBTCAPI()
global BTC_API
BTC_API = BTCAPI.make_BTCAPI()
import re
def hex_to_felt(hex_string):
# Seperate hex_string into chunks of 8 chars.
Expand Down
12 changes: 1 addition & 11 deletions src/utxo_set/utxo_set.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,7 @@ func utxo_set_extract{hash_ptr: HashBuiltin*, utreexo_roots: felt*}(txid: felt*,
setup_python_defs();
%{
txid = hash_from_memory(ids.txid)
import urllib3
http = urllib3.PoolManager()
url = 'https://blockstream.info/api/tx/' + txid
r = http.request('GET', url)
if r.status != 200:
print("ERROR: Utxo_set_extract received a bad answer from the API: ", r.status, r.data.decode('utf-8'))
exit(-1)
import json
tx = json.loads(r.data)
tx = BTC_API.get_transaction_by_id(txid)
tx_output = tx["vout"][ids.vout]
ids.amount = tx_output["value"]
Expand Down

0 comments on commit 056c96d

Please sign in to comment.