From 26cbca8bd315f02fec66aaca9da08722c6f0d824 Mon Sep 17 00:00:00 2001 From: Lasse Alm Date: Tue, 10 Dec 2024 15:09:20 +0100 Subject: [PATCH 01/21] start adding delegation --- .../0003_account_delegation.down.sql | 1 + .../migrations/0003_account_delegation.up.sql | 4 + backend-rust/src/graphql_api.rs | 36 +++- backend-rust/src/indexer.rs | 162 ++++++++++++++++++ 4 files changed, 198 insertions(+), 5 deletions(-) create mode 100644 backend-rust/migrations/0003_account_delegation.down.sql create mode 100644 backend-rust/migrations/0003_account_delegation.up.sql diff --git a/backend-rust/migrations/0003_account_delegation.down.sql b/backend-rust/migrations/0003_account_delegation.down.sql new file mode 100644 index 000000000..d2f607c5b --- /dev/null +++ b/backend-rust/migrations/0003_account_delegation.down.sql @@ -0,0 +1 @@ +-- Add down migration script here diff --git a/backend-rust/migrations/0003_account_delegation.up.sql b/backend-rust/migrations/0003_account_delegation.up.sql new file mode 100644 index 000000000..8219e7c71 --- /dev/null +++ b/backend-rust/migrations/0003_account_delegation.up.sql @@ -0,0 +1,4 @@ +-- Add up migration script here +ALTER TABLE accounts +ADD COLUMN delegated_restake_earnings BOOLEAN NULL, +ADD COLUMN delegated_target_baker_id BIGINT NULL \ No newline at end of file diff --git a/backend-rust/src/graphql_api.rs b/backend-rust/src/graphql_api.rs index 34de60eb4..e7732a79e 100644 --- a/backend-rust/src/graphql_api.rs +++ b/backend-rust/src/graphql_api.rs @@ -642,7 +642,9 @@ impl BaseQuery { address, amount, delegated_stake, - num_txs + num_txs, + delegated_restake_earnings, + delegated_target_baker_id FROM accounts WHERE -- Filter for only the accounts that are within the @@ -3075,15 +3077,17 @@ struct Account { /// The total number of transactions this account has been involved in or /// affected by. num_txs: i64, + delegated_restake_earnings: Option, + delegated_target_baker_id: Option // Get baker information if this account is baking. // baker: Option, - // delegation: Option, + } impl Account { async fn query_by_index(pool: &PgPool, index: AccountIndex) -> ApiResult> { let account = sqlx::query_as!( Account, - "SELECT index, transaction_index, address, amount, delegated_stake, num_txs + "SELECT index, transaction_index, address, amount, delegated_stake, num_txs, delegated_restake_earnings, delegated_target_baker_id FROM accounts WHERE index = $1", index @@ -3096,7 +3100,7 @@ impl Account { async fn query_by_address(pool: &PgPool, address: String) -> ApiResult> { let account = sqlx::query_as!( Account, - "SELECT index, transaction_index, address, amount, delegated_stake, num_txs + "SELECT index, transaction_index, address, amount, delegated_stake, num_txs, delegated_restake_earnings, delegated_target_baker_id FROM accounts WHERE address = $1", address @@ -3117,6 +3121,29 @@ impl Account { /// The total amount of CCD hold by the account. async fn amount(&self) -> Amount { self.amount } + async fn delegation(&self) -> Option { + if let Some(delegated_restake_earnings) = &self.delegated_restake_earnings { + Some(Delegation { + delegator_id: self.index, + restake_earnings: *delegated_restake_earnings, + staked_amount: self.delegated_stake, + delegation_target: if let Some(target) = self.delegated_target_baker_id { + DelegationTarget::BakerDelegationTarget(BakerDelegationTarget { + baker_id: target + }) + } else { + DelegationTarget::PassiveDelegationTarget(PassiveDelegationTarget { + dummy: false + }) + } + }) + } else { + None + } + + } + + /// Timestamp of the block where this account was created. async fn created_at(&self, ctx: &Context<'_>) -> ApiResult { let slot_time = sqlx::query_scalar!( @@ -3579,7 +3606,6 @@ struct Delegation { staked_amount: Amount, restake_earnings: bool, delegation_target: DelegationTarget, - pending_change: PendingDelegationChange, } #[derive(Union, serde::Serialize, serde::Deserialize)] diff --git a/backend-rust/src/indexer.rs b/backend-rust/src/indexer.rs index 43c013b01..3aae0aefe 100644 --- a/backend-rust/src/indexer.rs +++ b/backend-rust/src/indexer.rs @@ -22,6 +22,7 @@ use concordium_rust_sdk::{ RPCError, }, }; +use concordium_rust_sdk::types::DelegationTarget; use futures::{StreamExt, TryStreamExt}; use prometheus_client::{ metrics::{ @@ -1042,6 +1043,8 @@ enum PreparedEvent { AccountCreation(PreparedAccountCreation), /// Changes related to validators (previously referred to as bakers). BakerEvents(Vec), + // TODO + AccountDelegationEvents(Vec), /// Smart contract module got deployed. ModuleDeployed(PreparedModuleDeployed), /// Contract got initialized. @@ -1237,6 +1240,12 @@ impl PreparedEvent { } Ok(()) } + PreparedEvent::AccountDelegationEvents(events) => { + for event in events { + event.save(tx).await?; + } + Ok(()) + } PreparedEvent::NoOperation => Ok(()), } } @@ -1295,6 +1304,159 @@ impl PreparedAccountCreation { } } +enum PreparedAccountDelegationEvent { + StakeIncrease { + account_id: i64, + staked: i64, + }, + StakeDecrease { + account_id: i64, + staked: i64, + }, + SetRestakeEarnings { + account_id: i64, + restake_earnings: bool, + }, + Added { + account_id: i64 + }, + Removed { + account_id: i64 + }, + SetDelegationTarget { + account_id: i64, + target_id: Option + }, + BakerRemoved { + baker_id: i64 + } +} + +impl PreparedAccountDelegationEvent { + + fn prepare(event: &concordium_rust_sdk::types::DelegationEvent) -> anyhow::Result { + use concordium_rust_sdk::types::DelegationEvent; + let prepared = match event { + DelegationEvent::DelegationStakeIncreased { + delegator_id, + new_stake, + } => PreparedAccountDelegationEvent::StakeIncrease { + account_id: delegator_id.id.index.try_into()?, + staked: new_stake.micro_ccd.try_into()? + }, + DelegationEvent::DelegationStakeDecreased { + delegator_id, + new_stake, + } => PreparedAccountDelegationEvent::StakeIncrease { + account_id: delegator_id.id.index.try_into()?, + staked: new_stake.micro_ccd.try_into()? + }, + DelegationEvent::DelegationSetRestakeEarnings { + delegator_id, + restake_earnings, + } => PreparedAccountDelegationEvent::SetRestakeEarnings { + account_id: delegator_id.id.index.try_into()?, + restake_earnings: *restake_earnings + }, + DelegationEvent::DelegationSetDelegationTarget { + delegator_id, + delegation_target + } => { + PreparedAccountDelegationEvent::SetDelegationTarget { + account_id: delegator_id.id.index.try_into()?, + target_id: if let DelegationTarget::Baker { baker_id } = delegation_target { + Some(baker_id.id.index.try_into()?) + } else { + None + } + } + }, + DelegationEvent::DelegationAdded { + delegator_id + } => PreparedAccountDelegationEvent::Added { + account_id: delegator_id.id.index.try_into()?, + }, + DelegationEvent::DelegationRemoved { + delegator_id + } => PreparedAccountDelegationEvent::Removed { + account_id: delegator_id.id.index.try_into()? + }, + DelegationEvent::BakerRemoved { + baker_id + } => PreparedAccountDelegationEvent::BakerRemoved { + baker_id: baker_id.id.index.try_into()? + } + }; + Ok(prepared) + } + + async fn save( + &self, + tx: &mut sqlx::Transaction<'static, sqlx::Postgres>, + ) -> anyhow::Result<()> { + match self { + PreparedAccountDelegationEvent::StakeIncrease { + account_id, + staked + } | PreparedAccountDelegationEvent::StakeDecrease { + account_id, + staked + } => { + sqlx::query!( + r#"UPDATE accounts SET delegated_stake = $1 WHERE index = $2"#, + staked, + account_id + ) + .execute(tx.as_mut()) + .await?; + }, + PreparedAccountDelegationEvent::Added { + account_id + } | PreparedAccountDelegationEvent::Removed { + account_id + } => { + sqlx::query!( + r#"UPDATE accounts SET delegated_stake = 0, delegated_restake_earnings = false, delegated_target_baker_id = NULL WHERE index = $1"#, + account_id + ) + .execute(tx.as_mut()) + .await?; + }, + + PreparedAccountDelegationEvent::SetRestakeEarnings { + account_id, + restake_earnings + } => { + sqlx::query!( + r#"UPDATE accounts SET delegated_restake_earnings = $1 WHERE index = $2"#, + *restake_earnings, + account_id + ) + .execute(tx.as_mut()) + .await?; + }, + PreparedAccountDelegationEvent::SetDelegationTarget { + account_id, + target_id, + } => { + sqlx::query!( + r#"UPDATE accounts SET delegated_target_baker_id = $1 WHERE index = $2"#, + *target_id, + account_id + ) + .execute(tx.as_mut()) + .await?; + }, + PreparedAccountDelegationEvent::BakerRemoved { + baker_id + } => { + todo!() + } + } + Ok(()) + } +} + /// Event changing state related to validators (bakers). enum PreparedBakerEvent { Add { From bf8105fb71c16439b073a0b59bf77a99268e4f26 Mon Sep 17 00:00:00 2001 From: Lasse Alm Date: Tue, 10 Dec 2024 20:49:41 +0100 Subject: [PATCH 02/21] fmt --- backend-rust/src/graphql_api.rs | 40 +++++++-------- backend-rust/src/indexer.rs | 88 +++++++++++++++++---------------- 2 files changed, 64 insertions(+), 64 deletions(-) diff --git a/backend-rust/src/graphql_api.rs b/backend-rust/src/graphql_api.rs index e7732a79e..4aa60c4de 100644 --- a/backend-rust/src/graphql_api.rs +++ b/backend-rust/src/graphql_api.rs @@ -3063,31 +3063,30 @@ struct Rejected<'a> { #[derive(sqlx::FromRow)] struct Account { // release_schedule: AccountReleaseSchedule, - index: i64, + index: i64, /// Index of the transaction creating this account. /// Only `None` for genesis accounts. transaction_index: Option, /// The address of the account in Base58Check. #[sqlx(try_from = "String")] - address: AccountAddress, + address: AccountAddress, /// The total amount of CCD hold by the account. - amount: Amount, + amount: Amount, /// The total delegated stake of this account. - delegated_stake: Amount, + delegated_stake: Amount, /// The total number of transactions this account has been involved in or /// affected by. - num_txs: i64, + num_txs: i64, delegated_restake_earnings: Option, - delegated_target_baker_id: Option - // Get baker information if this account is baking. - // baker: Option, - + delegated_target_baker_id: Option, /* Get baker information if this account is baking. + * baker: Option, */ } impl Account { async fn query_by_index(pool: &PgPool, index: AccountIndex) -> ApiResult> { let account = sqlx::query_as!( Account, - "SELECT index, transaction_index, address, amount, delegated_stake, num_txs, delegated_restake_earnings, delegated_target_baker_id + "SELECT index, transaction_index, address, amount, delegated_stake, num_txs, \ + delegated_restake_earnings, delegated_target_baker_id FROM accounts WHERE index = $1", index @@ -3100,7 +3099,8 @@ impl Account { async fn query_by_address(pool: &PgPool, address: String) -> ApiResult> { let account = sqlx::query_as!( Account, - "SELECT index, transaction_index, address, amount, delegated_stake, num_txs, delegated_restake_earnings, delegated_target_baker_id + "SELECT index, transaction_index, address, amount, delegated_stake, num_txs, \ + delegated_restake_earnings, delegated_target_baker_id FROM accounts WHERE address = $1", address @@ -3124,26 +3124,24 @@ impl Account { async fn delegation(&self) -> Option { if let Some(delegated_restake_earnings) = &self.delegated_restake_earnings { Some(Delegation { - delegator_id: self.index, - restake_earnings: *delegated_restake_earnings, - staked_amount: self.delegated_stake, + delegator_id: self.index, + restake_earnings: *delegated_restake_earnings, + staked_amount: self.delegated_stake, delegation_target: if let Some(target) = self.delegated_target_baker_id { - DelegationTarget::BakerDelegationTarget(BakerDelegationTarget { - baker_id: target - }) + DelegationTarget::BakerDelegationTarget(BakerDelegationTarget { + baker_id: target, + }) } else { DelegationTarget::PassiveDelegationTarget(PassiveDelegationTarget { - dummy: false + dummy: false, }) - } + }, }) } else { None } - } - /// Timestamp of the block where this account was created. async fn created_at(&self, ctx: &Context<'_>) -> ApiResult { let slot_time = sqlx::query_scalar!( diff --git a/backend-rust/src/indexer.rs b/backend-rust/src/indexer.rs index 3aae0aefe..643769f96 100644 --- a/backend-rust/src/indexer.rs +++ b/backend-rust/src/indexer.rs @@ -15,14 +15,14 @@ use concordium_rust_sdk::{ types::{ self as sdk_types, queries::BlockInfo, AccountStakingInfo, AccountTransactionDetails, AccountTransactionEffects, BlockItemSummary, BlockItemSummaryDetails, - ContractInitializedEvent, ContractTraceElement, PartsPerHundredThousands, RewardsOverview, + ContractInitializedEvent, ContractTraceElement, DelegationTarget, PartsPerHundredThousands, + RewardsOverview, }, v2::{ self, BlockIdentifier, ChainParameters, FinalizedBlockInfo, QueryError, QueryResult, RPCError, }, }; -use concordium_rust_sdk::types::DelegationTarget; use futures::{StreamExt, TryStreamExt}; use prometheus_client::{ metrics::{ @@ -1307,33 +1307,32 @@ impl PreparedAccountCreation { enum PreparedAccountDelegationEvent { StakeIncrease { account_id: i64, - staked: i64, + staked: i64, }, StakeDecrease { account_id: i64, - staked: i64, + staked: i64, }, SetRestakeEarnings { - account_id: i64, + account_id: i64, restake_earnings: bool, }, Added { - account_id: i64 + account_id: i64, }, Removed { - account_id: i64 + account_id: i64, }, SetDelegationTarget { account_id: i64, - target_id: Option + target_id: Option, }, BakerRemoved { - baker_id: i64 - } + baker_id: i64, + }, } impl PreparedAccountDelegationEvent { - fn prepare(event: &concordium_rust_sdk::types::DelegationEvent) -> anyhow::Result { use concordium_rust_sdk::types::DelegationEvent; let prepared = match event { @@ -1342,50 +1341,51 @@ impl PreparedAccountDelegationEvent { new_stake, } => PreparedAccountDelegationEvent::StakeIncrease { account_id: delegator_id.id.index.try_into()?, - staked: new_stake.micro_ccd.try_into()? + staked: new_stake.micro_ccd.try_into()?, }, DelegationEvent::DelegationStakeDecreased { delegator_id, new_stake, } => PreparedAccountDelegationEvent::StakeIncrease { account_id: delegator_id.id.index.try_into()?, - staked: new_stake.micro_ccd.try_into()? + staked: new_stake.micro_ccd.try_into()?, }, DelegationEvent::DelegationSetRestakeEarnings { delegator_id, restake_earnings, } => PreparedAccountDelegationEvent::SetRestakeEarnings { - account_id: delegator_id.id.index.try_into()?, - restake_earnings: *restake_earnings + account_id: delegator_id.id.index.try_into()?, + restake_earnings: *restake_earnings, }, DelegationEvent::DelegationSetDelegationTarget { delegator_id, - delegation_target - } => { - PreparedAccountDelegationEvent::SetDelegationTarget { - account_id: delegator_id.id.index.try_into()?, - target_id: if let DelegationTarget::Baker { baker_id } = delegation_target { - Some(baker_id.id.index.try_into()?) - } else { - None - } - } + delegation_target, + } => PreparedAccountDelegationEvent::SetDelegationTarget { + account_id: delegator_id.id.index.try_into()?, + target_id: if let DelegationTarget::Baker { + baker_id, + } = delegation_target + { + Some(baker_id.id.index.try_into()?) + } else { + None + }, }, DelegationEvent::DelegationAdded { - delegator_id + delegator_id, } => PreparedAccountDelegationEvent::Added { account_id: delegator_id.id.index.try_into()?, }, DelegationEvent::DelegationRemoved { - delegator_id + delegator_id, } => PreparedAccountDelegationEvent::Removed { - account_id: delegator_id.id.index.try_into()? + account_id: delegator_id.id.index.try_into()?, }, DelegationEvent::BakerRemoved { - baker_id + baker_id, } => PreparedAccountDelegationEvent::BakerRemoved { - baker_id: baker_id.id.index.try_into()? - } + baker_id: baker_id.id.index.try_into()?, + }, }; Ok(prepared) } @@ -1397,10 +1397,11 @@ impl PreparedAccountDelegationEvent { match self { PreparedAccountDelegationEvent::StakeIncrease { account_id, - staked - } | PreparedAccountDelegationEvent::StakeDecrease { + staked, + } + | PreparedAccountDelegationEvent::StakeDecrease { account_id, - staked + staked, } => { sqlx::query!( r#"UPDATE accounts SET delegated_stake = $1 WHERE index = $2"#, @@ -1409,11 +1410,12 @@ impl PreparedAccountDelegationEvent { ) .execute(tx.as_mut()) .await?; - }, + } PreparedAccountDelegationEvent::Added { - account_id - } | PreparedAccountDelegationEvent::Removed { - account_id + account_id, + } + | PreparedAccountDelegationEvent::Removed { + account_id, } => { sqlx::query!( r#"UPDATE accounts SET delegated_stake = 0, delegated_restake_earnings = false, delegated_target_baker_id = NULL WHERE index = $1"#, @@ -1421,11 +1423,11 @@ impl PreparedAccountDelegationEvent { ) .execute(tx.as_mut()) .await?; - }, + } PreparedAccountDelegationEvent::SetRestakeEarnings { account_id, - restake_earnings + restake_earnings, } => { sqlx::query!( r#"UPDATE accounts SET delegated_restake_earnings = $1 WHERE index = $2"#, @@ -1434,7 +1436,7 @@ impl PreparedAccountDelegationEvent { ) .execute(tx.as_mut()) .await?; - }, + } PreparedAccountDelegationEvent::SetDelegationTarget { account_id, target_id, @@ -1446,9 +1448,9 @@ impl PreparedAccountDelegationEvent { ) .execute(tx.as_mut()) .await?; - }, + } PreparedAccountDelegationEvent::BakerRemoved { - baker_id + baker_id, } => { todo!() } From 34927441f9629e41c2110df38de62eff3df92ca3 Mon Sep 17 00:00:00 2001 From: Lasse Alm Date: Tue, 10 Dec 2024 20:53:06 +0100 Subject: [PATCH 03/21] update queries --- ...49eefe69f67bc6905eebbd5fa085088f6dfd3.json | 15 ++++ ...72bfd471ad01b0525963cd54f90af0e7ff754.json | 15 ++++ ...854ebd057e3a6a102b568237c8133a3b59d0.json} | 18 ++++- ...ae3ddb055d1c6eee9d9acc8ab19fb761ead93.json | 15 ++++ ...99ab3efe860096d0eca57eec544d9fc425dc7.json | 72 +++++++++++++++++++ ...029ec71a46d79dccbf7212637410007baef1a.json | 14 ++++ ...eadac44aa7c1d1908890b5ef17228eb1cfdd.json} | 18 ++++- ...253d6df9f1c2e73c6fa614b0ee006fe02a4f1.json | 60 ---------------- 8 files changed, 161 insertions(+), 66 deletions(-) create mode 100644 backend-rust/.sqlx/query-21e808b7fc16e08f69073962b8249eefe69f67bc6905eebbd5fa085088f6dfd3.json create mode 100644 backend-rust/.sqlx/query-4a2e248547ecff1597a2af85bc972bfd471ad01b0525963cd54f90af0e7ff754.json rename backend-rust/.sqlx/{query-53a59d1b3fac5c589b1932eb0e8e163ece1478e0b1e4ac11132b81fb60a92c45.json => query-847daec8c3fceb973b3953d545e3854ebd057e3a6a102b568237c8133a3b59d0.json} (64%) create mode 100644 backend-rust/.sqlx/query-97eb429901e2743d5f5856898e6ae3ddb055d1c6eee9d9acc8ab19fb761ead93.json create mode 100644 backend-rust/.sqlx/query-9c80a9a238f8edb4ec6cafbe30699ab3efe860096d0eca57eec544d9fc425dc7.json create mode 100644 backend-rust/.sqlx/query-9cd093d07f4b4f12296aae9be64029ec71a46d79dccbf7212637410007baef1a.json rename backend-rust/.sqlx/{query-c9ef64880eb1000b7a70b277a5e9f3e01cf8f0f6e47a65b034e7d468aedf60d6.json => query-df348e3b818510fd64f27578e842eadac44aa7c1d1908890b5ef17228eb1cfdd.json} (64%) delete mode 100644 backend-rust/.sqlx/query-f5f36873b5724173f3e7733dece253d6df9f1c2e73c6fa614b0ee006fe02a4f1.json diff --git a/backend-rust/.sqlx/query-21e808b7fc16e08f69073962b8249eefe69f67bc6905eebbd5fa085088f6dfd3.json b/backend-rust/.sqlx/query-21e808b7fc16e08f69073962b8249eefe69f67bc6905eebbd5fa085088f6dfd3.json new file mode 100644 index 000000000..4ee0bc38a --- /dev/null +++ b/backend-rust/.sqlx/query-21e808b7fc16e08f69073962b8249eefe69f67bc6905eebbd5fa085088f6dfd3.json @@ -0,0 +1,15 @@ +{ + "db_name": "PostgreSQL", + "query": "UPDATE accounts SET delegated_restake_earnings = $1 WHERE index = $2", + "describe": { + "columns": [], + "parameters": { + "Left": [ + "Bool", + "Int8" + ] + }, + "nullable": [] + }, + "hash": "21e808b7fc16e08f69073962b8249eefe69f67bc6905eebbd5fa085088f6dfd3" +} diff --git a/backend-rust/.sqlx/query-4a2e248547ecff1597a2af85bc972bfd471ad01b0525963cd54f90af0e7ff754.json b/backend-rust/.sqlx/query-4a2e248547ecff1597a2af85bc972bfd471ad01b0525963cd54f90af0e7ff754.json new file mode 100644 index 000000000..58d4c35b5 --- /dev/null +++ b/backend-rust/.sqlx/query-4a2e248547ecff1597a2af85bc972bfd471ad01b0525963cd54f90af0e7ff754.json @@ -0,0 +1,15 @@ +{ + "db_name": "PostgreSQL", + "query": "UPDATE accounts SET delegated_stake = $1 WHERE index = $2", + "describe": { + "columns": [], + "parameters": { + "Left": [ + "Int8", + "Int8" + ] + }, + "nullable": [] + }, + "hash": "4a2e248547ecff1597a2af85bc972bfd471ad01b0525963cd54f90af0e7ff754" +} diff --git a/backend-rust/.sqlx/query-53a59d1b3fac5c589b1932eb0e8e163ece1478e0b1e4ac11132b81fb60a92c45.json b/backend-rust/.sqlx/query-847daec8c3fceb973b3953d545e3854ebd057e3a6a102b568237c8133a3b59d0.json similarity index 64% rename from backend-rust/.sqlx/query-53a59d1b3fac5c589b1932eb0e8e163ece1478e0b1e4ac11132b81fb60a92c45.json rename to backend-rust/.sqlx/query-847daec8c3fceb973b3953d545e3854ebd057e3a6a102b568237c8133a3b59d0.json index d549a2608..fcf4ff4ad 100644 --- a/backend-rust/.sqlx/query-53a59d1b3fac5c589b1932eb0e8e163ece1478e0b1e4ac11132b81fb60a92c45.json +++ b/backend-rust/.sqlx/query-847daec8c3fceb973b3953d545e3854ebd057e3a6a102b568237c8133a3b59d0.json @@ -1,6 +1,6 @@ { "db_name": "PostgreSQL", - "query": "SELECT index, transaction_index, address, amount, delegated_stake, num_txs\n FROM accounts\n WHERE address = $1", + "query": "SELECT index, transaction_index, address, amount, delegated_stake, num_txs, delegated_restake_earnings, delegated_target_baker_id\n FROM accounts\n WHERE address = $1", "describe": { "columns": [ { @@ -32,6 +32,16 @@ "ordinal": 5, "name": "num_txs", "type_info": "Int8" + }, + { + "ordinal": 6, + "name": "delegated_restake_earnings", + "type_info": "Bool" + }, + { + "ordinal": 7, + "name": "delegated_target_baker_id", + "type_info": "Int8" } ], "parameters": { @@ -45,8 +55,10 @@ false, false, false, - false + false, + true, + true ] }, - "hash": "53a59d1b3fac5c589b1932eb0e8e163ece1478e0b1e4ac11132b81fb60a92c45" + "hash": "847daec8c3fceb973b3953d545e3854ebd057e3a6a102b568237c8133a3b59d0" } diff --git a/backend-rust/.sqlx/query-97eb429901e2743d5f5856898e6ae3ddb055d1c6eee9d9acc8ab19fb761ead93.json b/backend-rust/.sqlx/query-97eb429901e2743d5f5856898e6ae3ddb055d1c6eee9d9acc8ab19fb761ead93.json new file mode 100644 index 000000000..b563ca0d6 --- /dev/null +++ b/backend-rust/.sqlx/query-97eb429901e2743d5f5856898e6ae3ddb055d1c6eee9d9acc8ab19fb761ead93.json @@ -0,0 +1,15 @@ +{ + "db_name": "PostgreSQL", + "query": "UPDATE accounts SET delegated_target_baker_id = $1 WHERE index = $2", + "describe": { + "columns": [], + "parameters": { + "Left": [ + "Int8", + "Int8" + ] + }, + "nullable": [] + }, + "hash": "97eb429901e2743d5f5856898e6ae3ddb055d1c6eee9d9acc8ab19fb761ead93" +} diff --git a/backend-rust/.sqlx/query-9c80a9a238f8edb4ec6cafbe30699ab3efe860096d0eca57eec544d9fc425dc7.json b/backend-rust/.sqlx/query-9c80a9a238f8edb4ec6cafbe30699ab3efe860096d0eca57eec544d9fc425dc7.json new file mode 100644 index 000000000..bbbb86889 --- /dev/null +++ b/backend-rust/.sqlx/query-9c80a9a238f8edb4ec6cafbe30699ab3efe860096d0eca57eec544d9fc425dc7.json @@ -0,0 +1,72 @@ +{ + "db_name": "PostgreSQL", + "query": "SELECT * FROM (\n SELECT\n index,\n transaction_index,\n address,\n amount,\n delegated_stake,\n num_txs,\n delegated_restake_earnings,\n delegated_target_baker_id\n FROM accounts\n WHERE\n -- Filter for only the accounts that are within the\n -- range that correspond to the requested page.\n -- The first condition is true only if we don't order by that field.\n -- Then the whole OR condition will be true, so the filter for that\n -- field will be ignored.\n (NOT $3 OR index > $1 AND index < $2) AND\n (NOT $4 OR amount > $1 AND amount < $2) AND\n (NOT $5 OR num_txs > $1 AND num_txs < $2) AND\n (NOT $6 OR delegated_stake > $1 AND delegated_stake < $2) AND\n -- Need to filter for only delegators if the user requests this.\n (NOT $7 OR delegated_stake > 0)\n ORDER BY\n -- Order by the field requested, and by desc/asc as appropriate.\n -- The first condition is true if we order by that field.\n -- Otherwise false, which makes the CASE null, which means\n -- it will not affect the ordering at all.\n (CASE WHEN $3 AND $8 THEN index END) DESC,\n (CASE WHEN $3 AND NOT $8 THEN index END) ASC,\n (CASE WHEN $4 AND $8 THEN amount END) DESC,\n (CASE WHEN $4 AND NOT $8 THEN amount END) ASC,\n (CASE WHEN $5 AND $8 THEN num_txs END) DESC,\n (CASE WHEN $5 AND NOT $8 THEN num_txs END) ASC,\n (CASE WHEN $6 AND $8 THEN delegated_stake END) DESC,\n (CASE WHEN $6 AND NOT $8 THEN delegated_stake END) ASC\n LIMIT $9\n )\n -- We need to order each page ASC still, we only use the DESC/ASC ordering above\n -- to select page items from the start/end of the range.\n -- Each page must still independently be ordered ascending.\n -- See also https://relay.dev/graphql/connections.htm#sec-Edge-order\n ORDER BY CASE\n WHEN $3 THEN index\n WHEN $4 THEN amount\n WHEN $5 THEN num_txs\n WHEN $6 THEN delegated_stake\n END ASC", + "describe": { + "columns": [ + { + "ordinal": 0, + "name": "index", + "type_info": "Int8" + }, + { + "ordinal": 1, + "name": "transaction_index", + "type_info": "Int8" + }, + { + "ordinal": 2, + "name": "address", + "type_info": "Bpchar" + }, + { + "ordinal": 3, + "name": "amount", + "type_info": "Int8" + }, + { + "ordinal": 4, + "name": "delegated_stake", + "type_info": "Int8" + }, + { + "ordinal": 5, + "name": "num_txs", + "type_info": "Int8" + }, + { + "ordinal": 6, + "name": "delegated_restake_earnings", + "type_info": "Bool" + }, + { + "ordinal": 7, + "name": "delegated_target_baker_id", + "type_info": "Int8" + } + ], + "parameters": { + "Left": [ + "Int8", + "Int8", + "Bool", + "Bool", + "Bool", + "Bool", + "Bool", + "Bool", + "Int8" + ] + }, + "nullable": [ + false, + true, + false, + false, + false, + false, + true, + true + ] + }, + "hash": "9c80a9a238f8edb4ec6cafbe30699ab3efe860096d0eca57eec544d9fc425dc7" +} diff --git a/backend-rust/.sqlx/query-9cd093d07f4b4f12296aae9be64029ec71a46d79dccbf7212637410007baef1a.json b/backend-rust/.sqlx/query-9cd093d07f4b4f12296aae9be64029ec71a46d79dccbf7212637410007baef1a.json new file mode 100644 index 000000000..559854b77 --- /dev/null +++ b/backend-rust/.sqlx/query-9cd093d07f4b4f12296aae9be64029ec71a46d79dccbf7212637410007baef1a.json @@ -0,0 +1,14 @@ +{ + "db_name": "PostgreSQL", + "query": "UPDATE accounts SET delegated_stake = 0, delegated_restake_earnings = false, delegated_target_baker_id = NULL WHERE index = $1", + "describe": { + "columns": [], + "parameters": { + "Left": [ + "Int8" + ] + }, + "nullable": [] + }, + "hash": "9cd093d07f4b4f12296aae9be64029ec71a46d79dccbf7212637410007baef1a" +} diff --git a/backend-rust/.sqlx/query-c9ef64880eb1000b7a70b277a5e9f3e01cf8f0f6e47a65b034e7d468aedf60d6.json b/backend-rust/.sqlx/query-df348e3b818510fd64f27578e842eadac44aa7c1d1908890b5ef17228eb1cfdd.json similarity index 64% rename from backend-rust/.sqlx/query-c9ef64880eb1000b7a70b277a5e9f3e01cf8f0f6e47a65b034e7d468aedf60d6.json rename to backend-rust/.sqlx/query-df348e3b818510fd64f27578e842eadac44aa7c1d1908890b5ef17228eb1cfdd.json index c6c522027..526f2f921 100644 --- a/backend-rust/.sqlx/query-c9ef64880eb1000b7a70b277a5e9f3e01cf8f0f6e47a65b034e7d468aedf60d6.json +++ b/backend-rust/.sqlx/query-df348e3b818510fd64f27578e842eadac44aa7c1d1908890b5ef17228eb1cfdd.json @@ -1,6 +1,6 @@ { "db_name": "PostgreSQL", - "query": "SELECT index, transaction_index, address, amount, delegated_stake, num_txs\n FROM accounts\n WHERE index = $1", + "query": "SELECT index, transaction_index, address, amount, delegated_stake, num_txs, delegated_restake_earnings, delegated_target_baker_id\n FROM accounts\n WHERE index = $1", "describe": { "columns": [ { @@ -32,6 +32,16 @@ "ordinal": 5, "name": "num_txs", "type_info": "Int8" + }, + { + "ordinal": 6, + "name": "delegated_restake_earnings", + "type_info": "Bool" + }, + { + "ordinal": 7, + "name": "delegated_target_baker_id", + "type_info": "Int8" } ], "parameters": { @@ -45,8 +55,10 @@ false, false, false, - false + false, + true, + true ] }, - "hash": "c9ef64880eb1000b7a70b277a5e9f3e01cf8f0f6e47a65b034e7d468aedf60d6" + "hash": "df348e3b818510fd64f27578e842eadac44aa7c1d1908890b5ef17228eb1cfdd" } diff --git a/backend-rust/.sqlx/query-f5f36873b5724173f3e7733dece253d6df9f1c2e73c6fa614b0ee006fe02a4f1.json b/backend-rust/.sqlx/query-f5f36873b5724173f3e7733dece253d6df9f1c2e73c6fa614b0ee006fe02a4f1.json deleted file mode 100644 index 3104b7557..000000000 --- a/backend-rust/.sqlx/query-f5f36873b5724173f3e7733dece253d6df9f1c2e73c6fa614b0ee006fe02a4f1.json +++ /dev/null @@ -1,60 +0,0 @@ -{ - "db_name": "PostgreSQL", - "query": "SELECT * FROM (\n SELECT\n index,\n transaction_index,\n address,\n amount,\n delegated_stake,\n num_txs\n FROM accounts\n WHERE\n -- Filter for only the accounts that are within the\n -- range that correspond to the requested page.\n -- The first condition is true only if we don't order by that field.\n -- Then the whole OR condition will be true, so the filter for that\n -- field will be ignored.\n (NOT $3 OR index > $1 AND index < $2) AND\n (NOT $4 OR amount > $1 AND amount < $2) AND\n (NOT $5 OR num_txs > $1 AND num_txs < $2) AND\n (NOT $6 OR delegated_stake > $1 AND delegated_stake < $2) AND\n -- Need to filter for only delegators if the user requests this.\n (NOT $7 OR delegated_stake > 0)\n ORDER BY\n -- Order by the field requested, and by desc/asc as appropriate.\n -- The first condition is true if we order by that field.\n -- Otherwise false, which makes the CASE null, which means\n -- it will not affect the ordering at all.\n (CASE WHEN $3 AND $8 THEN index END) DESC,\n (CASE WHEN $3 AND NOT $8 THEN index END) ASC,\n (CASE WHEN $4 AND $8 THEN amount END) DESC,\n (CASE WHEN $4 AND NOT $8 THEN amount END) ASC,\n (CASE WHEN $5 AND $8 THEN num_txs END) DESC,\n (CASE WHEN $5 AND NOT $8 THEN num_txs END) ASC,\n (CASE WHEN $6 AND $8 THEN delegated_stake END) DESC,\n (CASE WHEN $6 AND NOT $8 THEN delegated_stake END) ASC\n LIMIT $9\n )\n -- We need to order each page ASC still, we only use the DESC/ASC ordering above\n -- to select page items from the start/end of the range.\n -- Each page must still independently be ordered ascending.\n -- See also https://relay.dev/graphql/connections.htm#sec-Edge-order\n ORDER BY CASE\n WHEN $3 THEN index\n WHEN $4 THEN amount\n WHEN $5 THEN num_txs\n WHEN $6 THEN delegated_stake\n END ASC", - "describe": { - "columns": [ - { - "ordinal": 0, - "name": "index", - "type_info": "Int8" - }, - { - "ordinal": 1, - "name": "transaction_index", - "type_info": "Int8" - }, - { - "ordinal": 2, - "name": "address", - "type_info": "Bpchar" - }, - { - "ordinal": 3, - "name": "amount", - "type_info": "Int8" - }, - { - "ordinal": 4, - "name": "delegated_stake", - "type_info": "Int8" - }, - { - "ordinal": 5, - "name": "num_txs", - "type_info": "Int8" - } - ], - "parameters": { - "Left": [ - "Int8", - "Int8", - "Bool", - "Bool", - "Bool", - "Bool", - "Bool", - "Bool", - "Int8" - ] - }, - "nullable": [ - false, - true, - false, - false, - false, - false - ] - }, - "hash": "f5f36873b5724173f3e7733dece253d6df9f1c2e73c6fa614b0ee006fe02a4f1" -} From b0b7176fd25f5e7c9a4c40470b01e6e580a36640 Mon Sep 17 00:00:00 2001 From: Lasse Alm Date: Tue, 10 Dec 2024 21:02:23 +0100 Subject: [PATCH 04/21] fix clippy --- backend-rust/src/graphql_api.rs | 28 ++++++++++------------------ 1 file changed, 10 insertions(+), 18 deletions(-) diff --git a/backend-rust/src/graphql_api.rs b/backend-rust/src/graphql_api.rs index 4aa60c4de..b74658b30 100644 --- a/backend-rust/src/graphql_api.rs +++ b/backend-rust/src/graphql_api.rs @@ -3122,24 +3122,16 @@ impl Account { async fn amount(&self) -> Amount { self.amount } async fn delegation(&self) -> Option { - if let Some(delegated_restake_earnings) = &self.delegated_restake_earnings { - Some(Delegation { - delegator_id: self.index, - restake_earnings: *delegated_restake_earnings, - staked_amount: self.delegated_stake, - delegation_target: if let Some(target) = self.delegated_target_baker_id { - DelegationTarget::BakerDelegationTarget(BakerDelegationTarget { - baker_id: target, - }) - } else { - DelegationTarget::PassiveDelegationTarget(PassiveDelegationTarget { - dummy: false, - }) - }, - }) - } else { - None - } + self.delegated_restake_earnings.map(|restake_earnings| Delegation { + delegator_id: self.index, + restake_earnings, + staked_amount: self.delegated_stake, + delegation_target: if let Some(target) = self.delegated_target_baker_id { + DelegationTarget::BakerDelegationTarget(BakerDelegationTarget { baker_id: target }) + } else { + DelegationTarget::PassiveDelegationTarget(PassiveDelegationTarget { dummy: false }) + } + }) } /// Timestamp of the block where this account was created. From e605ba95778cf94e4b51935bd3bebacb06a7ebb4 Mon Sep 17 00:00:00 2001 From: Lasse Alm Date: Tue, 10 Dec 2024 21:13:36 +0100 Subject: [PATCH 05/21] fix fmt --- backend-rust/src/graphql_api.rs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/backend-rust/src/graphql_api.rs b/backend-rust/src/graphql_api.rs index b74658b30..f73296e32 100644 --- a/backend-rust/src/graphql_api.rs +++ b/backend-rust/src/graphql_api.rs @@ -3127,10 +3127,14 @@ impl Account { restake_earnings, staked_amount: self.delegated_stake, delegation_target: if let Some(target) = self.delegated_target_baker_id { - DelegationTarget::BakerDelegationTarget(BakerDelegationTarget { baker_id: target }) + DelegationTarget::BakerDelegationTarget(BakerDelegationTarget { + baker_id: target, + }) } else { - DelegationTarget::PassiveDelegationTarget(PassiveDelegationTarget { dummy: false }) - } + DelegationTarget::PassiveDelegationTarget(PassiveDelegationTarget { + dummy: false, + }) + }, }) } From f52f8025a19ecb2483497f6013dd1cc3b28ba8df Mon Sep 17 00:00:00 2001 From: Lasse Alm Date: Tue, 10 Dec 2024 21:20:23 +0100 Subject: [PATCH 06/21] made annoying comment --- backend-rust/src/indexer.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend-rust/src/indexer.rs b/backend-rust/src/indexer.rs index 643769f96..106ee4a13 100644 --- a/backend-rust/src/indexer.rs +++ b/backend-rust/src/indexer.rs @@ -1043,7 +1043,7 @@ enum PreparedEvent { AccountCreation(PreparedAccountCreation), /// Changes related to validators (previously referred to as bakers). BakerEvents(Vec), - // TODO + /// Account delegation events AccountDelegationEvents(Vec), /// Smart contract module got deployed. ModuleDeployed(PreparedModuleDeployed), From a76ff55c299e2b02e43b3fd947db985cc18a9eb5 Mon Sep 17 00:00:00 2001 From: Lasse Alm Date: Tue, 10 Dec 2024 21:25:54 +0100 Subject: [PATCH 07/21] fix newline --- backend-rust/migrations/0003_account_delegation.up.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend-rust/migrations/0003_account_delegation.up.sql b/backend-rust/migrations/0003_account_delegation.up.sql index 8219e7c71..a48c4e695 100644 --- a/backend-rust/migrations/0003_account_delegation.up.sql +++ b/backend-rust/migrations/0003_account_delegation.up.sql @@ -1,4 +1,4 @@ -- Add up migration script here ALTER TABLE accounts ADD COLUMN delegated_restake_earnings BOOLEAN NULL, -ADD COLUMN delegated_target_baker_id BIGINT NULL \ No newline at end of file +ADD COLUMN delegated_target_baker_id BIGINT NULL From 549b0dd217f9d78341840bf8cce65720cc4324d2 Mon Sep 17 00:00:00 2001 From: Lasse Alm Date: Wed, 11 Dec 2024 11:49:38 +0100 Subject: [PATCH 08/21] fix events which were not implemented --- backend-rust/src/indexer.rs | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/backend-rust/src/indexer.rs b/backend-rust/src/indexer.rs index 106ee4a13..ad95f0eeb 100644 --- a/backend-rust/src/indexer.rs +++ b/backend-rust/src/indexer.rs @@ -1452,7 +1452,15 @@ impl PreparedAccountDelegationEvent { PreparedAccountDelegationEvent::BakerRemoved { baker_id, } => { - todo!() + sqlx::query!( + r#"UPDATE accounts SET delegated_stake = 0, delegated_restake_earnings = false, delegated_target_baker_id = NULL WHERE index = $1"#, + *baker_id + ) + .execute(tx.as_mut()) + .await?; + sqlx::query!(r#"DELETE FROM bakers WHERE id=$1"#, baker_id,) + .execute(tx.as_mut()) + .await?; } } Ok(()) @@ -1710,10 +1718,14 @@ impl PreparedBakerEvent { .await?; } PreparedBakerEvent::RemoveDelegation { - delegator_id: _, + delegator_id, } => { - // TODO: Implement this when database is tracking delegation as well. - todo!() + sqlx::query!( + r#"UPDATE accounts SET delegated_stake = 0, delegated_restake_earnings = false, delegated_target_baker_id = NULL WHERE index = $1"#, + delegator_id + ) + .execute(tx.as_mut()) + .await?; } PreparedBakerEvent::NoOperation => (), } From 34cd6b9886bf610ba10a14b7442af6b5417b6b56 Mon Sep 17 00:00:00 2001 From: Lasse Alm Date: Wed, 11 Dec 2024 15:13:08 +0100 Subject: [PATCH 09/21] fix issues --- backend-rust/src/graphql_api.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/backend-rust/src/graphql_api.rs b/backend-rust/src/graphql_api.rs index f73296e32..b036ad632 100644 --- a/backend-rust/src/graphql_api.rs +++ b/backend-rust/src/graphql_api.rs @@ -4452,7 +4452,9 @@ pub fn events_from_summary( DelegationEvent::BakerRemoved { baker_id, } => { - unimplemented!(); + Ok(Event::BakerRemoved(BakerRemoved { + baker_id: baker_id.id.index.try_into()? + })) } }) .collect::>>()? From 9f3fb1db74eb0f1599dfdeb00144d7a3d9171c5c Mon Sep 17 00:00:00 2001 From: Lasse Alm Date: Wed, 11 Dec 2024 15:19:59 +0100 Subject: [PATCH 10/21] fmt --- backend-rust/src/graphql_api.rs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/backend-rust/src/graphql_api.rs b/backend-rust/src/graphql_api.rs index b036ad632..e65b62f89 100644 --- a/backend-rust/src/graphql_api.rs +++ b/backend-rust/src/graphql_api.rs @@ -4451,11 +4451,9 @@ pub fn events_from_summary( })), DelegationEvent::BakerRemoved { baker_id, - } => { - Ok(Event::BakerRemoved(BakerRemoved { - baker_id: baker_id.id.index.try_into()? - })) - } + } => Ok(Event::BakerRemoved(BakerRemoved { + baker_id: baker_id.id.index.try_into()?, + })), }) .collect::>>()? } From f5be9bd9923ca99f380895a59067e183f1c26492 Mon Sep 17 00:00:00 2001 From: Lasse Alm Date: Mon, 16 Dec 2024 20:32:14 +0100 Subject: [PATCH 11/21] fix migration --- backend-rust/migrations/0001_initialize.up.sql | 10 +++++++--- .../migrations/0003_account_delegation.down.sql | 1 - backend-rust/migrations/0003_account_delegation.up.sql | 4 ---- 3 files changed, 7 insertions(+), 8 deletions(-) delete mode 100644 backend-rust/migrations/0003_account_delegation.down.sql delete mode 100644 backend-rust/migrations/0003_account_delegation.up.sql diff --git a/backend-rust/migrations/0001_initialize.up.sql b/backend-rust/migrations/0001_initialize.up.sql index a4d8e9455..272a7b3d0 100644 --- a/backend-rust/migrations/0001_initialize.up.sql +++ b/backend-rust/migrations/0001_initialize.up.sql @@ -192,7 +192,6 @@ CREATE TABLE accounts( BIGINT REFERENCES transactions, -- The total balance of this account in micro CCD. - -- TODO: Actually populate this in the indexer. amount BIGINT NOT NULL @@ -206,11 +205,16 @@ CREATE TABLE accounts( -- Starting at 1 to count the transaction that made the account. DEFAULT 1, -- The total delegated stake of this account in micro CCD. - -- TODO: Actually populate this in the indexer. delegated_stake BIGINT NOT NULL - DEFAULT 0 + DEFAULT 0, + delegated_restake_earnings + BOOLEAN + NULL, + delegated_target_baker_id + BIGINT + NULL ); -- These are important for the sorting options on the accounts query. diff --git a/backend-rust/migrations/0003_account_delegation.down.sql b/backend-rust/migrations/0003_account_delegation.down.sql deleted file mode 100644 index d2f607c5b..000000000 --- a/backend-rust/migrations/0003_account_delegation.down.sql +++ /dev/null @@ -1 +0,0 @@ --- Add down migration script here diff --git a/backend-rust/migrations/0003_account_delegation.up.sql b/backend-rust/migrations/0003_account_delegation.up.sql deleted file mode 100644 index a48c4e695..000000000 --- a/backend-rust/migrations/0003_account_delegation.up.sql +++ /dev/null @@ -1,4 +0,0 @@ --- Add up migration script here -ALTER TABLE accounts -ADD COLUMN delegated_restake_earnings BOOLEAN NULL, -ADD COLUMN delegated_target_baker_id BIGINT NULL From 4f84a5aba70271d5698ad0f804e3315f3955db3c Mon Sep 17 00:00:00 2001 From: Lasse Alm Date: Tue, 17 Dec 2024 10:11:00 +0100 Subject: [PATCH 12/21] finish account delegation --- backend-rust/src/indexer.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/backend-rust/src/indexer.rs b/backend-rust/src/indexer.rs index ad95f0eeb..a23d88238 100644 --- a/backend-rust/src/indexer.rs +++ b/backend-rust/src/indexer.rs @@ -1452,6 +1452,7 @@ impl PreparedAccountDelegationEvent { PreparedAccountDelegationEvent::BakerRemoved { baker_id, } => { + // TODO: This could maybe be left out because the subsequent events should do the same. sqlx::query!( r#"UPDATE accounts SET delegated_stake = 0, delegated_restake_earnings = false, delegated_target_baker_id = NULL WHERE index = $1"#, *baker_id From 9c21a92336616ec281c5799dd31968d8c5c89280 Mon Sep 17 00:00:00 2001 From: Lasse Alm Date: Tue, 17 Dec 2024 10:53:33 +0100 Subject: [PATCH 13/21] update src --- backend-rust/src/indexer.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/backend-rust/src/indexer.rs b/backend-rust/src/indexer.rs index a23d88238..34794eed4 100644 --- a/backend-rust/src/indexer.rs +++ b/backend-rust/src/indexer.rs @@ -1452,7 +1452,8 @@ impl PreparedAccountDelegationEvent { PreparedAccountDelegationEvent::BakerRemoved { baker_id, } => { - // TODO: This could maybe be left out because the subsequent events should do the same. + // TODO: This could maybe be left out because the subsequent events should do + // the same. sqlx::query!( r#"UPDATE accounts SET delegated_stake = 0, delegated_restake_earnings = false, delegated_target_baker_id = NULL WHERE index = $1"#, *baker_id From 120e5636a492323ab5c96bb56d1281f0bdcb95d9 Mon Sep 17 00:00:00 2001 From: Lasse Alm Date: Tue, 17 Dec 2024 11:06:57 +0100 Subject: [PATCH 14/21] add todo comment --- backend-rust/migrations/0001_initialize.up.sql | 2 ++ 1 file changed, 2 insertions(+) diff --git a/backend-rust/migrations/0001_initialize.up.sql b/backend-rust/migrations/0001_initialize.up.sql index 272a7b3d0..3d102c6b1 100644 --- a/backend-rust/migrations/0001_initialize.up.sql +++ b/backend-rust/migrations/0001_initialize.up.sql @@ -209,9 +209,11 @@ CREATE TABLE accounts( BIGINT NOT NULL DEFAULT 0, + -- Whether we are re-staking earnings. Null means we are not using delegation. delegated_restake_earnings BOOLEAN NULL, + -- Target id of the baker When this is null it means that we are using passive delegation. delegated_target_baker_id BIGINT NULL From 4917798287b6b34d89b9eb2c1a834260fe033c66 Mon Sep 17 00:00:00 2001 From: Lasse Alm Date: Tue, 17 Dec 2024 11:21:37 +0100 Subject: [PATCH 15/21] added index --- backend-rust/migrations/0001_initialize.up.sql | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/backend-rust/migrations/0001_initialize.up.sql b/backend-rust/migrations/0001_initialize.up.sql index 3d102c6b1..7c92a3a05 100644 --- a/backend-rust/migrations/0001_initialize.up.sql +++ b/backend-rust/migrations/0001_initialize.up.sql @@ -219,10 +219,14 @@ CREATE TABLE accounts( NULL ); + -- These are important for the sorting options on the accounts query. CREATE INDEX accounts_amount_idx ON accounts (amount); CREATE INDEX accounts_delegated_stake_idx ON accounts (delegated_stake); CREATE INDEX accounts_num_txs_idx ON accounts (num_txs); +CREATE INDEX accounts_delegated_target_baker_id_index + on accounts (delegated_target_baker_id, delegated_stake desc) + where delegated_target_baker_id is not null; -- Add foreign key constraint now that the account table is created. ALTER TABLE transactions From d8f298524ef724cf3b927c81b0756d12312c8cd4 Mon Sep 17 00:00:00 2001 From: Lasse Alm Date: Tue, 17 Dec 2024 11:22:18 +0100 Subject: [PATCH 16/21] added index --- backend-rust/migrations/0001_initialize.up.sql | 1 - 1 file changed, 1 deletion(-) diff --git a/backend-rust/migrations/0001_initialize.up.sql b/backend-rust/migrations/0001_initialize.up.sql index 7c92a3a05..a3d39e31c 100644 --- a/backend-rust/migrations/0001_initialize.up.sql +++ b/backend-rust/migrations/0001_initialize.up.sql @@ -219,7 +219,6 @@ CREATE TABLE accounts( NULL ); - -- These are important for the sorting options on the accounts query. CREATE INDEX accounts_amount_idx ON accounts (amount); CREATE INDEX accounts_delegated_stake_idx ON accounts (delegated_stake); From 71fa1f44f59b173bd9b6b7a26fc2b3aa75bb0f70 Mon Sep 17 00:00:00 2001 From: Lasse Alm Date: Tue, 17 Dec 2024 11:23:32 +0100 Subject: [PATCH 17/21] updated sql to uppercase --- backend-rust/migrations/0001_initialize.up.sql | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/backend-rust/migrations/0001_initialize.up.sql b/backend-rust/migrations/0001_initialize.up.sql index a3d39e31c..2a1639bde 100644 --- a/backend-rust/migrations/0001_initialize.up.sql +++ b/backend-rust/migrations/0001_initialize.up.sql @@ -224,8 +224,8 @@ CREATE INDEX accounts_amount_idx ON accounts (amount); CREATE INDEX accounts_delegated_stake_idx ON accounts (delegated_stake); CREATE INDEX accounts_num_txs_idx ON accounts (num_txs); CREATE INDEX accounts_delegated_target_baker_id_index - on accounts (delegated_target_baker_id, delegated_stake desc) - where delegated_target_baker_id is not null; + ON accounts (delegated_target_baker_id, delegated_stake DESC) + WHERE delegated_target_baker_id IS NOT NULL; -- Add foreign key constraint now that the account table is created. ALTER TABLE transactions From eb47db77681dc9f486525436690023097ece5b5b Mon Sep 17 00:00:00 2001 From: Lasse Alm Date: Tue, 17 Dec 2024 13:06:44 +0100 Subject: [PATCH 18/21] introduce nooperatin --- .../migrations/0001_initialize.up.sql | 3 --- backend-rust/src/indexer.rs | 25 +++---------------- 2 files changed, 4 insertions(+), 24 deletions(-) diff --git a/backend-rust/migrations/0001_initialize.up.sql b/backend-rust/migrations/0001_initialize.up.sql index 2a1639bde..3d102c6b1 100644 --- a/backend-rust/migrations/0001_initialize.up.sql +++ b/backend-rust/migrations/0001_initialize.up.sql @@ -223,9 +223,6 @@ CREATE TABLE accounts( CREATE INDEX accounts_amount_idx ON accounts (amount); CREATE INDEX accounts_delegated_stake_idx ON accounts (delegated_stake); CREATE INDEX accounts_num_txs_idx ON accounts (num_txs); -CREATE INDEX accounts_delegated_target_baker_id_index - ON accounts (delegated_target_baker_id, delegated_stake DESC) - WHERE delegated_target_baker_id IS NOT NULL; -- Add foreign key constraint now that the account table is created. ALTER TABLE transactions diff --git a/backend-rust/src/indexer.rs b/backend-rust/src/indexer.rs index 34794eed4..d0fd94546 100644 --- a/backend-rust/src/indexer.rs +++ b/backend-rust/src/indexer.rs @@ -1327,9 +1327,7 @@ enum PreparedAccountDelegationEvent { account_id: i64, target_id: Option, }, - BakerRemoved { - baker_id: i64, - }, + NoOperation, } impl PreparedAccountDelegationEvent { @@ -1383,9 +1381,7 @@ impl PreparedAccountDelegationEvent { }, DelegationEvent::BakerRemoved { baker_id, - } => PreparedAccountDelegationEvent::BakerRemoved { - baker_id: baker_id.id.index.try_into()?, - }, + } => PreparedAccountDelegationEvent::NoOperation, }; Ok(prepared) } @@ -1449,21 +1445,8 @@ impl PreparedAccountDelegationEvent { .execute(tx.as_mut()) .await?; } - PreparedAccountDelegationEvent::BakerRemoved { - baker_id, - } => { - // TODO: This could maybe be left out because the subsequent events should do - // the same. - sqlx::query!( - r#"UPDATE accounts SET delegated_stake = 0, delegated_restake_earnings = false, delegated_target_baker_id = NULL WHERE index = $1"#, - *baker_id - ) - .execute(tx.as_mut()) - .await?; - sqlx::query!(r#"DELETE FROM bakers WHERE id=$1"#, baker_id,) - .execute(tx.as_mut()) - .await?; - } + PreparedAccountDelegationEvent::NoOperation { + } => (), } Ok(()) } From e9b67b1f75b1b479a2dc8d094ef9ae81a7f6a92d Mon Sep 17 00:00:00 2001 From: Lasse Alm Date: Tue, 17 Dec 2024 13:09:07 +0100 Subject: [PATCH 19/21] fmt --- backend-rust/src/indexer.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/backend-rust/src/indexer.rs b/backend-rust/src/indexer.rs index d0fd94546..62a7b988e 100644 --- a/backend-rust/src/indexer.rs +++ b/backend-rust/src/indexer.rs @@ -1445,8 +1445,7 @@ impl PreparedAccountDelegationEvent { .execute(tx.as_mut()) .await?; } - PreparedAccountDelegationEvent::NoOperation { - } => (), + PreparedAccountDelegationEvent::NoOperation {} => (), } Ok(()) } From 2c9c42bb064fb6c9b03a47a2413531cd1de8764a Mon Sep 17 00:00:00 2001 From: Lasse Alm Date: Tue, 17 Dec 2024 13:13:17 +0100 Subject: [PATCH 20/21] updated version --- backend-rust/Cargo.lock | 2 +- backend-rust/Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/backend-rust/Cargo.lock b/backend-rust/Cargo.lock index 4efc2f28c..09dc13a81 100644 --- a/backend-rust/Cargo.lock +++ b/backend-rust/Cargo.lock @@ -891,7 +891,7 @@ dependencies = [ [[package]] name = "concordium-scan" -version = "0.1.6" +version = "0.1.7" dependencies = [ "anyhow", "async-graphql", diff --git a/backend-rust/Cargo.toml b/backend-rust/Cargo.toml index e0ff03f88..54471b039 100644 --- a/backend-rust/Cargo.toml +++ b/backend-rust/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "concordium-scan" -version = "0.1.6" +version = "0.1.7" edition = "2021" description = "CCDScan: Indexer and API for the Concordium blockchain" authors = ["Concordium "] From 1add8a4f15b58cfe6b92f2d83312ff2342b1661c Mon Sep 17 00:00:00 2001 From: Lasse Alm Date: Tue, 17 Dec 2024 14:47:38 +0100 Subject: [PATCH 21/21] backend-rust --- backend-rust/docker-compose.yml | 2 -- backend-rust/migrations/0001_initialize.up.sql | 1 + 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/backend-rust/docker-compose.yml b/backend-rust/docker-compose.yml index 6827f76b3..eb0d4ebd6 100644 --- a/backend-rust/docker-compose.yml +++ b/backend-rust/docker-compose.yml @@ -5,8 +5,6 @@ services: container_name: postgres_db ports: - "5432:5432" - volumes: - - ./data:/var/lib/postgresql/data environment: POSTGRES_PASSWORD: ${PGPASSWORD} POSTGRES_DB: ccdscan diff --git a/backend-rust/migrations/0001_initialize.up.sql b/backend-rust/migrations/0001_initialize.up.sql index 3d102c6b1..2d1a2ffe3 100644 --- a/backend-rust/migrations/0001_initialize.up.sql +++ b/backend-rust/migrations/0001_initialize.up.sql @@ -192,6 +192,7 @@ CREATE TABLE accounts( BIGINT REFERENCES transactions, -- The total balance of this account in micro CCD. + -- TODO: Actually populate this in the indexer. amount BIGINT NOT NULL