Skip to content

Commit

Permalink
fix(aot): authorization cost fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Meshiest committed Mar 29, 2024
1 parent 4a108f4 commit 35132a0
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 2 deletions.
67 changes: 65 additions & 2 deletions crates/aot/src/authorized.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ use anyhow::{bail, Result};
use clap::Args;
use rand::{CryptoRng, Rng};
use snarkvm::{
console::program::Network as NetworkTrait,
ledger::{
query::Query,
store::{helpers::memory::ConsensusMemory, ConsensusStore},
},
prelude::{
de, Deserialize, DeserializeExt, Deserializer, Serialize, SerializeStruct, Serializer,
},
synthesizer::process::cost_in_microcredits,
utilities::ToBytes,
};
use tracing::error;

Expand Down Expand Up @@ -111,8 +114,8 @@ impl Authorized {
// Retrieve the execution ID.
let execution_id: snarkvm::prelude::Field<Network> = function.to_execution_id()?;
// Determine the base fee in microcredits.
let base_fee_in_microcredits = 0;
// get_base_fee_in_microcredits(program_id, function_name)?;
let base_fee_in_microcredits = estimate_cost(&function)?;

// Authorize the fee.
let fee = match base_fee_in_microcredits == 0 && priority_fee_in_microcredits == 0 {
true => None,
Expand Down Expand Up @@ -181,6 +184,66 @@ impl Authorized {
}
}

fn estimate_cost(func: &Authorization) -> Result<u64> {
let transitions = func.transitions();

let storage_cost = {
let mut cost = 0u64;

cost += 1; // execution version, 1 byte
cost += 1; // number of transitions, 1 byte

// write each transition
for transition in transitions.values() {
cost += transition.to_bytes_le()?.len() as u64;
}

// state root (this is 32 bytes)
cost += <Network as NetworkTrait>::StateRoot::default()
.to_bytes_le()?
.len() as u64;

// proof option is_some (1 byte)
cost += 1;
// Proof<Network> version
cost += 1;

cost += 956; // size of proof with 1 batch size

/* cost += varuna::Proof::<<Network as Environment>::PairingCurve>::new(
todo!("batch_sizes"),
todo!("commitments"),
todo!("evaluations"),
todo!("prover_third_message"),
todo!("prover_fourth_message"),
todo!("pc_proof"),
)?
.to_bytes_le()?
.len() as u64; */

cost
};
//execution.size_in_bytes().map_err(|e| e.to_string())?;

// Compute the finalize cost in microcredits.
let mut finalize_cost = 0u64;
// Iterate over the transitions to accumulate the finalize cost.
for (_key, transition) in transitions {
// Retrieve the function name, program id, and program.
let function_name = transition.function_name();
let stack = PROCESS.get_stack(transition.program_id())?;
let cost = cost_in_microcredits(stack, function_name)?;

// Accumulate the finalize cost.
if let Some(cost) = finalize_cost.checked_add(cost) {
finalize_cost = cost;
} else {
bail!("The finalize cost computation overflowed for an execution")
};
}
Ok(storage_cost + finalize_cost)
}

impl Serialize for Authorized {
/// Serializes the authorization into string or bytes.
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
Expand Down
5 changes: 5 additions & 0 deletions crates/snot/src/cannon/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,11 @@ impl ExecutionContext {
.ok_or_else(|| anyhow::anyhow!("env dropped"))?
.matching_nodes(target, &pool, PortType::Rest)
.collect::<Vec<_>>();

if nodes.is_empty() {
bail!("no nodes available to broadcast transactions")
}

let Some(node) = nodes.get(rand::random::<usize>() % nodes.len()) else {
bail!("no nodes available to broadcast transactions")
};
Expand Down
43 changes: 43 additions & 0 deletions specs/test-4-validator-cannon.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
---
version: storage.snarkos.testing.monadic.us/v1

id: base
name: base-ledger

---
version: nodes.snarkos.testing.monadic.us/v1
name: 4-validators

nodes:
validator/test:
replicas: 4
key: committee.$
height: 0
validators: [validator/*]
peers: []

---
version: cannon.snarkos.testing.monadic.us/v1

name: committee-tx-public

source:
file-name: txs.json

sink:
target: validator/test-1
tx-delay-ms: 1000

---
version: timeline.snarkos.testing.monadic.us/v1

name: tx-local

timeline:
- cannon.await:
- name: committee-tx-public
count: 10
- offline.await:
- validator/test-0
- validator/test-2
- validator/test-3

0 comments on commit 35132a0

Please sign in to comment.