Skip to content

Commit

Permalink
client: use a strongly typed EventKey instead of a string
Browse files Browse the repository at this point in the history
  • Loading branch information
bmwill authored and bors-libra committed May 6, 2021
1 parent 867e8bc commit 5bb1e9b
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 24 deletions.
8 changes: 5 additions & 3 deletions client/json-rpc/src/broadcast_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ use diem_client::{
},
Client, MethodRequest, MethodResponse, Response, Result,
};
use diem_types::{account_address::AccountAddress, transaction::SignedTransaction};
use diem_types::{
account_address::AccountAddress, event::EventKey, transaction::SignedTransaction,
};

use crate::views::{AccountStateWithProofView, CurrencyInfoView, EventView, StateProofView};
use futures::future::join_all;
Expand Down Expand Up @@ -134,7 +136,7 @@ impl BroadcastingClient {

pub async fn get_events(
&self,
key: &str,
key: EventKey,
start_seq: u64,
limit: u64,
) -> Result<Response<Vec<EventView>>> {
Expand Down Expand Up @@ -198,7 +200,7 @@ impl BroadcastingClient {

pub async fn get_events_with_proofs(
&self,
key: &str,
key: EventKey,
start_seq: u64,
limit: u64,
) -> Result<Response<Vec<EventWithProofView>>> {
Expand Down
10 changes: 7 additions & 3 deletions json-rpc/src/tests/unit_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1225,7 +1225,11 @@ fn test_get_events_page_limit() {
let (_, client, _runtime) = create_database_client_and_runtime();

let ret = client
.get_events("13000000000000000000000000000000000000000a550c18", 0, 1001)
.get_events(
EventKey::from_hex("13000000000000000000000000000000000000000a550c18").unwrap(),
0,
1001,
)
.unwrap_err();

let error = ret.json_rpc_error().unwrap();
Expand All @@ -1250,11 +1254,11 @@ fn test_get_events() {
let event_index = 0;
let mock_db_events = mock_db.events;
let (first_event_version, first_event) = mock_db_events[event_index].clone();
let event_key = hex::encode(first_event.key().as_bytes());
let event_key = first_event.key();

let events = client
.get_events(
&event_key,
*event_key,
first_event.sequence_number(),
first_event.sequence_number() + 10,
)
Expand Down
2 changes: 1 addition & 1 deletion language/diem-tools/diem-events-fetcher/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl DiemEventsFetcher {
) -> Result<Vec<EventView>> {
let resp = self
.0
.get_events(&hex::encode(event_key.as_bytes()), start, limit)
.get_events(*event_key, start, limit)
.await?
.into_inner();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl DiemValidatorInterface for JsonRpcDebuggerInterface {
) -> Result<Vec<EventWithProof>> {
let events = self
.client
.get_events_with_proofs(key.to_string().as_str(), start_seq, limit)?
.get_events_with_proofs(*key, start_seq, limit)?
.into_inner();
let mut result = vec![];
for event in events {
Expand Down
6 changes: 3 additions & 3 deletions sdk/client/src/blocking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ impl BlockingClient {

pub fn get_events(
&self,
key: &str,
key: EventKey,
start_seq: u64,
limit: u64,
) -> Result<Response<Vec<EventView>>> {
Expand Down Expand Up @@ -238,7 +238,7 @@ impl BlockingClient {

pub fn get_events_with_proofs(
&self,
key: &str,
key: EventKey,
start_seq: u64,
limit: u64,
) -> Result<Response<Vec<EventWithProofView>>> {
Expand All @@ -256,7 +256,7 @@ impl BlockingClient {
limit: u64,
) -> Result<Response<Vec<Event<T>>>> {
let (events, state) = self
.get_events_with_proofs(&hex::encode(event_key.as_bytes()), start_seq, limit)?
.get_events_with_proofs(*event_key, start_seq, limit)?
.into_parts();
Ok(Response::new(
move_deserialize::get_events::<T>(events)?,
Expand Down
6 changes: 3 additions & 3 deletions sdk/client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ impl Client {

pub async fn get_events(
&self,
key: &str,
key: EventKey,
start_seq: u64,
limit: u64,
) -> Result<Response<Vec<EventView>>> {
Expand Down Expand Up @@ -257,7 +257,7 @@ impl Client {

pub async fn get_events_with_proofs(
&self,
key: &str,
key: EventKey,
start_seq: u64,
limit: u64,
) -> Result<Response<Vec<EventWithProofView>>> {
Expand All @@ -276,7 +276,7 @@ impl Client {
limit: u64,
) -> Result<Response<Vec<Event<T>>>> {
let (events, state) = self
.get_events_with_proofs(&hex::encode(event_key.as_bytes()), start_seq, limit)
.get_events_with_proofs(*event_key, start_seq, limit)
.await?
.into_parts();
Ok(Response::new(
Expand Down
16 changes: 9 additions & 7 deletions sdk/client/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
// SPDX-License-Identifier: Apache-2.0

use super::{JsonRpcVersion, Method};
use diem_types::{account_address::AccountAddress, transaction::SignedTransaction};
use diem_types::{
account_address::AccountAddress, event::EventKey, transaction::SignedTransaction,
};
use serde::{Deserialize, Serialize};
use std::sync::atomic::AtomicU64;

Expand All @@ -16,7 +18,7 @@ pub enum MethodRequest {
GetTransactions(u64, u64, bool),
GetAccountTransaction(AccountAddress, u64, bool),
GetAccountTransactions(AccountAddress, u64, u64, bool),
GetEvents(String, u64, u64),
GetEvents(EventKey, u64, u64),
GetCurrencies([(); 0]),
GetNetworkStatus([(); 0]),

Expand All @@ -26,7 +28,7 @@ pub enum MethodRequest {
GetStateProof((u64,)),
GetAccountStateWithProof(AccountAddress, Option<u64>, Option<u64>),
GetTransactionsWithProofs(u64, u64),
GetEventsWithProofs(String, u64, u64),
GetEventsWithProofs(EventKey, u64, u64),
}

impl MethodRequest {
Expand Down Expand Up @@ -72,8 +74,8 @@ impl MethodRequest {
Self::GetAccountTransactions(address, start_seq, limit, include_events)
}

pub fn get_events(key: &str, start_seq: u64, limit: u64) -> Self {
Self::GetEvents(key.to_owned(), start_seq, limit)
pub fn get_events(key: EventKey, start_seq: u64, limit: u64) -> Self {
Self::GetEvents(key, start_seq, limit)
}

pub fn get_currencies() -> Self {
Expand Down Expand Up @@ -103,8 +105,8 @@ impl MethodRequest {
Self::GetTransactionsWithProofs(start_version, limit)
}

pub fn get_events_with_proofs(key: &str, start_seq: u64, limit: u64) -> Self {
Self::GetEventsWithProofs(key.to_owned(), start_seq, limit)
pub fn get_events_with_proofs(key: EventKey, start_seq: u64, limit: u64) -> Self {
Self::GetEventsWithProofs(key, start_seq, limit)
}

pub fn method(&self) -> Method {
Expand Down
2 changes: 1 addition & 1 deletion sdk/compatibility/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ fn get_events() -> Result<()> {
let currencies = client.get_currencies()?.into_inner();

for currency in currencies {
client.get_events(&currency.mint_events_key.to_string(), 0, 10)?;
client.get_events(currency.mint_events_key, 0, 10)?;
}

Ok(())
Expand Down
5 changes: 3 additions & 2 deletions testsuite/cli/src/diem_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use diem_types::{
account_config::{ACCOUNT_RECEIVED_EVENT_PATH, ACCOUNT_SENT_EVENT_PATH},
account_state_blob::AccountStateBlob,
epoch_change::EpochChangeProof,
event::EventKey,
ledger_info::LedgerInfoWithSignatures,
transaction::{SignedTransaction, Version},
trusted_state::{TrustedState, TrustedStateChange},
Expand Down Expand Up @@ -90,7 +91,7 @@ impl DiemClient {

pub fn get_events(
&self,
event_key: &str,
event_key: EventKey,
start: u64,
limit: u64,
) -> Result<Vec<views::EventView>> {
Expand Down Expand Up @@ -244,7 +245,7 @@ impl DiemClient {
};

// get_events
let events = self.get_events(&event_key.to_string(), start_event_seq_num, limit)?;
let events = self.get_events(*event_key, start_event_seq_num, limit)?;
Ok((events, account_view))
}
}
Expand Down

0 comments on commit 5bb1e9b

Please sign in to comment.