Skip to content

Commit

Permalink
Add add nodes to plan when subscribing from node file.
Browse files Browse the repository at this point in the history
  • Loading branch information
freQniK committed Mar 29, 2024
1 parent 66d2480 commit ac72288
Showing 1 changed file with 58 additions and 11 deletions.
69 changes: 58 additions & 11 deletions helpers/plan-node-subscriptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ def __init__(self, keyring_passphrase, wallet_name, seed_phrase = None):
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)

private_key = self.keyring.get_password("meile-plan", self.wallet_name)

grpcaddr, grpcport = urlparse(scrtxxs.GRPC).netloc.split(":")

self.sdk = SDKInstance(grpcaddr, int(grpcport), secret=private_key)


def __keyring(self, keyring_passphrase: str):
kr = CryptFileKeyring()
kr.filename = "keyring.cfg"
Expand All @@ -68,6 +75,14 @@ def GetPlanNodes(self):

return c.fetchall()

def GetPlanID(self, uuid):
c = self._db.cursor()
q = f"SELECT plan_id FROM meile_plans WHERE uuid = '{uuid}';"

c.execute(q)

return c.fetchone()

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

Expand All @@ -84,20 +99,14 @@ def ComputeResub(self, plan_nodes):

def subscribe_to_nodes_for_plan(self, nodeaddress, duration):

private_key = self.keyring.get_password("meile-plan", self.wallet_name)

grpcaddr, grpcport = urlparse(scrtxxs.GRPC).netloc.split(":")

sdk = SDKInstance(grpcaddr, int(grpcport), secret=private_key)

tx_params = TxParams(
# denom="udvpn", # TODO: from ConfParams
# fee_amount=20000, # TODO: from ConfParams
# gas=ConfParams.GAS,
gas_multiplier=1.15
)

tx = sdk.nodes.SubscribeToNode(
tx = self.sdk.nodes.SubscribeToNode(
node_address=nodeaddress,
gigabytes=0, # TODO: review this please
hours=int(duration), # TODO: review this please
Expand All @@ -109,7 +118,7 @@ def subscribe_to_nodes_for_plan(self, nodeaddress, duration):
return(False, tx["log"])

if tx.get("hash", None) is not None:
tx_response = sdk.nodes.wait_transaction(tx["hash"])
tx_response = self.sdk.nodes.wait_transaction(tx["hash"])
print(tx_response)
subscription_id = search_attribute(
tx_response, "sentinel.node.v2.EventCreateSubscription", "id"
Expand All @@ -118,6 +127,32 @@ def subscribe_to_nodes_for_plan(self, nodeaddress, duration):
return (True,subscription_id)

return(False, "Tx error")


def add_node_to_plan(self, plan_id, node):
tx_params = TxParams(
# denom="udvpn", # TODO: from ConfParams
# fee_amount=20000, # TODO: from ConfParams
# gas=ConfParams.GAS,
gas_multiplier=1.15
)


tx = self.sdk.plans.LinkNode(
plan_id=plan_id,
node_address=node
)

if tx.get("log", None) is not None:
return (False, tx["log"])


if tx.get("hash", None) is not None:
tx_response = self.sdk.nodes.wait_transaction(tx["hash"])
print(tx_response)
return (True, None)

return (False,"Tx error")

def run_update(uuid):
update_cmd = f"{scrtxxs.HELPERS}/update-node-scriptions.py --uuid {uuid}"
Expand Down Expand Up @@ -151,27 +186,39 @@ def run_insert(node_file, uuid):
ps = PlanSubscribe(scrtxxs.HotWalletPW, scrtxxs.WalletName, None)

if args.file and args.uuid:
plan_id = ps.GetPlanID(args.uuid)['plan_id']

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

for n in nodes:
print(f"Subscribing to {n} for {scrtxxs.HOURS} hour(s) on plan {args.uuid}...")
response = ps.subscribe_to_nodes_for_plan(n, scrtxxs.HOURS)
print(response)
print("Waiting 5s...")
sleep(5)
print(f"Adding {n} to plan {plan_id},{args.uuid}...")
ps.add_node_to_plan(plan_id, n)



print("Inserting nodes in plan DB...", end='')
run_insert(args.file, args.uuid)
sleep(2)
print("Done.")
print("Done.")


else:
resub_plan_nodes = ps.ComputeResub(ps.GetPlanNodes())
print(resub_plan_nodes)

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)
response = ps.subscribe_to_nodes_for_plan(n, scrtxxs.HOURS)
print(response)

# Run db updater script with UUIDs
uuids = uuids.split(',')[1:]
Expand Down

0 comments on commit ac72288

Please sign in to comment.