module Registry.Scheduled declares *

export clearExpired_86400, replicate_3600, renew_43200

import "registry-service.aqua"
import "registry-api.aqua"
import "@fluencelabs/aqua-lib/builtin.aqua"
import "@fluencelabs/trust-graph/trust-graph.aqua"

-- clears expired records
func clearExpired_86400():
  on HOST_PEER_ID:
    t <- Peer.timestamp_sec()
    Registry.clear_expired(t)

-- update stale local records
func renew_43200():
  on HOST_PEER_ID:
    t <- Peer.timestamp_sec()
    res <- Registry.get_stale_local_records(t)
    for r <- res.result par:
      signature <- getRecordSignature(r.metadata, t)
      putRecord(r.metadata, t, signature.signature!)

-- get all old records and replicate it by routes
func replicate_3600():
  on HOST_PEER_ID:
    t <- Peer.timestamp_sec()
    res <- Registry.evict_stale(t)
    for r <- res.results par:
      k <- Op.string_to_b58(r.key.id)
      nodes <- Kademlia.neighborhood(k, nil, nil)
      for n <- nodes par:
        on n:
          tt <- Peer.timestamp_sec()
          key_weight <- TrustGraph.get_weight(r.key.owner_peer_id, tt)
          Registry.republish_key(r.key, key_weight, tt)

          records_weights: *WeightResult
          for record <- r.records:
            records_weights <- TrustGraph.get_weight(record.metadata.issued_by, tt)
          Registry.republish_records(r.records, records_weights, tt)
