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

Fix bug in host scanning #143

Merged
merged 12 commits into from
Nov 26, 2024
1 change: 1 addition & 0 deletions explorer/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ func AppliedEvents(cs consensus.State, b types.Block, cu ChainUpdate) []Event {
})
}
}

addEvent(types.Hash256(txn.ID()), cs.Index.Height, &e, relevant) // transaction maturity height is the current block height
}

Expand Down
72 changes: 55 additions & 17 deletions explorer/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ type (
Metrics Metrics
TreeUpdates []TreeNodeUpdate

HostAnnouncements []chain.HostAnnouncement
V2HostAnnouncements []V2HostAnnouncement

NewSiacoinElements []SiacoinOutput
SpentSiacoinElements []SiacoinOutput
EphemeralSiacoinElements []SiacoinOutput
Expand Down Expand Up @@ -216,6 +219,28 @@ func applyChainUpdate(tx UpdateTx, cau chain.ApplyUpdate) error {
})
})

var hostAnnouncements []chain.HostAnnouncement
for _, txn := range cau.Block.Transactions {
for _, arb := range txn.ArbitraryData {
var ha chain.HostAnnouncement
if ha.FromArbitraryData(arb) {
hostAnnouncements = append(hostAnnouncements, ha)
}
}
}
var v2HostAnnouncements []V2HostAnnouncement
for _, txn := range cau.Block.V2Transactions() {
for _, a := range txn.Attestations {
var ha chain.V2HostAnnouncement
if ha.FromAttestation(a) == nil {
v2HostAnnouncements = append(v2HostAnnouncements, V2HostAnnouncement{
PublicKey: a.PublicKey,
V2HostAnnouncement: ha,
})
}
}
}

events := AppliedEvents(cau.State, cau.Block, cau)

state := UpdateState{
Expand All @@ -225,6 +250,9 @@ func applyChainUpdate(tx UpdateTx, cau chain.ApplyUpdate) error {
Events: events,
TreeUpdates: treeUpdates,

HostAnnouncements: hostAnnouncements,
V2HostAnnouncements: v2HostAnnouncements,

NewSiacoinElements: newSiacoinElements,
SpentSiacoinElements: spentSiacoinElements,
EphemeralSiacoinElements: ephemeralSiacoinElements,
Expand Down Expand Up @@ -391,24 +419,34 @@ func revertChainUpdate(tx UpdateTx, cru chain.RevertUpdate, revertedIndex types.

func updateMetrics(tx UpdateTx, s UpdateState, metrics Metrics) (Metrics, error) {
seenHosts := make(map[types.PublicKey]struct{})
for _, event := range s.Events {
if event.Data.EventType() == EventTypeTransaction {
txn := event.Data.(*EventTransaction)
for _, host := range txn.HostAnnouncements {
if _, ok := seenHosts[host.PublicKey]; ok {
continue
}
for _, host := range s.HostAnnouncements {
if _, ok := seenHosts[host.PublicKey]; ok {
continue
}

exists, err := tx.HostExists(host.PublicKey)
if err != nil {
return Metrics{}, err
}
if !exists {
// we haven't seen this host yet, increment count
metrics.TotalHosts++
seenHosts[host.PublicKey] = struct{}{}
}
}
exists, err := tx.HostExists(host.PublicKey)
if err != nil {
return Metrics{}, err
}
if !exists {
// we haven't seen this host yet, increment count
metrics.TotalHosts++
seenHosts[host.PublicKey] = struct{}{}
}
}
for _, host := range s.V2HostAnnouncements {
if _, ok := seenHosts[host.PublicKey]; ok {
continue
}

exists, err := tx.HostExists(host.PublicKey)
if err != nil {
return Metrics{}, err
}
if !exists {
// we haven't seen this host yet, increment count
metrics.TotalHosts++
seenHosts[host.PublicKey] = struct{}{}
}
}

Expand Down
Loading
Loading