snops 0.1.0

The snarkops control plane responsible for managing environments and agents
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
pub mod context;
pub mod error;
pub mod file;
mod net;
pub mod router;
pub mod sink;
pub mod source;
pub mod status;
pub mod tracker;

use std::{
    path::PathBuf,
    sync::{
        atomic::{AtomicU64, AtomicUsize},
        Arc,
    },
};

use context::ExecutionContext;
use dashmap::DashMap;
use snops_common::{
    aot_cmds::{AotCmd, Authorization},
    format::PackedUint,
    state::{CannonId, EnvId, NetworkId, StorageId},
};
use status::{TransactionSendState, TransactionStatusSender};
use tokio::{
    sync::{
        mpsc::{UnboundedReceiver, UnboundedSender},
        Semaphore,
    },
    task::AbortHandle,
};
use tracing::{error, trace, warn};
use tracker::TransactionTracker;

use self::{
    error::{CannonError, CannonInstanceError},
    sink::TxSink,
    source::TxSource,
};
use crate::{cannon::source::QueryTarget, state::GlobalState};

/*

STEP ONE
cannon transaction source: (GEN OR PLAYBACK)
- AOT: storage file
- REALTIME: generate executions from available agents?? via rpc


STEP 2
cannon query source:
/cannon/<id>/<network>/latest/stateRoot forwards to one of the following:
- REALTIME-(GEN|PLAYBACK): (test_id, node-key) with a rest ports Client/Validator only
- AOT-GEN: ledger service locally (file mode)
- AOT-PLAYBACK: n/a

STEP 3
cannon broadcast ALWAYS HITS control plane at
/cannon/<id>/<network>/transaction/broadcast
cannon TX OUTPUT pointing at
- REALTIME: (test_id, node-key)
- AOT: file


cannon rate
cannon buffer size
burst mode??

*/

/// Transaction cannon state
/// using the `TxSource` and `TxSink` for configuration.
#[derive(Debug)]
pub struct CannonInstance {
    pub id: CannonId,
    // a copy of the global state
    global_state: Arc<GlobalState>,

    pub source: TxSource,
    pub sink: TxSink,

    /// The test_id/storage associated with this cannon.
    /// To point at an external node, create a topology with external node
    /// To generate ahead-of-time, upload a test with a timeline referencing a
    /// cannon pointing at a file
    pub env_id: EnvId,
    pub network: NetworkId,

    /// Local query service port. Only present if the TxSource uses a local
    /// query source.
    query_port: Option<u16>,

    // TODO: run the actual cannon in this task
    pub task: Option<AbortHandle>,

    /// Child process must exist for the duration of the cannon instance.
    /// This value is never used
    #[allow(dead_code)]
    child: Option<tokio::process::Child>,

    /// channel to send transaction ids to the the task
    pub(crate) tx_sender: UnboundedSender<String>,
    /// channel to send authorizations (by transaction id) to the the task
    pub(crate) auth_sender: UnboundedSender<(String, TransactionStatusSender)>,
    /// transaction ids that are currently being processed
    pub(crate) transactions: Arc<DashMap<String, TransactionTracker>>,

    pub(crate) received_txs: Arc<AtomicU64>,
    pub(crate) fired_txs: Arc<AtomicUsize>,
}

pub struct CannonReceivers {
    transactions: UnboundedReceiver<String>,
    authorizations: UnboundedReceiver<(String, TransactionStatusSender)>,
}

pub type CannonInstanceMeta = (EnvId, NetworkId, StorageId, PathBuf);

impl CannonInstance {
    /// Increment and save the received transaction count
    pub(crate) fn inc_received_txs(
        state: &GlobalState,
        env_id: EnvId,
        cannon_id: CannonId,
        txs: &AtomicU64,
    ) -> u64 {
        let index = txs.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
        if let Err(e) = state
            .db
            .tx_index
            .save(&(env_id, cannon_id, String::new()), &PackedUint(index))
        {
            error!("cannon {env_id}.{cannon_id} failed to save received tx count: {e}");
        }
        index
    }

    /// Load transactions for this cannon/env from the store
    fn restore_transactions(
        state: &GlobalState,
        env_id: EnvId,
        cannon_id: CannonId,
    ) -> (DashMap<String, TransactionTracker>, AtomicU64) {
        let transactions = DashMap::new();

        // Restore the received transaction count (empty string key for tx_index)
        let received_txs = match state
            .db
            .tx_index
            .restore(&(env_id, cannon_id, String::new()))
        {
            Ok(Some(index)) => AtomicU64::new(index.0),
            Ok(None) => AtomicU64::new(0),
            Err(e) => {
                error!("cannon {env_id}.{cannon_id} failed to parse received tx count: {e}");
                AtomicU64::new(0)
            }
        };

        let statuses = match state.db.tx_status.read_with_prefix(&(env_id, cannon_id)) {
            Ok(statuses) => statuses,
            Err(e) => {
                error!("cannon {env_id}.{cannon_id} failed to restore transaction statuses: {e}");
                return (transactions, received_txs);
            }
        };

        // Walk through the statuses and restore the transactions (every transaction has
        // a status)
        for (key, status) in statuses {
            // Ensure the transaction has an index
            let index = match state.db.tx_index.restore(&key) {
                Ok(Some(index)) => index.0,
                Ok(None) => {
                    warn!(
                        "cannon {env_id}.{cannon_id} failed to restore index for transaction {} (missing index)", key.2
                    );
                    continue;
                }
                Err(e) => {
                    error!(
                        "cannon {env_id}.{cannon_id} failed to parse index for transaction {}: {e}",
                        key.2
                    );
                    continue;
                }
            };

            let authorization = match state.db.tx_auths.restore(&key) {
                Ok(auth) => auth.map(Arc::new),
                Err(e) => {
                    error!(
                        "cannon {env_id}.{cannon_id} failed to restore authorization for transaction {}: {e}",
                        key.2
                    );
                    continue;
                }
            };

            // Restore the transaction, if it exists. If there is an issue restoring the
            // transaction, it is possible to re-execute the authorization when
            // present.
            let transaction = match state.db.tx_blobs.restore(&key) {
                Ok(tx) => tx.map(Arc::new),
                Err(e) => {
                    if authorization.is_some() {
                        warn!(
                            "cannon {env_id}.{cannon_id} failed to restore json for transaction {}: {e}. Recovering from authorization",
                            key.2
                        );
                        None
                    } else {
                        error!(
                            "cannon {env_id}.{cannon_id} failed to restore json for transaction {}: {e}",
                            key.2
                        );
                        continue;
                    }
                }
            };

            transactions.insert(
                key.2,
                TransactionTracker {
                    index,
                    authorization,
                    transaction,
                    status,
                },
            );
        }

        (transactions, received_txs)
    }

    /// Create a new active transaction cannon
    /// with the given source and sink.
    ///
    /// Locks the global state's tests and storage for reading.
    pub fn new(
        global_state: Arc<GlobalState>,
        id: CannonId,
        (env_id, network, storage_id, aot_bin): CannonInstanceMeta,
        source: TxSource,
        sink: TxSink,
    ) -> Result<(Self, CannonReceivers), CannonError> {
        let (tx_sender, tx_receiver) = tokio::sync::mpsc::unbounded_channel();
        let query_port = source.get_query_port()?;
        let fired_txs = Arc::new(AtomicUsize::new(0));

        let storage_path = global_state.storage_path(network, storage_id);

        // spawn child process for ledger service if the source is local
        let child = query_port
            .map(|port| AotCmd::new(aot_bin, network).ledger_query(storage_path, port))
            .transpose()
            .map_err(|e| CannonError::Command(id, e))?;

        let (auth_sender, auth_receiver) = tokio::sync::mpsc::unbounded_channel();
        let (transactions, received_txs) = Self::restore_transactions(&global_state, env_id, id);

        Ok((
            Self {
                id,
                global_state,
                source,
                sink,
                env_id,
                network,
                tx_sender,
                auth_sender,
                query_port,
                child,
                task: None,
                fired_txs,
                received_txs: Arc::new(received_txs),
                transactions: Arc::new(transactions),
            },
            CannonReceivers {
                transactions: tx_receiver,
                authorizations: auth_receiver,
            },
        ))
    }

    /// Create an execution context for this cannon
    pub fn ctx(&self) -> ExecutionContext {
        ExecutionContext {
            id: self.id,
            env_id: self.env_id,
            network: self.network,
            source: self.source.clone(),
            sink: self.sink.clone(),
            fired_txs: Arc::clone(&self.fired_txs),
            state: Arc::clone(&self.global_state),
            transactions: Arc::clone(&self.transactions),
        }
    }

    /// Spawn the cannon's execution context as an abortable local task
    pub fn spawn_local(
        &mut self,
        rx: CannonReceivers,
        env_ready: Arc<Semaphore>,
    ) -> Result<(), CannonError> {
        let ctx = self.ctx();

        let handle = tokio::task::spawn(async move {
            // wait for the cannons to be ready
            let _ = env_ready.acquire().await;

            ctx.spawn(rx).await
        });
        self.task = Some(handle.abort_handle());

        Ok(())
    }

    /// Spawn the cannon's execution context and wait for it to finish
    #[deprecated = "originally used in the timeline API for temporary cannons with finite transaction counts"]
    pub async fn spawn(&mut self, rx: CannonReceivers) -> Result<(), CannonError> {
        self.ctx().spawn(rx).await
    }

    /// Get an expected local query address for this cannon
    pub fn get_local_query(&self) -> String {
        format!(
            "http://{}/api/v1/env/{}/cannons/{}",
            self.global_state.cli.get_local_addr(),
            self.env_id,
            self.id
        )
    }

    /// Called by axum to forward /cannon/<id>/<network>/latest/stateRoot
    /// to the ledger query service's /<network>/latest/stateRoot
    pub async fn proxy_state_root(&self) -> Result<String, CannonError> {
        let cannon_id = self.id;
        let env_id = self.env_id;
        let network = self.network;

        match &self.source.query {
            QueryTarget::Local(qs) => {
                if let Some(port) = self.query_port {
                    qs.get_state_root(network, port).await
                } else {
                    Err(CannonInstanceError::MissingQueryPort(cannon_id).into())
                }
            }
            QueryTarget::Node(target) => {
                // shortcut to cached state root if the target is all nodes
                if target.is_all() {
                    if let Some(info) = self.global_state.get_env_block_info(env_id) {
                        return Ok(info.state_root);
                    }
                }

                Ok(self
                    .global_state
                    .snarkos_get::<String>(env_id, "/stateRoot/latest", target)
                    .await?)
            }
        }
    }

    /// Called by axum to forward /cannon/<id>/<network>/transaction/broadcast
    /// to the desired sink
    pub fn proxy_broadcast(
        &self,
        tx_id: String,
        body: serde_json::Value,
    ) -> Result<(), CannonError> {
        let key = (self.env_id, self.id, tx_id.to_owned());

        // if the transaction is in the cache, it has already been broadcasted
        if let Some(cache) = self.global_state.env_network_cache.get(&self.env_id) {
            if cache.has_transaction(&tx_id) {
                if let Err(e) = TransactionTracker::delete(&self.global_state, &key) {
                    error!(
                        "cannon {}.{} failed to delete {tx_id} (in proxy_broadcast): {e:?}",
                        self.env_id, self.id
                    );
                }
                return Err(CannonError::TransactionAlreadyExists(self.id, tx_id));
            }
        }

        // prevent already queued transactions from being re-broadcasted
        let tracker = if let Some(mut tx) = self.transactions.get(&tx_id).as_deref().cloned() {
            // if we receive a transaction that is not executing, it is a duplicate
            if !matches!(tx.status, TransactionSendState::Executing(_)) {
                return Err(CannonError::TransactionAlreadyExists(self.id, tx_id));
            }

            // clear attempts (as this was a successful execute)
            if let Err(e) = TransactionTracker::clear_attempts(&self.global_state, &key) {
                error!(
                    "cannon {}.{} failed to clear attempts for {tx_id} (in proxy_broadcast): {e:?}",
                    self.env_id, self.id
                );
            }
            // update the status to pending broadcast, and write the transaction
            tx.status = TransactionSendState::Unsent;
            tx.transaction = Some(Arc::new(body));
            tx
        } else {
            trace!(
                "cannon {}.{} received broadcast {tx_id}",
                self.env_id,
                self.id
            );
            TransactionTracker {
                index: Self::inc_received_txs(
                    &self.global_state,
                    self.env_id,
                    self.id,
                    &self.received_txs,
                ),
                authorization: None,
                transaction: Some(Arc::new(body)),
                status: TransactionSendState::Unsent,
            }
        };

        // write the transaction to the store to prevent data loss
        tracker.write(&self.global_state, &key)?;
        self.transactions.insert(tx_id.to_owned(), tracker);

        // forward the transaction to the task, which will broadcast it
        // rather than waiting for the next broadcast check cycle
        self.tx_sender
            .send(tx_id)
            .map_err(|e| CannonError::SendTxError(self.id, e))?;

        Ok(())
    }

    /// Called by axum to forward /cannon/<id>/auth to a listen source
    pub async fn proxy_auth(
        &self,
        body: Authorization,
        events: TransactionStatusSender,
    ) -> Result<String, CannonError> {
        let Some(storage) = self
            .global_state
            .get_env(self.env_id)
            .map(|e| Arc::clone(&e.storage))
        else {
            // this error is very unlikely
            return Err(CannonError::BinaryError(
                self.id,
                "missing environment".to_owned(),
            ));
        };

        // resolve the binary for the compute target
        let compute_bin = storage
            .resolve_compute_binary(&self.global_state)
            .await
            .map_err(|e| CannonError::BinaryError(self.id, e.to_string()))?;
        let aot = AotCmd::new(compute_bin, self.network);

        // derive the transaction id from the authorization
        let tx_id = aot
            .get_tx_id(&body)
            .await
            .map_err(|e| CannonError::BinaryError(self.id, format!("derive tx id: {e}")))?;

        // prevent already queued transactions from being re-computed
        if self.transactions.contains_key(&tx_id) {
            return Err(CannonError::TransactionAlreadyExists(self.id, tx_id));
        }

        let tracker = TransactionTracker {
            index: Self::inc_received_txs(
                &self.global_state,
                self.env_id,
                self.id,
                &self.received_txs,
            ),
            authorization: Some(Arc::new(body)),
            transaction: None,
            status: TransactionSendState::Authorized,
        };
        // write the transaction to the store to prevent data loss
        tracker.write(
            &self.global_state,
            &(self.env_id, self.id, tx_id.to_owned()),
        )?;
        self.transactions.insert(tx_id.to_owned(), tracker);

        trace!("cannon {}.{} received auth {tx_id}", self.env_id, self.id);
        self.auth_sender
            .send((tx_id.to_owned(), events))
            .map_err(|e| CannonError::SendAuthError(self.id, e))?;

        Ok(tx_id)
    }
}

impl Drop for CannonInstance {
    fn drop(&mut self) {
        // cancel the task on drop
        if let Some(handle) = self.task.take() {
            handle.abort();
        }
    }
}