1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Copyright (c) The Diem Core Contributors
// SPDX-License-Identifier: Apache-2.0

use crate::{errors::JsonRpcError, request::RawJsonRpcRequest, Id, JsonRpcVersion};
use diem_types::event::EventKey;
use serde::{Deserialize, Serialize};
use std::str::FromStr;

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct StreamJsonRpcRequest {
    jsonrpc: JsonRpcVersion,
    #[serde(flatten)]
    pub method_request: StreamMethodRequest,
    pub id: Id,
}

impl StreamJsonRpcRequest {
    pub fn new(method_request: StreamMethodRequest, id: Id) -> Self {
        Self {
            jsonrpc: JsonRpcVersion::V2,
            method_request,
            id,
        }
    }

    fn finish_parsing(
        jsonrpc: serde_json::Value,
        method: serde_json::Value,
        params: serde_json::Value,
        id: Id,
    ) -> Result<Self, (JsonRpcError, Option<StreamMethod>, Option<Id>)> {
        let jsonrpc: JsonRpcVersion = serde_json::from_value(jsonrpc)
            .map_err(|_| (JsonRpcError::invalid_request(), None, Some(id.clone())))?;
        let method: StreamMethod = serde_json::from_value(method)
            .map_err(|_| (JsonRpcError::method_not_found(), None, Some(id.clone())))?;
        let method_request = StreamMethodRequest::from_value(method, params).map_err(|_| {
            (
                JsonRpcError::invalid_params(method.as_str()),
                Some(method),
                Some(id.clone()),
            )
        })?;

        Ok(StreamJsonRpcRequest {
            jsonrpc,
            method_request,
            id,
        })
    }

    pub fn method_name(&self) -> &'static str {
        self.method_request.method_name()
    }
}

impl FromStr for StreamJsonRpcRequest {
    type Err = (JsonRpcError, Option<StreamMethod>, Option<Id>);

    fn from_str(string: &str) -> Result<Self, (JsonRpcError, Option<StreamMethod>, Option<Id>)> {
        let RawJsonRpcRequest {
            jsonrpc,
            method,
            params,
            id,
        } = serde_json::from_str(string)
            .map_err(|_| (JsonRpcError::invalid_format(), None, None))?;
        StreamJsonRpcRequest::finish_parsing(jsonrpc, method, params, id)
    }
}

#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
#[serde(rename_all = "snake_case")]
#[serde(tag = "method", content = "params")]
pub enum StreamMethodRequest {
    SubscribeToTransactions(SubscribeToTransactionsParams),
    SubscribeToEvents(SubscribeToEventsParams),
    Unsubscribe,
}

impl StreamMethodRequest {
    pub fn method_name(&self) -> &'static str {
        self.method().as_str()
    }

    pub fn from_value(
        method: StreamMethod,
        value: serde_json::Value,
    ) -> Result<Self, serde_json::Error> {
        let method_request = match method {
            StreamMethod::SubscribeToTransactions => {
                StreamMethodRequest::SubscribeToTransactions(serde_json::from_value(value)?)
            }
            StreamMethod::SubscribeToEvents => {
                StreamMethodRequest::SubscribeToEvents(serde_json::from_value(value)?)
            }
            StreamMethod::Unsubscribe => StreamMethodRequest::Unsubscribe,
        };

        Ok(method_request)
    }

    pub fn method(&self) -> StreamMethod {
        match self {
            StreamMethodRequest::SubscribeToTransactions(_) => {
                StreamMethod::SubscribeToTransactions
            }
            StreamMethodRequest::SubscribeToEvents(_) => StreamMethod::SubscribeToEvents,
            StreamMethodRequest::Unsubscribe => StreamMethod::Unsubscribe,
        }
    }
}

#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum StreamMethod {
    SubscribeToTransactions,
    SubscribeToEvents,
    Unsubscribe,
}

impl StreamMethod {
    pub fn as_str(&self) -> &'static str {
        match self {
            StreamMethod::SubscribeToTransactions => "subscribe_to_transactions",
            StreamMethod::SubscribeToEvents => "subscribe_to_events",
            StreamMethod::Unsubscribe => "unsubscribe",
        }
    }
}

#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
pub struct SubscribeToEventsParams {
    pub event_key: EventKey,
    pub event_seq_num: u64,
}

#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
pub struct SubscribeToTransactionsParams {
    pub starting_version: u64,
    pub include_events: Option<bool>,
}