From ff112ce382336514ee608c9268a1cb1d9a139cab Mon Sep 17 00:00:00 2001 From: Dylan Duan Date: Tue, 5 Sep 2023 15:42:18 +0800 Subject: [PATCH] refactor rust language --- src/api.rs | 8 +-- src/business/helper.rs | 4 +- src/ckb/indexer.rs | 6 +- src/ckb/rpc.rs | 25 ++++---- src/entries/claim.rs | 6 +- src/entries/claim_update.rs | 8 +-- src/entries/define.rs | 6 +- src/entries/extension.rs | 22 +++---- src/entries/helper.rs | 9 +-- src/entries/mint.rs | 6 +- src/entries/sequential_transfer.rs | 14 ++--- src/entries/smt.rs | 18 +++--- src/entries/social.rs | 10 ++-- src/entries/subkey.rs | 4 +- src/entries/transfer.rs | 8 +-- src/entries/transfer_update.rs | 8 +-- src/entries/update.rs | 6 +- src/entries/withdrawal.rs | 6 +- src/entries/witness.rs | 22 +++---- src/models/block.rs | 4 +- src/models/claim.rs | 4 +- src/models/class.rs | 4 +- src/models/common.rs | 19 +++--- src/models/define.rs | 10 ++-- src/models/extension/leaves.rs | 6 +- src/models/extension/social.rs | 5 +- src/models/extension/subkey.rs | 2 +- src/models/hold.rs | 28 ++++----- src/models/issuer.rs | 2 +- src/models/joyid.rs | 4 +- src/models/scripts.rs | 8 +-- src/models/withdrawal/nft.rs | 31 +++++----- src/models/withdrawal/transaction.rs | 8 +-- src/request/extension.rs | 6 +- src/request/fetch.rs | 12 ++-- src/request/helper.rs | 22 +++---- src/request/transfer.rs | 6 +- src/response/define.rs | 2 +- src/response/witness/cota.rs | 14 ++--- src/response/witness/parser.rs | 86 ++++++++++++++-------------- src/smt/db/db.rs | 6 +- src/smt/db/transaction.rs | 8 +-- src/smt/store/smt_store.rs | 8 +-- src/smt/tests/smt.rs | 2 +- src/utils/error.rs | 44 +++++++------- src/utils/helper.rs | 2 +- 46 files changed, 268 insertions(+), 281 deletions(-) diff --git a/src/api.rs b/src/api.rs index 9a0f08c..1e458ca 100644 --- a/src/api.rs +++ b/src/api.rs @@ -277,10 +277,10 @@ pub async fn get_issuer_info(params: Params) -> Result { lock_hash, } = FetchIssuerReq::from_map(&map).map_err(rpc_err)?; let lock_hash_ = if lock_script.is_some() { - blake2b_256(&lock_script.unwrap()) + blake2b_256(lock_script.unwrap()) } else if address.is_some() { let lock = script_from_address(address.unwrap()).map_err(rpc_err)?; - blake2b_256(&lock.as_slice()) + blake2b_256(lock.as_slice()) } else { lock_hash.unwrap() }; @@ -350,10 +350,10 @@ pub async fn get_joyid_info(params: Params) -> Result { address, } = FetchJoyIDReq::from_map(&map).map_err(rpc_err)?; let lock_hash = if lock_script.is_some() { - blake2b_256(&lock_script.unwrap()) + blake2b_256(lock_script.unwrap()) } else { let lock = script_from_address(address.unwrap()).map_err(rpc_err)?; - blake2b_256(&lock.as_slice()) + blake2b_256(lock.as_slice()) }; let joyid_info_opt = get_joyid_info_by_lock_hash(lock_hash).map_err(rpc_err)?; parse_joyid_metadata_response(joyid_info_opt, tip_number()?).map_err(rpc_err) diff --git a/src/business/helper.rs b/src/business/helper.rs index ceb9876..7936173 100644 --- a/src/business/helper.rs +++ b/src/business/helper.rs @@ -12,7 +12,7 @@ pub fn address_from_script(slice: &[u8]) -> Result { Err(_e) => false, }; let payload = - AddressPayload::from(Script::from_slice(slice).map_err(|_| Error::CKBScriptError)?); + AddressPayload::from(Script::from_slice(slice).map_err(|_| Error::CKBScriptInvalid)?); let network = if is_mainnet { NetworkType::Mainnet } else { @@ -23,7 +23,7 @@ pub fn address_from_script(slice: &[u8]) -> Result { } pub fn script_from_address(address: String) -> Result { - let addr = Address::from_str(&address).map_err(|e| Error::CKBRPCError(e.to_string()))?; + let addr = Address::from_str(&address).map_err(|e| Error::CKBRPCInvalid(e))?; let payload = addr.payload(); let script = Script::new_builder() .hash_type(payload.hash_type().into()) diff --git a/src/ckb/indexer.rs b/src/ckb/indexer.rs index 879cc9e..41e9109 100644 --- a/src/ckb/indexer.rs +++ b/src/ckb/indexer.rs @@ -23,7 +23,7 @@ pub async fn get_cota_smt_root(lock_script: &[u8]) -> Result, E ret.copy_from_slice(&cell_data[1..]); Ok(Some(ret)) } - _ => Err(Error::CKBIndexerError( + _ => Err(Error::CKBIndexerInvalid( "CoTA cell data length error".to_owned(), )), } @@ -61,10 +61,10 @@ async fn build_rpc(method: &str, params: Option) -> match output { jsonrpc_core::response::Output::Success(success) => { serde_json::from_value::(success.result) - .map_err(|_e| Error::CKBIndexerError("Parse response error".to_owned())) + .map_err(|_e| Error::CKBIndexerInvalid("Parse response error".to_owned())) } jsonrpc_core::response::Output::Failure(failure) => { - Err(Error::CKBIndexerError(failure.error.message)) + Err(Error::CKBIndexerInvalid(failure.error.message)) } } } diff --git a/src/ckb/rpc.rs b/src/ckb/rpc.rs index 7e8847d..611bd0c 100644 --- a/src/ckb/rpc.rs +++ b/src/ckb/rpc.rs @@ -44,8 +44,8 @@ pub async fn get_withdraw_info( let mut client = ckb_node_client()?; let block = client .get_block_by_number(Uint64::from(block_number)) - .map_err(|_e| Error::CKBRPCError("get_block_by_number".to_string()))? - .ok_or(Error::CKBRPCError("get_block error".to_string()))?; + .map_err(|_e| Error::CKBRPCInvalid("get_block_by_number".to_string()))? + .ok_or(Error::CKBRPCInvalid("get_block error".to_string()))?; let block_hash = block.header.hash; let block_number = block.header.inner.number.value(); let mut output_index = Uint32::default(); @@ -56,20 +56,19 @@ pub async fn get_withdraw_info( let position = tx.inner.outputs.clone().into_iter().position(|output| { let lock: Script = output.lock.clone().into(); let lock_ret: bool = blake2b_256(lock.as_slice()) == withdrawal_lock_hash; - let type_ret: bool = output.type_.clone().map_or(false, |type_: RPCScript| { - type_.code_hash.as_bytes() == &cota_code_hash + let type_ret: bool = output.type_.map_or(false, |type_: RPCScript| { + type_.code_hash.as_bytes() == cota_code_hash }); lock_ret && type_ret }); - if position.is_some() { - output_index = - Uint32::from_slice(&(position.unwrap() as u32).to_be_bytes()).unwrap(); + if let Some(pos) = position { + output_index = Uint32::from_slice(&(pos).to_be_bytes()).unwrap(); } - tx.hash.as_bytes() == &withdrawal_tx_hash + tx.hash.as_bytes() == withdrawal_tx_hash }) .collect(); if txs.is_empty() { - return Err(Error::CKBRPCError(format!( + return Err(Error::CKBRPCInvalid(format!( "The tx dose not exist in the block#{:?}", block_number ))); @@ -89,7 +88,7 @@ pub async fn get_withdraw_info( let transaction_proof = client .get_transaction_proof(vec![tx_view.hash], Some(block_hash.clone())) - .map_err(|_e| Error::CKBRPCError("get_transaction_proof".to_string()))?; + .map_err(|_e| Error::CKBRPCInvalid("get_transaction_proof".to_string()))?; let tx_proof = get_tx_proof(transaction_proof); let withdraw_info = WithdrawRawTx { @@ -112,7 +111,7 @@ pub async fn get_node_tip_block_number() -> Result { let mut client = ckb_node_client()?; let block_number = client .get_tip_block_number() - .map_err(|_e| Error::CKBRPCError("get_tip_block_number".to_string()))?; + .map_err(|_e| Error::CKBRPCInvalid("get_tip_block_number".to_string()))?; Ok(u64::from(block_number)) }) .await @@ -124,8 +123,8 @@ pub async fn get_block_timestamp(block_number: u64) -> Result { let mut client = ckb_node_client()?; let header = client .get_header_by_number(BlockNumber::from(block_number)) - .map_err(|_e| Error::CKBRPCError("get_header_by_number".to_string()))? - .ok_or(Error::CKBRPCError("get_header_by_number".to_string()))?; + .map_err(|_e| Error::CKBRPCInvalid("get_header_by_number".to_string()))? + .ok_or(Error::CKBRPCInvalid("get_header_by_number".to_string()))?; Ok(header.inner.timestamp.value()) }) .await diff --git a/src/entries/claim.rs b/src/entries/claim.rs index bb1c5ee..780d52e 100644 --- a/src/entries/claim.rs +++ b/src/entries/claim.rs @@ -114,7 +114,7 @@ pub async fn generate_claim_smt( generate_history_smt(&mut claim_smt, claim_lock_hash, claim_smt_root)?; claim_smt .update_all(claim_update_leaves.clone()) - .map_err(|e| Error::SMTError(e.to_string()))?; + .map_err(|e| Error::SMTInvalid(e.to_string()))?; claim_smt.save_root_and_leaves(previous_leaves.clone())?; claim_smt.commit() })?; @@ -122,11 +122,11 @@ pub async fn generate_claim_smt( let leaf_keys: Vec = claim_update_leaves.iter().map(|leave| leave.0).collect(); let claim_merkle_proof = claim_smt.merkle_proof(leaf_keys.clone()).map_err(|e| { error!("Claim SMT proof error: {:?}", e.to_string()); - Error::SMTProofError("Claim".to_string()) + Error::SMTProofInvalid("Claim".to_string()) })?; let claim_merkle_proof_compiled = claim_merkle_proof.compile(leaf_keys).map_err(|e| { error!("Claim SMT proof error: {:?}", e.to_string()); - Error::SMTProofError("Claim".to_string()) + Error::SMTProofInvalid("Claim".to_string()) })?; let merkel_proof_vec: Vec = claim_merkle_proof_compiled.into(); diff --git a/src/entries/claim_update.rs b/src/entries/claim_update.rs index 30c23d2..58ebf1c 100644 --- a/src/entries/claim_update.rs +++ b/src/entries/claim_update.rs @@ -39,7 +39,7 @@ pub async fn generate_claim_update_smt( } let is_receiver = sender_withdrawals .iter() - .any(|withdrawal| &withdrawal.receiver_lock_script == &claim_lock_script); + .any(|withdrawal| withdrawal.receiver_lock_script == claim_lock_script); if !is_receiver { return Err(Error::CotaIdAndTokenIndexHasNotWithdrawn); } @@ -127,7 +127,7 @@ pub async fn generate_claim_update_smt( generate_history_smt(&mut claim_smt, claim_lock_hash, claim_smt_root)?; claim_smt .update_all(claim_update_leaves.clone()) - .map_err(|e| Error::SMTError(e.to_string()))?; + .map_err(|e| Error::SMTInvalid(e.to_string()))?; claim_smt.save_root_and_leaves(previous_leaves.clone())?; claim_smt.commit() })?; @@ -135,12 +135,12 @@ pub async fn generate_claim_update_smt( let leaf_keys: Vec = claim_update_leaves.iter().map(|leave| leave.0).collect(); let claim_update_merkle_proof = claim_smt.merkle_proof(leaf_keys.clone()).map_err(|e| { error!("Claim update SMT proof error: {:?}", e.to_string()); - Error::SMTProofError("ClaimUpdate".to_string()) + Error::SMTProofInvalid("ClaimUpdate".to_string()) })?; let claim_update_merkle_proof_compiled = claim_update_merkle_proof.compile(leaf_keys).map_err(|e| { error!("Claim update SMT proof error: {:?}", e.to_string()); - Error::SMTProofError("ClaimUpdate".to_string()) + Error::SMTProofInvalid("ClaimUpdate".to_string()) })?; let merkel_proof_vec: Vec = claim_update_merkle_proof_compiled.into(); diff --git a/src/entries/define.rs b/src/entries/define.rs index fdcca3e..f823a0e 100644 --- a/src/entries/define.rs +++ b/src/entries/define.rs @@ -39,7 +39,7 @@ pub async fn generate_define_smt( with_lock(lock_hash, || { generate_history_smt(&mut smt, lock_hash, smt_root)?; smt.update(key, value) - .map_err(|e| Error::SMTError(e.to_string()))?; + .map_err(|e| Error::SMTInvalid(e.to_string()))?; smt.save_root_and_leaves(previous_leaves.clone())?; smt.commit() })?; @@ -47,11 +47,11 @@ pub async fn generate_define_smt( let leaf_keys: Vec = update_leaves.iter().map(|leave| leave.0).collect(); let define_merkle_proof = smt.merkle_proof(leaf_keys.clone()).map_err(|e| { error!("Define SMT proof error: {:?}", e.to_string()); - Error::SMTProofError("Mint".to_string()) + Error::SMTProofInvalid("Mint".to_string()) })?; let define_merkle_proof_compiled = define_merkle_proof.compile(leaf_keys).map_err(|e| { error!("Define SMT proof error: {:?}", e.to_string()); - Error::SMTProofError("Define".to_string()) + Error::SMTProofInvalid("Define".to_string()) })?; let merkel_proof_vec: Vec = define_merkle_proof_compiled.into(); diff --git a/src/entries/extension.rs b/src/entries/extension.rs index 1f69e21..cf4fc68 100644 --- a/src/entries/extension.rs +++ b/src/entries/extension.rs @@ -52,7 +52,7 @@ pub async fn generate_ext_subkey_smt( if let Some(leaf) = leaf_opt { previous_leaves.push((key, H256::from(leaf.value))); } else { - return Err(Error::CKBRPCError( + return Err(Error::CKBRPCInvalid( "Extension old subkey value does not exist".to_string(), )); } @@ -67,7 +67,7 @@ pub async fn generate_ext_subkey_smt( with_lock(lock_hash, || { generate_history_smt(&mut smt, lock_hash, smt_root)?; smt.update_all(update_leaves.clone()) - .map_err(|e| Error::SMTError(e.to_string()))?; + .map_err(|e| Error::SMTInvalid(e.to_string()))?; smt.save_root_and_leaves(previous_leaves.clone())?; smt.commit() })?; @@ -75,12 +75,12 @@ pub async fn generate_ext_subkey_smt( let leaf_keys: Vec = update_leaves.iter().map(|leave| leave.0).collect(); let extension_merkle_proof = smt.merkle_proof(leaf_keys.clone()).map_err(|e| { error!("Extension subkey SMT proof error: {:?}", e.to_string()); - Error::SMTProofError("Extension subkey".to_string()) + Error::SMTProofInvalid("Extension subkey".to_string()) })?; let extension_merkle_proof_compiled = extension_merkle_proof.compile(leaf_keys).map_err(|e| { error!("Extension subkey SMT proof error: {:?}", e.to_string()); - Error::SMTProofError("Extension subkey".to_string()) + Error::SMTProofInvalid("Extension subkey".to_string()) })?; let merkel_proof_vec: Vec = extension_merkle_proof_compiled.into(); @@ -146,7 +146,7 @@ pub async fn generate_ext_social_smt( if let Some(leaf) = leaf_opt { previous_leaves.push((key, H256::from(leaf.value))); } else { - return Err(Error::CKBRPCError( + return Err(Error::CKBRPCInvalid( "Extension old social value does not exist".to_string(), )); } @@ -160,7 +160,7 @@ pub async fn generate_ext_social_smt( with_lock(lock_hash, || { generate_history_smt(&mut smt, lock_hash, smt_root)?; smt.update_all(update_leaves.clone()) - .map_err(|e| Error::SMTError(e.to_string()))?; + .map_err(|e| Error::SMTInvalid(e.to_string()))?; smt.save_root_and_leaves(previous_leaves.clone())?; smt.commit() })?; @@ -168,12 +168,12 @@ pub async fn generate_ext_social_smt( let leaf_keys: Vec = update_leaves.iter().map(|leave| leave.0).collect(); let extension_merkle_proof = smt.merkle_proof(leaf_keys.clone()).map_err(|e| { error!("Extension social SMT proof error: {:?}", e.to_string()); - Error::SMTProofError("Extension social".to_string()) + Error::SMTProofInvalid("Extension social".to_string()) })?; let extension_merkle_proof_compiled = extension_merkle_proof.compile(leaf_keys).map_err(|e| { error!("Extension social SMT proof error: {:?}", e.to_string()); - Error::SMTProofError("Extension social".to_string()) + Error::SMTProofInvalid("Extension social".to_string()) })?; let merkel_proof_vec: Vec = extension_merkle_proof_compiled.into(); @@ -245,17 +245,17 @@ pub async fn generate_adding_subkey_smt( let transaction = &StoreTransaction::new(ROCKS_DB.transaction()); let mut smt = init_smt(transaction, lock_hash)?; smt.update_all(update_leaves.clone()) - .map_err(|e| Error::SMTError(e.to_string()))?; + .map_err(|e| Error::SMTInvalid(e.to_string()))?; let leaf_keys: Vec = update_leaves.iter().map(|leave| leave.0).collect(); let extension_merkle_proof = smt.merkle_proof(leaf_keys.clone()).map_err(|e| { error!("Extension subkey SMT proof error: {:?}", e.to_string()); - Error::SMTProofError("Extension subkey".to_string()) + Error::SMTProofInvalid("Extension subkey".to_string()) })?; let extension_merkle_proof_compiled = extension_merkle_proof.compile(leaf_keys).map_err(|e| { error!("Extension subkey SMT proof error: {:?}", e.to_string()); - Error::SMTProofError("Extension subkey".to_string()) + Error::SMTProofInvalid("Extension subkey".to_string()) })?; let merkel_proof_vec: Vec = extension_merkle_proof_compiled.into(); diff --git a/src/entries/helper.rs b/src/entries/helper.rs index ef23069..4f4a4f4 100644 --- a/src/entries/helper.rs +++ b/src/entries/helper.rs @@ -243,12 +243,9 @@ fn generate_social_leaf_value( recovery_mode: &u8, must: &u8, total: &u8, - signers: &Vec>, + signers: &[Vec], ) -> (SocialValue, H256) { - let friends: Vec = signers - .into_iter() - .map(|signer| vec_to_bytes(&signer)) - .collect(); + let friends: Vec = signers.iter().map(|signer| vec_to_bytes(signer)).collect(); let signers = LockScriptVecBuilder::default().set(friends).build(); let social_value = SocialValueBuilder::default() .recovery_mode(Byte::from_slice(&[*recovery_mode]).unwrap()) @@ -271,7 +268,7 @@ pub fn with_lock(lock_hash: [u8; 32], mut operator: F) -> Result<(), Error> where F: FnMut() -> Result<(), Error>, { - let &(ref lock, ref cond) = &*Arc::clone(&SMT_LOCK); + let (lock, cond) = &*Arc::clone(&SMT_LOCK); { let mut set = lock.lock(); while !set.insert(lock_hash) { diff --git a/src/entries/mint.rs b/src/entries/mint.rs index 20f5eef..9fadfb8 100644 --- a/src/entries/mint.rs +++ b/src/entries/mint.rs @@ -111,7 +111,7 @@ pub async fn generate_mint_smt(mint_req: MintReq) -> Result<(H256, MintCotaNFTV1 with_lock(lock_hash, || { generate_history_smt(&mut smt, lock_hash, smt_root)?; smt.update_all(update_leaves.clone()) - .map_err(|e| Error::SMTError(e.to_string()))?; + .map_err(|e| Error::SMTInvalid(e.to_string()))?; smt.save_root_and_leaves(previous_leaves.clone())?; smt.commit() })?; @@ -120,11 +120,11 @@ pub async fn generate_mint_smt(mint_req: MintReq) -> Result<(H256, MintCotaNFTV1 let leaf_keys: Vec = update_leaves.iter().map(|leave| leave.0).collect(); let mint_merkle_proof = smt.merkle_proof(leaf_keys.clone()).map_err(|e| { error!("Mint SMT proof error: {:?}", e.to_string()); - Error::SMTProofError("Mint".to_string()) + Error::SMTProofInvalid("Mint".to_string()) })?; let mint_merkle_proof_compiled = mint_merkle_proof.compile(leaf_keys).map_err(|e| { error!("Mint SMT proof error: {:?}", e.to_string()); - Error::SMTProofError("Mint".to_string()) + Error::SMTProofInvalid("Mint".to_string()) })?; diff_time(start_time, "Generate mint smt proof"); diff --git a/src/entries/sequential_transfer.rs b/src/entries/sequential_transfer.rs index 8ebdcf8..474401b 100644 --- a/src/entries/sequential_transfer.rs +++ b/src/entries/sequential_transfer.rs @@ -58,7 +58,7 @@ pub async fn generate_sequential_transfer_smt( if index == transfers_len - 1 { current_withdrawal = withdrawals.first().unwrap().clone(); - if ¤t_withdrawal.receiver_lock_script != &transfer_lock_script { + if current_withdrawal.receiver_lock_script != transfer_lock_script { return Err(Error::CotaIdAndTokenIndexHasNotWithdrawn); } let is_claimed = is_exist_in_claim( @@ -145,12 +145,12 @@ pub async fn generate_sequential_transfer_smt( generate_history_smt(&mut transfer_smt, transfer_lock_hash, transfer_smt_root)?; transfer_smt .update_all(transfer_pervious_leaves.clone()) - .map_err(|e| Error::SMTError(e.to_string()))?; + .map_err(|e| Error::SMTInvalid(e.to_string()))?; current_subkey_entries = generate_subkey_smt(transfer_lock_hash, &subkey_opt, &transfer_smt)?; transfer_smt .update_all(transfer_update_leaves.clone()) - .map_err(|e| Error::SMTError(e.to_string()))?; + .map_err(|e| Error::SMTInvalid(e.to_string()))?; next_subkey_entries = generate_subkey_smt(transfer_lock_hash, &subkey_opt, &transfer_smt)?; transfer_smt.save_root_and_leaves(previous_leaves.clone())?; transfer_smt.commit() @@ -159,10 +159,10 @@ pub async fn generate_sequential_transfer_smt( let leaf_keys: Vec = transfer_update_leaves.iter().map(|leave| leave.0).collect(); let transfer_merkle_proof = transfer_smt .merkle_proof(leaf_keys.clone()) - .map_err(|_e| Error::SMTProofError("Transfer".to_string()))?; + .map_err(|_e| Error::SMTProofInvalid("Transfer".to_string()))?; let transfer_merkle_proof_compiled = transfer_merkle_proof .compile(leaf_keys) - .map_err(|_e| Error::SMTProofError("Transfer".to_string()))?; + .map_err(|_e| Error::SMTProofInvalid("Transfer".to_string()))?; let transfer_merkel_proof_vec: Vec = transfer_merkle_proof_compiled.into(); let transfer_merkel_proof_bytes = BytesBuilder::default() @@ -238,10 +238,10 @@ fn generate_subkey_smt( let subkey_merkle_proof = smt .merkle_proof(vec![key]) - .map_err(|_| Error::SMTProofError("Subkey unlock".to_owned()))?; + .map_err(|_| Error::SMTProofInvalid("Subkey unlock".to_owned()))?; let subkey_merkle_proof_compiled = subkey_merkle_proof .compile(vec![key]) - .map_err(|_| Error::SMTProofError("Subkey unlock".to_owned()))?; + .map_err(|_| Error::SMTProofInvalid("Subkey unlock".to_owned()))?; let merkel_proof_vec: Vec = subkey_merkle_proof_compiled.into(); let merkel_proof_bytes = joyid_smt::common::BytesBuilder::default() diff --git a/src/entries/smt.rs b/src/entries/smt.rs index d6d08db..4753c17 100644 --- a/src/entries/smt.rs +++ b/src/entries/smt.rs @@ -22,21 +22,18 @@ use cota_smt::smt::H256; use log::debug; use std::collections::HashMap; -pub fn init_smt<'a>( - transaction: &'a StoreTransaction, - lock_hash: [u8; 32], -) -> Result, Error> { +pub fn init_smt(transaction: &StoreTransaction, lock_hash: [u8; 32]) -> Result { let smt_store = SMTStore::new( lock_hash, COLUMN_SMT_LEAF, COLUMN_SMT_BRANCH, COLUMN_SMT_ROOT, COLUMN_SMT_TEMP_LEAVES, - &transaction, + transaction, ); let root = smt_store .get_root() - .map_err(|_e| Error::SMTError("Get smt root".to_string()))? + .map_err(|_e| Error::SMTInvalid("Get smt root".to_string()))? .unwrap_or_default(); debug!( "rocksdb smt root: {:?} of {:?}", @@ -46,8 +43,8 @@ pub fn init_smt<'a>( Ok(CotaSMT::new(root, smt_store)) } -pub fn generate_history_smt<'a>( - smt: &mut CotaSMT<'a>, +pub fn generate_history_smt( + smt: &mut CotaSMT, lock_hash: [u8; 32], smt_root_opt: Option<[u8; 32]>, ) -> Result<(), Error> { @@ -76,7 +73,7 @@ pub fn generate_history_smt<'a>( generate_mysql_smt(smt, lock_hash) } -pub fn generate_mysql_smt<'a>(smt: &mut CotaSMT<'a>, lock_hash: [u8; 32]) -> Result<(), Error> { +pub fn generate_mysql_smt(smt: &mut CotaSMT, lock_hash: [u8; 32]) -> Result<(), Error> { let start_time = Local::now().timestamp_millis(); let (defines, holds, withdrawals, claims, extension_leaves) = get_all_cota_by_lock_hash(lock_hash)?; @@ -175,7 +172,6 @@ pub fn generate_mysql_smt<'a>(smt: &mut CotaSMT<'a>, lock_hash: [u8; 32]) -> Res let value = H256::from(extension_leaf.value); leaves.push((key, value)); } - debug!("history leaves count: {}", leaves.len()); if !leaves.is_empty() { smt.update_all(leaves).expect("SMT update leave error"); } @@ -184,7 +180,7 @@ pub fn generate_mysql_smt<'a>(smt: &mut CotaSMT<'a>, lock_hash: [u8; 32]) -> Res Ok(()) } -fn reset_smt_temp_leaves<'a>(smt: &mut CotaSMT<'a>) -> Result<(), Error> { +fn reset_smt_temp_leaves(smt: &mut CotaSMT) -> Result<(), Error> { let leaves_opt = smt.store().get_leaves()?; if let Some(leaves) = leaves_opt { smt.update_all(leaves) diff --git a/src/entries/social.rs b/src/entries/social.rs index de8f806..52ae54a 100644 --- a/src/entries/social.rs +++ b/src/entries/social.rs @@ -46,11 +46,11 @@ pub async fn generate_social_unlock_smt( let social_merkle_proof = smt.merkle_proof(vec![key]).map_err(|e| { error!("Social unlock SMT proof error: {:?}", e.to_string()); - Error::SMTProofError("Social unlock".to_string()) + Error::SMTProofInvalid("Social unlock".to_string()) })?; let social_merkle_proof_compiled = social_merkle_proof.compile(vec![key]).map_err(|e| { error!("Social unlock SMT proof error: {:?}", e.to_string()); - Error::SMTProofError("Social unlock".to_string()) + Error::SMTProofInvalid("Social unlock".to_string()) })?; let merkel_proof_vec: Vec = social_merkle_proof_compiled.into(); @@ -71,7 +71,7 @@ fn generate_social_friends(friends: Vec) -> Result) -> Result = subkey_merkle_proof_compiled.into(); diff --git a/src/entries/subkey.rs b/src/entries/subkey.rs index c5a2d3b..51603d3 100644 --- a/src/entries/subkey.rs +++ b/src/entries/subkey.rs @@ -44,11 +44,11 @@ pub async fn generate_subkey_unlock_smt( let subkey_merkle_proof = smt.merkle_proof(vec![key]).map_err(|e| { error!("Subkey unlock SMT proof error: {:?}", e.to_string()); - Error::SMTProofError("Subkey unlock".to_string()) + Error::SMTProofInvalid("Subkey unlock".to_string()) })?; let subkey_merkle_proof_compiled = subkey_merkle_proof.compile(vec![key]).map_err(|e| { error!("Subkey unlock SMT proof error: {:?}", e.to_string()); - Error::SMTProofError("Subkey unlock".to_string()) + Error::SMTProofInvalid("Subkey unlock".to_string()) })?; let merkel_proof_vec: Vec = subkey_merkle_proof_compiled.into(); diff --git a/src/entries/transfer.rs b/src/entries/transfer.rs index fa2af2b..d540927 100644 --- a/src/entries/transfer.rs +++ b/src/entries/transfer.rs @@ -44,7 +44,7 @@ pub async fn generate_transfer_smt( } let is_receiver = sender_withdrawals .iter() - .any(|withdrawal| &withdrawal.receiver_lock_script == &transfer_lock_script); + .any(|withdrawal| withdrawal.receiver_lock_script == transfer_lock_script); if !is_receiver { return Err(Error::CotaIdAndTokenIndexHasNotWithdrawn); } @@ -132,7 +132,7 @@ pub async fn generate_transfer_smt( generate_history_smt(&mut transfer_smt, transfer_lock_hash, transfer_smt_root)?; transfer_smt .update_all(transfer_update_leaves.clone()) - .map_err(|e| Error::SMTError(e.to_string()))?; + .map_err(|e| Error::SMTInvalid(e.to_string()))?; transfer_smt.save_root_and_leaves(previous_leaves.clone())?; transfer_smt.commit() })?; @@ -141,11 +141,11 @@ pub async fn generate_transfer_smt( let leaf_keys: Vec = transfer_update_leaves.iter().map(|leave| leave.0).collect(); let transfer_merkle_proof = transfer_smt.merkle_proof(leaf_keys.clone()).map_err(|e| { error!("Transfer SMT proof error: {:?}", e.to_string()); - Error::SMTProofError("Transfer".to_string()) + Error::SMTProofInvalid("Transfer".to_string()) })?; let transfer_merkle_proof_compiled = transfer_merkle_proof.compile(leaf_keys).map_err(|e| { error!("Transfer SMT proof error: {:?}", e.to_string()); - Error::SMTProofError("Transfer".to_string()) + Error::SMTProofInvalid("Transfer".to_string()) })?; diff_time(start_time, "Generate transfer smt proof"); diff --git a/src/entries/transfer_update.rs b/src/entries/transfer_update.rs index 5a45595..5e2a10c 100644 --- a/src/entries/transfer_update.rs +++ b/src/entries/transfer_update.rs @@ -43,7 +43,7 @@ pub async fn generate_transfer_update_smt( } let is_receiver = sender_withdrawals .iter() - .any(|withdrawal| &withdrawal.receiver_lock_script == &transfer_lock_script); + .any(|withdrawal| withdrawal.receiver_lock_script == transfer_lock_script); if !is_receiver { return Err(Error::CotaIdAndTokenIndexHasNotWithdrawn); } @@ -151,7 +151,7 @@ pub async fn generate_transfer_update_smt( )?; transfer_update_smt .update_all(transfer_update_leaves.clone()) - .map_err(|e| Error::SMTError(e.to_string()))?; + .map_err(|e| Error::SMTInvalid(e.to_string()))?; transfer_update_smt.save_root_and_leaves(previous_leaves.clone())?; transfer_update_smt.commit() })?; @@ -161,13 +161,13 @@ pub async fn generate_transfer_update_smt( .merkle_proof(leaf_keys.clone()) .map_err(|e| { error!("Transfer update SMT proof error: {:?}", e.to_string()); - Error::SMTProofError("Transfer".to_string()) + Error::SMTProofInvalid("Transfer".to_string()) })?; let transfer_update_merkle_proof_compiled = transfer_update_merkle_proof .compile(leaf_keys) .map_err(|e| { error!("Transfer SMT proof error: {:?}", e.to_string()); - Error::SMTProofError("Transfer update".to_string()) + Error::SMTProofInvalid("Transfer update".to_string()) })?; let transfer_update_merkel_proof_vec: Vec = transfer_update_merkle_proof_compiled.into(); diff --git a/src/entries/update.rs b/src/entries/update.rs index 6890f2a..58a9b30 100644 --- a/src/entries/update.rs +++ b/src/entries/update.rs @@ -55,7 +55,7 @@ pub async fn generate_update_smt( with_lock(lock_hash, || { generate_history_smt(&mut smt, lock_hash, smt_root)?; smt.update_all(update_leaves.clone()) - .map_err(|e| Error::SMTError(e.to_string()))?; + .map_err(|e| Error::SMTInvalid(e.to_string()))?; smt.save_root_and_leaves(previous_leaves.clone())?; smt.commit() })?; @@ -63,11 +63,11 @@ pub async fn generate_update_smt( let leaf_keys: Vec = update_leaves.iter().map(|leave| leave.0).collect(); let update_merkle_proof = smt.merkle_proof(leaf_keys.clone()).map_err(|e| { error!("Update SMT proof error: {:?}", e.to_string()); - Error::SMTProofError("Update".to_string()) + Error::SMTProofInvalid("Update".to_string()) })?; let update_merkle_proof_compiled = update_merkle_proof.compile(leaf_keys).map_err(|e| { error!("Update SMT proof error: {:?}", e.to_string()); - Error::SMTProofError("Update".to_string()) + Error::SMTProofInvalid("Update".to_string()) })?; let merkel_proof_vec: Vec = update_merkle_proof_compiled.into(); diff --git a/src/entries/withdrawal.rs b/src/entries/withdrawal.rs index 527f9d2..3a036dc 100644 --- a/src/entries/withdrawal.rs +++ b/src/entries/withdrawal.rs @@ -78,7 +78,7 @@ pub async fn generate_withdrawal_smt( with_lock(lock_hash, || { generate_history_smt(&mut smt, lock_hash, smt_root)?; smt.update_all(update_leaves.clone()) - .map_err(|e| Error::SMTError(e.to_string()))?; + .map_err(|e| Error::SMTInvalid(e.to_string()))?; smt.save_root_and_leaves(previous_leaves.clone())?; smt.commit() })?; @@ -86,12 +86,12 @@ pub async fn generate_withdrawal_smt( let leaf_keys: Vec = update_leaves.iter().map(|leave| leave.0).collect(); let withdrawal_merkle_proof = smt.merkle_proof(leaf_keys.clone()).map_err(|e| { error!("Withdraw SMT proof error: {:?}", e.to_string()); - Error::SMTProofError("Withdraw".to_string()) + Error::SMTProofInvalid("Withdraw".to_string()) })?; let withdrawal_merkle_proof_compiled = withdrawal_merkle_proof.compile(leaf_keys).map_err(|e| { error!("Withdraw SMT proof error: {:?}", e.to_string()); - Error::SMTProofError("Withdraw".to_string()) + Error::SMTProofInvalid("Withdraw".to_string()) })?; let merkel_proof_vec: Vec = withdrawal_merkle_proof_compiled.into(); diff --git a/src/entries/witness.rs b/src/entries/witness.rs index 272b5d3..2d986cc 100644 --- a/src/entries/witness.rs +++ b/src/entries/witness.rs @@ -43,7 +43,7 @@ pub fn parse_witness_withdraw_proof( continue; } let input_type = input_type_opt.to_opt().unwrap().raw_data(); - match u8::from(input_type[0]) { + match input_type[0] { MINT => { if let Ok(entries_v0) = MintCotaNFTEntries::from_slice(&input_type[1..]) { let withdrawal_keys = entries_v0.withdrawal_keys(); @@ -81,7 +81,7 @@ pub fn parse_witness_withdraw_proof( entries_v1.proof().raw_data().to_vec(), ); } - return Err(Error::WitnessParseError( + return Err(Error::WitnessParseInvalid( "Mint witness parse error".to_string(), )); } @@ -107,7 +107,7 @@ pub fn parse_witness_withdraw_proof( entries_v1.proof().raw_data().to_vec(), ); } - return Err(Error::WitnessParseError( + return Err(Error::WitnessParseInvalid( "Withdraw witness parse error".to_string(), )); } @@ -142,7 +142,7 @@ pub fn parse_witness_withdraw_proof( entries_v2.proof().raw_data().to_vec(), ); } - return Err(Error::WitnessParseError( + return Err(Error::WitnessParseInvalid( "Transfer witness parse error".to_string(), )); } @@ -183,7 +183,7 @@ pub fn parse_witness_withdraw_proof( entries_v2.proof().raw_data().to_vec(), ); } - return Err(Error::WitnessParseError( + return Err(Error::WitnessParseInvalid( "Transfer-update witness parse error".to_string(), )); } @@ -193,14 +193,14 @@ pub fn parse_witness_withdraw_proof( continue; } } - Err(Error::WitnessParseError( + Err(Error::WitnessParseInvalid( "Match cota_id and token_index error".to_string(), )) } fn match_cota_id_index(id: &CotaNFTId, pairs: Pairs) -> bool { - pairs.to_vec().into_iter().any(|(cota_id, token_index)| { - &cota_id == id.cota_id().as_slice() && &token_index == id.index().as_slice() + pairs.into_iter().any(|(cota_id, token_index)| { + cota_id == id.cota_id().as_slice() && token_index == id.index().as_slice() }) } @@ -277,7 +277,7 @@ fn parse_withdraw_v0( } } if count != pairs.len() { - return Err(Error::WitnessParseError( + return Err(Error::WitnessParseInvalid( "Match cota_id and token_index error".to_string(), )); } @@ -310,7 +310,7 @@ fn parse_withdraw_v1( } if count != pairs.len() { - return Err(Error::WitnessParseError( + return Err(Error::WitnessParseInvalid( "Match cota_id and token_index error".to_string(), )); } @@ -321,7 +321,7 @@ fn parse_sub_proof(proof: Vec, all_leaves: Leaves) -> Result { let merkel_proof = CompiledMerkleProof(proof); let compiled_proof = merkel_proof .extract_proof::(all_leaves) - .map_err(|e| Error::SMTProofError(e.to_string()))?; + .map_err(|e| Error::SMTProofInvalid(e.to_string()))?; let sub_merkel_proof: Vec = compiled_proof.into(); let sub_proof = BytesBuilder::default() .extend(sub_merkel_proof.iter().map(|v| Byte::from(*v))) diff --git a/src/models/block.rs b/src/models/block.rs index 3d0363f..3e3125a 100644 --- a/src/models/block.rs +++ b/src/models/block.rs @@ -14,7 +14,7 @@ pub fn get_syncer_tip_block_number() -> Result { .first::(&get_conn()) .map_err(|e| { error!("Query tip block number error: {}", e.to_string()); - Error::DatabaseQueryError(e.to_string()) + Error::DatabaseQueryInvalid(e.to_string()) }) } @@ -28,7 +28,7 @@ pub fn get_syncer_tip_block_numbers() -> Result<(u64, u64), Error> { .first::(&get_conn()) .map_err(|e| { error!("Query tip metadata number error: {}", e.to_string()); - Error::DatabaseQueryError(e.to_string()) + Error::DatabaseQueryInvalid(e.to_string()) })?; Ok((tip_block_number, tip_metadata_number)) } diff --git a/src/models/claim.rs b/src/models/claim.rs index d808865..da8772e 100644 --- a/src/models/claim.rs +++ b/src/models/claim.rs @@ -41,7 +41,7 @@ pub fn get_claim_cota_by_lock_hash(lock_hash_: [u8; 32]) -> DBResult { .map_or_else( |e| { error!("Query claim error: {}", e.to_string()); - Err(Error::DatabaseQueryError(e.to_string())) + Err(Error::DatabaseQueryInvalid(e.to_string())) }, |claims| Ok(parse_claimed_cota_nft(claims)), )?; @@ -71,7 +71,7 @@ pub fn is_exist_in_claim( let out_point_crc_u32 = generate_crc(out_point_hex.as_bytes()); claimed_cota_nft_kv_pairs .filter(lock_hash_crc.eq(lock_hash_crc_)) - .filter(lock_hash.eq(lock_hash_hex.clone())) + .filter(lock_hash.eq(lock_hash_hex)) .filter(cota_id_crc.eq(cota_id_crc_u32)) .filter(cota_id.eq(cota_id_hex)) .filter(token_index.eq(token_index_u32)) diff --git a/src/models/class.rs b/src/models/class.rs index 7723de7..ee02414 100644 --- a/src/models/class.rs +++ b/src/models/class.rs @@ -58,7 +58,7 @@ pub fn get_class_info_by_cota_id(cota_id_: [u8; 20]) -> Result .map_or_else( |e| { error!("Query class info error: {}", e.to_string()); - Err(Error::DatabaseQueryError(e.to_string())) + Err(Error::DatabaseQueryInvalid(e.to_string())) }, Ok, )?; @@ -98,7 +98,7 @@ pub fn get_class_audios_by_cota_id(cota_id_hex: String) -> Result get_withdrawal_cota_by_script_id(script_id, cota_id_opt), None => Ok((vec![], 0, 0)), }?; - withdrawal_nfts = withdrawal_nfts - .into_iter() - .filter(|withdrawal| { - !is_exist_in_claim( - lock_hash, - withdrawal.cota_id, - withdrawal.token_index, - withdrawal.out_point, - ) - }) - .collect(); + withdrawal_nfts.retain(|withdrawal| { + !is_exist_in_claim( + lock_hash, + withdrawal.cota_id, + withdrawal.token_index, + withdrawal.out_point, + ) + }); let total = withdrawal_nfts.len() as i64; let mut nfts: Vec<(WithdrawNFTDb, Option)> = vec![]; diff --git a/src/models/define.rs b/src/models/define.rs index 81241c8..6b9f7da 100644 --- a/src/models/define.rs +++ b/src/models/define.rs @@ -47,7 +47,7 @@ pub fn get_define_cota_by_lock_hash(lock_hash_: [u8; 32]) -> DBResult .map_or_else( |e| { error!("Query define error: {}", e.to_string()); - Err(Error::DatabaseQueryError(e.to_string())) + Err(Error::DatabaseQueryInvalid(e.to_string())) }, |defines| Ok(parse_define_cota_nft(defines)), )?; @@ -80,12 +80,12 @@ pub fn get_define_cota_by_lock_hash_and_cota_id( .map_or_else( |e| { error!("Query define error: {}", e.to_string()); - Err(Error::DatabaseQueryError(e.to_string())) + Err(Error::DatabaseQueryInvalid(e.to_string())) }, |defines| Ok(parse_define_cota_nft(defines)), )?; diff_time(start_time, "SQL get_define_cota_by_lock_hash_and_cota_id"); - Ok(defines.get(0).map(|v| *v)) + Ok(defines.get(0).copied()) } pub fn get_define_cota_by_cota_id(cota_id_: [u8; 20]) -> Result, Error> { @@ -99,7 +99,7 @@ pub fn get_define_cota_by_cota_id(cota_id_: [u8; 20]) -> Result .map_or_else( |e| { error!("Query define error: {}", e.to_string()); - Err(Error::DatabaseQueryError(e.to_string())) + Err(Error::DatabaseQueryInvalid(e.to_string())) }, |defines| Ok(parse_define_cota_nft(defines)), )?; @@ -117,7 +117,7 @@ pub fn get_lock_hash_by_cota_id(cota_id_: [u8; 20]) -> Result<[u8; 32], Error> { .map_or_else( |e| { error!("Query lock hash by cota id error: {}", e.to_string()); - Err(Error::DatabaseQueryError(e.to_string())) + Err(Error::DatabaseQueryInvalid(e.to_string())) }, |lock_hash_| Ok(parse_bytes_n::<32>(lock_hash_).unwrap()), ) diff --git a/src/models/extension/leaves.rs b/src/models/extension/leaves.rs index 317b100..ea13d59 100644 --- a/src/models/extension/leaves.rs +++ b/src/models/extension/leaves.rs @@ -46,7 +46,7 @@ pub fn get_extension_leaves_by_lock_hash(lock_hash_: [u8; 32]) -> DBResult = extension_kv_pairs .select((key, value)) .filter(lock_hash_crc.eq(lock_hash_crc_)) - .filter(lock_hash.eq(lock_hash_hex.clone())) + .filter(lock_hash.eq(lock_hash_hex)) .filter(key.eq(hex::encode(key_.as_slice()))) .limit(1) .load::(&get_conn()) .map_or_else( |e| { error!("Query extension error: {}", e.to_string()); - Err(Error::DatabaseQueryError(e.to_string())) + Err(Error::DatabaseQueryInvalid(e.to_string())) }, |leaves_| Ok(parse_extension_leaves(leaves_)), )?; diff --git a/src/models/extension/social.rs b/src/models/extension/social.rs index 23f2a5c..c410a14 100644 --- a/src/models/extension/social.rs +++ b/src/models/extension/social.rs @@ -37,7 +37,7 @@ pub fn get_social_config_by_lock(lock_hash_: [u8; 32]) -> Result) -> Vec { fn parse_signers(signers_str: String) -> Vec> { signers_str - .split(",") - .into_iter() + .split(',') .map(|str| hex::decode(str).unwrap()) .collect() } diff --git a/src/models/extension/subkey.rs b/src/models/extension/subkey.rs index 3ec70af..0d1f1de 100644 --- a/src/models/extension/subkey.rs +++ b/src/models/extension/subkey.rs @@ -39,7 +39,7 @@ pub fn get_subkey_by_pubkey_hash( .map_or_else( |e| { error!("Query subkey error: {}", e.to_string()); - Err(Error::DatabaseQueryError(e.to_string())) + Err(Error::DatabaseQueryInvalid(e.to_string())) }, |subkeys_| Ok(parse_subkey(subkeys_)), )?; diff --git a/src/models/hold.rs b/src/models/hold.rs index 8bd0503..5917008 100644 --- a/src/models/hold.rs +++ b/src/models/hold.rs @@ -52,7 +52,7 @@ pub fn get_hold_cota_by_lock_hash( .map_or_else( |e| { error!("Query hold error: {}", e.to_string()); - Err(Error::DatabaseQueryError(e.to_string())) + Err(Error::DatabaseQueryInvalid(e.to_string())) }, |holds| Ok(parse_hold_cota_nfts(holds)), )?; @@ -80,7 +80,7 @@ pub fn get_hold_cota_by_lock_hash( .map_or_else( |e| { error!("Query hold error: {}", e.to_string()); - Err(Error::DatabaseQueryError(e.to_string())) + Err(Error::DatabaseQueryInvalid(e.to_string())) }, |holds| Ok(parse_hold_cota_nfts(holds)), )?; @@ -105,13 +105,13 @@ pub fn get_hold_cota_count_by_lock_hash( let cota_id_str = hex::encode(cota_id_); let hold_count: i64 = hold_cota_nft_kv_pairs .filter(lock_hash_crc.eq(lock_hash_crc_)) - .filter(lock_hash.eq(lock_hash_hex.clone())) + .filter(lock_hash.eq(lock_hash_hex)) .filter(cota_id.eq(cota_id_str)) .count() .get_result::(&get_conn()) .map_err(|e| { error!("Query hold error: {}", e.to_string()); - Error::DatabaseQueryError(e.to_string()) + Error::DatabaseQueryInvalid(e.to_string()) })?; diff_time(start_time, "SQL get_hold_cota_count_by_lock_hash"); Ok(hold_count) @@ -129,12 +129,12 @@ pub fn check_hold_cota_by_lock_hash( .filter(cota_id.eq(cota_id_str)) .filter(token_index.eq(token_index_u32)) .filter(lock_hash_crc.eq(lock_hash_crc_)) - .filter(lock_hash.eq(lock_hash_hex.clone())) + .filter(lock_hash.eq(lock_hash_hex)) .count() .get_result::(&get_conn()) .map_err(|e| { error!("Check hold cota count error: {:?}", e.to_string()); - Error::DatabaseQueryError("Hold".to_string()) + Error::DatabaseQueryInvalid("Hold".to_string()) })?; let is_exist = count > 0; let block_height = get_syncer_tip_block_number()?; @@ -155,19 +155,19 @@ pub fn get_hold_cota_by_lock_hash_and_page( Some(cota_id_) => hold_cota_nft_kv_pairs .filter(lock_hash_crc.eq(lock_hash_crc_)) .filter(lock_hash.eq(lock_hash_hex.clone())) - .filter(cota_id.eq(hex::encode(&cota_id_))) + .filter(cota_id.eq(hex::encode(cota_id_))) .count() .get_result::(conn), None => hold_cota_nft_kv_pairs .filter(lock_hash_crc.eq(lock_hash_crc_)) .filter(lock_hash.eq(lock_hash_hex.clone())) - .filter(cota_id.ne(hex::encode(&[0u8; 20]))) + .filter(cota_id.ne(hex::encode([0u8; 20]))) .count() .get_result::(conn), }; let total: i64 = total_result.map_err(|e| { error!("Query hold error: {}", e.to_string()); - Error::DatabaseQueryError(e.to_string()) + Error::DatabaseQueryInvalid(e.to_string()) })?; let block_height: u64 = get_syncer_tip_block_number()?; @@ -175,8 +175,8 @@ pub fn get_hold_cota_by_lock_hash_and_page( Some(cota_id_) => hold_cota_nft_kv_pairs .select(get_selection()) .filter(lock_hash_crc.eq(lock_hash_crc_)) - .filter(lock_hash.eq(lock_hash_hex.clone())) - .filter(cota_id.eq(hex::encode(&cota_id_))) + .filter(lock_hash.eq(lock_hash_hex)) + .filter(cota_id.eq(hex::encode(cota_id_))) .order(updated_at.desc()) .limit(page_size) .offset(page_size * page) @@ -184,8 +184,8 @@ pub fn get_hold_cota_by_lock_hash_and_page( None => hold_cota_nft_kv_pairs .select(get_selection()) .filter(lock_hash_crc.eq(lock_hash_crc_)) - .filter(lock_hash.eq(lock_hash_hex.clone())) - .filter(cota_id.ne(hex::encode(&[0u8; 20]))) + .filter(lock_hash.eq(lock_hash_hex)) + .filter(cota_id.ne(hex::encode([0u8; 20]))) .order(updated_at.desc()) .limit(page_size) .offset(page_size * page) @@ -194,7 +194,7 @@ pub fn get_hold_cota_by_lock_hash_and_page( let holds: Vec = holds_result.map_or_else( |e| { error!("Query hold error: {}", e.to_string()); - Err(Error::DatabaseQueryError(e.to_string())) + Err(Error::DatabaseQueryInvalid(e.to_string())) }, |holds| Ok(parse_hold_cota_nfts(holds)), )?; diff --git a/src/models/issuer.rs b/src/models/issuer.rs index fa819f9..282e69b 100644 --- a/src/models/issuer.rs +++ b/src/models/issuer.rs @@ -26,7 +26,7 @@ pub fn get_issuer_info_by_lock_hash(lock_hash_: [u8; 32]) -> Result Result Result) -> Result Result, E let lock_code_hash = hex::encode(lock.code_hash().as_slice()); let lock_code_hash_crc = generate_crc(lock_code_hash.as_bytes()); - let lock_args = hex::encode(lock.args().raw_data().to_vec()); + let lock_args = hex::encode(&lock.args().raw_data()); let lock_args_crc = generate_crc(lock_args.as_bytes()); let script_ids: Vec = scripts @@ -79,10 +79,10 @@ pub fn get_script_id_by_lock_script(lock_script: &[u8]) -> Result, E .load::(&get_conn()) .map_err(|e| { error!("Query script error: {}", e.to_string()); - Error::DatabaseQueryError(e.to_string()) + Error::DatabaseQueryInvalid(e.to_string()) })?; diff_time(start_time, "SQL get_script_id_by_lock_script"); - Ok(script_ids.get(0).cloned()) + Ok(script_ids.first().cloned()) } fn parse_script(scripts_: Vec