@@ -589,6 +589,7 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
589589 "CREATE TABLE ml.delegations (
590590 delegation_id TEXT NOT NULL,
591591 block_height bigint NOT NULL,
592+ creation_block_height bigint NOT NULL,
592593 pool_id TEXT NOT NULL,
593594 balance TEXT NOT NULL,
594595 next_nonce bytea NOT NULL,
@@ -846,7 +847,7 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
846847 let row = self
847848 . tx
848849 . query_opt (
849- r#"SELECT pool_id, balance, spend_destination, next_nonce
850+ r#"SELECT pool_id, balance, spend_destination, next_nonce, creation_block_height
850851 FROM ml.delegations
851852 WHERE delegation_id = $1
852853 AND block_height = (SELECT MAX(block_height) FROM ml.delegations WHERE delegation_id = $1);
@@ -868,6 +869,7 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
868869 let balance: String = data. get ( 1 ) ;
869870 let spend_destination: Vec < u8 > = data. get ( 2 ) ;
870871 let next_nonce: Vec < u8 > = data. get ( 3 ) ;
872+ let creation_block_height: i64 = data. get ( 4 ) ;
871873
872874 let balance = Amount :: from_fixedpoint_str ( & balance, 0 ) . ok_or_else ( || {
873875 ApiServerStorageError :: DeserializationError ( format ! (
@@ -890,7 +892,13 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
890892 ) )
891893 } ) ?;
892894
893- let delegation = Delegation :: new ( spend_destination, pool_id, balance, next_nonce) ;
895+ let delegation = Delegation :: new (
896+ BlockHeight :: new ( creation_block_height as u64 ) ,
897+ spend_destination,
898+ pool_id,
899+ balance,
900+ next_nonce,
901+ ) ;
894902 Ok ( Some ( delegation) )
895903 }
896904
@@ -902,9 +910,9 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
902910 let rows = self
903911 . tx
904912 . query (
905- r#"SELECT delegation_id, pool_id, balance, spend_destination, next_nonce
913+ r#"SELECT delegation_id, pool_id, balance, spend_destination, next_nonce, creation_block_height
906914 FROM (
907- SELECT delegation_id, pool_id, balance, spend_destination, next_nonce, ROW_NUMBER() OVER(PARTITION BY delegation_id ORDER BY block_height DESC) as newest
915+ SELECT delegation_id, pool_id, balance, spend_destination, next_nonce, creation_block_height, ROW_NUMBER() OVER(PARTITION BY delegation_id ORDER BY block_height DESC) as newest
908916 FROM ml.delegations
909917 WHERE spend_destination = $1
910918 ) AS sub
@@ -929,6 +937,7 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
929937 let balance: String = row. get ( 2 ) ;
930938 let spend_destination: Vec < u8 > = row. get ( 3 ) ;
931939 let next_nonce: Vec < u8 > = row. get ( 4 ) ;
940+ let creation_block_height: i64 = row. get ( 5 ) ;
932941
933942 let balance = Amount :: from_fixedpoint_str ( & balance, 0 ) . ok_or_else ( || {
934943 ApiServerStorageError :: DeserializationError ( format ! (
@@ -950,7 +959,13 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
950959 ) )
951960 } ) ?;
952961
953- let delegation = Delegation :: new ( spend_destination, pool_id, balance, next_nonce) ;
962+ let delegation = Delegation :: new (
963+ BlockHeight :: new ( creation_block_height as u64 ) ,
964+ spend_destination,
965+ pool_id,
966+ balance,
967+ next_nonce,
968+ ) ;
954969 Ok ( ( delegation_id, delegation) )
955970 } )
956971 . collect ( )
@@ -964,6 +979,8 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
964979 chain_config : & ChainConfig ,
965980 ) -> Result < ( ) , ApiServerStorageError > {
966981 let height = Self :: block_height_to_postgres_friendly ( block_height) ;
982+ let creation_block_height =
983+ Self :: block_height_to_postgres_friendly ( delegation. creation_block_height ( ) ) ;
967984 let pool_id = Address :: new ( chain_config, * delegation. pool_id ( ) )
968985 . map_err ( |_| ApiServerStorageError :: AddressableError ) ?;
969986 let delegation_id = Address :: new ( chain_config, delegation_id)
@@ -972,10 +989,10 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
972989 self . tx
973990 . execute (
974991 r#"
975- INSERT INTO ml.delegations (delegation_id, block_height, pool_id, balance, spend_destination, next_nonce)
976- VALUES($1, $2, $3, $4, $5, $6)
992+ INSERT INTO ml.delegations (delegation_id, block_height, pool_id, balance, spend_destination, next_nonce, creation_block_height )
993+ VALUES($1, $2, $3, $4, $5, $6, $7 )
977994 ON CONFLICT (delegation_id, block_height) DO UPDATE
978- SET pool_id = $3, balance = $4, spend_destination = $5, next_nonce = $6;
995+ SET pool_id = $3, balance = $4, spend_destination = $5, next_nonce = $6, creation_block_height = $7 ;
979996 "# ,
980997 & [
981998 & delegation_id. as_str ( ) ,
@@ -984,6 +1001,7 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
9841001 & amount_to_str ( * delegation. balance ( ) ) ,
9851002 & delegation. spend_destination ( ) . encode ( ) ,
9861003 & delegation. next_nonce ( ) . encode ( ) ,
1004+ & creation_block_height,
9871005 ] ,
9881006 )
9891007 . await
@@ -1065,7 +1083,7 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
10651083 . map_err ( |_| ApiServerStorageError :: AddressableError ) ?;
10661084 self . tx
10671085 . query (
1068- r#"SELECT delegation_id, balance, spend_destination, next_nonce
1086+ r#"SELECT delegation_id, balance, spend_destination, next_nonce, creation_block_height
10691087 FROM ml.delegations
10701088 WHERE pool_id = $1
10711089 AND (delegation_id, block_height) in (SELECT delegation_id, MAX(block_height)
@@ -1087,6 +1105,7 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
10871105 let balance: String = row. get ( 1 ) ;
10881106 let spend_destination: Vec < u8 > = row. get ( 2 ) ;
10891107 let next_nonce: Vec < u8 > = row. get ( 3 ) ;
1108+ let creation_block_height: i64 = row. get ( 4 ) ;
10901109
10911110 let balance = Amount :: from_fixedpoint_str ( & balance, 0 ) . ok_or_else ( || {
10921111 ApiServerStorageError :: DeserializationError ( format ! (
@@ -1110,7 +1129,13 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
11101129
11111130 Ok ( (
11121131 delegation_id,
1113- Delegation :: new ( spend_destination, pool_id, balance, next_nonce) ,
1132+ Delegation :: new (
1133+ BlockHeight :: new ( creation_block_height as u64 ) ,
1134+ spend_destination,
1135+ pool_id,
1136+ balance,
1137+ next_nonce,
1138+ ) ,
11141139 ) )
11151140 } )
11161141 . collect ( )
@@ -1526,14 +1551,17 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
15261551 let rows = self
15271552 . tx
15281553 . query (
1529- r#"
1530- SELECT outpoint, utxo
1531- FROM ml.utxo
1532- WHERE address = $1
1533- UNION
1554+ r#"SELECT outpoint, utxo
1555+ FROM (
1556+ SELECT outpoint, utxo, spent, ROW_NUMBER() OVER(PARTITION BY outpoint ORDER BY block_height DESC) as newest
1557+ FROM ml.utxo
1558+ WHERE address = $1
1559+ ) AS sub
1560+ WHERE newest = 1 AND spent = false
1561+ UNION ALL
15341562 SELECT outpoint, utxo
1535- FROM ml.locked_utxo
1536- WHERE address = $1
1563+ FROM ml.locked_utxo AS locked
1564+ WHERE locked. address = $1 AND NOT EXISTS (SELECT 1 FROM ml.utxo WHERE outpoint = locked.outpoint)
15371565 ;"# ,
15381566 & [ & address] ,
15391567 )
0 commit comments