Ejemplo n.º 1
0
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
Ejemplo n.º 2
0
def get_db():
    rlock = threading.RLock()
    with rlock:
        if database.db is None:
            mysqlcontsraints = get_db_constraints()
            database.db = database_details(MYSQL_CONSTANTS=mysqlcontsraints)
            database.db.initialize()
    database.db.initializeSession()
    return database
Ejemplo n.º 3
0
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()
            DATABASE = database
    DATABASE.db.initializeSession()
    return DATABASE
Ejemplo n.º 4
0
print("## ")

db_password = getpass.getpass("MySQL Password: "******"Grin Pool"

# Connect to DB
try:
    connected = False
    while not connected:
        sql_check_read_only = text(
            """SHOW VARIABLES where variable_name="read_only";""")
        mysqlcontsraints = MysqlConstants(db_host, db_user, db_password,
                                          db_name)
        database.db = database_details(MYSQL_CONSTANTS=mysqlcontsraints)
        database.db.initialize()
        database.db.initializeSession()
        result = database.db.engine.execute(sql_check_read_only).first()
        if result[1] == "OFF":
            connected = True
        else:
            pass

except Exception as e:
    print(" ")
    print("Failed to connect to the database with:")
    print("  db_host: {}".format(db_host))
    print("  db_user: {}".format(db_user))
    print("  db_password: {}".format(db_password))
    print("  db_name: {}".format(db_name))
Ejemplo n.º 5
0
#!/usr/bin/python3

from grinbase.constants.MysqlConstants import MysqlConstants
from grinbase.dbaccess import database
from grinbase.dbaccess.database import database_details
from grinbase.model.pool_utxo import Pool_utxo

if __name__ == '__main__':
    database.db = database_details(MYSQL_CONSTANTS=MysqlConstants())
    database.db.initialize()

#    for i in range(0,10):
#        tmp = Pool_utxo(id=str(i), address=str(i), amount=1.5*i)
#        database.db.createDataObj(tmp)


    utxo = Pool_utxo.getPayable(0)[0]
    print(utxo)
    locked_utxo = Pool_utxo.get_locked_by_id(utxo.id)
    print(locked_utxo)
    locked_utxo.amount=1.0
    database.db.getSession().begin_nested();
    locked_utxo.amount=7.0
    database.db.getSession().commit()
    database.db.getSession().commit()

    utxo = Pool_utxo.getPayable(0)[0]
    print(utxo)


#    for utxo in Pool_utxo.getPayable(0):
Ejemplo n.º 6
0
def main():
    global LOGGER
    global CONFIG
    CONFIG = lib.get_config()
    LOGGER = lib.get_logger(PROCESS)
    LOGGER.warn("=== Starting {}".format(PROCESS))

    # DB connection details
    db_host = CONFIG["db"]["address"] + ":" + CONFIG["db"]["port"]
    db_user = CONFIG["db"]["user"]
    db_password = CONFIG["db"]["password"]
    db_name = CONFIG["db"]["db_name"]
    mysqlcontsraints = MysqlConstants(db_host, db_user, db_password, db_name)

    # Connect to DB
    database.db = database_details(MYSQL_CONSTANTS=mysqlcontsraints)
    database.db.initialize()

    wallet_dir = CONFIG[PROCESS]["wallet_dir"]
    minimum_payout = int(CONFIG[PROCESS]["minimum_payout"])
    os.chdir(wallet_dir)
    utxos = Pool_utxo.getPayable(minimum_payout)
    database.db.getSession().commit()
    # XXX TODO: Use the current balance, timestamp, the last_attempt timestamp, last_payout, and failed_attempts
    # XXX TODO: to filter and sort by order we want to make payment attempts
    for utxo in utxos:
        try:
            LOGGER.warn("Trying to pay: {} {} {}".format(
                utxo.id, utxo.address, utxo.amount))
            # Lock just this current record for update
            locked_utxo = Pool_utxo.get_locked_by_id(utxo.id)
            # Save and Zero the balance
            original_balance = locked_utxo.amount
            locked_utxo.amount = 0
            # Savepoint changes - if we crash after sending coins but before commit we roll back to here.
            #   The pool audit service finds lost payouts and restores user balance
            database.db.getSession().begin_nested()
            # Attempt to make the payment
            timestamp = "{:%B %d, %Y %H:%M:%S.%f}".format(datetime.now())
            status = makePayout(locked_utxo.address, original_balance)
            LOGGER.warn("Payout status: {}".format(status))
            if status == 0:
                LOGGER.warn("Made payout for {} {} {}".format(
                    locked_utxo.id, locked_utxo.address, original_balance))
                # Update timestamp of last payout, number of failed payout attempts
                locked_utxo.amount = 0
                locked_utxo.failure_count = 0
                locked_utxo.last_try = timestamp
                locked_utxo.last_success = timestamp
                # Commit changes
                database.db.getSession().commit()
            else:
                LOGGER.error("Failed to make payout: {} {} {}".format(
                    locked_utxo.id, locked_utxo.address, original_balance))
                # Restore the users balance
                locked_utxo.amount = original_balance
                # Update number of failed payout attempts
                if locked_utxo.failure_count is None:
                    locked_utxo.failure_count = 0
                locked_utxo.failure_count += 1
                locked_utxo.last_try = timestamp
                # Commit changes
                database.db.getSession().commit()
            database.db.getSession().commit()

        except Exception as e:
            LOGGER.error("Failed to process utxo: {} because {}".format(
                utxo.id, str(e)))
            database.db.getSession().rollback()
            sys.exit(1)

    LOGGER.warn("=== Completed {}".format(PROCESS))