Skip to content

Commit

Permalink
Merge pull request #28 from jokpine/consolidate
Browse files Browse the repository at this point in the history
Consolidate server communication and error handling
  • Loading branch information
kanzure authored May 3, 2022
2 parents b997ff0 + 070f501 commit 4718ffa
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 34 deletions.
9 changes: 4 additions & 5 deletions miner.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
import datetime
import json
import base64
import requests
import time
from decimal import Decimal

from webcash.webcashbase import (
Expand All @@ -25,6 +23,7 @@
save_webcash_wallet,
create_webcash_wallet,
generate_new_secret,
webcash_server_request_raw,
)

from webcash.utils import lock_wallet
Expand All @@ -34,7 +33,7 @@
WALLET_FILENAME = "default_wallet.webcash"

def get_protocol_settings():
response = requests.get(WEBCASH_ENDPOINT_TARGET)
response = webcash_server_request_raw(WEBCASH_ENDPOINT_TARGET)
# difficulty_target_bits, ratio, mining_amount, mining_subsidy_amount
return response.json()

Expand Down Expand Up @@ -111,7 +110,7 @@ def mine():
keep = generate_new_secret(webcash_wallet, chain_code="MINING")
subsidy = generate_new_secret(webcash_wallet, chain_code="PAY")

response = requests.post(WEBCASH_ENDPOINT_MINING_REPORT, json=mining_report)
response = webcash_server_request_raw(WEBCASH_ENDPOINT_MINING_REPORT, mining_report)
print(f"submission response: {response.content}")
if response.status_code != 200:
# difficulty may have changed against us
Expand Down Expand Up @@ -153,7 +152,7 @@ def mine():
webcash_wallet["unconfirmed"].extend(unconfirmed_webcash)
save_webcash_wallet(webcash_wallet)
# Attempt replacement (should not fail!)
replace_response = requests.post(WEBCASH_ENDPOINT_REPLACE, json=replace_request)
replace_response = webcash_server_request_raw(WEBCASH_ENDPOINT_REPLACE, replace_request)
if replace_response.status_code != 200:
# might happen if difficulty changed against us during mining
# in which case we shouldn't get this far
Expand Down
50 changes: 21 additions & 29 deletions webcash/walletclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,20 @@ def setup():
webcash_wallet = load_webcash_wallet()
ask_user_for_legal_agreements(webcash_wallet)

def webcash_server_request_raw(url, json_data=None):
method = "post" if json_data is not None else "get"
response = requests.request(method=method, url=url, json=json_data)
return response

def webcash_server_request(url, json_data):
response = webcash_server_request_raw(url, json_data)
if response.status_code != 200:
raise Exception(f"Something went wrong on the server: {response.content}")
json_response = response.json()
if json_response.get("status", "") != "success":
raise Exception(f"Something went wrong on the server: {response}")
return json_response

def check_wallet():
webcash_wallet = load_webcash_wallet()

Expand All @@ -249,12 +263,7 @@ def check_wallet():

print(f"Checking batch of {len(batch)} public webcash")
health_check_request = [str(x) for x in batch.values()]
response = requests.post(WEBCASH_ENDPOINT_HEALTH_CHECK, json=health_check_request)
if response.status_code != 200:
raise Exception("Something went wrong on the server: ", response.content)
response = json.loads(response.content)
if response.get("status", "") != "success":
raise Exception("Something went wrong on the server: ", json.dumps(response))
response = webcash_server_request(WEBCASH_ENDPOINT_HEALTH_CHECK, health_check_request)

for webcash, result in response["results"].items():
if result["spent"] in (None, True):
Expand Down Expand Up @@ -336,12 +345,7 @@ def recover(gaplimit):
webcash.walletdepth = x

health_check_request = [str(swc.to_public()) for (pwc, swc) in check_webcashes.items()]
response = requests.post(WEBCASH_ENDPOINT_HEALTH_CHECK, json=health_check_request)
if response.status_code != 200:
raise Exception("Something went wrong on the server: ", response.content)
response = json.loads(response.content)
if response.get("status", "") != "success":
raise Exception("Something went wrong on the server: ", json.dumps(response))
response = webcash_server_request(WEBCASH_ENDPOINT_HEALTH_CHECK, health_check_request)

#idx = 0
for (public_webcash, result) in response["results"].items():
Expand Down Expand Up @@ -416,9 +420,7 @@ def insert(webcash, memo=""):
save_webcash_wallet(webcash_wallet)
#print("Sending to the server this replacement request: ", replace_request)

response = requests.post(WEBCASH_ENDPOINT_REPLACE, json=replace_request)
if response.status_code != 200:
raise click.ClickException(f"Something went wrong on the server: {response.content}")
webcash_server_request(WEBCASH_ENDPOINT_REPLACE, replace_request)

# save this one in the wallet
webcash_wallet["webcash"].append(str(new_webcash))
Expand Down Expand Up @@ -486,9 +488,7 @@ def insertmany(webcash):
webcash_wallet["unconfirmed"].extend(unconfirmed_webcashes)
save_webcash_wallet(webcash_wallet)

response = requests.post(WEBCASH_ENDPOINT_REPLACE, json=replace_request)
if response.status_code != 200:
raise Exception("Something went wrong on the server: ", response.content)
webcash_server_request(WEBCASH_ENDPOINT_REPLACE, replace_request)

webcash_wallet["webcash"].append(str(merged_webcash))

Expand Down Expand Up @@ -572,10 +572,7 @@ def pay(amount, memo=""):

# Attempt replacement
#print("Sending to the server this replacement request: ", replace_request)
response = requests.post(WEBCASH_ENDPOINT_REPLACE, json=replace_request)

if response.status_code != 200:
raise Exception("Something went wrong on the server: ", response.content)
webcash_server_request(WEBCASH_ENDPOINT_REPLACE, replace_request)

# remove old webcashes
for ec in use_this_webcash:
Expand Down Expand Up @@ -616,10 +613,7 @@ def pay(amount, memo=""):
#print("replace_request: ", replace_request)

#print("Sending to the server this replacement request: ", replace_request)
response = requests.post(WEBCASH_ENDPOINT_REPLACE, json=replace_request)

if response.status_code != 200:
raise Exception("Something went wrong on the server: ", response.content)
webcash_server_request(WEBCASH_ENDPOINT_REPLACE, replace_request)

# remove unconfirmed webcashes
for wc in unconfirmed_webcash:
Expand Down Expand Up @@ -694,9 +688,7 @@ def merge(group, max, memo):
save_webcash_wallet(webcash_wallet)

# Send replacement request to the server
response = requests.post(WEBCASH_ENDPOINT_REPLACE, json=replace_request)
if response.status_code != 200:
raise Exception("Something went wrong on the server: ", response.content)
webcash_server_request(WEBCASH_ENDPOINT_REPLACE, replace_request)

# remove old webcash
for wc in replace_request["webcashes"]:
Expand Down

0 comments on commit 4718ffa

Please sign in to comment.