State improvements
Signed-off-by: Julien Riou <julien@riou.xyz>
This commit is contained in:
parent
607d48b524
commit
bd204d6b7a
2 changed files with 28 additions and 20 deletions
37
main.py
37
main.py
|
@ -7,7 +7,7 @@ from coingecko import get_rate
|
||||||
from config import read_config, validate_config
|
from config import read_config, validate_config
|
||||||
from flexpool import BlockNotFoundException, LastBlock, Miner
|
from flexpool import BlockNotFoundException, LastBlock, Miner
|
||||||
from requests.exceptions import HTTPError
|
from requests.exceptions import HTTPError
|
||||||
from state import read_state, write_state
|
from state import create_state, read_state, write_state
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
@ -35,7 +35,7 @@ def setup_logging(args):
|
||||||
logging.basicConfig(format=log_format, level=args.loglevel, filename=args.logfile)
|
logging.basicConfig(format=log_format, level=args.loglevel, filename=args.logfile)
|
||||||
|
|
||||||
|
|
||||||
def watch_block(state_file, config, disable_notifications, exchange_rate=None, currency=None):
|
def watch_block(config, disable_notifications, last_block=None, exchange_rate=None, currency=None):
|
||||||
logger.debug('fetching last mined block')
|
logger.debug('fetching last mined block')
|
||||||
try:
|
try:
|
||||||
block = LastBlock(exchange_rate=exchange_rate, currency=currency)
|
block = LastBlock(exchange_rate=exchange_rate, currency=currency)
|
||||||
|
@ -43,10 +43,7 @@ def watch_block(state_file, config, disable_notifications, exchange_rate=None, c
|
||||||
logger.warning('last block found')
|
logger.warning('last block found')
|
||||||
return
|
return
|
||||||
|
|
||||||
logger.debug('reading state file')
|
if block.number != last_block:
|
||||||
state = read_state(filename=state_file)
|
|
||||||
|
|
||||||
if block.number != state.get('block'):
|
|
||||||
logger.info(f'block {block.number} mined')
|
logger.info(f'block {block.number} mined')
|
||||||
logger.debug(block)
|
logger.debug(block)
|
||||||
|
|
||||||
|
@ -62,11 +59,10 @@ def watch_block(state_file, config, disable_notifications, exchange_rate=None, c
|
||||||
logger.error('failed to send notification to telegram')
|
logger.error('failed to send notification to telegram')
|
||||||
logger.debug(str(err))
|
logger.debug(str(err))
|
||||||
|
|
||||||
logger.debug('writing block to state file')
|
return block
|
||||||
write_state(filename=state_file, block_number=block.number)
|
|
||||||
|
|
||||||
|
|
||||||
def watch_miner(address, state_file, config, disable_notifications, exchange_rate=None, currency=None):
|
def watch_miner(address, config, disable_notifications, last_balance=None, exchange_rate=None, currency=None):
|
||||||
logger.debug(f'watching miner {address}')
|
logger.debug(f'watching miner {address}')
|
||||||
try:
|
try:
|
||||||
miner = Miner(address=address, exchange_rate=exchange_rate, currency=currency)
|
miner = Miner(address=address, exchange_rate=exchange_rate, currency=currency)
|
||||||
|
@ -77,11 +73,8 @@ def watch_miner(address, state_file, config, disable_notifications, exchange_rat
|
||||||
|
|
||||||
logger.debug(miner)
|
logger.debug(miner)
|
||||||
|
|
||||||
logger.debug('reading state file')
|
|
||||||
state = read_state(filename=state_file)
|
|
||||||
|
|
||||||
# watch balance
|
# watch balance
|
||||||
if miner.raw_balance != state.get('balance'):
|
if miner.raw_balance != last_balance:
|
||||||
logger.info(f'miner {address} balance has changed')
|
logger.info(f'miner {address} balance has changed')
|
||||||
if not disable_notifications and config.get('telegram'):
|
if not disable_notifications and config.get('telegram'):
|
||||||
logger.debug('sending balance notification to telegram')
|
logger.debug('sending balance notification to telegram')
|
||||||
|
@ -96,8 +89,7 @@ def watch_miner(address, state_file, config, disable_notifications, exchange_rat
|
||||||
logger.error('failed to send notification to telegram')
|
logger.error('failed to send notification to telegram')
|
||||||
logger.debug(str(err))
|
logger.debug(str(err))
|
||||||
|
|
||||||
logger.debug('writing balance to state file')
|
return miner
|
||||||
write_state(filename=state_file, miner_balance=miner.raw_balance)
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
@ -106,7 +98,10 @@ def main():
|
||||||
|
|
||||||
config = read_config(args.config)
|
config = read_config(args.config)
|
||||||
validate_config(config)
|
validate_config(config)
|
||||||
|
|
||||||
state_file = config.get('state_file', DEFAULT_STATE_FILE)
|
state_file = config.get('state_file', DEFAULT_STATE_FILE)
|
||||||
|
create_state(state_file)
|
||||||
|
state = read_state(state_file)
|
||||||
|
|
||||||
exchange_rate = None
|
exchange_rate = None
|
||||||
currency = config.get('currency', DEFAULT_CURRENCY)
|
currency = config.get('currency', DEFAULT_CURRENCY)
|
||||||
|
@ -118,8 +113,16 @@ def main():
|
||||||
logger.warning(f'failed to get ETH/{currency} rate')
|
logger.warning(f'failed to get ETH/{currency} rate')
|
||||||
logger.debug(str(err))
|
logger.debug(str(err))
|
||||||
|
|
||||||
watch_block(state_file, config, args.disable_notifications, exchange_rate, currency)
|
block = watch_block(last_block=state.get('block'), config=config, disable_notifications=args.disable_notifications,
|
||||||
watch_miner(config['miner'], state_file, config, args.disable_notifications, exchange_rate, currency)
|
exchange_rate=exchange_rate, currency=currency)
|
||||||
|
logger.debug('saving block number to state file')
|
||||||
|
write_state(state_file, block_number=block.number)
|
||||||
|
|
||||||
|
miner = watch_miner(last_balance=state.get('balance'), address=config['miner'], config=config,
|
||||||
|
disable_notifications=args.disable_notifications, exchange_rate=exchange_rate,
|
||||||
|
currency=currency)
|
||||||
|
logger.debug('saving miner balance to state file')
|
||||||
|
write_state(state_file, miner_balance=miner.raw_balance)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
11
state.py
11
state.py
|
@ -2,6 +2,11 @@ import json
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
|
||||||
|
def read_state(filename):
|
||||||
|
with open(filename, 'r') as fd:
|
||||||
|
return json.load(fd)
|
||||||
|
|
||||||
|
|
||||||
def write_state(filename, block_number=None, miner_balance=None):
|
def write_state(filename, block_number=None, miner_balance=None):
|
||||||
data = {}
|
data = {}
|
||||||
if os.path.isfile(filename):
|
if os.path.isfile(filename):
|
||||||
|
@ -17,6 +22,6 @@ def write_state(filename, block_number=None, miner_balance=None):
|
||||||
json.dump(data, fd)
|
json.dump(data, fd)
|
||||||
|
|
||||||
|
|
||||||
def read_state(filename):
|
def create_state(filename):
|
||||||
with open(filename, 'r') as fd:
|
if not os.path.isfile(filename):
|
||||||
return json.load(fd)
|
write_state(filename)
|
||||||
|
|
Reference in a new issue