forked from spool-labs/tape
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.rs
More file actions
306 lines (240 loc) · 7.94 KB
/
Copy pathcli.rs
File metadata and controls
306 lines (240 loc) · 7.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
use anyhow::Result;
use clap::{Parser, Subcommand};
use solana_client::nonblocking::rpc_client::RpcClient;
use solana_sdk::commitment_config::CommitmentConfig;
use solana_sdk::signature::Keypair;
use tape_network::store::TapeStore;
use std::env;
use std::str::FromStr;
use std::path::PathBuf;
use std::sync::Arc;
use crate::keypair::{get_keypair_path, get_payer};
#[derive(Parser)]
#[command(
name = "tapedrive",
about = "Your data, permanently recorded — uncensorable, uneditable, and here for good.",
arg_required_else_help = true,
version = env!("CARGO_PKG_VERSION")
)]
pub struct Cli {
#[command(subcommand)]
pub command: Commands,
#[arg(short = 'k', long = "keypair", global = true)]
pub keypair_path: Option<PathBuf>,
#[arg(
short = 'u',
long = "cluster",
default_value = "l",
global = true,
help = "Cluster to use: l (localnet), m (mainnet), d (devnet), t (testnet),\n or a custom RPC URL"
)]
pub cluster: Cluster,
#[arg(short = 'v', long = "verbose", help = "Print verbose output", global = true)]
pub verbose: bool,
}
#[derive(Subcommand)]
pub enum Commands {
// Tape Commands
Read {
#[arg(help = "Tape account to read")]
tape: String,
#[arg(short = 'o', long = "output", help = "Output file")]
output: Option<String>,
},
Write {
#[arg(
help = "File to write, message text, or remote URL",
required_unless_present_any = ["filename", "message", "remote"],
conflicts_with_all = ["message", "remote"]
)]
filename: Option<String>,
#[arg(short = 'm', long = "message", conflicts_with_all = ["filename", "remote"])]
message: Option<String>,
#[arg(short = 'r', long = "remote", conflicts_with_all = ["filename", "message"])]
remote: Option<String>,
#[arg(short = 'n', long = "tape-name", help = "Custom name for the tape (defaults to timestamp)")]
tape_name: Option<String>,
},
// Miner Commands
#[command(hide = true)]
Register {
#[arg(help = "The name of the miner you're registering")]
name: String,
},
Claim {
#[arg(help = "Miner account public key")]
miner: String,
#[arg(help = "Amount of tokens to claim")]
amount: u64,
},
// Node Commands
Archive {
#[arg(help = "Trusted peer to connect to", short = 'p', long = "peer")]
trusted_peer: Option<String>,
#[arg(help = "Miner account public key", short = 'm', long = "miner")]
miner_address: Option<String>,
},
Mine {
#[arg(help = "Miner account public key", conflicts_with = "name")]
pubkey: Option<String>,
#[arg(help = "Name of the miner you're mining with", conflicts_with = "pubkey", short = 'n', long = "name")]
name: Option<String>,
#[arg(help = "Trusted peer to connect to", short = 'p', long = "peer")]
trusted_peer: Option<String>,
#[arg(help = "Miner account public key", short = 'm', long = "miner")]
miner_address: Option<String>,
},
Web {
#[arg(help = "Port to run the web RPC service on")]
port: Option<u16>,
#[arg(help = "Trusted peer to connect to", short = 'p', long = "peer")]
trusted_peer: Option<String>,
#[arg(help = "Miner account public key", short = 'm', long = "miner")]
miner_address: Option<String>,
},
// Admin Commands
#[command(hide = true)]
Init {},
Airdrop {
#[arg(help = "Amount of tokens to airdrop")]
amount: u64,
},
// Store Management Commands
#[command(subcommand)]
Snapshot(SnapshotCommands),
// Info Commands
#[command(subcommand)]
Info(InfoCommands),
}
#[derive(Subcommand)]
pub enum SnapshotCommands {
Stats {},
Resync {
#[arg(help = "Tape account public key to re-sync")]
tape_address: String,
#[arg(help = "Miner account public key", short = 'm', long = "miner")]
miner_address: Option<String>,
},
Create {
#[arg(help = "Output path for the snapshot file (defaults to a timestamped file in current directory)")]
output: Option<String>,
},
Load {
#[arg(help = "Path to the snapshot file to load")]
input: String,
},
GetTape {
#[arg(help = "Tape account public key")]
tape_address: String,
#[arg(short = 'o', long = "output", help = "Output file")]
output: Option<String>,
#[arg(short = 'r', long = "raw", help = "Output raw segments instead of decoded tape")]
raw: bool,
#[arg(help = "Miner account public key", short = 'm', long = "miner")]
miner_address: Option<String>,
},
GetSegment {
#[arg(help = "Tape account public key")]
tape_address: String,
#[arg(help = "Segment index (0 to tape size - 1)")]
index: u32,
#[arg(help = "Miner account public key", short = 'm', long = "miner")]
miner_address: Option<String>,
},
}
#[derive(Subcommand)]
pub enum InfoCommands {
Tape {
#[arg(help = "Tape account public key")]
pubkey: String,
},
FindTape {
#[arg(help = "Tape number to find")]
number: u64,
},
Miner {
#[arg(help = "Miner account public key", conflicts_with = "name")]
pubkey: Option<String>,
#[arg(help = "Name of the miner you're mining with", conflicts_with = "pubkey", short = 'n', long = "name")]
name: Option<String>,
},
Archive {},
Epoch {},
Block {},
}
#[derive(Debug, Clone)]
pub enum Cluster {
Localnet,
Mainnet,
Devnet,
Testnet,
Custom(String),
}
impl Cluster {
pub fn rpc_url(&self) -> String {
match self {
Cluster::Localnet => "http://127.0.0.1:8899".to_string(),
Cluster::Mainnet => "https://api.mainnet-beta.solana.com".to_string(),
Cluster::Devnet => "https://api.devnet.solana.com".to_string(),
Cluster::Testnet => "https://api.testnet.solana.com".to_string(),
Cluster::Custom(url) => url.clone(),
}
}
}
impl FromStr for Cluster {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"l" => Ok(Cluster::Localnet),
"m" => Ok(Cluster::Mainnet),
"d" => Ok(Cluster::Devnet),
"t" => Ok(Cluster::Testnet),
s if s.starts_with("http://") || s.starts_with("https://") => Ok(Cluster::Custom(s.to_string())),
_ => Err(format!(
"Invalid cluster value: '{s}'. Use l, m, d, t, or a valid RPC URL (http:// or https://)"
)),
}
}
}
pub struct Context {
pub rpc: Arc<RpcClient>,
pub keypair_path: PathBuf,
pub payer: Keypair
}
impl Context{
pub fn try_build(cli:&Cli) -> Result<Self> {
let rpc_url = cli.cluster.rpc_url();
let rpc = Arc::new(
RpcClient::new_with_commitment(rpc_url.clone(),
CommitmentConfig::finalized())
);
let keypair_path = get_keypair_path(cli.keypair_path.clone());
let payer = get_payer(keypair_path.clone())?;
Ok(Self {
rpc,
keypair_path,
payer
})
}
pub fn keyapir_path(&self) -> &PathBuf{
&self.keypair_path
}
pub fn rpc(&self) -> &Arc<RpcClient>{
&self.rpc
}
pub fn open_primary_store_conn(&self) -> Result<TapeStore> {
Ok(tape_network::store::primary()?)
}
pub fn open_secondary_store_conn_mine(&self) -> Result<TapeStore> {
Ok(tape_network::store::secondary_mine()?)
}
pub fn open_secondary_store_conn_web(&self) -> Result<TapeStore> {
Ok(tape_network::store::secondary_web()?)
}
pub fn open_read_only_store_conn(&self) -> Result<TapeStore> {
Ok(tape_network::store::read_only()?)
}
pub fn payer(&self) -> &Keypair{
&self.payer
}
}