Skip to content

Commit eb36d2f

Browse files
committed
Take accumulator as ref, in zkapp application
1 parent 3b3f383 commit eb36d2f

File tree

5 files changed

+73
-80
lines changed

5 files changed

+73
-80
lines changed

ledger/src/proofs/zkapp.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -488,10 +488,12 @@ pub fn zkapp_command_witnesses_exn(
488488
zkapp_command,
489489
} = v;
490490

491-
let (txn_applied, states) = {
492-
let (partial_txn, states) = first_pass_ledger
491+
let mut states = Vec::with_capacity(16);
492+
let txn_applied = {
493+
let partial_txn = first_pass_ledger
493494
.clone()
494495
.apply_zkapp_first_pass_unchecked_with_states(
496+
&mut states,
495497
global_slot,
496498
&state_view,
497499
fee_excess,
@@ -503,7 +505,7 @@ pub fn zkapp_command_witnesses_exn(
503505

504506
second_pass_ledger
505507
.clone()
506-
.apply_zkapp_second_pass_unchecked_with_states(states, partial_txn)
508+
.apply_zkapp_second_pass_unchecked_with_states(&mut states, partial_txn)
507509
.unwrap()
508510
};
509511

ledger/src/scan_state/scan_state.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,13 @@ use crate::{
3232
transaction_partially_applied::{
3333
TransactionPartiallyApplied, ZkappCommandPartiallyApplied,
3434
},
35-
zkapp_command::AccountUpdate,
3635
TransactionStatus,
3736
},
3837
},
3938
sparse_ledger::SparseLedger,
4039
staged_ledger::hash::AuxHash,
4140
verifier::Verifier,
42-
zkapps::{intefaces::LedgerInterface, non_snark::LedgerNonSnark},
43-
Account,
41+
zkapps::non_snark::LedgerNonSnark,
4442
};
4543

4644
use self::transaction_snark::{InitStack, LedgerProof, OneOrTwo, Registers};

ledger/src/scan_state/transaction_logic.rs

Lines changed: 49 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use crate::{
2525
Account, AccountId, ReceiptChainHash, Timing, TokenId,
2626
};
2727

28-
use self::zkapp_command::{AccessedOrNot, Numeric};
28+
use self::zkapp_command::AccessedOrNot;
2929
use self::{
3030
local_state::{CallStack, LocalStateEnv, StackFrame},
3131
protocol_state::{GlobalState, ProtocolStateView},
@@ -35,7 +35,7 @@ use self::{
3535
TransactionApplied, ZkappCommandApplied,
3636
},
3737
transaction_union_payload::TransactionUnionPayload,
38-
zkapp_command::{AccountUpdate, WithHash, ZkAppCommand, ZkAppPreconditions},
38+
zkapp_command::{AccountUpdate, WithHash, ZkAppCommand},
3939
};
4040

4141
use super::currency::SlotSpan;
@@ -5806,18 +5806,18 @@ pub mod local_state {
58065806

58075807
fn step_all<A, L>(
58085808
_constraint_constants: &ConstraintConstants,
5809-
f: &impl Fn(A, (&GlobalState<L>, &LocalStateEnv<L>)) -> A,
5810-
mut user_acc: A,
5809+
f: &impl Fn(&mut A, &GlobalState<L>, &LocalStateEnv<L>),
5810+
user_acc: &mut A,
58115811
(g_state, l_state): (&mut GlobalState<L>, &mut LocalStateEnv<L>),
5812-
) -> Result<(A, Vec<Vec<TransactionFailure>>), String>
5812+
) -> Result<Vec<Vec<TransactionFailure>>, String>
58135813
where
58145814
L: LedgerNonSnark,
58155815
{
58165816
while !l_state.stack_frame.calls.is_empty() {
58175817
zkapps::non_snark::step(g_state, l_state)?;
5818-
user_acc = f(user_acc, (g_state, l_state));
5818+
f(user_acc, g_state, l_state);
58195819
}
5820-
Ok((user_acc, l_state.failure_status_tbl.clone()))
5820+
Ok(l_state.failure_status_tbl.clone())
58215821
}
58225822

58235823
/// apply zkapp command fee payer's while stubbing out the second pass ledger
@@ -5827,16 +5827,16 @@ pub fn apply_zkapp_command_first_pass_aux<A, F, L>(
58275827
constraint_constants: &ConstraintConstants,
58285828
global_slot: Slot,
58295829
state_view: &ProtocolStateView,
5830-
init: A,
5830+
init: &mut A,
58315831
f: F,
58325832
fee_excess: Option<Signed<Amount>>,
58335833
supply_increase: Option<Signed<Amount>>,
58345834
ledger: &mut L,
58355835
command: &ZkAppCommand,
5836-
) -> Result<(ZkappCommandPartiallyApplied<L>, A), String>
5836+
) -> Result<ZkappCommandPartiallyApplied<L>, String>
58375837
where
58385838
L: LedgerNonSnark,
5839-
F: Fn(A, (&GlobalState<L>, &LocalStateEnv<L>)) -> A,
5839+
F: Fn(&mut A, &GlobalState<L>, &LocalStateEnv<L>),
58405840
{
58415841
let fee_excess = fee_excess.unwrap_or_else(Signed::zero);
58425842
let supply_increase = supply_increase.unwrap_or_else(Signed::zero);
@@ -5882,7 +5882,7 @@ where
58825882
},
58835883
);
58845884

5885-
let user_acc = f(init, (&global_state, &local_state));
5885+
f(init, &global_state, &local_state);
58865886
let account_updates = command.all_account_updates();
58875887

58885888
zkapps::non_snark::start(
@@ -5911,7 +5911,7 @@ where
59115911
local_state,
59125912
};
59135913

5914-
Ok((res, user_acc))
5914+
Ok(res)
59155915
}
59165916

59175917
fn apply_zkapp_command_first_pass<L>(
@@ -5926,12 +5926,13 @@ fn apply_zkapp_command_first_pass<L>(
59265926
where
59275927
L: LedgerNonSnark,
59285928
{
5929-
let (partial_stmt, _user_acc) = apply_zkapp_command_first_pass_aux(
5929+
let mut acc = ();
5930+
let partial_stmt = apply_zkapp_command_first_pass_aux(
59305931
constraint_constants,
59315932
global_slot,
59325933
state_view,
5933-
None,
5934-
|_acc, (g, l)| Some((g.clone(), l.clone())),
5934+
&mut acc,
5935+
|_acc, _g, _l| {},
59355936
fee_excess,
59365937
supply_increase,
59375938
ledger,
@@ -5943,14 +5944,14 @@ where
59435944

59445945
pub fn apply_zkapp_command_second_pass_aux<A, F, L>(
59455946
constraint_constants: &ConstraintConstants,
5946-
init: A,
5947+
init: &mut A,
59475948
f: F,
59485949
ledger: &mut L,
59495950
c: ZkappCommandPartiallyApplied<L>,
5950-
) -> Result<(ZkappCommandApplied, A), String>
5951+
) -> Result<ZkappCommandApplied, String>
59515952
where
59525953
L: LedgerNonSnark,
5953-
F: Fn(A, (&GlobalState<L>, &LocalStateEnv<L>)) -> A,
5954+
F: Fn(&mut A, &GlobalState<L>, &LocalStateEnv<L>),
59545955
{
59555956
// let perform = |eff: Eff<L>| Env::perform(eff);
59565957

@@ -6041,10 +6042,10 @@ where
60416042
}
60426043
};
60436044

6044-
let acc = f(init, (&global_state, &local_state));
6045+
f(init, &global_state, &local_state);
60456046
let start = (&mut global_state, &mut local_state);
60466047

6047-
let (user_acc, reversed_failure_status_tbl) = step_all(constraint_constants, &f, acc, start)?;
6048+
let reversed_failure_status_tbl = step_all(constraint_constants, &f, init, start)?;
60486049

60496050
let failure_status_tbl = reversed_failure_status_tbl
60506051
.into_iter()
@@ -6091,21 +6092,18 @@ where
60916092

60926093
let new_accounts_is_empty = new_accounts.is_empty();
60936094

6094-
let valid_result = Ok((
6095-
ZkappCommandApplied {
6096-
accounts: accounts(),
6097-
command: WithStatus {
6098-
data: c.command,
6099-
status: if successfully_applied {
6100-
TransactionStatus::Applied
6101-
} else {
6102-
TransactionStatus::Failed(failure_status_tbl)
6103-
},
6095+
let valid_result = Ok(ZkappCommandApplied {
6096+
accounts: accounts(),
6097+
command: WithStatus {
6098+
data: c.command,
6099+
status: if successfully_applied {
6100+
TransactionStatus::Applied
6101+
} else {
6102+
TransactionStatus::Failed(failure_status_tbl)
61046103
},
6105-
new_accounts,
61066104
},
6107-
user_acc,
6108-
));
6105+
new_accounts,
6106+
});
61096107

61106108
if successfully_applied {
61116109
valid_result
@@ -6137,27 +6135,32 @@ fn apply_zkapp_command_second_pass<L>(
61376135
where
61386136
L: LedgerNonSnark,
61396137
{
6140-
let (x, _) =
6141-
apply_zkapp_command_second_pass_aux(constraint_constants, (), |a, _| a, ledger, c)?;
6138+
let x = apply_zkapp_command_second_pass_aux(
6139+
constraint_constants,
6140+
&mut (),
6141+
|_, _, _| {},
6142+
ledger,
6143+
c,
6144+
)?;
61426145
Ok(x)
61436146
}
61446147

61456148
fn apply_zkapp_command_unchecked_aux<A, F, L>(
61466149
constraint_constants: &ConstraintConstants,
61476150
global_slot: Slot,
61486151
state_view: &ProtocolStateView,
6149-
init: A,
6152+
init: &mut A,
61506153
f: F,
61516154
fee_excess: Option<Signed<Amount>>,
61526155
supply_increase: Option<Signed<Amount>>,
61536156
ledger: &mut L,
61546157
command: &ZkAppCommand,
6155-
) -> Result<(ZkappCommandApplied, A), String>
6158+
) -> Result<ZkappCommandApplied, String>
61566159
where
61576160
L: LedgerNonSnark,
6158-
F: Fn(A, (&GlobalState<L>, &LocalStateEnv<L>)) -> A,
6161+
F: Fn(&mut A, &GlobalState<L>, &LocalStateEnv<L>),
61596162
{
6160-
let (partial_stmt, user_acc) = apply_zkapp_command_first_pass_aux(
6163+
let partial_stmt = apply_zkapp_command_first_pass_aux(
61616164
constraint_constants,
61626165
global_slot,
61636166
state_view,
@@ -6169,7 +6172,7 @@ where
61696172
command,
61706173
)?;
61716174

6172-
apply_zkapp_command_second_pass_aux(constraint_constants, user_acc, f, ledger, partial_stmt)
6175+
apply_zkapp_command_second_pass_aux(constraint_constants, init, &f, ledger, partial_stmt)
61736176
}
61746177

61756178
fn apply_zkapp_command_unchecked<L>(
@@ -6192,10 +6195,13 @@ where
61926195
command,
61936196
)?;
61946197

6195-
let (account_update_applied, state_res) = apply_zkapp_command_second_pass_aux(
6198+
let mut state_res = None;
6199+
let account_update_applied = apply_zkapp_command_second_pass_aux(
61966200
constraint_constants,
6197-
None,
6198-
|_acc, (global_state, local_state)| Some((local_state.clone(), global_state.fee_excess)),
6201+
&mut state_res,
6202+
|acc, global_state, local_state| {
6203+
*acc = Some((local_state.clone(), global_state.fee_excess))
6204+
},
61996205
ledger,
62006206
zkapp_partially_applied,
62016207
)?;

ledger/src/sparse_ledger/sparse_ledger.rs

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -128,25 +128,20 @@ impl SparseLedger {
128128

129129
pub fn apply_zkapp_first_pass_unchecked_with_states(
130130
&mut self,
131+
states: &mut Vec<(GlobalState<SparseLedger>, LocalStateEnv<SparseLedger>)>,
131132
global_slot: Slot,
132133
state_view: &ProtocolStateView,
133134
fee_excess: Signed<Amount>,
134135
supply_increase: Signed<Amount>,
135136
second_pass_ledger: &Self,
136137
zkapp_command: &ZkAppCommand,
137-
) -> Result<
138-
(
139-
ZkappCommandPartiallyApplied<SparseLedger>,
140-
Vec<(GlobalState<SparseLedger>, LocalStateEnv<SparseLedger>)>,
141-
),
142-
String,
143-
> {
138+
) -> Result<ZkappCommandPartiallyApplied<SparseLedger>, String> {
144139
apply_zkapp_command_first_pass_aux(
145140
constraint_constants(),
146141
global_slot,
147142
state_view,
148-
Vec::with_capacity(16),
149-
|mut acc, (global_state, local_state)| {
143+
states,
144+
|acc, global_state, local_state| {
150145
let GlobalState {
151146
first_pass_ledger,
152147
second_pass_ledger: _,
@@ -173,7 +168,6 @@ impl SparseLedger {
173168
local_state,
174169
),
175170
);
176-
acc
177171
},
178172
Some(fee_excess),
179173
Some(supply_increase),
@@ -184,19 +178,13 @@ impl SparseLedger {
184178

185179
pub fn apply_zkapp_second_pass_unchecked_with_states(
186180
&mut self,
187-
init: Vec<(GlobalState<SparseLedger>, LocalStateEnv<SparseLedger>)>,
181+
init: &mut Vec<(GlobalState<SparseLedger>, LocalStateEnv<SparseLedger>)>,
188182
c: ZkappCommandPartiallyApplied<Self>,
189-
) -> Result<
190-
(
191-
ZkappCommandApplied,
192-
Vec<(GlobalState<SparseLedger>, LocalStateEnv<SparseLedger>)>,
193-
),
194-
String,
195-
> {
196-
let (account_update_applied, mut rev_states) = apply_zkapp_command_second_pass_aux(
183+
) -> Result<ZkappCommandApplied, String> {
184+
let account_update_applied = apply_zkapp_command_second_pass_aux(
197185
constraint_constants(),
198186
init,
199-
|mut acc, (global_state, local_state)| {
187+
|acc, global_state, local_state| {
200188
let GlobalState {
201189
first_pass_ledger,
202190
second_pass_ledger,
@@ -223,12 +211,13 @@ impl SparseLedger {
223211
local_state,
224212
),
225213
);
226-
acc
214+
// acc
227215
},
228216
self,
229217
c,
230218
)?;
231219

220+
let rev_states = init;
232221
let will_succeed = account_update_applied.command.status.is_applied();
233222

234223
rev_states.reverse();
@@ -239,8 +228,8 @@ impl SparseLedger {
239228
local_state.will_succeed = will_succeed;
240229
}
241230

242-
let states = rev_states;
243-
Ok((account_update_applied, states))
231+
// let states = rev_states;
232+
Ok(account_update_applied)
244233
}
245234
}
246235

ledger/src/staged_ledger/staged_ledger.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,19 @@ use crate::{
2424
},
2525
snark_work::spec,
2626
transaction_logic::{
27-
apply_transaction_first_pass, apply_transaction_second_pass,
28-
local_state::LocalState,
27+
apply_transaction_first_pass, apply_transaction_second_pass, local_state::LocalState,
2928
protocol_state::ProtocolStateView,
30-
transaction_partially_applied::TransactionPartiallyApplied,
31-
valid,
32-
zkapp_command::{AccountUpdate, MaybeWithStatus},
33-
CoinbaseFeeTransfer, Transaction, TransactionStatus, UserCommand, WithStatus,
29+
transaction_partially_applied::TransactionPartiallyApplied, valid,
30+
zkapp_command::MaybeWithStatus, CoinbaseFeeTransfer, Transaction, TransactionStatus,
31+
UserCommand, WithStatus,
3432
},
3533
},
3634
sparse_ledger::SparseLedger,
3735
split_at, split_at_vec,
3836
staged_ledger::{pre_diff_info, resources::IncreaseBy, transaction_validator},
3937
verifier::{Verifier, VerifierError},
40-
zkapps::{intefaces::LedgerInterface, non_snark::LedgerNonSnark},
41-
Account, AccountId, BaseLedger, Mask, TokenId,
38+
zkapps::non_snark::LedgerNonSnark,
39+
AccountId, BaseLedger, Mask, TokenId,
4240
};
4341

4442
use super::{

0 commit comments

Comments
 (0)