diff --git a/chain/src/store.rs b/chain/src/store.rs index f72fd27fcf..3492b7239b 100644 --- a/chain/src/store.rs +++ b/chain/src/store.rs @@ -584,6 +584,28 @@ impl OutputPosList { } Ok(()) } + + /// Pop the head of the list. + /// Returns the output_pos. + /// Returns None if list was empty. + pub fn pop_entry(batch: &Batch<'_>, commit: Commitment) -> Result, Error> { + match Self::get_list(batch, commit)? { + None => Ok(None), + Some(OutputPosList::Unique { pos }) => { + // TODO - delete the list itself. + + Ok(Some(pos)) + } + Some(OutputPosList::Multi { head, tail }) => { + // read head from db + // read next one + // update next to a head if it was a middle + // update list head + // update list to a unique if next is a tail + Ok(None) + } + } + } } pub enum OutputPosEntry { diff --git a/chain/tests/store_output_pos_list.rs b/chain/tests/store_output_pos_list.rs index 7c1c6cb817..8dd03db365 100644 --- a/chain/tests/store_output_pos_list.rs +++ b/chain/tests/store_output_pos_list.rs @@ -99,6 +99,20 @@ fn test_store_output_pos_list() { Ok(Some(OutputPosList::Multi { head: 3, tail: 1 })), ); + assert_eq!( + OutputPosList::pop_entry(&batch, commit,), + Ok(Some(OutputPos { + pos: 3, + height: 3, + features: OutputFeatures::Plain, + })), + ); + + assert_eq!( + OutputPosList::get_list(&batch, commit), + Ok(Some(OutputPosList::Multi { head: 2, tail: 1 })), + ); + // Cleanup chain directory clean_output_dir(chain_dir); }