Skip to content

Commit

Permalink
Merge pull request #3288 from oasisprotocol/kostko/feature/registrati…
Browse files Browse the repository at this point in the history
…on-entity-check

go/worker/registration: Verify entity exists before node registration
  • Loading branch information
kostko authored Sep 18, 2020
2 parents 39266d5 + 8ba60c6 commit 37f6539
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
4 changes: 4 additions & 0 deletions .changelog/3286.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
go/worker/registration: Verify entity exists before node registration

This avoids some cases of failed node registration transactions when the
entity under which the node is being registered does not actually exist.
35 changes: 33 additions & 2 deletions go/worker/registration/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,10 @@ func (w *Worker) registrationLoop() { // nolint: gocyclo
}, off)
}

// (re-)register the node on entity registration update.
entityCh, entitySub, _ := w.registry.WatchEntities(w.ctx)
defer entitySub.Close()

var epoch epochtime.EpochTime
var lastTLSRotationEpoch epochtime.EpochTime
tlsRotationPending := true
Expand Down Expand Up @@ -310,6 +314,11 @@ Loop:
}
}
}
case ev := <-entityCh:
// Entity registration update.
if !ev.IsRegistration || !ev.Entity.ID.Equal(w.entityID) {
continue
}
case <-w.registerCh:
// Notification that a role provider has been updated.
}
Expand Down Expand Up @@ -345,18 +354,40 @@ Loop:
continue Loop
}

// Check if the entity under which we are registering actually exists.
_, err := w.registry.GetEntity(w.ctx, &registry.IDQuery{
Height: consensus.HeightLatest,
ID: w.entityID,
})
switch err {
case nil:
case registry.ErrNoSuchEntity:
// Entity does not yet exist.
w.logger.Warn("defering registration as the owning entity does not exist",
"entity_id", w.entityID,
)
continue
default:
// Unknown error while trying to look up entity.
w.logger.Error("failed to query owning entity",
"err", err,
"entity_id", w.entityID,
)
continue
}

// Package all per-role/runtime hooks into a metahook.
hook := func(n *node.Node) error {
for _, hook := range hooks {
if err := hook(n); err != nil {
if err = hook(n); err != nil {
return fmt.Errorf("hook failed: %w", err)
}
}
return nil
}

// Attempt a registration.
if err := regFn(epoch, hook, first); err != nil {
if err = regFn(epoch, hook, first); err != nil {
if first {
w.logger.Error("failed to register node",
"err", err,
Expand Down

0 comments on commit 37f6539

Please sign in to comment.