node_set-conns.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #!/usr/bin/env python3
  2. # This file is part of DarkFi (https://dark.fi)
  3. #
  4. # Copyright (C) 2020-2025 Dyne.org foundation
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU Affero General Public License as
  8. # published by the Free Software Foundation, either version 3 of the
  9. # License, or (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU Affero General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU Affero General Public License
  17. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. import asyncio
  19. import sys
  20. from node_get_info import JsonRpc
  21. async def main(argv):
  22. if len(argv) != 2:
  23. print(f"Usage: {argv[0]} <num_connections>", file=sys.stderr)
  24. sys.exit(1)
  25. try:
  26. num_conns = int(argv[1])
  27. if num_conns <= 0:
  28. raise ValueError()
  29. except ValueError:
  30. print("Error: num_connections must be a positive integer", file=sys.stderr)
  31. sys.exit(1)
  32. rpc = JsonRpc()
  33. while True:
  34. try:
  35. await rpc.start("localhost", 26660)
  36. break
  37. except OSError:
  38. pass
  39. response = await rpc.set_outbound_connections(num_conns)
  40. if "error" in response:
  41. print(f"Error: {response['error']}")
  42. await rpc.stop()
  43. sys.exit(1)
  44. print(f"Set outbound connections to {num_conns}: {response['result']}")
  45. await rpc.stop()
  46. asyncio.run(main(sys.argv))