taskfast-cli 0.22.0

`taskfast` CLI: worker + poster hot-loop operations, JSON-envelope output.
Documentation
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
// SPDX-License-Identifier: MIT
//! Deep Tempo transaction and wallet signing session module.
//!
//! Encapsulates keystore handling, address validation, RPC client binding,
//! ERC-20 allowances/balances, and automatically serializes every mutating
//! transaction broadcast under the exclusive [`wallet_lock`].

use std::path::PathBuf;
use std::time::Duration;

use alloy_primitives::{Address, Bytes, TxHash, U256};
use alloy_signer_local::PrivateKeySigner;
use alloy_sol_types::SolCall;

use taskfast_agent::chain::{decode_u256, IERC20};
use taskfast_agent::tempo_rpc::{sign_and_broadcast_tx, TempoRpcClient};
use taskfast_chains::tempo::TEMPO_MAINNET_CHAIN_ID;

use crate::cmd::CmdError;
use crate::wallet_lock::{self, WalletGuard};
use crate::Network;

#[cfg(test)]
use std::collections::HashMap;
#[cfg(test)]
use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
#[cfg(test)]
use std::sync::{Arc, Mutex};

/// Default receipt timeouts and polling interval across all on-chain commands.
pub const DEFAULT_RECEIPT_TIMEOUT_MAINNET: Duration = Duration::from_mins(3);
pub const DEFAULT_RECEIPT_TIMEOUT_TESTNET: Duration = Duration::from_mins(1);
pub const RECEIPT_POLL_INTERVAL: Duration = Duration::from_secs(2);

/// Default receipt timeout given a chain ID.
pub fn default_receipt_timeout_for_chain_id(chain_id: u64) -> Duration {
    if chain_id == TEMPO_MAINNET_CHAIN_ID {
        DEFAULT_RECEIPT_TIMEOUT_MAINNET
    } else {
        DEFAULT_RECEIPT_TIMEOUT_TESTNET
    }
}

/// Default receipt timeout given an abstract Network.
pub fn default_receipt_timeout_for_network(network: Network) -> Duration {
    match network {
        Network::Mainnet => DEFAULT_RECEIPT_TIMEOUT_MAINNET,
        Network::Testnet => DEFAULT_RECEIPT_TIMEOUT_TESTNET,
    }
}

/// In-process mock RPC adapter for hermetic tests without WireMock scaffolding.
#[cfg(test)]
#[derive(Debug)]
pub struct MockTempoRpc {
    pub chain_id: AtomicU64,
    pub balances: Mutex<HashMap<(Address, Address), U256>>,
    pub allowances: Mutex<HashMap<(Address, Address, Address), U256>>,
    pub broadcast_delay: Mutex<Option<Duration>>,
    pub active_broadcasts: AtomicUsize,
    pub max_concurrent_broadcasts: AtomicUsize,
    pub receipts: Mutex<HashMap<TxHash, bool>>,
    pub sent_transactions: Mutex<Vec<(Address, Bytes)>>,
    nonce: AtomicU64,
}

#[cfg(test)]
impl Default for MockTempoRpc {
    fn default() -> Self {
        Self {
            chain_id: AtomicU64::new(42427),
            balances: Mutex::new(HashMap::new()),
            allowances: Mutex::new(HashMap::new()),
            broadcast_delay: Mutex::new(None),
            active_broadcasts: AtomicUsize::new(0),
            max_concurrent_broadcasts: AtomicUsize::new(0),
            receipts: Mutex::new(HashMap::new()),
            sent_transactions: Mutex::new(Vec::new()),
            nonce: AtomicU64::new(1),
        }
    }
}

#[cfg(test)]
impl MockTempoRpc {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn set_balance(&self, token: Address, account: Address, balance: U256) {
        self.balances
            .lock()
            .unwrap()
            .insert((token, account), balance);
    }

    pub fn set_allowance(&self, token: Address, owner: Address, spender: Address, allowance: U256) {
        self.allowances
            .lock()
            .unwrap()
            .insert((token, owner, spender), allowance);
    }

    pub fn set_broadcast_delay(&self, delay: Duration) {
        *self.broadcast_delay.lock().unwrap() = Some(delay);
    }
}

/// Backend variant for RPC operations: live HTTP client or in-process mock.
#[derive(Clone, Debug)]
pub(crate) enum TempoRpcBackend {
    Client(TempoRpcClient),
    #[cfg(test)]
    Mock(Arc<MockTempoRpc>),
}

/// Deep session wrapping a signer, keystore file lock, and RPC transport.
#[derive(Clone, Debug)]
pub struct TempoSession {
    signer: PrivateKeySigner,
    keystore_path: Option<PathBuf>,
    backend: TempoRpcBackend,
    receipt_timeout: Duration,
}

impl TempoSession {
    /// Create a new session with a live RPC client.
    pub fn new(
        signer: PrivateKeySigner,
        keystore_path: Option<PathBuf>,
        rpc: TempoRpcClient,
        receipt_timeout: Duration,
    ) -> Self {
        Self {
            signer,
            keystore_path,
            backend: TempoRpcBackend::Client(rpc),
            receipt_timeout,
        }
    }

    /// Create a new session with an in-process mock adapter for testing.
    #[cfg(test)]
    pub fn from_mock(
        signer: PrivateKeySigner,
        keystore_path: Option<PathBuf>,
        mock: Arc<MockTempoRpc>,
        receipt_timeout: Duration,
    ) -> Self {
        Self {
            signer,
            keystore_path,
            backend: TempoRpcBackend::Mock(mock),
            receipt_timeout,
        }
    }

    pub fn signer(&self) -> &PrivateKeySigner {
        &self.signer
    }

    pub fn address(&self) -> Address {
        self.signer.address()
    }

    /// Acquire the exclusive wallet lock for the keystore path, if configured.
    pub fn acquire_lock(&self) -> Result<Option<WalletGuard>, CmdError> {
        if let Some(ref path) = self.keystore_path {
            Ok(Some(wallet_lock::acquire(path)?))
        } else {
            Ok(None)
        }
    }

    /// Read-only call impersonating an explicit sender (TIP-20 privacy compliance).
    pub async fn eth_call_from(
        &self,
        from: Address,
        to: Address,
        calldata: &Bytes,
    ) -> Result<Vec<u8>, CmdError> {
        match &self.backend {
            TempoRpcBackend::Client(client) => client
                .eth_call_from(from, to, calldata)
                .await
                .map_err(|e| CmdError::Server(format!("eth_call: {e}"))),
            #[cfg(test)]
            TempoRpcBackend::Mock(mock) => {
                if let Ok(call) = IERC20::balanceOfCall::abi_decode(calldata) {
                    let bal = mock
                        .balances
                        .lock()
                        .unwrap()
                        .get(&(to, call.account))
                        .copied()
                        .unwrap_or(U256::ZERO);
                    return Ok(bal.to_be_bytes::<32>().to_vec());
                }
                if let Ok(call) = IERC20::allowanceCall::abi_decode(calldata) {
                    let allow = mock
                        .allowances
                        .lock()
                        .unwrap()
                        .get(&(to, call.owner, call.spender))
                        .copied()
                        .unwrap_or(U256::ZERO);
                    return Ok(allow.to_be_bytes::<32>().to_vec());
                }
                Ok(vec![0u8; 32])
            }
        }
    }

    /// Query ERC-20 balance for an account.
    pub async fn erc20_balance_of(
        &self,
        token: Address,
        account: Address,
    ) -> Result<U256, CmdError> {
        let calldata: Bytes = IERC20::balanceOfCall { account }.abi_encode().into();
        let raw = self.eth_call_from(account, token, &calldata).await?;
        decode_u256(&raw).map_err(CmdError::Decode)
    }

    /// Query ERC-20 allowance for (owner, spender).
    pub async fn erc20_allowance(
        &self,
        token: Address,
        owner: Address,
        spender: Address,
    ) -> Result<U256, CmdError> {
        let calldata: Bytes = IERC20::allowanceCall { owner, spender }.abi_encode().into();
        let raw = self.eth_call_from(owner, token, &calldata).await?;
        decode_u256(&raw).map_err(CmdError::Decode)
    }

    /// Send a mutating transaction under the exclusive wallet lock without waiting for receipt.
    pub async fn send_transaction(&self, to: Address, calldata: Bytes) -> Result<TxHash, CmdError> {
        let _guard = self.acquire_lock()?;
        self.broadcast_internal(to, calldata)
            .await
            .map_err(|e| CmdError::Server(format!("tempo rpc: {e}")))
    }

    /// Send a mutating transaction under the exclusive wallet lock and wait for receipt.
    pub async fn send_transaction_and_wait(
        &self,
        to: Address,
        calldata: Bytes,
        action: &str,
        revert_suffix: Option<&str>,
    ) -> Result<TxHash, CmdError> {
        let _guard = self.acquire_lock()?;
        let (broadcast_msg, receipt_msg, tx_prefix) = if action.is_empty() {
            (
                "broadcast failed".to_string(),
                "receipt".to_string(),
                "tx".to_string(),
            )
        } else {
            (
                format!("{action} broadcast failed"),
                format!("{action} receipt"),
                format!("{action} tx"),
            )
        };
        let tx_hash = self
            .broadcast_internal(to, calldata)
            .await
            .map_err(|e| CmdError::Server(format!("{broadcast_msg}: {e}")))?;
        let ok = self
            .wait_for_receipt_internal(tx_hash)
            .await
            .map_err(|e| CmdError::Server(format!("{receipt_msg}: {e}")))?;
        if !ok {
            let suffix = revert_suffix.unwrap_or("");
            return Err(CmdError::Server(format!(
                "{tx_prefix} {tx_hash:#x} reverted on-chain{suffix}"
            )));
        }
        Ok(tx_hash)
    }

    /// Unified ERC-20 allowance check: checks current allowance and sends `approve` if needed.
    pub async fn ensure_allowance(
        &self,
        token: Address,
        spender: Address,
        required: U256,
    ) -> Result<Option<TxHash>, CmdError> {
        let current_allowance = self.erc20_allowance(token, self.address(), spender).await?;
        if current_allowance >= required {
            return Ok(None);
        }
        let approve_calldata: Bytes = IERC20::approveCall {
            spender,
            amount: required,
        }
        .abi_encode()
        .into();
        let approve_hash = self
            .send_transaction_and_wait(token, approve_calldata, "approve", None)
            .await?;
        let new_allowance = self.erc20_allowance(token, self.address(), spender).await?;
        if new_allowance < required {
            return Err(CmdError::Server(format!(
                "allowance still {new_allowance} after approve tx {approve_hash:#x} (needed {required})"
            )));
        }
        Ok(Some(approve_hash))
    }

    async fn broadcast_internal(&self, to: Address, calldata: Bytes) -> Result<TxHash, String> {
        match &self.backend {
            TempoRpcBackend::Client(client) => {
                sign_and_broadcast_tx(client, &self.signer, to, calldata)
                    .await
                    .map_err(|e| e.to_string())
            }
            #[cfg(test)]
            TempoRpcBackend::Mock(mock) => {
                let active = mock.active_broadcasts.fetch_add(1, Ordering::SeqCst) + 1;
                mock.max_concurrent_broadcasts
                    .fetch_max(active, Ordering::SeqCst);

                let delay = *mock.broadcast_delay.lock().unwrap();
                if let Some(d) = delay {
                    tokio::time::sleep(d).await;
                }
                if let Ok(call) = IERC20::approveCall::abi_decode(&calldata) {
                    mock.allowances
                        .lock()
                        .unwrap()
                        .insert((to, self.address(), call.spender), call.amount);
                }
                mock.sent_transactions
                    .lock()
                    .unwrap()
                    .push((to, calldata.clone()));
                let nonce = mock.nonce.fetch_add(1, Ordering::SeqCst);
                let mut data = calldata.to_vec();
                data.extend_from_slice(&nonce.to_be_bytes());
                let hash = alloy_primitives::keccak256(&data);

                mock.active_broadcasts.fetch_sub(1, Ordering::SeqCst);
                Ok(TxHash::from(hash))
            }
        }
    }

    async fn wait_for_receipt_internal(&self, tx_hash: TxHash) -> Result<bool, String> {
        match &self.backend {
            TempoRpcBackend::Client(client) => client
                .wait_for_receipt(tx_hash, self.receipt_timeout, RECEIPT_POLL_INTERVAL)
                .await
                .map_err(|e| e.to_string()),
            #[cfg(test)]
            TempoRpcBackend::Mock(mock) => {
                let ok = mock
                    .receipts
                    .lock()
                    .unwrap()
                    .get(&tx_hash)
                    .copied()
                    .unwrap_or(true);
                Ok(ok)
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn test_signer() -> PrivateKeySigner {
        PrivateKeySigner::random()
    }

    #[tokio::test]
    async fn test_mock_balance_and_allowance() {
        let signer = test_signer();
        let mock = Arc::new(MockTempoRpc::new());
        let token = Address::repeat_byte(0x01);
        let spender = Address::repeat_byte(0x02);
        mock.set_balance(token, signer.address(), U256::from(5000));
        mock.set_allowance(token, signer.address(), spender, U256::from(1000));

        let session = TempoSession::from_mock(signer, None, mock, Duration::from_secs(10));

        let bal = session
            .erc20_balance_of(token, session.address())
            .await
            .unwrap();
        assert_eq!(bal, U256::from(5000));

        let allow = session
            .erc20_allowance(token, session.address(), spender)
            .await
            .unwrap();
        assert_eq!(allow, U256::from(1000));

        // When required <= current allowance, ensure_allowance returns Ok(None)
        let tx = session
            .ensure_allowance(token, spender, U256::from(800))
            .await
            .unwrap();
        assert!(tx.is_none());

        // When required > current allowance, ensure_allowance broadcasts approve and returns Ok(Some)
        let tx = session
            .ensure_allowance(token, spender, U256::from(2000))
            .await
            .unwrap();
        assert!(tx.is_some());

        let new_allow = session
            .erc20_allowance(token, session.address(), spender)
            .await
            .unwrap();
        assert_eq!(new_allow, U256::from(2000));
    }

    #[test]
    fn test_concurrency_serialization_under_wallet_lock() {
        let tmp = tempfile::TempDir::new().unwrap();
        let keystore_file = tmp.path().join("keystore.json");
        std::fs::write(&keystore_file, "{}").unwrap();

        let mock = Arc::new(MockTempoRpc::new());
        // Set broadcast delay so a concurrent broadcast must wait
        mock.set_broadcast_delay(Duration::from_millis(50));

        let mut threads = Vec::new();
        for _ in 0..4 {
            let signer = test_signer();
            let session = TempoSession::from_mock(
                signer,
                Some(keystore_file.clone()),
                mock.clone(),
                Duration::from_secs(5),
            );

            let handle = std::thread::spawn(move || {
                let rt = tokio::runtime::Builder::new_current_thread()
                    .enable_all()
                    .build()
                    .unwrap();
                rt.block_on(async move {
                    session
                        .send_transaction(Address::repeat_byte(0x05), Bytes::from_static(b"test"))
                        .await
                })
            });
            threads.push(handle);
        }

        for h in threads {
            let res = h.join().unwrap();
            assert!(res.is_ok());
        }

        // Under wallet_lock, max concurrency MUST be 1!
        assert_eq!(
            mock.max_concurrent_broadcasts.load(Ordering::SeqCst),
            1,
            "wallet_lock must serialize concurrent broadcasts so max concurrency is 1"
        );
        assert_eq!(mock.sent_transactions.lock().unwrap().len(), 4);
    }
}