Skip to content

Commit

Permalink
Fill the teleportation tree
Browse files Browse the repository at this point in the history
  • Loading branch information
keyvank committed Oct 12, 2023
1 parent 3d7797b commit f4f3e71
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/blockchain/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ pub struct BlockchainConfig {
pub chain_start_timestamp: u32,
pub check_validator: bool,
pub max_validator_commission: Ratio,
pub teleport_log4_tree_size: u8,
pub teleport_contract_id: ContractId,
}
27 changes: 27 additions & 0 deletions src/blockchain/ops/apply_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,33 @@ pub fn apply_block<K: KvStore>(

let is_genesis = block.header.number == 0;

if is_genesis {
let teleport_state_model = zk::ZkStateModel::List {
item_type: Box::new(zk::ZkStateModel::Struct {
field_types: vec![
zk::ZkStateModel::Scalar, // Address
zk::ZkStateModel::Scalar, // Commitment
],
}),
log4_size: chain.config().teleport_log4_tree_size,
};
let teleport_contract = zk::ZkContract {
token: None,
initial_state: zk::ZkCompressedState::empty::<CoreZkHasher>(
teleport_state_model.clone(),
)
.into(),
state_model: teleport_state_model,
deposit_functions: vec![],
withdraw_functions: vec![],
functions: vec![],
};
chain.database.update(&[WriteOp::Put(
keys::contract(&chain.config().teleport_contract_id),
teleport_contract.into(),
)])?;
}

if curr_height > 0 {
if block.merkle_tree().root() != block.header.block_root {
return Err(BlockchainError::InvalidMerkleRoot);
Expand Down
20 changes: 18 additions & 2 deletions src/blockchain/ops/apply_tx/regular_send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,25 @@ fn put_in_teleport_tree<K: KvStore>(
dst: Address,
money: Money,
) -> Result<(), BlockchainError> {
let cont_id = chain.config().teleport_contract_id.clone();
let as_scalar =
ZkScalar::from_str_vartime(&BigUint::from_bytes_le(&dst.to_bytes()[..31]).to_string());
let commitment = CoreZkHasher::hash(&[money.token_id.into(), money.amount.into()]);
ZkScalar::from_str_vartime(&BigUint::from_bytes_le(&dst.to_bytes()[..31]).to_string())
.unwrap();
let height = zk::KvStoreStateManager::<CoreZkHasher>::height_of(&chain.database, cont_id)?;
let salt = ZkScalar::from(0);
let commitment = CoreZkHasher::hash(&[money.token_id.into(), money.amount.into(), salt]);
zk::KvStoreStateManager::<CoreZkHasher>::update_contract(
&mut chain.database,
cont_id,
&zk::ZkDeltaPairs(
[
(zk::ZkDataLocator(vec![height as u64, 0]), Some(as_scalar)),
(zk::ZkDataLocator(vec![height as u64, 1]), Some(commitment)),
]
.into(),
),
height + 1,
)?;
Ok(())
}

Expand Down
7 changes: 7 additions & 0 deletions src/config/blockchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::wallet::TxBuilder;
use crate::zk;

use std::collections::HashMap;
use std::str::FromStr;

use rand::SeedableRng;
use rand_chacha::ChaChaRng;
Expand Down Expand Up @@ -342,6 +343,12 @@ pub fn blockchain_config_template(initial_balances: bool) -> BlockchainConfig {
chain_start_timestamp: CHAIN_START_TIMESTAMP,
check_validator: true,
max_validator_commission: Ratio(26), // 26 / 255 ~= 10%

teleport_log4_tree_size: 10,
teleport_contract_id: ContractId::from_str(
"0x0000000000000000000000000000000000000000000000000000000000000000",
)
.unwrap(),
}
}

Expand Down

0 comments on commit f4f3e71

Please sign in to comment.