From a41f0d3891b8af96c3ef7ee974aad8ee93be29e0 Mon Sep 17 00:00:00 2001 From: Ethan Buchman Date: Sun, 13 May 2018 19:53:54 -0400 Subject: [PATCH] rpc: /consensus_state for simplified output --- consensus/state.go | 8 ++++++++ consensus/types/height_vote_set.go | 9 +++++++-- consensus/types/round_state.go | 23 +++++++++++++++++++++++ rpc/core/consensus.go | 7 +++++++ rpc/core/pipe.go | 1 + rpc/core/routes.go | 1 + rpc/core/types/responses.go | 5 +++++ 7 files changed, 52 insertions(+), 2 deletions(-) diff --git a/consensus/state.go b/consensus/state.go index 10bbb15f7..4450c74e7 100644 --- a/consensus/state.go +++ b/consensus/state.go @@ -185,6 +185,14 @@ func (cs *ConsensusState) GetRoundStateJSON() ([]byte, error) { return cdc.MarshalJSON(cs.RoundState) } +// GetRoundStateSimpleJSON returns a json of RoundStateSimple, marshalled using go-amino. +func (cs *ConsensusState) GetRoundStateSimpleJSON() ([]byte, error) { + cs.mtx.Lock() + defer cs.mtx.Unlock() + + return cdc.MarshalJSON(cs.RoundState.RoundStateSimple()) +} + // GetValidators returns a copy of the current validators. func (cs *ConsensusState) GetValidators() (int64, []*types.Validator) { cs.mtx.Lock() diff --git a/consensus/types/height_vote_set.go b/consensus/types/height_vote_set.go index a861571e0..2013f9199 100644 --- a/consensus/types/height_vote_set.go +++ b/consensus/types/height_vote_set.go @@ -219,6 +219,12 @@ type roundVotes struct { func (hvs *HeightVoteSet) MarshalJSON() ([]byte, error) { hvs.mtx.Lock() defer hvs.mtx.Unlock() + + roundsVotes := hvs.toRoundVotes() + return cdc.MarshalJSON(roundsVotes) +} + +func (hvs *HeightVoteSet) toRoundVotes() []roundVotes { totalRounds := hvs.round + 1 roundsVotes := make([]roundVotes, totalRounds) // rounds 0 ~ hvs.round inclusive @@ -232,8 +238,7 @@ func (hvs *HeightVoteSet) MarshalJSON() ([]byte, error) { } } // TODO: all other peer catchup rounds - - return cdc.MarshalJSON(roundsVotes) + return roundsVotes } // If a peer claims that it has 2/3 majority for given blockKey, call this. diff --git a/consensus/types/round_state.go b/consensus/types/round_state.go index 65c40781e..4e1db3cd5 100644 --- a/consensus/types/round_state.go +++ b/consensus/types/round_state.go @@ -1,10 +1,12 @@ package types import ( + "encoding/json" "fmt" "time" "github.com/tendermint/tendermint/types" + cmn "github.com/tendermint/tmlibs/common" ) //----------------------------------------------------------------------------- @@ -77,6 +79,27 @@ type RoundState struct { LastValidators *types.ValidatorSet `json:"last_validators"` } +type RoundStateSimple struct { + HeightRoundStep string `json:"height/round/step"` + StartTime time.Time `json:"start_time"` + ProposalBlockHash cmn.HexBytes `json:"proposal_block_hash"` + LockedBlockHash cmn.HexBytes `json:"locked_block_hash"` + ValidBlockHash cmn.HexBytes `json:"valid_block_hash"` + Votes json.RawMessage `json:"height_vote_set"` +} + +func (rs *RoundState) RoundStateSimple() RoundStateSimple { + votesJSON, _ := rs.Votes.MarshalJSON() // TODO err + return RoundStateSimple{ + HeightRoundStep: fmt.Sprintf("%d/%d/%d", rs.Height, rs.Round, rs.Step), + StartTime: rs.StartTime, + ProposalBlockHash: rs.ProposalBlock.Hash(), + LockedBlockHash: rs.LockedBlock.Hash(), + ValidBlockHash: rs.ValidBlock.Hash(), + Votes: votesJSON, + } +} + // RoundStateEvent returns the H/R/S of the RoundState as an event. func (rs *RoundState) RoundStateEvent() types.EventDataRoundState { // XXX: copy the RoundState diff --git a/rpc/core/consensus.go b/rpc/core/consensus.go index a747c600a..2cc26e52f 100644 --- a/rpc/core/consensus.go +++ b/rpc/core/consensus.go @@ -211,3 +211,10 @@ func DumpConsensusState() (*ctypes.ResultDumpConsensusState, error) { } return &ctypes.ResultDumpConsensusState{roundState, peerStates}, nil } + +// UNSTABLE +func ConsensusState() (*ctypes.ResultConsensusState, error) { + // Get self round state. + bz, err := consensusState.GetRoundStateSimpleJSON() + return &ctypes.ResultConsensusState{bz}, err +} diff --git a/rpc/core/pipe.go b/rpc/core/pipe.go index 0bbead943..e93ba2f80 100644 --- a/rpc/core/pipe.go +++ b/rpc/core/pipe.go @@ -23,6 +23,7 @@ type Consensus interface { GetState() sm.State GetValidators() (int64, []*types.Validator) GetRoundStateJSON() ([]byte, error) + GetRoundStateSimpleJSON() ([]byte, error) } type P2P interface { diff --git a/rpc/core/routes.go b/rpc/core/routes.go index be3881c36..bf90d6fbd 100644 --- a/rpc/core/routes.go +++ b/rpc/core/routes.go @@ -25,6 +25,7 @@ var Routes = map[string]*rpc.RPCFunc{ "tx_search": rpc.NewRPCFunc(TxSearch, "query,prove"), "validators": rpc.NewRPCFunc(Validators, "height"), "dump_consensus_state": rpc.NewRPCFunc(DumpConsensusState, ""), + "consensus_state": rpc.NewRPCFunc(ConsensusState, ""), "unconfirmed_txs": rpc.NewRPCFunc(UnconfirmedTxs, ""), "num_unconfirmed_txs": rpc.NewRPCFunc(NumUnconfirmedTxs, ""), diff --git a/rpc/core/types/responses.go b/rpc/core/types/responses.go index 41d765bf9..18c545453 100644 --- a/rpc/core/types/responses.go +++ b/rpc/core/types/responses.go @@ -140,6 +140,11 @@ type PeerStateInfo struct { PeerState json.RawMessage `json:"peer_state"` } +// UNSTABLE +type ResultConsensusState struct { + RoundState json.RawMessage `json:"round_state"` +} + // CheckTx result type ResultBroadcastTx struct { Code uint32 `json:"code"`