Skip to content

Commit

Permalink
ScriptUnspentKey uzize to be
Browse files Browse the repository at this point in the history
  • Loading branch information
canonbrother committed Nov 22, 2024
1 parent fbfbf3a commit 59795e9
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
11 changes: 7 additions & 4 deletions lib/ain-ocean/src/api/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -458,14 +458,17 @@ async fn list_transaction_unspent(
msg: format!("Invalid height: {}", height),
})?;
let txid = Txid::from_str(txid)?;
let n = n.parse::<usize>()?;
Ok::<([u8; 4], Txid, usize), Error>((height, txid, n))
let decoded_n = hex::decode(n)?;
let n = decoded_n.try_into().map_err(|_| Error::Other {
msg: format!("Invalid txno: {}", n)
})?;
Ok::<([u8; 4], Txid, [u8; 8]), Error>((height, txid, n))
})
.transpose()?
.unwrap_or((
[0u8, 0u8, 0u8, 0u8],
[0u8; 4],
Txid::from_byte_array([0x00u8; 32]),
usize::default(),
[0u8; 8],
));

let res = ctx
Expand Down
14 changes: 7 additions & 7 deletions lib/ain-ocean/src/indexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ fn index_script_unspent_vin(
vin: &VinStandard,
ctx: &Context,
) -> Result<()> {
let key = (ctx.block.height.to_be_bytes(), vin.txid, vin.vout);
let key = (ctx.block.height.to_be_bytes(), vin.txid, vin.vout.to_be_bytes());
let id = services.script_unspent.by_key.get(&key)?;
if let Some(id) = id {
services.script_unspent.by_id.delete(&id)?;
Expand Down Expand Up @@ -255,8 +255,8 @@ fn index_script_unspent_vout(services: &Arc<Services>, vout: &Vout, ctx: &Contex
},
};

let id = (hid, block.height.to_be_bytes(), tx.txid, vout.n);
let key = (block.height.to_be_bytes(), tx.txid, vout.n);
let id = (hid, block.height.to_be_bytes(), tx.txid, vout.n.to_be_bytes());
let key = (block.height.to_be_bytes(), tx.txid, vout.n.to_be_bytes());
services.script_unspent.by_key.put(&key, &id)?;
services.script_unspent.by_id.put(&id, &script_unspent)?;
Ok(())
Expand Down Expand Up @@ -458,12 +458,12 @@ fn invalidate_script_unspent_vin(
hid,
transaction.block.height.to_be_bytes(),
transaction.txid,
vout.n,
vout.n.to_be_bytes(),
);
let key = (
transaction.block.height.to_be_bytes(),
transaction.txid,
vout.n,
vout.n.to_be_bytes(),
);

services.script_unspent.by_key.put(&key, &id)?;
Expand Down Expand Up @@ -496,8 +496,8 @@ fn invalidate_script_unspent_vout(
vout: &Vout,
) -> Result<()> {
let hid = as_sha256(&vout.script_pub_key.hex);
let id = (hid, ctx.block.height.to_be_bytes(), ctx.tx.txid, vout.n);
let key = (ctx.block.height.to_be_bytes(), ctx.tx.txid, vout.n);
let id = (hid, ctx.block.height.to_be_bytes(), ctx.tx.txid, vout.n.to_be_bytes());
let key = (ctx.block.height.to_be_bytes(), ctx.tx.txid, vout.n.to_be_bytes());
services.script_unspent.by_id.delete(&id)?;
services.script_unspent.by_key.delete(&key)?;

Expand Down
4 changes: 2 additions & 2 deletions lib/ain-ocean/src/model/script_unspent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use serde::{Deserialize, Serialize};

use super::BlockContext;

pub type ScriptUnspentId = ([u8; 32], [u8; 4], Txid, usize); // hid + block.height + txid + vout_index
pub type ScriptUnspentKey = ([u8; 4], Txid, usize); // block.height + txid + vout_index, ps: key is required in index_script_unspent_vin
pub type ScriptUnspentId = ([u8; 32], [u8; 4], Txid, [u8; 8]); // hid + block.height + txid + vout_index
pub type ScriptUnspentKey = ([u8; 4], Txid, [u8; 8]); // block.height + txid + vout_index, ps: key is required in index_script_unspent_vin

#[derive(Debug, Serialize, Deserialize)]
pub struct ScriptUnspent {
Expand Down

0 comments on commit 59795e9

Please sign in to comment.