Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix rosetta staking balances #15246

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .github/workflows/copy-images-to-dockerhub.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,4 @@ jobs:
AWS_ACCOUNT_ID: ${{ secrets.AWS_ECR_ACCOUNT_NUM }}
GCP_DOCKER_ARTIFACT_REPO: ${{ vars.GCP_DOCKER_ARTIFACT_REPO }}
IMAGE_TAG_PREFIX: ${{ inputs.image_tag_prefix }}
DRY_RUN: ${{ inputs.dry_run }}
run: ./docker/release-images.mjs --wait-for-image-seconds=3600 ${{ inputs.dry_run && '--dry-run' || '' }}
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
remote_endpoint: ~
name: signer_native_format_fix
proposals:
- name: enable_signer_native_format_fix
metadata:
title: "Enable the signer native format fix"
description: "Fixes the issue of string_utils::native_format in formatting signer values"
execution_mode: MultiStep
update_sequence:
- FeatureFlag:
enabled:
- signer_native_format_fix
251 changes: 150 additions & 101 deletions crates/aptos-rosetta/src/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ use aptos_logger::{debug, trace, warn};
use aptos_rest_client::{
aptos_api_types::{AptosError, AptosErrorCode, ViewFunction},
error::{AptosErrorResponse, RestError},
Client,
};
use aptos_types::{account_address::AccountAddress, account_config::AccountResource};
use move_core_types::{
ident_str,
language_storage::{ModuleId, StructTag, TypeTag},
parser::parse_type_tag,
};
use std::str::FromStr;
use std::{collections::HashSet, str::FromStr};
use warp::Filter;

/// Account routes e.g. balance
Expand Down Expand Up @@ -105,43 +106,62 @@ async fn get_balances(

let mut balances = vec![];
let mut lockup_expiration: u64 = 0;
let mut total_requested_balance: Option<u64> = None;
let mut maybe_operators = None;

// Lookup the delegation pool, if it's provided in the account information
if pool_address.is_some() {
match get_delegation_stake_balances(
rest_client.as_ref(),
&account,
owner_address,
pool_address.unwrap(),
version,
)
.await
{
Ok(Some(balance_result)) => {
if let Some(balance) = balance_result.balance {
total_requested_balance = Some(
total_requested_balance.unwrap_or_default()
+ u64::from_str(&balance.value).unwrap_or_default(),
);
}
lockup_expiration = balance_result.lockup_expiration;
if let Some(balance) = total_requested_balance {
balances.push(Amount {
value: balance.to_string(),
currency: native_coin(),
})
}
},
result => {
warn!(
"Failed to retrieve requested balance for delegator_address: {}, pool_address: {}: {:?}",
owner_address, pool_address.unwrap(), result
)
},
// Handle the things that must always happen

// Retrieve the sequence number
let sequence_number = get_sequence_number(&rest_client, owner_address, version).await?;

// Filter currencies to lookup
let currencies_to_lookup = if let Some(currencies) = maybe_filter_currencies {
currencies.into_iter().collect()
} else {
server_context.currencies.clone()
};

// Regular account, FA and Coin
if account.is_base_account() {
balances =
get_base_balances(&rest_client, owner_address, version, currencies_to_lookup).await?;
} else if pool_address.is_some() {
// Lookup the delegation pool, if it's provided in the account information
// Filter appropriately, must have native coin
if currencies_to_lookup.contains(&native_coin()) {
(balances, lockup_expiration) = get_delegation_info(
&rest_client,
&account,
owner_address,
pool_address.unwrap(),
version,
)
.await?;
}
} else {
// Retrieve staking information (if it applies)
// Only non-pool addresses, and non base accounts
//
// These are special cases around querying the stake amounts
// Filter appropriately, must have native coin
if currencies_to_lookup.contains(&native_coin()) {
(balances, lockup_expiration, maybe_operators) =
get_staking_info(&rest_client, &account, owner_address, version).await?;
}
}

Ok((
sequence_number,
maybe_operators,
balances,
lockup_expiration,
))
}

async fn get_sequence_number(
rest_client: &Client,
owner_address: AccountAddress,
version: u64,
) -> ApiResult<u64> {
// Retrieve sequence number
let sequence_number = match rest_client
.get_account_resource_at_version_bcs(owner_address, "0x1::account::Account", version)
Expand Down Expand Up @@ -178,77 +198,111 @@ async fn get_balances(
},
};

// Retrieve staking information (if it applies)
// Only non-pool addresses, and non base accounts
Ok(sequence_number)
}

async fn get_staking_info(
rest_client: &Client,
account: &AccountIdentifier,
owner_address: AccountAddress,
version: u64,
) -> ApiResult<(Vec<Amount>, u64, Option<Vec<AccountAddress>>)> {
let mut balances = vec![];
let mut lockup_expiration: u64 = 0;
let mut maybe_operators = None;
if !account.is_base_account() && pool_address.is_none() {
if let Ok(response) = rest_client
.get_account_resource_at_version_bcs(
owner_address,
"0x1::staking_contract::Store",
version,
)
.await
{
let store: Store = response.into_inner();
maybe_operators = Some(vec![]);
for (operator, contract) in store.staking_contracts {
// Keep track of operators
maybe_operators.as_mut().unwrap().push(operator);
match get_stake_balances(
rest_client.as_ref(),
let mut total_balance = 0;
let mut has_staking = false;

if let Ok(response) = rest_client
.get_account_resource_at_version_bcs(owner_address, "0x1::staking_contract::Store", version)
.await
{
let store: Store = response.into_inner();
maybe_operators = Some(vec![]);
for (operator, contract) in store.staking_contracts {
// Keep track of operators
maybe_operators.as_mut().unwrap().push(operator);
match get_stake_balances(rest_client, account, contract.pool_address, version).await {
Ok(Some(balance_result)) => {
if let Some(balance) = balance_result.balance {
has_staking = true;
total_balance += u64::from_str(&balance.value).unwrap_or_default();
}
// TODO: This seems like it only works if there's only one staking contract (hopefully it stays that way)
lockup_expiration = balance_result.lockup_expiration;
},
result => {
warn!(
"Failed to retrieve requested balance for account: {}, address: {}: {:?}",
owner_address, contract.pool_address, result
)
},
}
}
if has_staking {
balances.push(Amount {
value: total_balance.to_string(),
currency: native_coin(),
})
}

/* TODO: Right now operator stake is not supported
else if account.is_operator_stake() {
// For operator stake, filter on operator address
let operator_address = account.operator_address()?;
if let Some(contract) = store.staking_contracts.get(&operator_address) {
balances.push(get_total_stake(
rest_client,
&account,
contract.pool_address,
version,
)
.await
{
Ok(Some(balance_result)) => {
if let Some(balance) = balance_result.balance {
total_requested_balance = Some(
total_requested_balance.unwrap_or_default()
+ u64::from_str(&balance.value).unwrap_or_default(),
);
}
lockup_expiration = balance_result.lockup_expiration;
},
result => {
warn!(
"Failed to retrieve requested balance for account: {}, address: {}: {:?}",
owner_address, contract.pool_address, result
)
},
}
).await?);
}
if let Some(balance) = total_requested_balance {
}*/
}

Ok((balances, lockup_expiration, maybe_operators))
}

async fn get_delegation_info(
rest_client: &Client,
account: &AccountIdentifier,
owner_address: AccountAddress,
pool_address: AccountAddress,
version: u64,
) -> ApiResult<(Vec<Amount>, u64)> {
let mut balances = vec![];
let mut lockup_expiration: u64 = 0;

match get_delegation_stake_balances(rest_client, account, owner_address, pool_address, version)
.await
{
Ok(Some(balance_result)) => {
if let Some(balance) = balance_result.balance {
balances.push(Amount {
value: balance.to_string(),
value: balance.value,
currency: native_coin(),
})
});
}

/* TODO: Right now operator stake is not supported
else if account.is_operator_stake() {
// For operator stake, filter on operator address
let operator_address = account.operator_address()?;
if let Some(contract) = store.staking_contracts.get(&operator_address) {
balances.push(get_total_stake(
rest_client,
&account,
contract.pool_address,
version,
).await?);
}
}*/
}
lockup_expiration = balance_result.lockup_expiration;
},
result => {
warn!(
"Failed to retrieve requested balance for delegator_address: {}, pool_address: {}: {:?}",
owner_address, pool_address, result
)
},
}
Ok((balances, lockup_expiration))
}

// Filter currencies to lookup
let currencies_to_lookup = if let Some(currencies) = maybe_filter_currencies {
currencies.into_iter().collect()
} else {
server_context.currencies.clone()
};
async fn get_base_balances(
rest_client: &Client,
owner_address: AccountAddress,
version: u64,
currencies_to_lookup: HashSet<Currency>,
) -> ApiResult<Vec<Amount>> {
let mut balances = vec![];

// Retrieve the fungible asset balances and the coin balances
for currency in currencies_to_lookup.iter() {
Expand Down Expand Up @@ -330,10 +384,5 @@ async fn get_balances(
}
}

Ok((
sequence_number,
maybe_operators,
balances,
lockup_expiration,
))
Ok(balances)
}
2 changes: 1 addition & 1 deletion docker/builder/docker-bake-rust-all.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ target "debian-base" {
dockerfile = "docker/builder/debian-base.Dockerfile"
contexts = {
# Run `docker buildx imagetools inspect debian:bullseye` to find the latest multi-platform hash
debian = "docker-image://debian:bullseye@sha256:152b9a5dc2a03f18ddfd88fbe7b1df41bd2b16be9f2df573a373caf46ce78c08"
debian = "docker-image://debian:bullseye@sha256:d0036be35fe0a4d2649bf074ca467a37dab8c5b26bbbdfca0375b4dc682f011d"
}
}

Expand Down
2 changes: 1 addition & 1 deletion docker/builder/tools.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
install \
wget \
curl \
perl-base=5.32.1-4+deb11u1 \
perl-base=5.32.1-4+deb11u4 \
libtinfo6=6.2+20201114-2+deb11u2 \
git \
libssl1.1 \
Expand Down
1 change: 1 addition & 0 deletions docker/release-images.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ async function main() {
const imageTarget = `${targetRegistry}/${image}:${joinTagSegments(parsedArgs.IMAGE_TAG_PREFIX, profilePrefix, featureSuffix)}`;
console.info(chalk.green(`INFO: copying ${imageSource} to ${imageTarget}`));
if (parsedArgs.DRY_RUN) {
console.info(chalk.yellow(`INFO: skipping copy of ${imageSource} to ${imageTarget} due to dry run`));
continue;
}
await waitForImageToBecomeAvailable(imageSource, parsedArgs.WAIT_FOR_IMAGE_SECONDS);
Expand Down
Loading
Loading