Skip to content

Commit

Permalink
Merge pull request #55 from nervina-labs/fix-ccid
Browse files Browse the repository at this point in the history
Add account_num to rpc response
  • Loading branch information
duanyytop authored Sep 15, 2022
2 parents 85ba979 + ff4bcd1 commit 8ec3e0d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
12 changes: 10 additions & 2 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub async fn register_rpc(params: Params, db: &RocksDB) -> Result<Value, Error>
info!("Register cota cells request: {:?}", params);
let registries: Vec<Value> = Params::parse(params)?;
let lock_hashes = parse_request_param::<32>(registries).map_err(|err| err.into())?;
let (root_hash, registry_entry) = generate_registry_smt(db, lock_hashes)
let (root_hash, registry_entry, output_account_num) = generate_registry_smt(db, lock_hashes)
.await
.map_err(|err| err.into())?;
let block_number = get_syncer_tip_block_number().map_err(|err| err.into())?;
Expand All @@ -23,6 +23,10 @@ pub async fn register_rpc(params: Params, db: &RocksDB) -> Result<Value, Error>
"registry_smt_entry".to_string(),
Value::String(registry_entry),
);
response.insert(
"output_account_num".to_string(),
Value::Number(Number::from(output_account_num)),
);
response.insert(
"block_number".to_string(),
Value::Number(Number::from(block_number)),
Expand Down Expand Up @@ -51,7 +55,7 @@ pub async fn check_registered_rpc(params: Params) -> Result<Value, Error> {
pub async fn update_registered_ccid_rpc(db: &RocksDB) -> Result<Value, Error> {
info!("Update registered ccid request");
let lock_hashes = get_50_registered_lock_hashes().map_err(|err| err.into())?;
let (root_hash, registry_entry) = generate_registry_smt(db, lock_hashes)
let (root_hash, registry_entry, output_account_num) = generate_registry_smt(db, lock_hashes)
.await
.map_err(|err| err.into())?;
let block_number = get_syncer_tip_block_number().map_err(|err| err.into())?;
Expand All @@ -61,6 +65,10 @@ pub async fn update_registered_ccid_rpc(db: &RocksDB) -> Result<Value, Error> {
"registry_smt_entry".to_string(),
Value::String(registry_entry),
);
response.insert(
"output_account_num".to_string(),
Value::Number(Number::from(output_account_num)),
);
response.insert(
"block_number".to_string(),
Value::Number(Number::from(block_number)),
Expand Down
5 changes: 3 additions & 2 deletions src/smt/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ lazy_static! {
pub async fn generate_registry_smt(
db: &RocksDB,
lock_hashes: Vec<[u8; 32]>,
) -> Result<(String, String), Error> {
) -> Result<(String, String, u64), Error> {
let update_leaves_count = lock_hashes.len();
let registry_state = check_lock_hashes_registered(lock_hashes.clone())?.0;
if registry_state == RegistryState::WithCCID {
Expand All @@ -36,6 +36,7 @@ pub async fn generate_registry_smt(
smt_root,
account_num,
} = get_registry_info().await?;
let output_account_num = account_num + lock_hashes.len() as u64;
for (index, lock_hash) in lock_hashes.into_iter().enumerate() {
let key: H256 = H256::from(lock_hash);
registry_value[0..8].copy_from_slice(&(account_num + index as u64).to_be_bytes());
Expand Down Expand Up @@ -103,7 +104,7 @@ pub async fn generate_registry_smt(

let registry_entry = hex::encode(registry_entries.as_slice());

Ok((root_hash, registry_entry))
Ok((root_hash, registry_entry, output_account_num))
}

fn with_lock<F>(mut operator: F) -> Result<(), Error>
Expand Down

0 comments on commit 8ec3e0d

Please sign in to comment.