From 223a837e5d115eafba95cfb1decdc8d104d7be5a Mon Sep 17 00:00:00 2001 From: raph Date: Tue, 6 Jun 2023 20:05:45 +0200 Subject: [PATCH] Fix sat index test and unbound assignment (#2154) --- src/index.rs | 65 ++++++++++++++---------- src/index/updater/inscription_updater.rs | 29 ++++++----- 2 files changed, 54 insertions(+), 40 deletions(-) diff --git a/src/index.rs b/src/index.rs index 81c19a0e28..530724de08 100644 --- a/src/index.rs +++ b/src/index.rs @@ -837,35 +837,44 @@ impl Index { inscription_id, ); - if let Some(sat) = sat { - if self.has_sat_index().unwrap() { - assert_eq!( - InscriptionId::load( - *rtx - .open_table(SAT_TO_INSCRIPTION_ID) - .unwrap() - .get(&sat) - .unwrap() - .unwrap() - .value() - ), - inscription_id, - ); - - // we do not track common sats or anything in the unbound output - if !Sat(sat).is_common() && satpoint.outpoint != unbound_outpoint() { + match sat { + Some(sat) => { + if self.has_sat_index().unwrap() { + // unbound inscriptions should not be assigned to a sat + assert!(satpoint.outpoint != unbound_outpoint()); assert_eq!( - SatPoint::load( + InscriptionId::load( *rtx - .open_table(SAT_TO_SATPOINT) + .open_table(SAT_TO_INSCRIPTION_ID) .unwrap() .get(&sat) .unwrap() .unwrap() .value() ), - satpoint, + inscription_id, ); + + // we do not track common sats (only the sat ranges) + if !Sat(sat).is_common() { + assert_eq!( + SatPoint::load( + *rtx + .open_table(SAT_TO_SATPOINT) + .unwrap() + .get(&sat) + .unwrap() + .unwrap() + .value() + ), + satpoint, + ); + } + } + } + None => { + if self.has_sat_index().unwrap() { + assert!(satpoint.outpoint == unbound_outpoint()) } } } @@ -2177,7 +2186,7 @@ mod tests { outpoint: unbound_outpoint(), offset: 0, }, - Some(50 * COIN_VALUE), + None, // should not be on a sat ); assert!(context @@ -2354,7 +2363,7 @@ mod tests { outpoint: OutPoint { txid, vout: 0 }, offset: 0, }, - None, + Some(50 * COIN_VALUE), ); context.index.assert_inscription_location( @@ -2363,7 +2372,7 @@ mod tests { outpoint: OutPoint { txid, vout: 0 }, offset: 50 * COIN_VALUE, }, - None, + Some(100 * COIN_VALUE), ); context.index.assert_inscription_location( @@ -2372,7 +2381,7 @@ mod tests { outpoint: OutPoint { txid, vout: 0 }, offset: 100 * COIN_VALUE, }, - None, + Some(150 * COIN_VALUE), ); assert_eq!( @@ -2459,7 +2468,7 @@ mod tests { outpoint: OutPoint { txid, vout: 0 }, offset: 0, }, - None, + Some(50 * COIN_VALUE), ); context.index.assert_inscription_location( @@ -2566,7 +2575,7 @@ mod tests { outpoint: OutPoint { txid, vout: 0 }, offset: 0, }, - None, + Some(50 * COIN_VALUE), ); context.index.assert_inscription_location( @@ -2575,7 +2584,7 @@ mod tests { outpoint: OutPoint { txid, vout: 0 }, offset: 50 * COIN_VALUE, }, - None, + Some(100 * COIN_VALUE), ); context.index.assert_inscription_location( @@ -2718,7 +2727,7 @@ mod tests { }, offset: 0, }, - None, + Some(100 * COIN_VALUE), ); assert_eq!( diff --git a/src/index/updater/inscription_updater.rs b/src/index/updater/inscription_updater.rs index 89f9435ea7..2b9abf7753 100644 --- a/src/index/updater/inscription_updater.rs +++ b/src/index/updater/inscription_updater.rs @@ -316,20 +316,25 @@ impl<'a, 'db, 'tx> InscriptionUpdater<'a, 'db, 'tx> { self.number_to_id.insert(number, &inscription_id)?; - let mut sat = None; - if let Some(input_sat_ranges) = input_sat_ranges { - let mut offset = 0; - for (start, end) in input_sat_ranges { - let size = end - start; - if offset + size > flotsam.offset { - let n = start + flotsam.offset - offset; - self.sat_to_inscription_id.insert(&n, &inscription_id)?; - sat = Some(Sat(n)); - break; + let sat = if unbound { + None + } else { + let mut sat = None; + if let Some(input_sat_ranges) = input_sat_ranges { + let mut offset = 0; + for (start, end) in input_sat_ranges { + let size = end - start; + if offset + size > flotsam.offset { + let n = start + flotsam.offset - offset; + self.sat_to_inscription_id.insert(&n, &inscription_id)?; + sat = Some(Sat(n)); + break; + } + offset += size; } - offset += size; } - } + sat + }; self.id_to_entry.insert( &inscription_id,