Skip to content

Commit 059d77d

Browse files
committed
improve address encoding/decoding
1 parent f8918c2 commit 059d77d

File tree

9 files changed

+97
-108
lines changed

9 files changed

+97
-108
lines changed

gsrest/db/cassandra.py

Lines changed: 36 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import re
22
import time
3-
# import hashlib
43
import asyncio
54
import heapq
65
from async_lru import alru_cache
@@ -19,7 +18,11 @@
1918
from gsrest.util.eth_logs import decode_db_logs
2019
from gsrest.errors import NotFoundException, BadUserInputException
2120
from gsrest.util import is_eth_like
22-
from gsrest.util.address import cannonicalize_address, address_to_user_format
21+
from gsrest.util.address import (address_to_user_format)
22+
from gsrest.util.evm import (bytes_to_hex, strip_0x)
23+
from gsrest.util.tron import partial_tron_to_partial_evm
24+
25+
# import hashlib
2326

2427
SMALL_PAGE_SIZE = 1000
2528
BIG_PAGE_SIZE = 5000
@@ -58,20 +61,6 @@ def transaction_ordering_key(tx_id_key, tx):
5861
return (tx[tx_id_key], -trace_index, -log_index)
5962

6063

61-
def evm_address_from_hex(currency, address):
62-
# eth addresses are case insensitive
63-
try:
64-
if address.startswith("0x"):
65-
return bytes.fromhex(address[2:].lower())
66-
else:
67-
return bytes.fromhex(address.lower())
68-
except ValueError:
69-
# bytes.fromHex throws value error if non hex chars are found
70-
raise BadUserInputException(
71-
"The address provided does not look"
72-
f" like a {currency.upper()} address: {address}")
73-
74-
7564
def identity(y, x):
7665
return x
7766

@@ -792,10 +781,6 @@ async def get_addresses_by_ids(self,
792781
result = await self.concurrent_with_args(currency, 'transformed',
793782
query, params)
794783

795-
for row in result:
796-
if is_eth_like(currency):
797-
row['address'] = \
798-
address_to_user_format(currency, row['address'])
799784
return result
800785

801786
async def get_new_address(self, currency, address):
@@ -805,7 +790,7 @@ async def get_new_address(self, currency, address):
805790
if not prefix:
806791
return None
807792
if is_eth_like(currency):
808-
address = evm_address_from_hex(currency, address)
793+
#address = evm_address_from_hex(currency, address)
809794
prefix = prefix.upper()
810795
prefix_length = self.get_prefix_lengths(currency)['address']
811796
query = ("SELECT * FROM new_addresses "
@@ -825,7 +810,7 @@ async def get_address_id(self, currency, address):
825810
if not prefix:
826811
return None
827812
if is_eth_like(currency):
828-
address = evm_address_from_hex(currency, address)
813+
#address = evm_address_from_hex(currency, address)
829814
prefix = prefix.upper()
830815
query = ("SELECT address_id FROM address_ids_by_address_prefix "
831816
"WHERE address_prefix = %s AND address = %s")
@@ -914,10 +899,7 @@ async def new_address(self, currency, address):
914899
async def new_entity(self, currency, address):
915900
data = await self.new_address(currency, address)
916901
data['no_addresses'] = 1
917-
if is_eth_like(currency):
918-
data['root_address'] = address_to_user_format(currency, address)
919-
else:
920-
data['root_address'] = address
902+
data['root_address'] = address
921903
return data
922904

923905
async def get_address_by_address_id(self, currency, address_id):
@@ -932,7 +914,7 @@ async def get_address_by_address_id(self, currency, address_id):
932914
return None
933915
raise NotFoundException(
934916
f'Address {address_id} has no external transactions')
935-
return address_to_user_format(currency, result["address"])
917+
return result["address"]
936918

937919
async def get_address(self, currency, address):
938920
try:
@@ -1105,7 +1087,9 @@ async def list_links(self,
11051087
async def list_matching_addresses(self, currency, expression, limit=10):
11061088
prefix_lengths = self.get_prefix_lengths(currency)
11071089
expression_orginal = expression
1108-
expression = cannonicalize_address(currency, expression, partial=True)
1090+
if currency == 'trx':
1091+
expression = partial_tron_to_partial_evm(expression)
1092+
11091093
if len(expression) < prefix_lengths['address']:
11101094
return []
11111095
norm = identity
@@ -1120,10 +1104,9 @@ async def list_matching_addresses(self, currency, expression, limit=10):
11201104
prefix = prefix.upper()
11211105
rows = []
11221106

1123-
async def collect(query, paging_state):
1124-
while paging_state and len(rows) < limit:
1125-
if paging_state is True:
1126-
paging_state = None
1107+
async def collect(query):
1108+
paging_state = None
1109+
while len(rows) < limit:
11271110
result = await self.execute_async(currency,
11281111
'transformed',
11291112
query, [prefix],
@@ -1137,18 +1120,20 @@ async def collect(query, paging_state):
11371120
expression_orginal)
11381121
])
11391122
paging_state = result.paging_state
1123+
if paging_state is None:
1124+
break
11401125

11411126
query = "SELECT address FROM address_ids_by_address_prefix "\
11421127
"WHERE address_prefix = %s"
11431128

1144-
await collect(query, True)
1129+
await collect(query)
11451130

11461131
if len(rows) < limit:
11471132
query = "SELECT address FROM new_addresses "\
11481133
"WHERE address_prefix = %s"
11491134
if self.parameters[currency]["use_delta_updater_v1"]:
11501135
try:
1151-
await collect(query, True)
1136+
await collect(query)
11521137
except InvalidRequest as e:
11531138
if 'new_addresses' not in str(e):
11541139
raise e
@@ -1538,11 +1523,17 @@ async def list_matching_txs(self, currency, expression, limit):
15381523

15391524
return rows[0:limit]
15401525

1541-
@eth
15421526
def scrub_prefix(self, currency, expression):
1527+
if isinstance(expression, bytes):
1528+
expression = bytes_to_hex(expression)
1529+
1530+
if currency == 'eth':
1531+
expression = strip_0x(expression)
1532+
15431533
if currency not in self.parameters:
15441534
raise NotFoundException(f'{currency} not found')
1545-
bech32_prefix = self.parameters[currency]['bech_32_prefix']
1535+
1536+
bech32_prefix = self.parameters[currency].get('bech_32_prefix', '')
15461537
return expression[len(bech32_prefix):] \
15471538
if expression.startswith(bech32_prefix) \
15481539
else expression
@@ -1614,7 +1605,7 @@ async def finish_entity(self, currency, row, with_txs=True):
16141605
return await self.finish_address(currency, row, with_txs)
16151606

16161607
async def finish_entity_eth(self, currency, row, with_txs=True):
1617-
row['root_address'] = address_to_user_format(currency, row['address'])
1608+
row['root_address'] = row['address']
16181609
return await self.finish_address(currency, row, with_txs)
16191610

16201611
async def finish_addresses(self, currency, rows, with_txs=True):
@@ -1661,8 +1652,6 @@ async def finish_address(self, currency, row, with_txs=True):
16611652
return row
16621653

16631654
async def finish_address_eth(self, currency, row, with_txs=True):
1664-
if 'address' in row:
1665-
row['address'] = address_to_user_format(currency, row['address'])
16661655
row['cluster_id'] = row['address_id']
16671656
row['total_received'] = \
16681657
self.markup_currency(currency, row['total_received'])
@@ -1720,7 +1709,7 @@ async def is_address_dirty(self, currency, address):
17201709
if not prefix:
17211710
return None
17221711
if is_eth_like(currency):
1723-
address = evm_address_from_hex(currency, address)
1712+
#address = evm_address_from_hex(currency, address)
17241713
prefix = prefix.upper()
17251714
prefix_length = self.get_prefix_lengths(currency)['address']
17261715
query = ("SELECT address FROM dirty_addresses "
@@ -1786,6 +1775,7 @@ async def add_balance_eth(self, currency, row):
17861775

17871776
def scrub_prefix_eth(self, currency, expression):
17881777
# remove 0x prefix
1778+
expression = bytes_to_hex(expression)
17891779
if expression.startswith("0x"):
17901780
return expression[2:]
17911781
else:
@@ -2064,10 +2054,8 @@ async def list_txs_by_node_type_eth(self,
20642054
token_tx = await self.fetch_token_transaction(
20652055
currency, full_tx, addr_tx["log_index"])
20662056

2067-
addr_tx['to_address'] = address_to_user_format(
2068-
currency, token_tx['to_address'])
2069-
addr_tx['from_address'] = address_to_user_format(
2070-
currency, token_tx['from_address'])
2057+
addr_tx['to_address'] = token_tx['to_address']
2058+
addr_tx['from_address'] = token_tx['from_address']
20712059
addr_tx['currency'] = token_tx["currency"]
20722060
addr_tx['token_tx_id'] = addr_tx["log_index"]
20732061
value = token_tx['value'] * \
@@ -2081,9 +2069,6 @@ async def list_txs_by_node_type_eth(self,
20812069
(-1 if addr_tx['is_outgoing'] else 1)
20822070

20832071
contract_creation = full_tx.get('contract_creation', None)
2084-
if contract_creation is not None:
2085-
contract_creation = address_to_user_format(
2086-
currency, contract_creation)
20872072

20882073
addr_tx['contract_creation'] = contract_creation
20892074
addr_tx['tx_hash'] = full_tx['tx_hash']
@@ -2145,20 +2130,16 @@ async def list_txs_by_hashes_eth(self,
21452130
result_with_tokens = []
21462131
for row in result:
21472132

2148-
row['from_address'] = address_to_user_format(
2149-
currency, row['from_address'])
21502133
to_address = row['to_address']
21512134
if to_address is None:
21522135
# this is a contract creation transaction
21532136
# set recipient to newly created contract
21542137
# and mark tx as creation
2155-
row['to_address'] = address_to_user_format(
2156-
currency, row['receipt_contract_address'])
2138+
row['to_address'] = row['receipt_contract_address']
21572139
row['contract_creation'] = True
21582140
else:
21592141
# normal transaction
2160-
row['to_address'] = address_to_user_format(
2161-
currency, to_address)
2142+
row['to_address'] = to_address
21622143
# result['contract_creation'] = False
21632144

21642145
result_with_tokens.append(row)
@@ -2189,15 +2170,12 @@ async def get_tx_by_hash_eth(self, currency, hash):
21892170
if to_address is None:
21902171
# this is a contract creation transaction
21912172
# set recipient to newly created contract and mark tx as creation
2192-
result['to_address'] = address_to_user_format(
2193-
currency, result['receipt_contract_address'])
2173+
result['to_address'] = result['receipt_contract_address']
21942174
result['contract_creation'] = True
21952175
else:
21962176
# normal transaction
2197-
result['to_address'] = address_to_user_format(currency, to_address)
2177+
result['to_address'] = to_address
21982178
# result['contract_creation'] = False
2199-
result['from_address'] = address_to_user_format(
2200-
currency, result['from_address'])
22012179
return result
22022180

22032181
async def list_links_eth(self,
@@ -2324,8 +2302,6 @@ async def list_links_eth(self,
23242302
tx_ids,
23252303
include_token_txs=True)
23262304

2327-
neighbor = address_to_user_format(currency, neighbor)
2328-
address = address_to_user_format(currency, address)
23292305
txs = [
23302306
tx for tx in all_txs
23312307
if tx["to_address"] == neighbor and tx["from_address"] == address

gsrest/service/addresses_service.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from gsrest.service.rates_service import get_rates
44
import gsrest.service.common_service as common
55
from gsrest.errors import NotFoundException
6-
from gsrest.util.address import cannonicalize_address
6+
from gsrest.util.address import cannonicalize_address, address_to_user_format
77
from openapi_server.models.neighbor_addresses import NeighborAddresses
88
from openapi_server.models.neighbor_address import NeighborAddress
99
import asyncio
@@ -18,7 +18,6 @@ async def list_tags_by_address(request,
1818
address,
1919
page=None,
2020
pagesize=None):
21-
address = cannonicalize_address(currency, address)
2221
return await common.list_tags_by_address(request,
2322
currency,
2423
address,
@@ -76,7 +75,7 @@ async def list_address_neighbors(request,
7675
return NeighborAddresses()
7776
aws = [
7877
get_address(request, currency,
79-
cannonicalize_address(currency, row[dst + '_address']))
78+
address_to_user_format(currency, row[dst + '_address']))
8079
for row in results
8180
]
8281

@@ -113,7 +112,6 @@ async def list_address_links(request,
113112

114113
async def try_get_delta_update_entity_dummy(request, currency, address,
115114
notfound):
116-
address = cannonicalize_address(currency, address)
117115
db = request.app['db']
118116
try:
119117
aws = [get_rates(request, currency), db.new_entity(currency, address)]
@@ -127,22 +125,22 @@ async def try_get_delta_update_entity_dummy(request, currency, address,
127125

128126

129127
async def get_address_entity(request, currency, address):
130-
address = cannonicalize_address(currency, address)
128+
address_canonical = cannonicalize_address(currency, address)
131129
db = request.app['db']
132130

133131
notfound = NotFoundException(
134132
'Entity for address {} not found'.format(address))
135133
try:
136-
entity_id = await db.get_address_entity_id(currency, address)
134+
entity_id = await db.get_address_entity_id(currency, address_canonical)
137135
except NotFoundException as e:
138136
if 'not found' not in str(e):
139137
raise e
140138
return await try_get_delta_update_entity_dummy(request, currency,
141-
address, notfound)
139+
address_canonical, notfound)
142140

143141
if entity_id is None:
144142
return await try_get_delta_update_entity_dummy(request, currency,
145-
address, notfound)
143+
address_canonical, notfound)
146144

147145
result = await get_entity(request,
148146
currency,

0 commit comments

Comments
 (0)