1
1
import os
2
2
import yaml
3
3
import logging
4
- from pathlib import Path
5
4
from typing import Optional , Set , List , Tuple , Dict
6
5
7
- import asyncio
8
6
import aiomysql
9
7
import pymysql
10
8
from blspy import G1Element
@@ -94,7 +92,7 @@ def _row_to_farmer_record(row) -> FarmerRecord:
94
92
)
95
93
96
94
async def add_farmer_record (self , farmer_record : FarmerRecord , metadata : RequestMetadata ):
97
- with ( await self .pool ) as connection :
95
+ with await self .pool as connection :
98
96
cursor = await connection .cursor ()
99
97
await cursor .execute (
100
98
f"INSERT INTO farmer VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) "
@@ -127,7 +125,7 @@ async def add_farmer_record(self, farmer_record: FarmerRecord, metadata: Request
127
125
128
126
async def get_farmer_record (self , launcher_id : bytes32 ) -> Optional [FarmerRecord ]:
129
127
# TODO(pool): use cache
130
- with ( await self .pool ) as connection :
128
+ with await self .pool as connection :
131
129
cursor = await connection .cursor ()
132
130
await cursor .execute (
133
131
f"SELECT * FROM farmer WHERE launcher_id=%s" ,
@@ -139,7 +137,7 @@ async def get_farmer_record(self, launcher_id: bytes32) -> Optional[FarmerRecord
139
137
return self ._row_to_farmer_record (row )
140
138
141
139
async def update_difficulty (self , launcher_id : bytes32 , difficulty : uint64 ):
142
- with ( await self .pool ) as connection :
140
+ with await self .pool as connection :
143
141
connection = await self .pool .acquire ()
144
142
cursor = await connection .cursor ()
145
143
await cursor .execute (
@@ -155,7 +153,7 @@ async def update_singleton(
155
153
is_pool_member : bool ,
156
154
):
157
155
entry = (bytes (singleton_tip ), bytes (singleton_tip_state ), int (is_pool_member ), launcher_id .hex ())
158
- with ( await self .pool ) as connection :
156
+ with await self .pool as connection :
159
157
cursor = await connection .cursor ()
160
158
await cursor .execute (
161
159
f"UPDATE farmer SET singleton_tip=%s, singleton_tip_state=%s, is_pool_member=%s WHERE launcher_id=%s" ,
@@ -164,7 +162,7 @@ async def update_singleton(
164
162
await connection .commit ()
165
163
166
164
async def get_pay_to_singleton_phs (self ) -> Set [bytes32 ]:
167
- with ( await self .pool ) as connection :
165
+ with await self .pool as connection :
168
166
cursor = await connection .cursor ()
169
167
await cursor .execute ("SELECT p2_singleton_puzzle_hash from farmer" )
170
168
rows = await cursor .fetchall ()
@@ -179,7 +177,7 @@ async def get_farmer_records_for_p2_singleton_phs(self, puzzle_hashes: Set[bytes
179
177
if len (puzzle_hashes ) == 0 :
180
178
return []
181
179
puzzle_hashes_db = tuple ([ph .hex () for ph in list (puzzle_hashes )])
182
- with ( await self .pool ) as connection :
180
+ with await self .pool as connection :
183
181
cursor = await connection .cursor ()
184
182
await cursor .execute (
185
183
f'SELECT * from farmer WHERE p2_singleton_puzzle_hash in ({ "%s," * (len (puzzle_hashes_db ) - 1 )} %s) ' ,
@@ -190,7 +188,7 @@ async def get_farmer_records_for_p2_singleton_phs(self, puzzle_hashes: Set[bytes
190
188
return [self ._row_to_farmer_record (row ) for row in rows ]
191
189
192
190
async def get_farmer_points_and_payout_instructions (self ) -> List [Tuple [uint64 , bytes ]]:
193
- with ( await self .pool ) as connection :
191
+ with await self .pool as connection :
194
192
cursor = await connection .cursor ()
195
193
await cursor .execute (f"SELECT points, payout_instructions FROM farmer" )
196
194
rows = await cursor .fetchall ()
@@ -210,21 +208,21 @@ async def get_farmer_points_and_payout_instructions(self) -> List[Tuple[uint64,
210
208
return ret
211
209
212
210
async def clear_farmer_points (self ) -> None :
213
- with ( await self .pool ) as connection :
211
+ with await self .pool as connection :
214
212
cursor = await connection .cursor ()
215
213
await cursor .execute (f"UPDATE farmer SET points=0" )
216
214
await cursor .close ()
217
215
await connection .commit ()
218
216
219
217
async def add_partial (self , launcher_id : bytes32 , timestamp : uint64 , difficulty : uint64 ):
220
- with ( await self .pool ) as connection :
218
+ with await self .pool as connection :
221
219
cursor = await connection .cursor ()
222
220
await cursor .execute (
223
221
"INSERT INTO partial VALUES(%s, %s, %s)" ,
224
222
(launcher_id .hex (), timestamp , difficulty ),
225
223
)
226
224
await connection .commit ()
227
- with ( await self .pool ) as connection :
225
+ with await self .pool as connection :
228
226
cursor = await connection .cursor ()
229
227
await cursor .execute (
230
228
f"UPDATE farmer SET points=points+%s WHERE launcher_id=%s" , (difficulty , launcher_id .hex ())
@@ -233,7 +231,7 @@ async def add_partial(self, launcher_id: bytes32, timestamp: uint64, difficulty:
233
231
await cursor .close ()
234
232
235
233
async def get_recent_partials (self , launcher_id : bytes32 , count : int ) -> List [Tuple [uint64 , uint64 ]]:
236
- with ( await self .pool ) as connection :
234
+ with await self .pool as connection :
237
235
cursor = await connection .cursor ()
238
236
await cursor .execute (
239
237
"SELECT timestamp, difficulty from partial WHERE launcher_id=%s ORDER BY timestamp DESC LIMIT %s" ,
0 commit comments