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

Merge 1.0.0-maint into main #226

Open
wants to merge 40 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
41e0ca0
refactor: insert bonds to use col count macro
mateuszjasiuk Dec 5, 2024
2b8fcf5
feat: insert votes in chunks
mateuszjasiuk Dec 5, 2024
fec6b0e
feat: add active at query param to bonds and unbonds (#198)
mateuszjasiuk Dec 13, 2024
68215a7
fetch validators out of consensus set
Fraccaman Dec 9, 2024
dddd8a8
return cargo tag
Fraccaman Dec 13, 2024
365a92b
improve logic
Fraccaman Dec 10, 2024
838dff1
update swagger
Fraccaman Dec 13, 2024
fb31083
feat: add block proposer to addresses_with_balance_change
joel-u410 Dec 12, 2024
f8c8276
Merge pull request #205 from anoma/backport-fix-pos
Fraccaman Dec 16, 2024
3024947
Merge pull request #207 from anoma/add-cargo-version
Fraccaman Dec 16, 2024
8e7893f
fetch rewards in batches
Fraccaman Dec 16, 2024
051b57a
fix: insert rewards in batch
RuslanGlaznyov Dec 16, 2024
a99a85e
minors
Fraccaman Dec 16, 2024
9128561
feat: print processed rewards batch
mateuszjasiuk Dec 16, 2024
cda97c1
Merge pull request #217 from MELLIFERA-Labs/fix/insert-db-rewards-in-…
Fraccaman Dec 16, 2024
e2ab703
Merge pull request #216 from anoma/improve-rewards-fetching
Fraccaman Dec 16, 2024
7cf71cf
Merge pull request #203 from joel-u410/joel/block-proposer-balances
Fraccaman Dec 16, 2024
6588900
enhancement: store blocks while crawling transactions and chain
joel-u410 Dec 9, 2024
daac819
update diesel schema for blocks
joel-u410 Dec 16, 2024
64d042a
upsert block before balances during initial_query
joel-u410 Dec 16, 2024
593ef20
Store the validator namada address on blocks instead of the tendermin…
joel-u410 Dec 16, 2024
e6ec420
Merge pull request #190 from joel-u410/joel/record-block-details
mateuszjasiuk Dec 17, 2024
fe544d5
parsing ack tx
Fraccaman Dec 2, 2024
e790084
parse sequence + ack + tx id
Fraccaman Dec 2, 2024
4d5886f
added handler
Fraccaman Dec 3, 2024
1e15f3b
update swagger
Fraccaman Dec 13, 2024
510b829
minors
Fraccaman Dec 16, 2024
1b91539
minors
Fraccaman Dec 17, 2024
d1eb8df
Merge pull request #219 from anoma/ibc-events
Fraccaman Dec 17, 2024
679b9d7
added gas_used to thw wrapper tx
Fraccaman Dec 17, 2024
321ca54
update endpoint and swagger
Fraccaman Dec 17, 2024
5b30e97
fix ibc_ack migration
Fraccaman Dec 17, 2024
509c992
fix ibc_ack migration
Fraccaman Dec 17, 2024
001e1e0
feat: return blocks by timestamp or height
mateuszjasiuk Dec 17, 2024
fbbd3bd
fmt
Fraccaman Dec 17, 2024
52bc198
Merge pull request #221 from anoma/add-gas
mateuszjasiuk Dec 17, 2024
cc865d9
Merge pull request #222 from anoma/feat/return-block-by-timestamp
mateuszjasiuk Dec 17, 2024
ccc9c61
chore: update version in cargo toml
mateuszjasiuk Dec 17, 2024
59dddc7
Merge pull request #224 from anoma/chore/update-version-in-cargo-toml…
mateuszjasiuk Dec 17, 2024
98325e7
Merge remote-tracking branch 'upstream/1.0.0-maint' into joel/1.0.0-m…
joel-u410 Dec 17, 2024
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ authors = ["Heliax <[email protected]>"]
edition = "2021"
license = "GPL-3.0"
readme = "README.md"
version = "1.0.0"
version = "1.1.4"

[workspace.dependencies]
clokwerk = "0.4.0"
Expand Down
160 changes: 118 additions & 42 deletions chain/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ use shared::crawler_state::ChainCrawlerState;
use shared::error::{AsDbError, AsRpcError, ContextDbInteractError, MainError};
use shared::id::Id;
use shared::token::Token;
use shared::utils::BalanceChange;
use shared::validator::ValidatorSet;
use tendermint_rpc::endpoint::block::Response as TendermintBlockResponse;
use tendermint_rpc::HttpClient;
use tokio_retry::strategy::{jitter, ExponentialBackoff};
use tokio_retry::Retry;
Expand Down Expand Up @@ -140,6 +142,7 @@ async fn main() -> Result<(), MainError> {
initial_query(
&client,
&conn,
checksums.clone(),
config.initial_query_retry_time,
config.initial_query_retry_attempts,
)
Expand Down Expand Up @@ -186,46 +189,15 @@ async fn crawling_fn(
return Err(MainError::NoAction);
}

tracing::debug!(block = block_height, "Query block...");
let tm_block_response =
tendermint_service::query_raw_block_at_height(&client, block_height)
.await
.into_rpc_error()?;
tracing::debug!(
block = block_height,
"Raw block contains {} txs...",
tm_block_response.block.data.len()
);

tracing::debug!(block = block_height, "Query block results...");
let tm_block_results_response =
tendermint_service::query_raw_block_results_at_height(
&client,
block_height,
)
.await
.into_rpc_error()?;
let block_results = BlockResult::from(tm_block_results_response);

tracing::debug!(block = block_height, "Query epoch...");
let epoch =
namada_service::get_epoch_at_block_height(&client, block_height)
.await
.into_rpc_error()?;

tracing::debug!(block = block_height, "Query first block in epoch...");
let first_block_in_epoch =
namada_service::get_first_block_in_epoch(&client)
.await
.into_rpc_error()?;

let block = Block::from(
tm_block_response,
&block_results,
checksums,
epoch,
block_height,
);
let (block, tm_block_response, epoch) =
get_block(block_height, &client, checksums).await?;

tracing::debug!(
block = block_height,
txs = block.transactions.len(),
Expand All @@ -245,6 +217,15 @@ async fn crawling_fn(

let addresses = block.addresses_with_balance_change(&native_token);

let block_proposer_address = block
.header
.proposer_address_namada
.as_ref()
.map(|address| BalanceChange {
address: Id::Account(address.clone()),
token: Token::Native(native_token.clone()),
});

let pgf_receipient_addresses = if first_block_in_epoch.eq(&block_height) {
conn.interact(move |conn| {
namada_pgf_repository::get_pgf_receipients_balance_changes(
Expand All @@ -260,8 +241,10 @@ async fn crawling_fn(
HashSet::default()
};

let all_balance_changed_addresses = pgf_receipient_addresses
.union(&addresses)
let all_balance_changed_addresses = addresses
.iter()
.chain(block_proposer_address.iter())
.chain(pgf_receipient_addresses.iter())
Comment on lines +244 to +247
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was probably the trickiest part of the merge -- given the changes on both sides to this, I had to make sure pgf_recipient_addresses was still accounted for (which it is, as you can see).

.cloned()
.collect::<HashSet<_>>();

Expand All @@ -272,7 +255,13 @@ async fn crawling_fn(
)
.await
.into_rpc_error()?;
tracing::info!("Updating balance for {} addresses...", addresses.len());

tracing::debug!(
block = block_height,
addresses = all_balance_changed_addresses.len(),
"Updating balance for {} addresses...",
all_balance_changed_addresses.len()
);

let next_governance_proposal_id =
namada_service::query_next_governance_id(&client, block_height)
Expand All @@ -298,12 +287,18 @@ async fn crawling_fn(
proposals_votes.len()
);

let validators = block.validators();
let validators = block.new_validators();
let validator_set = ValidatorSet {
validators: validators.clone(),
epoch,
};

let validators_state_change = block.update_validators_state();
tracing::debug!(
"Updating {} validators state",
validators_state_change.len()
);

let addresses = block.bond_addresses();
let bonds = query_bonds(&client, addresses).await.into_rpc_error()?;
tracing::debug!(
Expand Down Expand Up @@ -368,6 +363,7 @@ async fn crawling_fn(
withdraws = withdraw_addreses.len(),
claimed_rewards = reward_claimers.len(),
revealed_pks = revealed_pks.len(),
validator_state = validators_state_change.len(),
epoch = epoch,
first_block_in_epoch = first_block_in_epoch,
block = block_height,
Expand All @@ -383,6 +379,12 @@ async fn crawling_fn(
ibc_tokens,
)?;

repository::block::upsert_block(
transaction_conn,
block,
tm_block_response,
)?;

repository::balance::insert_balances(
transaction_conn,
balances,
Expand All @@ -402,6 +404,11 @@ async fn crawling_fn(
validator_set,
)?;

repository::pos::upsert_validator_state(
transaction_conn,
validators_state_change,
)?;

// We first remove all the bonds and then insert the new ones
repository::pos::clear_bonds(
transaction_conn,
Expand Down Expand Up @@ -453,29 +460,35 @@ async fn crawling_fn(
async fn initial_query(
client: &HttpClient,
conn: &Object,
checksums: Checksums,
retry_time: u64,
retry_attempts: usize,
) -> Result<(), MainError> {
let retry_strategy = ExponentialBackoff::from_millis(retry_time)
.map(jitter)
.take(retry_attempts);
Retry::spawn(retry_strategy, || try_initial_query(client, conn)).await
Retry::spawn(retry_strategy, || {
try_initial_query(client, conn, checksums.clone())
})
.await
}

async fn try_initial_query(
client: &HttpClient,
conn: &Object,
checksums: Checksums,
) -> Result<(), MainError> {
tracing::debug!("Querying initial data...");
let block_height =
query_last_block_height(client).await.into_rpc_error()?;
let epoch = namada_service::get_epoch_at_block_height(client, block_height)
.await
.into_rpc_error()?;

let first_block_in_epoch = namada_service::get_first_block_in_epoch(client)
.await
.into_rpc_error()?;

let (block, tm_block_response, epoch) =
get_block(block_height, client, checksums.clone()).await?;

let tokens = query_tokens(client).await.into_rpc_error()?;

// This can sometimes fail if the last block height in the node has moved
Expand Down Expand Up @@ -535,6 +548,12 @@ async fn try_initial_query(
.run(|transaction_conn| {
repository::balance::insert_tokens(transaction_conn, tokens)?;

repository::block::upsert_block(
transaction_conn,
block,
tm_block_response,
)?;

tracing::debug!(
block = block_height,
"Inserting {} balances...",
Expand Down Expand Up @@ -611,3 +630,60 @@ async fn update_crawler_timestamp(
.and_then(identity)
.into_db_error()
}

async fn get_block(
block_height: u32,
client: &HttpClient,
checksums: Checksums,
) -> Result<(Block, TendermintBlockResponse, u32), MainError> {
tracing::debug!(block = block_height, "Query block...");
let tm_block_response =
tendermint_service::query_raw_block_at_height(client, block_height)
.await
.into_rpc_error()?;
tracing::debug!(
block = block_height,
"Raw block contains {} txs...",
tm_block_response.block.data.len()
);

tracing::debug!(block = block_height, "Query block results...");
let tm_block_results_response =
tendermint_service::query_raw_block_results_at_height(
client,
block_height,
)
.await
.into_rpc_error()?;
let block_results = BlockResult::from(tm_block_results_response);

tracing::debug!(block = block_height, "Query epoch...");
let epoch = namada_service::get_epoch_at_block_height(client, block_height)
.await
.into_rpc_error()?;

let proposer_address_namada = namada_service::get_validator_namada_address(
client,
&Id::from(&tm_block_response.block.header.proposer_address),
)
.await
.into_rpc_error()?;

tracing::info!(
block = block_height,
tm_address = tm_block_response.block.header.proposer_address.to_string(),
namada_address = ?proposer_address_namada,
"Got block proposer address"
);

let block = Block::from(
&tm_block_response,
&block_results,
&proposer_address_namada,
checksums,
epoch,
block_height,
);

Ok((block, tm_block_response, epoch))
}
35 changes: 35 additions & 0 deletions chain/src/repository/balance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,17 @@ pub fn insert_tokens(
#[cfg(test)]
mod tests {

use std::collections::HashSet;

use anyhow::Context;
use diesel::{
BoolExpressionMethods, ExpressionMethods, QueryDsl, SelectableHelper,
};
use namada_sdk::token::Amount as NamadaAmount;
use namada_sdk::uint::MAX_SIGNED_VALUE;
use orm::balances::BalanceDb;
use orm::blocks::BlockInsertDb;
use orm::schema::blocks;
use orm::views::balances;
use shared::balance::{Amount, Balance};
use shared::id::Id;
Expand Down Expand Up @@ -130,6 +134,8 @@ mod tests {

insert_tokens(conn, vec![token.clone()])?;

seed_blocks_from_balances(conn, &[balance.clone()])?;

insert_balances(conn, vec![balance.clone()])?;

let queried_balance = query_balance_by_address(conn, owner, token)?;
Expand Down Expand Up @@ -175,6 +181,7 @@ mod tests {
..(balance.clone())
};

seed_blocks_from_balances(conn, &[new_balance.clone()])?;
insert_balances(conn, vec![new_balance])?;

let queried_balance =
Expand Down Expand Up @@ -376,6 +383,8 @@ mod tests {

seed_tokens_from_balance(conn, fake_balances.clone())?;

seed_blocks_from_balances(conn, &fake_balances)?;

insert_balances(conn, fake_balances.clone())?;

assert_eq!(query_all_balances(conn)?.len(), fake_balances.len());
Expand Down Expand Up @@ -410,6 +419,7 @@ mod tests {

insert_tokens(conn, vec![token.clone()])?;

seed_blocks_from_balances(conn, &[balance.clone()])?;
insert_balances(conn, vec![balance.clone()])?;

let queried_balance = query_balance_by_address(conn, owner, token)?;
Expand Down Expand Up @@ -442,6 +452,8 @@ mod tests {

insert_tokens(conn, vec![token])?;

seed_blocks_from_balances(conn, &balances)?;

let res = insert_balances(conn, balances);

assert!(res.is_ok());
Expand Down Expand Up @@ -475,6 +487,8 @@ mod tests {

seed_tokens_from_balance(conn, balances.clone())?;

seed_blocks_from_balances(conn, &balances)?;

let res = insert_balances(conn, balances);

assert!(res.is_ok());
Expand All @@ -500,12 +514,33 @@ mod tests {
anyhow::Ok(())
}

fn seed_blocks_from_balances(
conn: &mut PgConnection,
balances: &[Balance],
) -> anyhow::Result<()> {
for height in balances
.iter()
.map(|balance| balance.height as i32)
.collect::<HashSet<_>>()
{
diesel::insert_into(blocks::table)
.values::<&BlockInsertDb>(&BlockInsertDb::fake(height))
.on_conflict_do_nothing()
.execute(conn)
.context("Failed to insert block in db")?;
}

anyhow::Ok(())
}

fn seed_balance(
conn: &mut PgConnection,
balances: Vec<Balance>,
) -> anyhow::Result<()> {
seed_tokens_from_balance(conn, balances.clone())?;

seed_blocks_from_balances(conn, &balances)?;

diesel::insert_into(balance_changes::table)
.values::<&Vec<BalanceChangesInsertDb>>(
&balances
Expand Down
Loading
Loading