Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check for etching commit confirmations correctly #3507

Merged
merged 3 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions crates/mockcore/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ pub trait Api {
verbose: bool,
) -> Result<Value, jsonrpc_core::Error>;

#[rpc(name = "getblockheaderinfo")]
fn get_block_header_info(
&self,
block_hash: BlockHash,
) -> Result<GetBlockHeaderResult, jsonrpc_core::Error>;

#[rpc(name = "getblockstats")]
fn get_block_stats(&self, height: usize) -> Result<GetBlockStatsResult, jsonrpc_core::Error>;

Expand Down
44 changes: 39 additions & 5 deletions crates/mockcore/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,36 @@ impl Api for Server {
}
}

fn get_block_header_info(
&self,
block_hash: BlockHash,
) -> Result<GetBlockHeaderResult, jsonrpc_core::Error> {
let state = self.state();

let height = match state.hashes.iter().position(|hash| *hash == block_hash) {
Some(height) => height,
None => return Err(Self::not_found()),
};

Ok(GetBlockHeaderResult {
height,
hash: block_hash,
confirmations: 0,
version: Version::ONE,
version_hex: None,
merkle_root: TxMerkleNode::all_zeros(),
time: 0,
median_time: None,
nonce: 0,
bits: String::new(),
difficulty: 0.0,
chainwork: Vec::new(),
n_tx: 0,
previous_block_hash: None,
next_block_hash: None,
})
}

fn get_block_stats(&self, height: usize) -> Result<GetBlockStatsResult, jsonrpc_core::Error> {
let Some(block_hash) = self.state().hashes.get(height).cloned() else {
return Err(Self::not_found());
Expand Down Expand Up @@ -630,12 +660,16 @@ impl Api for Server {
blockhash: Option<BlockHash>,
) -> Result<Value, jsonrpc_core::Error> {
assert_eq!(blockhash, None, "Blockhash param is unsupported");

let state = self.state();

let current_height: u32 = (state.hashes.len() - 1).try_into().unwrap();
let confirmations = state
.txid_to_block_height
.get(&txid)
.map(|tx_height| current_height - tx_height);

let tx_height = state.txid_to_block_height.get(&txid);

let confirmations = tx_height.map(|tx_height| current_height - tx_height);

let blockhash = tx_height.map(|tx_height| state.hashes[usize::try_from(*tx_height).unwrap()]);

if verbose.unwrap_or(false) {
match state.transactions.get(&txid) {
Expand Down Expand Up @@ -667,7 +701,7 @@ impl Api for Server {
},
})
.collect(),
blockhash: None,
blockhash,
confirmations,
time: None,
blocktime: None,
Expand Down
27 changes: 21 additions & 6 deletions src/index/updater/rune_updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,20 +388,35 @@ impl<'a, 'tx, 'client> RuneUpdater<'a, 'tx, 'client> {
.get_raw_transaction_info(&input.previous_output.txid, None)
.into_option()?
else {
panic!("input not in UTXO set: {}", input.previous_output);
panic!(
"can't get input transaction: {}",
input.previous_output.txid
);
};

let taproot = tx_info.vout[input.previous_output.vout.into_usize()]
.script_pub_key
.script()?
.is_v1_p2tr();

let mature = tx_info
.confirmations
.map(|confirmations| confirmations >= Runestone::COMMIT_INTERVAL.into())
.unwrap_or_default();
if !taproot {
continue;
}

let commit_tx_height = self
.client
.get_block_header_info(&tx_info.blockhash.unwrap())
.into_option()?
.unwrap()
.height;

let confirmations = self
.height
.checked_sub(commit_tx_height.try_into().unwrap())
.unwrap()
+ 1;

if taproot && mature {
if confirmations >= Runestone::COMMIT_INTERVAL.into() {
return Ok(true);
}
}
Expand Down
64 changes: 63 additions & 1 deletion src/runes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5256,7 +5256,7 @@ mod tests {
..default()
});

context.mine_blocks((Runestone::COMMIT_INTERVAL - 1).into());
context.mine_blocks((Runestone::COMMIT_INTERVAL - 2).into());

let mut witness = Witness::new();

Expand Down Expand Up @@ -5302,6 +5302,68 @@ mod tests {
context.assert_runes([], []);
}

#[test]
fn immature_commits_are_not_valid_even_when_bitcoind_is_ahead() {
let context = Context::builder().arg("--index-runes").build();

let block_count = context.index.block_count().unwrap().into_usize();

context.mine_blocks_with_update(1, false);

context.core.broadcast_tx(TransactionTemplate {
inputs: &[(block_count, 0, 0, Witness::new())],
p2tr: true,
..default()
});

context.mine_blocks_with_update((Runestone::COMMIT_INTERVAL - 2).into(), false);

let mut witness = Witness::new();

let runestone = Runestone {
etching: Some(Etching {
rune: Some(Rune(RUNE)),
terms: Some(Terms {
amount: Some(1000),
..default()
}),
..default()
}),
..default()
};

let tapscript = script::Builder::new()
.push_slice::<&PushBytes>(
runestone
.etching
.unwrap()
.rune
.unwrap()
.commitment()
.as_slice()
.try_into()
.unwrap(),
)
.into_script();

witness.push(tapscript);

witness.push([]);

context.core.broadcast_tx(TransactionTemplate {
inputs: &[(block_count + 1, 1, 0, witness)],
op_return: Some(runestone.encipher()),
outputs: 1,
..default()
});

context.mine_blocks_with_update(2, false);

context.mine_blocks_with_update(1, true);

context.assert_runes([], []);
}

#[test]
fn etchings_are_not_valid_without_commitment() {
let context = Context::builder().arg("--index-runes").build();
Expand Down
14 changes: 7 additions & 7 deletions src/subcommand/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2078,7 +2078,7 @@ mod tests {
..default()
});

self.mine_blocks(Runestone::COMMIT_INTERVAL.into());
self.mine_blocks((Runestone::COMMIT_INTERVAL - 1).into());

let witness = witness.unwrap_or_else(|| {
let tapscript = script::Builder::new()
Expand Down Expand Up @@ -2535,8 +2535,8 @@ mod tests {

server.mine_blocks(1);

server.assert_redirect("/search/9:1", "/rune/AAAAAAAAAAAAA");
server.assert_redirect("/search?query=9:1", "/rune/AAAAAAAAAAAAA");
server.assert_redirect("/search/8:1", "/rune/AAAAAAAAAAAAA");
server.assert_redirect("/search?query=8:1", "/rune/AAAAAAAAAAAAA");

server.assert_response_regex(
"/search/100000000000000000000:200000000000000000",
Expand Down Expand Up @@ -2616,7 +2616,7 @@ mod tests {
server.mine_blocks(1);

server.assert_response_regex(
"/rune/9:1",
"/rune/8:1",
StatusCode::OK,
".*<title>Rune AAAAAAAAAAAAA</title>.*",
);
Expand Down Expand Up @@ -2763,11 +2763,11 @@ mod tests {
<dt>number</dt>
<dd>0</dd>
<dt>timestamp</dt>
<dd><time>1970-01-01 00:00:09 UTC</time></dd>
<dd><time>1970-01-01 00:00:08 UTC</time></dd>
<dt>id</dt>
<dd>9:1</dd>
<dd>8:1</dd>
<dt>etching block</dt>
<dd><a href=/block/9>9</a></dd>
<dd><a href=/block/8>8</a></dd>
<dt>etching transaction</dt>
<dd>1</dd>
<dt>mint</dt>
Expand Down
Loading