Skip to content

Commit 4895c42

Browse files
committed
model: support next index ops
1 parent f31bbff commit 4895c42

2 files changed

Lines changed: 40 additions & 4 deletions

File tree

src/model/db.rs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use std::rc::Rc;
2626
use std::sync::LazyLock;
2727

2828
use bpstd::psbt::Utxo;
29-
use bpstd::{DescrId, Keychain, NormalIndex, Outpoint, Sats, Terminal, XpubDerivable};
29+
use bpstd::{DescrId, Idx, Keychain, NormalIndex, Outpoint, Sats, Terminal, XpubDerivable};
3030
use native_db::transaction::{RTransaction, RwTransaction};
3131
use native_db::{Builder, Database, Models, db_type};
3232
use rgbp::descriptors::RgbDescr;
@@ -129,6 +129,15 @@ impl DbUtxos {
129129
})
130130
}
131131

132+
fn descr(&self) -> DescrModel {
133+
self.with_reader(|tx| {
134+
Ok(tx
135+
.get()
136+
.primary::<DescrModel>(self.id.0)?
137+
.expect("descriptor not found"))
138+
})
139+
}
140+
132141
pub fn utxos(&self) -> HashSet<Utxo> { self.all().collect() }
133142
}
134143

@@ -194,9 +203,26 @@ impl UtxoSet for DbUtxos {
194203

195204
fn outpoints(&self) -> impl Iterator<Item = Outpoint> { self.all().map(|utxo| utxo.outpoint) }
196205

197-
fn next_index_noshift(&self, keychain: impl Into<Keychain>) -> NormalIndex { todo!() }
206+
fn next_index_noshift(&self, keychain: impl Into<Keychain>) -> NormalIndex {
207+
self.descr().next_index(keychain)
208+
}
198209

199-
fn next_index(&mut self, keychain: impl Into<Keychain>, _shift: bool) -> NormalIndex { todo!() }
210+
fn next_index(&mut self, keychain: impl Into<Keychain>, shift: bool) -> NormalIndex {
211+
let keychain = keychain.into();
212+
let mut descr = self.descr();
213+
let next_index = descr.next_index(keychain);
214+
if shift {
215+
descr
216+
.next_index
217+
.entry(keychain)
218+
.and_modify(|i| *i = next_index.saturating_inc());
219+
self.with_writer(|tx| {
220+
tx.upsert(descr)?;
221+
Ok(())
222+
});
223+
}
224+
next_index
225+
}
200226
}
201227

202228
// We send it only once on `WriterService` construction, and then use it from a single thread

src/model/descr.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
// or implied. See the License for the specific language governing permissions and limitations under
2020
// the License.
2121

22-
use bpstd::DescrId;
22+
use std::collections::BTreeMap;
23+
24+
use bpstd::{DescrId, Keychain, NormalIndex};
2325
use native_db::ToKey;
2426
use native_model::Model;
2527
use rgbp::descriptors::RgbDescr;
@@ -32,8 +34,16 @@ pub struct DescrModel {
3234
#[primary_key]
3335
pub id: u64,
3436
pub descriptor: RgbDescr,
37+
pub next_index: BTreeMap<Keychain, NormalIndex>,
3538
}
3639

3740
impl DescrModel {
3841
pub fn descr_id(&self) -> DescrId { DescrId(self.id) }
42+
43+
pub fn next_index(&self, keychain: impl Into<Keychain>) -> NormalIndex {
44+
self.next_index
45+
.get(&keychain.into())
46+
.copied()
47+
.unwrap_or_default()
48+
}
3949
}

0 commit comments

Comments
 (0)