@@ -72,10 +72,10 @@ impl GetCardanoTransactionQuery {
72
72
Self { condition }
73
73
}
74
74
75
- pub fn by_slot_number ( slot_number : SlotNumber ) -> Self {
75
+ pub fn with_highest_block_number_below_slot_number ( slot_number : SlotNumber ) -> Self {
76
76
Self {
77
77
condition : WhereCondition :: new (
78
- "slot_number = ?*" ,
78
+ "block_number = (select max(block_number) from cardano_tx where slot_number < = ?*) " ,
79
79
vec ! [ Value :: Integer ( * slot_number as i64 ) ] ,
80
80
) ,
81
81
}
@@ -120,6 +120,18 @@ mod tests {
120
120
. unwrap ( ) ;
121
121
}
122
122
123
+ fn transaction_record (
124
+ block_number : BlockNumber ,
125
+ slot_number : SlotNumber ,
126
+ ) -> CardanoTransactionRecord {
127
+ CardanoTransactionRecord :: new (
128
+ format ! ( "tx-hash-{}" , slot_number) ,
129
+ block_number,
130
+ slot_number,
131
+ format ! ( "block-hash-{}" , block_number) ,
132
+ )
133
+ }
134
+
123
135
#[ test]
124
136
fn with_highest_block_number ( ) {
125
137
let connection = cardano_tx_db_connection ( ) . unwrap ( ) ;
@@ -132,30 +144,10 @@ mod tests {
132
144
insert_transactions (
133
145
& connection,
134
146
vec ! [
135
- CardanoTransactionRecord :: new(
136
- "tx-hash-0" ,
137
- BlockNumber ( 10 ) ,
138
- SlotNumber ( 50 ) ,
139
- "block-hash-10" ,
140
- ) ,
141
- CardanoTransactionRecord :: new(
142
- "tx-hash-1" ,
143
- BlockNumber ( 10 ) ,
144
- SlotNumber ( 51 ) ,
145
- "block-hash-10" ,
146
- ) ,
147
- CardanoTransactionRecord :: new(
148
- "tx-hash-2" ,
149
- BlockNumber ( 11 ) ,
150
- SlotNumber ( 54 ) ,
151
- "block-hash-11" ,
152
- ) ,
153
- CardanoTransactionRecord :: new(
154
- "tx-hash-3" ,
155
- BlockNumber ( 11 ) ,
156
- SlotNumber ( 55 ) ,
157
- "block-hash-11" ,
158
- ) ,
147
+ transaction_record( BlockNumber ( 10 ) , SlotNumber ( 50 ) ) ,
148
+ transaction_record( BlockNumber ( 10 ) , SlotNumber ( 51 ) ) ,
149
+ transaction_record( BlockNumber ( 11 ) , SlotNumber ( 54 ) ) ,
150
+ transaction_record( BlockNumber ( 11 ) , SlotNumber ( 55 ) ) ,
159
151
] ,
160
152
) ;
161
153
@@ -164,19 +156,74 @@ mod tests {
164
156
. unwrap ( ) ;
165
157
assert_eq ! (
166
158
vec![
167
- CardanoTransactionRecord :: new(
168
- "tx-hash-2" ,
169
- BlockNumber ( 11 ) ,
170
- SlotNumber ( 54 ) ,
171
- "block-hash-11"
159
+ transaction_record( BlockNumber ( 11 ) , SlotNumber ( 54 ) ) ,
160
+ transaction_record( BlockNumber ( 11 ) , SlotNumber ( 55 ) ) ,
161
+ ] ,
162
+ records
163
+ ) ;
164
+ }
165
+
166
+ #[ test]
167
+ fn with_highest_block_number_below_slot_number ( ) {
168
+ let connection = cardano_tx_db_connection ( ) . unwrap ( ) ;
169
+
170
+ let cursor = connection
171
+ . fetch (
172
+ GetCardanoTransactionQuery :: with_highest_block_number_below_slot_number (
173
+ SlotNumber ( 51 ) ,
172
174
) ,
173
- CardanoTransactionRecord :: new(
174
- "tx-hash-3" ,
175
- BlockNumber ( 11 ) ,
176
- SlotNumber ( 55 ) ,
177
- "block-hash-11"
175
+ )
176
+ . unwrap ( ) ;
177
+ assert_eq ! ( 0 , cursor. count( ) ) ;
178
+
179
+ insert_transactions (
180
+ & connection,
181
+ vec ! [ transaction_record( BlockNumber ( 2 ) , SlotNumber ( 5 ) ) ] ,
182
+ ) ;
183
+
184
+ let records: Vec < CardanoTransactionRecord > = connection
185
+ . fetch_collect (
186
+ GetCardanoTransactionQuery :: with_highest_block_number_below_slot_number (
187
+ SlotNumber ( 5 ) ,
178
188
) ,
189
+ )
190
+ . unwrap ( ) ;
191
+ assert_eq ! (
192
+ vec![ transaction_record( BlockNumber ( 2 ) , SlotNumber ( 5 ) ) , ] ,
193
+ records
194
+ ) ;
195
+
196
+ insert_transactions (
197
+ & connection,
198
+ vec ! [
199
+ transaction_record( BlockNumber ( 10 ) , SlotNumber ( 50 ) ) ,
200
+ transaction_record( BlockNumber ( 11 ) , SlotNumber ( 51 ) ) ,
201
+ transaction_record( BlockNumber ( 14 ) , SlotNumber ( 54 ) ) ,
202
+ transaction_record( BlockNumber ( 15 ) , SlotNumber ( 55 ) ) ,
179
203
] ,
204
+ ) ;
205
+
206
+ let records: Vec < CardanoTransactionRecord > = connection
207
+ . fetch_collect (
208
+ GetCardanoTransactionQuery :: with_highest_block_number_below_slot_number (
209
+ SlotNumber ( 53 ) ,
210
+ ) ,
211
+ )
212
+ . unwrap ( ) ;
213
+ assert_eq ! (
214
+ vec![ transaction_record( BlockNumber ( 11 ) , SlotNumber ( 51 ) ) , ] ,
215
+ records
216
+ ) ;
217
+
218
+ let records: Vec < CardanoTransactionRecord > = connection
219
+ . fetch_collect (
220
+ GetCardanoTransactionQuery :: with_highest_block_number_below_slot_number (
221
+ SlotNumber ( 54 ) ,
222
+ ) ,
223
+ )
224
+ . unwrap ( ) ;
225
+ assert_eq ! (
226
+ vec![ transaction_record( BlockNumber ( 14 ) , SlotNumber ( 54 ) ) , ] ,
180
227
records
181
228
) ;
182
229
}
0 commit comments