Skip to content

Commit

Permalink
[API] Use U64 and U128 at all endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
banool committed Jul 24, 2022
1 parent ba0d759 commit 60027e6
Show file tree
Hide file tree
Showing 16 changed files with 83 additions and 71 deletions.
4 changes: 2 additions & 2 deletions api/src/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{

use aptos_api_types::{
AccountData, Address, AsConverter, Error, LedgerInfo, MoveModuleBytecode, Response,
TransactionId,
TransactionId, U64,
};
use aptos_types::{
account_config::AccountResource,
Expand Down Expand Up @@ -113,7 +113,7 @@ impl Account {
if ledger_version > latest_ledger_info.version() {
return Err(Error::not_found(
"ledger",
TransactionId::Version(ledger_version),
TransactionId::Version(U64::from(ledger_version)),
latest_ledger_info.version(),
));
}
Expand Down
4 changes: 2 additions & 2 deletions api/src/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use crate::{context::Context, failpoint::fail_point, metrics::metrics, param::LedgerVersionParam};
use anyhow::Result;
use aptos_api_types::{Error, LedgerInfo, Response, TransactionId};
use aptos_api_types::{Error, LedgerInfo, Response, TransactionId, U64};
use warp::{filters::BoxedFilter, Filter, Rejection, Reply};

// GET /blocks/<version>
Expand Down Expand Up @@ -43,7 +43,7 @@ impl Block {
if ledger_version > latest_ledger_info.version() {
return Err(Error::not_found(
"ledger",
TransactionId::Version(ledger_version),
TransactionId::Version(U64::from(ledger_version)),
latest_ledger_info.version(),
));
}
Expand Down
4 changes: 2 additions & 2 deletions api/src/page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use crate::param::{Param, TransactionVersionParam};

use aptos_api_types::{Error, TransactionId};
use aptos_api_types::{Error, TransactionId, U64};

use anyhow::Result;
use serde::Deserialize;
Expand All @@ -28,7 +28,7 @@ impl Page {
if version > max {
return Err(Error::not_found(
"transaction",
TransactionId::Version(version),
TransactionId::Version(U64::from(version)),
max,
));
}
Expand Down
17 changes: 9 additions & 8 deletions api/src/poem_backend/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use super::{AptosErrorCode, BasicErrorWith404, BasicResultWith404};
use crate::context::Context;
use crate::failpoint::fail_point_poem;
use anyhow::Context as AnyhowContext;
use aptos_api_types::{AccountData, Address, AsConverter, MoveStructTag, TransactionId};
use aptos_api_types::{AccountData, Address, AsConverter, MoveStructTag, TransactionId, U64};
use aptos_api_types::{LedgerInfo, MoveModuleBytecode, MoveResource};
use aptos_types::access_path::AccessPath;
use aptos_types::account_config::AccountResource;
Expand Down Expand Up @@ -49,7 +49,7 @@ impl AccountsApi {
&self,
accept: Accept,
address: Path<Address>,
ledger_version: Query<Option<u64>>,
ledger_version: Query<Option<U64>>,
) -> BasicResultWith404<AccountData> {
fail_point_poem("endpoint_get_account")?;
let accept_type = parse_accept(&accept)?;
Expand All @@ -75,7 +75,7 @@ impl AccountsApi {
&self,
accept: Accept,
address: Path<Address>,
ledger_version: Query<Option<u64>>,
ledger_version: Query<Option<U64>>,
) -> BasicResultWith404<Vec<MoveResource>> {
fail_point_poem("endpoint_get_account_resources")?;
let accept_type = parse_accept(&accept)?;
Expand All @@ -99,7 +99,7 @@ impl AccountsApi {
&self,
accept: Accept,
address: Path<Address>,
ledger_version: Query<Option<u64>>,
ledger_version: Query<Option<U64>>,
) -> BasicResultWith404<Vec<MoveModuleBytecode>> {
fail_point_poem("endpoint_get_account_modules")?;
let accept_type = parse_accept(&accept)?;
Expand All @@ -119,16 +119,17 @@ impl Account {
pub fn new(
context: Arc<Context>,
address: Address,
requested_ledger_version: Option<u64>,
requested_ledger_version: Option<U64>,
) -> Result<Self, BasicErrorWith404> {
let latest_ledger_info = context.get_latest_ledger_info_poem()?;
let ledger_version: u64 =
requested_ledger_version.unwrap_or_else(|| latest_ledger_info.version());
let ledger_version: u64 = requested_ledger_version
.map(|v| v.0)
.unwrap_or_else(|| latest_ledger_info.version());

if ledger_version > latest_ledger_info.version() {
return Err(build_not_found(
"ledger",
TransactionId::Version(ledger_version),
TransactionId::Version(U64::from(ledger_version)),
latest_ledger_info.version(),
));
}
Expand Down
11 changes: 6 additions & 5 deletions api/src/poem_backend/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use super::{
use crate::context::Context;
use crate::failpoint::fail_point_poem;
use anyhow::Context as AnyhowContext;
use aptos_api_types::{Address, EventKey, IdentifierWrapper, MoveStructTagWrapper};
use aptos_api_types::{Address, EventKey, IdentifierWrapper, MoveStructTagWrapper, U64};
use aptos_api_types::{AsConverter, Event};
use poem::web::Accept;
use poem_openapi::param::Query;
Expand Down Expand Up @@ -41,12 +41,12 @@ impl EventsApi {
// Consider unpacking the inner EventKey type and taking two params, the creation
// number and the address.
event_key: Path<EventKey>,
start: Query<Option<u64>>,
start: Query<Option<U64>>,
limit: Query<Option<u16>>,
) -> BasicResultWith404<Vec<Event>> {
fail_point_poem("endpoint_get_events_by_event_key")?;
let accept_type = parse_accept(&accept)?;
let page = Page::new(start.0, limit.0);
let page = Page::new(start.0.map(|v| v.0), limit.0);
self.list(&accept_type, page, event_key.0)
}

Expand All @@ -67,12 +67,13 @@ impl EventsApi {
address: Path<Address>,
event_handle: Path<MoveStructTagWrapper>,
field_name: Path<IdentifierWrapper>,
start: Query<Option<u64>>,
start: Query<Option<U64>>,
limit: Query<Option<u16>>,
) -> BasicResultWith404<Vec<Event>> {
// TODO: Assert that Event represents u64s as strings.
fail_point_poem("endpoint_get_events_by_event_handle")?;
let accept_type = parse_accept(&accept)?;
let page = Page::new(start.0, limit.0);
let page = Page::new(start.0.map(|v| v.0), limit.0);
let account = Account::new(self.context.clone(), address.0, None)?;
let key = account
.find_event_key(event_handle.0.into(), field_name.0.into())?
Expand Down
14 changes: 7 additions & 7 deletions api/src/poem_backend/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,10 +237,10 @@ macro_rules! generate_success_response {
$name(
$crate::poem_backend::AptosResponseContent<T>,
#[oai(header = "X-Aptos-Chain-Id")] u16,
#[oai(header = "X-Aptos-Ledger-Version")] u64,
#[oai(header = "X-Aptos-Ledger-Oldest-Version")] u64,
#[oai(header = "X-Aptos-Ledger-TimestampUsec")] u64,
#[oai(header = "X-Aptos-Epoch")] u64,
#[oai(header = "X-Aptos-Ledger-Version")] U64,
#[oai(header = "X-Aptos-Ledger-Oldest-Version")] U64,
#[oai(header = "X-Aptos-Ledger-TimestampUsec")] U64,
#[oai(header = "X-Aptos-Epoch")] U64,
),
)*
}
Expand Down Expand Up @@ -274,9 +274,9 @@ macro_rules! generate_success_response {
$enum_name::$name(
value,
ledger_info.chain_id as u16,
ledger_info.ledger_version.into(),
ledger_info.oldest_ledger_version.into(),
ledger_info.ledger_timestamp.into(),
ledger_info.ledger_version,
ledger_info.oldest_ledger_version,
ledger_info.ledger_timestamp,
ledger_info.epoch,
)
},
Expand Down
30 changes: 15 additions & 15 deletions api/src/poem_backend/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::failpoint::fail_point_poem;
use anyhow::Context as AnyhowContext;
use aptos_api_types::{
Address, AsConverter, IdentifierWrapper, MoveModuleBytecode, MoveStructTag,
MoveStructTagWrapper, MoveValue, TableItemRequest, TransactionId, U128,
MoveStructTagWrapper, MoveValue, TableItemRequest, TransactionId, U128, U64,
};
use aptos_api_types::{LedgerInfo, MoveResource};
use aptos_state_view::StateView;
Expand Down Expand Up @@ -55,7 +55,7 @@ impl StateApi {
accept: Accept,
address: Path<Address>,
resource_type: Path<MoveStructTagWrapper>,
ledger_version: Query<Option<u64>>,
ledger_version: Query<Option<U64>>,
) -> BasicResultWith404<MoveResource> {
fail_point_poem("endpoint_get_account_resource")?;
let accept_type = parse_accept(&accept)?;
Expand All @@ -82,7 +82,7 @@ impl StateApi {
accept: Accept,
address: Path<Address>,
module_name: Path<IdentifierWrapper>,
ledger_version: Query<Option<u64>>,
ledger_version: Query<Option<U64>>,
) -> BasicResultWith404<MoveModuleBytecode> {
fail_point_poem("endpoint_get_account_module")?;
let accept_type = parse_accept(&accept)?;
Expand All @@ -102,17 +102,16 @@ impl StateApi {
async fn get_table_item(
&self,
accept: Accept,
// TODO: Cut over to u128 or U128 once https://github.com/poem-web/poem/pull/336 lands.
table_handle: Path<U128>,
table_item_request: Json<TableItemRequest>,
ledger_version: Query<Option<u64>>,
ledger_version: Query<Option<U64>>,
) -> BasicResultWith404<MoveValue> {
// TODO: fail_point could just be middleware.
fail_point_poem("endpoint_get_table_item")?;
let accept_type = parse_accept(&accept)?;
self.table_item(
&accept_type,
table_handle.0.into(),
table_handle.0,
table_item_request.0,
ledger_version.0,
)
Expand All @@ -122,16 +121,17 @@ impl StateApi {
impl StateApi {
fn preprocess_request<E: NotFoundError + InternalError>(
&self,
requested_ledger_version: Option<u64>,
requested_ledger_version: Option<U64>,
) -> Result<(LedgerInfo, u64, DbStateView), E> {
let latest_ledger_info = self.context.get_latest_ledger_info_poem()?;
let ledger_version: u64 =
requested_ledger_version.unwrap_or_else(|| latest_ledger_info.version());
let ledger_version: u64 = requested_ledger_version
.map(|v| v.0)
.unwrap_or_else(|| latest_ledger_info.version());

if ledger_version > latest_ledger_info.version() {
return Err(build_not_found(
"ledger",
TransactionId::Version(ledger_version),
TransactionId::Version(U64::from(ledger_version)),
latest_ledger_info.version(),
));
}
Expand All @@ -148,7 +148,7 @@ impl StateApi {
accept_type: &AcceptType,
address: Address,
resource_type: MoveStructTagWrapper,
ledger_version: Option<u64>,
ledger_version: Option<U64>,
) -> BasicResultWith404<MoveResource> {
let resource_type: MoveStructTag = resource_type.into();
let resource_type: StructTag = resource_type
Expand Down Expand Up @@ -185,7 +185,7 @@ impl StateApi {
accept_type: &AcceptType,
address: Address,
name: IdentifierWrapper,
ledger_version: Option<u64>,
ledger_version: Option<U64>,
) -> BasicResultWith404<MoveModuleBytecode> {
let module_id = ModuleId::new(address.into(), name.into());
let access_path = AccessPath::code_access_path(module_id.clone());
Expand Down Expand Up @@ -213,9 +213,9 @@ impl StateApi {
pub fn table_item(
&self,
accept_type: &AcceptType,
table_handle: u128,
table_handle: U128,
table_item_request: TableItemRequest,
ledger_version: Option<u64>,
ledger_version: Option<U64>,
) -> BasicResultWith404<MoveValue> {
let key_type = table_item_request
.key_type
Expand All @@ -242,7 +242,7 @@ impl StateApi {
.simple_serialize()
.ok_or_else(|| BasicErrorWith404::internal_str("Failed to serialize table key"))?;

let state_key = StateKey::table_item(TableHandle(table_handle), raw_key);
let state_key = StateKey::table_item(TableHandle(table_handle.0), raw_key);
let bytes = state_view
.get_state_value(&state_key)
.context(format!(
Expand Down
8 changes: 4 additions & 4 deletions api/src/poem_backend/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,12 @@ impl TransactionsApi {
async fn get_transactions(
&self,
accept: Accept,
start: Query<Option<u64>>,
start: Query<Option<U64>>,
limit: Query<Option<u16>>,
) -> BasicResultWith404<Vec<Transaction>> {
fail_point_poem("endppoint_get_transactions")?;
let accept_type = parse_accept(&accept)?;
let page = Page::new(start.0, limit.0);
let page = Page::new(start.0.map(|v| v.0), limit.0);
self.list(&accept_type, page)
}

Expand Down Expand Up @@ -152,12 +152,12 @@ impl TransactionsApi {
&self,
accept: Accept,
address: Path<Address>,
start: Query<Option<u64>>,
start: Query<Option<U64>>,
limit: Query<Option<u16>>,
) -> BasicResultWith404<Vec<Transaction>> {
fail_point_poem("endpoint_get_accounts_transactions")?;
let accept_type = parse_accept(&accept)?;
let page = Page::new(start.0, limit.0);
let page = Page::new(start.0.map(|v| v.0), limit.0);
self.list_by_account(&accept_type, page, address.0)
}

Expand Down
3 changes: 2 additions & 1 deletion api/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use crate::{
use anyhow::anyhow;
use aptos_api_types::{
AsConverter, Error, LedgerInfo, MoveModuleBytecode, Response, TableItemRequest, TransactionId,
U64,
};
use aptos_state_view::StateView;
use aptos_types::state_store::table::TableHandle;
Expand Down Expand Up @@ -131,7 +132,7 @@ impl State {
if ledger_version > latest_ledger_info.version() {
return Err(Error::not_found(
"ledger",
TransactionId::Version(ledger_version),
TransactionId::Version(U64::from(ledger_version)),
latest_ledger_info.version(),
));
}
Expand Down
2 changes: 1 addition & 1 deletion api/src/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ impl Transactions {
) -> Result<impl Reply, Error> {
let txn_data = match id.clone() {
TransactionId::Hash(hash) => self.get_by_hash(hash.into()).await?,
TransactionId::Version(version) => self.get_by_version(version)?,
TransactionId::Version(version) => self.get_by_version(version.0)?,
}
.ok_or_else(|| self.transaction_not_found(id))?;

Expand Down
3 changes: 3 additions & 0 deletions api/types/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
use crate::HashValue;
use serde::{Deserialize, Serialize};

// TODO: Consider including this in the API.
// If we do that, change these u64s to U64.

/// A description of a block
#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
pub struct BlockInfo {
Expand Down
8 changes: 6 additions & 2 deletions api/types/src/ledger_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, PoemObject)]
pub struct LedgerInfo {
pub chain_id: u8,
pub epoch: u64,
pub epoch: U64,
pub ledger_version: U64,
pub oldest_ledger_version: U64,
pub ledger_timestamp: U64,
Expand All @@ -26,13 +26,17 @@ impl LedgerInfo {
let ledger_info = info.ledger_info();
Self {
chain_id: chain_id.id(),
epoch: ledger_info.epoch(),
epoch: U64::from(ledger_info.epoch()),
ledger_version: ledger_info.version().into(),
oldest_ledger_version: oldest_ledger_version.into(),
ledger_timestamp: ledger_info.timestamp_usecs().into(),
}
}

pub fn epoch(&self) -> u64 {
self.epoch.into()
}

pub fn version(&self) -> u64 {
self.ledger_version.into()
}
Expand Down
Loading

0 comments on commit 60027e6

Please sign in to comment.