@@ -208,6 +208,42 @@ impl Tree {
208208 self . tree
209209 . commit_both ( self . io_root . namespace , self . io_root . version )
210210 }
211+
212+ /// Fetch the input artifact for the given transaction hash.
213+ pub fn get_input ( & self , tx_hash : Hash ) -> Result < Option < Vec < u8 > > > {
214+ let raw = self . tree . get (
215+ & TxnKeyFormat {
216+ tx_hash,
217+ kind : ArtifactKind :: Input ,
218+ }
219+ . encode ( ) ,
220+ ) ?;
221+ match raw {
222+ Some ( raw) => {
223+ let ia: InputArtifacts = cbor:: from_slice ( & raw ) ?;
224+ Ok ( Some ( ia. input ) )
225+ }
226+ None => Ok ( None ) ,
227+ }
228+ }
229+
230+ /// Fetch the output artifact for the given transaction hash.
231+ pub fn get_output ( & self , tx_hash : Hash ) -> Result < Option < Vec < u8 > > > {
232+ let raw = self . tree . get (
233+ & TxnKeyFormat {
234+ tx_hash,
235+ kind : ArtifactKind :: Output ,
236+ }
237+ . encode ( ) ,
238+ ) ?;
239+ match raw {
240+ Some ( raw) => {
241+ let oa: OutputArtifacts = cbor:: from_slice ( & raw ) ?;
242+ Ok ( Some ( oa. output ) )
243+ }
244+ None => Ok ( None ) ,
245+ }
246+ }
211247}
212248
213249#[ cfg( test) ]
@@ -228,6 +264,7 @@ mod test {
228264
229265 let input = b"this goes in" . to_vec ( ) ;
230266 let tx_hash = Hash :: digest_bytes ( & input) ;
267+ let orig_tx_hash = tx_hash;
231268 tree. add_input ( input, 0 ) . unwrap ( ) ;
232269 tree. add_output (
233270 tx_hash,
@@ -258,5 +295,16 @@ mod test {
258295 format!( "{:?}" , root_hash) ,
259296 "8399ffa753987b00ec6ab251337c6b88e40812662ed345468fcbf1dbdd16321c" ,
260297 ) ;
298+
299+ // Accessors.
300+ let dec_input = tree. get_input ( orig_tx_hash) . unwrap ( ) ;
301+ assert_eq ! ( dec_input, Some ( b"this goes in" . to_vec( ) ) ) ;
302+ let dec_output = tree. get_output ( orig_tx_hash) . unwrap ( ) ;
303+ assert_eq ! ( dec_output, Some ( b"and this comes out" . to_vec( ) ) ) ;
304+
305+ let dec_input = tree. get_input ( Hash :: empty_hash ( ) ) . unwrap ( ) ;
306+ assert ! ( dec_input. is_none( ) ) ;
307+ let dec_output = tree. get_output ( Hash :: empty_hash ( ) ) . unwrap ( ) ;
308+ assert ! ( dec_output. is_none( ) ) ;
261309 }
262310}
0 commit comments