Skip to content

Commit

Permalink
merge two traits
Browse files Browse the repository at this point in the history
  • Loading branch information
areshand committed May 29, 2024
1 parent f253b5c commit 5d4ec3b
Show file tree
Hide file tree
Showing 23 changed files with 153 additions and 148 deletions.
1 change: 0 additions & 1 deletion api/openapi-spec-generator/src/fake_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,5 @@ pub fn get_fake_context() -> Context {
mempool.ac_client,
NodeConfig::default(),
None, /* table info reader */
None,
)
}
16 changes: 4 additions & 12 deletions api/src/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,10 +342,8 @@ impl Account {
let state_view = self
.context
.latest_state_view_poem(&self.latest_ledger_info)?;
let converter = state_view.as_converter(
self.context.db.clone(),
self.context.table_info_reader.clone(),
);
let converter = state_view
.as_converter(self.context.db.clone(), self.context.indexer_reader.clone());
let converted_resources = converter
.try_into_resources(resources.iter().map(|(k, v)| (k.clone(), v.as_slice())))
.context("Failed to build move resource response from data in DB")
Expand Down Expand Up @@ -522,10 +520,7 @@ impl Account {
self.context.state_view(Some(self.ledger_version))?;

let bytes = state_view
.as_converter(
self.context.db.clone(),
self.context.table_info_reader.clone(),
)
.as_converter(self.context.db.clone(), self.context.indexer_reader.clone())
.find_resource(&state_view, self.address, resource_type)
.context(format!(
"Failed to query DB to check for {} at {}",
Expand All @@ -543,10 +538,7 @@ impl Account {
})?;

state_view
.as_converter(
self.context.db.clone(),
self.context.table_info_reader.clone(),
)
.as_converter(self.context.db.clone(), self.context.indexer_reader.clone())
.move_struct_fields(resource_type, &bytes)
.context("Failed to convert move structs from storage")
.map_err(|err| {
Expand Down
19 changes: 7 additions & 12 deletions api/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ use aptos_types::{
chain_id::ChainId,
contract_event::EventWithVersion,
event::EventKey,
indexer::{
db_tailer_reader::IndexerTransactionEventReader, table_info_reader::TableInfoReader,
},
indexer::db_tailer_reader::IndexerReader,
ledger_info::LedgerInfoWithSignatures,
on_chain_config::{GasSchedule, GasScheduleV2, OnChainConfig, OnChainExecutionConfig},
state_store::{
Expand Down Expand Up @@ -76,9 +74,8 @@ pub struct Context {
gas_limit_cache: Arc<RwLock<GasLimitCache>>,
view_function_stats: Arc<FunctionStats>,
simulate_txn_stats: Arc<FunctionStats>,
pub table_info_reader: Option<Arc<dyn TableInfoReader>>,
pub indexer_reader: Option<Arc<dyn IndexerReader>>,
pub wait_for_hash_active_connections: Arc<AtomicUsize>,
pub txn_event_reader: Option<Arc<dyn IndexerTransactionEventReader>>,
}

impl std::fmt::Debug for Context {
Expand All @@ -93,8 +90,7 @@ impl Context {
db: Arc<dyn DbReader>,
mp_sender: MempoolClientSender,
node_config: NodeConfig,
table_info_reader: Option<Arc<dyn TableInfoReader>>,
txn_event_reader: Option<Arc<dyn IndexerTransactionEventReader>>,
indexer_reader: Option<Arc<dyn IndexerReader>>,
) -> Self {
let (view_function_stats, simulate_txn_stats) = {
let log_per_call_stats = node_config.api.periodic_function_stats_sec.is_some();
Expand Down Expand Up @@ -131,9 +127,8 @@ impl Context {
})),
view_function_stats,
simulate_txn_stats,
table_info_reader,
indexer_reader,
wait_for_hash_active_connections: Arc::new(AtomicUsize::new(0)),
txn_event_reader,
}
}

Expand Down Expand Up @@ -421,7 +416,7 @@ impl Context {

// We should be able to do an unwrap here, otherwise the above db read would fail.
let state_view = self.state_view_at_version(version)?;
let converter = state_view.as_converter(self.db.clone(), self.table_info_reader.clone());
let converter = state_view.as_converter(self.db.clone(), self.indexer_reader.clone());

// Extract resources from resource groups and flatten into all resources
let kvs = kvs
Expand Down Expand Up @@ -621,7 +616,7 @@ impl Context {
}

let state_view = self.latest_state_view_poem(ledger_info)?;
let converter = state_view.as_converter(self.db.clone(), self.table_info_reader.clone());
let converter = state_view.as_converter(self.db.clone(), self.indexer_reader.clone());
let txns: Vec<aptos_api_types::Transaction> = data
.into_iter()
.map(|t| {
Expand Down Expand Up @@ -653,7 +648,7 @@ impl Context {
}

let state_view = self.latest_state_view_poem(ledger_info)?;
let converter = state_view.as_converter(self.db.clone(), self.table_info_reader.clone());
let converter = state_view.as_converter(self.db.clone(), self.indexer_reader.clone());
let txns: Vec<aptos_api_types::Transaction> = data
.into_iter()
.map(|t| {
Expand Down
5 changes: 1 addition & 4 deletions api/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,7 @@ impl EventsApi {
let events = self
.context
.latest_state_view_poem(&latest_ledger_info)?
.as_converter(
self.context.db.clone(),
self.context.table_info_reader.clone(),
)
.as_converter(self.context.db.clone(), self.context.indexer_reader.clone())
.try_into_versioned_events(&events)
.context("Failed to convert events from storage into response")
.map_err(|err| {
Expand Down
20 changes: 3 additions & 17 deletions api/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,7 @@ use aptos_config::config::{ApiConfig, NodeConfig};
use aptos_logger::info;
use aptos_mempool::MempoolClientSender;
use aptos_storage_interface::DbReader;
use aptos_types::{
chain_id::ChainId,
indexer::{
db_tailer_reader::IndexerTransactionEventReader, table_info_reader::TableInfoReader,
},
};
use aptos_types::{chain_id::ChainId, indexer::db_tailer_reader::IndexerReader};
use poem::{
handler,
http::Method,
Expand All @@ -39,20 +34,12 @@ pub fn bootstrap(
chain_id: ChainId,
db: Arc<dyn DbReader>,
mp_sender: MempoolClientSender,
table_info_reader: Option<Arc<dyn TableInfoReader>>,
txn_event_reader: Option<Arc<dyn IndexerTransactionEventReader>>,
indexer_reader: Option<Arc<dyn IndexerReader>>,
) -> anyhow::Result<Runtime> {
let max_runtime_workers = get_max_runtime_workers(&config.api);
let runtime = aptos_runtimes::spawn_named_runtime("api".into(), Some(max_runtime_workers));

let context = Context::new(
chain_id,
db,
mp_sender,
config.clone(),
table_info_reader,
txn_event_reader,
);
let context = Context::new(chain_id, db, mp_sender, config.clone(), indexer_reader);

attach_poem_to_runtime(runtime.handle(), context.clone(), config, false)
.context("Failed to attach poem to runtime")?;
Expand Down Expand Up @@ -354,7 +341,6 @@ mod tests {
context.db.clone(),
context.mempool.ac_client.clone(),
None,
None,
);
assert!(ret.is_ok());

Expand Down
16 changes: 4 additions & 12 deletions api/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,10 +287,7 @@ impl StateApi {

let (ledger_info, ledger_version, state_view) = self.context.state_view(ledger_version)?;
let bytes = state_view
.as_converter(
self.context.db.clone(),
self.context.table_info_reader.clone(),
)
.as_converter(self.context.db.clone(), self.context.indexer_reader.clone())
.find_resource(&state_view, address, &tag)
.context(format!(
"Failed to query DB to check for {} at {}",
Expand All @@ -308,10 +305,7 @@ impl StateApi {
match accept_type {
AcceptType::Json => {
let resource = state_view
.as_converter(
self.context.db.clone(),
self.context.table_info_reader.clone(),
)
.as_converter(self.context.db.clone(), self.context.indexer_reader.clone())
.try_into_resource(&tag, &bytes)
.context("Failed to deserialize resource data retrieved from DB")
.map_err(|err| {
Expand Down Expand Up @@ -412,10 +406,8 @@ impl StateApi {
.context
.state_view(ledger_version.map(|inner| inner.0))?;

let converter = state_view.as_converter(
self.context.db.clone(),
self.context.table_info_reader.clone(),
);
let converter =
state_view.as_converter(self.context.db.clone(), self.context.indexer_reader.clone());

// Convert key to lookup version for DB
let vm_key = converter
Expand Down
21 changes: 6 additions & 15 deletions api/src/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -898,7 +898,7 @@ impl TransactionsApi {
state_view
.as_converter(
self.context.db.clone(),
self.context.table_info_reader.clone(),
self.context.indexer_reader.clone(),
)
.try_into_onchain_transaction(timestamp, txn)
.context("Failed to convert on chain transaction to Transaction")
Expand All @@ -911,10 +911,7 @@ impl TransactionsApi {
})?
},
TransactionData::Pending(txn) => state_view
.as_converter(
self.context.db.clone(),
self.context.table_info_reader.clone(),
)
.as_converter(self.context.db.clone(), self.context.indexer_reader.clone())
.try_into_pending_transaction(*txn)
.context("Failed to convert on pending transaction to Transaction")
.map_err(|err| {
Expand Down Expand Up @@ -1092,10 +1089,7 @@ impl TransactionsApi {
SubmitTransactionPost::Json(data) => self
.context
.latest_state_view_poem(ledger_info)?
.as_converter(
self.context.db.clone(),
self.context.table_info_reader.clone(),
)
.as_converter(self.context.db.clone(), self.context.indexer_reader.clone())
.try_into_signed_transaction_poem(data.0, self.context.chain_id())
.context("Failed to create SignedTransaction from SubmitTransactionRequest")
.map_err(|err| {
Expand Down Expand Up @@ -1173,7 +1167,7 @@ impl TransactionsApi {
.enumerate()
.map(|(index, txn)| {
self.context.latest_state_view_poem(ledger_info)?
.as_converter(self.context.db.clone(), self.context.table_info_reader.clone())
.as_converter(self.context.db.clone(), self.context.indexer_reader.clone())
.try_into_signed_transaction_poem(txn, self.context.chain_id())
.context(format!("Failed to create SignedTransaction from SubmitTransactionRequest at position {}", index))
.map_err(|err| {
Expand Down Expand Up @@ -1264,7 +1258,7 @@ impl TransactionsApi {

// We provide the pending transaction so that users have the hash associated
let pending_txn = state_view
.as_converter(self.context.db.clone(), self.context.table_info_reader.clone())
.as_converter(self.context.db.clone(), self.context.indexer_reader.clone())
.try_into_pending_transaction_poem(txn)
.context("Failed to build PendingTransaction from mempool response, even though it said the request was accepted")
.map_err(|err| SubmitTransactionError::internal_with_code(
Expand Down Expand Up @@ -1500,10 +1494,7 @@ impl TransactionsApi {
let ledger_info = self.context.get_latest_ledger_info()?;
let state_view = self.context.latest_state_view_poem(&ledger_info)?;
let raw_txn: RawTransaction = state_view
.as_converter(
self.context.db.clone(),
self.context.table_info_reader.clone(),
)
.as_converter(self.context.db.clone(), self.context.indexer_reader.clone())
.try_into_raw_transaction_poem(request.transaction, self.context.chain_id())
.context("The given transaction is invalid")
.map_err(|err| {
Expand Down
6 changes: 3 additions & 3 deletions api/src/view_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ fn view_request(

let view_function: ViewFunction = match request {
ViewFunctionRequest::Json(data) => state_view
.as_converter(context.db.clone(), context.table_info_reader.clone())
.as_converter(context.db.clone(), context.indexer_reader.clone())
.convert_view_function(data.0)
.map_err(|err| {
BasicErrorWith404::bad_request_with_code(
Expand Down Expand Up @@ -167,7 +167,7 @@ fn view_request(
},
AcceptType::Json => {
let return_types = state_view
.as_converter(context.db.clone(), context.table_info_reader.clone())
.as_converter(context.db.clone(), context.indexer_reader.clone())
.function_return_types(&view_function)
.and_then(|tys| {
tys.into_iter()
Expand All @@ -187,7 +187,7 @@ fn view_request(
.zip(return_types.into_iter())
.map(|(v, ty)| {
state_view
.as_converter(context.db.clone(), context.table_info_reader.clone())
.as_converter(context.db.clone(), context.indexer_reader.clone())
.try_into_move_value(&ty, &v)
})
.collect::<anyhow::Result<Vec<_>>>()
Expand Down
1 change: 0 additions & 1 deletion api/test-context/src/test_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ pub fn new_test_context(
mempool.ac_client.clone(),
node_config.clone(),
None, /* table info reader */
None,
);

// Configure the testing depending on which API version we're testing.
Expand Down
10 changes: 5 additions & 5 deletions api/types/src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use aptos_types::{
access_path::{AccessPath, Path},
chain_id::ChainId,
contract_event::{ContractEvent, EventWithVersion},
indexer::table_info_reader::TableInfoReader,
indexer::db_tailer_reader::IndexerReader,
state_store::{
state_key::{inner::StateKeyInner, StateKey},
table::{TableHandle, TableInfo},
Expand Down Expand Up @@ -65,14 +65,14 @@ const OBJECT_STRUCT: &IdentStr = ident_str!("Object");
pub struct MoveConverter<'a, S> {
inner: AptosValueAnnotator<'a, S>,
db: Arc<dyn DbReader>,
table_info_reader: Option<Arc<dyn TableInfoReader>>,
table_info_reader: Option<Arc<dyn IndexerReader>>,
}

impl<'a, S: StateView> MoveConverter<'a, S> {
pub fn new(
inner: &'a S,
db: Arc<dyn DbReader>,
table_info_reader: Option<Arc<dyn TableInfoReader>>,
table_info_reader: Option<Arc<dyn IndexerReader>>,
) -> Self {
Self {
inner: AptosValueAnnotator::new(inner),
Expand Down Expand Up @@ -1045,15 +1045,15 @@ pub trait AsConverter<R> {
fn as_converter(
&self,
db: Arc<dyn DbReader>,
table_info_reader: Option<Arc<dyn TableInfoReader>>,
table_info_reader: Option<Arc<dyn IndexerReader>>,
) -> MoveConverter<R>;
}

impl<R: StateView> AsConverter<R> for R {
fn as_converter(
&self,
db: Arc<dyn DbReader>,
table_info_reader: Option<Arc<dyn TableInfoReader>>,
table_info_reader: Option<Arc<dyn IndexerReader>>,
) -> MoveConverter<R> {
MoveConverter::new(self, db, table_info_reader)
}
Expand Down
Loading

0 comments on commit 5d4ec3b

Please sign in to comment.