Skip to content

Commit

Permalink
Unify ContractIds with TokenIds + Allow contracts control minting of …
Browse files Browse the repository at this point in the history
…tokens
  • Loading branch information
keyvank committed Sep 9, 2023
1 parent 556d531 commit f191149
Show file tree
Hide file tree
Showing 47 changed files with 283 additions and 814 deletions.
4 changes: 2 additions & 2 deletions src/blockchain/config.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::core::{Address, Block, Ratio, TokenId};
use crate::core::{Address, Block, ContractId, Ratio};
use crate::mpn::MpnConfig;
use std::collections::HashSet;

Expand All @@ -8,7 +8,7 @@ pub struct BlockchainConfig {
pub genesis: Block,
pub reward_ratio: u64,
pub max_block_size: usize,
pub ziesha_token_id: TokenId,
pub ziesha_token_id: ContractId,
pub mpn_config: MpnConfig,
pub testnet_height_limit: Option<u64>,
pub max_memo_length: usize,
Expand Down
2 changes: 0 additions & 2 deletions src/blockchain/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,6 @@ pub enum BlockchainError {
TokenNotFound,
#[error("token not updatable")]
TokenNotUpdatable,
#[error("token is being updated by a wrong account")]
TokenUpdatePermissionDenied,
#[error("token supply not enough to be redeemed")]
TokenSupplyInsufficient,
#[error("token supply overflows when issued")]
Expand Down
14 changes: 8 additions & 6 deletions src/blockchain/mempool.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::{Blockchain, BlockchainError, TransactionMetadata, TransactionStats};
use crate::core::{
Address, Amount, GeneralAddress, GeneralTransaction, MpnDeposit, MpnWithdraw, NonceGroup,
TokenId, TransactionAndDelta, TransactionKind,
Address, Amount, ContractId, GeneralAddress, GeneralTransaction, MpnDeposit, MpnWithdraw,
NonceGroup, TransactionAndDelta, TransactionKind,
};
use crate::db::KvStore;
use crate::zk::MpnTransaction;
Expand Down Expand Up @@ -151,7 +151,7 @@ impl Mempool {
for (ng, mempool) in self.txs.iter() {
if let Some((tx, _)) = mempool.first_tx() {
let fee = tx.fee();
if fee.token_id == TokenId::Ziesha {
if fee.token_id == ContractId::Ziesha {
firsts.entry(ng.kind()).or_default().push(fee.amount);
}
}
Expand Down Expand Up @@ -226,7 +226,7 @@ impl Mempool {
return Ok(());
}

if tx.fee().token_id != TokenId::Ziesha {
if tx.fee().token_id != ContractId::Ziesha {
return Ok(());
}

Expand Down Expand Up @@ -297,13 +297,15 @@ impl Mempool {
}

let ziesha_balance = match tx.sender() {
GeneralAddress::ChainAddress(addr) => blockchain.get_balance(addr, TokenId::Ziesha)?,
GeneralAddress::ChainAddress(addr) => {
blockchain.get_balance(addr, ContractId::Ziesha)?
}
GeneralAddress::MpnAddress(mpn_addr) => {
let acc = blockchain.get_mpn_account(mpn_addr)?;
acc.tokens
.get(&0)
.map(|m| {
if m.token_id == TokenId::Ziesha {
if m.token_id == ContractId::Ziesha {
m.amount
} else {
0.into()
Expand Down
20 changes: 10 additions & 10 deletions src/blockchain/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ mod ops;
use crate::core::{
hash::Hash, Address, Amount, Block, ContractAccount, ContractDeposit, ContractId,
ContractUpdate, ContractUpdateData, ContractWithdraw, Delegate, Hasher, Header, Money,
MpnAddress, ProofOfStake, Ratio, RegularSendEntry, Signature, Staker, Token, TokenId,
TokenUpdate, Transaction, TransactionAndDelta, TransactionData, Undelegation, UndelegationId,
ValidatorProof, Vrf, ZkHasher as CoreZkHasher,
MpnAddress, ProofOfStake, Ratio, RegularSendEntry, Signature, Staker, Token, Transaction,
TransactionAndDelta, TransactionData, Undelegation, UndelegationId, ValidatorProof, Vrf,
ZkHasher as CoreZkHasher,
};
use crate::crypto::VerifiableRandomFunction;
use crate::db::{keys, KvStore, RamMirrorKvStore, WriteOp};
Expand Down Expand Up @@ -108,13 +108,13 @@ pub trait Blockchain<K: KvStore> {

fn db_checksum(&self) -> Result<String, BlockchainError>;

fn get_token(&self, token_id: TokenId) -> Result<Option<Token>, BlockchainError>;
fn get_token(&self, token_id: ContractId) -> Result<Option<Token>, BlockchainError>;

fn get_balance(&self, addr: Address, token_id: TokenId) -> Result<Amount, BlockchainError>;
fn get_balance(&self, addr: Address, token_id: ContractId) -> Result<Amount, BlockchainError>;
fn get_contract_balance(
&self,
contract_id: ContractId,
token_id: TokenId,
token_id: ContractId,
) -> Result<Amount, BlockchainError>;
fn get_delegate(
&self,
Expand Down Expand Up @@ -307,7 +307,7 @@ impl<K: KvStore> Blockchain<K> for KvStoreChain<K> {
fn get_contract_balance(
&self,
contract_id: ContractId,
token_id: TokenId,
token_id: ContractId,
) -> Result<Amount, BlockchainError> {
Ok(
match self
Expand All @@ -320,14 +320,14 @@ impl<K: KvStore> Blockchain<K> for KvStoreChain<K> {
)
}

fn get_token(&self, token_id: TokenId) -> Result<Option<Token>, BlockchainError> {
fn get_token(&self, token_id: ContractId) -> Result<Option<Token>, BlockchainError> {
Ok(match self.database.get(keys::token(&token_id))? {
Some(b) => Some(b.try_into()?),
None => None,
})
}

fn get_balance(&self, addr: Address, token_id: TokenId) -> Result<Amount, BlockchainError> {
fn get_balance(&self, addr: Address, token_id: ContractId) -> Result<Amount, BlockchainError> {
Ok(
match self.database.get(keys::account_balance(&addr, token_id))? {
Some(b) => b.try_into()?,
Expand Down Expand Up @@ -500,7 +500,7 @@ impl<K: KvStore> Blockchain<K> for KvStoreChain<K> {
Ok(blks)
}
fn next_reward(&self) -> Result<Amount, BlockchainError> {
let supply = self.get_balance(Default::default(), TokenId::Ziesha)?;
let supply = self.get_balance(Default::default(), ContractId::Ziesha)?;
Ok(supply / self.config.reward_ratio)
}
fn draft_block(
Expand Down
26 changes: 11 additions & 15 deletions src/blockchain/ops/apply_tx/create_contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ 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,
Expand All @@ -17,12 +16,13 @@ pub fn create_contract<K: KvStore>(
return Err(BlockchainError::TokenBadNameSymbol);
}
chain.database.update(&[WriteOp::Put(
keys::account_balance(&tx_src, token_id),
keys::account_balance(&tx_src, contract_id),
token.token.supply.into(),
)])?;
chain
.database
.update(&[WriteOp::Put(keys::token(&token_id), (&token.token).into())])?;
chain.database.update(&[WriteOp::Put(
keys::token(&contract_id),
(&token.token).into(),
)])?;
}
chain.database.update(&[WriteOp::Put(
keys::contract(&contract_id),
Expand Down Expand Up @@ -87,12 +87,9 @@ mod tests {
)
.unwrap();
let contract_id: ContractId =
"0001020304050607080900010203040506070809000102030405060708090001"
"0x0001020304050607080900010203040506070809000102030405060708090001"
.parse()
.unwrap();
let token_id: TokenId = "0001020304050607080900010203040506070809000102030405060708090001"
.parse()
.unwrap();
let state_model = zk::ZkStateModel::Struct {
field_types: vec![zk::ZkStateModel::Scalar, zk::ZkStateModel::Scalar],
};
Expand All @@ -112,7 +109,6 @@ mod tests {
chain,
abc.get_address(),
contract_id,
token_id,
&contract,
&Some(Default::default()),
Money::ziesha(2345),
Expand All @@ -127,28 +123,28 @@ mod tests {
7655u64.into(),
),
WriteOp::Put(
"CAB-0001020304050607080900010203040506070809000102030405060708090001-Ziesha"
"CAB-0x0001020304050607080900010203040506070809000102030405060708090001-Ziesha"
.into(),
2345u64.into(),
),
WriteOp::Put(
"CAC-0001020304050607080900010203040506070809000102030405060708090001".into(),
"CAC-0x0001020304050607080900010203040506070809000102030405060708090001".into(),
ContractAccount {
height: 1,
compressed_state: initial_state.clone(),
}
.into(),
),
WriteOp::Put(
"CON-0001020304050607080900010203040506070809000102030405060708090001".into(),
"CON-0x0001020304050607080900010203040506070809000102030405060708090001".into(),
contract.into(),
),
WriteOp::Put(
"S-0001020304050607080900010203040506070809000102030405060708090001-HGT".into(),
"S-0x0001020304050607080900010203040506070809000102030405060708090001-HGT".into(),
1u64.into(),
),
WriteOp::Put(
"S-0001020304050607080900010203040506070809000102030405060708090001-RT".into(),
"S-0x0001020304050607080900010203040506070809000102030405060708090001-RT".into(),
initial_state.into(),
),
];
Expand Down
Loading

0 comments on commit f191149

Please sign in to comment.