main.rs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2025 Dyne.org foundation
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. use std::sync::Arc;
  19. use clap::{Parser, Subcommand};
  20. use darkfi::{cli_desc, Result};
  21. use prettytable::{format, row, Table};
  22. use smol::Executor;
  23. use rlnd_cli::RlndCli;
  24. #[derive(Parser)]
  25. #[command(about = cli_desc!())]
  26. struct Args {
  27. #[arg(short, long, default_value = "tcp://127.0.0.1:25637")]
  28. /// rldn JSON-RPC endpoint
  29. endpoint: String,
  30. #[command(subcommand)]
  31. /// Sub command to execute
  32. command: Subcmd,
  33. }
  34. #[derive(Subcommand)]
  35. enum Subcmd {
  36. /// Send a ping request to the rlnd RPC endpoint
  37. Ping,
  38. /// List all memberships
  39. List,
  40. /// Register a membership
  41. Register {
  42. /// Stake of this membership
  43. stake: u64,
  44. },
  45. /// Slash a membership
  46. Slash {
  47. /// Membership id to slash
  48. id: String,
  49. },
  50. }
  51. fn main() -> Result<()> {
  52. // Initialize an executor
  53. let executor = Arc::new(Executor::new());
  54. let ex = executor.clone();
  55. smol::block_on(executor.run(async {
  56. // Parse arguments
  57. let args = Args::parse();
  58. // Execute a subcommand
  59. let rlnd_cli = RlndCli::new(&args.endpoint, ex).await?;
  60. match args.command {
  61. Subcmd::Ping => {
  62. rlnd_cli.ping().await?;
  63. }
  64. Subcmd::List => {
  65. match rlnd_cli.get_all_memberships().await {
  66. Ok(memberships) => {
  67. // Create a prettytable with the memberships:
  68. let mut table = Table::new();
  69. table.set_format(*format::consts::FORMAT_NO_BORDER_LINE_SEPARATOR);
  70. table.set_titles(row!["ID", "Leaf Position", "Stake"]);
  71. for (id, membership) in memberships.iter() {
  72. table.add_row(row![
  73. id,
  74. format!("{:?}", membership.leaf_position),
  75. membership.stake
  76. ]);
  77. }
  78. if table.is_empty() {
  79. println!("No memberships found");
  80. } else {
  81. println!("{table}");
  82. }
  83. }
  84. Err(e) => println!("Membership registration failed: {e}"),
  85. }
  86. }
  87. Subcmd::Register { stake } => match rlnd_cli.register_membership(stake).await {
  88. Ok((id, membership)) => println!("Registered membership {id:?}: {membership:?}"),
  89. Err(e) => println!("Membership registration failed: {e}"),
  90. },
  91. Subcmd::Slash { id } => {
  92. println!("Slashing membership: {id}");
  93. match rlnd_cli.slash_membership(&id).await {
  94. Ok(membership) => println!("Slashed membership {id}: {membership:?}"),
  95. Err(e) => println!("Membership slashing failed: {e}"),
  96. }
  97. }
  98. }
  99. rlnd_cli.rpc_client.stop().await;
  100. Ok(())
  101. }))
  102. }