Skip to content

Commit

Permalink
Begin indexing mpn addrs
Browse files Browse the repository at this point in the history
  • Loading branch information
keyvank committed Apr 30, 2023
1 parent 9d313fe commit c5f6e0e
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
6 changes: 2 additions & 4 deletions src/blockchain/ops/apply_tx/create_contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,11 @@ pub fn create_contract<K: KvStore>(
}
.into(),
)])?;
let state = state.clone().ok_or(BlockchainError::StateNotGiven)?;
zk::KvStoreStateManager::<CoreZkHasher>::update_contract(
&mut chain.database,
contract_id,
&state
.clone()
.ok_or(BlockchainError::StateNotGiven)?
.as_delta(),
&state.as_delta(),
1,
)?;
if zk::KvStoreStateManager::<CoreZkHasher>::root(&mut chain.database, contract_id)?
Expand Down
35 changes: 34 additions & 1 deletion src/blockchain/ops/apply_tx/update_contract/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,34 @@ mod function_call;
mod withdraw;

use super::*;
use crate::crypto::jubjub;
use crate::zk::ZkScalar;

pub fn index_mpn_accounts<K: KvStore>(
chain: &mut KvStoreChain<K>,
delta: &zk::ZkDeltaPairs,
) -> Result<(), BlockchainError> {
let mut org = HashMap::<u64, HashMap<u64, ZkScalar>>::new();
for (k, v) in delta.0.iter() {
if k.0.len() == 2 && (k.0[1] == 2 || k.0[1] == 3) {
org.entry(k.0[0])
.or_default()
.entry(k.0[1])
.or_insert(v.unwrap_or_default());
}
}
for (ind, data) in org {
let x = data.get(&2).ok_or(BlockchainError::Inconsistency)?;
let y = data.get(&3).ok_or(BlockchainError::Inconsistency)?;
let addr = MpnAddress {
pub_key: jubjub::PublicKey(jubjub::PointAffine(*x, *y).compress()),
};
chain
.database
.update(&[WriteOp::Put(keys::mpn_account_index(&addr, ind), ().into())])?;
}
Ok(())
}

pub fn update_contract<K: KvStore>(
chain: &mut KvStoreChain<K>,
Expand Down Expand Up @@ -105,10 +133,15 @@ pub fn update_contract<K: KvStore>(

let cont_account = chain.get_contract_account(*contract_id)?;

let delta = delta.clone().ok_or(BlockchainError::StateNotGiven)?;
if *contract_id == chain.config().mpn_config.mpn_contract_id {
index_mpn_accounts(chain, &delta)?;
}

zk::KvStoreStateManager::<CoreZkHasher>::update_contract(
&mut chain.database,
*contract_id,
&delta.clone().ok_or(BlockchainError::StateNotGiven)?,
&delta,
cont_account.height,
)?;
if zk::KvStoreStateManager::<CoreZkHasher>::root(&mut chain.database, *contract_id)?
Expand Down
6 changes: 5 additions & 1 deletion src/db/keys.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::*;
use crate::core::{Address, ContractId, TokenId, UndelegationId};
use crate::core::{Address, ContractId, MpnAddress, TokenId, UndelegationId};
use crate::zk::ZkDataLocator;
use thiserror::Error;

Expand Down Expand Up @@ -271,6 +271,10 @@ pub fn token(token_id: &TokenId) -> StringKey {
format!("TKN-{}", token_id).into()
}

pub fn mpn_account_index(mpn_address: &MpnAddress, index: u64) -> StringKey {
format!("MPN-{}-{}", mpn_address, index).into()
}

pub fn local_prefix(contract_id: &ContractId) -> String {
format!("S-{}", contract_id)
}
Expand Down

0 comments on commit c5f6e0e

Please sign in to comment.