mirror of
https://github.com/fluencelabs/tendermint
synced 2025-06-15 14:21:22 +00:00
rpc: Block and Commit take pointers; return latest on nil
This commit is contained in:
@ -143,7 +143,7 @@ func (c *HTTP) Genesis() (*ctypes.ResultGenesis, error) {
|
|||||||
return result, nil
|
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)
|
result := new(ctypes.ResultBlock)
|
||||||
_, err := c.rpc.Call("block", map[string]interface{}{"height": height}, result)
|
_, err := c.rpc.Call("block", map[string]interface{}{"height": height}, result)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -152,7 +152,7 @@ func (c *HTTP) Block(height int) (*ctypes.ResultBlock, error) {
|
|||||||
return result, nil
|
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)
|
result := new(ctypes.ResultCommit)
|
||||||
_, err := c.rpc.Call("commit", map[string]interface{}{"height": height}, result)
|
_, err := c.rpc.Call("commit", map[string]interface{}{"height": height}, result)
|
||||||
if err != nil {
|
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) {
|
func (c *HTTP) Validators(height *int) (*ctypes.ResultValidators, error) {
|
||||||
result := new(ctypes.ResultValidators)
|
result := new(ctypes.ResultValidators)
|
||||||
_, err := c.rpc.Call("validators", map[string]interface{}{
|
_, err := c.rpc.Call("validators", map[string]interface{}{"height": height}, result)
|
||||||
"height": height,
|
|
||||||
}, result)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "Validators")
|
return nil, errors.Wrap(err, "Validators")
|
||||||
}
|
}
|
||||||
|
@ -42,8 +42,8 @@ type ABCIClient interface {
|
|||||||
// SignClient groups together the interfaces need to get valid
|
// SignClient groups together the interfaces need to get valid
|
||||||
// signatures and prove anything about the chain
|
// signatures and prove anything about the chain
|
||||||
type SignClient interface {
|
type SignClient interface {
|
||||||
Block(height int) (*ctypes.ResultBlock, error)
|
Block(height *int) (*ctypes.ResultBlock, error)
|
||||||
Commit(height int) (*ctypes.ResultCommit, error)
|
Commit(height *int) (*ctypes.ResultCommit, error)
|
||||||
Validators(height *int) (*ctypes.ResultValidators, error)
|
Validators(height *int) (*ctypes.ResultValidators, error)
|
||||||
Tx(hash []byte, prove bool) (*ctypes.ResultTx, error)
|
Tx(hash []byte, prove bool) (*ctypes.ResultTx, error)
|
||||||
}
|
}
|
||||||
|
@ -93,11 +93,11 @@ func (c Local) Genesis() (*ctypes.ResultGenesis, error) {
|
|||||||
return core.Genesis()
|
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)
|
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)
|
return core.Commit(height)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -116,11 +116,11 @@ func (c Client) Genesis() (*ctypes.ResultGenesis, error) {
|
|||||||
return core.Genesis()
|
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)
|
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)
|
return core.Commit(height)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -110,7 +110,8 @@ func TestAppCalls(t *testing.T) {
|
|||||||
sh := s.LatestBlockHeight
|
sh := s.LatestBlockHeight
|
||||||
|
|
||||||
// look for the future
|
// look for the future
|
||||||
_, err = c.Block(sh + 2)
|
h := sh + 2
|
||||||
|
_, err = c.Block(&h)
|
||||||
assert.NotNil(err) // no block yet
|
assert.NotNil(err) // no block yet
|
||||||
|
|
||||||
// write something
|
// write something
|
||||||
@ -137,7 +138,7 @@ func TestAppCalls(t *testing.T) {
|
|||||||
assert.EqualValues(tx, ptx.Tx)
|
assert.EqualValues(tx, ptx.Tx)
|
||||||
|
|
||||||
// and we can even check the block is added
|
// 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)
|
require.Nil(err, "%d: %+v", i, err)
|
||||||
appHash := block.BlockMeta.Header.AppHash
|
appHash := block.BlockMeta.Header.AppHash
|
||||||
assert.True(len(appHash) > 0)
|
assert.True(len(appHash) > 0)
|
||||||
@ -158,14 +159,15 @@ func TestAppCalls(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// and get the corresponding commit with the same apphash
|
// 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)
|
require.Nil(err, "%d: %+v", i, err)
|
||||||
cappHash := commit.Header.AppHash
|
cappHash := commit.Header.AppHash
|
||||||
assert.Equal(appHash, cappHash)
|
assert.Equal(appHash, cappHash)
|
||||||
assert.NotNil(commit.Commit)
|
assert.NotNil(commit.Commit)
|
||||||
|
|
||||||
// compare the commits (note Commit(2) has commit from Block(3))
|
// 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)
|
require.Nil(err, "%d: %+v", i, err)
|
||||||
assert.Equal(block.Block.LastCommit, commit2.Commit)
|
assert.Equal(block.Block.LastCommit, commit2.Commit)
|
||||||
|
|
||||||
|
@ -184,8 +184,16 @@ func BlockchainInfo(minHeight, maxHeight int) (*ctypes.ResultBlockchainInfo, err
|
|||||||
// "jsonrpc": "2.0"
|
// "jsonrpc": "2.0"
|
||||||
// }
|
// }
|
||||||
// ```
|
// ```
|
||||||
func Block(height int) (*ctypes.ResultBlock, error) {
|
func Block(heightPtr *int) (*ctypes.ResultBlock, error) {
|
||||||
if height == 0 {
|
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")
|
return nil, fmt.Errorf("Height must be greater than 0")
|
||||||
}
|
}
|
||||||
if height > blockStore.Height() {
|
if height > blockStore.Height() {
|
||||||
@ -267,8 +275,16 @@ func Block(height int) (*ctypes.ResultBlock, error) {
|
|||||||
// "jsonrpc": "2.0"
|
// "jsonrpc": "2.0"
|
||||||
// }
|
// }
|
||||||
// ```
|
// ```
|
||||||
func Commit(height int) (*ctypes.ResultCommit, error) {
|
func Commit(heightPtr *int) (*ctypes.ResultCommit, error) {
|
||||||
if height == 0 {
|
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")
|
return nil, fmt.Errorf("Height must be greater than 0")
|
||||||
}
|
}
|
||||||
storeHeight := blockStore.Height()
|
storeHeight := blockStore.Height()
|
||||||
|
@ -42,17 +42,19 @@ import (
|
|||||||
// "jsonrpc": "2.0"
|
// "jsonrpc": "2.0"
|
||||||
// }
|
// }
|
||||||
// ```
|
// ```
|
||||||
func Validators(height *int) (*ctypes.ResultValidators, error) {
|
func Validators(heightPtr *int) (*ctypes.ResultValidators, error) {
|
||||||
if height == nil {
|
if heightPtr == nil {
|
||||||
blockHeight, validators := consensusState.GetValidators()
|
blockHeight, validators := consensusState.GetValidators()
|
||||||
return &ctypes.ResultValidators{blockHeight, validators}, nil
|
return &ctypes.ResultValidators{blockHeight, validators}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
height := *heightPtr
|
||||||
state := consensusState.GetState()
|
state := consensusState.GetState()
|
||||||
validators, err := state.LoadValidators(*height)
|
validators, err := state.LoadValidators(height)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &ctypes.ResultValidators{*height, validators.Validators}, nil
|
return &ctypes.ResultValidators{height, validators.Validators}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Dump consensus state.
|
// Dump consensus state.
|
||||||
|
Reference in New Issue
Block a user