Skip to content

Commit 63a2925

Browse files
authored
feat(repo): Add block_time to predicate_transactions (#492)
1 parent a919e1c commit 63a2925

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

crates/domains/migrations/20250320204420_create_predicates_table.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ CREATE TABLE IF NOT EXISTS predicate_transactions (
1515
subject TEXT UNIQUE NOT NULL,
1616
predicate_id INTEGER REFERENCES predicates(id),
1717
block_height BIGINT NOT NULL,
18+
block_time TIMESTAMP WITH TIME ZONE NOT NULL,
1819
tx_id TEXT NOT NULL,
1920
tx_index INTEGER NOT NULL,
2021
input_index INTEGER NOT NULL,
@@ -25,6 +26,7 @@ CREATE TABLE IF NOT EXISTS predicate_transactions (
2526

2627
CREATE INDEX IF NOT EXISTS idx_predicate_transactions_cursor ON predicate_transactions (cursor);
2728
CREATE INDEX IF NOT EXISTS idx_predicate_transactions_predicate_id ON predicate_transactions (predicate_id);
29+
CREATE INDEX IF NOT EXISTS idx_predicate_transactions_block_time ON predicate_transactions (block_time);
2830
CREATE INDEX IF NOT EXISTS idx_predicate_transactions_block_height ON predicate_transactions (block_height);
2931
CREATE INDEX IF NOT EXISTS idx_predicate_transactions_tx_id ON predicate_transactions (tx_id);
3032
CREATE INDEX IF NOT EXISTS idx_predicate_transactions_tx_index ON predicate_transactions (tx_index);

crates/domains/src/predicates/repository.rs

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,21 @@ impl Predicate {
5656
cursor,
5757
subject,
5858
block_height,
59+
block_time,
5960
tx_id,
6061
tx_index,
6162
input_index,
6263
asset_id,
6364
bytecode
6465
)
65-
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
66+
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
6667
"#,
6768
)
6869
.bind(predicate_id)
6970
.bind(db_item.cursor().to_string())
7071
.bind(&db_item.subject)
7172
.bind(db_item.block_height.into_inner() as i64)
73+
.bind(db_item.block_time)
7274
.bind(&db_item.tx_id)
7375
.bind(db_item.tx_index)
7476
.bind(db_item.input_index)
@@ -145,17 +147,16 @@ mod tests {
145147
db: &Arc<Db>,
146148
input: Option<Input>,
147149
height: BlockHeight,
150+
block_time: BlockTimestamp,
148151
namespace: &str,
149152
) -> anyhow::Result<(PredicateDbItem, Input, DynPredicateSubject)> {
150153
let input = input.unwrap_or_else(MockInput::coin_predicate);
151154
let tx =
152155
MockTransaction::script(vec![input.to_owned()], vec![], vec![]);
153-
154156
let subject =
155157
DynPredicateSubject::new(&input, &height, &tx.id, 0, 0).unwrap();
156-
let timestamps = BlockTimestamp::default();
157158
let packet = subject
158-
.build_packet(timestamps, RecordPointer {
159+
.build_packet(block_time, RecordPointer {
159160
block_height: height,
160161
tx_id: Some(tx.id.to_owned()),
161162
tx_index: Some(0_u32),
@@ -178,8 +179,14 @@ mod tests {
178179
) -> anyhow::Result<Vec<PredicateDbItem>> {
179180
let mut predicates = Vec::with_capacity(count as usize);
180181
for height in 1..=count {
181-
let (db_item, _, _) =
182-
insert_predicate(db, None, height.into(), namespace).await?;
182+
let (db_item, _, _) = insert_predicate(
183+
db,
184+
None,
185+
height.into(),
186+
BlockTimestamp::now(),
187+
namespace,
188+
)
189+
.await?;
183190
predicates.push(db_item);
184191
}
185192
Ok(predicates)
@@ -192,6 +199,7 @@ mod tests {
192199
&db,
193200
Some(MockInput::coin_predicate()),
194201
1.into(),
202+
BlockTimestamp::now(),
195203
&namespace,
196204
)
197205
.await?;
@@ -205,6 +213,7 @@ mod tests {
205213
&db,
206214
Some(MockInput::coin_signed(None)),
207215
1.into(),
216+
BlockTimestamp::now(),
208217
&namespace,
209218
)
210219
.await?;
@@ -214,8 +223,14 @@ mod tests {
214223
#[tokio::test]
215224
async fn test_find_one_predicate() -> anyhow::Result<()> {
216225
let (db, namespace) = setup_db().await?;
217-
let (db_item, _, subject) =
218-
insert_predicate(&db, None, 1.into(), &namespace).await?;
226+
let (db_item, _, subject) = insert_predicate(
227+
&db,
228+
None,
229+
1.into(),
230+
BlockTimestamp::now(),
231+
&namespace,
232+
)
233+
.await?;
219234

220235
let mut query = subject.to_query_params();
221236
query.with_namespace(Some(namespace));

0 commit comments

Comments
 (0)