Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 18 additions & 14 deletions sei-tendermint/rpc/coretypes/responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,32 +129,36 @@ type ValidatorInfo struct {
VotingPower int64
}

// CosmJS compatible response, which contains a public key = 0 in case of non-validator nodes.
type validatorInfoJSON struct {
Address bytes.HexBytes `json:"address"`
PubKey json.RawMessage `json:"pub_key"`
VotingPower int64 `json:"voting_power,string"`
}

func (v ValidatorInfo) MarshalJSON() ([]byte, error) {
j := validatorInfoJSON{VotingPower: v.VotingPower}
if k, ok := v.PubKey.Get(); ok {
pk, err := jsontypes.Marshal(k)
if err != nil {
return nil, err
}
j.PubKey = pk
j.Address = k.Address()
k := v.PubKey.Or(crypto.PubKey{})
pk, err := jsontypes.Marshal(k)
if err != nil {
return nil, err
}
return json.Marshal(j)
return json.Marshal(validatorInfoJSON{
VotingPower: v.VotingPower,
PubKey: pk,
Address: k.Address(),
})
}

func (v *ValidatorInfo) UnmarshalJSON(data []byte) error {
var val validatorInfoJSON
if len(val.PubKey) != 0 {
var pk crypto.PubKey
if err := jsontypes.Unmarshal(val.PubKey, &pk); err != nil {
return err
}
if err := json.Unmarshal(data, &val); err != nil {
return err
}
var pk crypto.PubKey
if err := jsontypes.Unmarshal(val.PubKey, &pk); err != nil {
return err
}
if pk != (crypto.PubKey{}) {
v.PubKey = utils.Some(pk)
}
v.VotingPower = val.VotingPower
Expand Down
27 changes: 27 additions & 0 deletions sei-tendermint/rpc/coretypes/responses_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import (
"github.com/stretchr/testify/require"

abci "github.com/sei-protocol/sei-chain/sei-tendermint/abci/types"
"github.com/sei-protocol/sei-chain/sei-tendermint/crypto"
"github.com/sei-protocol/sei-chain/sei-tendermint/crypto/ed25519"
"github.com/sei-protocol/sei-chain/sei-tendermint/libs/utils"
pbcrypto "github.com/sei-protocol/sei-chain/sei-tendermint/proto/tendermint/crypto"
"github.com/sei-protocol/sei-chain/sei-tendermint/types"
)
Expand Down Expand Up @@ -91,3 +94,27 @@ func TestResultBlockResults_regression8583(t *testing.T) {
t.Errorf("Unmarshaled result (-want, +got):\n%s", diff)
}
}

func TestValidatorInfoJSONReencoding(t *testing.T) {
cases := map[string]ValidatorInfo{
"non-validator": {
PubKey: utils.None[crypto.PubKey](),
VotingPower: 0,
},
"validator": {
PubKey: utils.Some(ed25519.TestSecretKey([]byte("validator-info-json")).Public()),
VotingPower: 123,
},
}

for name, want := range cases {
t.Run(name, func(t *testing.T) {
raw, err := json.Marshal(want)
require.NoError(t, err)

var got ValidatorInfo
require.NoError(t, json.Unmarshal(raw, &got))
require.NoError(t, utils.TestDiff(want, got))
})
}
}
Loading