Skip to content

Commit

Permalink
Allow binding tokens with contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
keyvank committed Sep 7, 2023
1 parent 0546cb9 commit 9b7107b
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/blockchain/ops/apply_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ pub fn apply_block<K: KvStore>(
ContractUpdateData::FunctionCall { .. } => {
num_mpn_function_calls += 1;
}
_ => {}
}
}
}
Expand Down
18 changes: 18 additions & 0 deletions src/blockchain/ops/apply_tx/create_contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,26 @@ pub fn create_contract<K: KvStore>(
chain: &mut KvStoreChain<K>,
tx_src: Address,
contract_id: ContractId,
token_id: TokenId,
contract: &zk::ZkContract,
state: &Option<zk::ZkDataPairs>,
money: Money,
) -> Result<(), BlockchainError> {
if !contract.state_model.is_valid::<CoreZkHasher>() {
return Err(BlockchainError::InvalidStateModel);
}
if let Some(token) = &contract.token {
if !token.token.validate() {
return Err(BlockchainError::TokenBadNameSymbol);
}
chain.database.update(&[WriteOp::Put(
keys::account_balance(&tx_src, token_id),
token.token.supply.into(),
)])?;
chain
.database
.update(&[WriteOp::Put(keys::token(&token_id), (&token.token).into())])?;
}
chain.database.update(&[WriteOp::Put(
keys::contract(&contract_id),
contract.clone().into(),
Expand Down Expand Up @@ -77,12 +90,16 @@ mod tests {
"0001020304050607080900010203040506070809000102030405060708090001"
.parse()
.unwrap();
let token_id: TokenId = "0001020304050607080900010203040506070809000102030405060708090001"
.parse()
.unwrap();
let state_model = zk::ZkStateModel::Struct {
field_types: vec![zk::ZkStateModel::Scalar, zk::ZkStateModel::Scalar],
};
let initial_state =
zk::ZkCompressedState::empty::<crate::core::ZkHasher>(state_model.clone());
let contract = zk::ZkContract {
token: None,
state_model,
initial_state: initial_state.clone(),
deposit_functions: vec![],
Expand All @@ -95,6 +112,7 @@ mod tests {
chain,
abc.get_address(),
contract_id,
token_id,
&contract,
&Some(Default::default()),
Money::ziesha(2345),
Expand Down
9 changes: 9 additions & 0 deletions src/blockchain/ops/apply_tx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,19 @@ pub fn apply_tx<K: KvStore>(
money,
} => {
let contract_id = ContractId::new(tx);
let token_id = {
let tid = TokenId::new(tx);
if tid == chain.config.ziesha_token_id {
TokenId::Ziesha
} else {
tid
}
};
create_contract::create_contract(
chain,
tx_src,
contract_id,
token_id,
contract,
state,
*money,
Expand Down
3 changes: 3 additions & 0 deletions src/blockchain/ops/apply_tx/update_contract/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ pub fn update_contract<K: KvStore>(
)?;
(circuit, aux_data)
}
ContractUpdateData::Mint { amount } => {
unimplemented!();
}
};

let mut cont_account = chain.get_contract_account(*contract_id)?;
Expand Down
1 change: 1 addition & 0 deletions src/blockchain/test/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ fn test_contract_create_patch() {
let tx = alice.create_contract(
"".into(),
zk::ZkContract {
token: None,
state_model: state_model.clone(),
initial_state: state_model
.compress::<CoreZkHasher>(&full_state.data)
Expand Down
6 changes: 6 additions & 0 deletions src/client/explorer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,9 @@ pub enum ExplorerContractUpdateData {
FunctionCall {
fee: ExplorerMoney,
},
Mint {
amount: u64,
},
}

impl From<&ContractUpdateData> for ExplorerContractUpdateData {
Expand All @@ -326,6 +329,9 @@ impl From<&ContractUpdateData> for ExplorerContractUpdateData {
withdraws: withdraws.iter().map(|p| p.into()).collect(),
},
ContractUpdateData::FunctionCall { fee } => Self::FunctionCall { fee: (*fee).into() },
ContractUpdateData::Mint { amount } => Self::Mint {
amount: (*amount).into(),
},
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/config/blockchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ fn get_mpn_contract(
}

let mpn_contract = zk::ZkContract {
token: None,
state_model: mpn_state_model.clone(),
initial_state: state_builder.compress().unwrap(),
deposit_functions: vec![zk::ZkMultiInputVerifierKey {
Expand Down
3 changes: 3 additions & 0 deletions src/core/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,9 @@ pub enum ContractUpdateData<H: Hash, S: SignatureScheme> {
FunctionCall {
fee: Money, // Executor fee
},
Mint {
amount: Amount,
},
}

#[derive(serde::Serialize, serde::Deserialize, PartialEq, Eq, Debug, Clone)]
Expand Down
9 changes: 8 additions & 1 deletion src/zk/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::core::{hash::Hash, Amount, Hasher, Money, TokenId, ZkHasher as ZkMainHasher};
use crate::core::{hash::Hash, Amount, Hasher, Money, Token, TokenId, ZkHasher as ZkMainHasher};
use crate::crypto::{jubjub, ZkSignatureScheme};

use ff::{Field, PrimeField};
Expand Down Expand Up @@ -627,13 +627,20 @@ impl MpnTransaction {
}
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct ZkTokenContract {
pub token: Token,
pub mint_functions: Vec<ZkSingleInputVerifierKey>,
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct ZkContract {
pub initial_state: ZkCompressedState, // 32byte
pub state_model: ZkStateModel,
pub deposit_functions: Vec<ZkMultiInputVerifierKey>, // VK f(prev_state, deposit_txs (L1)) -> next_state
pub withdraw_functions: Vec<ZkMultiInputVerifierKey>, // VK f(prev_state, withdraw_txs (L1)) -> next_state
pub functions: Vec<ZkSingleInputVerifierKey>, // Vec<VK> f(prev_state) -> next_state
pub token: Option<ZkTokenContract>,
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
Expand Down
1 change: 1 addition & 0 deletions src/zk/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ impl<H: ZkHasher> ZkStateBuilder<H> {
db.update(&[WriteOp::Put(
keys::contract(&contract_id),
ZkContract {
token: None,
initial_state: ZkCompressedState::empty::<H>(state_model.clone()),
state_model,
deposit_functions: vec![],
Expand Down
1 change: 1 addition & 0 deletions src/zk/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ impl ZkHasher for SumHasher {

fn empty_contract<H: ZkHasher>(state_model: ZkStateModel) -> ZkContract {
ZkContract {
token: None,
initial_state: ZkCompressedState::empty::<H>(state_model.clone()).into(),
state_model: state_model,
deposit_functions: vec![],
Expand Down

0 comments on commit 9b7107b

Please sign in to comment.