diff --git a/tapdb/sqlc/querier.go b/tapdb/sqlc/querier.go index 62c213c34..04daa9a6b 100644 --- a/tapdb/sqlc/querier.go +++ b/tapdb/sqlc/querier.go @@ -131,8 +131,6 @@ type Querier interface { QueryAssets(ctx context.Context, arg QueryAssetsParams) ([]QueryAssetsRow, error) QueryEventIDs(ctx context.Context, arg QueryEventIDsParams) ([]QueryEventIDsRow, error) QueryFederationGlobalSyncConfigs(ctx context.Context) ([]FederationGlobalSyncConfig, error) - // Join on mssmt_nodes to get leaf related fields. - // Join on genesis_info_view to get leaf related fields. QueryFederationProofSyncLog(ctx context.Context, arg QueryFederationProofSyncLogParams) ([]QueryFederationProofSyncLogRow, error) QueryFederationUniSyncConfigs(ctx context.Context) ([]FederationUniSyncConfig, error) QueryMultiverseLeaves(ctx context.Context, arg QueryMultiverseLeavesParams) ([]QueryMultiverseLeavesRow, error) diff --git a/tapdb/sqlc/queries/universe.sql b/tapdb/sqlc/queries/universe.sql index 787162906..1193bac60 100644 --- a/tapdb/sqlc/queries/universe.sql +++ b/tapdb/sqlc/queries/universe.sql @@ -6,10 +6,10 @@ FROM universe_roots JOIN mssmt_roots ON universe_roots.namespace_root = mssmt_roots.namespace JOIN mssmt_nodes - ON mssmt_nodes.hash_key = mssmt_roots.root_hash AND - mssmt_nodes.namespace = mssmt_roots.namespace + ON mssmt_nodes.hash_key = mssmt_roots.root_hash + AND mssmt_nodes.namespace = mssmt_roots.namespace JOIN genesis_assets - ON genesis_assets.asset_id = universe_roots.asset_id + ON genesis_assets.asset_id = universe_roots.asset_id WHERE mssmt_nodes.namespace = @namespace; -- name: UpsertUniverseRoot :one @@ -18,8 +18,6 @@ INSERT INTO universe_roots ( ) VALUES ( @namespace_root, @asset_id, @group_key, @proof_type ) ON CONFLICT (namespace_root) - -- This is a NOP, namespace_root is the unique field that caused the - -- conflict. DO UPDATE SET namespace_root = EXCLUDED.namespace_root RETURNING id; @@ -30,7 +28,7 @@ WITH root_id AS ( WHERE namespace_root = @namespace_root ) DELETE FROM universe_events -WHERE universe_root_id = (SELECT id from root_id); +WHERE universe_root_id = (SELECT id FROM root_id); -- name: DeleteUniverseRoot :exec DELETE FROM universe_roots @@ -54,25 +52,23 @@ DELETE FROM universe_leaves WHERE leaf_node_namespace = @namespace; -- name: QueryUniverseLeaves :many -SELECT leaves.script_key_bytes, gen.gen_asset_id, nodes.value genesis_proof, - nodes.sum sum_amt, gen.asset_id -FROM universe_leaves leaves -JOIN mssmt_nodes nodes - ON leaves.leaf_node_key = nodes.key AND - leaves.leaf_node_namespace = nodes.namespace -JOIN genesis_info_view gen +SELECT leaves.script_key_bytes, gen.gen_asset_id, nodes.value AS genesis_proof, + nodes.sum AS sum_amt, gen.asset_id +FROM universe_leaves AS leaves +JOIN mssmt_nodes AS nodes + ON leaves.leaf_node_key = nodes.key + AND leaves.leaf_node_namespace = nodes.namespace +JOIN genesis_info_view AS gen ON leaves.asset_genesis_id = gen.gen_asset_id WHERE leaves.leaf_node_namespace = @namespace - AND - (leaves.minting_point = sqlc.narg('minting_point_bytes') OR - sqlc.narg('minting_point_bytes') IS NULL) - AND - (leaves.script_key_bytes = sqlc.narg('script_key_bytes') OR - sqlc.narg('script_key_bytes') IS NULL); + AND (leaves.minting_point = sqlc.narg('minting_point_bytes') OR + sqlc.narg('minting_point_bytes') IS NULL) + AND (leaves.script_key_bytes = sqlc.narg('script_key_bytes') OR + sqlc.narg('script_key_bytes') IS NULL); -- name: FetchUniverseKeys :many SELECT leaves.minting_point, leaves.script_key_bytes -FROM universe_leaves leaves +FROM universe_leaves AS leaves WHERE leaves.leaf_node_namespace = @namespace ORDER BY CASE WHEN sqlc.narg('sort_direction') = 0 THEN leaves.id END ASC, @@ -84,14 +80,14 @@ SELECT * FROM universe_leaves; -- name: UniverseRoots :many SELECT universe_roots.asset_id, group_key, proof_type, - mssmt_roots.root_hash root_hash, mssmt_nodes.sum root_sum, - genesis_assets.asset_tag asset_name + mssmt_roots.root_hash AS root_hash, mssmt_nodes.sum AS root_sum, + genesis_assets.asset_tag AS asset_name FROM universe_roots JOIN mssmt_roots ON universe_roots.namespace_root = mssmt_roots.namespace JOIN mssmt_nodes - ON mssmt_nodes.hash_key = mssmt_roots.root_hash AND - mssmt_nodes.namespace = mssmt_roots.namespace + ON mssmt_nodes.hash_key = mssmt_roots.root_hash + AND mssmt_nodes.namespace = mssmt_roots.namespace JOIN genesis_assets ON genesis_assets.asset_id = universe_roots.asset_id ORDER BY @@ -329,8 +325,8 @@ SELECT SUM(CASE WHEN event_type = 'SYNC' THEN 1 ELSE 0 END) AS sync_events, SUM(CASE WHEN event_type = 'NEW_PROOF' THEN 1 ELSE 0 END) AS new_proof_events FROM universe_events -WHERE event_type IN ('SYNC', 'NEW_PROOF') AND - event_timestamp >= @start_time AND event_timestamp <= @end_time +WHERE event_type IN ('SYNC', 'NEW_PROOF') + AND event_timestamp BETWEEN @start_time AND @end_time GROUP BY day ORDER BY day; @@ -367,13 +363,12 @@ FROM federation_uni_sync_config ORDER BY group_key NULLS LAST, asset_id NULLS LAST, proof_type; -- name: UpsertFederationProofSyncLog :one -INSERT INTO federation_proof_sync_log as log ( +INSERT INTO federation_proof_sync_log AS log ( status, timestamp, sync_direction, proof_leaf_id, universe_root_id, servers_id ) VALUES ( @status, @timestamp, @sync_direction, ( - -- Select the leaf id from the universe_leaves table. SELECT id FROM universe_leaves WHERE leaf_node_namespace = @leaf_namespace @@ -382,14 +377,12 @@ INSERT INTO federation_proof_sync_log as log ( LIMIT 1 ), ( - -- Select the universe root id from the universe_roots table. SELECT id FROM universe_roots WHERE namespace_root = @universe_id_namespace LIMIT 1 ), ( - -- Select the server id from the universe_servers table. SELECT id FROM universe_servers WHERE server_host = @server_host @@ -399,9 +392,8 @@ INSERT INTO federation_proof_sync_log as log ( DO UPDATE SET status = EXCLUDED.status, timestamp = EXCLUDED.timestamp, - -- Increment the attempt counter. attempt_counter = CASE - WHEN @bump_sync_attempt_counter = true THEN log.attempt_counter + 1 + WHEN @bump_sync_attempt_counter THEN log.attempt_counter + 1 ELSE log.attempt_counter END RETURNING id; @@ -409,58 +401,33 @@ RETURNING id; -- name: QueryFederationProofSyncLog :many SELECT log.id, status, timestamp, sync_direction, attempt_counter, - - -- Select fields from the universe_servers table. - server.id as server_id, + server.id AS server_id, server.server_host, - - -- Select universe leaf related fields. - leaf.minting_point as leaf_minting_point_bytes, - leaf.script_key_bytes as leaf_script_key_bytes, - mssmt_node.value as leaf_genesis_proof, - genesis.gen_asset_id as leaf_gen_asset_id, - genesis.asset_id as leaf_asset_id, - - -- Select fields from the universe_roots table. - root.asset_id as uni_asset_id, - root.group_key as uni_group_key, - root.proof_type as uni_proof_type - -FROM federation_proof_sync_log as log - -JOIN universe_leaves as leaf + leaf.minting_point AS leaf_minting_point_bytes, + leaf.script_key_bytes AS leaf_script_key_bytes, + mssmt_node.value AS leaf_genesis_proof, + genesis.gen_asset_id AS leaf_gen_asset_id, + genesis.asset_id AS leaf_asset_id, + root.asset_id AS uni_asset_id, + root.group_key AS uni_group_key, + root.proof_type AS uni_proof_type +FROM federation_proof_sync_log AS log +JOIN universe_leaves AS leaf ON leaf.id = log.proof_leaf_id - --- Join on mssmt_nodes to get leaf related fields. -JOIN mssmt_nodes mssmt_node - ON leaf.leaf_node_key = mssmt_node.key AND - leaf.leaf_node_namespace = mssmt_node.namespace - --- Join on genesis_info_view to get leaf related fields. -JOIN genesis_info_view genesis +JOIN mssmt_nodes AS mssmt_node + ON leaf.leaf_node_key = mssmt_node.key + AND leaf.leaf_node_namespace = mssmt_node.namespace +JOIN genesis_info_view AS genesis ON leaf.asset_genesis_id = genesis.gen_asset_id - -JOIN universe_servers as server +JOIN universe_servers AS server ON server.id = log.servers_id - -JOIN universe_roots as root +JOIN universe_roots AS root ON root.id = log.universe_root_id - -WHERE (log.sync_direction = sqlc.narg('sync_direction') - OR sqlc.narg('sync_direction') IS NULL) - AND - (log.status = sqlc.narg('status') OR sqlc.narg('status') IS NULL) - AND - - -- Universe leaves WHERE clauses. - (leaf.leaf_node_namespace = sqlc.narg('leaf_namespace') - OR sqlc.narg('leaf_namespace') IS NULL) - AND - (leaf.minting_point = sqlc.narg('leaf_minting_point_bytes') - OR sqlc.narg('leaf_minting_point_bytes') IS NULL) - AND - (leaf.script_key_bytes = sqlc.narg('leaf_script_key_bytes') - OR sqlc.narg('leaf_script_key_bytes') IS NULL); +WHERE (log.sync_direction = sqlc.narg('sync_direction') OR sqlc.narg('sync_direction') IS NULL) + AND (log.status = sqlc.narg('status') OR sqlc.narg('status') IS NULL) + AND (leaf.leaf_node_namespace = sqlc.narg('leaf_namespace') OR sqlc.narg('leaf_namespace') IS NULL) + AND (leaf.minting_point = sqlc.narg('leaf_minting_point_bytes') OR sqlc.narg('leaf_minting_point_bytes') IS NULL) + AND (leaf.script_key_bytes = sqlc.narg('leaf_script_key_bytes') OR sqlc.narg('leaf_script_key_bytes') IS NULL); -- name: DeleteFederationProofSyncLog :exec WITH selected_server_id AS ( diff --git a/tapdb/sqlc/universe.sql.go b/tapdb/sqlc/universe.sql.go index 9049862b7..8bdb6306e 100644 --- a/tapdb/sqlc/universe.sql.go +++ b/tapdb/sqlc/universe.sql.go @@ -71,7 +71,7 @@ WITH root_id AS ( WHERE namespace_root = $1 ) DELETE FROM universe_events -WHERE universe_root_id = (SELECT id from root_id) +WHERE universe_root_id = (SELECT id FROM root_id) ` func (q *Queries) DeleteUniverseEvents(ctx context.Context, namespaceRoot string) error { @@ -140,7 +140,7 @@ func (q *Queries) FetchMultiverseRoot(ctx context.Context, namespaceRoot string) const fetchUniverseKeys = `-- name: FetchUniverseKeys :many SELECT leaves.minting_point, leaves.script_key_bytes -FROM universe_leaves leaves +FROM universe_leaves AS leaves WHERE leaves.leaf_node_namespace = $1 ORDER BY CASE WHEN $2 = 0 THEN leaves.id END ASC, @@ -196,10 +196,10 @@ FROM universe_roots JOIN mssmt_roots ON universe_roots.namespace_root = mssmt_roots.namespace JOIN mssmt_nodes - ON mssmt_nodes.hash_key = mssmt_roots.root_hash AND - mssmt_nodes.namespace = mssmt_roots.namespace + ON mssmt_nodes.hash_key = mssmt_roots.root_hash + AND mssmt_nodes.namespace = mssmt_roots.namespace JOIN genesis_assets - ON genesis_assets.asset_id = universe_roots.asset_id + ON genesis_assets.asset_id = universe_roots.asset_id WHERE mssmt_nodes.namespace = $1 ` @@ -364,8 +364,8 @@ SELECT SUM(CASE WHEN event_type = 'SYNC' THEN 1 ELSE 0 END) AS sync_events, SUM(CASE WHEN event_type = 'NEW_PROOF' THEN 1 ELSE 0 END) AS new_proof_events FROM universe_events -WHERE event_type IN ('SYNC', 'NEW_PROOF') AND - event_timestamp >= $1 AND event_timestamp <= $2 +WHERE event_type IN ('SYNC', 'NEW_PROOF') + AND event_timestamp BETWEEN $1 AND $2 GROUP BY day ORDER BY day ` @@ -482,56 +482,33 @@ func (q *Queries) QueryFederationGlobalSyncConfigs(ctx context.Context) ([]Feder const queryFederationProofSyncLog = `-- name: QueryFederationProofSyncLog :many SELECT log.id, status, timestamp, sync_direction, attempt_counter, - - -- Select fields from the universe_servers table. - server.id as server_id, + server.id AS server_id, server.server_host, - - -- Select universe leaf related fields. - leaf.minting_point as leaf_minting_point_bytes, - leaf.script_key_bytes as leaf_script_key_bytes, - mssmt_node.value as leaf_genesis_proof, - genesis.gen_asset_id as leaf_gen_asset_id, - genesis.asset_id as leaf_asset_id, - - -- Select fields from the universe_roots table. - root.asset_id as uni_asset_id, - root.group_key as uni_group_key, - root.proof_type as uni_proof_type - -FROM federation_proof_sync_log as log - -JOIN universe_leaves as leaf + leaf.minting_point AS leaf_minting_point_bytes, + leaf.script_key_bytes AS leaf_script_key_bytes, + mssmt_node.value AS leaf_genesis_proof, + genesis.gen_asset_id AS leaf_gen_asset_id, + genesis.asset_id AS leaf_asset_id, + root.asset_id AS uni_asset_id, + root.group_key AS uni_group_key, + root.proof_type AS uni_proof_type +FROM federation_proof_sync_log AS log +JOIN universe_leaves AS leaf ON leaf.id = log.proof_leaf_id - -JOIN mssmt_nodes mssmt_node - ON leaf.leaf_node_key = mssmt_node.key AND - leaf.leaf_node_namespace = mssmt_node.namespace - -JOIN genesis_info_view genesis +JOIN mssmt_nodes AS mssmt_node + ON leaf.leaf_node_key = mssmt_node.key + AND leaf.leaf_node_namespace = mssmt_node.namespace +JOIN genesis_info_view AS genesis ON leaf.asset_genesis_id = genesis.gen_asset_id - -JOIN universe_servers as server +JOIN universe_servers AS server ON server.id = log.servers_id - -JOIN universe_roots as root +JOIN universe_roots AS root ON root.id = log.universe_root_id - -WHERE (log.sync_direction = $1 - OR $1 IS NULL) - AND - (log.status = $2 OR $2 IS NULL) - AND - - -- Universe leaves WHERE clauses. - (leaf.leaf_node_namespace = $3 - OR $3 IS NULL) - AND - (leaf.minting_point = $4 - OR $4 IS NULL) - AND - (leaf.script_key_bytes = $5 - OR $5 IS NULL) +WHERE (log.sync_direction = $1 OR $1 IS NULL) + AND (log.status = $2 OR $2 IS NULL) + AND (leaf.leaf_node_namespace = $3 OR $3 IS NULL) + AND (leaf.minting_point = $4 OR $4 IS NULL) + AND (leaf.script_key_bytes = $5 OR $5 IS NULL) ` type QueryFederationProofSyncLogParams struct { @@ -560,8 +537,6 @@ type QueryFederationProofSyncLogRow struct { UniProofType string } -// Join on mssmt_nodes to get leaf related fields. -// Join on genesis_info_view to get leaf related fields. func (q *Queries) QueryFederationProofSyncLog(ctx context.Context, arg QueryFederationProofSyncLogParams) ([]QueryFederationProofSyncLogRow, error) { rows, err := q.db.QueryContext(ctx, queryFederationProofSyncLog, arg.SyncDirection, @@ -865,21 +840,19 @@ func (q *Queries) QueryUniverseAssetStats(ctx context.Context, arg QueryUniverse } const queryUniverseLeaves = `-- name: QueryUniverseLeaves :many -SELECT leaves.script_key_bytes, gen.gen_asset_id, nodes.value genesis_proof, - nodes.sum sum_amt, gen.asset_id -FROM universe_leaves leaves -JOIN mssmt_nodes nodes - ON leaves.leaf_node_key = nodes.key AND - leaves.leaf_node_namespace = nodes.namespace -JOIN genesis_info_view gen +SELECT leaves.script_key_bytes, gen.gen_asset_id, nodes.value AS genesis_proof, + nodes.sum AS sum_amt, gen.asset_id +FROM universe_leaves AS leaves +JOIN mssmt_nodes AS nodes + ON leaves.leaf_node_key = nodes.key + AND leaves.leaf_node_namespace = nodes.namespace +JOIN genesis_info_view AS gen ON leaves.asset_genesis_id = gen.gen_asset_id WHERE leaves.leaf_node_namespace = $1 - AND - (leaves.minting_point = $2 OR - $2 IS NULL) - AND - (leaves.script_key_bytes = $3 OR - $3 IS NULL) + AND (leaves.minting_point = $2 OR + $2 IS NULL) + AND (leaves.script_key_bytes = $3 OR + $3 IS NULL) ` type QueryUniverseLeavesParams struct { @@ -1058,14 +1031,14 @@ func (q *Queries) UniverseLeaves(ctx context.Context) ([]UniverseLeafe, error) { const universeRoots = `-- name: UniverseRoots :many SELECT universe_roots.asset_id, group_key, proof_type, - mssmt_roots.root_hash root_hash, mssmt_nodes.sum root_sum, - genesis_assets.asset_tag asset_name + mssmt_roots.root_hash AS root_hash, mssmt_nodes.sum AS root_sum, + genesis_assets.asset_tag AS asset_name FROM universe_roots JOIN mssmt_roots ON universe_roots.namespace_root = mssmt_roots.namespace JOIN mssmt_nodes - ON mssmt_nodes.hash_key = mssmt_roots.root_hash AND - mssmt_nodes.namespace = mssmt_roots.namespace + ON mssmt_nodes.hash_key = mssmt_roots.root_hash + AND mssmt_nodes.namespace = mssmt_roots.namespace JOIN genesis_assets ON genesis_assets.asset_id = universe_roots.asset_id ORDER BY @@ -1142,13 +1115,12 @@ func (q *Queries) UpsertFederationGlobalSyncConfig(ctx context.Context, arg Upse } const upsertFederationProofSyncLog = `-- name: UpsertFederationProofSyncLog :one -INSERT INTO federation_proof_sync_log as log ( +INSERT INTO federation_proof_sync_log AS log ( status, timestamp, sync_direction, proof_leaf_id, universe_root_id, servers_id ) VALUES ( $1, $2, $3, ( - -- Select the leaf id from the universe_leaves table. SELECT id FROM universe_leaves WHERE leaf_node_namespace = $4 @@ -1157,14 +1129,12 @@ INSERT INTO federation_proof_sync_log as log ( LIMIT 1 ), ( - -- Select the universe root id from the universe_roots table. SELECT id FROM universe_roots WHERE namespace_root = $7 LIMIT 1 ), ( - -- Select the server id from the universe_servers table. SELECT id FROM universe_servers WHERE server_host = $8 @@ -1174,9 +1144,8 @@ INSERT INTO federation_proof_sync_log as log ( DO UPDATE SET status = EXCLUDED.status, timestamp = EXCLUDED.timestamp, - -- Increment the attempt counter. attempt_counter = CASE - WHEN $9 = true THEN log.attempt_counter + 1 + WHEN $9 THEN log.attempt_counter + 1 ELSE log.attempt_counter END RETURNING id @@ -1191,7 +1160,7 @@ type UpsertFederationProofSyncLogParams struct { LeafScriptKeyBytes []byte UniverseIDNamespace string ServerHost string - BumpSyncAttemptCounter interface{} + BumpSyncAttemptCounter int64 } func (q *Queries) UpsertFederationProofSyncLog(ctx context.Context, arg UpsertFederationProofSyncLogParams) (int64, error) { @@ -1342,8 +1311,6 @@ INSERT INTO universe_roots ( ) VALUES ( $1, $2, $3, $4 ) ON CONFLICT (namespace_root) - -- This is a NOP, namespace_root is the unique field that caused the - -- conflict. DO UPDATE SET namespace_root = EXCLUDED.namespace_root RETURNING id ` diff --git a/tapdb/universe_federation.go b/tapdb/universe_federation.go index 5529d6272..69a3685b3 100644 --- a/tapdb/universe_federation.go +++ b/tapdb/universe_federation.go @@ -41,7 +41,7 @@ type ( // NewUniverseServer is used to create a new universe server. NewUniverseServer = sqlc.InsertUniverseServerParams - // DelUniverseServer is used to delete a universe server. + // DelUniverseServer is used to delete a universe server from the store. DelUniverseServer = sqlc.DeleteUniverseServerParams // UpsertFedGlobalSyncConfigParams is used to set the global federation @@ -345,7 +345,7 @@ func (u *UniverseFederationDB) UpsertFederationProofSyncLog( LeafMintingPointBytes: leafKeyOutpointBytes, LeafScriptKeyBytes: scriptKeyPubKeyBytes, ServerHost: addr.HostStr(), - BumpSyncAttemptCounter: bumpSyncAttemptCounter, + BumpSyncAttemptCounter: boolToInt64(bumpSyncAttemptCounter), } logID, err = db.UpsertFederationProofSyncLog(ctx, params) if err != nil { @@ -831,3 +831,10 @@ var ( _ universe.FederationLog = (*UniverseFederationDB)(nil) _ universe.FederationSyncConfigDB = (*UniverseFederationDB)(nil) ) + +func boolToInt64(b bool) int64 { + if b { + return 1 + } + return 0 +}