1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-15 20:19:23 +00:00

coin_info: sort keys after deduplication

this ensures that key order is stable in cases where some ERC20 tokens
are sorted with identical keys which change later

i.e.: Two tokens with erc20:eth:REP keys are sorted based on the order
of reading them from disk. Previously, at most one such key would be
left in data so their mutual order wouldn't matter. Now, one of them can
be deprecated and get a ":deprecated" suffix. Depending on the load
order, this could be the first or the second of them, so the resulting
sort would not be stable.
To fix that, we do key deduplication first and sorting second.

To prevent further problems like this, we also sort glob results.
This commit is contained in:
matejcik 2018-12-03 16:38:49 +01:00
parent 22f5ba1dfe
commit 8906ebf92c

View File

@ -228,7 +228,7 @@ def _load_erc20_tokens():
chain = network["chain"]
chain_path = os.path.join(DEFS_DIR, "ethereum", "tokens", "tokens", chain)
for filename in glob.glob(os.path.join(chain_path, "*.json")):
for filename in sorted(glob.glob(os.path.join(chain_path, "*.json"))):
token = load_json(filename)
token.update(
chain=chain,
@ -513,6 +513,13 @@ def collect_coin_info():
misc=_load_misc(),
)
for k, coins in all_coins.items():
_ensure_mandatory_values(coins)
return all_coins
def sort_coin_infos(all_coins):
for k, coins in all_coins.items():
if k == "bitcoin":
coins.sort(key=_btc_sort_key)
@ -525,10 +532,6 @@ def collect_coin_info():
else:
coins.sort(key=lambda c: c["key"].upper())
_ensure_mandatory_values(coins)
return all_coins
def coin_info_with_duplicates():
"""Collects coin info, detects duplicates but does not remove them.
@ -539,6 +542,8 @@ def coin_info_with_duplicates():
buckets = mark_duplicate_shortcuts(all_coins.as_list())
deduplicate_erc20(buckets, all_coins.eth)
deduplicate_keys(all_coins.as_list())
sort_coin_infos(all_coins)
return all_coins, buckets