Skip to content

Commit

Permalink
Add support for multi-plan providers. Now run update script from plan…
Browse files Browse the repository at this point in the history
…-node-subscriptions. create every minute cron job for plan-node-subscriptions.py. Auto renew complete.
  • Loading branch information
freQniK committed Mar 25, 2024
1 parent 1c5b905 commit add507f
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 23 deletions.
84 changes: 62 additions & 22 deletions helpers/plan-node-subscriptions.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
#!/bin/env python3

'''
Run in a crontab:
* * * * * cmd
'''


import argparse
import scrtxxs
from urllib.parse import urlparse
from bip_utils import Bip39SeedGenerator, Bip44, Bip44Coins
from sentinel_protobuf.sentinel.subscription.v2.msg_pb2 import MsgCancelRequest, MsgCancelResponse

from sentinel_sdk.sdk import SDKInstance
from sentinel_sdk.types import NodeType, TxParams, Status
from sentinel_sdk.types import TxParams
from sentinel_sdk.utils import search_attribute
from pywgkey import WgKey
from mnemonic import Mnemonic
from keyrings.cryptfile.cryptfile import CryptFileKeyring
import ecdsa
import hashlib
import bech32
from os import path
from getpass import getpass
import sys

from os import path, getcwd
import pymysql
from datetime import datetime,timedelta
from subprocess import Popen
class PlanSubscribe():

def __init__(self, keyring_passphrase, wallet_name, seed_phrase = None):
Expand All @@ -38,6 +42,13 @@ def __init__(self, keyring_passphrase, wallet_name, seed_phrase = None):
else:
self.keyring = self.__keyring(keyring_passphrase)

self._db = pymysql.connect(host=scrtxxs.MySQLHost,
port=scrtxxs.MySQLPort,
user=scrtxxs.MySQLUsername,
passwd=scrtxxs.MySQLPassword,
db=scrtxxs.MySQLDB,
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)

def __keyring(self, keyring_passphrase: str):
kr = CryptFileKeyring()
Expand All @@ -46,6 +57,28 @@ def __keyring(self, keyring_passphrase: str):
kr.keyring_key = keyring_passphrase
return kr

def GetPlanNodes(self):

c = self._db.cursor()
q = "SELECT * FROM plan_node_subscriptions;"
c.execute(q)

return c.fetchall()

def ComputeResub(self, plan_nodes):
now = datetime.now()

resub_nodes = []
resub_plan_nodes = {}

now_plus_30 = now + timedelta(days=30)

for n in plan_nodes:
if n['inactive_date'] < now_plus_30:
resub_nodes.append(n['node_address'])
resub_plan_nodes[n['uuid']] = resub_nodes
return resub_plan_nodes

def subscribe_to_nodes_for_plan(self, nodeaddress, duration):

private_key = self.keyring.get_password("meile-plan", self.wallet_name)
Expand Down Expand Up @@ -86,9 +119,9 @@ def subscribe_to_nodes_for_plan(self, nodeaddress, duration):
if __name__ == "__main__":


parser = argparse.ArgumentParser(description="Meile Plan Subscriber - v0.1 - freQniK")
parser = argparse.ArgumentParser(description="Meile Plan Subscriber - v0.2 - freQniK")

parser.add_argument('--file', help="--file <nodefile>, absolute path of a list of sentnode... addresses separated by newline", metavar="file")
#parser.add_argument('--file', help="--file <nodefile>, absolute path of a list of sentnode... addresses separated by newline", metavar="file")
parser.add_argument('--seed', action='store_true',help='set if you are specifying a seedphrase', default=False)

args = parser.parse_args()
Expand All @@ -98,16 +131,23 @@ def subscribe_to_nodes_for_plan(self, nodeaddress, duration):
else:
ps = PlanSubscribe(scrtxxs.HotWalletPW, scrtxxs.WalletName, None)

if args.file:
with open(args.file, 'r') as nodefile:
nodes = nodefile.readlines()

else:
sys.exit(1)

for n in nodes:
print(f"Subscribing to {n} for {scrtxxs.HOURS} hour(s)...")
response = ps.subscribe_to_nodes_for_plan(n, scrtxxs.HOURS)
print(response)


resub_plan_nodes = ps.ComputeResub(ps.GetPlanNodes())
uuids = ''
for plan,nodes in resub_plan_nodes.items():
uuids = ','.join([uuids,plan])
for n in nodes:
print(f"Subscribing to {n} for {scrtxxs.HOURS} hour(s) on plan {plan}...")
#response = ps.subscribe_to_nodes_for_plan(n, scrtxxs.HOURS)
#print(response)

# Run db updater script with UUIDs
uuids = uuids.split(',')[1:]
print(uuids)
for uuid in uuids:
update_cmd = f"{scrtxxs.HELPERS}/update-node-scriptions.py --uuid {uuid}"

proc1 = Popen(update_cmd, shell=True)
proc1.wait(timeout=30)

proc_out,proc_err = proc1.communicate()
4 changes: 3 additions & 1 deletion helpers/update-node-scriptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def UpdateSubsExpiration(self, subs, uuid):


if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Meile Plan Subscriber - v0.1 - freQniK")
parser = argparse.ArgumentParser(description="Meile Plan Sub Updater - v0.1 - freQniK")
parser.add_argument('--uuid', help="--uuid <uuid> , plan uuid", metavar="uuid")

args = parser.parse_args()
Expand All @@ -106,7 +106,9 @@ def UpdateSubsExpiration(self, subs, uuid):
parser.print_help()
sys.exit(1)


uns = UpdateNodeScriptions()

nodes = uns.GetNodesOnPlan(args.uuid)
subs_on_plan = uns.qs_on_plan(nodes)

Expand Down

0 comments on commit add507f

Please sign in to comment.