Skip to content

Commit

Permalink
Unbind inscriptions from zero-sat transactions (ordinals#2107)
Browse files Browse the repository at this point in the history
  • Loading branch information
casey authored May 24, 2023
1 parent 93fb15e commit 950740a
Show file tree
Hide file tree
Showing 7 changed files with 253 additions and 74 deletions.
160 changes: 113 additions & 47 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ pub(crate) enum Statistic {
LostSats = 2,
OutputsTraversed = 3,
SatRanges = 4,
UnboundInscriptions = 5,
}

impl Statistic {
Expand Down Expand Up @@ -801,7 +802,7 @@ impl Index {
&self,
inscription_id: InscriptionId,
satpoint: SatPoint,
sat: u64,
sat: Option<u64>,
) {
let rtx = self.database.begin_read().unwrap();

Expand Down Expand Up @@ -836,32 +837,34 @@ impl Index {
inscription_id,
);

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,
);

assert_eq!(
SatPoint::load(
*rtx
.open_table(SAT_TO_SATPOINT)
.unwrap()
.get(&sat)
.unwrap()
.unwrap()
.value()
),
satpoint,
);
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,
);

assert_eq!(
SatPoint::load(
*rtx
.open_table(SAT_TO_SATPOINT)
.unwrap()
.get(&sat)
.unwrap()
.unwrap()
.value()
),
satpoint,
);
}
}
}

Expand Down Expand Up @@ -1378,7 +1381,70 @@ mod tests {
outpoint: OutPoint { txid, vout: 0 },
offset: 0,
},
50 * COIN_VALUE,
Some(50 * COIN_VALUE),
);
}
}

#[test]
fn inscriptions_without_sats_are_unbound() {
for context in Context::configurations() {
context.mine_blocks(1);

context.rpc_server.broadcast_tx(TransactionTemplate {
inputs: &[(1, 0, 0)],
fee: 50 * 100_000_000,
..Default::default()
});

context.mine_blocks(1);

let txid = context.rpc_server.broadcast_tx(TransactionTemplate {
inputs: &[(2, 1, 0)],
witness: inscription("text/plain", "hello").to_witness(),
..Default::default()
});

let inscription_id = InscriptionId::from(txid);

context.mine_blocks(1);

context.index.assert_inscription_location(
inscription_id,
SatPoint {
outpoint: unbound_outpoint(),
offset: 0,
},
None,
);

context.mine_blocks(1);

context.rpc_server.broadcast_tx(TransactionTemplate {
inputs: &[(4, 0, 0)],
fee: 50 * 100_000_000,
..Default::default()
});

context.mine_blocks(1);

let txid = context.rpc_server.broadcast_tx(TransactionTemplate {
inputs: &[(5, 1, 0)],
witness: inscription("text/plain", "hello").to_witness(),
..Default::default()
});

let inscription_id = InscriptionId::from(txid);

context.mine_blocks(1);

context.index.assert_inscription_location(
inscription_id,
SatPoint {
outpoint: unbound_outpoint(),
offset: 1,
},
None,
);
}
}
Expand All @@ -1403,7 +1469,7 @@ mod tests {
outpoint: OutPoint { txid, vout: 0 },
offset: 0,
},
50 * COIN_VALUE,
Some(50 * COIN_VALUE),
);

let send_txid = context.rpc_server.broadcast_tx(TransactionTemplate {
Expand All @@ -1422,7 +1488,7 @@ mod tests {
},
offset: 50 * COIN_VALUE,
},
50 * COIN_VALUE,
Some(50 * COIN_VALUE),
);
}
}
Expand Down Expand Up @@ -1465,7 +1531,7 @@ mod tests {
},
offset: 0,
},
50 * COIN_VALUE,
Some(50 * COIN_VALUE),
);

context.index.assert_inscription_location(
Expand All @@ -1477,7 +1543,7 @@ mod tests {
},
offset: 50 * COIN_VALUE,
},
100 * COIN_VALUE,
Some(100 * COIN_VALUE),
);
}
}
Expand All @@ -1502,7 +1568,7 @@ mod tests {
outpoint: OutPoint { txid, vout: 0 },
offset: 0,
},
50 * COIN_VALUE,
Some(50 * COIN_VALUE),
);

let send_txid = context.rpc_server.broadcast_tx(TransactionTemplate {
Expand All @@ -1522,7 +1588,7 @@ mod tests {
},
offset: 0,
},
50 * COIN_VALUE,
Some(50 * COIN_VALUE),
);
}
}
Expand Down Expand Up @@ -1551,7 +1617,7 @@ mod tests {
outpoint: OutPoint { txid, vout: 0 },
offset: 0,
},
50 * COIN_VALUE,
Some(50 * COIN_VALUE),
);

let send_txid = context.rpc_server.broadcast_tx(TransactionTemplate {
Expand All @@ -1570,7 +1636,7 @@ mod tests {
},
offset: 50 * COIN_VALUE,
},
50 * COIN_VALUE,
Some(50 * COIN_VALUE),
);
}
}
Expand Down Expand Up @@ -1606,7 +1672,7 @@ mod tests {
},
offset: 50 * COIN_VALUE,
},
50 * COIN_VALUE,
Some(50 * COIN_VALUE),
);
}
}
Expand Down Expand Up @@ -1642,7 +1708,7 @@ mod tests {
},
offset: 50 * COIN_VALUE,
},
50 * COIN_VALUE,
Some(50 * COIN_VALUE),
);
}
}
Expand Down Expand Up @@ -1671,7 +1737,7 @@ mod tests {
},
offset: 50 * COIN_VALUE,
},
50 * COIN_VALUE,
Some(50 * COIN_VALUE),
);
}
}
Expand All @@ -1697,7 +1763,7 @@ mod tests {
outpoint: OutPoint::null(),
offset: 0,
},
50 * COIN_VALUE,
Some(50 * COIN_VALUE),
);
}
}
Expand Down Expand Up @@ -1734,7 +1800,7 @@ mod tests {
outpoint: OutPoint::null(),
offset: 0,
},
50 * COIN_VALUE,
Some(50 * COIN_VALUE),
);

context.index.assert_inscription_location(
Expand All @@ -1743,7 +1809,7 @@ mod tests {
outpoint: OutPoint::null(),
offset: 50 * COIN_VALUE,
},
150 * COIN_VALUE,
Some(150 * COIN_VALUE),
);
}
}
Expand Down Expand Up @@ -1854,7 +1920,7 @@ mod tests {
outpoint: OutPoint::null(),
offset: 75 * COIN_VALUE,
},
100 * COIN_VALUE,
Some(100 * COIN_VALUE),
);
}
}
Expand All @@ -1880,7 +1946,7 @@ mod tests {
outpoint: OutPoint { txid, vout: 1 },
offset: 0,
},
50 * COIN_VALUE,
Some(50 * COIN_VALUE),
);
}
}
Expand All @@ -1905,7 +1971,7 @@ mod tests {
outpoint: OutPoint::null(),
offset: 0,
},
50 * COIN_VALUE,
Some(50 * COIN_VALUE),
);
}
}
Expand Down Expand Up @@ -2089,7 +2155,7 @@ mod tests {
},
offset: 0,
},
50 * COIN_VALUE,
Some(50 * COIN_VALUE),
);

let second = context.rpc_server.broadcast_tx(TransactionTemplate {
Expand All @@ -2109,7 +2175,7 @@ mod tests {
},
offset: 0,
},
50 * COIN_VALUE,
Some(50 * COIN_VALUE),
);

assert!(context
Expand Down
15 changes: 13 additions & 2 deletions src/index/updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,11 @@ impl Updater {
.map(|lost_sats| lost_sats.value())
.unwrap_or(0);

let unbound_inscriptions = statistic_to_count
.get(&Statistic::UnboundInscriptions.key())?
.map(|unbound_inscriptions| unbound_inscriptions.value())
.unwrap_or(0);

let mut inscription_updater = InscriptionUpdater::new(
self.height,
&mut inscription_id_to_satpoint,
Expand All @@ -428,6 +433,7 @@ impl Updater {
&mut sat_to_inscription_id,
&mut satpoint_to_inscription_id,
block.header.time,
unbound_inscriptions,
value_cache,
)?;

Expand Down Expand Up @@ -523,11 +529,16 @@ impl Updater {
}
} else {
for (tx, txid) in block.txdata.iter().skip(1).chain(block.txdata.first()) {
lost_sats += inscription_updater.index_transaction_inscriptions(tx, *txid, None)?;
inscription_updater.index_transaction_inscriptions(tx, *txid, None)?;
}
}

statistic_to_count.insert(&Statistic::LostSats.key(), &lost_sats)?;
statistic_to_count.insert(&Statistic::LostSats.key(), &inscription_updater.lost_sats)?;

statistic_to_count.insert(
&Statistic::UnboundInscriptions.key(),
&inscription_updater.unbound_inscriptions,
)?;

height_to_block_hash.insert(&self.height, &block.header.block_hash().store())?;

Expand Down
Loading

0 comments on commit 950740a

Please sign in to comment.