diff --git a/pool/payment/payment.py b/pool/payment/payment.py index 502356e5..433cb720 100644 --- a/pool/payment/payment.py +++ b/pool/payment/payment.py @@ -21,10 +21,11 @@ from chia.wallet.transaction_record import TransactionRecord from pool.store.abstract import AbstractPoolStore -from pool.store.pg_store import PGStore +from pool.store.sqlite_store import SqlitePoolStore from ..pay_record import PaymentRecord + class Payment: def __init__(self, config: Dict, constants: ConsensusConstants, pool_store: Optional[AbstractPoolStore] = None): self.log = logging @@ -41,7 +42,7 @@ def __init__(self, config: Dict, constants: ConsensusConstants, pool_store: Opti self.config = config self.constants = constants - self.store: AbstractPoolStore = pool_store or PGStore() + self.store: AbstractPoolStore = pool_store or SqlitePoolStore() self.pool_fee = pool_config["pool_fee"] @@ -190,11 +191,13 @@ async def create_payment_loop(self): self.log.info(f"Paying out {mojo_per_point} mojo / point") additions_sub_list: List[Dict] = [ - {"puzzle_hash": self.pool_fee_puzzle_hash, "amount": pool_coin_amount, "launcher_id": self.default_target_puzzle_hash, "points": 0} + {"puzzle_hash": self.pool_fee_puzzle_hash, "amount": pool_coin_amount, + "launcher_id": self.default_target_puzzle_hash, "points": 0} ] for points, ph, launcher in points_and_ph: if points > 0: - additions_sub_list.append({"puzzle_hash": ph, "amount": points * mojo_per_point, "launcher_id": launcher, "points": points}) + additions_sub_list.append( + {"puzzle_hash": ph, "amount": points * mojo_per_point, "launcher_id": launcher, "points": points}) if len(additions_sub_list) == self.max_additions_per_transaction: await self.pending_payments.put(additions_sub_list.copy()) @@ -211,7 +214,6 @@ async def create_payment_loop(self): # Subtract the points from each farmer await self.store.clear_farmer_points() - else: self.log.info(f"No points for any farmer. Waiting {self.payment_interval}") diff --git a/pool/pool.py b/pool/pool.py index 0bdc35b6..967e49f5 100644 --- a/pool/pool.py +++ b/pool/pool.py @@ -49,7 +49,7 @@ from .store.sqlite_store import SqlitePoolStore from .record import FarmerRecord from .util import error_dict, RequestMetadata -from pool.store.pg_store import PGStore + class Pool: def __init__( @@ -77,7 +77,7 @@ def __init__( self.config = config self.constants = constants - self.store: AbstractPoolStore = pool_store or PGStore() + self.store: AbstractPoolStore = pool_store or SqlitePoolStore() self.pool_fee = pool_config["pool_fee"] diff --git a/pool/reward/reward_collector.py b/pool/reward/reward_collector.py index 70afa22d..38c28cf2 100644 --- a/pool/reward/reward_collector.py +++ b/pool/reward/reward_collector.py @@ -23,10 +23,12 @@ from pool.singleton import create_absorb_transaction, get_singleton_state, get_coin_spend from pool.store.abstract import AbstractPoolStore -from pool.store.pg_store import PGStore +from pool.store.sqlite_store import SqlitePoolStore + from ..reward_record import RewardRecord + class RewardCollector: def __init__(self, config: Dict, constants: ConsensusConstants, pool_store: Optional[AbstractPoolStore] = None): self.log = logging @@ -45,7 +47,7 @@ def __init__(self, config: Dict, constants: ConsensusConstants, pool_store: Opti self.config = config self.constants = constants - self.store: AbstractPoolStore = pool_store or PGStore() + self.store: AbstractPoolStore = pool_store or SqlitePoolStore() # This is the wallet fingerprint and ID for the wallet spending the funds from `self.default_target_puzzle_hash` self.wallet_fingerprint = pool_config["wallet_fingerprint"] diff --git a/pool/snapshot/snapshot.py b/pool/snapshot/snapshot.py index a594dd13..b0d86509 100644 --- a/pool/snapshot/snapshot.py +++ b/pool/snapshot/snapshot.py @@ -11,7 +11,8 @@ from chia.util.chia_logging import initialize_logging from pool.store.abstract import AbstractPoolStore -from pool.store.pg_store import PGStore +from pool.store.sqlite_store import SqlitePoolStore + class Snapshot: def __init__(self, config: Dict, constants: ConsensusConstants, pool_store: Optional[AbstractPoolStore] = None): @@ -29,7 +30,7 @@ def __init__(self, config: Dict, constants: ConsensusConstants, pool_store: Opti self.config = config self.constants = constants - self.store: AbstractPoolStore = pool_store or PGStore() + self.store: AbstractPoolStore = pool_store or SqlitePoolStore() # Interval for taking snapshot of farmer's points self.snapshot_interval = pool_config["snapshot_interval"] diff --git a/pool/store/pg_store.py b/pool/store/pg_store.py index 772aa9cb..d2ae2b4e 100644 --- a/pool/store/pg_store.py +++ b/pool/store/pg_store.py @@ -20,6 +20,7 @@ class PGStore(AbstractPoolStore): """ Pool store based on SQLite. """ + def __init__(self): super().__init__() self.connection: Optional[asyncpg.Pool] = None @@ -75,11 +76,12 @@ async def connect(self): "launcher_id text, /* farmer */" "points bigint, /* farmer's points */" "delay_time bigint, /* delayed time */" - "timestamp bigint, /* snapshot timestamp */ - "ss_type smallint /* 0: normal snapshot, 1: clear snapshot, 2: pool total */ - )" + "timestamp bigint, /* snapshot timestamp */" + "ss_type smallint /* 0: normal snapshot, 1: clear snapshot, 2: pool total */" + ")" ) ) + await self.connection.execute("CREATE INDEX IF NOT EXISTS ss_launcher_id_index on maxi_points_ss(launcher_id)") # create rewards tx table @@ -95,7 +97,7 @@ async def connect(self): ) await self.connection.execute("CREATE INDEX IF NOT EXISTS re_launcher_id_index on maxi_rewards_tx(launcher_id)") - @staticmethod + @ staticmethod def _row_to_farmer_record(row) -> FarmerRecord: return FarmerRecord( bytes.fromhex(row[0]), @@ -133,7 +135,7 @@ async def add_farmer_record(self, farmer_record: FarmerRecord, metadata: Request ) async def get_farmer_record(self, launcher_id: bytes32) -> Optional[FarmerRecord]: - row = await self.connection.fetchrow( + row=await self.connection.fetchrow( "SELECT * from maxi_farmer where launcher_id=$1", launcher_id.hex(), ) @@ -154,9 +156,9 @@ async def update_singleton( is_pool_member: bool, ): if is_pool_member: - entry = (bytes(singleton_tip), bytes(singleton_tip_state), 1, launcher_id.hex()) + entry=(bytes(singleton_tip), bytes(singleton_tip_state), 1, launcher_id.hex()) else: - entry = (bytes(singleton_tip), bytes(singleton_tip_state), 0, launcher_id.hex()) + entry=(bytes(singleton_tip), bytes(singleton_tip_state), 0, launcher_id.hex()) await self.connection.execute( f"UPDATE maxi_farmer SET singleton_tip=$1, singleton_tip_state=$2, is_pool_member=$3 WHERE launcher_id=$4", *entry, diff --git a/pool/store/sqlite_store.py b/pool/store/sqlite_store.py index 02782484..5c3706df 100644 --- a/pool/store/sqlite_store.py +++ b/pool/store/sqlite_store.py @@ -14,6 +14,7 @@ from ..pay_record import PaymentRecord from ..reward_record import RewardRecord + class SqlitePoolStore(AbstractPoolStore): """ Pool store based on SQLite. @@ -77,7 +78,7 @@ async def connect(self): "points bigint," "delay_time bigint," "timestamp bigint," - "ss_type smallint)" + "ss_type smallint /* 0: normal snapshot, 1: clear snapshot, 2: pool total */)" ) ) await self.connection.execute("CREATE INDEX IF NOT EXISTS ss_launcher_id_index on points_ss(launcher_id)") @@ -97,7 +98,6 @@ async def connect(self): await self.connection.commit() - @staticmethod def _row_to_farmer_record(row) -> FarmerRecord: return FarmerRecord( @@ -218,14 +218,13 @@ async def get_farmer_points_and_payout_instructions(self) -> List[Tuple[uint64, async def snapshot_farmer_points(self, ss_type: int) -> None: cursor = await self.connection.execute( f"INSERT into points_ss(launcher_id, points, timestamp, delay_time, ss_type)" - "SELECT launcher_id, points, strftime('%s', 'now'), delay_time, ? from farmer" - "WHERE points != 0", - ss_type + "SELECT launcher_id, points, strftime('%s', 'now'), delay_time, ? from farmer WHERE points != 0", + (ss_type,) ) await cursor.close() await self.connection.commit() - async def snapshot_farmer_points(self) -> None: + async def snapshot_pool_points(self) -> None: cursor = await self.connection.execute( ( "INSERT into points_ss(launcher_id, points, timestamp, delay_time, ss_type)" @@ -276,14 +275,14 @@ async def add_payment(self, payment: PaymentRecord): cursor = await self.connection.execute( f"INSERT into payment (launcher_id, amount, payment_type, timestamp, points, txid, note) VALUES(?, ?, ?, ?, ?, ?, ?)", ( - payment.launcher_id.hex(), - payment.payment_amount, - payment.payment_type, - payment.timestamp, - payment.points, - payment.txid, - payment.note - ), + payment.launcher_id.hex(), + payment.payment_amount, + payment.payment_type, + payment.timestamp, + payment.points, + payment.txid, + payment.note + ), ) await cursor.close() await self.connection.commit()