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

Take advantage of some of the trait impls in RPC module #516

Open
2 tasks
ceyhunsen opened this issue Feb 10, 2025 · 0 comments
Open
2 tasks

Take advantage of some of the trait impls in RPC module #516

ceyhunsen opened this issue Feb 10, 2025 · 0 comments
Assignees
Labels
code quality Improves code quality while not affecting any other P-Medium Priority: Medium

Comments

@ceyhunsen
Copy link
Member

Proposal Description

RPC module can be simplified:

  • Use map, instead of StreamExt::map

    while let Some(sighash) = sighash_stream.next().await -> sighash_stream.map(|sh| operator.sign_with_tweak(sh))
    
  • Streamline RPC param/response encoding/decoding with a new trait type

    trait RpcParse { fallible parsing from rpc type to domain types }
    trait RpcEncode { (in?)fallible encoding from domain type to rpc type }
    
    src/rpc/parsers/{entity_name}.rs:
    
    impl RpcParse for {entity_name}Input {}
    impl RpcEncode for … {}
    
    src/rpc/{entity_name}.rs:
    
    use parsers::{RpcParse, RpcEncode};
    fn some_rpc_call(some_input_stream: Stream<Item = RpcInput) {
        let output_stream = actual_entity.some_call(some_input_stream.map(RpcParse::parse));
        return Response::new(output_stream.map(RpcEncode::encode);
    }
    
    // Calls with initial args
    pub async fn with_initial_args(stream: impl Stream<RPCMsg>) -> Result<(initial_args, Stream<Item =    Result>), BridgeErr> {
        let initial_args = stream.next().await?.parse_initial_args()?;
        (initial_args, stream.map(RPCParse::parse))
    }
    
    trait RPCParse {
        type InitialArgs = DomainInitialArgs;
        type InnerMsg = DomainInnerMsg;
        fn parse_initial_args(&self) -> Result<Self::InitialArgs> {
            match self {
                InitialArgs => ...,
                _ => return Err("Expected initial args but got another msg")
            }
        }
        fn parse(self) -> Result<Self::InnerMsg> {
            ...
        }
    }
    
@ceyhunsen ceyhunsen added code quality Improves code quality while not affecting any other P-Medium Priority: Medium labels Feb 10, 2025
@ceyhunsen ceyhunsen self-assigned this Feb 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
code quality Improves code quality while not affecting any other P-Medium Priority: Medium
Projects
None yet
Development

No branches or pull requests

1 participant