thru-base 0.5.1

Core Rust primitives for the Thru blockchain: key and address types, transaction builders, state proof helpers, and RPC config.
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
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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
//! Program + official ABI deploys in one Multicall transaction.
//!
//! The Public Manager and the ABI Manager both read their new contents from
//! uploader buffers. These builders put the program change and the official
//! ABI change into a single Multicall instruction so both land or neither
//! does. The authority is always the fee payer (account index 0), which the
//! callee programs treat as authorized.

use anyhow::{Result, bail};

use crate::txn_lib::{TnPubkey, Transaction};
use crate::txn_tools::{
    ABI_ACCOUNT_HEADER_SIZE, ABI_MANAGER_INSTRUCTION_CREATE_ABI_OFFICIAL_EPHEMERAL,
    ABI_MANAGER_INSTRUCTION_CREATE_ABI_OFFICIAL_PERMANENT,
    ABI_MANAGER_INSTRUCTION_CREATE_META_OFFICIAL_EPHEMERAL,
    ABI_MANAGER_INSTRUCTION_CREATE_META_OFFICIAL_PERMANENT, MANAGER_INSTRUCTION_CREATE_EPHEMERAL,
    MANAGER_INSTRUCTION_CREATE_PERMANENT, account_state_units,
    build_abi_manager_create_abi_official_instruction,
    build_abi_manager_create_meta_official_instruction,
    build_abi_manager_upgrade_abi_official_instruction, build_manager_create_instruction,
    build_manager_upgrade_instruction,
};

/// Transaction MTU; a Multicall instruction must fit inside it.
const TXN_MTU: usize = 32_768;

/// Compute budget for a combined deploy (both managers copy buffers).
pub const PROGRAM_ABI_COMPUTE_UNITS: u32 = 500_000_000;

/// Memory budget for a combined deploy, matching the TypeScript SDK.
pub const PROGRAM_ABI_MEMORY_UNITS: u16 = 10_000;

/// Encode Multicall instruction data: a u16 call count, then for each call
/// the u16 account index of the callee program, the u64 length of its
/// instruction data, and the data itself (all little-endian).
pub fn build_multicall_instruction(calls: &[(u16, Vec<u8>)]) -> Result<Vec<u8>> {
    if calls.is_empty() {
        bail!("multicall needs at least one call");
    }
    let count = u16::try_from(calls.len())?;
    let mut out = Vec::new();
    out.extend_from_slice(&count.to_le_bytes());
    for (program_idx, data) in calls {
        out.extend_from_slice(&program_idx.to_le_bytes());
        out.extend_from_slice(&(data.len() as u64).to_le_bytes());
        out.extend_from_slice(data);
    }
    if out.len() > TXN_MTU {
        bail!(
            "multicall instruction is {} bytes, over the {} byte transaction limit",
            out.len(),
            TXN_MTU
        );
    }
    Ok(out)
}

/// Accounts touched by a program + official ABI deploy.
#[derive(Debug, Clone, Copy)]
pub struct ProgramAbiAccounts {
    pub manager_program: TnPubkey,
    pub abi_manager_program: TnPubkey,
    pub program_meta: TnPubkey,
    pub program: TnPubkey,
    pub abi_meta: TnPubkey,
    pub abi: TnPubkey,
    pub program_buffer: TnPubkey,
    pub abi_buffer: TnPubkey,
}

/// What happens to the managed program.
#[derive(Debug, Clone)]
pub enum ProgramStep<'a> {
    /// Create a new managed program from `seed`. Proofs are required for a
    /// permanent program and must be absent for an ephemeral one.
    Create {
        seed: &'a [u8],
        meta_proof: Option<&'a [u8]>,
        program_proof: Option<&'a [u8]>,
    },
    /// Upgrade an existing managed program.
    Upgrade,
}

/// What happens to the program's official ABI.
#[derive(Debug, Clone)]
pub enum AbiStep<'a> {
    /// Create the ABI meta account and the ABI account.
    CreateMetaAndAbi {
        meta_proof: Option<&'a [u8]>,
        abi_proof: Option<&'a [u8]>,
    },
    /// The ABI meta account exists; create the ABI account.
    CreateAbi { abi_proof: Option<&'a [u8]> },
    /// Upgrade the existing ABI account.
    Upgrade,
}

/// SHA-256 of the new program image and of the new ABI content. Each manager
/// instruction commits to its digest and installs only buffer bytes that hash
/// to it, so the buffers cannot be swapped after the transaction (or a
/// multisig request) is built.
#[derive(Debug, Clone, Copy)]
pub struct ProgramAbiDigests {
    pub program: [u8; 32],
    pub abi: [u8; 32],
}

/// Sizes used to size the deploy's state-unit budget.
#[derive(Debug, Clone, Copy)]
pub struct ProgramAbiSizes {
    /// New program image size.
    pub program: u32,
    /// New ABI content size.
    pub abi: u32,
    /// Current program account data size (upgrades only).
    pub current_program_data: Option<u64>,
    /// Current ABI content size (ABI upgrades only).
    pub current_abi: Option<u64>,
}

/// State units the deploy needs: new pages for created accounts, and the
/// page growth of upgraded accounts.
pub fn program_abi_state_units(
    program_step: &ProgramStep<'_>,
    abi_step: &AbiStep<'_>,
    sizes: &ProgramAbiSizes,
) -> Result<u16> {
    let pages = |bytes: u64| -> Result<u64> { Ok(u64::from(account_state_units(bytes)?)) };
    let mut units: u64 = 0;
    match program_step {
        // Program account plus the one-page manager meta account.
        ProgramStep::Create { .. } => units += pages(u64::from(sizes.program) + 4096)?,
        ProgramStep::Upgrade => {
            let current = sizes
                .current_program_data
                .unwrap_or(u64::from(sizes.program));
            units += pages(u64::from(sizes.program))?.saturating_sub(pages(current)?);
        }
    }
    let abi_bytes = u64::from(sizes.abi) + ABI_ACCOUNT_HEADER_SIZE;
    match abi_step {
        AbiStep::CreateMetaAndAbi { .. } => units += 1 + pages(abi_bytes)?,
        AbiStep::CreateAbi { .. } => units += pages(abi_bytes)?,
        AbiStep::Upgrade => {
            let current =
                sizes.current_abi.unwrap_or(u64::from(sizes.abi)) + ABI_ACCOUNT_HEADER_SIZE;
            units += pages(abi_bytes)?.saturating_sub(pages(current)?);
        }
    }
    Ok(u16::try_from(units)?)
}

/// Sorted account layout: index 0 is the fee payer, 1 the Multicall
/// program, then read-write accounts and read-only accounts, each sorted.
struct Layout {
    rw: Vec<TnPubkey>,
    ro: Vec<TnPubkey>,
}

impl Layout {
    fn new(mut rw: Vec<TnPubkey>, mut ro: Vec<TnPubkey>) -> Result<Self> {
        rw.sort();
        ro.sort();
        let mut all: Vec<TnPubkey> = rw.iter().chain(ro.iter()).copied().collect();
        let total = all.len();
        all.sort();
        all.dedup();
        if all.len() != total {
            bail!("program and ABI deploy accounts must be distinct");
        }
        Ok(Self { rw, ro })
    }

    fn idx(&self, key: &TnPubkey) -> Result<u16> {
        let pos = self
            .rw
            .iter()
            .chain(self.ro.iter())
            .position(|k| k == key)
            .ok_or_else(|| anyhow::anyhow!("account missing from deploy layout"))?;
        Ok(u16::try_from(pos + 2)?)
    }
}

/// Build the Multicall instruction calls for a program + official ABI
/// deploy against `idx`, which maps each account to its transaction index.
/// `authority_idx` is the index of the program authority (0 when the fee
/// payer signs directly).
#[allow(clippy::too_many_arguments)]
pub fn build_program_abi_calls(
    idx: &dyn Fn(&TnPubkey) -> Result<u16>,
    accounts: &ProgramAbiAccounts,
    program_step: &ProgramStep<'_>,
    abi_step: &AbiStep<'_>,
    program_size: u32,
    abi_size: u32,
    digests: &ProgramAbiDigests,
    authority_idx: u16,
    ephemeral: bool,
) -> Result<Vec<(u16, Vec<u8>)>> {
    let manager_idx = idx(&accounts.manager_program)?;
    let abi_manager_idx = idx(&accounts.abi_manager_program)?;
    let program_meta_idx = idx(&accounts.program_meta)?;
    let program_idx = idx(&accounts.program)?;
    let abi_meta_idx = idx(&accounts.abi_meta)?;
    let abi_idx = idx(&accounts.abi)?;
    let program_buffer_idx = idx(&accounts.program_buffer)?;
    let abi_buffer_idx = idx(&accounts.abi_buffer)?;

    let mut calls = Vec::with_capacity(3);
    match program_step {
        ProgramStep::Create {
            seed,
            meta_proof,
            program_proof,
        } => {
            let proof = match (meta_proof, program_proof) {
                (Some(meta), Some(program)) if !ephemeral => {
                    let mut proof = Vec::with_capacity(meta.len() + program.len());
                    proof.extend_from_slice(meta);
                    proof.extend_from_slice(program);
                    Some(proof)
                }
                (None, None) if ephemeral => None,
                _ => bail!(
                    "program creation proofs must be given exactly when the program is permanent"
                ),
            };
            let discriminant = if ephemeral {
                MANAGER_INSTRUCTION_CREATE_EPHEMERAL
            } else {
                MANAGER_INSTRUCTION_CREATE_PERMANENT
            };
            calls.push((
                manager_idx,
                build_manager_create_instruction(
                    discriminant,
                    program_meta_idx,
                    program_idx,
                    program_buffer_idx,
                    authority_idx,
                    0,
                    program_size,
                    digests.program,
                    seed,
                    proof.as_deref(),
                )?,
            ));
        }
        ProgramStep::Upgrade => calls.push((
            manager_idx,
            build_manager_upgrade_instruction(
                program_meta_idx,
                program_idx,
                program_buffer_idx,
                0,
                program_size,
                digests.program,
            )?,
        )),
    }

    let check_proof = |proof: &Option<&[u8]>, what: &str| -> Result<()> {
        if proof.is_some() == ephemeral {
            bail!("{what} creation proof must be given exactly when the ABI is permanent");
        }
        Ok(())
    };
    let create_abi = |proof: Option<&[u8]>| -> Result<(u16, Vec<u8>)> {
        let discriminant = if ephemeral {
            ABI_MANAGER_INSTRUCTION_CREATE_ABI_OFFICIAL_EPHEMERAL
        } else {
            ABI_MANAGER_INSTRUCTION_CREATE_ABI_OFFICIAL_PERMANENT
        };
        Ok((
            abi_manager_idx,
            build_abi_manager_create_abi_official_instruction(
                discriminant,
                abi_meta_idx,
                program_meta_idx,
                abi_idx,
                abi_buffer_idx,
                0,
                abi_size,
                digests.abi,
                authority_idx,
                proof,
            )?,
        ))
    };
    match abi_step {
        AbiStep::CreateMetaAndAbi {
            meta_proof,
            abi_proof,
        } => {
            check_proof(meta_proof, "ABI meta")?;
            check_proof(abi_proof, "ABI")?;
            let discriminant = if ephemeral {
                ABI_MANAGER_INSTRUCTION_CREATE_META_OFFICIAL_EPHEMERAL
            } else {
                ABI_MANAGER_INSTRUCTION_CREATE_META_OFFICIAL_PERMANENT
            };
            calls.push((
                abi_manager_idx,
                build_abi_manager_create_meta_official_instruction(
                    discriminant,
                    abi_meta_idx,
                    program_meta_idx,
                    authority_idx,
                    *meta_proof,
                )?,
            ));
            calls.push(create_abi(*abi_proof)?);
        }
        AbiStep::CreateAbi { abi_proof } => {
            check_proof(abi_proof, "ABI")?;
            calls.push(create_abi(*abi_proof)?);
        }
        AbiStep::Upgrade => calls.push((
            abi_manager_idx,
            build_abi_manager_upgrade_abi_official_instruction(
                abi_meta_idx,
                program_meta_idx,
                abi_idx,
                abi_buffer_idx,
                0,
                abi_size,
                digests.abi,
                authority_idx,
            )?,
        )),
    }
    Ok(calls)
}

/// Which accounts the deploy writes and which it only reads.
pub fn program_abi_account_access(
    accounts: &ProgramAbiAccounts,
    abi_step: &AbiStep<'_>,
) -> (Vec<TnPubkey>, Vec<TnPubkey>) {
    let mut rw = vec![accounts.program_meta, accounts.program, accounts.abi];
    let mut ro = vec![
        accounts.manager_program,
        accounts.abi_manager_program,
        accounts.program_buffer,
        accounts.abi_buffer,
    ];
    match abi_step {
        AbiStep::CreateMetaAndAbi { .. } => rw.push(accounts.abi_meta),
        AbiStep::CreateAbi { .. } | AbiStep::Upgrade => ro.push(accounts.abi_meta),
    }
    (rw, ro)
}

/// Build a fee-payer-signed transaction that creates or upgrades a managed
/// program and creates or upgrades its official ABI in one Multicall.
#[allow(clippy::too_many_arguments)]
pub fn build_program_abi_transaction(
    fee_payer: TnPubkey,
    multicall_program: TnPubkey,
    accounts: &ProgramAbiAccounts,
    program_step: &ProgramStep<'_>,
    abi_step: &AbiStep<'_>,
    sizes: &ProgramAbiSizes,
    digests: &ProgramAbiDigests,
    ephemeral: bool,
    fee: u64,
    nonce: u64,
    start_slot: u64,
) -> Result<Transaction> {
    let (rw, ro) = program_abi_account_access(accounts, abi_step);
    let layout = Layout::new(rw, ro)?;
    let calls = build_program_abi_calls(
        &|key| layout.idx(key),
        accounts,
        program_step,
        abi_step,
        sizes.program,
        sizes.abi,
        digests,
        0,
        ephemeral,
    )?;
    let instructions = build_multicall_instruction(&calls)?;
    let state_units = program_abi_state_units(program_step, abi_step, sizes)?;

    Ok(Transaction::new(fee_payer, multicall_program, fee, nonce)
        .with_start_slot(start_slot)
        .with_expiry_after(10000)
        .with_compute_units(PROGRAM_ABI_COMPUTE_UNITS)
        .with_memory_units(PROGRAM_ABI_MEMORY_UNITS)
        .with_state_units(state_units)
        .with_rw_accounts(layout.rw)
        .with_r_accounts(layout.ro)
        .with_instructions(instructions))
}

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

    fn key(b: u8) -> TnPubkey {
        [b; 32]
    }

    const DIGESTS: ProgramAbiDigests = ProgramAbiDigests {
        program: [0xA1; 32],
        abi: [0xB2; 32],
    };

    fn accounts() -> ProgramAbiAccounts {
        ProgramAbiAccounts {
            manager_program: key(10),
            abi_manager_program: key(11),
            program_meta: key(5),
            program: key(4),
            abi_meta: key(3),
            abi: key(2),
            program_buffer: key(9),
            abi_buffer: key(8),
        }
    }

    /// Split multicall data back into (program index, instruction data).
    fn decode(data: &[u8]) -> Vec<(u16, Vec<u8>)> {
        let count = u16::from_le_bytes([data[0], data[1]]) as usize;
        let mut off = 2;
        let mut out = Vec::new();
        for _ in 0..count {
            let idx = u16::from_le_bytes([data[off], data[off + 1]]);
            let len = u64::from_le_bytes(data[off + 2..off + 10].try_into().unwrap()) as usize;
            out.push((idx, data[off + 10..off + 10 + len].to_vec()));
            off += 10 + len;
        }
        assert_eq!(off, data.len());
        out
    }

    #[test]
    fn multicall_encoding_round_trips() {
        let data = build_multicall_instruction(&[(3, vec![1, 2]), (7, vec![])]).unwrap();
        assert_eq!(decode(&data), vec![(3, vec![1, 2]), (7, vec![])]);
        assert!(build_multicall_instruction(&[]).is_err());
        assert!(build_multicall_instruction(&[(2, vec![0; TXN_MTU])]).is_err());
    }

    #[test]
    fn create_matches_individual_instructions() {
        let a = accounts();
        let seed = b"my_seed";
        let (mp, pp, amp, ap) = (
            vec![1u8; 104],
            vec![2u8; 136],
            vec![3u8; 104],
            vec![4u8; 104],
        );
        let program_step = ProgramStep::Create {
            seed,
            meta_proof: Some(&mp),
            program_proof: Some(&pp),
        };
        let abi_step = AbiStep::CreateMetaAndAbi {
            meta_proof: Some(&amp),
            abi_proof: Some(&ap),
        };
        let sizes = ProgramAbiSizes {
            program: 5000,
            abi: 300,
            current_program_data: None,
            current_abi: None,
        };
        let tx = build_program_abi_transaction(
            key(1),
            key(12),
            &a,
            &program_step,
            &abi_step,
            &sizes,
            &DIGESTS,
            false,
            0,
            7,
            100,
        )
        .unwrap();

        // rw sorted: abi(2) abi_meta(3) program(4) program_meta(5);
        // ro sorted: abi_buffer(8) program_buffer(9) manager(10) abi_manager(11).
        let (rw, ro) = (tx.rw_accs.clone().unwrap(), tx.r_accs.clone().unwrap());
        assert_eq!(rw, vec![key(2), key(3), key(4), key(5)]);
        assert_eq!(ro, vec![key(8), key(9), key(10), key(11)]);

        let calls = decode(tx.instructions.as_ref().unwrap());
        assert_eq!(calls.len(), 3);
        let mut proof = mp.clone();
        proof.extend_from_slice(&pp);
        assert_eq!(
            calls[0],
            (
                8,
                build_manager_create_instruction(
                    MANAGER_INSTRUCTION_CREATE_PERMANENT,
                    5,
                    4,
                    7,
                    0,
                    0,
                    5000,
                    DIGESTS.program,
                    seed,
                    Some(&proof)
                )
                .unwrap()
            )
        );
        assert_eq!(
            calls[1],
            (
                9,
                build_abi_manager_create_meta_official_instruction(
                    ABI_MANAGER_INSTRUCTION_CREATE_META_OFFICIAL_PERMANENT,
                    3,
                    5,
                    0,
                    Some(&amp)
                )
                .unwrap()
            )
        );
        assert_eq!(
            calls[2],
            (
                9,
                build_abi_manager_create_abi_official_instruction(
                    ABI_MANAGER_INSTRUCTION_CREATE_ABI_OFFICIAL_PERMANENT,
                    3,
                    5,
                    2,
                    6,
                    0,
                    300,
                    DIGESTS.abi,
                    0,
                    Some(&ap)
                )
                .unwrap()
            )
        );
        // Program + manager meta (3 pages) + ABI meta (1) + ABI (1).
        assert_eq!(tx.req_state_units, 3 + 1 + 1);
    }

    #[test]
    fn upgrade_reads_abi_meta_and_charges_growth_only() {
        let a = accounts();
        let sizes = ProgramAbiSizes {
            program: 9000,
            abi: 100,
            current_program_data: Some(5000),
            current_abi: Some(4000),
        };
        let tx = build_program_abi_transaction(
            key(1),
            key(12),
            &a,
            &ProgramStep::Upgrade,
            &AbiStep::Upgrade,
            &sizes,
            &DIGESTS,
            false,
            0,
            1,
            1,
        )
        .unwrap();
        assert_eq!(tx.rw_accs.clone().unwrap(), vec![key(2), key(4), key(5)]);
        assert_eq!(
            tx.r_accs.clone().unwrap(),
            vec![key(3), key(8), key(9), key(10), key(11)]
        );
        let calls = decode(tx.instructions.as_ref().unwrap());
        assert_eq!(
            calls,
            vec![
                (
                    8,
                    build_manager_upgrade_instruction(4, 3, 7, 0, 9000, DIGESTS.program).unwrap()
                ),
                (
                    9,
                    build_abi_manager_upgrade_abi_official_instruction(5, 4, 2, 6, 0, 100, DIGESTS.abi, 0)
                        .unwrap()
                ),
            ]
        );
        // Program grows 2 -> 3 pages; ABI shrinks, which costs nothing.
        assert_eq!(tx.req_state_units, 1);
    }

    #[test]
    fn proofs_must_match_permanence() {
        let a = accounts();
        let sizes = ProgramAbiSizes {
            program: 10,
            abi: 10,
            current_program_data: Some(10),
            current_abi: None,
        };
        let missing = AbiStep::CreateAbi { abi_proof: None };
        assert!(
            build_program_abi_transaction(
                key(1),
                key(12),
                &a,
                &ProgramStep::Upgrade,
                &missing,
                &sizes,
                &DIGESTS,
                false,
                0,
                1,
                1
            )
            .is_err()
        );
        assert!(
            build_program_abi_transaction(
                key(1),
                key(12),
                &a,
                &ProgramStep::Upgrade,
                &missing,
                &sizes,
                &DIGESTS,
                true,
                0,
                1,
                1
            )
            .is_ok()
        );
    }

    #[test]
    fn duplicate_accounts_are_rejected() {
        let mut a = accounts();
        a.abi_buffer = a.program_buffer;
        let sizes = ProgramAbiSizes {
            program: 10,
            abi: 10,
            current_program_data: Some(10),
            current_abi: Some(10),
        };
        assert!(
            build_program_abi_transaction(
                key(1),
                key(12),
                &a,
                &ProgramStep::Upgrade,
                &AbiStep::Upgrade,
                &sizes,
                &DIGESTS,
                false,
                0,
                1,
                1
            )
            .is_err()
        );
    }
}