mirror of
https://github.com/tendermint/tendermint.git
synced 2026-09-19 22:44:24 +00:00
Merge remote-tracking branch 'origin/master' into wb/pbts-rebase-master
This commit is contained in:
+50
-16
@@ -6,9 +6,9 @@ import (
|
||||
"strings"
|
||||
|
||||
abci "github.com/tendermint/tendermint/abci/types"
|
||||
"github.com/tendermint/tendermint/internal/jsontypes"
|
||||
tmpubsub "github.com/tendermint/tendermint/internal/pubsub"
|
||||
tmquery "github.com/tendermint/tendermint/internal/pubsub/query"
|
||||
tmjson "github.com/tendermint/tendermint/libs/json"
|
||||
)
|
||||
|
||||
// Reserved event types (alphabetically sorted).
|
||||
@@ -89,23 +89,21 @@ var (
|
||||
// ENCODING / DECODING
|
||||
|
||||
// TMEventData implements events.EventData.
|
||||
type TMEventData interface {
|
||||
// empty interface
|
||||
}
|
||||
type TMEventData interface{}
|
||||
|
||||
func init() {
|
||||
tmjson.RegisterType(EventDataNewBlock{}, "tendermint/event/NewBlock")
|
||||
tmjson.RegisterType(EventDataNewBlockHeader{}, "tendermint/event/NewBlockHeader")
|
||||
tmjson.RegisterType(EventDataNewEvidence{}, "tendermint/event/NewEvidence")
|
||||
tmjson.RegisterType(EventDataTx{}, "tendermint/event/Tx")
|
||||
tmjson.RegisterType(EventDataRoundState{}, "tendermint/event/RoundState")
|
||||
tmjson.RegisterType(EventDataNewRound{}, "tendermint/event/NewRound")
|
||||
tmjson.RegisterType(EventDataCompleteProposal{}, "tendermint/event/CompleteProposal")
|
||||
tmjson.RegisterType(EventDataVote{}, "tendermint/event/Vote")
|
||||
tmjson.RegisterType(EventDataValidatorSetUpdates{}, "tendermint/event/ValidatorSetUpdates")
|
||||
tmjson.RegisterType(EventDataString(""), "tendermint/event/ProposalString")
|
||||
tmjson.RegisterType(EventDataBlockSyncStatus{}, "tendermint/event/FastSyncStatus")
|
||||
tmjson.RegisterType(EventDataStateSyncStatus{}, "tendermint/event/StateSyncStatus")
|
||||
jsontypes.MustRegister(EventDataBlockSyncStatus{})
|
||||
jsontypes.MustRegister(EventDataCompleteProposal{})
|
||||
jsontypes.MustRegister(EventDataNewBlock{})
|
||||
jsontypes.MustRegister(EventDataNewBlockHeader{})
|
||||
jsontypes.MustRegister(EventDataNewEvidence{})
|
||||
jsontypes.MustRegister(EventDataNewRound{})
|
||||
jsontypes.MustRegister(EventDataRoundState{})
|
||||
jsontypes.MustRegister(EventDataStateSyncStatus{})
|
||||
jsontypes.MustRegister(EventDataTx{})
|
||||
jsontypes.MustRegister(EventDataValidatorSetUpdates{})
|
||||
jsontypes.MustRegister(EventDataVote{})
|
||||
jsontypes.MustRegister(EventDataString(""))
|
||||
}
|
||||
|
||||
// Most event messages are basic types (a block, a transaction)
|
||||
@@ -119,6 +117,9 @@ type EventDataNewBlock struct {
|
||||
ResultEndBlock abci.ResponseEndBlock `json:"result_end_block"`
|
||||
}
|
||||
|
||||
// TypeTag implements the required method of jsontypes.Tagged.
|
||||
func (EventDataNewBlock) TypeTag() string { return "tendermint/event/NewBlock" }
|
||||
|
||||
type EventDataNewBlockHeader struct {
|
||||
Header Header `json:"header"`
|
||||
|
||||
@@ -127,17 +128,26 @@ type EventDataNewBlockHeader struct {
|
||||
ResultEndBlock abci.ResponseEndBlock `json:"result_end_block"`
|
||||
}
|
||||
|
||||
// TypeTag implements the required method of jsontypes.Tagged.
|
||||
func (EventDataNewBlockHeader) TypeTag() string { return "tendermint/event/NewBlockHeader" }
|
||||
|
||||
type EventDataNewEvidence struct {
|
||||
Evidence Evidence `json:"evidence"`
|
||||
|
||||
Height int64 `json:"height"`
|
||||
}
|
||||
|
||||
// TypeTag implements the required method of jsontypes.Tagged.
|
||||
func (EventDataNewEvidence) TypeTag() string { return "tendermint/event/NewEvidence" }
|
||||
|
||||
// All txs fire EventDataTx
|
||||
type EventDataTx struct {
|
||||
abci.TxResult
|
||||
}
|
||||
|
||||
// TypeTag implements the required method of jsontypes.Tagged.
|
||||
func (EventDataTx) TypeTag() string { return "tendermint/event/Tx" }
|
||||
|
||||
// NOTE: This goes into the replay WAL
|
||||
type EventDataRoundState struct {
|
||||
Height int64 `json:"height"`
|
||||
@@ -145,6 +155,9 @@ type EventDataRoundState struct {
|
||||
Step string `json:"step"`
|
||||
}
|
||||
|
||||
// TypeTag implements the required method of jsontypes.Tagged.
|
||||
func (EventDataRoundState) TypeTag() string { return "tendermint/event/RoundState" }
|
||||
|
||||
type ValidatorInfo struct {
|
||||
Address Address `json:"address"`
|
||||
Index int32 `json:"index"`
|
||||
@@ -158,6 +171,9 @@ type EventDataNewRound struct {
|
||||
Proposer ValidatorInfo `json:"proposer"`
|
||||
}
|
||||
|
||||
// TypeTag implements the required method of jsontypes.Tagged.
|
||||
func (EventDataNewRound) TypeTag() string { return "tendermint/event/NewRound" }
|
||||
|
||||
type EventDataCompleteProposal struct {
|
||||
Height int64 `json:"height"`
|
||||
Round int32 `json:"round"`
|
||||
@@ -166,16 +182,28 @@ type EventDataCompleteProposal struct {
|
||||
BlockID BlockID `json:"block_id"`
|
||||
}
|
||||
|
||||
// TypeTag implements the required method of jsontypes.Tagged.
|
||||
func (EventDataCompleteProposal) TypeTag() string { return "tendermint/event/CompleteProposal" }
|
||||
|
||||
type EventDataVote struct {
|
||||
Vote *Vote
|
||||
}
|
||||
|
||||
// TypeTag implements the required method of jsontypes.Tagged.
|
||||
func (EventDataVote) TypeTag() string { return "tendermint/event/Vote" }
|
||||
|
||||
type EventDataString string
|
||||
|
||||
// TypeTag implements the required method of jsontypes.Tagged.
|
||||
func (EventDataString) TypeTag() string { return "tendermint/event/ProposalString" }
|
||||
|
||||
type EventDataValidatorSetUpdates struct {
|
||||
ValidatorUpdates []*Validator `json:"validator_updates"`
|
||||
}
|
||||
|
||||
// TypeTag implements the required method of jsontypes.Tagged.
|
||||
func (EventDataValidatorSetUpdates) TypeTag() string { return "tendermint/event/ValidatorSetUpdates" }
|
||||
|
||||
// EventDataBlockSyncStatus shows the fastsync status and the
|
||||
// height when the node state sync mechanism changes.
|
||||
type EventDataBlockSyncStatus struct {
|
||||
@@ -183,6 +211,9 @@ type EventDataBlockSyncStatus struct {
|
||||
Height int64 `json:"height"`
|
||||
}
|
||||
|
||||
// TypeTag implements the required method of jsontypes.Tagged.
|
||||
func (EventDataBlockSyncStatus) TypeTag() string { return "tendermint/event/FastSyncStatus" }
|
||||
|
||||
// EventDataStateSyncStatus shows the statesync status and the
|
||||
// height when the node state sync mechanism changes.
|
||||
type EventDataStateSyncStatus struct {
|
||||
@@ -190,6 +221,9 @@ type EventDataStateSyncStatus struct {
|
||||
Height int64 `json:"height"`
|
||||
}
|
||||
|
||||
// TypeTag implements the required method of jsontypes.Tagged.
|
||||
func (EventDataStateSyncStatus) TypeTag() string { return "tendermint/event/StateSyncStatus" }
|
||||
|
||||
// PUBSUB
|
||||
|
||||
const (
|
||||
|
||||
+28
-4
@@ -4,6 +4,7 @@ import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/binary"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"sort"
|
||||
@@ -14,7 +15,6 @@ import (
|
||||
"github.com/tendermint/tendermint/crypto/merkle"
|
||||
"github.com/tendermint/tendermint/crypto/tmhash"
|
||||
"github.com/tendermint/tendermint/internal/jsontypes"
|
||||
tmjson "github.com/tendermint/tendermint/libs/json"
|
||||
tmrand "github.com/tendermint/tendermint/libs/rand"
|
||||
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
|
||||
)
|
||||
@@ -554,6 +554,33 @@ func LightClientAttackEvidenceFromProto(lpb *tmproto.LightClientAttackEvidence)
|
||||
// EvidenceList is a list of Evidence. Evidences is not a word.
|
||||
type EvidenceList []Evidence
|
||||
|
||||
func (evl EvidenceList) MarshalJSON() ([]byte, error) {
|
||||
lst := make([]json.RawMessage, len(evl))
|
||||
for i, ev := range evl {
|
||||
bits, err := jsontypes.Marshal(ev)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
lst[i] = bits
|
||||
}
|
||||
return json.Marshal(lst)
|
||||
}
|
||||
|
||||
func (evl *EvidenceList) UnmarshalJSON(data []byte) error {
|
||||
var lst []json.RawMessage
|
||||
if err := json.Unmarshal(data, &lst); err != nil {
|
||||
return err
|
||||
}
|
||||
out := make([]Evidence, len(lst))
|
||||
for i, elt := range lst {
|
||||
if err := jsontypes.Unmarshal(elt, &out[i]); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
*evl = EvidenceList(out)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Hash returns the simple merkle root hash of the EvidenceList.
|
||||
func (evl EvidenceList) Hash() []byte {
|
||||
// These allocations are required because Evidence is not of type Bytes, and
|
||||
@@ -638,9 +665,6 @@ func EvidenceFromProto(evidence *tmproto.Evidence) (Evidence, error) {
|
||||
}
|
||||
|
||||
func init() {
|
||||
tmjson.RegisterType(&DuplicateVoteEvidence{}, "tendermint/DuplicateVoteEvidence")
|
||||
tmjson.RegisterType(&LightClientAttackEvidence{}, "tendermint/LightClientAttackEvidence")
|
||||
|
||||
jsontypes.MustRegister((*DuplicateVoteEvidence)(nil))
|
||||
jsontypes.MustRegister((*LightClientAttackEvidence)(nil))
|
||||
}
|
||||
|
||||
+31
-14
@@ -11,7 +11,6 @@ import (
|
||||
"github.com/tendermint/tendermint/crypto"
|
||||
"github.com/tendermint/tendermint/internal/jsontypes"
|
||||
tmbytes "github.com/tendermint/tendermint/libs/bytes"
|
||||
tmjson "github.com/tendermint/tendermint/libs/json"
|
||||
tmtime "github.com/tendermint/tendermint/libs/time"
|
||||
)
|
||||
|
||||
@@ -28,10 +27,17 @@ const (
|
||||
|
||||
// GenesisValidator is an initial validator.
|
||||
type GenesisValidator struct {
|
||||
Address Address `json:"address"`
|
||||
PubKey crypto.PubKey `json:"pub_key"`
|
||||
Power int64 `json:"power,string"`
|
||||
Name string `json:"name"`
|
||||
Address Address
|
||||
PubKey crypto.PubKey
|
||||
Power int64
|
||||
Name string
|
||||
}
|
||||
|
||||
type genesisValidatorJSON struct {
|
||||
Address Address `json:"address"`
|
||||
PubKey json.RawMessage `json:"pub_key"`
|
||||
Power int64 `json:"power,string"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
func (g GenesisValidator) MarshalJSON() ([]byte, error) {
|
||||
@@ -39,19 +45,30 @@ func (g GenesisValidator) MarshalJSON() ([]byte, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return json.Marshal(struct {
|
||||
Address Address `json:"address"`
|
||||
PubKey json.RawMessage `json:"pub_key"`
|
||||
Power int64 `json:"power,string"`
|
||||
Name string `json:"name"`
|
||||
}{Address: g.Address, PubKey: pk, Power: g.Power, Name: g.Name})
|
||||
return json.Marshal(genesisValidatorJSON{
|
||||
Address: g.Address, PubKey: pk, Power: g.Power, Name: g.Name,
|
||||
})
|
||||
}
|
||||
|
||||
func (g *GenesisValidator) UnmarshalJSON(data []byte) error {
|
||||
var gv genesisValidatorJSON
|
||||
if err := json.Unmarshal(data, &gv); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := jsontypes.Unmarshal(gv.PubKey, &g.PubKey); err != nil {
|
||||
return err
|
||||
}
|
||||
g.Address = gv.Address
|
||||
g.Power = gv.Power
|
||||
g.Name = gv.Name
|
||||
return nil
|
||||
}
|
||||
|
||||
// GenesisDoc defines the initial conditions for a tendermint blockchain, in particular its validator set.
|
||||
type GenesisDoc struct {
|
||||
GenesisTime time.Time `json:"genesis_time"`
|
||||
ChainID string `json:"chain_id"`
|
||||
InitialHeight int64 `json:"initial_height"`
|
||||
InitialHeight int64 `json:"initial_height,string"`
|
||||
ConsensusParams *ConsensusParams `json:"consensus_params,omitempty"`
|
||||
Validators []GenesisValidator `json:"validators,omitempty"`
|
||||
AppHash tmbytes.HexBytes `json:"app_hash"`
|
||||
@@ -60,7 +77,7 @@ type GenesisDoc struct {
|
||||
|
||||
// SaveAs is a utility method for saving GenensisDoc as a JSON file.
|
||||
func (genDoc *GenesisDoc) SaveAs(file string) error {
|
||||
genDocBytes, err := tmjson.MarshalIndent(genDoc, "", " ")
|
||||
genDocBytes, err := json.MarshalIndent(genDoc, "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -125,7 +142,7 @@ func (genDoc *GenesisDoc) ValidateAndComplete() error {
|
||||
// GenesisDocFromJSON unmarshalls JSON data into a GenesisDoc.
|
||||
func GenesisDocFromJSON(jsonBlob []byte) (*GenesisDoc, error) {
|
||||
genDoc := GenesisDoc{}
|
||||
err := tmjson.Unmarshal(jsonBlob, &genDoc)
|
||||
err := json.Unmarshal(jsonBlob, &genDoc)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package types
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
@@ -8,7 +9,6 @@ import (
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/tendermint/tendermint/crypto/ed25519"
|
||||
tmjson "github.com/tendermint/tendermint/libs/json"
|
||||
tmtime "github.com/tendermint/tendermint/libs/time"
|
||||
)
|
||||
|
||||
@@ -87,7 +87,7 @@ func TestBasicGenesisDoc(t *testing.T) {
|
||||
ChainID: "abc",
|
||||
Validators: []GenesisValidator{{pubkey.Address(), pubkey, 10, "myval"}},
|
||||
}
|
||||
genDocBytes, err = tmjson.Marshal(baseGenDoc)
|
||||
genDocBytes, err = json.Marshal(baseGenDoc)
|
||||
assert.NoError(t, err, "error marshaling genDoc")
|
||||
|
||||
// test base gendoc and check consensus params were filled
|
||||
@@ -99,15 +99,15 @@ func TestBasicGenesisDoc(t *testing.T) {
|
||||
assert.NotNil(t, genDoc.Validators[0].Address, "expected validator's address to be filled in")
|
||||
|
||||
// create json with consensus params filled
|
||||
genDocBytes, err = tmjson.Marshal(genDoc)
|
||||
genDocBytes, err = json.Marshal(genDoc)
|
||||
assert.NoError(t, err, "error marshaling genDoc")
|
||||
genDoc, err = GenesisDocFromJSON(genDocBytes)
|
||||
require.NoError(t, err, "expected no error for valid genDoc json")
|
||||
|
||||
// test with invalid consensus params
|
||||
genDoc.ConsensusParams.Block.MaxBytes = 0
|
||||
genDocBytes, err = tmjson.Marshal(genDoc)
|
||||
require.NoError(t, err, "error marshaling genDoc")
|
||||
genDocBytes, err = json.Marshal(genDoc)
|
||||
assert.NoError(t, err, "error marshaling genDoc")
|
||||
_, err = GenesisDocFromJSON(genDocBytes)
|
||||
assert.Error(t, err, "expected error for genDoc json with block size of 0")
|
||||
|
||||
|
||||
+3
-3
@@ -24,9 +24,9 @@ func MaxNodeInfoSize() int {
|
||||
|
||||
// ProtocolVersion contains the protocol versions for the software.
|
||||
type ProtocolVersion struct {
|
||||
P2P uint64 `json:"p2p"`
|
||||
Block uint64 `json:"block"`
|
||||
App uint64 `json:"app"`
|
||||
P2P uint64 `json:"p2p,string"`
|
||||
Block uint64 `json:"block,string"`
|
||||
App uint64 `json:"app,string"`
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------
|
||||
|
||||
+25
-9
@@ -7,7 +7,6 @@ import (
|
||||
"github.com/tendermint/tendermint/crypto"
|
||||
"github.com/tendermint/tendermint/crypto/ed25519"
|
||||
"github.com/tendermint/tendermint/internal/jsontypes"
|
||||
tmjson "github.com/tendermint/tendermint/libs/json"
|
||||
tmos "github.com/tendermint/tendermint/libs/os"
|
||||
)
|
||||
|
||||
@@ -19,9 +18,14 @@ import (
|
||||
// It contains the nodes private key for authentication.
|
||||
type NodeKey struct {
|
||||
// Canonical ID - hex-encoded pubkey's address (IDByteLength bytes)
|
||||
ID NodeID `json:"id"`
|
||||
ID NodeID
|
||||
// Private key
|
||||
PrivKey crypto.PrivKey `json:"priv_key"`
|
||||
PrivKey crypto.PrivKey
|
||||
}
|
||||
|
||||
type nodeKeyJSON struct {
|
||||
ID NodeID `json:"id"`
|
||||
PrivKey json.RawMessage `json:"priv_key"`
|
||||
}
|
||||
|
||||
func (nk NodeKey) MarshalJSON() ([]byte, error) {
|
||||
@@ -29,10 +33,22 @@ func (nk NodeKey) MarshalJSON() ([]byte, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return json.Marshal(struct {
|
||||
ID NodeID `json:"id"`
|
||||
PrivKey json.RawMessage `json:"priv_key"`
|
||||
}{ID: nk.ID, PrivKey: pk})
|
||||
return json.Marshal(nodeKeyJSON{
|
||||
ID: nk.ID, PrivKey: pk,
|
||||
})
|
||||
}
|
||||
|
||||
func (nk *NodeKey) UnmarshalJSON(data []byte) error {
|
||||
var nkjson nodeKeyJSON
|
||||
if err := json.Unmarshal(data, &nkjson); err != nil {
|
||||
return err
|
||||
}
|
||||
var pk crypto.PrivKey
|
||||
if err := jsontypes.Unmarshal(nkjson.PrivKey, &pk); err != nil {
|
||||
return err
|
||||
}
|
||||
*nk = NodeKey{ID: nkjson.ID, PrivKey: pk}
|
||||
return nil
|
||||
}
|
||||
|
||||
// PubKey returns the peer's PubKey
|
||||
@@ -42,7 +58,7 @@ func (nk NodeKey) PubKey() crypto.PubKey {
|
||||
|
||||
// SaveAs persists the NodeKey to filePath.
|
||||
func (nk NodeKey) SaveAs(filePath string) error {
|
||||
jsonBytes, err := tmjson.Marshal(nk)
|
||||
jsonBytes, err := json.Marshal(nk)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -85,7 +101,7 @@ func LoadNodeKey(filePath string) (NodeKey, error) {
|
||||
return NodeKey{}, err
|
||||
}
|
||||
nodeKey := NodeKey{}
|
||||
err = tmjson.Unmarshal(jsonBytes, &nodeKey)
|
||||
err = json.Unmarshal(jsonBytes, &nodeKey)
|
||||
if err != nil {
|
||||
return NodeKey{}, err
|
||||
}
|
||||
|
||||
+8
-8
@@ -55,15 +55,15 @@ type HashedParams struct {
|
||||
// BlockParams define limits on the block size and gas plus minimum time
|
||||
// between blocks.
|
||||
type BlockParams struct {
|
||||
MaxBytes int64 `json:"max_bytes"`
|
||||
MaxGas int64 `json:"max_gas"`
|
||||
MaxBytes int64 `json:"max_bytes,string"`
|
||||
MaxGas int64 `json:"max_gas,string"`
|
||||
}
|
||||
|
||||
// EvidenceParams determine how we handle evidence of malfeasance.
|
||||
type EvidenceParams struct {
|
||||
MaxAgeNumBlocks int64 `json:"max_age_num_blocks"` // only accept new evidence more recent than this
|
||||
MaxAgeDuration time.Duration `json:"max_age_duration"`
|
||||
MaxBytes int64 `json:"max_bytes"`
|
||||
MaxAgeNumBlocks int64 `json:"max_age_num_blocks,string"` // only accept new evidence more recent than this
|
||||
MaxAgeDuration time.Duration `json:"max_age_duration,string"`
|
||||
MaxBytes int64 `json:"max_bytes,string"`
|
||||
}
|
||||
|
||||
// ValidatorParams restrict the public key types validators can use.
|
||||
@@ -73,14 +73,14 @@ type ValidatorParams struct {
|
||||
}
|
||||
|
||||
type VersionParams struct {
|
||||
AppVersion uint64 `json:"app_version"`
|
||||
AppVersion uint64 `json:"app_version,string"`
|
||||
}
|
||||
|
||||
// SynchronyParams influence the validity of block timestamps.
|
||||
// TODO (@wbanfield): add link to proposer-based timestamp spec when completed.
|
||||
type SynchronyParams struct {
|
||||
Precision time.Duration `json:"precision"`
|
||||
MessageDelay time.Duration `json:"message_delay"`
|
||||
Precision time.Duration `json:"precision,string"`
|
||||
MessageDelay time.Duration `json:"message_delay,string"`
|
||||
}
|
||||
|
||||
// DefaultConsensusParams returns a default ConsensusParams.
|
||||
|
||||
+1
-1
@@ -24,7 +24,7 @@ var (
|
||||
// If POLRound >= 0, then BlockID corresponds to the block that is locked in POLRound.
|
||||
type Proposal struct {
|
||||
Type tmproto.SignedMsgType
|
||||
Height int64 `json:"height"`
|
||||
Height int64 `json:"height,string"`
|
||||
Round int32 `json:"round"` // there can not be greater than 2_147_483_647 rounds
|
||||
POLRound int32 `json:"pol_round"` // -1 if null.
|
||||
BlockID BlockID `json:"block_id"`
|
||||
|
||||
+37
-13
@@ -17,23 +17,47 @@ import (
|
||||
// NOTE: The ProposerPriority is not included in Validator.Hash();
|
||||
// make sure to update that method if changes are made here
|
||||
type Validator struct {
|
||||
Address Address `json:"address"`
|
||||
PubKey crypto.PubKey `json:"pub_key"`
|
||||
VotingPower int64 `json:"voting_power,string"`
|
||||
ProposerPriority int64 `json:"proposer_priority,string"`
|
||||
Address Address
|
||||
PubKey crypto.PubKey
|
||||
VotingPower int64
|
||||
ProposerPriority int64
|
||||
}
|
||||
|
||||
type validatorJSON struct {
|
||||
Address Address `json:"address"`
|
||||
PubKey json.RawMessage `json:"pub_key,omitempty"`
|
||||
VotingPower int64 `json:"voting_power,string"`
|
||||
ProposerPriority int64 `json:"proposer_priority,string"`
|
||||
}
|
||||
|
||||
func (v Validator) MarshalJSON() ([]byte, error) {
|
||||
pk, err := jsontypes.Marshal(v.PubKey)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
val := validatorJSON{
|
||||
Address: v.Address,
|
||||
VotingPower: v.VotingPower,
|
||||
ProposerPriority: v.ProposerPriority,
|
||||
}
|
||||
return json.Marshal(struct {
|
||||
Addr Address `json:"address"`
|
||||
PubKey json.RawMessage `json:"pub_key"`
|
||||
Power int64 `json:"voting_power,string"`
|
||||
Priority int64 `json:"proposer_priority,string"`
|
||||
}{Addr: v.Address, PubKey: pk, Power: v.VotingPower, Priority: v.ProposerPriority})
|
||||
if v.PubKey != nil {
|
||||
pk, err := jsontypes.Marshal(v.PubKey)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
val.PubKey = pk
|
||||
}
|
||||
return json.Marshal(val)
|
||||
}
|
||||
|
||||
func (v *Validator) UnmarshalJSON(data []byte) error {
|
||||
var val validatorJSON
|
||||
if err := json.Unmarshal(data, &val); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := jsontypes.Unmarshal(val.PubKey, &v.PubKey); err != nil {
|
||||
return err
|
||||
}
|
||||
v.Address = val.Address
|
||||
v.VotingPower = val.VotingPower
|
||||
v.ProposerPriority = val.ProposerPriority
|
||||
return nil
|
||||
}
|
||||
|
||||
// NewValidator returns a new validator with the given pubkey and voting power.
|
||||
|
||||
Reference in New Issue
Block a user