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

Introduce Host object #1082

Merged
merged 1 commit into from
Oct 10, 2023
Merged

Introduce Host object #1082

merged 1 commit into from
Oct 10, 2023

Conversation

frisitano
Copy link
Contributor

This PR introduces a Host trait as described in #1073 with a default implementation provided by the DefaultHost.

Copy link
Contributor

@bobbinth bobbinth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! Looks good! I left some comments inline - mostly about simplifying the structure a bit and renaming a few method.

Also, let's merge #1085 into this as I think that's the way to go and it would make it simpler to review the changes more holistically.

processor/src/lib.rs Outdated Show resolved Hide resolved
processor/src/lib.rs Outdated Show resolved Hide resolved
processor/src/lib.rs Outdated Show resolved Hide resolved
processor/src/lib.rs Outdated Show resolved Hide resolved
core/src/operations/decorators/host_function.rs Outdated Show resolved Hide resolved
processor/src/host/advice/mod.rs Outdated Show resolved Hide resolved
processor/src/host/advice/mod.rs Show resolved Hide resolved
processor/src/host/advice/mod.rs Outdated Show resolved Hide resolved
processor/src/host/mod.rs Outdated Show resolved Hide resolved
processor/src/host/debug.rs Outdated Show resolved Hide resolved
@bobbinth
Copy link
Contributor

bobbinth commented Oct 3, 2023

One other thought: maybe it makes sense to rename AdviceInjector into AdviceRequest and use it as a single enum for both AdviceInjector's and AdviceExtractor's.

Update: though, maybe let's not do this for now - maybe we can do it as a next step.

@frisitano frisitano force-pushed the frisitano-host-refactor branch 2 times, most recently from 4ecd55d to 9d8311a Compare October 4, 2023 08:31
@frisitano frisitano requested a review from bobbinth October 5, 2023 02:38
@frisitano frisitano marked this pull request as ready for review October 5, 2023 02:38
Copy link
Contributor

@bobbinth bobbinth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! Looks good! I left a few more comments inline - mostly about improving ergonomics of the code.

core/src/operations/decorators/advice.rs Outdated Show resolved Hide resolved
processor/src/lib.rs Outdated Show resolved Hide resolved
processor/src/lib.rs Outdated Show resolved Hide resolved
processor/src/operations/crypto_ops.rs Outdated Show resolved Hide resolved
processor/src/operations/crypto_ops.rs Outdated Show resolved Hide resolved
processor/src/host/mod.rs Outdated Show resolved Hide resolved
processor/src/host/mod.rs Outdated Show resolved Hide resolved
processor/src/host/advice/extractors.rs Outdated Show resolved Hide resolved
@bobbinth
Copy link
Contributor

bobbinth commented Oct 5, 2023

Another thing I'm wondering is whether it would make sense to split Host::handle_request() into 3 separate methods? For example, the trait could look like so:

pub trait Host {
    // REQUIRED METHODS
    // --------------------------------------------------------------------------------------------

    fn get_advice<S: ProcessState>(
        &mut self,
        process: &S,
        extractor: AdviceExtractor,
    ) -> Result<HostResponse, ExecutionError>;

    fn set_advice<S: ProcessState>(
        &mut self,
        process: &S,
        injector: AdviceInjector,
    ) -> Result<HostResponse, ExecutionError>;

    // PROVIDED METHODS
    // --------------------------------------------------------------------------------------------

    fn on_debug<S: ProcessState>(&self, process: &S, options: &DebugOptions) {
        debug::print_debug_info(process, options);
    }

    fn pop_adv_stack<S: ProcessState>(&mut self, process: &S) -> Result<Felt, ExecutionError> {
        self.get_advice(process, AdviceExtractor::PopStack).map(|res| res.into())
    }

    // TODO: other extractor wrappers

From the implementer's standpoint, I don't think this is any more complicated than the current approach (and maybe slightly simpler), and I think it is cleaner overall. Would be even cleaner if UpdateMerkleNode didn't return any values - but not sure if we can do anything about it.

@frisitano
Copy link
Contributor Author

frisitano commented Oct 9, 2023

Another thing I'm wondering is whether it would make sense to split Host::handle_request() into 3 separate methods? For example, the trait could look like so:

pub trait Host {
    // REQUIRED METHODS
    // --------------------------------------------------------------------------------------------

    fn get_advice<S: ProcessState>(
        &mut self,
        process: &S,
        extractor: AdviceExtractor,
    ) -> Result<HostResponse, ExecutionError>;

    fn set_advice<S: ProcessState>(
        &mut self,
        process: &S,
        injector: AdviceInjector,
    ) -> Result<HostResponse, ExecutionError>;

    // PROVIDED METHODS
    // --------------------------------------------------------------------------------------------

    fn on_debug<S: ProcessState>(&self, process: &S, options: &DebugOptions) {
        debug::print_debug_info(process, options);
    }

    fn pop_adv_stack<S: ProcessState>(&mut self, process: &S) -> Result<Felt, ExecutionError> {
        self.get_advice(process, AdviceExtractor::PopStack).map(|res| res.into())
    }

    // TODO: other extractor wrappers

From the implementer's standpoint, I don't think this is any more complicated than the current approach (and maybe slightly simpler), and I think it is cleaner overall. Would be even cleaner if UpdateMerkleNode didn't return any values - but not sure if we can do anything about it.

I think this has implications for encapsulation and extensibility. We are removing a level of indirection / encapsulation (removal of the HostRequest layer) which means the Host now has to be aware of the different variants of the HostRequest enum. Additionally if we want to introduce a new variant of the HostRequest enum we would have to modify the Host trait to support it (reduced extensibility). However maybe this is fine, what do you think?

@frisitano frisitano force-pushed the frisitano-host-refactor branch 2 times, most recently from 304cd25 to a5efd8f Compare October 9, 2023 06:29
@bobbinth
Copy link
Contributor

bobbinth commented Oct 9, 2023

I think this has implications for encapsulation and extensibility. We are removing a level of indirection / encapsulation (removal of the HostRequest layer) which means the Host now has to be aware of the different variants of the HostRequest enum. Additionally if we want to introduce a new variant of the HostRequest enum we would have to modify the Host trait to support it (reduced extensibility). However maybe this is fine, what do you think?

I agree that there is a tradeoff here - but I think it is probably OK to make it. My thinking is that we probably won't introduce top-level variants of HostRequest enum too frequently (I'm thinking we might add something like on_event() method, but not sure what beyond that). But also, even with having a single handle_request(), changing HostRequest enum would probably result in some amount of downstream changes.

@frisitano frisitano force-pushed the frisitano-host-refactor branch 2 times, most recently from c61f8fd to 6161c14 Compare October 9, 2023 07:37
@frisitano frisitano requested a review from bobbinth October 9, 2023 10:58
Copy link
Contributor

@bobbinth bobbinth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Thank you! I left a few small comments inline. Once these are addressed, we can merge.

processor/src/host/mod.rs Outdated Show resolved Hide resolved
processor/src/host/mod.rs Outdated Show resolved Hide resolved
processor/src/host/debug.rs Outdated Show resolved Hide resolved
processor/src/lib.rs Show resolved Hide resolved
core/src/operations/decorators/advice.rs Outdated Show resolved Hide resolved
processor/src/host/advice/providers.rs Outdated Show resolved Hide resolved
processor/src/host/advice/mod.rs Outdated Show resolved Hide resolved
processor/src/host/advice/mod.rs Outdated Show resolved Hide resolved
processor/src/lib.rs Outdated Show resolved Hide resolved
@frisitano frisitano force-pushed the frisitano-host-refactor branch from 6161c14 to 1d5c025 Compare October 10, 2023 04:07
@frisitano frisitano force-pushed the frisitano-host-refactor branch from 1d5c025 to 1efc2c0 Compare October 10, 2023 04:12
@frisitano frisitano merged commit fb47d83 into next Oct 10, 2023
@frisitano frisitano deleted the frisitano-host-refactor branch October 10, 2023 04:23
@frisitano frisitano self-assigned this Oct 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

2 participants