Skip to content

Commit

Permalink
refactor: add missing files to api dir
Browse files Browse the repository at this point in the history
  • Loading branch information
christopherbrumm committed Sep 4, 2024
1 parent 7e74a7a commit 05ba4e5
Show file tree
Hide file tree
Showing 11 changed files with 727 additions and 0 deletions.
121 changes: 121 additions & 0 deletions api/cometbft/abci/v1/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package v1

import (
"bytes"

"github.com/cosmos/gogoproto/jsonpb"
)

const (
CodeTypeOK uint32 = 0
)

// IsOK returns true if Code is OK.
func (r CheckTxResponse) IsOK() bool {
return r.Code == CodeTypeOK
}

// IsErr returns true if Code is something other than OK.
func (r CheckTxResponse) IsErr() bool {
return r.Code != CodeTypeOK
}

// IsOK returns true if Code is OK.
func (r QueryResponse) IsOK() bool {
return r.Code == CodeTypeOK
}

// IsErr returns true if Code is something other than OK.
func (r QueryResponse) IsErr() bool {
return r.Code != CodeTypeOK
}

// IsAccepted returns true if Code is ACCEPT
func (r ProcessProposalResponse) IsAccepted() bool {
return r.Status == PROCESS_PROPOSAL_STATUS_ACCEPT
}

// IsStatusUnknown returns true if Code is UNKNOWN
func (r ProcessProposalResponse) IsStatusUnknown() bool {
return r.Status == PROCESS_PROPOSAL_STATUS_UNKNOWN
}

func (r VerifyVoteExtensionResponse) IsAccepted() bool {
return r.Status == VERIFY_VOTE_EXTENSION_STATUS_ACCEPT
}

// IsStatusUnknown returns true if Code is Unknown
func (r VerifyVoteExtensionResponse) IsStatusUnknown() bool {
return r.Status == VERIFY_VOTE_EXTENSION_STATUS_UNKNOWN
}

// IsOK returns true if Code is OK.
func (r ExecTxResult) IsOK() bool {
return r.Code == CodeTypeOK
}

// IsErr returns true if Code is something other than OK.
func (r ExecTxResult) IsErr() bool {
return r.Code != CodeTypeOK
}

// ---------------------------------------------------------------------------
// override JSON marshaling so we emit defaults (ie. disable omitempty)

var (
jsonpbMarshaller = jsonpb.Marshaler{
EnumsAsInts: true,
EmitDefaults: true,
}
jsonpbUnmarshaller = jsonpb.Unmarshaler{}
)

func (r *CheckTxResponse) MarshalJSON() ([]byte, error) {
s, err := jsonpbMarshaller.MarshalToString(r)
return []byte(s), err
}

func (r *CheckTxResponse) UnmarshalJSON(b []byte) error {
reader := bytes.NewBuffer(b)
return jsonpbUnmarshaller.Unmarshal(reader, r)
}

func (r *QueryResponse) MarshalJSON() ([]byte, error) {
s, err := jsonpbMarshaller.MarshalToString(r)
return []byte(s), err
}

func (r *QueryResponse) UnmarshalJSON(b []byte) error {
reader := bytes.NewBuffer(b)
return jsonpbUnmarshaller.Unmarshal(reader, r)
}

func (r *CommitResponse) MarshalJSON() ([]byte, error) {
s, err := jsonpbMarshaller.MarshalToString(r)
return []byte(s), err
}

func (r *CommitResponse) UnmarshalJSON(b []byte) error {
reader := bytes.NewBuffer(b)
return jsonpbUnmarshaller.Unmarshal(reader, r)
}

func (r *EventAttribute) MarshalJSON() ([]byte, error) {
s, err := jsonpbMarshaller.MarshalToString(r)
return []byte(s), err
}

func (r *EventAttribute) UnmarshalJSON(b []byte) error {
reader := bytes.NewBuffer(b)
return jsonpbUnmarshaller.Unmarshal(reader, r)
}

func (r *ExecTxResult) MarshalJSON() ([]byte, error) {
s, err := jsonpbMarshaller.MarshalToString(r)
return []byte(s), err
}

func (r *ExecTxResult) UnmarshalJSON(b []byte) error {
reader := bytes.NewBuffer(b)
return jsonpbUnmarshaller.Unmarshal(reader, r)
}
42 changes: 42 additions & 0 deletions api/cometbft/abci/v1beta1/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package v1beta1

import (
"bytes"

"github.com/cosmos/gogoproto/jsonpb"
)

const (
CodeTypeOK uint32 = 0
)

// IsOK returns true if Code is OK.
func (r ResponseQuery) IsOK() bool {
return r.Code == CodeTypeOK
}

// IsErr returns true if Code is something other than OK.
func (r ResponseQuery) IsErr() bool {
return r.Code != CodeTypeOK
}

// ---------------------------------------------------------------------------
// override JSON marshaling so we emit defaults (ie. disable omitempty)

var (
jsonpbMarshaller = jsonpb.Marshaler{
EnumsAsInts: true,
EmitDefaults: true,
}
jsonpbUnmarshaller = jsonpb.Unmarshaler{}
)

func (r *ResponseQuery) MarshalJSON() ([]byte, error) {
s, err := jsonpbMarshaller.MarshalToString(r)
return []byte(s), err
}

func (r *ResponseQuery) UnmarshalJSON(b []byte) error {
reader := bytes.NewBuffer(b)
return jsonpbUnmarshaller.Unmarshal(reader, r)
}
38 changes: 38 additions & 0 deletions api/cometbft/abci/v1beta2/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package v1beta2

import (
"bytes"

"github.com/cosmos/gogoproto/jsonpb"
)

// IsAccepted returns true if Code is ACCEPT
func (r ResponseProcessProposal) IsAccepted() bool {
return r.Status == ResponseProcessProposal_ACCEPT
}

// IsStatusUnknown returns true if Code is UNKNOWN
func (r ResponseProcessProposal) IsStatusUnknown() bool {
return r.Status == ResponseProcessProposal_UNKNOWN
}

// ---------------------------------------------------------------------------
// override JSON marshaling so we emit defaults (ie. disable omitempty)

var (
jsonpbMarshaller = jsonpb.Marshaler{
EnumsAsInts: true,
EmitDefaults: true,
}
jsonpbUnmarshaller = jsonpb.Unmarshaler{}
)

func (r *EventAttribute) MarshalJSON() ([]byte, error) {
s, err := jsonpbMarshaller.MarshalToString(r)
return []byte(s), err
}

func (r *EventAttribute) UnmarshalJSON(b []byte) error {
reader := bytes.NewBuffer(b)
return jsonpbUnmarshaller.Unmarshal(reader, r)
}
81 changes: 81 additions & 0 deletions api/cometbft/abci/v1beta3/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package v1beta3

import (
"bytes"

"github.com/cosmos/gogoproto/jsonpb"
)

const (
CodeTypeOK uint32 = 0
)

// IsOK returns true if Code is OK.
func (r ResponseCheckTx) IsOK() bool {
return r.Code == CodeTypeOK
}

// IsErr returns true if Code is something other than OK.
func (r ResponseCheckTx) IsErr() bool {
return r.Code != CodeTypeOK
}

// IsOK returns true if Code is OK.
func (r ExecTxResult) IsOK() bool {
return r.Code == CodeTypeOK
}

// IsErr returns true if Code is something other than OK.
func (r ExecTxResult) IsErr() bool {
return r.Code != CodeTypeOK
}

func (r ResponseVerifyVoteExtension) IsAccepted() bool {
return r.Status == ResponseVerifyVoteExtension_ACCEPT
}

// IsStatusUnknown returns true if Code is Unknown
func (r ResponseVerifyVoteExtension) IsStatusUnknown() bool {
return r.Status == ResponseVerifyVoteExtension_UNKNOWN
}

// ---------------------------------------------------------------------------
// override JSON marshaling so we emit defaults (ie. disable omitempty)

var (
jsonpbMarshaller = jsonpb.Marshaler{
EnumsAsInts: true,
EmitDefaults: true,
}
jsonpbUnmarshaller = jsonpb.Unmarshaler{}
)

func (r *ResponseCheckTx) MarshalJSON() ([]byte, error) {
s, err := jsonpbMarshaller.MarshalToString(r)
return []byte(s), err
}

func (r *ResponseCheckTx) UnmarshalJSON(b []byte) error {
reader := bytes.NewBuffer(b)
return jsonpbUnmarshaller.Unmarshal(reader, r)
}

func (r *ResponseCommit) MarshalJSON() ([]byte, error) {
s, err := jsonpbMarshaller.MarshalToString(r)
return []byte(s), err
}

func (r *ResponseCommit) UnmarshalJSON(b []byte) error {
reader := bytes.NewBuffer(b)
return jsonpbUnmarshaller.Unmarshal(reader, r)
}

func (r *ExecTxResult) MarshalJSON() ([]byte, error) {
s, err := jsonpbMarshaller.MarshalToString(r)
return []byte(s), err
}

func (r *ExecTxResult) UnmarshalJSON(b []byte) error {
reader := bytes.NewBuffer(b)
return jsonpbUnmarshaller.Unmarshal(reader, r)
}
66 changes: 66 additions & 0 deletions api/cometbft/blocksync/v1/message.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package v1

import (
"fmt"

"github.com/cosmos/gogoproto/proto"
)

const (
BlockResponseMessagePrefixSize = 4
BlockResponseMessageFieldKeySize = 1
)

func (m *BlockRequest) Wrap() proto.Message {
bm := &Message{}
bm.Sum = &Message_BlockRequest{BlockRequest: m}
return bm
}

func (m *BlockResponse) Wrap() proto.Message {
bm := &Message{}
bm.Sum = &Message_BlockResponse{BlockResponse: m}
return bm
}

func (m *NoBlockResponse) Wrap() proto.Message {
bm := &Message{}
bm.Sum = &Message_NoBlockResponse{NoBlockResponse: m}
return bm
}

func (m *StatusRequest) Wrap() proto.Message {
bm := &Message{}
bm.Sum = &Message_StatusRequest{StatusRequest: m}
return bm
}

func (m *StatusResponse) Wrap() proto.Message {
bm := &Message{}
bm.Sum = &Message_StatusResponse{StatusResponse: m}
return bm
}

// Unwrap implements the p2p Wrapper interface and unwraps a wrapped blockchain
// message.
func (m *Message) Unwrap() (proto.Message, error) {
switch msg := m.Sum.(type) {
case *Message_BlockRequest:
return m.GetBlockRequest(), nil

case *Message_BlockResponse:
return m.GetBlockResponse(), nil

case *Message_NoBlockResponse:
return m.GetNoBlockResponse(), nil

case *Message_StatusRequest:
return m.GetStatusRequest(), nil

case *Message_StatusResponse:
return m.GetStatusResponse(), nil

default:
return nil, fmt.Errorf("unknown message: %T", msg)
}
}
Loading

0 comments on commit 05ba4e5

Please sign in to comment.