From 4073addede2a9eed8110a4461882436167dbe574 Mon Sep 17 00:00:00 2001 From: Christopher Tarry Date: Wed, 20 Nov 2024 18:11:34 -0500 Subject: [PATCH] fix last announcement query and add regression test --- persist/sqlite/consensus.go | 2 +- persist/sqlite/scan_test.go | 115 ++++++++++++++++++++++++------------ 2 files changed, 79 insertions(+), 38 deletions(-) diff --git a/persist/sqlite/consensus.go b/persist/sqlite/consensus.go index b4f3987f..a57ffdef 100644 --- a/persist/sqlite/consensus.go +++ b/persist/sqlite/consensus.go @@ -1122,7 +1122,7 @@ func (ut *updateTx) RevertIndex(state explorer.UpdateState) error { } func addHosts(tx *txn, scans []explorer.Host) error { - stmt, err := tx.Prepare(`INSERT INTO host_info(public_key, net_address, country_code, known_since, last_scan, last_scan_successful, last_announcement, total_scans, successful_interactions, failed_interactions, settings_accepting_contracts, settings_max_download_batch_size, settings_max_duration, settings_max_revise_batch_size, settings_net_address, settings_remaining_storage, settings_sector_size, settings_total_storage, settings_address, settings_window_size, settings_collateral, settings_max_collateral, settings_base_rpc_price, settings_contract_price, settings_download_bandwidth_price, settings_sector_access_price, settings_storage_price, settings_upload_bandwidth_price, settings_ephemeral_account_expiry, settings_max_ephemeral_account_balance, settings_revision_number, settings_version, settings_release, settings_sia_mux_port, price_table_uid, price_table_validity, price_table_host_block_height, price_table_update_price_table_cost, price_table_account_balance_cost, price_table_fund_account_cost, price_table_latest_revision_cost, price_table_subscription_memory_cost, price_table_subscription_notification_cost, price_table_init_base_cost, price_table_memory_time_cost, price_table_download_bandwidth_cost, price_table_upload_bandwidth_cost, price_table_drop_sectors_base_cost, price_table_drop_sectors_unit_cost, price_table_has_sector_base_cost, price_table_read_base_cost, price_table_read_length_cost, price_table_renew_contract_cost, price_table_revision_base_cost, price_table_swap_sector_base_cost, price_table_write_base_cost, price_table_write_length_cost, price_table_write_store_cost, price_table_txn_fee_min_recommended, price_table_txn_fee_max_recommended, price_table_contract_price, price_table_collateral_cost, price_table_max_collateral, price_table_max_duration, price_table_window_size, price_table_registry_entries_left, price_table_registry_entries_total, rhp4_settings_protocol_version, rhp4_settings_release, rhp4_settings_wallet_address, rhp4_settings_accepting_contracts, rhp4_settings_max_collateral, rhp4_settings_max_collateral_duration, rhp4_settings_max_sector_duration, rhp4_settings_max_sector_batch_size, rhp4_settings_remaining_storage, rhp4_settings_total_storage, rhp4_prices_contract_price, rhp4_prices_collateral_price, rhp4_prices_storage_price, rhp4_prices_ingress_price, rhp4_prices_egress_price, rhp4_prices_free_sector_price, rhp4_prices_tip_height, rhp4_prices_valid_until, rhp4_prices_signature) VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18,$19,$20,$21,$22,$23,$24,$25,$26,$27,$28,$29,$30,$31,$32,$33,$34,$35,$36,$37,$38,$39,$40,$41,$42,$43,$44,$45,$46,$47,$48,$49,$50,$51,$52,$53,$54,$55,$56,$57,$58,$59,$60,$61,$62,$63,$64,$65,$66,$67,$68,$69,$70,$71,$72,$73,$74,$75,$76,$77,$78,$79,$80,$81,$82,$83,$84,$85,$86) ON CONFLICT (public_key) DO UPDATE SET net_address = $2, country_code = $3, last_scan = $5, last_scan_successful = $6, last_announcement = CASE WHEN $7 > 0 THEN last_announcement ELSE $7 END, total_scans = $8, successful_interactions = $9, failed_interactions = failed_interactions + $10, settings_accepting_contracts = $11, settings_max_download_batch_size = $12, settings_max_duration = $13, settings_max_revise_batch_size = $14 , settings_net_address = $15, settings_remaining_storage = $16, settings_sector_size = $17, settings_total_storage = $18, settings_address = $19, settings_window_size = $20, settings_collateral = $21, settings_max_collateral = $22, settings_base_rpc_price = $23, settings_contract_price = $24, settings_download_bandwidth_price = $25, settings_sector_access_price = $26, settings_storage_price = $27, settings_upload_bandwidth_price = $28, settings_ephemeral_account_expiry = $29, settings_max_ephemeral_account_balance = $30, settings_revision_number = $31, settings_version = $32, settings_release = $33, settings_sia_mux_port = $34, price_table_uid = $35, price_table_validity = $36, price_table_host_block_height = $37, price_table_update_price_table_cost = $38, price_table_account_balance_cost = $39, price_table_fund_account_cost = $40, price_table_latest_revision_cost = $41, price_table_subscription_memory_cost = $42, price_table_subscription_notification_cost = $43, price_table_init_base_cost = $44, price_table_memory_time_cost = $45, price_table_download_bandwidth_cost = $46, price_table_upload_bandwidth_cost = $47, price_table_drop_sectors_base_cost = $48, price_table_drop_sectors_unit_cost = $49, price_table_has_sector_base_cost = $50, price_table_read_base_cost = $51, price_table_read_length_cost = $52, price_table_renew_contract_cost = $53, price_table_revision_base_cost = $54, price_table_swap_sector_base_cost = $55, price_table_write_base_cost = $56, price_table_write_length_cost = $57, price_table_write_store_cost = $58, price_table_txn_fee_min_recommended = $59, price_table_txn_fee_max_recommended = $60, price_table_contract_price = $61, price_table_collateral_cost = $62, price_table_max_collateral = $63, price_table_max_duration = $64, price_table_window_size = $65, price_table_registry_entries_left = $66, price_table_registry_entries_total = $67, rhp4_settings_protocol_version = $68, rhp4_settings_release = $69, rhp4_settings_wallet_address = $70, rhp4_settings_accepting_contracts = $71, rhp4_settings_max_collateral = $72, rhp4_settings_max_collateral_duration = $73, rhp4_settings_max_sector_duration = $74, rhp4_settings_max_sector_batch_size = $75, rhp4_settings_remaining_storage = $76, rhp4_settings_total_storage = $77, rhp4_prices_contract_price = $78, rhp4_prices_collateral_price = $79, rhp4_prices_storage_price = $80, rhp4_prices_ingress_price = $81, rhp4_prices_egress_price = $82, rhp4_prices_free_sector_price = $83, rhp4_prices_tip_height = $84, rhp4_prices_valid_until = $85, rhp4_prices_signature = $86`) + stmt, err := tx.Prepare(`INSERT INTO host_info(public_key, net_address, country_code, known_since, last_scan, last_scan_successful, last_announcement, total_scans, successful_interactions, failed_interactions, settings_accepting_contracts, settings_max_download_batch_size, settings_max_duration, settings_max_revise_batch_size, settings_net_address, settings_remaining_storage, settings_sector_size, settings_total_storage, settings_address, settings_window_size, settings_collateral, settings_max_collateral, settings_base_rpc_price, settings_contract_price, settings_download_bandwidth_price, settings_sector_access_price, settings_storage_price, settings_upload_bandwidth_price, settings_ephemeral_account_expiry, settings_max_ephemeral_account_balance, settings_revision_number, settings_version, settings_release, settings_sia_mux_port, price_table_uid, price_table_validity, price_table_host_block_height, price_table_update_price_table_cost, price_table_account_balance_cost, price_table_fund_account_cost, price_table_latest_revision_cost, price_table_subscription_memory_cost, price_table_subscription_notification_cost, price_table_init_base_cost, price_table_memory_time_cost, price_table_download_bandwidth_cost, price_table_upload_bandwidth_cost, price_table_drop_sectors_base_cost, price_table_drop_sectors_unit_cost, price_table_has_sector_base_cost, price_table_read_base_cost, price_table_read_length_cost, price_table_renew_contract_cost, price_table_revision_base_cost, price_table_swap_sector_base_cost, price_table_write_base_cost, price_table_write_length_cost, price_table_write_store_cost, price_table_txn_fee_min_recommended, price_table_txn_fee_max_recommended, price_table_contract_price, price_table_collateral_cost, price_table_max_collateral, price_table_max_duration, price_table_window_size, price_table_registry_entries_left, price_table_registry_entries_total, rhp4_settings_protocol_version, rhp4_settings_release, rhp4_settings_wallet_address, rhp4_settings_accepting_contracts, rhp4_settings_max_collateral, rhp4_settings_max_collateral_duration, rhp4_settings_max_sector_duration, rhp4_settings_max_sector_batch_size, rhp4_settings_remaining_storage, rhp4_settings_total_storage, rhp4_prices_contract_price, rhp4_prices_collateral_price, rhp4_prices_storage_price, rhp4_prices_ingress_price, rhp4_prices_egress_price, rhp4_prices_free_sector_price, rhp4_prices_tip_height, rhp4_prices_valid_until, rhp4_prices_signature) VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18,$19,$20,$21,$22,$23,$24,$25,$26,$27,$28,$29,$30,$31,$32,$33,$34,$35,$36,$37,$38,$39,$40,$41,$42,$43,$44,$45,$46,$47,$48,$49,$50,$51,$52,$53,$54,$55,$56,$57,$58,$59,$60,$61,$62,$63,$64,$65,$66,$67,$68,$69,$70,$71,$72,$73,$74,$75,$76,$77,$78,$79,$80,$81,$82,$83,$84,$85,$86) ON CONFLICT (public_key) DO UPDATE SET net_address = $2, country_code = $3, last_scan = $5, last_scan_successful = $6, last_announcement = CASE WHEN $7 > 0 THEN $7 ELSE last_announcement END, total_scans = $8, successful_interactions = $9, failed_interactions = failed_interactions + $10, settings_accepting_contracts = $11, settings_max_download_batch_size = $12, settings_max_duration = $13, settings_max_revise_batch_size = $14 , settings_net_address = $15, settings_remaining_storage = $16, settings_sector_size = $17, settings_total_storage = $18, settings_address = $19, settings_window_size = $20, settings_collateral = $21, settings_max_collateral = $22, settings_base_rpc_price = $23, settings_contract_price = $24, settings_download_bandwidth_price = $25, settings_sector_access_price = $26, settings_storage_price = $27, settings_upload_bandwidth_price = $28, settings_ephemeral_account_expiry = $29, settings_max_ephemeral_account_balance = $30, settings_revision_number = $31, settings_version = $32, settings_release = $33, settings_sia_mux_port = $34, price_table_uid = $35, price_table_validity = $36, price_table_host_block_height = $37, price_table_update_price_table_cost = $38, price_table_account_balance_cost = $39, price_table_fund_account_cost = $40, price_table_latest_revision_cost = $41, price_table_subscription_memory_cost = $42, price_table_subscription_notification_cost = $43, price_table_init_base_cost = $44, price_table_memory_time_cost = $45, price_table_download_bandwidth_cost = $46, price_table_upload_bandwidth_cost = $47, price_table_drop_sectors_base_cost = $48, price_table_drop_sectors_unit_cost = $49, price_table_has_sector_base_cost = $50, price_table_read_base_cost = $51, price_table_read_length_cost = $52, price_table_renew_contract_cost = $53, price_table_revision_base_cost = $54, price_table_swap_sector_base_cost = $55, price_table_write_base_cost = $56, price_table_write_length_cost = $57, price_table_write_store_cost = $58, price_table_txn_fee_min_recommended = $59, price_table_txn_fee_max_recommended = $60, price_table_contract_price = $61, price_table_collateral_cost = $62, price_table_max_collateral = $63, price_table_max_duration = $64, price_table_window_size = $65, price_table_registry_entries_left = $66, price_table_registry_entries_total = $67, rhp4_settings_protocol_version = $68, rhp4_settings_release = $69, rhp4_settings_wallet_address = $70, rhp4_settings_accepting_contracts = $71, rhp4_settings_max_collateral = $72, rhp4_settings_max_collateral_duration = $73, rhp4_settings_max_sector_duration = $74, rhp4_settings_max_sector_batch_size = $75, rhp4_settings_remaining_storage = $76, rhp4_settings_total_storage = $77, rhp4_prices_contract_price = $78, rhp4_prices_collateral_price = $79, rhp4_prices_storage_price = $80, rhp4_prices_ingress_price = $81, rhp4_prices_egress_price = $82, rhp4_prices_free_sector_price = $83, rhp4_prices_tip_height = $84, rhp4_prices_valid_until = $85, rhp4_prices_signature = $86`) if err != nil { return fmt.Errorf("failed to prepare host_info stmt: %w", err) } diff --git a/persist/sqlite/scan_test.go b/persist/sqlite/scan_test.go index 36e02f7d..67879783 100644 --- a/persist/sqlite/scan_test.go +++ b/persist/sqlite/scan_test.go @@ -217,47 +217,88 @@ func TestScan(t *testing.T) { time.Sleep(4 * cfg.Timeout) - dbHosts, err := e.Hosts([]types.PublicKey{pubkey0, pubkey1, pubkey2}) - if err != nil { - t.Fatal(err) + { + dbHosts, err := e.Hosts([]types.PublicKey{pubkey0, pubkey1, pubkey2}) + if err != nil { + t.Fatal(err) + } + testutil.Equal(t, "len(dbHosts)", 3, len(dbHosts)) + + sort.Slice(hosts, func(i, j int) bool { + return hosts[i].NetAddress < hosts[j].NetAddress + }) + sort.Slice(dbHosts, func(i, j int) bool { + return dbHosts[i].NetAddress < dbHosts[j].NetAddress + }) + + host0 := dbHosts[0] + testutil.Equal(t, "host0.NetAddress", hosts[0].NetAddress, host0.NetAddress) + testutil.Equal(t, "host0.PublicKey", hosts[0].PublicKey, host0.PublicKey) + testutil.Equal(t, "host0.TotalScans", 1, host0.TotalScans) + testutil.Equal(t, "host0.SuccessfulInteractions", 1, host0.SuccessfulInteractions) + testutil.Equal(t, "host0.FailedInteractions", 0, host0.FailedInteractions) + testutil.Equal(t, "host0.LastScanSuccessful", true, host0.LastScanSuccessful) + testutil.Equal(t, "host0.LastAnnouncement", ts, host0.LastAnnouncement) + if !host0.RHPV4Settings.AcceptingContracts { + log.Fatal("AcceptingContracts = false on host that's supposed to be active") + } + + host1 := dbHosts[1] + testutil.Equal(t, "host1.NetAddress", hosts[1].NetAddress, host1.NetAddress) + testutil.Equal(t, "host1.PublicKey", hosts[1].PublicKey, host1.PublicKey) + testutil.Equal(t, "host1.TotalScans", 1, host1.TotalScans) + testutil.Equal(t, "host1.SuccessfulInteractions", 0, host1.SuccessfulInteractions) + testutil.Equal(t, "host1.FailedInteractions", 1, host1.FailedInteractions) + testutil.Equal(t, "host1.LastScanSuccessful", false, host1.LastScanSuccessful) + testutil.Equal(t, "host1.LastAnnouncement", ts, host1.LastAnnouncement) + + host2 := dbHosts[2] + testutil.Equal(t, "host2.NetAddress", hosts[2].NetAddress, host2.NetAddress) + testutil.Equal(t, "host2.PublicKey", hosts[2].PublicKey, host2.PublicKey) + testutil.Equal(t, "host2.CountryCode", "CA", host2.CountryCode) + testutil.Equal(t, "host2.TotalScans", 1, host2.TotalScans) + testutil.Equal(t, "host2.SuccessfulInteractions", 1, host2.SuccessfulInteractions) + testutil.Equal(t, "host2.FailedInteractions", 0, host2.FailedInteractions) + testutil.Equal(t, "host2.LastScanSuccessful", true, host2.LastScanSuccessful) + testutil.Equal(t, "host2.LastAnnouncement", ts, host2.LastAnnouncement) + if host2.Settings.SectorSize <= 0 { + log.Fatal("SectorSize = 0 on host that's supposed to be active") + } } - testutil.Equal(t, "len(dbHosts)", 3, len(dbHosts)) - sort.Slice(hosts, func(i, j int) bool { - return hosts[i].NetAddress < hosts[j].NetAddress - }) - sort.Slice(dbHosts, func(i, j int) bool { - return dbHosts[i].NetAddress < dbHosts[j].NetAddress - }) + ts = types.CurrentTimestamp() + for i := range hosts { + hosts[i].LastAnnouncement = ts + } - host0 := dbHosts[0] - testutil.Equal(t, "host0.NetAddress", hosts[0].NetAddress, host0.NetAddress) - testutil.Equal(t, "host0.PublicKey", hosts[0].PublicKey, host0.PublicKey) - testutil.Equal(t, "host0.TotalScans", 1, host0.TotalScans) - testutil.Equal(t, "host0.SuccessfulInteractions", 1, host0.SuccessfulInteractions) - testutil.Equal(t, "host0.FailedInteractions", 0, host0.FailedInteractions) - testutil.Equal(t, "host0.LastScanSuccessful", true, host0.LastScanSuccessful) - if !host0.RHPV4Settings.AcceptingContracts { - log.Fatal("AcceptingContracts = false on host that's supposed to be active") + if err := db.transaction(func(tx *txn) error { + return addHosts(tx, hosts) + }); err != nil { + t.Fatal(err) } - host1 := dbHosts[1] - testutil.Equal(t, "host1.NetAddress", hosts[1].NetAddress, host1.NetAddress) - testutil.Equal(t, "host1.PublicKey", hosts[1].PublicKey, host1.PublicKey) - testutil.Equal(t, "host1.TotalScans", 1, host1.TotalScans) - testutil.Equal(t, "host1.SuccessfulInteractions", 0, host1.SuccessfulInteractions) - testutil.Equal(t, "host1.FailedInteractions", 1, host1.FailedInteractions) - testutil.Equal(t, "host1.LastScanSuccessful", false, host1.LastScanSuccessful) - - host2 := dbHosts[2] - testutil.Equal(t, "host2.NetAddress", hosts[2].NetAddress, host2.NetAddress) - testutil.Equal(t, "host2.PublicKey", hosts[2].PublicKey, host2.PublicKey) - testutil.Equal(t, "host2.CountryCode", "CA", host2.CountryCode) - testutil.Equal(t, "host2.TotalScans", 1, host2.TotalScans) - testutil.Equal(t, "host2.SuccessfulInteractions", 1, host2.SuccessfulInteractions) - testutil.Equal(t, "host2.FailedInteractions", 0, host2.FailedInteractions) - testutil.Equal(t, "host2.LastScanSuccessful", true, host2.LastScanSuccessful) - if host2.Settings.SectorSize <= 0 { - log.Fatal("SectorSize = 0 on host that's supposed to be active") + { + dbHosts, err := e.Hosts([]types.PublicKey{pubkey0, pubkey1, pubkey2}) + if err != nil { + t.Fatal(err) + } + testutil.Equal(t, "len(dbHosts)", 3, len(dbHosts)) + + sort.Slice(hosts, func(i, j int) bool { + return hosts[i].NetAddress < hosts[j].NetAddress + }) + sort.Slice(dbHosts, func(i, j int) bool { + return dbHosts[i].NetAddress < dbHosts[j].NetAddress + }) + + host0 := dbHosts[0] + testutil.Equal(t, "host0.LastAnnouncement", ts, host0.LastAnnouncement) + + host1 := dbHosts[1] + testutil.Equal(t, "host1.LastAnnouncement", ts, host1.LastAnnouncement) + + host2 := dbHosts[2] + testutil.Equal(t, "host2.LastAnnouncement", ts, host2.LastAnnouncement) } + }