Browse Source

Merge pull request #2009 from tendermint/1772-call-validators-with-timeout

make `/status` RPC endpoint resistant to consensus halt
pull/2022/head
Alexander Simmerl 6 years ago
committed by GitHub
parent
commit
63835c0360
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 79 additions and 2 deletions
  1. +40
    -2
      rpc/core/status.go
  2. +39
    -0
      rpc/core/status_test.go

+ 40
- 2
rpc/core/status.go View File

@ -4,10 +4,10 @@ import (
"bytes"
"time"
cmn "github.com/tendermint/tendermint/libs/common"
ctypes "github.com/tendermint/tendermint/rpc/core/types"
sm "github.com/tendermint/tendermint/state"
"github.com/tendermint/tendermint/types"
cmn "github.com/tendermint/tendermint/libs/common"
)
// Get Tendermint status including node info, pubkey, latest block
@ -104,8 +104,17 @@ func Status() (*ctypes.ResultStatus, error) {
return result, nil
}
const consensusTimeout = time.Second
func validatorAtHeight(h int64) *types.Validator {
lastBlockHeight, vals := consensusState.GetValidators()
lastBlockHeight, vals := getValidatorsWithTimeout(
consensusState,
consensusTimeout,
)
if lastBlockHeight == -1 {
return nil
}
privValAddress := pubKey.Address()
@ -131,3 +140,32 @@ func validatorAtHeight(h int64) *types.Validator {
return nil
}
type validatorRetriever interface {
GetValidators() (int64, []*types.Validator)
}
// NOTE: Consensus might halt, but we still need to process RPC requests (at
// least for endpoints whole output does not depend on consensus state).
func getValidatorsWithTimeout(
vr validatorRetriever,
t time.Duration,
) (int64, []*types.Validator) {
resultCh := make(chan struct {
lastBlockHeight int64
vals []*types.Validator
})
go func() {
h, v := vr.GetValidators()
resultCh <- struct {
lastBlockHeight int64
vals []*types.Validator
}{h, v}
}()
select {
case res := <-resultCh:
return res.lastBlockHeight, res.vals
case <-time.After(t):
return -1, []*types.Validator{}
}
}

+ 39
- 0
rpc/core/status_test.go View File

@ -0,0 +1,39 @@
package core
import (
"testing"
"time"
"github.com/tendermint/tendermint/types"
)
func TestGetValidatorsWithTimeout(t *testing.T) {
height, vs := getValidatorsWithTimeout(
testValidatorReceiver{},
time.Millisecond,
)
if height != -1 {
t.Errorf("expected negative height")
}
if len(vs) != 0 {
t.Errorf("expected no validators")
}
}
type testValidatorReceiver struct{}
func (tr testValidatorReceiver) GetValidators() (int64, []*types.Validator) {
vs := []*types.Validator{}
for i := 0; i < 3; i++ {
v, _ := types.RandValidator(true, 10)
vs = append(vs, v)
}
time.Sleep(time.Millisecond)
return 10, vs
}

Loading…
Cancel
Save