Skip to content

Commit

Permalink
feat: add ChainID() method for getting the chain ID from node status (#…
Browse files Browse the repository at this point in the history
…69)

## Description


For getting `chain ID`, replace `querying genesis` with `querying status` as `querying genesis` is heavy and may put much load on the node which is unnecessary, especially when the app crashed and restarted. 

## Checklist
- [ ] Targeted PR against correct branch.
- [ ] Linked to Github issue with discussion and accepted design OR link to spec that describes this work.
- [ ] Wrote unit tests.  
- [ ] Re-reviewed `Files changed` in the Github PR explorer.
  • Loading branch information
huichiaotsou authored Jul 5, 2022
1 parent d0d22cf commit 88a8bc2
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 deletions.
5 changes: 5 additions & 0 deletions node/local/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,11 @@ func (cp *Node) LatestHeight() (int64, error) {
return cp.blockStore.Height(), nil
}

// ChainID implements node.Node
func (cp *Node) ChainID() (string, error) {
return cp.genesisDoc.ChainID, nil
}

// Validators implements node.Node
func (cp *Node) Validators(height int64) (*tmctypes.ResultValidators, error) {
height, err := cp.getHeight(cp.blockStore.Height(), &height)
Expand Down
3 changes: 3 additions & 0 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ type Node interface {
// is returned if the query fails.
LatestHeight() (int64, error)

// ChainID returns the network ID
ChainID() (string, error)

// Validators returns all the known Tendermint validators for a given block
// height. An error is returned if the query fails.
Validators(height int64) (*tmctypes.ResultValidators, error)
Expand Down
11 changes: 11 additions & 0 deletions node/remote/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,17 @@ func (cp *Node) LatestHeight() (int64, error) {
return height, nil
}

// ChainID implements node.Node
func (cp *Node) ChainID() (string, error) {
status, err := cp.client.Status(cp.ctx)
if err != nil {
return "", err
}

chainID := status.NodeInfo.Network
return chainID, err
}

// Validators implements node.Node
func (cp *Node) Validators(height int64) (*tmctypes.ResultValidators, error) {
vals := &tmctypes.ResultValidators{
Expand Down
6 changes: 3 additions & 3 deletions parser/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ func NewWorker(ctx *Context, queue types.HeightQueue, index int) Worker {
// given worker queue. Any failed job is logged and re-enqueued.
func (w Worker) Start() {
logging.WorkerCount.Inc()
nodeInfo, err := w.node.Genesis()
chainID, err := w.node.ChainID()
if err != nil {
w.logger.Error("error while getting genesis info from the node ", "err", err)
w.logger.Error("error while getting chain ID from the node ", "err", err)
}

for i := range w.queue {
Expand All @@ -68,7 +68,7 @@ func (w Worker) Start() {
}()
}

logging.WorkerHeight.WithLabelValues(fmt.Sprintf("%d", w.index), nodeInfo.Genesis.ChainID).Set(float64(i))
logging.WorkerHeight.WithLabelValues(fmt.Sprintf("%d", w.index), chainID).Set(float64(i))
}
}

Expand Down

0 comments on commit 88a8bc2

Please sign in to comment.