def get_db(): global DATABASE rlock = threading.RLock() with rlock: if DATABASE is None: print("INIT DB") mysqlcontsraints = get_db_constraints() database.db = database_details(MYSQL_CONSTANTS=mysqlcontsraints) database.db.initialize() try: GRIN_POOL_ADMIN_USER = os.environ['GRIN_POOL_ADMIN_USER'] GRIN_POOL_ADMIN_PASSWORD = os.environ[ 'GRIN_POOL_ADMIN_PASSWORD'] from grinbase.model.users import Users try: user = Users.get_by_id(1) if user is None: user = Users( id=1, username=GRIN_POOL_ADMIN_USER, password=GRIN_POOL_ADMIN_PASSWORD, ) database.db.createDataObj(user) except: pass except KeyError: pass DATABASE = database DATABASE.db.initializeSession() return DATABASE
def init_pool_users(CONFIG, LOGGER, database): ## # WebUI LOGGER.warn("Validate WebUI user account") try: webui_user = os.environ["GRIN_POOL_WEBUI_USER"] webui_pass = os.environ["GRIN_POOL_WEBUI_PASSWORD"] except KeyError: LOGGER.warn("We dont have WebUI account info, skipping...") return # Verify / create the account user = Users.get_by_id(1) if user is None: user = User( id=1, username=webui_user, password=webui_pass, ) database.db.getSession().add(user) database.db.getSession().commit() ## # Admin try: admin_user = os.environ["GRIN_POOL_ADMIN_USER"] admin_pass = os.environ["GRIN_POOL_ADMIN_PASSWORD"] except KeyError: LOGGER.warn("We dont have Admin account info, skipping...") return # Verify / create the account user = Users.get_by_id(2) if user is None: user = User( id=2, username=admin_user, password=admin_pass, ) database.db.getSession().add(user) database.db.getSession().commit() ## # Grin Developers Fund try: devfund_user = "******" devfund_pass = "******" except KeyError: LOGGER.warn("We dont have Admin account info, skipping...") return # Verify / create the account user = Users.get_by_id(3) if user is None: user = User( id=3, username=admin_user, password=admin_pass, ) database.db.getSession().add(user) database.db.getSession().commit()
def verify_password(username_or_token, password=None): global database #database = lib.get_db() LOGGER = lib.get_logger(PROCESS) print("Will Verify User: {}, {}", username_or_token, password) # First try to verify via token user_rec = Users.verify_auth_token(app.config['SECRET_KEY'], username_or_token) if user_rec is None: # try to authenticate with username/password user_rec = Users.get(username_or_token, password) if user_rec is None: return False g.user = user_rec return True
def post(self): global database LOGGER = lib.get_logger(PROCESS) username = None password = None try: debug and print("json request = {}".format(request.form)) username = request.form.get('username') password = request.form.get('password') debug and LOGGER.warn("PoolAPI_users POST: user:{} password:{}".format(username, password)) except AttributeError as e: LOGGER.warn("Missing username or password - {}".format(str(e))) if username is None or password is None: response = jsonify({ 'message': 'Missing arguments: username and pasword required' }) response.status_code = 400 return response if username == "" or password == "": response = jsonify({ 'message': 'Missing arguments: username and pasword required' }) response.status_code = 400 return response if "." in username: response = jsonify({ 'message': 'Invalid Username: May not contain "."' }) response.status_code = 400 return response # Check if the username is taken exists = Users.check_username_exists(username) if exists: debug and print("Failed to add - conflict with existing user = {}".format(username)) response = jsonify({ 'message': 'Conflict with existing account' }) response.status_code = 409 return response # Create the users record user_rec = Users.create(username, password) if user_rec is None: debug and print("Failed to add - unable to create a new user record") response = jsonify({ 'message': 'System Error: Failed to create account' }) response.status_code = 500 return response # initialize a worker_stats record for this user (previous block) so they get instance feedback on the UI lb = Blocks.get_latest() if lb is not None: height = Blocks.get_latest().height initial_stat = Worker_stats(datetime.utcnow(), height, user_rec.id) database.db.createDataObj(initial_stat) debug and print("Added user = {}".format(user_rec)) response = jsonify({ 'username': user_rec.username, 'id': user_rec.id }) response.status_code = 201 return response
def verify_password(username_or_token, password=None): global database #database = lib.get_db() LOGGER = lib.get_logger(PROCESS) debug and LOGGER.warn("Will Verify User: {}, {}".format(username_or_token, password)) # First try to verify via token user_rec = Users.verify_auth_token(app.config['SECRET_KEY'], username_or_token) if user_rec is None: # try to authenticate with username/password user_rec = Users.get(username_or_token, password) if user_rec is None: return False g.user = user_rec # Cache username<->user_id in redis for our stratum server redis_key = redis_userid_key + user_rec.username r.set(redis_key, user_rec.id) return True
def main(): CONFIG = lib.get_config() LOGGER = lib.get_logger(PROCESS) LOGGER.warn("=== Starting {}".format(PROCESS)) ## # Update user records in MySQL and REDIS database = lib.get_db() database.db.initializeSession() redisdb = lib.get_redis_db() redis_userid_key = "userid." id = 1 try: while True: thisuser = Users.get_by_id(id) if thisuser is None: if id > 2358: LOGGER.warn("last id = {}".format(id)) break id = id + 1 continue if thisuser.username == "bjg62hj8byyksphuw95vqc3f74.lionm1": orig_username = thisuser.username thisuser.username = "******" LOGGER.warn("Updated: {} to {}".format(orig_username, thisuser.username)) if thisuser.username == "[email protected]_d1": orig_username = thisuser.username thisuser.username = "******" LOGGER.warn("Updated: {} to {}".format(orig_username, thisuser.username)) if thisuser.username == "*****@*****.**": orig_username = thisuser.username thisuser.username = "******" LOGGER.warn("Updated: {} to {}".format(orig_username, thisuser.username)) if "." in thisuser.username: orig_username = thisuser.username # Update mysql thisuser.username = thisuser.username.replace(".", "_") # Update redis redis_key = redis_userid_key + orig_username COMMIT and redisdb.delete(redis_key) redis_key = redis_userid_key + thisuser.username COMMIT and redisdb.set(redis_key, id) LOGGER.warn("Updated: {} to {}".format(orig_username, thisuser.username)) id = id + 1 except Exception as e: # AssertionError as e: LOGGER.error("Something went wrong: {} - {}".format( e, traceback.print_stack())) COMMIT or LOGGER.warn("XXX No Commit - Edit for final run") COMMIT and database.db.getSession().commit() LOGGER.warn("=== Completed {}".format(PROCESS))
def get(self, username): try: userid = Users.get_id_by_username(username) if userid is None: response = jsonify({ 'id': None }) response.status_code = 404 return response response = jsonify({ 'id': userid }) response.status_code = 200 return response except Exception as e: LOGGER = lib.get_logger(PROCESS) LOGGER.error("Failed to lookup user: {} because: {}".format(username, repr(e))) response = jsonify({ 'message': 'Failed to lookup user: {}'.format(username) }) response.status_code = 500 return response
if new_state == "1": new_state = "canceled" elif new_state == "2": new_state = "expired" else: print("Invalid state choice") sys.exit(1) # Get the payout record payout_record = Pool_payment.get_by_id(payout_id) print("{}".format(payout_record)) amount = payout_record.amount / float(NANOGRIN) # Get the user id user_id = payout_record.user_id user_record = Users.get_by_id(user_id) username = user_record.username print("User: {}".format(username)) # Get the users UTXO record user_utxo = Pool_utxo.get_by_userid(user_id) # Print a report print("Will update account for {} on {}".format(username, poolname)) print( "Will cancel payout {} and add {} to users current balance of {} for a new balance of {}" .format(payout_id, amount, float(user_utxo.amount) / NANOGRIN, float(user_utxo.amount) / NANOGRIN + amount)) # Confirm action
def main(): CONFIG = lib.get_config() LOGGER = lib.get_logger(PROCESS) LOGGER.warn("=== Starting {}".format(PROCESS)) # Get DB Config db_address = CONFIG["db"]["address"] db_port = CONFIG["db"]["port"] db_user = CONFIG["db"]["user"] db_password = os.environ['MYSQL_ROOT_PASSWORD'] db_name = CONFIG["db"]["db_name"] LOGGER.warn("Got Config: db_address: {}, db_port: {}, db_user: {}, db_password: {}, db_name: {}".format(db_address, db_port, db_user, db_password, db_name)) # Create the DB if it does not already exist mysql_engine_string = "mysql+pymysql://{user}:{password}@{host}:{port}".format( user = db_user, password = db_password, host = db_address, port = db_port) sys.stdout.flush() # Create db if needed tmp_engine = create_engine(mysql_engine_string) conn = tmp_engine.connect() conn.execute("commit") conn.execute("CREATE DATABASE IF NOT EXISTS {};".format(db_name)) conn.close() LOGGER.warn("Created db: {}".format(db_name)) # Connect to the DB mysql_string = "mysql+pymysql://{user}:{passwd}@{host}:{port}/{db_name}".format( user = db_user, passwd = db_password, host = db_address, db_name = db_name, port = db_port) engine = create_engine( mysql_string, echo=False, pool_recycle=3600, isolation_level="READ_COMMITTED", max_overflow=125, pool_size=32 ) LOGGER.warn("Get Engine") # Create Special Users database = lib.get_db() ## # Admin LOGGER.warn("Validate Admin user account") try: admin_user = os.environ["GRIN_POOL_ADMIN_USER"] admin_pass = os.environ["GRIN_POOL_ADMIN_PASSWORD"] except KeyError: LOGGER.error("We dont have Admin account info, failed...") sys.exit(1) # Create the account user = Users.get_by_id(1) if user is not None: database.db.deleteDataObj(user) user = Users( id = 1, username = admin_user, password = admin_pass, ) database.db.createDataObj(user)
print(" db_password: {}".format(db_password)) print(" db_name: {}".format(db_name)) print(" ") print(" Error: {}".format(e)) sys.exit(1) username = input("Account username: "******"Ammount to credit: ")) debit_pool = "x" while debit_pool != "n" and debit_pool != "y": debit_pool = input("Debit the pools account? (y/n): ") if debit_pool != "n" and debit_pool != "y": print("Invalid input") # Get the user id user_id = Users.get_id_by_username(username) if user_id == 0: print("Could not find user: {} in the database".format(username)) sys.exit(1) #user_record = Users.get_by_id(user_id) #print(user_record) # Get the users UTXO record user_utxo = Pool_utxo.get_by_userid(user_id) # Get the pools UTXO if needed if debit_pool == "y": pooladmin_utxo = Pool_utxo.get_by_userid(1) # Print a report print("Will update account for {} on {}".format(username, poolname)) print("Will add {} to users current balance of {} for a new balance of {}".
def calculate_block_payout_map(height, window_size, pool_fee, logger, estimate=False): block_payout_map = {} # Get the grinpool admin user ID for pool fee pool_admin_user_id = 1 # Total the payments for sanity check total_payments_this_block = 0 try: admin_user = os.environ["GRIN_POOL_ADMIN_USER"] pool_admin_user_id = Users.get_id_by_username(admin_user) logger.warn("Pool Fee goes to admin account with id={}".format( pool_admin_user_id)) except Exception as e: logger.warn( "We dont have Admin account info, using default id={}: {}".format( pool_admin_user_id, e)) # Create the payout map # Get pool_block record and check block state #print("getting the pool block: {}".format(height)) #sys.stdout.flush() if estimate == False: poolblock = Pool_blocks.get_by_height(height) if poolblock is None or poolblock.state != "unlocked": return {} #print("The pool block {}".format(poolblock.to_json())) #sys.stdout.flush() # Get total value of this block: reward + tx fees reward = get_reward_by_block(height) print("Reward for block {} = {}".format(height, reward)) sys.stdout.flush() # The Pools Fee the_pools_fee = reward * pool_fee block_payout_map[pool_admin_user_id] = the_pools_fee reward = reward - the_pools_fee logger.warn("Pool Fee = {}".format(block_payout_map)) # Get the "secondary_scaling" value for this block scale = get_scale_by_block(height) #print("Secondary Scaling value for block = {}".format(scale)) #sys.stdout.flush() # build a map of total shares of each size for each user shares_count_map = get_share_counts(height, window_size) # DUMMY DATA # scale = 529 # shares_count_map = { # 1: {29: 50}, # 2: {29: 25, 31: 10}, # 3: {32: 5}, # } #print("Shares Count Map:") #sys.stdout.flush() #pp = pprint.PrettyPrinter(indent=4) #pp.pprint(shares_count_map) #sys.stdout.flush() # Calcualte total value of all shares total_value = calculate_total_share_value(shares_count_map, scale) print("total share value in payment window: {}".format(total_value)) sys.stdout.flush() # For each user with shares in the window, calculate payout and add to block_payout_map for user_id, worker_shares_count in shares_count_map.items(): #print("xxx: {} {}".format(user_id, worker_shares_count)) #sys.stdout.flush() # Calcualte the total share value from this worker total_worker_value = calculate_total_share_value( {user_id: worker_shares_count}, scale) if total_value * reward > 0: worker_payment = int(total_worker_value / total_value * reward) else: # For next block estimate, there may be no shares submitted to the pool worker_payment = 0 total_payments_this_block += worker_payment print("worker_payment: {}".format(worker_payment / 1000000000)) sys.stdout.flush() if user_id in block_payout_map.keys(): block_payout_map[user_id] += worker_payment else: block_payout_map[user_id] = worker_payment logger.warn( "Total Grin Paid Out this block: {} + the_pools_fee: {} ".format( total_payments_this_block, the_pools_fee)) print("block_payout_map = {}".format(block_payout_map)) #sys.stdout.flush() #logger.warn("calculate_map: {}".format(block_payout_map)) return block_payout_map
import os import sys import json import time import redis from datetime import datetime import requests from grinbase.model.users import Users from grinlib import lib from grinlib import grin from grinlib import pool redis_userid_key = "userid." r = redis.Redis( host='redis-master', port=6379) database = lib.get_db() for id in range(1, 9999): user_rec = Users.get_by_id(id) redis_key = redis_userid_key + user_rec.username r.set(redis_key, user_rec.id)
#!/usr/bin/python # Given a username and plaintext password, # authenticate against the db user records import sys from grinbase.model.users import Users from grinlib import lib # User to authenticate username = sys.argv[1] password = sys.argv[2] # Initialize a db connection database = lib.get_db() # Lookup / authenticate theUserRecord = Users.get(username, password) # Print the results if theUserRecord is None: print("Failed to find or autenticate user: {}".format(username)) else: print("Success, {} has id {}".format(username, theUserRecord.id))
def calculate_block_payout_map(height, window_size, pool_fee, logger, estimate=False): block_payout_map = {} # Get the grinpool admin user ID for pool fee pool_admin_user_id = 1 # Total the payments for sanity check total_payments_this_block = 0 try: admin_user = os.environ["GRIN_POOL_ADMIN_USER"] pool_admin_user_id = Users.get_id_by_username(admin_user) logger.warn("Pool Fee goes to admin account with id={}".format(pool_admin_user_id)) except Exception as e: logger.warn("We dont have Admin account info, using default id={}: {}".format(pool_admin_user_id, e)) # Create the payout map try: if estimate == True: cached_map = get_block_payout_map_estimate(height, logger) if cached_map is not None: return cached_map # Get pool_block record and check block state print("getting the pool block: {}".format(height)) sys.stdout.flush() poolblock = Pool_blocks.get_by_height(height) if poolblock is None: return {} print("The pool block {}".format(poolblock.to_json())) sys.stdout.flush() if estimate == True: if poolblock.state != "new" and poolblock.state != "unlocked": return {} else: if poolblock.state != "unlocked": return {} # Get total value of this block: reward + tx fees reward = get_reward_by_block(height) print("Reward for block {} = {}".format(height, reward)) sys.stdout.flush() # The Pools Fee the_pools_fee = reward * pool_fee block_payout_map[pool_admin_user_id] = the_pools_fee reward = reward - the_pools_fee logger.warn("Pool Fee = {}".format(block_payout_map)) # Get the "secondary_scaling" value for this block scale = get_scale_by_block(height) print("Secondary Scaling value for block = {}".format(scale)) sys.stdout.flush() # build a map of total shares of each size for each user shares_count_map = get_share_counts(height, window_size) # DUMMY DATA # scale = 529 # shares_count_map = { # 1: {29: 50}, # 2: {29: 25, 31: 10}, # 3: {32: 5}, # } #print("Shares Count Map:") #sys.stdout.flush() #pp = pprint.PrettyPrinter(indent=4) #pp.pprint(shares_count_map) #sys.stdout.flush() # Calcualte total value of all shares total_value = calculate_total_share_value(shares_count_map, scale) print("total share value in payment window: {}".format(total_value)) sys.stdout.flush() # For each user with shares in the window, calculate payout and add to block_payout_map for user_id, worker_shares_count in shares_count_map.items(): print("xxx: {} {}".format(user_id, worker_shares_count)) sys.stdout.flush() # Calcualte the total share value from this worker total_worker_value = calculate_total_share_value({user_id:worker_shares_count}, scale) worker_payment = total_worker_value / total_value * reward total_payments_this_block += worker_payment print("worker_payment: {}".format(worker_payment/1000000000)) sys.stdout.flush() if user_id in block_payout_map.keys(): block_payout_map[user_id] += worker_payment else: block_payout_map[user_id] = worker_payment logger.warn("Total Grin Paid Out this block: {} + the_pools_fee: {} ".format(total_payments_this_block, the_pools_fee)) print("block_payout_map = {}".format(block_payout_map)) #sys.stdout.flush() if estimate == True: payout_estimate_map_key = "payout-estimate-for-block-" + str(height) try: # Estimates are cached in redis, save it there if we can redisdb = lib.get_redis_db() #redisdb.hmset(payout_estimate_map_key, json.dumps(block_payout_map)) redisdb.set(payout_estimate_map_key, pickle.dumps(block_payout_map)) except Exception as e: logger.warn("block_payout_map cache insert failed: {} - {}".format(payout_estimate_map_key, repr(e))) except Exception as e: logger.error("Estimate went wrong: {} - {}".format(e, traceback.print_exc(e))) raise e #logger.warn("calculate_map: {}".format(block_payout_map)) return block_payout_map