Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions pool/payment/payment.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"]

Expand Down Expand Up @@ -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())
Expand All @@ -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}")

Expand Down
4 changes: 2 additions & 2 deletions pool/pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__(
Expand Down Expand Up @@ -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"]

Expand Down
6 changes: 4 additions & 2 deletions pool/reward/reward_collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"]
Expand Down
5 changes: 3 additions & 2 deletions pool/snapshot/snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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"]
Expand Down
16 changes: 9 additions & 7 deletions pool/store/pg_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class PGStore(AbstractPoolStore):
"""
Pool store based on SQLite.
"""

def __init__(self):
super().__init__()
self.connection: Optional[asyncpg.Pool] = None
Expand Down Expand Up @@ -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
Expand All @@ -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]),
Expand Down Expand Up @@ -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(),
)
Expand All @@ -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,
Expand Down
27 changes: 13 additions & 14 deletions pool/store/sqlite_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from ..pay_record import PaymentRecord
from ..reward_record import RewardRecord


class SqlitePoolStore(AbstractPoolStore):
"""
Pool store based on SQLite.
Expand Down Expand Up @@ -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)")
Expand All @@ -97,7 +98,6 @@ async def connect(self):

await self.connection.commit()


@staticmethod
def _row_to_farmer_record(row) -> FarmerRecord:
return FarmerRecord(
Expand Down Expand Up @@ -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)"
Expand Down Expand Up @@ -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()
Expand Down