penumbra_sdk_sct/component/
source.rs

1use async_trait::async_trait;
2use cnidarium::StateWrite;
3
4use crate::{state_key, CommitmentSource};
5
6/// A helper trait for placing a `CommitmentSource` as ambient context during execution.
7#[async_trait]
8pub trait SourceContext: StateWrite {
9    fn put_current_source(&mut self, source: Option<CommitmentSource>) {
10        if let Some(source) = source {
11            self.object_put(state_key::ambient::current_source(), source)
12        } else {
13            self.object_delete(state_key::ambient::current_source())
14        }
15    }
16
17    fn get_current_source(&self) -> Option<CommitmentSource> {
18        self.object_get(state_key::ambient::current_source())
19    }
20
21    /// Sets a mock source, for testing.
22    ///
23    /// The `counter` field allows distinguishing hashes at different stages of the test.
24    fn put_mock_source(&mut self, counter: u8) {
25        self.put_current_source(Some(CommitmentSource::Transaction {
26            id: Some([counter; 32]),
27        }))
28    }
29}
30impl<T: StateWrite + ?Sized> SourceContext for T {}