netget-tool 0.3.5

Network protocol stubbing toolkit: ~280 protocol servers and clients whose responses come from an LLM, a script, a static rule or a person.
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
//! Stratum V1 pool (server role). Rust owns each session — subscription, extranonce1, jobs,
//! the share arithmetic and every refusal the arithmetic decides — and asks the model only
//! who may mine and whether a share proven valid is credited.
pub mod actions;
pub mod wire;

use crate::llm::action_helper::call_llm;
use crate::llm::actions::protocol_trait::ActionResult;
use crate::logging::emit::Log;
use crate::protocol::{Event, SpawnContext};
use crate::server::accept_bounded::{accept_bounded, ConnectionLimiter, DEFAULT_MAX_CONNECTIONS};
use crate::server::connection::ConnectionId;
use crate::state::server::{ConnectionState, ConnectionStatus, ProtocolConnectionInfo};
use anyhow::{Context, Result};
use serde_json::{json, Value};
use std::collections::{HashSet, VecDeque};
use std::net::SocketAddr;
use std::sync::atomic::{AtomicU32, Ordering};
use std::sync::Arc;
use std::time::Duration;
use tokio::io::AsyncWriteExt;
use tokio::net::{TcpListener, TcpStream};
use wire::{error, Job, Message};

pub const IDLE_TIMEOUT: Duration = Duration::from_secs(900);
pub const MAX_IDLE_TIMEOUT_SECS: u64 = 86400;
/// Jobs a connection keeps; shares for older ones are stale.
pub const MAX_JOBS: usize = 16;
/// Shares remembered per connection to refuse duplicates.
pub const MAX_SEEN_SHARES: usize = 4096;
/// Workers one connection may authorize.
pub const MAX_WORKERS: usize = 32;
/// nbits of the pool's own jobs: the easiest target there is, as on regtest.
pub const POOL_NBITS: u32 = 0x207f_ffff;

/// What every connection of one pool shares.
struct Pool {
    next_extranonce1: AtomicU32,
    next_job: AtomicU32,
    difficulty: f64,
}

struct Session {
    extranonce1: [u8; wire::EXTRANONCE1_SIZE],
    subscribed: bool,
    user_agent: Option<String>,
    workers: HashSet<String>,
    difficulty: f64,
    jobs: VecDeque<Job>,
    seen: HashSet<(String, String, String, String)>,
    height: u32,
}

fn params(ctx: &SpawnContext) -> Result<(f64, Duration)> {
    let p = ctx.startup_params.as_ref();
    let difficulty = p
        .map(|p| p.get_optional_f64("difficulty"))
        .transpose()?
        .flatten()
        .unwrap_or(actions::DEFAULT_DIFFICULTY);
    anyhow::ensure!(
        difficulty > 0.0 && difficulty <= actions::MAX_DIFFICULTY,
        "difficulty must be above 0 and at most {}",
        actions::MAX_DIFFICULTY
    );
    let idle = p
        .map(|p| p.get_optional_u64("idle_timeout_secs"))
        .transpose()?
        .flatten()
        .unwrap_or(IDLE_TIMEOUT.as_secs());
    anyhow::ensure!(
        (1..=MAX_IDLE_TIMEOUT_SECS).contains(&idle),
        "idle_timeout_secs must be between 1 and {MAX_IDLE_TIMEOUT_SECS}"
    );
    Ok((difficulty, Duration::from_secs(idle)))
}

pub async fn spawn(ctx: SpawnContext) -> Result<SocketAddr> {
    let (difficulty, idle) = params(&ctx)?;
    let listener = TcpListener::bind(ctx.legacy_listen_addr()).await?;
    let addr = listener.local_addr()?;
    Log::new(Some(&ctx.status_tx)).info(format!(
        "Stratum pool listening on {addr}, difficulty {difficulty}"
    ));
    let pool = Arc::new(Pool {
        next_extranonce1: AtomicU32::new(rand::random()),
        next_job: AtomicU32::new(1),
        difficulty,
    });
    let state = ctx.state.clone();
    let server_id = ctx.server_id;
    let accept = tokio::spawn(async move {
        let limiter = ConnectionLimiter::new(DEFAULT_MAX_CONNECTIONS);
        loop {
            let (socket, peer, permit) =
                match accept_bounded(&listener, &limiter, b"", "Stratum", Some(&ctx.status_tx))
                    .await
                {
                    Ok(v) => v,
                    Err(_) => break,
                };
            let id = ConnectionId::new(ctx.state.get_next_unified_id().await);
            let now = crate::utils::clock::Instant::now();
            ctx.state
                .add_connection_to_server(
                    server_id,
                    ConnectionState {
                        id,
                        remote_addr: peer,
                        local_addr: addr,
                        bytes_sent: 0,
                        bytes_received: 0,
                        packets_sent: 0,
                        packets_received: 0,
                        last_activity: now,
                        status: ConnectionStatus::Active,
                        status_changed_at: now,
                        protocol_info: ProtocolConnectionInfo::empty(),
                    },
                )
                .await;
            let child = ctx.clone();
            let pool = pool.clone();
            ctx.state
                .spawn_server_task(server_id, async move {
                    let _permit = permit;
                    if let Err(e) = session(&child, &pool, id, socket, peer, idle).await {
                        Log::new(Some(&child.status_tx))
                            .warn(format!("Stratum connection {id} ended: {e}"));
                    }
                    child
                        .state
                        .update_connection_status(server_id, id, ConnectionStatus::Closed)
                        .await;
                    let _ = child.status_tx.send("__UPDATE_UI__".into());
                })
                .await;
        }
    });
    state.register_server_task(server_id, accept).await;
    Ok(addr)
}

fn outcome(ctx: &SpawnContext, id: ConnectionId, operation: &str, decision: &str) {
    let summary = format!("Stratum connection {id} operation={operation} decision={decision}");
    let log = Log::new(Some(&ctx.status_tx));
    if decision.starts_with("fail_closed_") || decision == "model_silent" {
        log.error(summary);
    } else {
        log.info(summary);
    }
}

fn reply(id: &Value, result: Value, err: Value) -> Value {
    json!({"id": id, "result": result, "error": err})
}

fn notify(method: &str, params: Value) -> Value {
    json!({"id": null, "method": method, "params": params})
}

fn now_secs() -> u32 {
    crate::utils::clock::SystemTime::now()
        .duration_since(crate::utils::clock::UNIX_EPOCH)
        .map(|d| d.as_secs() as u32)
        .unwrap_or(0)
}

impl Session {
    fn new_job(&mut self, pool: &Pool, message: Option<&str>, prev: [u8; 32], clean: bool) -> Job {
        self.height = self.height.wrapping_add(1).max(1);
        let job = wire::pool_job(
            format!("{:x}", pool.next_job.fetch_add(1, Ordering::Relaxed)),
            &prev,
            self.height,
            message.unwrap_or(actions::DEFAULT_COINBASE_MESSAGE),
            POOL_NBITS,
            now_secs(),
            clean,
        );
        if clean {
            self.jobs.clear();
            self.seen.clear();
        }
        if self.jobs.len() >= MAX_JOBS {
            self.jobs.pop_front();
        }
        self.jobs.push_back(job.clone());
        job
    }
}

/// The model's answer, split into its verdict and what accompanies it.
struct Answer {
    verdict: Option<Result<(), String>>,
    extras: Vec<Value>,
    invalid: bool,
}

fn split(results: Vec<ActionResult>) -> Answer {
    let mut verdicts = Vec::new();
    let mut extras = Vec::new();
    let mut stack = results;
    let mut ordered = Vec::new();
    while let Some(r) = stack.pop() {
        match r {
            ActionResult::Custom { data, .. } => ordered.push(data),
            ActionResult::Multiple(items) => stack.extend(items),
            _ => {}
        }
    }
    ordered.reverse();
    for a in ordered {
        match a["type"].as_str().unwrap_or_default() {
            actions::ACCEPT => verdicts.push(Ok(())),
            actions::REJECT => {
                verdicts.push(Err(a["message"].as_str().unwrap_or("rejected").to_string()))
            }
            _ => extras.push(a),
        }
    }
    Answer {
        invalid: verdicts.len() > 1,
        verdict: verdicts.into_iter().next(),
        extras,
    }
}

/// Lines for the work, difficulty and messages accompanying an answer.
fn apply_extras(
    session: &mut Session,
    pool: &Pool,
    extras: &[Value],
    send_work: bool,
    out: &mut Vec<Value>,
) {
    let mut sent_job = false;
    for a in extras {
        match a["type"].as_str().unwrap_or_default() {
            actions::SET_DIFFICULTY => {
                if let Some(d) = a["difficulty"].as_f64() {
                    session.difficulty = d;
                    out.push(notify("mining.set_difficulty", json!([d])));
                }
            }
            actions::NEW_JOB => {
                let prev = wire::parse_prev_display(a["prev_hash"].as_str()).unwrap_or([0u8; 32]);
                let job = session.new_job(
                    pool,
                    a["message"].as_str(),
                    prev,
                    a["clean_jobs"].as_bool().unwrap_or(true),
                );
                out.push(notify("mining.notify", job.notify_params()));
                sent_job = true;
            }
            actions::SHOW_MESSAGE => {
                out.push(notify("client.show_message", json!([a["message"]])));
            }
            _ => {}
        }
    }
    if send_work && !sent_job {
        let job = match session.jobs.back() {
            Some(j) => j.clone(),
            None => session.new_job(pool, None, [0u8; 32], true),
        };
        if !out.iter().any(|m| m["method"] == "mining.set_difficulty") {
            out.insert(
                0,
                notify("mining.set_difficulty", json!([session.difficulty])),
            );
        }
        out.push(notify("mining.notify", job.notify_params()));
    }
}

async fn ask(
    ctx: &SpawnContext,
    id: ConnectionId,
    event: Event,
    operation: &str,
) -> Result<Answer, anyhow::Error> {
    match call_llm(
        &ctx.llm_client,
        &ctx.state,
        ctx.server_id,
        Some(id),
        &event,
        &actions::StratumProtocol,
    )
    .await
    {
        Ok(r) if r.failures.is_empty() => {
            let answer = split(r.protocol_results);
            let decision = match (&answer.verdict, answer.invalid) {
                (_, true) => "fail_closed_invalid_reply",
                (Some(Ok(())), _) => "model_answer",
                (Some(Err(_)), _) => "model_reject",
                (None, _) => "model_silent",
            };
            outcome(ctx, id, operation, decision);
            Ok(answer)
        }
        Ok(_) => {
            outcome(ctx, id, operation, "fail_closed_invalid_reply");
            Err(anyhow::anyhow!("invalid reply"))
        }
        Err(e) => {
            outcome(ctx, id, operation, "fail_closed_llm_error");
            Err(e)
        }
    }
}

fn failure_text(e: &anyhow::Error) -> &'static str {
    crate::utils::wire_failure::wire_failure_text(e)
}

/// Lines answering one request.
#[allow(clippy::too_many_arguments)]
async fn answer(
    ctx: &SpawnContext,
    pool: &Pool,
    session: &mut Session,
    id: ConnectionId,
    peer: SocketAddr,
    rid: &Value,
    method: &str,
    params: &Value,
) -> Vec<Value> {
    let log = Log::new(Some(&ctx.status_tx));
    let p = params.as_array().cloned().unwrap_or_default();
    match method {
        "mining.subscribe" => {
            session.subscribed = true;
            session.user_agent = p
                .first()
                .and_then(Value::as_str)
                .map(|s| s.chars().take(128).collect());
            let sub = hex::encode(session.extranonce1);
            vec![reply(
                rid,
                json!([
                    [["mining.set_difficulty", sub], ["mining.notify", sub]],
                    hex::encode(session.extranonce1),
                    wire::EXTRANONCE2_SIZE
                ]),
                Value::Null,
            )]
        }
        "mining.configure" => vec![reply(rid, json!({}), Value::Null)],
        "mining.extranonce.subscribe" => vec![reply(rid, json!(false), Value::Null)],
        "mining.suggest_difficulty" => {
            log.debug(format!(
                "Stratum connection {id} suggested difficulty {:?} (ignored)",
                p.first()
            ));
            vec![reply(rid, json!(false), Value::Null)]
        }
        "mining.authorize" => {
            if !session.subscribed {
                return vec![reply(rid, json!(false), error(25, "Not subscribed"))];
            }
            let Some(worker) = p
                .first()
                .and_then(Value::as_str)
                .filter(|w| !w.is_empty() && w.len() <= 128)
            else {
                return vec![reply(rid, json!(false), error(20, "worker name required"))];
            };
            if session.workers.len() >= MAX_WORKERS && !session.workers.contains(worker) {
                return vec![reply(
                    rid,
                    json!(false),
                    error(20, "too many workers on one connection"),
                )];
            }
            let password = p.get(1).and_then(Value::as_str).unwrap_or_default();
            let event = Event::new(
                &actions::AUTHORIZE_EVENT,
                json!({"worker": worker, "password_given": !(password.is_empty() || password == "x"),
                       "user_agent": session.user_agent, "remote_addr": peer.to_string()}),
            );
            match ask(ctx, id, event, "authorize").await {
                Ok(a) if !a.invalid && matches!(a.verdict, Some(Ok(()))) => {
                    session.workers.insert(worker.to_string());
                    let mut out = vec![reply(rid, json!(true), Value::Null)];
                    apply_extras(session, pool, &a.extras, true, &mut out);
                    out
                }
                Ok(a) if !a.invalid => {
                    let why = match a.verdict {
                        Some(Err(why)) => why,
                        _ => crate::utils::WireFailure::Unavailable.text().to_string(),
                    };
                    let mut out = vec![reply(rid, json!(false), error(24, &why))];
                    apply_extras(session, pool, &a.extras, false, &mut out);
                    out
                }
                Ok(_) => vec![reply(
                    rid,
                    json!(false),
                    error(20, crate::utils::WireFailure::Unavailable.text()),
                )],
                Err(e) => vec![reply(rid, json!(false), error(20, failure_text(&e)))],
            }
        }
        "mining.submit" => submit(ctx, pool, session, id, peer, rid, &p).await,
        _ => vec![reply(rid, Value::Null, error(20, "Unknown method"))],
    }
}

async fn submit(
    ctx: &SpawnContext,
    pool: &Pool,
    session: &mut Session,
    id: ConnectionId,
    peer: SocketAddr,
    rid: &Value,
    p: &[Value],
) -> Vec<Value> {
    let refuse = |code: u32, why: &str| {
        Log::new(Some(&ctx.status_tx))
            .info(format!("Stratum connection {id} share refused: {why}"));
        vec![reply(rid, json!(false), error(code, why))]
    };
    if !session.subscribed {
        return refuse(25, "Not subscribed");
    }
    if p.len() != 5 {
        return refuse(
            20,
            "submit takes worker, job_id, extranonce2, ntime and nonce (no version rolling)",
        );
    }
    let s = |i: usize| p[i].as_str().unwrap_or_default();
    let (worker, job_id, en2_hex, ntime_hex, nonce_hex) = (s(0), s(1), s(2), s(3), s(4));
    if !session.workers.contains(worker) {
        return refuse(24, "Unauthorized worker");
    }
    let Some(job) = session.jobs.iter().find(|j| j.job_id == job_id).cloned() else {
        return refuse(21, "Job not found");
    };
    let Ok(en2) = hex::decode(en2_hex) else {
        return refuse(20, "extranonce2 is not hex");
    };
    if en2.len() != wire::EXTRANONCE2_SIZE {
        return refuse(20, "Invalid extranonce2 size");
    }
    let (Ok(ntime), Ok(nonce)) = (
        wire::parse_u32_hex(ntime_hex, "ntime"),
        wire::parse_u32_hex(nonce_hex, "nonce"),
    ) else {
        return refuse(20, "ntime and nonce must be 8 hex digits");
    };
    if ntime < job.ntime || ntime > job.ntime.saturating_add(wire::NTIME_ROLL) {
        return refuse(20, "Ntime out of range");
    }
    let key = (
        job_id.to_string(),
        en2_hex.to_lowercase(),
        ntime_hex.to_lowercase(),
        nonce_hex.to_lowercase(),
    );
    if session.seen.contains(&key) {
        return refuse(22, "Duplicate share");
    }
    let hash = job.share_hash(&session.extranonce1, &en2, ntime, nonce);
    let sdiff = wire::difficulty(&hash);
    let hash_hex = wire::display_hex(&hash);
    if sdiff < session.difficulty {
        return refuse(
            23,
            &format!(
                "Low difficulty share ({sdiff:.6} < {}): {hash_hex}",
                session.difficulty
            ),
        );
    }
    if session.seen.len() >= MAX_SEEN_SHARES {
        session.seen.clear();
    }
    session.seen.insert(key);
    let event = Event::new(
        &actions::SHARE_EVENT,
        json!({"worker": worker, "job_id": job_id, "hash": hash_hex, "share_difficulty": sdiff,
               "difficulty": session.difficulty, "remote_addr": peer.to_string()}),
    );
    match ask(ctx, id, event, "share").await {
        Ok(a) if !a.invalid && matches!(a.verdict, Some(Ok(()))) => {
            let mut out = vec![reply(rid, json!(true), Value::Null)];
            apply_extras(session, pool, &a.extras, false, &mut out);
            out
        }
        Ok(a) if !a.invalid => {
            let why = match a.verdict {
                Some(Err(why)) => why,
                _ => crate::utils::WireFailure::Unavailable.text().to_string(),
            };
            let mut out = vec![reply(rid, json!(false), error(20, &why))];
            apply_extras(session, pool, &a.extras, false, &mut out);
            out
        }
        Ok(_) => vec![reply(
            rid,
            json!(false),
            error(20, crate::utils::WireFailure::Unavailable.text()),
        )],
        Err(e) => vec![reply(rid, json!(false), error(20, failure_text(&e)))],
    }
}

async fn session(
    ctx: &SpawnContext,
    pool: &Pool,
    id: ConnectionId,
    socket: TcpStream,
    peer: SocketAddr,
    idle: Duration,
) -> Result<()> {
    let (reader, mut writer) = tokio::io::split(socket);
    let mut lines = wire::Lines::new(reader);
    let mut session = Session {
        extranonce1: pool
            .next_extranonce1
            .fetch_add(1, Ordering::Relaxed)
            .to_be_bytes(),
        subscribed: false,
        user_agent: None,
        workers: HashSet::new(),
        difficulty: pool.difficulty,
        jobs: VecDeque::new(),
        seen: HashSet::new(),
        height: 0,
    };
    while let Some(value) = lines.next(idle).await? {
        ctx.state
            .update_connection_stats(ctx.server_id, id, None, None, Some(1), None)
            .await;
        let out = match wire::parse(value)? {
            Message::Request {
                id: rid,
                method,
                params,
            } => answer(ctx, pool, &mut session, id, peer, &rid, &method, &params).await,
            Message::Notification { method, .. } => {
                Log::new(Some(&ctx.status_tx)).debug(format!(
                    "Stratum connection {id} notification {method} ignored"
                ));
                continue;
            }
            Message::Response { .. } => continue,
        };
        let bytes: Vec<u8> = out.iter().flat_map(wire::line).collect();
        tokio::time::timeout(wire::IO_TIMEOUT, writer.write_all(&bytes))
            .await
            .context("Stratum write deadline")??;
        ctx.state
            .update_connection_stats(
                ctx.server_id,
                id,
                None,
                Some(bytes.len() as u64),
                None,
                Some(out.len() as u64),
            )
            .await;
    }
    Ok(())
}