Skip to content
Open
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
74 changes: 56 additions & 18 deletions cmd/ingestor/antenna_claims.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ type antennaClaimRunner struct {
limits antennaclaim.Limits
auto autoApproval
now func() time.Time
// photos are the station photo switch and caps (#1017 M5).
photos photoPolicy

mu sync.Mutex
// pending maps a public key to its pending claims: at most
Expand All @@ -104,6 +106,7 @@ func newAntennaClaimRunner(store *Store, cfg *antennaclaim.Config) *antennaClaim
queue: channelregistry.NewQueue(antennaclaim.QueueDir(store.path)),
limits: cfg.Limits(),
auto: autoApproval{proven: cfg.AutoApprove(), details: cfg.AutoApproveDetailsOn()},
photos: photoPolicy{on: cfg.PhotosRequested(), limits: cfg.PhotoLimits()},
now: time.Now,
pending: pendingCodes[pendingClaim]{},
}
Expand Down Expand Up @@ -214,12 +217,17 @@ func (s *Store) proveAntennaClaim(ctx context.Context, id, hash string, lat, lon
status, nowMs, hash, lat, lon, reviewedAt, approvedBy, id); err != nil {
return "", err
}
var dropped []string
if status == antennaclaim.StatusApproved {
if err := putInForce(ctx, tx, pubkey, id, nowMs); err != nil {
if dropped, err = putInForce(ctx, tx, pubkey, id, nowMs); err != nil {
return "", err
}
}
return status, tx.Commit()
if err := tx.Commit(); err != nil {
return "", err
}
s.unlinkPhotos(dropped)
return status, nil
}

// putInForce runs inside an approval's transaction, after the claim's status
Expand All @@ -229,31 +237,44 @@ func (s *Store) proveAntennaClaim(ctx context.Context, id, hash string, lat, lon
// flags are both 0 is superseded; one that still has a part in force stays
// approved. Nothing is ever put back in force: a later revoke of this claim
// leaves the node without the part (no resurrection, #898 T5).
func putInForce(ctx context.Context, tx *sql.Tx, pubkey, id string, nowMs int64) error {
var carriesHeight, carriesDetails bool
//
// #1017 M5: the photos of a claim whose details are replaced or cleared are
// deleted, and their ids returned for unlinking after the commit, unless
// this claim keeps them (keep_photos): then they move to this claim.
func putInForce(ctx context.Context, tx *sql.Tx, pubkey, id string, nowMs int64) (dropped []string, err error) {
var carriesHeight, carriesDetails, keepPhotos bool
if err := tx.QueryRowContext(ctx, `UPDATE node_antenna_claims
SET height_in_force = (height_op IS NOT NULL), details_in_force = (details_op IS NOT NULL)
WHERE request_id = ? RETURNING height_in_force, details_in_force`, id).Scan(&carriesHeight, &carriesDetails); err != nil {
return err
WHERE request_id = ? RETURNING height_in_force, details_in_force, keep_photos`, id).Scan(&carriesHeight, &carriesDetails, &keepPhotos); err != nil {
return nil, err
}
const others = ` WHERE pubkey = ? AND status = 'approved' AND request_id != ?`
if carriesHeight {
if _, err := tx.ExecContext(ctx, `UPDATE node_antenna_claims SET height_in_force = 0`+others+` AND height_in_force = 1`, pubkey, id); err != nil {
return err
return nil, err
}
}
if carriesDetails {
if _, err := tx.ExecContext(ctx, `DELETE FROM node_claim_admin_contacts WHERE request_id IN
(SELECT request_id FROM node_antenna_claims`+others+` AND details_in_force = 1)`, pubkey, id); err != nil {
return err
return nil, err
}
replaced := `(SELECT request_id FROM node_antenna_claims` + others + ` AND details_in_force = 1)`
if keepPhotos {
_, err = tx.ExecContext(ctx, `UPDATE node_claim_photos SET request_id = ? WHERE request_id IN `+replaced, id, pubkey, id)
} else {
dropped, err = deletePhotos(ctx, tx, `request_id IN `+replaced, pubkey, id)
}
if err != nil {
return nil, err
}
if _, err := tx.ExecContext(ctx, `UPDATE node_antenna_claims SET details_in_force = 0, contact_public = NULL`+others+` AND details_in_force = 1`, pubkey, id); err != nil {
return err
return nil, err
}
}
_, err := tx.ExecContext(ctx, `UPDATE node_antenna_claims SET status = 'superseded', reviewed_at = ?`+others+`
_, err = tx.ExecContext(ctx, `UPDATE node_antenna_claims SET status = 'superseded', reviewed_at = ?`+others+`
AND height_in_force = 0 AND details_in_force = 0`, nowMs, pubkey, id)
return err
return dropped, err
}

// dropContacts deletes the contacts of claim id, inside a transition out of
Expand Down Expand Up @@ -322,12 +343,12 @@ func (s *Store) insertAntennaClaim(ctx context.Context, cmd antennaclaim.Command
if _, err := tx.ExecContext(ctx, `INSERT INTO node_antenna_claims
(request_id, pubkey, height_m, height_op, details_op, antenna_type, gain_dbi, power_mask, installation, contact_public,
hardware, hardware_model, tx_power_dbm, cable_loss_db, azimuth_deg, beamwidth_deg, battery_wh, solar_w,
code, status, needs_review, created_at, expires_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 'pending', ?, ?, ?)`,
keep_photos, code, status, needs_review, created_at, expires_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 'pending', ?, ?, ?)`,
cmd.RequestID, cmd.PubKey, cmd.HeightM, nullString(parts.Height), nullString(parts.Details),
antennaType, d.GainDBi, powerMask, installation, contactPublic, hardware, hardwareModel,
d.TxPowerDbm, d.CableLossDb, d.AzimuthDeg, d.BeamwidthDeg, d.BatteryWh, d.SolarW,
cmd.Code, cmd.NeedsReview, created, created+l.RequestTTL.Milliseconds()); err != nil {
cmd.KeepPhotos && cmd.Details != nil, cmd.Code, cmd.NeedsReview, created, created+l.RequestTTL.Milliseconds()); err != nil {
return "", err
}
if contactAdmin.Valid {
Expand All @@ -347,7 +368,8 @@ func nullString(s string) sql.NullString { return sql.NullString{String: s, Vali
// older than an approved claim that carries one of its parts is refused
// (ErrNewerApproved): the newest proof by the key holder wins, whatever order
// an administrator reviews them in. A reject or revoke deletes the claim's
// contacts, and a revoke takes its parts out of force.
// contacts and its photos (#1017 M5), and a revoke takes its parts out of
// force.
func (s *Store) reviewAntennaClaim(ctx context.Context, id string, sources []string, target string, nowMs int64) (string, error) {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
Expand Down Expand Up @@ -390,9 +412,10 @@ func (s *Store) reviewAntennaClaim(ctx context.Context, id string, sources []str
WHERE request_id = ? AND status = ?`, target, nowMs, approvedBy, id, status); err != nil {
return "", err
}
var dropped []string
switch target {
case antennaclaim.StatusApproved:
if err := putInForce(ctx, tx, pubkey, id, nowMs); err != nil {
if dropped, err = putInForce(ctx, tx, pubkey, id, nowMs); err != nil {
return "", err
}
case antennaclaim.StatusRevoked:
Expand All @@ -404,8 +427,15 @@ func (s *Store) reviewAntennaClaim(ctx context.Context, id string, sources []str
if err := dropContacts(ctx, tx, id); err != nil {
return "", err
}
if dropped, err = deletePhotos(ctx, tx, `request_id = ?`, id); err != nil {
return "", err
}
}
return target, tx.Commit()
if err := tx.Commit(); err != nil {
return "", err
}
s.unlinkPhotos(dropped)
return target, nil
}

// PruneAntennaClaims deletes finished claims decided before cutoffMs and
Expand Down Expand Up @@ -568,6 +598,13 @@ func (r *antennaClaimRunner) apply(ctx context.Context, data []byte) antennaclai
case antennaclaim.OpRevoke:
res.ClaimID = cmd.ClaimID
status, err = r.store.reviewAntennaClaim(ctx, cmd.ClaimID, []string{antennaclaim.StatusApproved}, antennaclaim.StatusRevoked, nowMs)
case antennaclaim.OpPhotoSubmit:
res.ClaimID = cmd.ClaimID
status, err = r.store.submitPhoto(ctx, cmd, r.photos, nowMs)
case antennaclaim.OpPhotoApprove:
status, err = r.store.approvePhoto(ctx, cmd.PhotoID, r.photos, nowMs)
case antennaclaim.OpPhotoReject:
status, err = r.store.rejectPhoto(ctx, cmd.PhotoID)
}
if err != nil {
if cmd.Op == antennaclaim.OpClaim {
Expand All @@ -586,7 +623,7 @@ func userFacingClaimError(err error) string {
switch {
case errors.As(err, &conflict), errors.Is(err, errTooManyClaims), errors.Is(err, errTooManyNodeClaims),
errors.Is(err, errClaimNotFound), errors.Is(err, errClaimExpired), errors.Is(err, antennaclaim.ErrInvalidCommand),
errors.Is(err, antennaclaim.ErrNewerApproved):
errors.Is(err, antennaclaim.ErrNewerApproved), isPhotoRefusal(err):
return err.Error()
}
log.Printf("[antenna-claims] command failed: %v", err)
Expand All @@ -605,6 +642,7 @@ func (r *antennaClaimRunner) Prune(ctx context.Context) {
if _, err := r.queue.Prune(now, r.limits.RequestTTL, r.limits.MaxResults); err != nil {
log.Printf("[antenna-claims] queue prune failed: %v", err)
}
r.store.sweepPhotos(ctx, now)
}

// Start runs RunOnce every tick and Prune every pruneEvery. The returned func
Expand Down
1 change: 1 addition & 0 deletions cmd/ingestor/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,7 @@ func LoadConfig(path string) (*Config, error) {
// read the same way as the server reads them (#884, #888).
keys := configkey.New(data, log.Printf)
cfg.AntennaClaims.ApplyGates(keys.On)
cfg.AntennaClaims.ApplyPhotoGate(keys.Bool)
cfg.Accounts.ApplyGates(keys.On)
cfg.ChannelProposals.ApplyGates(keys.On)
if cfg.ClientRxCoverage != nil {
Expand Down
Loading
Loading