Skip to content

Commit f89613b

Browse files
authored
Merge pull request #166 from atomicals/develop
Fix CBOR encode error on deep copy and various fixes
2 parents 05c6822 + 97b85d3 commit f89613b

File tree

9 files changed

+293
-463
lines changed

9 files changed

+293
-463
lines changed

.github/workflows/tests.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ jobs:
2121
with:
2222
python-version: '3.10'
2323
cache: 'pip'
24+
- name: Setup Python caches
25+
uses: actions/cache@v2
26+
with:
27+
path: ${{ env.pythonLocation }}
28+
key: ${{ env.pythonLocation }}-${{ hashFiles('setup.py','requirements.txt','requirements-test.txt') }}
2429
- name: Install dependencies
2530
run: |
2631
sudo apt-get update

electrumx/lib/util_atomicals.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
import pickle
4040
import math
4141
from electrumx.lib.hash import sha256, double_sha256
42-
from cbor2 import dumps, loads, CBORDecodeError
42+
from cbor2 import dumps, loads, CBORDecodeError, CBORTag
4343
from collections.abc import Mapping
4444
from functools import reduce
4545
from merkletools import MerkleTools
@@ -1282,27 +1282,32 @@ def encode_tx_hash_hex(state):
12821282
cloned_state[encode_tx_hash_hex(key)] = encode_tx_hash_hex(value)
12831283
return cloned_state
12841284

1285-
# Auto detect any bytes data and encoded it
1285+
1286+
# Auto encodes data into structured bytes data.
12861287
def auto_encode_bytes_elements(state):
12871288
if isinstance(state, bytes):
12881289
return {
12891290
'$b': state.hex(),
12901291
'$len': sys.getsizeof(state),
12911292
'$auto': True
12921293
}
1293-
if not isinstance(state, dict) and not isinstance(state, list):
1294-
return state
1295-
1294+
1295+
if isinstance(state, CBORTag):
1296+
dumped_bytes = dumps(state)
1297+
return auto_encode_bytes_elements(dumped_bytes)
1298+
12961299
if isinstance(state, list):
12971300
reformatted_list = []
12981301
for item in state:
12991302
reformatted_list.append(auto_encode_bytes_elements(item))
1300-
return reformatted_list
1303+
return reformatted_list
1304+
1305+
if isinstance(state, dict):
1306+
for key, value in state.items():
1307+
state[key] = auto_encode_bytes_elements(value)
1308+
1309+
return state
13011310

1302-
for key, value in state.items():
1303-
state[key] = auto_encode_bytes_elements(value)
1304-
return state
1305-
13061311

13071312
# Base atomical commit to reveal delay allowed
13081313
def is_within_acceptable_blocks_for_general_reveal(commit_height, reveal_location_height):

electrumx/server/block_processor.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1617,9 +1617,10 @@ def put_or_delete_init_state_updates(self, mint_info, data_payload, Delete):
16171617
height = mint_info['reveal_location_height']
16181618

16191619
# Make a deep copy of the data payload and remove the reserved sections
1620-
copied_data_state = copy.deepcopy(data_payload)
1621-
# Remove any of the reserved sections
1622-
copied_data_state.pop('args', None)
1620+
copied_data_state = {}
1621+
for k, v in data_payload.items():
1622+
if k != 'args':
1623+
copied_data_state[k] = v
16231624
init_payload_bytes = dumps(copied_data_state)
16241625
op_struct = {
16251626
'op': 'mod',
@@ -2566,7 +2567,11 @@ def populate_dmitem_subtype_specific_fields(self, atomical):
25662567
f'parent container not found atomical_id={atomical_id}, '
25672568
f'parent_container={parent_container}',
25682569
)
2569-
atomical['$parent_container_name'] = parent_container['$container']
2570+
# The parent container name may not be populated if it's still in the mempool,
2571+
# or it's not settled realm request yet. Therefore, check to make sure it exists
2572+
# before we can populate this dmitem's container name.
2573+
if parent_container.get('$container'):
2574+
atomical['$parent_container_name'] = parent_container['$container']
25702575
if status == 'verified' and candidate_id == atomical['atomical_id']:
25712576
atomical['subtype'] = 'dmitem'
25722577
atomical['$dmitem'] = request_dmitem

electrumx/server/controller.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ async def serve(self, shutdown_event):
9797
Daemon = env.coin.DAEMON
9898
BlockProcessor = env.coin.BLOCK_PROCESSOR
9999

100-
async with Daemon(env.coin, env.daemon_url) as daemon:
100+
async with Daemon(env.coin, env.daemon_url, proxy_url=env.daemon_proxy_url) as daemon:
101101
db = DB(env)
102102
bp = BlockProcessor(env, db, daemon, notifications)
103103

electrumx/server/daemon.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,20 +54,24 @@ def __init__(
5454
max_workqueue=10,
5555
init_retry=0.25,
5656
max_retry=4.0,
57+
proxy_url=None
5758
):
5859
self.coin = coin
5960
self.logger = class_logger(__name__, self.__class__.__name__)
6061
self.url_index = None
6162
self.urls = []
6263
self.set_url(url)
64+
self.proxy_url: str | None = proxy_url
65+
if proxy_url:
66+
self.logger.info(f'Using proxy {proxy_url} for daemon.')
6367
# Limit concurrent RPC calls to this number.
6468
# See DEFAULT_HTTP_WORKQUEUE in bitcoind, which is typically 16
6569
self.workqueue_semaphore = asyncio.Semaphore(value=max_workqueue)
6670
self.init_retry = init_retry
6771
self.max_retry = max_retry
6872
self._height = None
6973
self.available_rpcs = {}
70-
self.session = None
74+
self.session: aiohttp.ClientSession | None = None
7175

7276
self._networkinfo_cache = (None, 0)
7377
self._networkinfo_lock = asyncio.Lock()
@@ -117,7 +121,7 @@ def failover(self):
117121
async def _send_data(self, data):
118122
async with self.workqueue_semaphore:
119123
if self.session:
120-
async with self.session.post(self.current_url(), data=data) as resp:
124+
async with self.session.post(self.current_url(), data=data, proxy=self.proxy_url) as resp:
121125
kind = resp.headers.get('Content-Type', None)
122126
if kind == 'application/json':
123127
return await resp.json(loads=json_deserialize)

electrumx/server/env.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ def __init__(self, coin=None):
4545

4646
self.db_dir = self.required('DB_DIRECTORY')
4747
self.daemon_url = self.required('DAEMON_URL')
48+
self.daemon_proxy_url = self.default('DAEMON_PROXY_URL', None)
4849
if coin is not None:
4950
assert issubclass(coin, Coin)
5051
self.coin = coin

0 commit comments

Comments
 (0)