pcli/command/query/
shielded_pool.rs

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
use anyhow::Result;
use colored_json::prelude::*;
use penumbra_proto::DomainType;
use penumbra_sct::{CommitmentSource, NullificationInfo, Nullifier};
use penumbra_tct::StateCommitment;

#[derive(Debug, clap::Subcommand)]
pub enum ShieldedPool {
    /// Queries the state commitment tree anchor for a given height.
    Anchor {
        /// The height to query.
        height: u64,
    },
    /// Queries the source of a given commitment.
    Commitment {
        /// The commitment to query.
        #[clap(parse(try_from_str = StateCommitment::parse_hex))]
        commitment: StateCommitment,
    },
    /// Queries the note source of a given nullifier.
    Nullifier {
        /// The nullifier to query.
        #[clap(parse(try_from_str = Nullifier::parse_hex))]
        nullifier: Nullifier,
    },
    /// Queries the compact block at a given height.
    CompactBlock { height: u64 },
}

impl ShieldedPool {
    pub fn key(&self) -> String {
        use penumbra_sct::state_key as sct_state_key;
        match self {
            ShieldedPool::Anchor { height } => sct_state_key::tree::anchor_by_height(*height),
            ShieldedPool::CompactBlock { .. } => {
                unreachable!("should be handled at outer level via rpc");
            }
            ShieldedPool::Commitment { commitment } => sct_state_key::tree::note_source(commitment),
            ShieldedPool::Nullifier { nullifier } => {
                sct_state_key::nullifier_set::spent_nullifier_lookup(nullifier)
            }
        }
    }

    pub fn display_value(&self, bytes: &[u8]) -> Result<()> {
        let json = match self {
            ShieldedPool::Anchor { .. } => {
                let anchor = penumbra_tct::Root::decode(bytes)?;
                serde_json::to_string_pretty(&anchor)?
            }
            ShieldedPool::CompactBlock { .. } => {
                unreachable!("should be handled at outer level via rpc");
            }
            ShieldedPool::Commitment { .. } => {
                let commitment_source = CommitmentSource::decode(bytes)?;
                serde_json::to_string_pretty(&commitment_source)?
            }
            ShieldedPool::Nullifier { .. } => {
                let note_source = NullificationInfo::decode(bytes)?;
                serde_json::to_string_pretty(&note_source)?
            }
        };
        println!("{}", json.to_colored_json_auto()?);
        Ok(())
    }
}