Skip to content

Commit

Permalink
diem-rest-client: introduce a new rest client
Browse files Browse the repository at this point in the history
  • Loading branch information
bmwill authored and bors-libra committed Nov 19, 2021
1 parent 4992a8c commit fbc8cc6
Show file tree
Hide file tree
Showing 13 changed files with 493 additions and 11 deletions.
23 changes: 23 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ members = [
"crates/diem-crypto-derive",
"crates/diem-faucet",
"crates/diem-json-rpc-client",
"crates/diem-rest-client",
"crates/proxy",
"crates/swiss-knife",
"crates/transaction-emitter",
Expand Down
14 changes: 7 additions & 7 deletions api/types/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ impl<T: TransactionInfoTrait>
}
}

#[derive(Clone, Debug, PartialEq, Serialize)]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum Transaction {
PendingTransaction(PendingTransaction),
Expand Down Expand Up @@ -179,7 +179,7 @@ impl From<(&SignedTransaction, TransactionPayload)> for UserTransactionRequest {
}
}

#[derive(Clone, Debug, PartialEq, Serialize)]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct TransactionInfo {
pub version: U64,
pub hash: HashValue,
Expand All @@ -190,14 +190,14 @@ pub struct TransactionInfo {
pub vm_status: String,
}

#[derive(Clone, Debug, PartialEq, Serialize)]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct PendingTransaction {
pub hash: HashValue,
#[serde(flatten)]
pub request: UserTransactionRequest,
}

#[derive(Clone, Debug, PartialEq, Serialize)]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct UserTransaction {
#[serde(flatten)]
pub info: TransactionInfo,
Expand All @@ -219,15 +219,15 @@ pub struct UserTransactionRequest {
pub signature: Option<TransactionSignature>,
}

#[derive(Clone, Debug, PartialEq, Serialize)]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct GenesisTransaction {
#[serde(flatten)]
pub info: TransactionInfo,
pub payload: GenesisPayload,
pub events: Vec<Event>,
}

#[derive(Clone, Debug, PartialEq, Serialize)]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct BlockMetadataTransaction {
#[serde(flatten)]
pub info: TransactionInfo,
Expand Down Expand Up @@ -260,7 +260,7 @@ impl From<(&ContractEvent, serde_json::Value)> for Event {
}
}

#[derive(Clone, Debug, PartialEq, Serialize)]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum GenesisPayload {
WriteSetPayload(WriteSetPayload),
Expand Down
1 change: 1 addition & 0 deletions crates/diem-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ hex = "0.4.3"
serde = { version = "1.0.124", features = ["derive"] }
serde_json = "1.0.64"
tracing = "0.1.26"
url = "2.2.2"

diem-crypto = { path = "../diem-crypto", version = "0.0.3" }
diem-json-rpc-types = { path = "../../json-rpc/types", version = "0.0.3" }
Expand Down
8 changes: 8 additions & 0 deletions crates/diem-client/src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ impl<T> Response<T> {
Err(err) => Err(err),
}
}

pub fn map<U, F>(self, f: F) -> Response<U>
where
F: FnOnce(T) -> U,
{
let (inner, state) = self.into_parts();
Response::new(f(inner), state)
}
}

#[allow(clippy::large_enum_variant)]
Expand Down
33 changes: 32 additions & 1 deletion crates/diem-client/src/state.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Copyright (c) The Diem Core Contributors
// SPDX-License-Identifier: Apache-2.0

use diem_json_rpc_types::response::JsonRpcResponse;
use diem_json_rpc_types::response::{
JsonRpcResponse, X_DIEM_CHAIN_ID, X_DIEM_TIMESTAMP_USEC_ID, X_DIEM_VERSION_ID,
};
use std::cmp::max;

#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd)]
Expand All @@ -19,6 +21,35 @@ impl State {
timestamp_usecs: resp.diem_ledger_timestampusec,
}
}

pub fn from_headers(headers: &reqwest::header::HeaderMap) -> anyhow::Result<Self> {
let maybe_chain_id = headers
.get(X_DIEM_CHAIN_ID)
.and_then(|h| h.to_str().ok())
.and_then(|s| s.parse().ok());
let maybe_version = headers
.get(X_DIEM_VERSION_ID)
.and_then(|h| h.to_str().ok())
.and_then(|s| s.parse().ok());
let maybe_timestamp = headers
.get(X_DIEM_TIMESTAMP_USEC_ID)
.and_then(|h| h.to_str().ok())
.and_then(|s| s.parse().ok());

let state = if let (Some(chain_id), Some(version), Some(timestamp_usecs)) =
(maybe_chain_id, maybe_version, maybe_timestamp)
{
Self {
chain_id,
version,
timestamp_usecs,
}
} else {
todo!()
};

Ok(state)
}
}

cfg_async_or_blocking! {
Expand Down
28 changes: 28 additions & 0 deletions crates/diem-rest-client/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
[package]
name = "diem-rest-client"
version = "0.0.0"
authors = ["Diem Association <[email protected]>"]
description = "Diem Rest client"
repository = "https://github.com/diem/diem"
homepage = "https://diem.com"
license = "Apache-2.0"
publish = false
edition = "2018"

[dependencies]
anyhow = "1.0.38"
bcs = "0.1.2"
hex = "0.4.3"
reqwest = { version = "0.11.2", features = ["json"] }
serde = { version = "1.0.124", features = ["derive"] }
serde_json = "1.0.64"
tokio = { version = "1.8.1", features = ["full"] }
url = "2.2.2"

diem-api-types = { path = "../../api/types" }
diem-client = { path = "../diem-client" }
diem-crypto = { path = "../diem-crypto" }
diem-json-rpc-types = { path = "../../json-rpc/types" }
diem-types = { path = "../../types" }
diem-workspace-hack = { path = "../../common/workspace-hack" }
move-core-types = { path = "../../language/move-core/types" }
Loading

0 comments on commit fbc8cc6

Please sign in to comment.