diff --git a/api/src/accounts.rs b/api/src/accounts.rs index 03b3c0e271934..5d81cb5910769 100644 --- a/api/src/accounts.rs +++ b/api/src/accounts.rs @@ -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, @@ -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(), )); } diff --git a/api/src/blocks.rs b/api/src/blocks.rs index 04b381fe196f1..ce9df25c65e3d 100644 --- a/api/src/blocks.rs +++ b/api/src/blocks.rs @@ -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/ @@ -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(), )); } diff --git a/api/src/page.rs b/api/src/page.rs index d74df462195dd..7e64c683153e3 100644 --- a/api/src/page.rs +++ b/api/src/page.rs @@ -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; @@ -28,7 +28,7 @@ impl Page { if version > max { return Err(Error::not_found( "transaction", - TransactionId::Version(version), + TransactionId::Version(U64::from(version)), max, )); } diff --git a/api/src/poem_backend/accounts.rs b/api/src/poem_backend/accounts.rs index f25571d154ea2..e6a9bd4d69ea7 100644 --- a/api/src/poem_backend/accounts.rs +++ b/api/src/poem_backend/accounts.rs @@ -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; @@ -49,7 +49,7 @@ impl AccountsApi { &self, accept: Accept, address: Path
, - ledger_version: Query>, + ledger_version: Query>, ) -> BasicResultWith404 { fail_point_poem("endpoint_get_account")?; let accept_type = parse_accept(&accept)?; @@ -75,7 +75,7 @@ impl AccountsApi { &self, accept: Accept, address: Path
, - ledger_version: Query>, + ledger_version: Query>, ) -> BasicResultWith404> { fail_point_poem("endpoint_get_account_resources")?; let accept_type = parse_accept(&accept)?; @@ -99,7 +99,7 @@ impl AccountsApi { &self, accept: Accept, address: Path
, - ledger_version: Query>, + ledger_version: Query>, ) -> BasicResultWith404> { fail_point_poem("endpoint_get_account_modules")?; let accept_type = parse_accept(&accept)?; @@ -119,16 +119,17 @@ impl Account { pub fn new( context: Arc, address: Address, - requested_ledger_version: Option, + requested_ledger_version: Option, ) -> Result { 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(), )); } diff --git a/api/src/poem_backend/events.rs b/api/src/poem_backend/events.rs index 9af4b5ad6c975..197a0b9730dab 100644 --- a/api/src/poem_backend/events.rs +++ b/api/src/poem_backend/events.rs @@ -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; @@ -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, - start: Query>, + start: Query>, limit: Query>, ) -> BasicResultWith404> { 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) } @@ -67,12 +67,13 @@ impl EventsApi { address: Path
, event_handle: Path, field_name: Path, - start: Query>, + start: Query>, limit: Query>, ) -> BasicResultWith404> { + // 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())? diff --git a/api/src/poem_backend/response.rs b/api/src/poem_backend/response.rs index 1166eb28fe3ea..632a485fbacb9 100644 --- a/api/src/poem_backend/response.rs +++ b/api/src/poem_backend/response.rs @@ -237,10 +237,10 @@ macro_rules! generate_success_response { $name( $crate::poem_backend::AptosResponseContent, #[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, ), )* } @@ -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, ) }, diff --git a/api/src/poem_backend/state.rs b/api/src/poem_backend/state.rs index 3f3676bff1cf6..a05d0149cbaff 100644 --- a/api/src/poem_backend/state.rs +++ b/api/src/poem_backend/state.rs @@ -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; @@ -55,7 +55,7 @@ impl StateApi { accept: Accept, address: Path
, resource_type: Path, - ledger_version: Query>, + ledger_version: Query>, ) -> BasicResultWith404 { fail_point_poem("endpoint_get_account_resource")?; let accept_type = parse_accept(&accept)?; @@ -82,7 +82,7 @@ impl StateApi { accept: Accept, address: Path
, module_name: Path, - ledger_version: Query>, + ledger_version: Query>, ) -> BasicResultWith404 { fail_point_poem("endpoint_get_account_module")?; let accept_type = parse_accept(&accept)?; @@ -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, table_item_request: Json, - ledger_version: Query>, + ledger_version: Query>, ) -> BasicResultWith404 { // 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, ) @@ -122,16 +121,17 @@ impl StateApi { impl StateApi { fn preprocess_request( &self, - requested_ledger_version: Option, + requested_ledger_version: Option, ) -> 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(), )); } @@ -148,7 +148,7 @@ impl StateApi { accept_type: &AcceptType, address: Address, resource_type: MoveStructTagWrapper, - ledger_version: Option, + ledger_version: Option, ) -> BasicResultWith404 { let resource_type: MoveStructTag = resource_type.into(); let resource_type: StructTag = resource_type @@ -185,7 +185,7 @@ impl StateApi { accept_type: &AcceptType, address: Address, name: IdentifierWrapper, - ledger_version: Option, + ledger_version: Option, ) -> BasicResultWith404 { let module_id = ModuleId::new(address.into(), name.into()); let access_path = AccessPath::code_access_path(module_id.clone()); @@ -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, + ledger_version: Option, ) -> BasicResultWith404 { let key_type = table_item_request .key_type @@ -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!( diff --git a/api/src/poem_backend/transactions.rs b/api/src/poem_backend/transactions.rs index 5a8ae4146971c..7feda70f94e79 100644 --- a/api/src/poem_backend/transactions.rs +++ b/api/src/poem_backend/transactions.rs @@ -86,12 +86,12 @@ impl TransactionsApi { async fn get_transactions( &self, accept: Accept, - start: Query>, + start: Query>, limit: Query>, ) -> BasicResultWith404> { 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) } @@ -152,12 +152,12 @@ impl TransactionsApi { &self, accept: Accept, address: Path
, - start: Query>, + start: Query>, limit: Query>, ) -> BasicResultWith404> { 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) } diff --git a/api/src/state.rs b/api/src/state.rs index 2037cb473adda..ab1f543a66fa4 100644 --- a/api/src/state.rs +++ b/api/src/state.rs @@ -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; @@ -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(), )); } diff --git a/api/src/transactions.rs b/api/src/transactions.rs index 06c9f26919a38..3d269b7840769 100644 --- a/api/src/transactions.rs +++ b/api/src/transactions.rs @@ -428,7 +428,7 @@ impl Transactions { ) -> Result { 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))?; diff --git a/api/types/src/block.rs b/api/types/src/block.rs index 99d0ecfb2ab7f..7478a33c64960 100644 --- a/api/types/src/block.rs +++ b/api/types/src/block.rs @@ -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 { diff --git a/api/types/src/ledger_info.rs b/api/types/src/ledger_info.rs index 7a2e43dcc1daa..4206e3b7380f0 100644 --- a/api/types/src/ledger_info.rs +++ b/api/types/src/ledger_info.rs @@ -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, @@ -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() } diff --git a/api/types/src/move_types.rs b/api/types/src/move_types.rs index c85aa2a77277e..493ba91cfa372 100644 --- a/api/types/src/move_types.rs +++ b/api/types/src/move_types.rs @@ -23,7 +23,7 @@ use move_deps::{ move_resource_viewer::{AnnotatedMoveStruct, AnnotatedMoveValue}, }; -use poem_openapi::{Enum, NewType, Object}; +use poem_openapi::{Enum, Object}; use serde::{de::Error as _, Deserialize, Deserializer, Serialize, Serializer}; use std::{ collections::BTreeMap, @@ -52,7 +52,7 @@ impl TryFrom for MoveResource { } } -#[derive(Clone, Debug, Default, Eq, PartialEq, Copy, NewType)] +#[derive(Clone, Debug, Default, Eq, PartialEq, Copy)] pub struct U64(pub u64); impl U64 { @@ -67,18 +67,6 @@ impl From for U64 { } } -impl FromStr for U64 { - type Err = anyhow::Error; - - fn from_str(s: &str) -> Result { - let data = s - .parse::() - .map_err(|e| format_err!("parse u64 string {:?} failed, caused by error: {}", s, e))?; - - Ok(U64(data)) - } -} - impl From for warp::http::header::HeaderValue { fn from(d: U64) -> Self { d.0.into() @@ -119,8 +107,20 @@ impl<'de> Deserialize<'de> for U64 { } } +impl FromStr for U64 { + type Err = anyhow::Error; + + fn from_str(s: &str) -> Result { + let data = s.parse::().map_err(|e| { + format_err!("Parsing u64 string {:?} failed, caused by error: {}", s, e) + })?; + + Ok(U64(data)) + } +} + #[derive(Clone, Debug, Default, PartialEq, Copy)] -pub struct U128(u128); +pub struct U128(pub u128); impl U128 { pub fn inner(&self) -> &u128 { @@ -168,9 +168,9 @@ impl FromStr for U128 { type Err = anyhow::Error; fn from_str(s: &str) -> Result { - let data = s - .parse::() - .map_err(|e| format_err!("parse u128 string {:?} failed, caused by error: {}", s, e))?; + let data = s.parse::().map_err(|e| { + format_err!("Parsing u128 string {:?} failed, caused by error: {}", s, e) + })?; Ok(U128(data)) } @@ -1334,6 +1334,7 @@ impl_poem_type!( MoveType, HexEncodedBytes, MoveValue, + U64, U128 ); -impl_poem_parameter!(HexEncodedBytes, U128); +impl_poem_parameter!(HexEncodedBytes, U64, U128); diff --git a/api/types/src/transaction.rs b/api/types/src/transaction.rs index f7c9dbc619a3f..b8e4e973f77fa 100755 --- a/api/types/src/transaction.rs +++ b/api/types/src/transaction.rs @@ -861,7 +861,7 @@ impl From for TransactionSignature { #[oai(one_of)] pub enum TransactionId { Hash(HashValue), - Version(u64), + Version(U64), } impl FromStr for TransactionId { @@ -869,7 +869,7 @@ impl FromStr for TransactionId { fn from_str(hash_or_version: &str) -> Result { let id = match hash_or_version.parse::() { - Ok(version) => TransactionId::Version(version), + Ok(version) => TransactionId::Version(U64::from(version)), Err(_) => TransactionId::Hash(hash_or_version.parse()?), }; Ok(id) diff --git a/crates/aptos-faucet/src/main.rs b/crates/aptos-faucet/src/main.rs index 0b6c4b8a724f2..83be1f43e3802 100644 --- a/crates/aptos-faucet/src/main.rs +++ b/crates/aptos-faucet/src/main.rs @@ -224,7 +224,7 @@ mod tests { fn response(body: &T) -> warp::reply::Response { let li = LedgerInfo { chain_id: ChainId::test().id(), - epoch: 1, + epoch: 1.into(), ledger_version: 5.into(), oldest_ledger_version: 0.into(), ledger_timestamp: 5.into(), diff --git a/crates/aptos-rest-client/src/lib.rs b/crates/aptos-rest-client/src/lib.rs index 87aaac9891e94..61dfefaba32e4 100644 --- a/crates/aptos-rest-client/src/lib.rs +++ b/crates/aptos-rest-client/src/lib.rs @@ -80,6 +80,7 @@ impl Client { #[derive(Deserialize)] struct Response { chain_id: u8, + #[serde(deserialize_with = "types::deserialize_from_string")] epoch: u64, #[serde(deserialize_with = "types::deserialize_from_string")] ledger_version: u64,