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

go/extra/stats: fix & simplify node-entity mapping #2856

Merged
merged 1 commit into from
Apr 20, 2020
Merged
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
7 changes: 7 additions & 0 deletions .changelog/2856.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
go/extra/stats: fix & simplify node-entity mapping

Instead of separately querying for entities and nodes, we can get Entity IDs
from nodes directly.

This change also fixes a case that previous variant missed: node that was
removed from entity list of nodes, but has not yet expired.
55 changes: 14 additions & 41 deletions go/extra/stats/cmd/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/oasislabs/oasis-core/go/common/cbor"
"github.com/oasislabs/oasis-core/go/common/crypto/signature"
"github.com/oasislabs/oasis-core/go/common/logging"
"github.com/oasislabs/oasis-core/go/common/node"
consensusAPI "github.com/oasislabs/oasis-core/go/consensus/api"
tmApi "github.com/oasislabs/oasis-core/go/consensus/tendermint/api"
tmcrypto "github.com/oasislabs/oasis-core/go/consensus/tendermint/crypto"
Expand Down Expand Up @@ -155,62 +154,36 @@ func newStats() *stats {
}

func (s *stats) addRegistryData(ctx context.Context, registry registryAPI.Backend, height int64) error {
// Fetch entities.
entities, err := registry.GetEntities(ctx, height)
if err != nil {
return err
}

// Fetch nodes.
nodes, err := registry.GetNodes(ctx, height)
if err != nil {
return err
}

// Map: nodeID -> Node
nodesMap := make(map[signature.PublicKey]*node.Node)
// Update stored mappings.
for _, n := range nodes {
nodesMap[n.ID] = n
}

// Store new nodes and entities info.
for _, ent := range entities {
var es *entityStats
var ok bool

// Since registry data can be fetched at multiple heights, entities might
// already exist.
if es, ok = s.entities[ent.ID]; !ok {
// Get or create node entity.
if es, ok = s.entities[n.EntityID]; !ok {
es = &entityStats{
id: ent.ID,
id: n.EntityID,
nodeSignatures: make(map[signature.PublicKey]int64),
nodeProposals: make(map[signature.PublicKey]int64),
}
s.entities[ent.ID] = es
s.entities[n.EntityID] = es
}

for _, nodeID := range ent.Nodes {
node, ok := nodesMap[nodeID]
if !ok {
// Skip entity nodes that are not registered.
logger.Debug("skipping not registered entity node",
"node_id", nodeID,
)
continue
}

// Add missing nodes.
if _, ok := es.nodeSignatures[nodeID]; !ok {
es.nodeSignatures[nodeID] = 0
es.nodeProposals[nodeID] = 0
cID := node.Consensus.ID
tmADdr := tmcrypto.PublicKeyToTendermint(&cID).Address().String()
s.nodeAddressMap[tmADdr] = nodeIDs{
entityID: ent.ID,
nodeID: nodeID,
}
// Initialize node stats if missing.
if _, ok := es.nodeSignatures[n.ID]; !ok {
es.nodeSignatures[n.ID] = 0
es.nodeProposals[n.ID] = 0
cID := n.Consensus.ID
tmAddr := tmcrypto.PublicKeyToTendermint(&cID).Address().String()
s.nodeAddressMap[tmAddr] = nodeIDs{
entityID: n.EntityID,
nodeID: n.ID,
}

}
}

Expand Down