diff --git a/bcauto.py b/bcauto.py new file mode 100644 index 0000000..31074bd --- /dev/null +++ b/bcauto.py @@ -0,0 +1,105 @@ +To handle intermittent connection issues effectively, including errors like `[WinError 10053] An established connection was aborted by the software in your host machine`, you can introduce a robust retry mechanism in your script. This mechanism will attempt to reconnect to the RPC server periodically and resume the minting process only when the connection is successfully re-established. + +Here's an enhanced version of your script with a safeguard that continuously checks for the restoration of the RPC connection and the general availability of the host machine: + +```python +import subprocess +import time +import json +import re +import os +from bitcoinrpc.authproxy import AuthServiceProxy, JSONRPCException, JSONRPCConnectionError + +# Setup environment variables and RPC connection +rpc_user = os.getenv('RPC_USER', '') +rpc_password = os.getenv('RPC_PASSWORD', '') +rpc_host = os.getenv('RPC_HOST', 'localhost') +rpc_port = os.getenv('RPC_PORT', '22555') + +def get_rpc_connection(): + """Attempt to establish an RPC connection.""" + try: + return AuthServiceProxy(f"http://{rpc_user}:{rpc_password}@{rpc_host}:{rpc_port}/") + except Exception as e: + print(f"Failed to connect to RPC: {e}") + return None + +def check_connection(rpc_connection): + """Check if the RPC connection is alive by pinging the server.""" + try: + if rpc_connection is not None: + rpc_connection.getblockchaininfo() # Sample method to check connection + return True + except (JSONRPCConnectionError, Exception) as e: + print(f"Connection check failed: {e}") + return False + +def ensure_connection(): + """Ensure the RPC connection is available, with retries if necessary.""" + rpc_connection = get_rpc_connection() + while not check_connection(rpc_connection): + print("Waiting for connection to be restored...") + time.sleep(30) # Wait for 30 seconds before retrying + rpc_connection = get_rpc_connection() + return rpc_connection + +# Now incorporating connection checks in the minting process +def continuous_minting_process(directory, file_prefix, file_extension): + rpc_connection = ensure_connection() + airDropList_path = os.path.join(directory, 'AirDropList.json') + last_txid = read_last_output('AirDropOutput.json', rpc_connection) + details_list = extract_details(airDropList_path, rpc_connection) + start_index = next((i for i, detail in enumerate(details_list) if detail.get('txid') == last_txid), 0) + print(f"Resuming minting from index {start_index}.") + process_mint_batch(start_index, details_list, directory, file_prefix, file_extension, rpc_connection) + +# Including connection restoration in other required functions +def read_last_output(json_file_name, rpc_connection): + rpc_connection = ensure_connection() # Check and restore connection if needed + try: + if os.path.exists(json_file_name): + with open(json_file_name, 'r') as file: + data = json.load(file) + last_entry = max(data.values(), key=lambda x: x['txid']) if data else None + return last_entry['txid'] if last_entry else None + except json.JSONDecodeError as e: + print(f"JSON decode error in {json_file_name}: {e}") + except KeyError as e: + print(f"Missing expected key in JSON data: {e}") + return None + +def process_mint_batch(start_index, details_list, directory, file_prefix, file_extension, rpc_connection): + for detail in details_list[start_index:]: + rpc_connection = ensure_connection() # Ensure connection is alive before processing + file_number = str(detail['index']).zfill(5) + image_path = os.path.join(directory, f"{file_prefix}{file_number}.{file_extension}") + if not os.path.exists(image_path): + print(f"File not found: {image_path}") + continue + mint_command = f"node . mint {detail['dogecoin_address']} {image_path}" + print(f"Executing command: {mint_command}") + result_mint = http://subprocess.run(mint_command, shell=True, capture_output=True, text=True) + print("Output from mint command:", result_mint.stdout) + if result_mint.stderr: + print("Error in mint command:", result_mint.stderr) + txid_search = http://re.search("inscription txid: (\\w+)", result_mint.stdout) + if txid_search: + last_txid = txid_search.group(1) + "i0" + print(f"Successful mint, TXID: {last_txid}") + +# Initialize main variables and start the process +directory = r'E:\newminternode\dogecoin-ordinals-drc-20-inscription\DogPartyHTMLS' +file_prefix = 'dogparty' +file_extension = 'html' + +continuous_minting_process(directory, file_prefix, file_extension) + +``` + +### Key Enhancements: +1. **`get_rpc_connection` Function**: This function tries to establish an RPC connection and handles failures gracefully. +2. **`check_connection` Function**: It checks if the RPC connection is still alive by making a blockchain-related query. +3. **`ensure_connection` Function**: It ensures that the connection is active before proceeding, and retries every 30 seconds if the connection is down. +4. **Connection Check in Strategic Places**: Connection checks are integrated before significant actions, such as before reading from the JSON file and before processing each batch, to ensure that operations are paused when there is a connection issue, and resumed once the connection is restored. + +This robust mechanism will help mitigate disruptions caused by RPC or host connection issues, allowing your minting operations to proceed smoothly once the connection is re-established.