Skip to content

Commit

Permalink
[bitcoin-move][breaking]Refactor ordinals inscription updater (#2500)
Browse files Browse the repository at this point in the history
* [moveos_std] Make SimpleMap and SimpleMultiMap copy and drop

* [bitcoin-move] inscription updater

* [bitcoin-move] Support charms, reinscription, cursed

* [bitcoin-move] Add field to the inscription
  • Loading branch information
jolestar committed Aug 26, 2024
1 parent f214ead commit b11ee0b
Show file tree
Hide file tree
Showing 35 changed files with 2,801 additions and 1,432 deletions.
8 changes: 4 additions & 4 deletions crates/rooch-framework-tests/src/tests/bitcoin_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ fn check_utxo(txs: Vec<Transaction>, binding_test: &binding_test::RustBindingTes
let txid = tx.txid();
let rooch_btc_tx = rooch_types::bitcoin::types::Transaction::from(tx.clone());
ord_module
.from_transaction(&rooch_btc_tx, vec![], 0, 0)
.parse_inscription_from_tx(&rooch_btc_tx)
.unwrap()
.into_iter()
.enumerate()
Expand All @@ -174,9 +174,9 @@ fn check_utxo(txs: Vec<Transaction>, binding_test: &binding_test::RustBindingTes
);
let inscription_state = inscription_state.unwrap();
let inscription_object = inscription_state.into_object::<Inscription>().unwrap();
assert_eq!(inscription_object.value.txid, txid.into_address());
assert_eq!(inscription_object.value.index, index);
assert_eq!(inscription_object.value.body, inscription.body,);
assert_eq!(inscription_object.value.id.txid, txid.into_address());
assert_eq!(inscription_object.value.id.index, index);
assert_eq!(inscription_object.value.body, inscription.payload.body,);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,5 @@ async fn test_block_790964() {
// let expected_sequence_number = 8709019u32;
// assert_eq!(inscription.inscription_number, expected_inscription_number);
// assert_eq!(inscription.sequence_number, expected_sequence_number);
assert_eq!(inscription.offset, 0u64);
assert_eq!(inscription.location.offset, 0u64);
}
79 changes: 26 additions & 53 deletions crates/rooch-framework-tests/src/tests/ord_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,16 @@ use crate::{
};
use bitcoin::Transaction;
use moveos_types::module_binding::MoveFunctionCaller;
use rooch_types::bitcoin::network::Network;
use rooch_types::bitcoin::ord::Inscription;
use rooch_types::bitcoin::{
network::Network,
ord::{Envelope, InscriptionRecord},
};
use tracing::debug;

fn decode_inscription(
binding_test: &mut binding_test::RustBindingTest,
btc_tx: Transaction,
input_utxo_values: Vec<u64>,
next_inscription_number: u32,
next_sequence_number: u32,
) -> Vec<Inscription> {
) -> Vec<Envelope<InscriptionRecord>> {
debug!("tx_id: {}", btc_tx.txid());
for (i, input) in btc_tx.input.iter().enumerate() {
debug!("{}. input: {:?}", i, input.previous_output);
Expand All @@ -35,14 +34,7 @@ fn decode_inscription(
let move_btc_tx: rooch_types::bitcoin::types::Transaction =
rooch_types::bitcoin::types::Transaction::from(btc_tx);

ord_module
.from_transaction(
&move_btc_tx,
input_utxo_values,
next_inscription_number,
next_sequence_number,
)
.unwrap()
ord_module.parse_inscription_from_tx(&move_btc_tx).unwrap()
}

#[tokio::test]
Expand All @@ -55,52 +47,33 @@ async fn test_8706753() {
Network::Bitcoin,
"4b8111663106c242da8580ba38c36f261287b9c35b1aa5974f4c14306905e720",
);
let input_utxo_values = vec![3318u64];
let next_inscription_number = 8706753;
let next_sequence_number = 8709019;
let inscribe_tx_id = btc_tx.txid();
let mut inscriptions = decode_inscription(
&mut binding_test,
btc_tx,
input_utxo_values,
next_inscription_number,
next_sequence_number,
);
//let inscribe_tx_id = btc_tx.txid();
let inscriptions = decode_inscription(&mut binding_test, btc_tx);

assert_eq!(inscriptions.len(), 1);
let inscription = inscriptions.pop().unwrap();
let object_id = inscription.object_id();
//let inscription = inscriptions.pop().unwrap();
//let object_id = inscription.object_id();

let ord_module = binding_test.as_module_binding::<rooch_types::bitcoin::ord::OrdModule>();
//let ord_module = binding_test.as_module_binding::<rooch_types::bitcoin::ord::OrdModule>();
//https://mempool.space/api/tx/e5efc3b2bbf3d738d253e62ffde36b51abb5d12a748abf89d06fca456345fe48
let btc_tx_info: TxInfo = load_tx_info(
Network::Bitcoin,
"e5efc3b2bbf3d738d253e62ffde36b51abb5d12a748abf89d06fca456345fe48",
);
let input_index = btc_tx_info
.vin
.iter()
.position(|input| input.txid == inscribe_tx_id)
.unwrap() as u64;
let input_utxo_values: Vec<u64> = btc_tx_info
.vin
.iter()
.map(|input| input.prevout.value.to_sat())
.collect();
let spend_tx: Transaction = btc_tx_info.into();
let _spend_tx: Transaction = btc_tx_info.into();
//let expect_offset = 316084756u64;
let (is_match, sat_point) = ord_module
.match_utxo_and_generate_sat_point(
inscription.offset,
object_id,
&spend_tx.into(),
input_utxo_values,
input_index,
)
.unwrap();
debug!("is_match: {}, sat_point: {:?}", is_match, sat_point);
// let (is_match, sat_point) = ord_module
// .match_utxo_and_generate_sat_point(
// inscription.offset,
// object_id,
// &spend_tx.into(),
// input_utxo_values,
// input_index,
// )
// .unwrap();
//debug!("is_match: {}, sat_point: {:?}", is_match, sat_point);
//The inscription is spent via fee, so the is_match should be false
assert!(!is_match);
//assert!(!is_match);
//TODO how to verify the coinbase sat_point
}

Expand All @@ -114,7 +87,7 @@ async fn test_from_tx() {
Network::Bitcoin,
"69d52ccb5eb80372b7fc6c4fc3feb17038dd2f58313c5d16302d70f7ef0fff7f",
);
decode_inscription(&mut binding_test, btc_tx, vec![], 0, 0);
decode_inscription(&mut binding_test, btc_tx);
}

#[tokio::test]
Expand All @@ -123,8 +96,8 @@ async fn test_local_tx() {
let mut binding_test = binding_test::RustBindingTest::new().unwrap();
//commit tx
let btx_tx = bitcoin_tx_from_hex("01000000000101303a8b5191266d37103f6c4c6033019b59d98ac468e07f61ddbc6f50b204a7eb0000000000fdffffff029b270000000000002251201d44f728e28f6ffa0b0094edabefb466a348e1e7adbf2ff0e7e70abd2ed8871bcbd0029500000000225120ae68b97d450930db183dede9c33fbb1c147a69672ecc3100a75f9be25c50f0cf014064cf4601628581d59a8dc482f10c67106ffad818869e733d9cabbdab73240da556aa4d04afd5fa1e72d5ca1ea4fa151385e3e8a7596ebd3959212c2a57696c8400000000");
decode_inscription(&mut binding_test, btx_tx, vec![], 0, 0);
decode_inscription(&mut binding_test, btx_tx);
//reveal tx
let btc_tx = bitcoin_tx_from_hex("010000000001019cea25cbdacc895f9dbb85e4bfb7aa51d04cc69cc7f75ed49da3ff3f442f2e7f0000000000fdffffff01102700000000000022512036646c76dd6505025341c7cc1cf6c22fcc638c47454945da1948a4637a86f9200340e7f99517f921be44b83854b05d7eb98f7c1a9a0cd373ade7e29c1bcc321190bef1766976726641b69ccc61f9ea62ea55b97e644ec47a15481d3b70b0c44d008f4c207f6ef96528b25ace707fe33f4a23113c824da971dab921a2ad311c309edf0944ac0063036f7264010118746578742f706c61696e3b636861727365743d7574662d38000668656c6c6f0a6821c17f6ef96528b25ace707fe33f4a23113c824da971dab921a2ad311c309edf094400000000");
decode_inscription(&mut binding_test, btc_tx, vec![], 0, 0);
decode_inscription(&mut binding_test, btc_tx);
}
67 changes: 49 additions & 18 deletions crates/rooch-open-rpc-spec/schemas/openrpc.json
Original file line number Diff line number Diff line change
Expand Up @@ -896,6 +896,23 @@
}
}
},
"BitcoinOutPointView": {
"type": "object",
"required": [
"txid",
"vout"
],
"properties": {
"txid": {
"$ref": "#/components/schemas/bitcoin::hash_types::newtypes::Txid"
},
"vout": {
"type": "integer",
"format": "uint32",
"minimum": 0.0
}
}
},
"DisplayFieldsView": {
"type": "object",
"required": [
Expand Down Expand Up @@ -1507,24 +1524,25 @@
"InscriptionView": {
"type": "object",
"required": [
"bitcoin_txid",
"body",
"index",
"charms",
"id",
"inscription_number",
"is_curse",
"location",
"metadata",
"offset",
"parents",
"sequence_number",
"txid"
"sequence_number"
],
"properties": {
"bitcoin_txid": {
"$ref": "#/components/schemas/bitcoin::hash_types::newtypes::Txid"
},
"body": {
"$ref": "#/components/schemas/alloc::vec::Vec<u8>"
},
"charms": {
"type": "integer",
"format": "uint16",
"minimum": 0.0
},
"content_encoding": {
"anyOf": [
{
Expand All @@ -1545,10 +1563,8 @@
}
]
},
"index": {
"type": "integer",
"format": "uint32",
"minimum": 0.0
"id": {
"$ref": "#/components/schemas/rooch_types::bitcoin::ord::BitcoinInscriptionID"
},
"inscription_number": {
"type": "integer",
Expand All @@ -1558,6 +1574,9 @@
"is_curse": {
"type": "boolean"
},
"location": {
"$ref": "#/components/schemas/SatPointView"
},
"metadata": {
"$ref": "#/components/schemas/alloc::vec::Vec<u8>"
},
Expand All @@ -1571,9 +1590,6 @@
}
]
},
"offset": {
"$ref": "#/components/schemas/u64"
},
"parents": {
"$ref": "#/components/schemas/alloc::vec::Vec<moveos_types::moveos_std::object::ObjectID>"
},
Expand All @@ -1591,9 +1607,6 @@
"type": "integer",
"format": "uint32",
"minimum": 0.0
},
"txid": {
"$ref": "#/components/schemas/primitive_types::H256"
}
}
},
Expand Down Expand Up @@ -2633,6 +2646,21 @@
}
]
},
"SatPointView": {
"type": "object",
"required": [
"offset",
"output"
],
"properties": {
"offset": {
"$ref": "#/components/schemas/u64"
},
"output": {
"$ref": "#/components/schemas/BitcoinOutPointView"
}
}
},
"ScriptCallView": {
"type": "object",
"required": [
Expand Down Expand Up @@ -3294,6 +3322,9 @@
"rooch_types::address::RoochAddress": {
"type": "string"
},
"rooch_types::bitcoin::ord::BitcoinInscriptionID": {
"type": "string"
},
"rooch_types::repair::RepairIndexerType": {
"type": "string"
},
Expand Down
65 changes: 43 additions & 22 deletions crates/rooch-rpc-api/src/jsonrpc_types/btc/ord.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,44 @@
// Copyright (c) RoochNetwork
// SPDX-License-Identifier: Apache-2.0

use crate::jsonrpc_types::btc::transaction::{hex_to_txid, TxidView};
use crate::jsonrpc_types::btc::transaction::hex_to_txid;
use crate::jsonrpc_types::{
BytesView, H256View, IndexerObjectStateView, IndexerStateIDView, MoveStringView,
ObjectIDVecView, ObjectMetaView, StrView, UnitedAddressView,
};
use anyhow::Result;
use bitcoin::hashes::Hash;
use bitcoin::Txid;
use moveos_types::move_std::string::MoveString;
use moveos_types::state::MoveState;
use moveos_types::{moveos_std::object::ObjectID, state::MoveStructType};
use rooch_types::bitcoin::ord;
use rooch_types::bitcoin::ord::{self, SatPoint};
use rooch_types::bitcoin::ord::{BitcoinInscriptionID, Inscription, InscriptionID};
use rooch_types::indexer::state::ObjectStateFilter;
use rooch_types::into_address::IntoAddress;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use std::fmt::Display;
use std::str::FromStr;

#[derive(Debug, PartialEq, Clone, Serialize, Deserialize, Eq, JsonSchema)]
pub struct BitcoinInscriptionIDView {
pub txid: TxidView,
pub index: u32,
use super::utxo::BitcoinOutPointView;

pub type BitcoinInscriptionIDView = StrView<BitcoinInscriptionID>;

impl FromStr for BitcoinInscriptionIDView {
type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(StrView(BitcoinInscriptionID::from_str(s)?))
}
}

impl From<BitcoinInscriptionIDView> for BitcoinInscriptionID {
fn from(inscription: BitcoinInscriptionIDView) -> Self {
BitcoinInscriptionID {
txid: inscription.txid.into(),
index: inscription.index,
}
impl Display for BitcoinInscriptionIDView {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}

impl From<InscriptionID> for BitcoinInscriptionIDView {
fn from(inscription_id: InscriptionID) -> Self {
StrView(BitcoinInscriptionID::from(inscription_id))
}
}

Expand Down Expand Up @@ -76,14 +84,28 @@ pub struct InscriptionIDView {
}

#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema)]
pub struct InscriptionView {
pub txid: H256View,
pub bitcoin_txid: TxidView,
pub index: u32,
pub struct SatPointView {
pub output: BitcoinOutPointView,
pub offset: StrView<u64>,
}

impl From<SatPoint> for SatPointView {
fn from(sat_point: SatPoint) -> Self {
SatPointView {
output: sat_point.outpoint.into(),
offset: StrView(sat_point.offset),
}
}
}

#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema)]
pub struct InscriptionView {
pub id: BitcoinInscriptionIDView,
pub location: SatPointView,
pub sequence_number: u32,
pub inscription_number: u32,
pub is_curse: bool,
pub charms: u16,
pub body: BytesView,
pub content_encoding: Option<MoveStringView>,
pub content_type: Option<MoveStringView>,
Expand All @@ -96,13 +118,12 @@ pub struct InscriptionView {
impl From<Inscription> for InscriptionView {
fn from(inscription: Inscription) -> Self {
InscriptionView {
txid: inscription.txid.into(),
bitcoin_txid: StrView(Txid::from_byte_array(inscription.txid.into_bytes())),
index: inscription.index,
offset: inscription.offset.into(),
id: inscription.id.into(),
location: inscription.location.into(),
sequence_number: inscription.sequence_number,
inscription_number: inscription.inscription_number,
is_curse: inscription.is_curse,
charms: inscription.charms,
body: StrView(inscription.body),
content_encoding: Option::<MoveString>::from(inscription.content_encoding).map(StrView),
content_type: Option::<MoveString>::from(inscription.content_type).map(StrView),
Expand Down
Loading

0 comments on commit b11ee0b

Please sign in to comment.