rpc: getHeight helper function

This commit is contained in:
Ethan Buchman
2017-12-26 19:37:42 -05:00
parent 4171bd3bae
commit 801e3dfacf
2 changed files with 29 additions and 44 deletions

View File

@ -193,19 +193,9 @@ func BlockchainInfo(minHeight, maxHeight int64) (*ctypes.ResultBlockchainInfo, e
// } // }
// ``` // ```
func Block(heightPtr *int64) (*ctypes.ResultBlock, error) { func Block(heightPtr *int64) (*ctypes.ResultBlock, error) {
if heightPtr == nil { height, _, err := getHeight(blockStore, heightPtr)
height := blockStore.Height() if err != nil {
blockMeta := blockStore.LoadBlockMeta(height) return nil, err
block := blockStore.LoadBlock(height)
return &ctypes.ResultBlock{blockMeta, block}, nil
}
height := *heightPtr
if height <= 0 {
return nil, fmt.Errorf("Height must be greater than 0")
}
if height > blockStore.Height() {
return nil, fmt.Errorf("Height must be less than the current blockchain height")
} }
blockMeta := blockStore.LoadBlockMeta(height) blockMeta := blockStore.LoadBlockMeta(height)
@ -284,20 +274,9 @@ func Block(heightPtr *int64) (*ctypes.ResultBlock, error) {
// } // }
// ``` // ```
func Commit(heightPtr *int64) (*ctypes.ResultCommit, error) { func Commit(heightPtr *int64) (*ctypes.ResultCommit, error) {
if heightPtr == nil { height, storeHeight, err := getHeight(blockStore, heightPtr)
height := blockStore.Height() if err != nil {
header := blockStore.LoadBlockMeta(height).Header return nil, err
commit := blockStore.LoadSeenCommit(height)
return ctypes.NewResultCommit(header, commit, false), nil
}
height := *heightPtr
if height <= 0 {
return nil, fmt.Errorf("Height must be greater than 0")
}
storeHeight := blockStore.Height()
if height > storeHeight {
return nil, fmt.Errorf("Height must be less than or equal to the current blockchain height")
} }
header := blockStore.LoadBlockMeta(height).Header header := blockStore.LoadBlockMeta(height).Header
@ -334,7 +313,7 @@ func Commit(heightPtr *int64) (*ctypes.ResultCommit, error) {
// //
// ```json // ```json
// { // {
// "height": 88, // "height": 10,
// "results": [ // "results": [
// { // {
// "code": 0, // "code": 0,
@ -348,18 +327,9 @@ func Commit(heightPtr *int64) (*ctypes.ResultCommit, error) {
// } // }
// ``` // ```
func BlockResults(heightPtr *int64) (*ctypes.ResultBlockResults, error) { func BlockResults(heightPtr *int64) (*ctypes.ResultBlockResults, error) {
var height int64 height, _, err := getHeight(blockStore, heightPtr)
if heightPtr != nil { if err != nil {
height = *heightPtr return nil, err
if height <= 0 {
return nil, fmt.Errorf("Height must be greater than 0")
}
storeHeight := blockStore.Height()
if height > storeHeight {
return nil, fmt.Errorf("Height must be less than or equal to the current blockchain height")
}
} else {
height = blockStore.Height()
} }
// load the results // load the results
@ -375,3 +345,19 @@ func BlockResults(heightPtr *int64) (*ctypes.ResultBlockResults, error) {
} }
return res, nil return res, nil
} }
func getHeight(blockStore types.BlockStore, heightPtr *int64) (reqHeight int64, storeHeight int64, err error) {
storeHeight = blockStore.Height()
if heightPtr != nil {
reqHeight = *heightPtr
if reqHeight <= 0 {
return 0, 0, fmt.Errorf("Height must be greater than 0")
}
if reqHeight > storeHeight {
return 0, 0, fmt.Errorf("Height must be less than or equal to the current blockchain height")
}
} else {
reqHeight = blockStore.Height()
}
return reqHeight, storeHeight, nil
}

View File

@ -43,12 +43,11 @@ import (
// } // }
// ``` // ```
func Validators(heightPtr *int64) (*ctypes.ResultValidators, error) { func Validators(heightPtr *int64) (*ctypes.ResultValidators, error) {
if heightPtr == nil { height, _, err := getHeight(blockStore, heightPtr)
blockHeight, validators := consensusState.GetValidators() if err != nil {
return &ctypes.ResultValidators{blockHeight, validators}, nil return nil, err
} }
height := *heightPtr
state := consensusState.GetState() state := consensusState.GetState()
validators, err := state.LoadValidators(height) validators, err := state.LoadValidators(height)
if err != nil { if err != nil {