@startuml native_registration
title Native Indexer — Indexer Subscription (StartNativeRegistration)

participant "Indexer A" as IndexerA
participant "Indexer B" as IndexerB
participant "Native Indexer" as Native
participant "DHT Kademlia" as DHT
participant "GossipSub (oc-indexer-registry)" as PubSub

note over IndexerA,IndexerB: At start + every 60s (RecommendedHeartbeatInterval)\\nStartNativeRegistration → RegisterWithNative

par Indexer A subscribe
    IndexerA -> IndexerA: fillRateFn()\\n= len(StreamRecords[HB]) / maxNodes

    IndexerA -> IndexerA: Build IndexerRegistration{\\n  PeerID_A, Addr_A,\\n  Timestamp=now.UnixNano(),\\n  FillRate=fillRateFn(),\\n  PubKey, Signature\\n}\\nreg.Sign(h)

    IndexerA -> Native: NewStream /opencloud/native/subscribe/1.0
    IndexerA -> Native: stream.Encode(IndexerRegistration A)

    Native -> Native: reg.Verify() — verify signature
    Native -> Native: liveIndexerEntry{\\n  PeerID_A, Addr_A,\\n  ExpiresAt = now + IndexerTTL (90s),\\n  FillRate = reg.FillRate,\\n  PubKey, Signature\\n}
    Native -> Native: liveIndexers[PeerID_A] = entry A
    Native -> Native: knownPeerIDs[PeerID_A] = Addr_A

    Native -> DHT: PutValue("/indexer/"+PeerID_A, entry A)
    DHT --> Native: ok

    Native -> PubSub: topic.Publish([]byte(PeerID_A))
    note over PubSub: Gossip to other Natives\\n→ it adds PeerID_A to knownPeerIDs\\n→ refresh DHT next tick (30s)

    IndexerA -> Native: stream.Close()

else Indexer B subscribe
    IndexerB -> IndexerB: fillRateFn() + reg.Sign(h)
    IndexerB -> Native: NewStream /opencloud/native/subscribe/1.0
    IndexerB -> Native: stream.Encode(IndexerRegistration B)

    Native -> Native: reg.Verify() + liveIndexerEntry{FillRate=reg.FillRate, ExpiresAt=now+90s}
    Native -> Native: liveIndexers[PeerID_B] = entry B
    Native -> DHT: PutValue("/indexer/"+PeerID_B, entry B)
    Native -> PubSub: topic.Publish([]byte(PeerID_B))
    IndexerB -> Native: stream.Close()
end par

note over Native: liveIndexers = {PeerID_A: {FillRate:0.3}, PeerID_B: {FillRate:0.6}}\\nTTL 90s — IndexerTTL

note over Native: Explicit unsubcrive on stop :\\nUnregisterFromNative → /opencloud/native/unsubscribe/1.0\\nNative close all now.

@enduml
