rpc: Block and Commit take pointers; return latest on nil

This commit is contained in:
Ethan Buchman
2017-08-22 17:42:23 -04:00
parent e2e8746044
commit f0f1ebe013
7 changed files with 41 additions and 23 deletions

View File

@ -143,7 +143,7 @@ func (c *HTTP) Genesis() (*ctypes.ResultGenesis, error) {
return result, nil
}
func (c *HTTP) Block(height int) (*ctypes.ResultBlock, error) {
func (c *HTTP) Block(height *int) (*ctypes.ResultBlock, error) {
result := new(ctypes.ResultBlock)
_, err := c.rpc.Call("block", map[string]interface{}{"height": height}, result)
if err != nil {
@ -152,7 +152,7 @@ func (c *HTTP) Block(height int) (*ctypes.ResultBlock, error) {
return result, nil
}
func (c *HTTP) Commit(height int) (*ctypes.ResultCommit, error) {
func (c *HTTP) Commit(height *int) (*ctypes.ResultCommit, error) {
result := new(ctypes.ResultCommit)
_, err := c.rpc.Call("commit", map[string]interface{}{"height": height}, result)
if err != nil {
@ -176,9 +176,7 @@ func (c *HTTP) Tx(hash []byte, prove bool) (*ctypes.ResultTx, error) {
func (c *HTTP) Validators(height *int) (*ctypes.ResultValidators, error) {
result := new(ctypes.ResultValidators)
_, err := c.rpc.Call("validators", map[string]interface{}{
"height": height,
}, result)
_, err := c.rpc.Call("validators", map[string]interface{}{"height": height}, result)
if err != nil {
return nil, errors.Wrap(err, "Validators")
}

View File

@ -42,8 +42,8 @@ type ABCIClient interface {
// SignClient groups together the interfaces need to get valid
// signatures and prove anything about the chain
type SignClient interface {
Block(height int) (*ctypes.ResultBlock, error)
Commit(height int) (*ctypes.ResultCommit, error)
Block(height *int) (*ctypes.ResultBlock, error)
Commit(height *int) (*ctypes.ResultCommit, error)
Validators(height *int) (*ctypes.ResultValidators, error)
Tx(hash []byte, prove bool) (*ctypes.ResultTx, error)
}

View File

@ -93,11 +93,11 @@ func (c Local) Genesis() (*ctypes.ResultGenesis, error) {
return core.Genesis()
}
func (c Local) Block(height int) (*ctypes.ResultBlock, error) {
func (c Local) Block(height *int) (*ctypes.ResultBlock, error) {
return core.Block(height)
}
func (c Local) Commit(height int) (*ctypes.ResultCommit, error) {
func (c Local) Commit(height *int) (*ctypes.ResultCommit, error) {
return core.Commit(height)
}

View File

@ -116,11 +116,11 @@ func (c Client) Genesis() (*ctypes.ResultGenesis, error) {
return core.Genesis()
}
func (c Client) Block(height int) (*ctypes.ResultBlock, error) {
func (c Client) Block(height *int) (*ctypes.ResultBlock, error) {
return core.Block(height)
}
func (c Client) Commit(height int) (*ctypes.ResultCommit, error) {
func (c Client) Commit(height *int) (*ctypes.ResultCommit, error) {
return core.Commit(height)
}

View File

@ -110,7 +110,8 @@ func TestAppCalls(t *testing.T) {
sh := s.LatestBlockHeight
// look for the future
_, err = c.Block(sh + 2)
h := sh + 2
_, err = c.Block(&h)
assert.NotNil(err) // no block yet
// write something
@ -137,7 +138,7 @@ func TestAppCalls(t *testing.T) {
assert.EqualValues(tx, ptx.Tx)
// and we can even check the block is added
block, err := c.Block(apph)
block, err := c.Block(&apph)
require.Nil(err, "%d: %+v", i, err)
appHash := block.BlockMeta.Header.AppHash
assert.True(len(appHash) > 0)
@ -158,14 +159,15 @@ func TestAppCalls(t *testing.T) {
}
// and get the corresponding commit with the same apphash
commit, err := c.Commit(apph)
commit, err := c.Commit(&apph)
require.Nil(err, "%d: %+v", i, err)
cappHash := commit.Header.AppHash
assert.Equal(appHash, cappHash)
assert.NotNil(commit.Commit)
// compare the commits (note Commit(2) has commit from Block(3))
commit2, err := c.Commit(apph - 1)
h = apph - 1
commit2, err := c.Commit(&h)
require.Nil(err, "%d: %+v", i, err)
assert.Equal(block.Block.LastCommit, commit2.Commit)

View File

@ -184,8 +184,16 @@ func BlockchainInfo(minHeight, maxHeight int) (*ctypes.ResultBlockchainInfo, err
// "jsonrpc": "2.0"
// }
// ```
func Block(height int) (*ctypes.ResultBlock, error) {
if height == 0 {
func Block(heightPtr *int) (*ctypes.ResultBlock, error) {
if heightPtr == nil {
height := blockStore.Height()
blockMeta := blockStore.LoadBlockMeta(height)
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() {
@ -267,8 +275,16 @@ func Block(height int) (*ctypes.ResultBlock, error) {
// "jsonrpc": "2.0"
// }
// ```
func Commit(height int) (*ctypes.ResultCommit, error) {
if height == 0 {
func Commit(heightPtr *int) (*ctypes.ResultCommit, error) {
if heightPtr == nil {
height := blockStore.Height()
header := blockStore.LoadBlockMeta(height).Header
commit := blockStore.LoadSeenCommit(height)
return &ctypes.ResultCommit{header, commit, false}, nil
}
height := *heightPtr
if height <= 0 {
return nil, fmt.Errorf("Height must be greater than 0")
}
storeHeight := blockStore.Height()

View File

@ -42,17 +42,19 @@ import (
// "jsonrpc": "2.0"
// }
// ```
func Validators(height *int) (*ctypes.ResultValidators, error) {
if height == nil {
func Validators(heightPtr *int) (*ctypes.ResultValidators, error) {
if heightPtr == nil {
blockHeight, validators := consensusState.GetValidators()
return &ctypes.ResultValidators{blockHeight, validators}, nil
}
height := *heightPtr
state := consensusState.GetState()
validators, err := state.LoadValidators(*height)
validators, err := state.LoadValidators(height)
if err != nil {
return nil, err
}
return &ctypes.ResultValidators{*height, validators.Validators}, nil
return &ctypes.ResultValidators{height, validators.Validators}, nil
}
// Dump consensus state.