Skip to content

Commit

Permalink
Delete the custom libs/json (tmjson) package. (tendermint#7673)
Browse files Browse the repository at this point in the history
There are no further uses of this package anywhere in Tendermint.
All the uses in the Cosmos SDK are for types that now work correctly with the
standard encoding/json package.
  • Loading branch information
M. J. Fromberger authored Jan 24, 2022
1 parent f6ebd84 commit 7878ca6
Show file tree
Hide file tree
Showing 20 changed files with 104 additions and 1,216 deletions.
1 change: 1 addition & 0 deletions CHANGELOG_PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Special thanks to external contributors on this release:
- [config] \#7169 `WriteConfigFile` now returns an error. (@tychoish)
- [libs/service] \#7288 Remove SetLogger method on `service.Service` interface. (@tychoish)
- [abci/client] \#7607 Simplify client interface (removes most "async" methods). (@creachadair)
- [libs/json] \#7673 Remove the libs/json (tmjson) library. (@creachadair)

- Blockchain Protocol

Expand Down
8 changes: 4 additions & 4 deletions crypto/encoding/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import (
"github.com/tendermint/tendermint/crypto/ed25519"
"github.com/tendermint/tendermint/crypto/secp256k1"
"github.com/tendermint/tendermint/crypto/sr25519"
"github.com/tendermint/tendermint/libs/json"
"github.com/tendermint/tendermint/internal/jsontypes"
cryptoproto "github.com/tendermint/tendermint/proto/tendermint/crypto"
)

func init() {
json.RegisterType((*cryptoproto.PublicKey)(nil), "tendermint.crypto.PublicKey")
json.RegisterType((*cryptoproto.PublicKey_Ed25519)(nil), "tendermint.crypto.PublicKey_Ed25519")
json.RegisterType((*cryptoproto.PublicKey_Secp256K1)(nil), "tendermint.crypto.PublicKey_Secp256K1")
jsontypes.MustRegister((*cryptoproto.PublicKey)(nil))
jsontypes.MustRegister((*cryptoproto.PublicKey_Ed25519)(nil))
jsontypes.MustRegister((*cryptoproto.PublicKey_Secp256K1)(nil))
}

// PubKeyToProto takes crypto.PubKey and transforms it to a protobuf Pubkey
Expand Down
8 changes: 4 additions & 4 deletions crypto/merkle/proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ const (
// everything. This also affects the generalized proof system as
// well.
type Proof struct {
Total int64 `json:"total"` // Total number of items.
Index int64 `json:"index"` // Index of item to prove.
LeafHash []byte `json:"leaf_hash"` // Hash of item value.
Aunts [][]byte `json:"aunts"` // Hashes from leaf's sibling to a root's child.
Total int64 `json:"total,string"` // Total number of items.
Index int64 `json:"index,string"` // Index of item to prove.
LeafHash []byte `json:"leaf_hash"` // Hash of item value.
Aunts [][]byte `json:"aunts"` // Hashes from leaf's sibling to a root's child.
}

// ProofsFromByteSlices computes inclusion proof for given items.
Expand Down
56 changes: 38 additions & 18 deletions internal/consensus/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"fmt"

cstypes "github.com/tendermint/tendermint/internal/consensus/types"
"github.com/tendermint/tendermint/internal/jsontypes"
"github.com/tendermint/tendermint/libs/bits"
tmjson "github.com/tendermint/tendermint/libs/json"
tmmath "github.com/tendermint/tendermint/libs/math"
tmcons "github.com/tendermint/tendermint/proto/tendermint/consensus"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
Expand All @@ -18,30 +18,34 @@ import (
// converted to a Message via MsgFromProto.
type Message interface {
ValidateBasic() error

jsontypes.Tagged
}

func init() {
tmjson.RegisterType(&NewRoundStepMessage{}, "tendermint/NewRoundStepMessage")
tmjson.RegisterType(&NewValidBlockMessage{}, "tendermint/NewValidBlockMessage")
tmjson.RegisterType(&ProposalMessage{}, "tendermint/Proposal")
tmjson.RegisterType(&ProposalPOLMessage{}, "tendermint/ProposalPOL")
tmjson.RegisterType(&BlockPartMessage{}, "tendermint/BlockPart")
tmjson.RegisterType(&VoteMessage{}, "tendermint/Vote")
tmjson.RegisterType(&HasVoteMessage{}, "tendermint/HasVote")
tmjson.RegisterType(&VoteSetMaj23Message{}, "tendermint/VoteSetMaj23")
tmjson.RegisterType(&VoteSetBitsMessage{}, "tendermint/VoteSetBits")
jsontypes.MustRegister(&NewRoundStepMessage{})
jsontypes.MustRegister(&NewValidBlockMessage{})
jsontypes.MustRegister(&ProposalMessage{})
jsontypes.MustRegister(&ProposalPOLMessage{})
jsontypes.MustRegister(&BlockPartMessage{})
jsontypes.MustRegister(&VoteMessage{})
jsontypes.MustRegister(&HasVoteMessage{})
jsontypes.MustRegister(&VoteSetMaj23Message{})
jsontypes.MustRegister(&VoteSetBitsMessage{})
}

// NewRoundStepMessage is sent for every step taken in the ConsensusState.
// For every height/round/step transition
type NewRoundStepMessage struct {
Height int64
Height int64 `json:",string"`
Round int32
Step cstypes.RoundStepType
SecondsSinceStartTime int64
SecondsSinceStartTime int64 `json:",string"`
LastCommitRound int32
}

func (*NewRoundStepMessage) TypeTag() string { return "tendermint/NewRoundStepMessage" }

// ValidateBasic performs basic validation.
func (m *NewRoundStepMessage) ValidateBasic() error {
if m.Height < 0 {
Expand Down Expand Up @@ -93,13 +97,15 @@ func (m *NewRoundStepMessage) String() string {
// i.e., there is a Proposal for block B and 2/3+ prevotes for the block B in the round r.
// In case the block is also committed, then IsCommit flag is set to true.
type NewValidBlockMessage struct {
Height int64
Height int64 `json:",string"`
Round int32
BlockPartSetHeader types.PartSetHeader
BlockParts *bits.BitArray
IsCommit bool
}

func (*NewValidBlockMessage) TypeTag() string { return "tendermint/NewValidBlockMessage" }

// ValidateBasic performs basic validation.
func (m *NewValidBlockMessage) ValidateBasic() error {
if m.Height < 0 {
Expand Down Expand Up @@ -136,6 +142,8 @@ type ProposalMessage struct {
Proposal *types.Proposal
}

func (*ProposalMessage) TypeTag() string { return "tendermint/Proposal" }

// ValidateBasic performs basic validation.
func (m *ProposalMessage) ValidateBasic() error {
return m.Proposal.ValidateBasic()
Expand All @@ -148,11 +156,13 @@ func (m *ProposalMessage) String() string {

// ProposalPOLMessage is sent when a previous proposal is re-proposed.
type ProposalPOLMessage struct {
Height int64
Height int64 `json:",string"`
ProposalPOLRound int32
ProposalPOL *bits.BitArray
}

func (*ProposalPOLMessage) TypeTag() string { return "tendermint/ProposalPOL" }

// ValidateBasic performs basic validation.
func (m *ProposalPOLMessage) ValidateBasic() error {
if m.Height < 0 {
Expand All @@ -177,11 +187,13 @@ func (m *ProposalPOLMessage) String() string {

// BlockPartMessage is sent when gossipping a piece of the proposed block.
type BlockPartMessage struct {
Height int64
Height int64 `json:",string"`
Round int32
Part *types.Part
}

func (*BlockPartMessage) TypeTag() string { return "tendermint/BlockPart" }

// ValidateBasic performs basic validation.
func (m *BlockPartMessage) ValidateBasic() error {
if m.Height < 0 {
Expand All @@ -206,6 +218,8 @@ type VoteMessage struct {
Vote *types.Vote
}

func (*VoteMessage) TypeTag() string { return "tendermint/Vote" }

// ValidateBasic performs basic validation.
func (m *VoteMessage) ValidateBasic() error {
return m.Vote.ValidateBasic()
Expand All @@ -218,12 +232,14 @@ func (m *VoteMessage) String() string {

// HasVoteMessage is sent to indicate that a particular vote has been received.
type HasVoteMessage struct {
Height int64
Height int64 `json:",string"`
Round int32
Type tmproto.SignedMsgType
Index int32
}

func (*HasVoteMessage) TypeTag() string { return "tendermint/HasVote" }

// ValidateBasic performs basic validation.
func (m *HasVoteMessage) ValidateBasic() error {
if m.Height < 0 {
Expand All @@ -248,12 +264,14 @@ func (m *HasVoteMessage) String() string {

// VoteSetMaj23Message is sent to indicate that a given BlockID has seen +2/3 votes.
type VoteSetMaj23Message struct {
Height int64
Height int64 `json:",string"`
Round int32
Type tmproto.SignedMsgType
BlockID types.BlockID
}

func (*VoteSetMaj23Message) TypeTag() string { return "tendermint/VoteSetMaj23" }

// ValidateBasic performs basic validation.
func (m *VoteSetMaj23Message) ValidateBasic() error {
if m.Height < 0 {
Expand All @@ -280,13 +298,15 @@ func (m *VoteSetMaj23Message) String() string {
// VoteSetBitsMessage is sent to communicate the bit-array of votes seen for the
// BlockID.
type VoteSetBitsMessage struct {
Height int64
Height int64 `json:",string"`
Round int32
Type tmproto.SignedMsgType
BlockID types.BlockID
Votes *bits.BitArray
}

func (*VoteSetBitsMessage) TypeTag() string { return "tendermint/VoteSetBits" }

// ValidateBasic performs basic validation.
func (m *VoteSetBitsMessage) ValidateBasic() error {
if m.Height < 0 {
Expand Down
38 changes: 34 additions & 4 deletions internal/consensus/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/tendermint/tendermint/crypto"
cstypes "github.com/tendermint/tendermint/internal/consensus/types"
"github.com/tendermint/tendermint/internal/eventbus"
"github.com/tendermint/tendermint/internal/jsontypes"
sm "github.com/tendermint/tendermint/internal/state"
tmevents "github.com/tendermint/tendermint/libs/events"
"github.com/tendermint/tendermint/libs/log"
Expand Down Expand Up @@ -46,18 +47,47 @@ var msgQueueSize = 1000

// msgs from the reactor which may update the state
type msgInfo struct {
Msg Message `json:"msg"`
PeerID types.NodeID `json:"peer_key"`
Msg Message
PeerID types.NodeID
}

func (msgInfo) TypeTag() string { return "tendermint/wal/MsgInfo" }

type msgInfoJSON struct {
Msg json.RawMessage `json:"msg"`
PeerID types.NodeID `json:"peer_key"`
}

func (m msgInfo) MarshalJSON() ([]byte, error) {
msg, err := jsontypes.Marshal(m.Msg)
if err != nil {
return nil, err
}
return json.Marshal(msgInfoJSON{Msg: msg, PeerID: m.PeerID})
}

func (m *msgInfo) UnmarshalJSON(data []byte) error {
var msg msgInfoJSON
if err := json.Unmarshal(data, &msg); err != nil {
return err
}
if err := jsontypes.Unmarshal(msg.Msg, &m.Msg); err != nil {
return err
}
m.PeerID = msg.PeerID
return nil
}

// internally generated messages which may update the state
type timeoutInfo struct {
Duration time.Duration `json:"duration"`
Height int64 `json:"height"`
Duration time.Duration `json:"duration,string"`
Height int64 `json:"height,string"`
Round int32 `json:"round"`
Step cstypes.RoundStepType `json:"step"`
}

func (timeoutInfo) TypeTag() string { return "tendermint/wal/TimeoutInfo" }

func (ti *timeoutInfo) String() string {
return fmt.Sprintf("%v ; %d/%d %v", ti.Duration, ti.Height, ti.Round, ti.Step)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/consensus/types/round_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (rs RoundStepType) String() string {
// NOTE: Not thread safe. Should only be manipulated by functions downstream
// of the cs.receiveRoutine
type RoundState struct {
Height int64 `json:"height"` // Height we are working on
Height int64 `json:"height,string"` // Height we are working on
Round int32 `json:"round"`
Step RoundStepType `json:"step"`
StartTime time.Time `json:"start_time"`
Expand Down
12 changes: 7 additions & 5 deletions internal/consensus/wal.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (

"github.com/gogo/protobuf/proto"

"github.com/tendermint/tendermint/internal/jsontypes"
auto "github.com/tendermint/tendermint/internal/libs/autofile"
tmjson "github.com/tendermint/tendermint/libs/json"
"github.com/tendermint/tendermint/libs/log"
tmos "github.com/tendermint/tendermint/libs/os"
"github.com/tendermint/tendermint/libs/service"
Expand Down Expand Up @@ -41,15 +41,17 @@ type TimedWALMessage struct {
// EndHeightMessage marks the end of the given height inside WAL.
// @internal used by scripts/wal2json util.
type EndHeightMessage struct {
Height int64 `json:"height"`
Height int64 `json:"height,string"`
}

func (EndHeightMessage) TypeTag() string { return "tendermint/wal/EndHeightMessage" }

type WALMessage interface{}

func init() {
tmjson.RegisterType(msgInfo{}, "tendermint/wal/MsgInfo")
tmjson.RegisterType(timeoutInfo{}, "tendermint/wal/TimeoutInfo")
tmjson.RegisterType(EndHeightMessage{}, "tendermint/wal/EndHeightMessage")
jsontypes.MustRegister(msgInfo{})
jsontypes.MustRegister(timeoutInfo{})
jsontypes.MustRegister(EndHeightMessage{})
}

//--------------------------------------------------------
Expand Down
Loading

0 comments on commit 7878ca6

Please sign in to comment.