Skip to content

Commit

Permalink
fix: invalid parent inscription (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
wanyvic authored Apr 10, 2024
1 parent 7a379ac commit 9501474
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 152 deletions.
3 changes: 2 additions & 1 deletion src/index/updater/inscription_updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -656,16 +656,17 @@ impl<'a, 'db, 'tx> InscriptionUpdater<'a, 'db, 'tx> {
cursed,
fee: _,
hidden: _,
parent: _,
pointer: _,
reinscription: _,
unbound,
parent,
inscription,
vindicated,
} => Action::New {
cursed,
unbound,
vindicated,
parent,
inscription,
},
},
Expand Down
107 changes: 4 additions & 103 deletions src/okx/datastore/ord/operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,111 +23,12 @@ pub enum Action {
New {
cursed: bool,
unbound: bool,
#[serde(skip)]
inscription: Inscription,
#[serde(default)]
#[serde(skip)]
vindicated: bool,
#[serde(skip)]
parent: Option<InscriptionId>,
},
Transfer,
}

#[cfg(test)]
mod tests {

use super::*;
use crate::test::inscription;
use bitcoin::OutPoint;
use std::str::FromStr;

#[test]
fn test_inscription_op_deserialize_with_default_vindicated() {
let txid =
Txid::from_str("b61b0172d95e266c18aea0c624db987e971a5d6d4ebc2aaed85da4642d635735").unwrap();

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
struct OldInscriptionOp {
pub txid: Txid,
pub action: OldAction,
pub sequence_number: u32,
pub inscription_number: Option<i32>,
pub inscription_id: InscriptionId,
pub old_satpoint: SatPoint,
pub new_satpoint: Option<SatPoint>,
}

#[allow(clippy::large_enum_variant)]
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
enum OldAction {
New {
cursed: bool,
unbound: bool,
inscription: Inscription,
},
Transfer,
}

let old_action = OldAction::New {
cursed: true,
unbound: true,
inscription: inscription("text/plain;charset=utf-8", "foobar"),
};
let bytes = rmp_serde::to_vec(&old_action).unwrap();
let new_action: Action = rmp_serde::from_slice(&bytes).unwrap();
assert_eq!(
new_action,
Action::New {
cursed: true,
unbound: true,
vindicated: false,
inscription: inscription("text/plain;charset=utf-8", "foobar"),
}
);

let old_operation = OldInscriptionOp {
txid,
action: OldAction::New {
cursed: true,
unbound: true,
inscription: inscription("text/plain;charset=utf-8", "foobar"),
},
sequence_number: 100,
inscription_number: Some(100),
inscription_id: InscriptionId { txid, index: 0 },
old_satpoint: SatPoint::from_str(
"1111111111111111111111111111111111111111111111111111111111111111:1:1",
)
.unwrap(),
new_satpoint: Some(SatPoint {
outpoint: OutPoint { txid, vout: 0 },
offset: 1,
}),
};

let bytes = rmp_serde::to_vec(&old_operation).unwrap();

let new_operation: InscriptionOp = rmp_serde::from_slice(&bytes).unwrap();

assert_eq!(
new_operation,
InscriptionOp {
txid,
action: Action::New {
cursed: true,
unbound: true,
vindicated: false,
inscription: inscription("text/plain;charset=utf-8", "foobar"),
},
sequence_number: 100,
inscription_number: Some(100),
inscription_id: InscriptionId { txid, index: 0 },
old_satpoint: SatPoint::from_str(
"1111111111111111111111111111111111111111111111111111111111111111:1:1",
)
.unwrap(),
new_satpoint: Some(SatPoint {
outpoint: OutPoint { txid, vout: 0 },
offset: 1,
}),
}
);
}
}
1 change: 1 addition & 0 deletions src/okx/datastore/ord/redb/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ mod tests {
cursed: false,
unbound: false,
vindicated: false,
parent: None,
inscription: inscription("text/plain;charset=utf-8", "foobar"),
},
sequence_number: 100,
Expand Down
52 changes: 22 additions & 30 deletions src/okx/protocol/brc20/msg_resolver.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use super::*;
use crate::{
index::entry::{Entry, SatPointValue},
inscriptions::Inscription,
okx::{
datastore::{
brc20::TransferableLog,
Expand All @@ -16,7 +15,6 @@ use std::collections::HashMap;
impl Message {
pub(crate) fn resolve(
op: &InscriptionOp,
new_inscriptions: &[Inscription],
transfer_assets_cache: HashMap<SatPointValue, TransferableLog>,
) -> Result<Option<Message>> {
log::debug!("BRC20 resolving the message from {:?}", op);
Expand All @@ -25,20 +23,16 @@ impl Message {
.map(|satpoint| satpoint.outpoint.txid == op.txid)
.unwrap_or(false);

let brc20_operation = match op.action {
let brc20_operation = match &op.action {
// New inscription is not `cursed` or `unbound`.
Action::New {
cursed: false,
unbound: false,
vindicated: false,
inscription: _,
inscription,
..
} if sat_in_outputs => {
let Ok(brc20_opteration) = deserialize_brc20_operation(
new_inscriptions
.get(usize::try_from(op.inscription_id.index).unwrap())
.unwrap(),
&op.action,
) else {
let Ok(brc20_opteration) = deserialize_brc20_operation(inscription, &op.action) else {
return Ok(None);
};
brc20_opteration
Expand Down Expand Up @@ -75,7 +69,10 @@ impl Message {
#[cfg(test)]
mod tests {
use super::*;
use crate::okx::datastore::brc20::{Tick, TransferableLog};
use crate::{
okx::datastore::brc20::{Tick, TransferableLog},
Inscription,
};
use bitcoin::{Address, OutPoint};
use std::str::FromStr;

Expand All @@ -96,6 +93,7 @@ mod tests {
cursed: false,
unbound: false,
inscription: inscriptions.first().unwrap().clone(),
parent: None,
vindicated: false,
},
sequence_number: 1,
Expand Down Expand Up @@ -150,13 +148,10 @@ mod tests {
#[test]
fn test_invalid_protocol() {
let transfer_assets_cache = HashMap::new();
let (inscriptions, op) = create_inscribe_operation(
let (_inscriptions, op) = create_inscribe_operation(
r#"{ "p": "brc-20s","op": "deploy", "tick": "ordi", "max": "1000", "lim": "10" }"#,
);
assert_matches!(
Message::resolve(&op, &inscriptions, transfer_assets_cache),
Ok(None)
);
assert_matches!(Message::resolve(&op, transfer_assets_cache), Ok(None));
}

#[test]
Expand All @@ -171,12 +166,13 @@ mod tests {
cursed: true,
unbound: false,
inscription: inscriptions.first().unwrap().clone(),
parent: None,
vindicated: false,
},
..op
};
assert_matches!(
Message::resolve(&op, &inscriptions, transfer_assets_cache.clone()),
Message::resolve(&op, transfer_assets_cache.clone()),
Ok(None)
);

Expand All @@ -185,33 +181,32 @@ mod tests {
cursed: false,
unbound: true,
inscription: inscriptions.first().unwrap().clone(),
parent: None,
vindicated: false,
},
..op
};
assert_matches!(
Message::resolve(&op2, &inscriptions, transfer_assets_cache.clone()),
Message::resolve(&op2, transfer_assets_cache.clone()),
Ok(None)
);
let op3 = InscriptionOp {
action: Action::New {
cursed: true,
unbound: true,
inscription: inscriptions.first().unwrap().clone(),
parent: None,
vindicated: false,
},
..op
};
assert_matches!(
Message::resolve(&op3, &inscriptions, transfer_assets_cache),
Ok(None)
);
assert_matches!(Message::resolve(&op3, transfer_assets_cache), Ok(None));
}

#[test]
fn test_valid_inscribe_operation() {
let transfer_assets_cache = HashMap::new();
let (inscriptions, op) = create_inscribe_operation(
let (_inscriptions, op) = create_inscribe_operation(
r#"{ "p": "brc-20","op": "deploy", "tick": "ordi", "max": "1000", "lim": "10" }"#,
);
let _result_msg = Message {
Expand All @@ -230,7 +225,7 @@ mod tests {
sat_in_outputs: true,
};
assert_matches!(
Message::resolve(&op, &inscriptions, transfer_assets_cache),
Message::resolve(&op, transfer_assets_cache),
Ok(Some(_result_msg))
);
}
Expand All @@ -242,7 +237,7 @@ mod tests {
// inscribe transfer not found
let op = create_transfer_operation();
assert_matches!(
Message::resolve(&op, &[], transfer_assets_cache.clone()),
Message::resolve(&op, transfer_assets_cache.clone()),
Ok(None)
);

Expand All @@ -258,7 +253,7 @@ mod tests {
},
..op
};
assert_matches!(Message::resolve(&op1, &[], transfer_assets_cache), Ok(None));
assert_matches!(Message::resolve(&op1, transfer_assets_cache), Ok(None));
}

#[test]
Expand Down Expand Up @@ -292,9 +287,6 @@ mod tests {
sat_in_outputs: true,
};

assert_matches!(
Message::resolve(&op, &[], transfer_assets_cache),
Ok(Some(_msg))
);
assert_matches!(Message::resolve(&op, transfer_assets_cache), Ok(Some(_msg)));
}
}
7 changes: 5 additions & 2 deletions src/okx/protocol/brc20/operation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ pub(crate) fn deserialize_brc20_operation(
};

match action {
Action::New { .. } => match raw_operation {
Action::New { parent, .. } => match raw_operation {
RawOperation::Deploy(deploy) => Ok(Operation::Deploy(deploy)),
RawOperation::Mint(mint) => Ok(Operation::Mint {
mint,
parent: inscription.parent(),
parent: *parent,
}),
RawOperation::Transfer(transfer) => Ok(Operation::InscribeTransfer(transfer)),
},
Expand Down Expand Up @@ -219,6 +219,7 @@ mod tests {
cursed: false,
unbound: false,
vindicated: false,
parent: None,
inscription: inscription.clone()
},
)
Expand All @@ -243,6 +244,7 @@ mod tests {
cursed: false,
unbound: false,
vindicated: false,
parent: None,
inscription: inscription.clone()
},
)
Expand All @@ -267,6 +269,7 @@ mod tests {
cursed: false,
unbound: false,
vindicated: false,
parent: None,
inscription: inscription.clone()
},
)
Expand Down
7 changes: 1 addition & 6 deletions src/okx/protocol/ord/bitmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,7 @@ pub fn index_bitmap(

for op in positive_inscriptions.into_iter() {
match op.action {
Action::New {
cursed: _,
unbound: _,
vindicated: _,
inscription,
} => {
Action::New { inscription, .. } => {
if let Some((inscription_id, district)) =
index_district(context, inscription, op.inscription_id)?
{
Expand Down
Loading

0 comments on commit 9501474

Please sign in to comment.