Skip to content

Commit

Permalink
Merge pull request #103 from nervina-labs/fix-withdrawn-check
Browse files Browse the repository at this point in the history
Fix withdrawn checker
  • Loading branch information
duanyytop authored Jul 5, 2022
2 parents 6736fef + 5c5c585 commit 2114cf8
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cota-aggregator"
version = "0.6.5"
version = "0.6.6"
edition = "2018"

[dependencies]
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -925,6 +925,7 @@ indexer_block_number - The latest block number of ckb-indexer
node_block_number - The latest block number of ckb-node
syncer_block_number - The latest block number of cota-nft-entries-syncer
version - The current version of cota-aggregator
is_mainnet - The environment variable to indicate ckb network
```

```shell
Expand All @@ -934,7 +935,8 @@ version - The current version of cota-aggregator
"indexer_block_number":7561075,
"node_block_number":7561075,
"syncer_block_number":7561075,
"version":"v0.6.5"
"version":"v0.6.5",
"is_mainnet": true
},
"id":2
}
Expand Down
13 changes: 10 additions & 3 deletions src/entries/claim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub async fn generate_claim_smt(
claim_req: ClaimReq,
) -> Result<(H256, ClaimCotaNFTV2Entries, H256), Error> {
let claims = claim_req.claims;
let claim_lock_script = claim_req.lock_script;
let claims_len = claims.len();
if claims_len == 0 {
return Err(Error::RequestParamNotFound("claims".to_string()));
Expand All @@ -37,7 +38,13 @@ pub async fn generate_claim_smt(
if sender_withdrawals.is_empty() || sender_withdrawals.len() != claims_len {
return Err(Error::CotaIdAndTokenIndexHasNotWithdrawn);
}
let claim_lock_hash = blake2b_256(&claim_req.lock_script);
let is_receiver = sender_withdrawals
.iter()
.any(|withdrawal| &withdrawal.receiver_lock_script == &claim_lock_script);
if !is_receiver {
return Err(Error::CotaIdAndTokenIndexHasNotWithdrawn);
}
let claim_lock_hash = blake2b_256(&claim_lock_script);
let is_claimed = sender_withdrawals.iter().any(|withdrawal| {
is_exist_in_claim(
claim_lock_hash,
Expand Down Expand Up @@ -96,9 +103,9 @@ pub async fn generate_claim_smt(
previous_leaves.push((key, H256::zero()));
}

let claim_smt_root = get_cota_smt_root(&claim_req.lock_script).await?;
let claim_smt_root = get_cota_smt_root(&claim_lock_script).await?;

let claim_lock_hash = blake2b_256(&claim_req.lock_script);
let claim_lock_hash = blake2b_256(&claim_lock_script);
let transaction = &StoreTransaction::new(db.transaction());
let mut claim_smt = init_smt(transaction, claim_lock_hash)?;

Expand Down
13 changes: 10 additions & 3 deletions src/entries/claim_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub async fn generate_claim_update_smt(
if nfts_len == 0 {
return Err(Error::RequestParamNotFound("nfts".to_string()));
}
let claim_lock_script = claim_update_req.lock_script;
let cota_id_index_pairs: Vec<([u8; 20], [u8; 4])> = nfts
.iter()
.map(|nft| (nft.cota_id, nft.token_index))
Expand All @@ -37,7 +38,13 @@ pub async fn generate_claim_update_smt(
if sender_withdrawals.is_empty() || sender_withdrawals.len() != nfts_len {
return Err(Error::CotaIdAndTokenIndexHasNotWithdrawn);
}
let claim_lock_hash = blake2b_256(&claim_update_req.lock_script);
let is_receiver = sender_withdrawals
.iter()
.any(|withdrawal| &withdrawal.receiver_lock_script == &claim_lock_script);
if !is_receiver {
return Err(Error::CotaIdAndTokenIndexHasNotWithdrawn);
}
let claim_lock_hash = blake2b_256(&claim_lock_script);
let is_claimed = sender_withdrawals.iter().any(|withdrawal| {
is_exist_in_claim(
claim_lock_hash,
Expand Down Expand Up @@ -110,9 +117,9 @@ pub async fn generate_claim_update_smt(
previous_leaves.push((key, H256::zero()));
}

let claim_smt_root = get_cota_smt_root(&claim_update_req.lock_script).await?;
let claim_smt_root = get_cota_smt_root(&claim_lock_script).await?;

let claim_lock_hash = blake2b_256(&claim_update_req.lock_script);
let claim_lock_hash = blake2b_256(&claim_lock_script);
let transaction = &StoreTransaction::new(db.transaction());
let mut claim_smt = init_smt(transaction, claim_lock_hash)?;
// Add lock to smt
Expand Down
13 changes: 10 additions & 3 deletions src/entries/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub async fn generate_transfer_smt(
transfer_req: TransferReq,
) -> Result<(H256, TransferCotaNFTV2Entries, H256), Error> {
let transfers = transfer_req.transfers;
let transfer_lock_script = transfer_req.lock_script;
let transfers_len = transfers.len();
if transfers_len == 0 {
return Err(Error::RequestParamNotFound("transfers".to_string()));
Expand All @@ -42,7 +43,13 @@ pub async fn generate_transfer_smt(
if sender_withdrawals.is_empty() || sender_withdrawals.len() != transfers_len {
return Err(Error::CotaIdAndTokenIndexHasNotWithdrawn);
}
let transfer_lock_hash = blake2b_256(&transfer_req.lock_script);
let is_receiver = sender_withdrawals
.iter()
.any(|withdrawal| &withdrawal.receiver_lock_script == &transfer_lock_script);
if !is_receiver {
return Err(Error::CotaIdAndTokenIndexHasNotWithdrawn);
}
let transfer_lock_hash = blake2b_256(&transfer_lock_script);
let is_claimed = sender_withdrawals.iter().any(|withdrawal| {
is_exist_in_claim(
transfer_lock_hash,
Expand Down Expand Up @@ -115,10 +122,10 @@ pub async fn generate_transfer_smt(
"Generate transfer smt object with update leaves",
);

let transfer_smt_root = get_cota_smt_root(&transfer_req.lock_script).await?;
let transfer_smt_root = get_cota_smt_root(&transfer_lock_script).await?;

let transaction = &StoreTransaction::new(db.transaction());
let transfer_lock_hash = blake2b_256(&transfer_req.lock_script);
let transfer_lock_hash = blake2b_256(&transfer_lock_script);
let mut transfer_smt = init_smt(transaction, transfer_lock_hash)?;
// Add lock to transfer smt
with_lock(transfer_lock_hash, || {
Expand Down
13 changes: 10 additions & 3 deletions src/entries/transfer_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub async fn generate_transfer_update_smt(
transfer_update_req: TransferUpdateReq,
) -> Result<(H256, TransferUpdateCotaNFTV2Entries, H256), Error> {
let transfers = transfer_update_req.transfers;
let transfer_lock_script = transfer_update_req.lock_script;
let transfers_len = transfers.len();
if transfers_len == 0 {
return Err(Error::RequestParamNotFound("transfers".to_string()));
Expand All @@ -41,7 +42,13 @@ pub async fn generate_transfer_update_smt(
if sender_withdrawals.is_empty() || sender_withdrawals.len() != transfers_len {
return Err(Error::CotaIdAndTokenIndexHasNotWithdrawn);
}
let transfer_lock_hash = blake2b_256(&transfer_update_req.lock_script);
let is_receiver = sender_withdrawals
.iter()
.any(|withdrawal| &withdrawal.receiver_lock_script == &transfer_lock_script);
if !is_receiver {
return Err(Error::CotaIdAndTokenIndexHasNotWithdrawn);
}
let transfer_lock_hash = blake2b_256(&transfer_lock_script);
let is_claimed = sender_withdrawals.iter().any(|withdrawal| {
is_exist_in_claim(
transfer_lock_hash,
Expand Down Expand Up @@ -130,10 +137,10 @@ pub async fn generate_transfer_update_smt(
previous_leaves.push((key, H256::zero()));
}

let transfer_smt_root = get_cota_smt_root(&transfer_update_req.lock_script).await?;
let transfer_smt_root = get_cota_smt_root(&transfer_lock_script).await?;

let transaction = &StoreTransaction::new(db.transaction());
let transfer_lock_hash = blake2b_256(&transfer_update_req.lock_script);
let transfer_lock_hash = blake2b_256(&transfer_lock_script);
let mut transfer_update_smt = init_smt(transaction, transfer_lock_hash)?;
// Add lock to transfer smt
with_lock(transfer_lock_hash, || {
Expand Down
7 changes: 7 additions & 0 deletions src/response/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@ use crate::ckb::rpc::get_node_tip_block_number;
use crate::response::helper::Inserter;
use crate::utils::error::Error;
use jsonrpc_http_server::jsonrpc_core::serde_json::{Map, Value};
use serde_json::from_str;
use std::env;

pub async fn generate_aggregator_info(block_number: u64) -> Result<Map<String, Value>, Error> {
let version = env!("CARGO_PKG_VERSION");
let is_mainnet: bool = match env::var("IS_MAINNET") {
Ok(mainnet) => from_str::<bool>(&mainnet).unwrap(),
Err(_e) => false,
};
let mut map = Map::new();
map.insert_u64("node_block_number", get_node_tip_block_number().await?);
map.insert_u64(
Expand All @@ -14,5 +20,6 @@ pub async fn generate_aggregator_info(block_number: u64) -> Result<Map<String, V
);
map.insert_str("version", format!("v{:}", version));
map.insert_u64("syncer_block_number", block_number);
map.insert("is_mainnet".to_owned(), Value::Bool(is_mainnet));
Ok(map)
}

0 comments on commit 2114cf8

Please sign in to comment.