Skip to content

Commit

Permalink
Merge pull request #29 from nervina-labs/develop
Browse files Browse the repository at this point in the history
Release v0.4.1
  • Loading branch information
duanyytop authored May 4, 2022
2 parents b74f4bd + 748f475 commit a29ca3e
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 14 deletions.
86 changes: 77 additions & 9 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cota-registry-aggregator"
version = "0.4.0"
version = "0.4.1"
edition = "2018"

[dependencies]
Expand All @@ -20,6 +20,7 @@ serde = { version = "1.0", features = [ "derive" ] }
sparse-merkle-tree = "0.5.3"
ckb-types = "=0.101.8"
ckb-jsonrpc-types = ">=0.103.0"
parking_lot = "0.12.0"
reqwest = { version = "0.11", features = ["json"] }
serde_json = "1.0"
cota-smt = {package = "cota-smt", git = "https://github.com/nervina-labs/cota-smt", tag = "0.1.0"}
1 change: 1 addition & 0 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ pub fn get_syncer_tip_block_number() -> Result<u64, Error> {
let conn = &POOL.clone().get().expect("Mysql pool connection error");
check_infos
.select(block_number)
.order(block_number.desc())
.first::<u64>(conn)
.map_err(|e| {
error!("Query block number error: {}", e.to_string());
Expand Down
14 changes: 13 additions & 1 deletion src/smt/entry.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::db::check_lock_hashes_registered;
use crate::error::Error;
use crate::indexer::index::get_registry_smt_root;
use crate::smt::db::db::RocksDB;
use crate::smt::smt::{generate_history_smt, RootSaver};
use crate::smt::transaction::store_transaction::StoreTransaction;
Expand All @@ -9,7 +10,14 @@ use cota_smt::registry::{
CotaNFTRegistryEntriesBuilder, Registry, RegistryBuilder, RegistryVecBuilder,
};
use cota_smt::smt::H256;
use lazy_static::lazy_static;
use log::info;
use parking_lot::Mutex;
use std::sync::Arc;

lazy_static! {
static ref SMT_LOCK: Arc<Mutex<()>> = Arc::new(Mutex::new(()));
}

pub async fn generate_registry_smt(
db: &RocksDB,
Expand All @@ -28,12 +36,16 @@ pub async fn generate_registry_smt(
previous_leaves.push((key, H256::zero()));
}

let smt_root_opt = get_registry_smt_root().await?;

let lock = SMT_LOCK.lock();
let transaction = StoreTransaction::new(db.transaction());
let mut smt = generate_history_smt(&transaction).await?;
let mut smt = generate_history_smt(&transaction, smt_root_opt)?;
smt.update_all(update_leaves.clone())
.expect("SMT update leave error");
smt.save_root_and_leaves(previous_leaves)?;
transaction.commit()?;
drop(lock);

let root_hash = hex::encode(smt.root().as_slice());
info!("registry_smt_root_hash: {:?}", root_hash);
Expand Down
5 changes: 2 additions & 3 deletions src/smt/smt.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::db::get_registered_lock_hashes;
use crate::error::Error;
use crate::indexer::index::get_registry_smt_root;
use crate::smt::db::schema::{
COLUMN_SMT_BRANCH, COLUMN_SMT_LEAF, COLUMN_SMT_ROOT, COLUMN_SMT_TEMP_LEAVES,
};
Expand Down Expand Up @@ -28,8 +27,9 @@ impl<'a> RootSaver for CotaSMT<'a> {
}
}

pub async fn generate_history_smt<'a>(
pub fn generate_history_smt<'a>(
transaction: &'a StoreTransaction,
smt_root_opt: Option<Vec<u8>>,
) -> Result<CotaSMT<'a>, Error> {
let smt_store = SMTStore::new(
COLUMN_SMT_LEAF,
Expand All @@ -48,7 +48,6 @@ pub async fn generate_history_smt<'a>(
if root == H256::zero() {
return generate_mysql_smt(smt);
}
let smt_root_opt = get_registry_smt_root().await?;
debug!("registry cell smt root: {:?}", smt_root_opt,);
if let Some(smt_root) = smt_root_opt {
if smt_root.as_slice() == root.as_slice() {
Expand Down

0 comments on commit a29ca3e

Please sign in to comment.