Skip to content

Commit 4b24507

Browse files
committed
Use reference in zcheck, to avoid allocations
1 parent a6fee2e commit 4b24507

File tree

6 files changed

+49
-50
lines changed

6 files changed

+49
-50
lines changed

ledger/src/proofs/transaction.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4633,7 +4633,7 @@ pub(super) mod tests {
46334633
}
46344634

46354635
#[test]
4636-
fn test_zkapp_proof_sig() {
4636+
fn test_proof_zkapp_sig() {
46374637
let Ok(data) = std::fs::read(
46384638
Path::new(env!("CARGO_MANIFEST_DIR"))
46394639
.join(devnet_circuit_directory())

ledger/src/scan_state/transaction_logic.rs

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1541,7 +1541,7 @@ pub mod zkapp_command {
15411541
type B;
15421542

15431543
/// zkapp check
1544-
fn zcheck(&self, label: String, x: Self::B) -> Result<(), String>;
1544+
fn zcheck<F: Fn() -> String>(&self, label: F, x: &Self::B) -> Result<(), String>;
15451545
}
15461546

15471547
impl<T> OutSnarkCheck for T
@@ -1552,11 +1552,11 @@ pub mod zkapp_command {
15521552
type B = T;
15531553

15541554
/// zkapp check
1555-
fn zcheck(&self, label: String, rhs: Self::B) -> Result<(), String> {
1556-
if *self == rhs {
1555+
fn zcheck<F: Fn() -> String>(&self, label: F, rhs: &Self::B) -> Result<(), String> {
1556+
if self == rhs {
15571557
Ok(())
15581558
} else {
1559-
Err(format!("Equality check failed: {}", label))
1559+
Err(format!("Equality check failed: {}", label()))
15601560
}
15611561
}
15621562
}
@@ -1625,15 +1625,15 @@ pub mod zkapp_command {
16251625
type B = T;
16261626

16271627
/// zkapp check
1628-
fn zcheck(&self, label: String, rhs: Self::B) -> Result<(), String> {
1628+
fn zcheck<F: Fn() -> String>(&self, label: F, rhs: &Self::B) -> Result<(), String> {
16291629
/*println!(
16301630
"bounds check lower {:?} rhs {:?} upper {:?}",
16311631
self.lower, rhs, self.upper
16321632
);*/
1633-
if self.lower <= rhs && rhs <= self.upper {
1633+
if &self.lower <= rhs && rhs <= &self.upper {
16341634
Ok(())
16351635
} else {
1636-
Err(format!("Bounds check failed: {}", label))
1636+
Err(format!("Bounds check failed: {}", label()))
16371637
}
16381638
}
16391639
}
@@ -1740,7 +1740,7 @@ pub mod zkapp_command {
17401740
T: OutSnarkCheck<A = T>,
17411741
{
17421742
/// zkapp check
1743-
pub fn zcheck(&self, label: String, rhs: T::B) -> Result<(), String> {
1743+
pub fn zcheck<F: Fn() -> String>(&self, label: F, rhs: &T::B) -> Result<(), String> {
17441744
// println!("[rust] check {}, {:?}", label, ret);
17451745
match self {
17461746
Self::Ignore => Ok(()),
@@ -1806,9 +1806,12 @@ pub mod zkapp_command {
18061806

18071807
impl EpochLedger {
18081808
pub fn epoch_ledger(&self, t: &protocol_state::EpochLedger<Fp>) -> Result<(), String> {
1809-
self.hash.zcheck("epoch_ledger_hash".to_string(), t.hash)?;
1810-
self.total_currency
1811-
.zcheck("epoch_ledger_total_currency".to_string(), t.total_currency)
1809+
self.hash
1810+
.zcheck(|| "epoch_ledger_hash".to_string(), &t.hash)?;
1811+
self.total_currency.zcheck(
1812+
|| "epoch_ledger_total_currency".to_string(),
1813+
&t.total_currency,
1814+
)
18121815
}
18131816
}
18141817

@@ -1913,15 +1916,15 @@ pub mod zkapp_command {
19131916
self.ledger.epoch_ledger(&t.ledger)?;
19141917
// ignore seed
19151918
self.start_checkpoint.zcheck(
1916-
format!("{}_{}", label, "start_checkpoint"),
1917-
t.start_checkpoint,
1919+
|| format!("{}_{}", label, "start_checkpoint"),
1920+
&t.start_checkpoint,
19181921
)?;
19191922
self.lock_checkpoint.zcheck(
1920-
format!("{}_{}", label, "lock_checkpoint"),
1921-
t.lock_checkpoint,
1923+
|| format!("{}_{}", label, "lock_checkpoint"),
1924+
&t.lock_checkpoint,
19221925
)?;
19231926
self.epoch_length
1924-
.zcheck(format!("{}_{}", label, "epoch_length"), t.epoch_length)
1927+
.zcheck(|| format!("{}_{}", label, "epoch_length"), &t.epoch_length)
19251928
}
19261929

19271930
pub fn gen() -> Self {
@@ -1956,16 +1959,16 @@ pub mod zkapp_command {
19561959
/// zkapp check
19571960
pub fn zcheck(&self, s: &ProtocolStateView) -> Result<(), String> {
19581961
self.snarked_ledger_hash
1959-
.zcheck("snarker_ledger_hash".to_string(), s.snarked_ledger_hash)?;
1962+
.zcheck(|| "snarker_ledger_hash".to_string(), &s.snarked_ledger_hash)?;
19601963
self.blockchain_length
1961-
.zcheck("blockchain_length".to_string(), s.blockchain_length)?;
1964+
.zcheck(|| "blockchain_length".to_string(), &s.blockchain_length)?;
19621965
self.min_window_density
1963-
.zcheck("min_window_density".to_string(), s.min_window_density)?;
1966+
.zcheck(|| "min_window_density".to_string(), &s.min_window_density)?;
19641967
self.total_currency
1965-
.zcheck("total_currency".to_string(), s.total_currency)?;
1968+
.zcheck(|| "total_currency".to_string(), &s.total_currency)?;
19661969
self.global_slot_since_genesis.zcheck(
1967-
"global_slot_since_genesis".to_string(),
1968-
s.global_slot_since_genesis,
1970+
|| "global_slot_since_genesis".to_string(),
1971+
&s.global_slot_since_genesis,
19691972
)?;
19701973
self.staking_epoch_data
19711974
.epoch_data("staking_epoch_data", &s.staking_epoch_data)?;
@@ -2188,31 +2191,31 @@ pub mod zkapp_command {
21882191
let mut ret = vec![
21892192
(
21902193
TransactionFailure::AccountBalancePreconditionUnsatisfied,
2191-
self.balance.zcheck("balance".to_string(), a.balance),
2194+
self.balance.zcheck(|| "balance".to_string(), &a.balance),
21922195
),
21932196
(
21942197
TransactionFailure::AccountNoncePreconditionUnsatisfied,
2195-
self.nonce.zcheck("nonce".to_string(), a.nonce),
2198+
self.nonce.zcheck(|| "nonce".to_string(), &a.nonce),
21962199
),
21972200
(
21982201
TransactionFailure::AccountReceiptChainHashPreconditionUnsatisfied,
21992202
self.receipt_chain_hash
2200-
.zcheck("receipt_chain_hash".to_string(), a.receipt_chain_hash.0),
2203+
.zcheck(|| "receipt_chain_hash".to_string(), &a.receipt_chain_hash.0),
22012204
),
22022205
(
22032206
TransactionFailure::AccountDelegatePreconditionUnsatisfied,
22042207
self.delegate.zcheck(
2205-
"delegate".to_string(),
2206-
a.delegate.clone().unwrap_or_else(invalid_public_key),
2208+
|| "delegate".to_string(),
2209+
&a.delegate.clone().unwrap_or_else(invalid_public_key),
22072210
),
22082211
),
22092212
(
22102213
TransactionFailure::AccountActionStatePreconditionUnsatisfied,
2211-
match zkapp
2212-
.action_state
2213-
.iter()
2214-
.find(|state| self.action_state.zcheck("".to_string(), **state).is_ok())
2215-
{
2214+
match zkapp.action_state.iter().find(|state| {
2215+
self.action_state
2216+
.zcheck(|| "".to_string(), &**state)
2217+
.is_ok()
2218+
}) {
22162219
None => Err("Action state mismatch".to_string()),
22172220
Some(_) => Ok(()),
22182221
},
@@ -2222,19 +2225,19 @@ pub mod zkapp_command {
22222225
for (i, (c, v)) in self.state.iter().zip(zkapp.app_state.iter()).enumerate() {
22232226
ret.push((
22242227
TransactionFailure::AccountAppStatePreconditionUnsatisfied(i as u64),
2225-
c.zcheck(format!("state[{}]", i), *v),
2228+
c.zcheck(|| format!("state[{}]", i), &*v),
22262229
));
22272230
}
22282231

22292232
let mut ret2 = vec![
22302233
(
22312234
TransactionFailure::AccountProvedStatePreconditionUnsatisfied,
22322235
self.proved_state
2233-
.zcheck("proved_state".to_string(), zkapp.proved_state),
2236+
.zcheck(|| "proved_state".to_string(), &zkapp.proved_state),
22342237
),
22352238
(
22362239
TransactionFailure::AccountIsNewPreconditionUnsatisfied,
2237-
self.is_new.zcheck("is_new".to_string(), new_account),
2240+
self.is_new.zcheck(|| "is_new".to_string(), &new_account),
22382241
),
22392242
];
22402243

@@ -5853,8 +5856,8 @@ where
58535856
Eff::CheckValidWhilePrecondition(valid_while, global_state) => PerformResult::Bool(
58545857
valid_while
58555858
.zcheck(
5856-
"valid_while_precondition".to_string(),
5857-
global_state.block_global_slot,
5859+
|| "valid_while_precondition".to_string(),
5860+
&global_state.block_global_slot,
58585861
)
58595862
.is_ok(),
58605863
),

ledger/src/zkapps/intefaces.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,7 @@ pub trait ZkappHandler {
8181
global_state: &mut Self::GlobalState,
8282
w: &mut Self::W,
8383
) -> Self::Bool;
84-
fn init_account(account_update: &Self::AccountUpdate, account: &Self::Account)
85-
-> Self::Account;
84+
fn init_account(account_update: &Self::AccountUpdate, account: Self::Account) -> Self::Account;
8685
}
8786

8887
pub struct Opt<T> {

ledger/src/zkapps/non_snark.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,17 +171,17 @@ impl<
171171
) -> Self::Bool {
172172
valid_while
173173
.zcheck(
174-
"valid_while_precondition".to_string(),
175-
global_state.block_global_slot,
174+
|| "valid_while_precondition".to_string(),
175+
&global_state.block_global_slot,
176176
)
177177
.is_ok()
178178
}
179179

180180
fn init_account(
181181
_account_update: &Self::AccountUpdate,
182-
account: &Self::Account,
182+
account: Self::Account,
183183
) -> Self::Account {
184-
account.clone()
184+
account
185185
}
186186
}
187187

ledger/src/zkapps/snark.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -253,18 +253,15 @@ impl ZkappHandler for SnarkHandler {
253253
.var()
254254
}
255255

256-
fn init_account(
257-
account_update: &Self::AccountUpdate,
258-
account: &Self::Account,
259-
) -> Self::Account {
256+
fn init_account(account_update: &Self::AccountUpdate, account: Self::Account) -> Self::Account {
260257
let AccountUpdateSkeleton {
261258
body: account_update,
262259
authorization: _,
263260
} = account_update;
264261
let account = Box::new(crate::Account {
265262
public_key: account_update.data.public_key.clone(),
266263
token_id: account_update.data.token_id.clone(),
267-
..(*account.data).clone()
264+
..*account.data
268265
});
269266
let account2 = account.clone();
270267
let account = WithLazyHash::new(account, move |w: &mut Witness<Fp>| {

ledger/src/zkapps/zkapp_logic.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1177,7 +1177,7 @@ where
11771177
((), ())
11781178
};
11791179

1180-
let a = Z::Handler::init_account(&account_update, &a);
1180+
let a = Z::Handler::init_account(&account_update, a);
11811181

11821182
let local_delta = account_update_balance_change.negate();
11831183

0 commit comments

Comments
 (0)