Skip to content

Commit 25cb4f0

Browse files
committed
Implement ZkappApplication for non-snark context
1 parent 38d5c48 commit 25cb4f0

File tree

6 files changed

+736
-136
lines changed

6 files changed

+736
-136
lines changed

ledger/src/account/account.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -884,6 +884,10 @@ impl ZkAppAccount {
884884
pub fn empty_action_state() -> Fp {
885885
cache_one!(Fp, { hash_noinputs("MinaZkappActionStateEmptyElt") })
886886
}
887+
888+
pub fn is_default(&self) -> bool {
889+
self == &Self::default()
890+
}
887891
}
888892

889893
/// An `AccountId` implementing `Ord` & `PartialOrd`, reproducing OCaml ordering.

ledger/src/scan_state/transaction_logic.rs

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1805,7 +1805,7 @@ pub mod zkapp_command {
18051805
}
18061806

18071807
impl EpochLedger {
1808-
pub fn epoch_ledger(&self, t: protocol_state::EpochLedger<Fp>) -> Result<(), String> {
1808+
pub fn epoch_ledger(&self, t: &protocol_state::EpochLedger<Fp>) -> Result<(), String> {
18091809
self.hash.zcheck("epoch_ledger_hash".to_string(), t.hash)?;
18101810
self.total_currency
18111811
.zcheck("epoch_ledger_total_currency".to_string(), t.total_currency)
@@ -1908,9 +1908,9 @@ pub mod zkapp_command {
19081908
pub fn epoch_data(
19091909
&self,
19101910
label: &str,
1911-
t: protocol_state::EpochData<Fp>,
1911+
t: &protocol_state::EpochData<Fp>,
19121912
) -> Result<(), String> {
1913-
self.ledger.epoch_ledger(t.ledger)?;
1913+
self.ledger.epoch_ledger(&t.ledger)?;
19141914
// ignore seed
19151915
self.start_checkpoint.zcheck(
19161916
format!("{}_{}", label, "start_checkpoint"),
@@ -1954,7 +1954,7 @@ pub mod zkapp_command {
19541954

19551955
impl ZkAppPreconditions {
19561956
/// zkapp check
1957-
pub fn zcheck(&self, s: ProtocolStateView) -> Result<(), String> {
1957+
pub fn zcheck(&self, s: &ProtocolStateView) -> Result<(), String> {
19581958
self.snarked_ledger_hash
19591959
.zcheck("snarker_ledger_hash".to_string(), s.snarked_ledger_hash)?;
19601960
self.blockchain_length
@@ -1968,9 +1968,9 @@ pub mod zkapp_command {
19681968
s.global_slot_since_genesis,
19691969
)?;
19701970
self.staking_epoch_data
1971-
.epoch_data("staking_epoch_data", s.staking_epoch_data)?;
1971+
.epoch_data("staking_epoch_data", &s.staking_epoch_data)?;
19721972
self.next_epoch_data
1973-
.epoch_data("next_epoch_data", s.next_epoch_data)
1973+
.epoch_data("next_epoch_data", &s.next_epoch_data)
19741974
}
19751975

19761976
pub fn checked_zcheck(&self, s: &ProtocolStateView, w: &mut Witness<Fp>) -> Boolean {
@@ -2167,7 +2167,7 @@ pub mod zkapp_command {
21672167

21682168
impl Account {
21692169
/// zkapp check
2170-
pub fn zcheck<F>(&self, new_account: bool, mut check: F, a: account::Account)
2170+
pub fn zcheck<F>(&self, new_account: bool, mut check: F, a: &account::Account)
21712171
where
21722172
F: FnMut(TransactionFailure, bool),
21732173
{
@@ -2179,9 +2179,12 @@ pub mod zkapp_command {
21792179
fn zchecks(
21802180
&self,
21812181
new_account: bool,
2182-
a: account::Account,
2182+
a: &account::Account,
21832183
) -> Vec<(TransactionFailure, Result<(), String>)> {
2184-
let zkapp = a.zkapp.unwrap_or_default();
2184+
let zkapp = match a.zkapp.as_ref() {
2185+
Some(zkapp) => MyCow::Borrow(&**zkapp),
2186+
None => MyCow::Own(ZkAppAccount::default()),
2187+
};
21852188
let mut ret = vec![
21862189
(
21872190
TransactionFailure::AccountBalancePreconditionUnsatisfied,
@@ -2200,7 +2203,7 @@ pub mod zkapp_command {
22002203
TransactionFailure::AccountDelegatePreconditionUnsatisfied,
22012204
self.delegate.zcheck(
22022205
"delegate".to_string(),
2203-
a.delegate.unwrap_or_else(invalid_public_key),
2206+
a.delegate.clone().unwrap_or_else(invalid_public_key),
22042207
),
22052208
),
22062209
(
@@ -5227,7 +5230,7 @@ pub mod protocol_state {
52275230
}
52285231

52295232
#[must_use]
5230-
fn set_fee_excess(&self, fee_excess: Signed<Amount>) -> Self {
5233+
pub fn set_fee_excess(&self, fee_excess: Signed<Amount>) -> Self {
52315234
let mut this = self.clone();
52325235
this.fee_excess = fee_excess;
52335236
this
@@ -5244,7 +5247,7 @@ pub mod protocol_state {
52445247
this
52455248
}
52465249

5247-
fn block_global_slot(&self) -> Slot {
5250+
pub fn block_global_slot(&self) -> Slot {
52485251
self.block_global_slot
52495252
}
52505253
}
@@ -5856,7 +5859,7 @@ where
58565859
.is_ok(),
58575860
),
58585861
Eff::CheckProtocolStatePrecondition(pred, global_state) => {
5859-
PerformResult::Bool(pred.zcheck(global_state.protocol_state).is_ok())
5862+
PerformResult::Bool(pred.zcheck(&global_state.protocol_state).is_ok())
58605863
}
58615864
Eff::CheckAccountPrecondition(account_update, account, new_account, local_state) => {
58625865
let local_state = {
@@ -5865,7 +5868,7 @@ where
58655868
let check = |failure, b| {
58665869
_local_state = _local_state.add_check(failure, b);
58675870
};
5868-
precondition_account.zcheck(new_account, check, account);
5871+
precondition_account.zcheck(new_account, check, &account);
58695872
_local_state
58705873
};
58715874
PerformResult::LocalState(Box::new(local_state))
@@ -7895,7 +7898,7 @@ pub fn validate_timing(
78957898
pub fn account_check_timing(
78967899
txn_global_slot: &Slot,
78977900
account: &Account,
7898-
) -> (TimingValidation, Timing) {
7901+
) -> (TimingValidation<bool>, Timing) {
78997902
let (invalid_timing, timing, _) =
79007903
validate_timing_with_min_balance_impl(account, Amount::from_u64(0), txn_global_slot);
79017904
// TODO: In OCaml the returned Timing is actually converted to None/Some(fields of Timing structure)
@@ -7958,9 +7961,9 @@ pub fn timing_error_to_user_command_status(
79587961
}
79597962
}
79607963

7961-
pub enum TimingValidation {
7962-
InsufficientBalance(bool),
7963-
InvalidTiming(bool),
7964+
pub enum TimingValidation<B> {
7965+
InsufficientBalance(B),
7966+
InvalidTiming(B),
79647967
}
79657968

79667969
#[derive(Debug)]
@@ -7970,7 +7973,7 @@ fn validate_timing_with_min_balance_impl(
79707973
account: &Account,
79717974
txn_amount: Amount,
79727975
txn_global_slot: &Slot,
7973-
) -> (TimingValidation, Timing, MinBalance) {
7976+
) -> (TimingValidation<bool>, Timing, MinBalance) {
79747977
use crate::Timing::*;
79757978
use TimingValidation::*;
79767979

ledger/src/zkapps/intefaces.rs

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::zkapps::zkapp_logic;
1313
use crate::scan_state::transaction_logic::zkapp_command::{
1414
self, CheckAuthorizationResult, SetOrKeep,
1515
};
16-
use crate::scan_state::transaction_logic::TransactionFailure;
16+
use crate::scan_state::transaction_logic::{TimingValidation, TransactionFailure};
1717
use crate::sparse_ledger::LedgerIntf;
1818
use crate::{AccountId, AuthRequired, MyCow, ReceiptChainHash, TokenId, ZkAppAccount};
1919

@@ -90,6 +90,21 @@ pub struct Opt<T> {
9090
pub data: T,
9191
}
9292

93+
impl<T: Default> Opt<T> {
94+
pub fn from_option(opt: Option<T>) -> Self {
95+
match opt {
96+
Some(data) => Self {
97+
is_some: Boolean::True,
98+
data,
99+
},
100+
None => Self {
101+
is_some: Boolean::False,
102+
data: T::default(),
103+
},
104+
}
105+
}
106+
}
107+
93108
impl<A, B> Opt<(A, B)> {
94109
pub fn unzip(self) -> (Opt<A>, Opt<B>) {
95110
let Self {
@@ -320,11 +335,11 @@ where
320335
type CallForest: CallForestInterface;
321336
type Bool: BoolInterface;
322337
type SignedAmount: SignedAmountInterface;
338+
type VerificationKeyHash: VerificationKeyHashInterface;
323339

324340
// Only difference in our Rust code is the `WithHash`
325341
fn body(&self) -> &crate::scan_state::transaction_logic::zkapp_command::Body;
326-
fn set(&mut self, new: Self);
327-
fn verification_key_hash(&self) -> Fp;
342+
fn verification_key_hash(&self) -> Self::VerificationKeyHash;
328343
fn is_proved(&self) -> Self::Bool;
329344
fn is_signed(&self) -> Self::Bool;
330345
fn check_authorization(
@@ -372,7 +387,7 @@ pub trait ControllerInterface {
372387
auth: &AuthRequired,
373388
single_data: &Self::SingleData,
374389
w: &mut Self::W,
375-
) -> Self::Bool;
390+
) -> Result<Self::Bool, String>;
376391

377392
fn verification_key_perm_fallback_to_signature_with_older_version(
378393
auth: &AuthRequired,
@@ -434,6 +449,7 @@ where
434449
type Bool: BoolInterface;
435450
type Balance: BalanceInterface;
436451
type GlobalSlot: GlobalSlotSinceGenesisInterface;
452+
type VerificationKeyHash: VerificationKeyHashInterface;
437453
type D;
438454

439455
fn register_verification_key(&self, data: &Self::D, w: &mut Self::W);
@@ -442,7 +458,7 @@ where
442458
fn set_delegate(&mut self, new: CompressedPubKey);
443459
fn zkapp(&self) -> MyCow<ZkAppAccount>;
444460
fn zkapp_mut(&mut self) -> &mut ZkAppAccount;
445-
fn verification_key_hash(&self) -> Fp;
461+
fn verification_key_hash(&self) -> Self::VerificationKeyHash;
446462
fn set_token_id(&mut self, token_id: TokenId);
447463
fn is_timed(&self) -> Self::Bool;
448464
fn balance(&self) -> Self::Balance;
@@ -451,7 +467,7 @@ where
451467
&self,
452468
txn_global_slot: &Self::GlobalSlot,
453469
w: &mut Self::W,
454-
) -> (Self::Bool, crate::Timing);
470+
) -> (TimingValidation<Self::Bool>, crate::Timing);
455471
fn make_zkapp(&mut self);
456472
fn unmake_zkapp(&mut self);
457473
fn proved_state(&self) -> Self::Bool;
@@ -490,7 +506,7 @@ pub trait VerificationKeyHashInterface {
490506
type W: WitnessGenerator<Fp>;
491507
type Bool: BoolInterface;
492508

493-
fn equal(a: Fp, b: Fp, w: &mut Self::W) -> Self::Bool;
509+
fn equal(a: &Self, b: &Self, w: &mut Self::W) -> Self::Bool;
494510
}
495511

496512
pub trait SetOrKeepInterface {
@@ -599,6 +615,7 @@ where
599615
SingleData = Self::SingleData,
600616
Bool = Self::Bool,
601617
SignedAmount = Self::SignedAmount,
618+
VerificationKeyHash = Self::VerificationKeyHash,
602619
>;
603620
type AccountId: AccountIdInterface<W = Self::WitnessGenerator>;
604621
type TokenId: TokenIdInterface<W = Self::WitnessGenerator, Bool = Self::Bool>;
@@ -620,6 +637,7 @@ where
620637
Bool = Self::Bool,
621638
Balance = Self::Balance,
622639
GlobalSlot = Self::GlobalSlotSinceGenesis,
640+
VerificationKeyHash = Self::VerificationKeyHash,
623641
>;
624642
type VerificationKeyHash: VerificationKeyHashInterface<
625643
W = Self::WitnessGenerator,

0 commit comments

Comments
 (0)