Browse Source

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

pull/618/head
Ethan Buchman 7 years ago
parent
commit
f0f1ebe013
7 changed files with 41 additions and 23 deletions
  1. +3
    -5
      rpc/client/httpclient.go
  2. +2
    -2
      rpc/client/interface.go
  3. +2
    -2
      rpc/client/localclient.go
  4. +2
    -2
      rpc/client/mock/client.go
  5. +6
    -4
      rpc/client/rpc_test.go
  6. +20
    -4
      rpc/core/blocks.go
  7. +6
    -4
      rpc/core/consensus.go

+ 3
- 5
rpc/client/httpclient.go 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")
}


+ 2
- 2
rpc/client/interface.go 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)
}


+ 2
- 2
rpc/client/localclient.go 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)
}


+ 2
- 2
rpc/client/mock/client.go 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)
}


+ 6
- 4
rpc/client/rpc_test.go 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)


+ 20
- 4
rpc/core/blocks.go 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()


+ 6
- 4
rpc/core/consensus.go 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.


Loading…
Cancel
Save