Skip to content

Commit

Permalink
Fix sat index test and unbound assignment (#2154)
Browse files Browse the repository at this point in the history
  • Loading branch information
raphjaph authored Jun 6, 2023
1 parent 6801bb4 commit 223a837
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 40 deletions.
65 changes: 37 additions & 28 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}
}
Expand Down Expand Up @@ -2177,7 +2186,7 @@ mod tests {
outpoint: unbound_outpoint(),
offset: 0,
},
Some(50 * COIN_VALUE),
None, // should not be on a sat
);

assert!(context
Expand Down Expand Up @@ -2354,7 +2363,7 @@ mod tests {
outpoint: OutPoint { txid, vout: 0 },
offset: 0,
},
None,
Some(50 * COIN_VALUE),
);

context.index.assert_inscription_location(
Expand All @@ -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(
Expand All @@ -2372,7 +2381,7 @@ mod tests {
outpoint: OutPoint { txid, vout: 0 },
offset: 100 * COIN_VALUE,
},
None,
Some(150 * COIN_VALUE),
);

assert_eq!(
Expand Down Expand Up @@ -2459,7 +2468,7 @@ mod tests {
outpoint: OutPoint { txid, vout: 0 },
offset: 0,
},
None,
Some(50 * COIN_VALUE),
);

context.index.assert_inscription_location(
Expand Down Expand Up @@ -2566,7 +2575,7 @@ mod tests {
outpoint: OutPoint { txid, vout: 0 },
offset: 0,
},
None,
Some(50 * COIN_VALUE),
);

context.index.assert_inscription_location(
Expand All @@ -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(
Expand Down Expand Up @@ -2718,7 +2727,7 @@ mod tests {
},
offset: 0,
},
None,
Some(100 * COIN_VALUE),
);

assert_eq!(
Expand Down
29 changes: 17 additions & 12 deletions src/index/updater/inscription_updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 223a837

Please sign in to comment.