@@ -589,6 +589,7 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
589
589
"CREATE TABLE ml.delegations (
590
590
delegation_id TEXT NOT NULL,
591
591
block_height bigint NOT NULL,
592
+ creation_block_height bigint NOT NULL,
592
593
pool_id TEXT NOT NULL,
593
594
balance TEXT NOT NULL,
594
595
next_nonce bytea NOT NULL,
@@ -846,7 +847,7 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
846
847
let row = self
847
848
. tx
848
849
. 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
850
851
FROM ml.delegations
851
852
WHERE delegation_id = $1
852
853
AND block_height = (SELECT MAX(block_height) FROM ml.delegations WHERE delegation_id = $1);
@@ -868,6 +869,7 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
868
869
let balance: String = data. get ( 1 ) ;
869
870
let spend_destination: Vec < u8 > = data. get ( 2 ) ;
870
871
let next_nonce: Vec < u8 > = data. get ( 3 ) ;
872
+ let creation_block_height: i64 = data. get ( 4 ) ;
871
873
872
874
let balance = Amount :: from_fixedpoint_str ( & balance, 0 ) . ok_or_else ( || {
873
875
ApiServerStorageError :: DeserializationError ( format ! (
@@ -890,7 +892,13 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
890
892
) )
891
893
} ) ?;
892
894
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
+ ) ;
894
902
Ok ( Some ( delegation) )
895
903
}
896
904
@@ -902,9 +910,9 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
902
910
let rows = self
903
911
. tx
904
912
. 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
906
914
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
908
916
FROM ml.delegations
909
917
WHERE spend_destination = $1
910
918
) AS sub
@@ -929,6 +937,7 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
929
937
let balance: String = row. get ( 2 ) ;
930
938
let spend_destination: Vec < u8 > = row. get ( 3 ) ;
931
939
let next_nonce: Vec < u8 > = row. get ( 4 ) ;
940
+ let creation_block_height: i64 = row. get ( 5 ) ;
932
941
933
942
let balance = Amount :: from_fixedpoint_str ( & balance, 0 ) . ok_or_else ( || {
934
943
ApiServerStorageError :: DeserializationError ( format ! (
@@ -950,7 +959,13 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
950
959
) )
951
960
} ) ?;
952
961
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
+ ) ;
954
969
Ok ( ( delegation_id, delegation) )
955
970
} )
956
971
. collect ( )
@@ -964,6 +979,8 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
964
979
chain_config : & ChainConfig ,
965
980
) -> Result < ( ) , ApiServerStorageError > {
966
981
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 ( ) ) ;
967
984
let pool_id = Address :: new ( chain_config, * delegation. pool_id ( ) )
968
985
. map_err ( |_| ApiServerStorageError :: AddressableError ) ?;
969
986
let delegation_id = Address :: new ( chain_config, delegation_id)
@@ -972,10 +989,10 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
972
989
self . tx
973
990
. execute (
974
991
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 )
977
994
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 ;
979
996
"# ,
980
997
& [
981
998
& delegation_id. as_str ( ) ,
@@ -984,6 +1001,7 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
984
1001
& amount_to_str ( * delegation. balance ( ) ) ,
985
1002
& delegation. spend_destination ( ) . encode ( ) ,
986
1003
& delegation. next_nonce ( ) . encode ( ) ,
1004
+ & creation_block_height,
987
1005
] ,
988
1006
)
989
1007
. await
@@ -1065,7 +1083,7 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
1065
1083
. map_err ( |_| ApiServerStorageError :: AddressableError ) ?;
1066
1084
self . tx
1067
1085
. query (
1068
- r#"SELECT delegation_id, balance, spend_destination, next_nonce
1086
+ r#"SELECT delegation_id, balance, spend_destination, next_nonce, creation_block_height
1069
1087
FROM ml.delegations
1070
1088
WHERE pool_id = $1
1071
1089
AND (delegation_id, block_height) in (SELECT delegation_id, MAX(block_height)
@@ -1087,6 +1105,7 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
1087
1105
let balance: String = row. get ( 1 ) ;
1088
1106
let spend_destination: Vec < u8 > = row. get ( 2 ) ;
1089
1107
let next_nonce: Vec < u8 > = row. get ( 3 ) ;
1108
+ let creation_block_height: i64 = row. get ( 4 ) ;
1090
1109
1091
1110
let balance = Amount :: from_fixedpoint_str ( & balance, 0 ) . ok_or_else ( || {
1092
1111
ApiServerStorageError :: DeserializationError ( format ! (
@@ -1110,7 +1129,13 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
1110
1129
1111
1130
Ok ( (
1112
1131
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
+ ) ,
1114
1139
) )
1115
1140
} )
1116
1141
. collect ( )
@@ -1526,14 +1551,17 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
1526
1551
let rows = self
1527
1552
. tx
1528
1553
. 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
1534
1562
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)
1537
1565
;"# ,
1538
1566
& [ & address] ,
1539
1567
)
0 commit comments