Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(air): introduce explicit types for generation numbers [fixes VM-261] #530

Merged
merged 17 commits into from
Apr 10, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion air/src/execution_step/boxed_value/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use crate::ExecutionError;
use crate::JValue;
use crate::UncatchableError;

use air_interpreter_data::GenerationIdx;
use air_interpreter_data::TracePos;
use air_trace_handler::merger::ValueSource;
use air_trace_handler::TraceHandler;
Expand Down Expand Up @@ -250,7 +251,7 @@ impl Stream {
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Generation {
Last,
Nth(u32),
Nth(GenerationIdx),
}

pub(crate) struct StreamIter<'result> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ mod utils;
use crate::execution_step::ExecutionResult;
use crate::execution_step::Stream;
use crate::ExecutionError;

use stream_descriptor::*;
pub(crate) use stream_value_descriptor::StreamValueDescriptor;

use air_interpreter_data::GlobalStreamGens;
use air_interpreter_data::RestrictedStreamGens;
use air_interpreter_data::GenerationIdx;
use air_parser::ast::Span;
use air_parser::AirPos;
use air_trace_handler::TraceHandler;
Expand Down Expand Up @@ -194,7 +196,7 @@ impl Streams {
.copied()
}

fn collect_stream_generation(&mut self, name: String, position: AirPos, generation: u32) {
fn collect_stream_generation(&mut self, name: String, position: AirPos, generation: GenerationIdx) {
match self.new_restricted_stream_gens.entry(name) {
Occupied(mut streams) => match streams.get_mut().entry(position) {
Occupied(mut iterations) => iterations.get_mut().push(generation),
Expand Down
5 changes: 4 additions & 1 deletion crates/air-lib/interpreter-data/src/executed_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,14 @@ pub enum CallResult {

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]

pub type GenerationIdx = u32;
mikevoronov marked this conversation as resolved.
Show resolved Hide resolved

pub enum ValueRef {
Scalar(Rc<CID<JValue>>),
Stream {
cid: Rc<CID<JValue>>,
mikevoronov marked this conversation as resolved.
Show resolved Hide resolved
generation: u32,
generation: GenerationIdx,
},
}

Expand Down
4 changes: 2 additions & 2 deletions crates/air-lib/interpreter-data/src/executed_state/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl CallResult {
CallResult::Executed(value)
}

pub fn executed_stream(cid: Rc<CID<JValue>>, generation: u32) -> CallResult {
pub fn executed_stream(cid: Rc<CID<JValue>>, generation: GenerationIdx) -> CallResult {
let value = ValueRef::Stream { cid, generation };

CallResult::Executed(value)
Expand Down Expand Up @@ -80,7 +80,7 @@ impl ExecutedState {
}

impl ApResult {
pub fn new(res_generation: u32) -> Self {
pub fn new(res_generation: GenerationIdx) -> Self {
Self {
res_generations: vec![res_generation],
}
Expand Down
15 changes: 8 additions & 7 deletions crates/air-lib/test-utils/src/executed_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use crate::SubTraceDesc;
use air_interpreter_cid::value_to_json_cid;
use air_interpreter_data::CanonCidAggregate;
use air_interpreter_data::CidTracker;
use air_interpreter_data::GenerationIdx;
use avm_server::SecurityTetraplet;
use serde::Deserialize;
use serde::Serialize;
Expand All @@ -56,19 +57,19 @@ pub fn scalar_number(result: impl Into<serde_json::Number>) -> ExecutedState {
scalar(result)
}

pub fn stream_call_result(result: JValue, generation: u32) -> CallResult {
pub fn stream_call_result(result: JValue, generation: GenerationIdx) -> CallResult {
let cid = value_to_json_cid(&result)
.unwrap_or_else(|e| panic!("{:?}: failed to compute CID of {:?}", e, result));
CallResult::executed_stream(Rc::new(cid), generation)
}

pub fn stream(result: JValue, generation: u32) -> ExecutedState {
pub fn stream(result: JValue, generation: GenerationIdx) -> ExecutedState {
ExecutedState::Call(stream_call_result(result, generation))
}

pub fn stream_tracked(
value: impl Into<JValue>,
generation: u32,
generation: GenerationIdx,
tracker: &mut CidTracker,
) -> ExecutedState {
let cid = tracker.record_value(Rc::new(value.into())).unwrap();
Expand All @@ -90,19 +91,19 @@ pub fn scalar_string_array(result: Vec<impl Into<String>>) -> ExecutedState {
scalar(value)
}

pub fn stream_string(result: impl Into<String>, generation: u32) -> ExecutedState {
pub fn stream_string(result: impl Into<String>, generation: GenerationIdx) -> ExecutedState {
let result = JValue::String(result.into());

stream(result, generation)
}

pub fn stream_number(result: impl Into<serde_json::Number>, generation: u32) -> ExecutedState {
pub fn stream_number(result: impl Into<serde_json::Number>, generation: GenerationIdx) -> ExecutedState {
let result = JValue::Number(result.into());

stream(result, generation)
}

pub fn stream_string_array(result: Vec<impl Into<String>>, generation: u32) -> ExecutedState {
pub fn stream_string_array(result: Vec<impl Into<String>>, generation: GenerationIdx) -> ExecutedState {
let result = result
.into_iter()
.map(|s| JValue::String(s.into()))
Expand Down Expand Up @@ -157,7 +158,7 @@ pub fn subtrace_desc(begin_pos: impl Into<TracePos>, subtrace_len: u32) -> SubTr
}
}

pub fn ap(generation: u32) -> ExecutedState {
pub fn ap(generation: GenerationIdx) -> ExecutedState {
let ap_result = ApResult::new(generation);
ExecutedState::Ap(ap_result)
}
Expand Down
4 changes: 2 additions & 2 deletions crates/air-lib/trace-handler/src/merger/ap_merger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub enum MergerApResult {

#[derive(Debug, Clone)]
pub struct MetApResult {
pub generation: u32,
pub generation: GenerationIdx,
pub value_source: ValueSource,
}

Expand Down Expand Up @@ -83,7 +83,7 @@ fn prepare_merge_result(
}

impl MetApResult {
pub(crate) fn new(generation: u32, value_source: ValueSource) -> Self {
pub(crate) fn new(generation: GenerationIdx, value_source: ValueSource) -> Self {
Self {
generation,
value_source,
Expand Down