From 5a16934838e6c6a37e31eee3802164d38105ec27 Mon Sep 17 00:00:00 2001 From: sputn1ck Date: Wed, 18 Oct 2023 09:25:23 +0200 Subject: [PATCH] loopdb: add reservation sqlc code --- .../migrations/000003_reservations.down.sql | 2 + .../migrations/000003_reservations.up.sql | 56 +++++ loopdb/sqlc/models.go | 23 ++ loopdb/sqlc/querier.go | 6 + loopdb/sqlc/queries/reservations.sql | 66 ++++++ loopdb/sqlc/reservations.sql.go | 222 ++++++++++++++++++ 6 files changed, 375 insertions(+) create mode 100644 loopdb/sqlc/migrations/000003_reservations.down.sql create mode 100644 loopdb/sqlc/migrations/000003_reservations.up.sql create mode 100644 loopdb/sqlc/queries/reservations.sql create mode 100644 loopdb/sqlc/reservations.sql.go diff --git a/loopdb/sqlc/migrations/000003_reservations.down.sql b/loopdb/sqlc/migrations/000003_reservations.down.sql new file mode 100644 index 0000000..281e17f --- /dev/null +++ b/loopdb/sqlc/migrations/000003_reservations.down.sql @@ -0,0 +1,2 @@ +DROP TABLE IF EXISTS reservation_updates; +DROP TABLE IF EXISTS reservations; diff --git a/loopdb/sqlc/migrations/000003_reservations.up.sql b/loopdb/sqlc/migrations/000003_reservations.up.sql new file mode 100644 index 0000000..e3fb456 --- /dev/null +++ b/loopdb/sqlc/migrations/000003_reservations.up.sql @@ -0,0 +1,56 @@ +-- reservations contains all the information about a reservation. +CREATE TABLE IF NOT EXISTS reservations ( + -- id is the auto incrementing primary key. + id INTEGER PRIMARY KEY, + + -- reservation_id is the unique identifier for the reservation. + reservation_id BLOB NOT NULL UNIQUE, + + -- client_pubkey is the public key of the client. + client_pubkey BLOB NOT NULL, + + -- server_pubkey is the public key of the server. + server_pubkey BLOB NOT NULL, + + -- expiry is the absolute expiry height of the reservation. + expiry INTEGER NOT NULL, + + -- value is the value of the reservation. + value BIGINT NOT NULL, + + -- client_key_family is the key family of the client. + client_key_family INTEGER NOT NULL, + + -- client_key_index is the key index of the client. + client_key_index INTEGER NOT NULL, + + -- initiation_height is the height at which the reservation was initiated. + initiation_height INTEGER NOT NULL, + + -- tx_hash is the hash of the transaction that created the reservation. + tx_hash BLOB, + + -- out_index is the index of the output that created the reservation. + out_index INTEGER, + + -- confirmation_height is the height at which the reservation was confirmed. + confirmation_height INTEGER +); + +CREATE INDEX IF NOT EXISTS reservations_reservation_id_idx ON reservations(reservation_id); + +-- reservation_updates contains all the updates to a reservation. +CREATE TABLE IF NOT EXISTS reservation_updates ( + -- id is the auto incrementing primary key. + id INTEGER PRIMARY KEY, + + -- reservation_id is the unique identifier for the reservation. + reservation_id BLOB NOT NULL REFERENCES reservations(reservation_id), + + -- update_state is the state of the reservation at the time of the update. + update_state TEXT NOT NULL, + + -- update_timestamp is the timestamp of the update. + update_timestamp TIMESTAMP NOT NULL +); + diff --git a/loopdb/sqlc/models.go b/loopdb/sqlc/models.go index e1aadf5..413b146 100644 --- a/loopdb/sqlc/models.go +++ b/loopdb/sqlc/models.go @@ -5,6 +5,7 @@ package sqlc import ( + "database/sql" "time" ) @@ -43,6 +44,28 @@ type LoopoutSwap struct { PublicationDeadline time.Time } +type Reservation struct { + ID int32 + ReservationID []byte + ClientPubkey []byte + ServerPubkey []byte + Expiry int32 + Value int64 + ClientKeyFamily int32 + ClientKeyIndex int32 + InitiationHeight int32 + TxHash []byte + OutIndex sql.NullInt32 + ConfirmationHeight sql.NullInt32 +} + +type ReservationUpdate struct { + ID int32 + ReservationID []byte + UpdateState string + UpdateTimestamp time.Time +} + type Swap struct { ID int32 SwapHash []byte diff --git a/loopdb/sqlc/querier.go b/loopdb/sqlc/querier.go index 6f19a37..69ae559 100644 --- a/loopdb/sqlc/querier.go +++ b/loopdb/sqlc/querier.go @@ -9,17 +9,23 @@ import ( ) type Querier interface { + CreateReservation(ctx context.Context, arg CreateReservationParams) error FetchLiquidityParams(ctx context.Context) ([]byte, error) GetLoopInSwap(ctx context.Context, swapHash []byte) (GetLoopInSwapRow, error) GetLoopInSwaps(ctx context.Context) ([]GetLoopInSwapsRow, error) GetLoopOutSwap(ctx context.Context, swapHash []byte) (GetLoopOutSwapRow, error) GetLoopOutSwaps(ctx context.Context) ([]GetLoopOutSwapsRow, error) + GetReservation(ctx context.Context, reservationID []byte) (Reservation, error) + GetReservationUpdates(ctx context.Context, reservationID []byte) ([]ReservationUpdate, error) + GetReservations(ctx context.Context) ([]Reservation, error) GetSwapUpdates(ctx context.Context, swapHash []byte) ([]SwapUpdate, error) InsertHtlcKeys(ctx context.Context, arg InsertHtlcKeysParams) error InsertLoopIn(ctx context.Context, arg InsertLoopInParams) error InsertLoopOut(ctx context.Context, arg InsertLoopOutParams) error + InsertReservationUpdate(ctx context.Context, arg InsertReservationUpdateParams) error InsertSwap(ctx context.Context, arg InsertSwapParams) error InsertSwapUpdate(ctx context.Context, arg InsertSwapUpdateParams) error + UpdateReservation(ctx context.Context, arg UpdateReservationParams) error UpsertLiquidityParams(ctx context.Context, params []byte) error } diff --git a/loopdb/sqlc/queries/reservations.sql b/loopdb/sqlc/queries/reservations.sql new file mode 100644 index 0000000..c96d664 --- /dev/null +++ b/loopdb/sqlc/queries/reservations.sql @@ -0,0 +1,66 @@ +-- name: CreateReservation :exec +INSERT INTO reservations ( + reservation_id, + client_pubkey, + server_pubkey, + expiry, + value, + client_key_family, + client_key_index, + initiation_height +) VALUES ( + $1, + $2, + $3, + $4, + $5, + $6, + $7, + $8 +); + +-- name: UpdateReservation :exec +UPDATE reservations +SET + tx_hash = $2, + out_index = $3, + confirmation_height = $4 +WHERE + reservations.reservation_id = $1; + +-- name: InsertReservationUpdate :exec +INSERT INTO reservation_updates ( + reservation_id, + update_state, + update_timestamp +) VALUES ( + $1, + $2, + $3 +); + +-- name: GetReservation :one +SELECT + * +FROM + reservations +WHERE + reservation_id = $1; + +-- name: GetReservations :many +SELECT + * +FROM + reservations +ORDER BY + id ASC; + +-- name: GetReservationUpdates :many +SELECT + reservation_updates.* +FROM + reservation_updates +WHERE + reservation_id = $1 +ORDER BY + id ASC; \ No newline at end of file diff --git a/loopdb/sqlc/reservations.sql.go b/loopdb/sqlc/reservations.sql.go new file mode 100644 index 0000000..863371b --- /dev/null +++ b/loopdb/sqlc/reservations.sql.go @@ -0,0 +1,222 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.17.2 +// source: reservations.sql + +package sqlc + +import ( + "context" + "database/sql" + "time" +) + +const createReservation = `-- name: CreateReservation :exec +INSERT INTO reservations ( + reservation_id, + client_pubkey, + server_pubkey, + expiry, + value, + client_key_family, + client_key_index, + initiation_height +) VALUES ( + $1, + $2, + $3, + $4, + $5, + $6, + $7, + $8 +) +` + +type CreateReservationParams struct { + ReservationID []byte + ClientPubkey []byte + ServerPubkey []byte + Expiry int32 + Value int64 + ClientKeyFamily int32 + ClientKeyIndex int32 + InitiationHeight int32 +} + +func (q *Queries) CreateReservation(ctx context.Context, arg CreateReservationParams) error { + _, err := q.db.ExecContext(ctx, createReservation, + arg.ReservationID, + arg.ClientPubkey, + arg.ServerPubkey, + arg.Expiry, + arg.Value, + arg.ClientKeyFamily, + arg.ClientKeyIndex, + arg.InitiationHeight, + ) + return err +} + +const getReservation = `-- name: GetReservation :one +SELECT + id, reservation_id, client_pubkey, server_pubkey, expiry, value, client_key_family, client_key_index, initiation_height, tx_hash, out_index, confirmation_height +FROM + reservations +WHERE + reservation_id = $1 +` + +func (q *Queries) GetReservation(ctx context.Context, reservationID []byte) (Reservation, error) { + row := q.db.QueryRowContext(ctx, getReservation, reservationID) + var i Reservation + err := row.Scan( + &i.ID, + &i.ReservationID, + &i.ClientPubkey, + &i.ServerPubkey, + &i.Expiry, + &i.Value, + &i.ClientKeyFamily, + &i.ClientKeyIndex, + &i.InitiationHeight, + &i.TxHash, + &i.OutIndex, + &i.ConfirmationHeight, + ) + return i, err +} + +const getReservationUpdates = `-- name: GetReservationUpdates :many +SELECT + reservation_updates.id, reservation_updates.reservation_id, reservation_updates.update_state, reservation_updates.update_timestamp +FROM + reservation_updates +WHERE + reservation_id = $1 +ORDER BY + id ASC +` + +func (q *Queries) GetReservationUpdates(ctx context.Context, reservationID []byte) ([]ReservationUpdate, error) { + rows, err := q.db.QueryContext(ctx, getReservationUpdates, reservationID) + if err != nil { + return nil, err + } + defer rows.Close() + var items []ReservationUpdate + for rows.Next() { + var i ReservationUpdate + if err := rows.Scan( + &i.ID, + &i.ReservationID, + &i.UpdateState, + &i.UpdateTimestamp, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + +const getReservations = `-- name: GetReservations :many +SELECT + id, reservation_id, client_pubkey, server_pubkey, expiry, value, client_key_family, client_key_index, initiation_height, tx_hash, out_index, confirmation_height +FROM + reservations +ORDER BY + id ASC +` + +func (q *Queries) GetReservations(ctx context.Context) ([]Reservation, error) { + rows, err := q.db.QueryContext(ctx, getReservations) + if err != nil { + return nil, err + } + defer rows.Close() + var items []Reservation + for rows.Next() { + var i Reservation + if err := rows.Scan( + &i.ID, + &i.ReservationID, + &i.ClientPubkey, + &i.ServerPubkey, + &i.Expiry, + &i.Value, + &i.ClientKeyFamily, + &i.ClientKeyIndex, + &i.InitiationHeight, + &i.TxHash, + &i.OutIndex, + &i.ConfirmationHeight, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + +const insertReservationUpdate = `-- name: InsertReservationUpdate :exec +INSERT INTO reservation_updates ( + reservation_id, + update_state, + update_timestamp +) VALUES ( + $1, + $2, + $3 +) +` + +type InsertReservationUpdateParams struct { + ReservationID []byte + UpdateState string + UpdateTimestamp time.Time +} + +func (q *Queries) InsertReservationUpdate(ctx context.Context, arg InsertReservationUpdateParams) error { + _, err := q.db.ExecContext(ctx, insertReservationUpdate, arg.ReservationID, arg.UpdateState, arg.UpdateTimestamp) + return err +} + +const updateReservation = `-- name: UpdateReservation :exec +UPDATE reservations +SET + tx_hash = $2, + out_index = $3, + confirmation_height = $4 +WHERE + reservations.reservation_id = $1 +` + +type UpdateReservationParams struct { + ReservationID []byte + TxHash []byte + OutIndex sql.NullInt32 + ConfirmationHeight sql.NullInt32 +} + +func (q *Queries) UpdateReservation(ctx context.Context, arg UpdateReservationParams) error { + _, err := q.db.ExecContext(ctx, updateReservation, + arg.ReservationID, + arg.TxHash, + arg.OutIndex, + arg.ConfirmationHeight, + ) + return err +}