Skip to content

Commit

Permalink
fix[doge_customs]: confirmations maybe negative when reorg
Browse files Browse the repository at this point in the history
  • Loading branch information
hsxyl committed Feb 19, 2025
1 parent 70aba23 commit 9365eca
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 23 deletions.
3 changes: 2 additions & 1 deletion customs/doge/doge_customs.did
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
type BlockHeaderJsonResult = record {
height : nat64;
confirmations : nat64;
confirmations : int64;
chainwork : text;
bits : text;
hash : text;
Expand Down Expand Up @@ -36,6 +36,7 @@ type CustomsError = variant {
HttpStatusError : record { nat; text; text };
OrdTxError : text;
NotBridgeTx;
NegativeConfirmations : int64;
AmountIsZero;
InvalidRuneId : text;
InvalidArgs : text;
Expand Down
2 changes: 1 addition & 1 deletion customs/doge/src/doge/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl TryFrom<BlockJsonResult> for Header {
#[serde(rename_all = "camelCase")]
pub struct BlockHeaderJsonResult {
pub hash: String,
pub confirmations: u64,
pub confirmations: i64,
pub height: u64,
pub version: i32,
pub version_hex: String,
Expand Down
2 changes: 1 addition & 1 deletion customs/doge/src/doge/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ pub async fn get_raw_transaction_by_rpc(

#[test]
pub fn test_rpc_response() {
let body_bin = vec![123, 34, 106, 115, 111, 110, 114, 112, 99, 34, 58, 34, 50, 46, 48, 34, 44, 34, 101, 114, 114, 111, 114, 34, 58, 123, 34, 99, 111, 100, 101, 34, 58, 45, 51, 50, 48, 48, 48, 44, 34, 109, 101, 115, 115, 97, 103, 101, 34, 58, 34, 83, 101, 114, 118, 101, 114, 32, 101, 114, 114, 111, 114, 34, 125, 44, 34, 100, 97, 115, 104, 98, 111, 97, 114, 100, 76, 111, 103, 34, 58, 34, 104, 116, 116, 112, 115, 58, 47, 47, 100, 97, 115, 104, 98, 111, 97, 114, 100, 46, 116, 97, 116, 117, 109, 46, 105, 111, 47, 108, 111, 103, 115, 63, 105, 100, 61, 54, 55, 97, 57, 57, 99, 50, 49, 48, 56, 56, 53, 55, 100, 51, 57, 50, 53, 51, 56, 55, 102, 99, 97, 34, 125];
let body_bin = vec![123, 34, 114, 101, 115, 117, 108, 116, 34, 58, 123, 34, 104, 97, 115, 104, 34, 58, 34, 56, 99, 99, 55, 98, 55, 56, 100, 51, 48, 51, 48, 48, 98, 48, 97, 98, 98, 98, 51, 54, 54, 53, 53, 97, 48, 102, 49, 99, 51, 52, 101, 101, 51, 101, 99, 99, 53, 100, 52, 97, 52, 57, 53, 99, 98, 52, 53, 50, 53, 55, 50, 56, 54, 99, 48, 53, 50, 57, 52, 48, 99, 51, 101, 34, 44, 34, 99, 111, 110, 102, 105, 114, 109, 97, 116, 105, 111, 110, 115, 34, 58, 45, 49, 44, 34, 104, 101, 105, 103, 104, 116, 34, 58, 53, 53, 57, 51, 50, 50, 55, 44, 34, 118, 101, 114, 115, 105, 111, 110, 34, 58, 54, 52, 50, 50, 55, 56, 56, 44, 34, 118, 101, 114, 115, 105, 111, 110, 72, 101, 120, 34, 58, 34, 48, 48, 54, 50, 48, 49, 48, 52, 34, 44, 34, 109, 101, 114, 107, 108, 101, 114, 111, 111, 116, 34, 58, 34, 54, 49, 98, 97, 101, 49, 100, 56, 100, 49, 54, 56, 97, 54, 52, 97, 48, 99, 55, 100, 98, 53, 51, 48, 97, 49, 51, 100, 49, 49, 53, 100, 97, 52, 50, 53, 99, 51, 53, 56, 57, 56, 101, 102, 55, 54, 102, 49, 56, 52, 97, 52, 54, 98, 57, 97, 48, 100, 100, 100, 102, 51, 98, 50, 34, 44, 34, 116, 105, 109, 101, 34, 58, 49, 55, 51, 57, 57, 51, 50, 56, 51, 57, 44, 34, 109, 101, 100, 105, 97, 110, 116, 105, 109, 101, 34, 58, 49, 55, 51, 57, 57, 51, 50, 51, 56, 55, 44, 34, 110, 111, 110, 99, 101, 34, 58, 48, 44, 34, 98, 105, 116, 115, 34, 58, 34, 49, 97, 48, 48, 99, 100, 102, 49, 34, 44, 34, 100, 105, 102, 102, 105, 99, 117, 108, 116, 121, 34, 58, 50, 48, 56, 53, 52, 57, 54, 57, 46, 53, 54, 55, 51, 52, 53, 48, 56, 44, 34, 99, 104, 97, 105, 110, 119, 111, 114, 107, 34, 58, 34, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 49, 55, 100, 54, 101, 51, 53, 55, 50, 102, 51, 97, 98, 102, 50, 50, 53, 99, 101, 48, 34, 44, 34, 112, 114, 101, 118, 105, 111, 117, 115, 98, 108, 111, 99, 107, 104, 97, 115, 104, 34, 58, 34, 48, 102, 99, 100, 50, 52, 49, 98, 100, 53, 98, 102, 52, 49, 57, 101, 98, 56, 97, 52, 99, 98, 56, 52, 54, 52, 57, 52, 55, 53, 55, 49, 54, 51, 54, 56, 56, 54, 49, 55, 101, 97, 53, 50, 99, 53, 54, 57, 55, 51, 100, 53, 48, 52, 97, 51, 100, 51, 48, 100, 102, 55, 49, 50, 34, 125, 44, 34, 101, 114, 114, 111, 114, 34, 58, 110, 117, 108, 108, 44, 34, 105, 100, 34, 58, 49, 125];
let body_text = String::from_utf8_lossy(&body_bin).to_string();
dbg!(&body_text);
let doge_rpc_res: DogeRpcResponse<String> = serde_json::from_slice(&body_bin).unwrap();
Expand Down
8 changes: 4 additions & 4 deletions customs/doge/src/dogeoin_to_custom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,8 @@ pub async fn finalize_lock_ticket_request() {
.iter()
.filter(|&req| {
let wait_time = wait_time.as_nanos() as u64;
(req.1.received_at + wait_time < now)
&& (req.1.received_at + wait_time * 6 > now)
req.1.received_at + wait_time < now
&& req.1.received_at + wait_time * 6 > now
})
.map(|req| (req.0.clone(), req.1.clone()))
.collect::<Vec<(Txid, LockTicketRequest)>>()
Expand All @@ -197,13 +197,13 @@ pub async fn finalize_lock_ticket_request() {
log!(INFO, "finalize lock success: {:?}", txid);
}
Err(e) => {
log!(ERROR, "finalize lock error: {:?}", e);
log!(ERROR, "finalize lock ticket error: {:?}, txid: {:?}", e, txid.to_string());
}
}
}
}
Err(e) => {
log!(ERROR, "finalize lock error: {:?}", e);
log!(ERROR, "finalize lock ticket error: {:?}, txid: {:?}", e, txid.to_string());
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions customs/doge/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ pub enum CustomsError {
MerkleBlockVerifyError(String, String),
#[error("BlockHashNotEqual at height({0}): saved: {1}, queried block({1}) pre hash: {2}")]
BlockHashNotEqual(u64, String, String, String),
#[error("NegativeConfirmations: {0}")]
NegativeConfirmations(i64)
}

/// A block validation error.
Expand Down
35 changes: 19 additions & 16 deletions customs/doge/src/tasks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,18 @@ fn sync_doge_block_header_task() {
}

async fn process_sync_doge_block_header() -> Result<(), CustomsError> {
let mut max_sync_times = 5;
let mut max_sync_times = 10;
let mut maybe_reorg = false;
while max_sync_times > 0 {
let current_block_header =
read_state(|s| s.doge_block_headers.get(&s.sync_doge_block_header_height)).ok_or(
CustomsError::CustomError("current block header not found".to_string()),
)?;
let next_block_hash = if let Some(next_block_hash) = current_block_header.nextblockhash {
next_block_hash
let next_block_hash =
if current_block_header.nextblockhash.is_some() && !maybe_reorg {
current_block_header.nextblockhash.ok_or(
CustomsError::CustomError("next block hash not found".to_string())
)?
} else {
let doge_rpc: DogeRpc = read_state(|s| s.default_doge_rpc_config.clone()).into();
match doge_rpc
Expand All @@ -82,34 +86,30 @@ async fn process_sync_doge_block_header() -> Result<(), CustomsError> {
}
};

let r = match sync_doge_block_header(next_block_hash).await {
let r = match sync_doge_block_header(next_block_hash.clone()).await {
Ok(r) => {
r
},
Err(e) => {
match e {
CustomsError::BlockHashNotEqual(height, last_block_hash, block_hash, prev_block_hash) => {
log!(
ERROR,
"sync doge block header height: {:?}, hash: {:?} not equal, last_block_hash: {:?}, prev_block_hash: {:?}, maybe reorg happened.",
height,
block_hash,
last_block_hash,
prev_block_hash
);
CustomsError::NegativeConfirmations(_) |
CustomsError::BlockHashNotEqual(_, _ ,_ ,_) => {
log!(ERROR, "maybe reorg, sync doge block header({:?}) error: {:?}", next_block_hash, e);

mutate_state(
|s| {
s.sync_doge_block_header_height = height - 5;
s.sync_doge_block_header_height = s.sync_doge_block_header_height - 6;
}
);
maybe_reorg = true;

continue;
},

_ => {
log!(
ERROR,
"sync doge block header error: {:?}",
"sync doge block header({:?}) error: {:?}",
next_block_hash,
e
);

Expand Down Expand Up @@ -151,6 +151,9 @@ async fn sync_doge_block_header(block_hash: String) -> Result<BlockHeaderJsonRes
// fetch block header
let doge_rpc: DogeRpc = read_state(|s| s.default_doge_rpc_config.clone()).into();
let mut block_header_json_result = doge_rpc.get_block_header(block_hash.as_str()).await?;
if block_header_json_result.confirmations < 0 {
return Err(CustomsError::NegativeConfirmations(block_header_json_result.confirmations));
}
let blocker_header_hex = doge_rpc.get_block_header_hex(block_hash.as_str()).await?;
block_header_json_result.block_header_hex = Some(blocker_header_hex.clone());

Expand Down

0 comments on commit 9365eca

Please sign in to comment.