Skip to content

Commit

Permalink
[State Sync] Add storage service API call for fetching the number of …
Browse files Browse the repository at this point in the history
…accounts at a specific version.

Closes: #9277
  • Loading branch information
JoshLind authored and bors-libra committed Sep 28, 2021
1 parent 52fbfd0 commit 690a060
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
30 changes: 29 additions & 1 deletion state-sync/storage-service/server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ use diem_infallible::RwLock;
use diem_types::{
account_state_blob::AccountStatesChunkWithProof,
epoch_change::EpochChangeProof,
transaction::default_protocol::{TransactionListWithProof, TransactionOutputListWithProof},
transaction::{
default_protocol::{TransactionListWithProof, TransactionOutputListWithProof},
Version,
},
};
use serde::{Deserialize, Serialize};
use std::sync::Arc;
Expand Down Expand Up @@ -62,6 +65,9 @@ impl<T: StorageReaderInterface> StorageServiceServer<T> {
StorageServiceRequest::GetEpochEndingLedgerInfos(request) => {
self.get_epoch_ending_ledger_infos(request)
}
StorageServiceRequest::GetNumberOfAccountsAtVersion(version) => {
self.get_number_of_accounts_at_version(version)
}
StorageServiceRequest::GetServerProtocolVersion => self.get_server_protocol_version(),
StorageServiceRequest::GetStorageServerSummary => self.get_storage_server_summary(),
StorageServiceRequest::GetTransactionOutputsWithProof(request) => {
Expand Down Expand Up @@ -112,6 +118,17 @@ impl<T: StorageReaderInterface> StorageServiceServer<T> {
))
}

fn get_number_of_accounts_at_version(
&self,
version: Version,
) -> Result<StorageServiceResponse, Error> {
let number_of_accounts = self.storage.get_number_of_accounts(version)?;

Ok(StorageServiceResponse::NumberOfAccountsAtVersion(
number_of_accounts,
))
}

fn get_server_protocol_version(&self) -> Result<StorageServiceResponse, Error> {
let server_protocol_version = ServerProtocolVersion {
protocol_version: STORAGE_SERVER_VERSION,
Expand Down Expand Up @@ -205,6 +222,10 @@ pub trait StorageReaderInterface {
expected_num_transaction_outputs: u64,
) -> Result<TransactionOutputListWithProof, Error>;

/// Returns the number of accounts in the account state tree at the
/// specified version.
fn get_number_of_accounts(&self, version: u64) -> Result<u64, Error>;

/// Returns a chunk holding a list of account states starting at the
/// specified account key with *at most* `expected_num_account_states`.
fn get_account_states_chunk_with_proof(
Expand Down Expand Up @@ -312,4 +333,11 @@ impl StorageReaderInterface for StorageReader {
"Unimplemented! This API call needs to be implemented!".into(),
))
}

fn get_number_of_accounts(&self, _version: u64) -> Result<u64, Error> {
// TODO(joshlind): implement this once DbReaderWriter supports these calls.
Err(Error::UnexpectedErrorEncountered(
"Unimplemented! This API call needs to be implemented!".into(),
))
}
}
20 changes: 20 additions & 0 deletions state-sync/storage-service/server/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,26 @@ fn test_get_server_protocol_version() {
);
}

#[test]
fn test_get_number_of_accounts_at_version() {
// Create a storage service server
let storage_server = create_storage_server();

// Create a request to fetch the number of accounts at the specified version
let number_of_accounts_request = StorageServiceRequest::GetNumberOfAccountsAtVersion(10);

// Process the request
let number_of_accounts_response = storage_server
.handle_request(number_of_accounts_request)
.unwrap();

// Verify the response is correct (the API call is currently unsupported)
assert_matches!(
number_of_accounts_response,
StorageServiceResponse::StorageServiceError(StorageServiceError::InternalError)
);
}

#[test]
fn test_get_storage_server_summary() {
// Create a storage service server
Expand Down
6 changes: 4 additions & 2 deletions state-sync/storage-service/types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ use diem_types::{
pub enum StorageServiceRequest {
GetAccountStatesChunkWithProof(AccountStatesChunkWithProofRequest), // Fetches a list of account states with a proof
GetEpochEndingLedgerInfos(EpochEndingLedgerInfoRequest), // Fetches a list of epoch ending ledger infos
GetServerProtocolVersion, // Fetches the protocol version run by the server
GetStorageServerSummary, // Fetches a summary of the storage server state
GetNumberOfAccountsAtVersion(Version), // Fetches the number of accounts at the specified version
GetServerProtocolVersion, // Fetches the protocol version run by the server
GetStorageServerSummary, // Fetches a summary of the storage server state
GetTransactionOutputsWithProof(TransactionOutputsWithProofRequest), // Fetches a list of transaction outputs with a proof
GetTransactionsWithProof(TransactionsWithProofRequest), // Fetches a list of transactions with a proof
}
Expand All @@ -30,6 +31,7 @@ pub enum StorageServiceRequest {
pub enum StorageServiceResponse {
AccountStatesChunkWithProof(AccountStatesChunkWithProof),
EpochEndingLedgerInfos(EpochChangeProof),
NumberOfAccountsAtVersion(u64),
ServerProtocolVersion(ServerProtocolVersion),
StorageServiceError(StorageServiceError),
StorageServerSummary(StorageServerSummary),
Expand Down

0 comments on commit 690a060

Please sign in to comment.