mirror of
https://github.com/tendermint/tendermint.git
synced 2026-08-20 06:06:15 +00:00
rpc: simplify the encoding of interface-typed arguments in JSON (#7600)
Add package jsontypes that implements a subset of the custom libs/json package. Specifically it handles encoding and decoding of interface types wrapped in "tagged" JSON objects. It omits the deep reflection on arbitrary types, preserving only the handling of type tags wrapper encoding. - Register interface types (Evidence, PubKey, PrivKey) for tagged encoding. - Update the existing implementations to satisfy the type. - Register those types with the jsontypes registry. - Add string tags to 64-bit integer fields where needed. - Add marshalers to structs that export interface-typed fields.
This commit is contained in:
@@ -510,7 +510,9 @@ func (c *baseRPCClient) BroadcastEvidence(
|
||||
ev types.Evidence,
|
||||
) (*coretypes.ResultBroadcastEvidence, error) {
|
||||
result := new(coretypes.ResultBroadcastEvidence)
|
||||
if err := c.caller.Call(ctx, "broadcast_evidence", map[string]interface{}{"evidence": ev}, result); err != nil {
|
||||
if err := c.caller.Call(ctx, "broadcast_evidence", evidenceArgs{
|
||||
Evidence: ev,
|
||||
}, result); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return result, nil
|
||||
|
||||
@@ -4,7 +4,11 @@ package http
|
||||
// from the client to the server.
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"github.com/tendermint/tendermint/internal/jsontypes"
|
||||
"github.com/tendermint/tendermint/libs/bytes"
|
||||
"github.com/tendermint/tendermint/types"
|
||||
)
|
||||
|
||||
type abciQueryArgs struct {
|
||||
@@ -57,3 +61,19 @@ type validatorArgs struct {
|
||||
Page *int `json:"page,string,omitempty"`
|
||||
PerPage *int `json:"per_page,string,omitempty"`
|
||||
}
|
||||
|
||||
type evidenceArgs struct {
|
||||
Evidence types.Evidence
|
||||
}
|
||||
|
||||
// MarshalJSON implements json.Marshaler to encode the evidence using the
|
||||
// wrapped concrete type of the implementation.
|
||||
func (e evidenceArgs) MarshalJSON() ([]byte, error) {
|
||||
ev, err := jsontypes.Marshal(e.Evidence)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return json.Marshal(struct {
|
||||
Evidence json.RawMessage `json:"evidence"`
|
||||
}{Evidence: ev})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user