Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store v2 host net addresses #137

Merged
merged 7 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions explorer/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,7 @@ type EventTransaction struct {
}

// An EventV2Transaction represents a v2 transaction that affects the wallet.
type EventV2Transaction struct {
Transaction V2Transaction `json:"transaction"`
HostAnnouncements []chain.HostAnnouncement `json:"hostAnnouncements"`
Fee types.Currency `json:"fee"`
}
type EventV2Transaction V2Transaction

// An EventMinerPayout represents a miner payout from a block.
type EventMinerPayout struct {
Expand Down Expand Up @@ -221,7 +217,10 @@ func AppliedEvents(cs consensus.State, b types.Block, cu ChainUpdate) []Event {
for _, a := range txn.Attestations {
var ha chain.V2HostAnnouncement
if ha.FromAttestation(a) == nil {
// TODO: handle attestation
e.HostAnnouncements = append(e.HostAnnouncements, V2HostAnnouncement{
PublicKey: a.PublicKey,
V2HostAnnouncement: ha,
})
}
}
addEvent(types.Hash256(txn.ID()), cs.Index.Height, &e, relevant) // transaction maturity height is the current block height
Expand Down
8 changes: 5 additions & 3 deletions explorer/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,9 +299,11 @@ type HostScan struct {

// Host represents a host and the information gathered from scanning it.
type Host struct {
PublicKey types.PublicKey `json:"publicKey"`
NetAddress string `json:"netAddress"`
CountryCode string `json:"countryCode"`
PublicKey types.PublicKey `json:"publicKey"`
NetAddress string `json:"netAddress"`
V2NetAddresses []chain.NetAddress `json:"v2NetAddresses,omitempty"`

CountryCode string `json:"countryCode"`

KnownSince time.Time `json:"knownSince"`
LastScan time.Time `json:"lastScan"`
Expand Down
18 changes: 17 additions & 1 deletion internal/testutil/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,22 @@ func CheckTransaction(t *testing.T, expectTxn types.Transaction, gotTxn explorer
// cf.X = make([]uint64, d.ReadPrefix()) and the prefix is 0
// testutil.Equal(t, "covered fields", expected.CoveredFields, got.CoveredFields)
}

var hostAnnouncements []chain.HostAnnouncement
for _, arb := range expectTxn.ArbitraryData {
var ha chain.HostAnnouncement
if ha.FromArbitraryData(arb) {
hostAnnouncements = append(hostAnnouncements, ha)
}
}
Equal(t, "host announcements", len(hostAnnouncements), len(gotTxn.HostAnnouncements))
for i := range hostAnnouncements {
expected := hostAnnouncements[i]
got := gotTxn.HostAnnouncements[i]

Equal(t, "public key", expected.PublicKey, got.PublicKey)
Equal(t, "net address", expected.NetAddress, got.NetAddress)
}
}

// CheckV2Transaction checks the inputs and outputs of the retrieved transaction
Expand Down Expand Up @@ -195,7 +211,7 @@ func CheckV2Transaction(t *testing.T, expectTxn types.V2Transaction, gotTxn expl
var hostAnnouncements []explorer.V2HostAnnouncement
for _, attestation := range expectTxn.Attestations {
var ha chain.V2HostAnnouncement
if ha.FromAttestation(attestation) != nil {
if ha.FromAttestation(attestation) == nil {
hostAnnouncements = append(hostAnnouncements, explorer.V2HostAnnouncement{
V2HostAnnouncement: ha,
PublicKey: attestation.PublicKey,
Expand Down
62 changes: 34 additions & 28 deletions persist/sqlite/addresses.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,43 +26,25 @@ func scanEvent(tx *txn, s scanner) (ev explorer.Event, eventID int64, err error)
if err != nil {
return explorer.Event{}, 0, fmt.Errorf("failed to fetch transaction ID: %w", err)
}
txns, err := getTransactions(tx, map[int64]transactionID{txnID: {id: types.TransactionID(ev.ID)}})
txns, err := getTransactions(tx, map[int64]transactionID{0: {dbID: txnID, id: types.TransactionID(ev.ID)}})
if err != nil || len(txns) == 0 {
return explorer.Event{}, 0, fmt.Errorf("failed to fetch transaction: %w", err)
}

rows, err := tx.Query(`SELECT public_key, net_address FROM host_announcements WHERE transaction_id = ? ORDER BY transaction_order ASC`, txnID)
if err != nil {
return explorer.Event{}, 0, fmt.Errorf("failed to get host announcements: %w", err)
}
defer rows.Close()

eventTx.Transaction = txns[0]
for rows.Next() {
var announcement chain.HostAnnouncement
if err := rows.Scan(decode(&announcement.PublicKey), &announcement.NetAddress); err != nil {
return explorer.Event{}, 0, fmt.Errorf("failed to scan announcement: %w", err)
}
eventTx.HostAnnouncements = append(eventTx.HostAnnouncements, announcement)
}
eventTx.HostAnnouncements = eventTx.Transaction.HostAnnouncements
ev.Data = &eventTx
case explorer.EventTypeV2Transaction:
var txnID int64
var eventTx explorer.EventV2Transaction
err = tx.QueryRow(`SELECT transaction_id, fee FROM v2_transaction_events WHERE event_id = ?`, eventID).Scan(&txnID, decode(&eventTx.Fee))
err = tx.QueryRow(`SELECT transaction_id FROM v2_transaction_events WHERE event_id = ?`, eventID).Scan(&txnID)
if err != nil {
return explorer.Event{}, 0, fmt.Errorf("failed to fetch v2 transaction ID: %w", err)
}
txns, err := getV2Transactions(tx, []types.TransactionID{types.TransactionID(ev.ID)})
if err != nil || len(txns) == 0 {
return explorer.Event{}, 0, fmt.Errorf("failed to fetch v2 transaction: %w", err)
}

rows, err := tx.Query(`SELECT public_key, net_address FROM v2_host_announcements WHERE transaction_id = ? ORDER BY transaction_order ASC`, txnID)
if err != nil {
return explorer.Event{}, 0, fmt.Errorf("failed to get host announcements: %w", err)
}
defer rows.Close()
eventTx := explorer.EventV2Transaction(txns[0])
ev.Data = &eventTx
case explorer.EventTypeContractPayout:
var m explorer.EventContractPayout
err = tx.QueryRow(`SELECT sce.output_id, sce.leaf_index, sce.maturity_height, sce.address, sce.value, fce.contract_id, fce.leaf_index, fce.filesize, fce.file_merkle_root, fce.window_start, fce.window_end, fce.payout, fce.unlock_hash, fce.revision_number, ev.missed
Expand Down Expand Up @@ -113,14 +95,38 @@ func (st *Store) Hosts(pks []types.PublicKey) (result []explorer.Host, err error
}
defer rows.Close()

v2AddrStmt, err := tx.Prepare(`SELECT protocol,address FROM host_info_v2_netaddresses WHERE public_key = ? ORDER BY netaddress_order`)
if err != nil {
return err
}
defer v2AddrStmt.Close()

for rows.Next() {
var host explorer.Host
s, p := &host.Settings, &host.PriceTable
if err := rows.Scan(decode(&host.PublicKey), &host.NetAddress, &host.CountryCode, decode(&host.KnownSince), decode(&host.LastScan), &host.LastScanSuccessful, decode(&host.LastAnnouncement), &host.TotalScans, &host.SuccessfulInteractions, &host.FailedInteractions, &s.AcceptingContracts, decode(&s.MaxDownloadBatchSize), decode(&s.MaxDuration), decode(&s.MaxReviseBatchSize), &s.NetAddress, decode(&s.RemainingStorage), decode(&s.SectorSize), decode(&s.TotalStorage), decode(&s.Address), decode(&s.WindowSize), decode(&s.Collateral), decode(&s.MaxCollateral), decode(&s.BaseRPCPrice), decode(&s.ContractPrice), decode(&s.DownloadBandwidthPrice), decode(&s.SectorAccessPrice), decode(&s.StoragePrice), decode(&s.UploadBandwidthPrice), &s.EphemeralAccountExpiry, decode(&s.MaxEphemeralAccountBalance), decode(&s.RevisionNumber), &s.Version, &s.Release, &s.SiaMuxPort, decode(&p.UID), &p.Validity, decode(&p.HostBlockHeight), decode(&p.UpdatePriceTableCost), decode(&p.AccountBalanceCost), decode(&p.FundAccountCost), decode(&p.LatestRevisionCost), decode(&p.SubscriptionMemoryCost), decode(&p.SubscriptionNotificationCost), decode(&p.InitBaseCost), decode(&p.MemoryTimeCost), decode(&p.DownloadBandwidthCost), decode(&p.UploadBandwidthCost), decode(&p.DropSectorsBaseCost), decode(&p.DropSectorsUnitCost), decode(&p.HasSectorBaseCost), decode(&p.ReadBaseCost), decode(&p.ReadLengthCost), decode(&p.RenewContractCost), decode(&p.RevisionBaseCost), decode(&p.SwapSectorBaseCost), decode(&p.WriteBaseCost), decode(&p.WriteLengthCost), decode(&p.WriteStoreCost), decode(&p.TxnFeeMinRecommended), decode(&p.TxnFeeMaxRecommended), decode(&p.ContractPrice), decode(&p.CollateralCost), decode(&p.MaxCollateral), decode(&p.MaxDuration), decode(&p.WindowSize), decode(&p.RegistryEntriesLeft), decode(&p.RegistryEntriesTotal)); err != nil {
if err := func() error {
var host explorer.Host
s, p := &host.Settings, &host.PriceTable
if err := rows.Scan(decode(&host.PublicKey), &host.NetAddress, &host.CountryCode, decode(&host.KnownSince), decode(&host.LastScan), &host.LastScanSuccessful, decode(&host.LastAnnouncement), &host.TotalScans, &host.SuccessfulInteractions, &host.FailedInteractions, &s.AcceptingContracts, decode(&s.MaxDownloadBatchSize), decode(&s.MaxDuration), decode(&s.MaxReviseBatchSize), &s.NetAddress, decode(&s.RemainingStorage), decode(&s.SectorSize), decode(&s.TotalStorage), decode(&s.Address), decode(&s.WindowSize), decode(&s.Collateral), decode(&s.MaxCollateral), decode(&s.BaseRPCPrice), decode(&s.ContractPrice), decode(&s.DownloadBandwidthPrice), decode(&s.SectorAccessPrice), decode(&s.StoragePrice), decode(&s.UploadBandwidthPrice), &s.EphemeralAccountExpiry, decode(&s.MaxEphemeralAccountBalance), decode(&s.RevisionNumber), &s.Version, &s.Release, &s.SiaMuxPort, decode(&p.UID), &p.Validity, decode(&p.HostBlockHeight), decode(&p.UpdatePriceTableCost), decode(&p.AccountBalanceCost), decode(&p.FundAccountCost), decode(&p.LatestRevisionCost), decode(&p.SubscriptionMemoryCost), decode(&p.SubscriptionNotificationCost), decode(&p.InitBaseCost), decode(&p.MemoryTimeCost), decode(&p.DownloadBandwidthCost), decode(&p.UploadBandwidthCost), decode(&p.DropSectorsBaseCost), decode(&p.DropSectorsUnitCost), decode(&p.HasSectorBaseCost), decode(&p.ReadBaseCost), decode(&p.ReadLengthCost), decode(&p.RenewContractCost), decode(&p.RevisionBaseCost), decode(&p.SwapSectorBaseCost), decode(&p.WriteBaseCost), decode(&p.WriteLengthCost), decode(&p.WriteStoreCost), decode(&p.TxnFeeMinRecommended), decode(&p.TxnFeeMaxRecommended), decode(&p.ContractPrice), decode(&p.CollateralCost), decode(&p.MaxCollateral), decode(&p.MaxDuration), decode(&p.WindowSize), decode(&p.RegistryEntriesLeft), decode(&p.RegistryEntriesTotal)); err != nil {
return err
}

v2AddrRows, err := v2AddrStmt.Query(encode(host.PublicKey))
if err != nil {
return err
}
defer v2AddrRows.Close()
for v2AddrRows.Next() {
var netAddr chain.NetAddress
if err := v2AddrRows.Scan(&netAddr.Protocol, &netAddr.Address); err != nil {
return err
}
host.V2NetAddresses = append(host.V2NetAddresses, netAddr)
}

result = append(result, host)
return nil
}(); err != nil {
return err
}

result = append(result, host)
}
return nil
})
Expand Down
Loading
Loading