From 35132a05a027f0198b46fb96f4a2ff6a1a5c7a8f Mon Sep 17 00:00:00 2001 From: Meshiest Date: Fri, 29 Mar 2024 17:12:47 -0500 Subject: [PATCH] fix(aot): authorization cost fix --- crates/aot/src/authorized.rs | 67 +++++++++++++++++++++++++++++- crates/snot/src/cannon/mod.rs | 5 +++ specs/test-4-validator-cannon.yaml | 43 +++++++++++++++++++ 3 files changed, 113 insertions(+), 2 deletions(-) create mode 100644 specs/test-4-validator-cannon.yaml diff --git a/crates/aot/src/authorized.rs b/crates/aot/src/authorized.rs index b9bd1b22..437c12d5 100644 --- a/crates/aot/src/authorized.rs +++ b/crates/aot/src/authorized.rs @@ -4,6 +4,7 @@ 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}, @@ -11,6 +12,8 @@ use snarkvm::{ prelude::{ de, Deserialize, DeserializeExt, Deserializer, Serialize, SerializeStruct, Serializer, }, + synthesizer::process::cost_in_microcredits, + utilities::ToBytes, }; use tracing::error; @@ -111,8 +114,8 @@ impl Authorized { // Retrieve the execution ID. let execution_id: snarkvm::prelude::Field = 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, @@ -181,6 +184,66 @@ impl Authorized { } } +fn estimate_cost(func: &Authorization) -> Result { + 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 += ::StateRoot::default() + .to_bytes_le()? + .len() as u64; + + // proof option is_some (1 byte) + cost += 1; + // Proof version + cost += 1; + + cost += 956; // size of proof with 1 batch size + + /* cost += varuna::Proof::<::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(&self, serializer: S) -> Result { diff --git a/crates/snot/src/cannon/mod.rs b/crates/snot/src/cannon/mod.rs index e2dcde9d..bfe83bbe 100644 --- a/crates/snot/src/cannon/mod.rs +++ b/crates/snot/src/cannon/mod.rs @@ -498,6 +498,11 @@ impl ExecutionContext { .ok_or_else(|| anyhow::anyhow!("env dropped"))? .matching_nodes(target, &pool, PortType::Rest) .collect::>(); + + if nodes.is_empty() { + bail!("no nodes available to broadcast transactions") + } + let Some(node) = nodes.get(rand::random::() % nodes.len()) else { bail!("no nodes available to broadcast transactions") }; diff --git a/specs/test-4-validator-cannon.yaml b/specs/test-4-validator-cannon.yaml new file mode 100644 index 00000000..14ed6309 --- /dev/null +++ b/specs/test-4-validator-cannon.yaml @@ -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