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
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
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 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