Skip to content

Commit

Permalink
Merge branch 'master' into prathyusha/8571-create-proposal
Browse files Browse the repository at this point in the history
  • Loading branch information
PrathyushaLakkireddy authored Feb 15, 2021
2 parents 0b5dc7d + a534a96 commit bddc63f
Show file tree
Hide file tree
Showing 8 changed files with 249 additions and 25 deletions.
4 changes: 4 additions & 0 deletions client/grpc/tmservice/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ func (s queryServer) GetLatestValidatorSet(ctx context.Context, req *GetLatestVa
outputValidatorsRes := &GetLatestValidatorSetResponse{
BlockHeight: validatorsRes.BlockHeight,
Validators: make([]*Validator, len(validatorsRes.Validators)),
Pagination: &qtypes.PageResponse{
Total: validatorsRes.Total,
},
}

for i, validator := range validatorsRes.Validators {
Expand Down Expand Up @@ -156,6 +159,7 @@ func (s queryServer) GetValidatorSetByHeight(ctx context.Context, req *GetValida
outputValidatorsRes := &GetValidatorSetByHeightResponse{
BlockHeight: validatorsRes.BlockHeight,
Validators: make([]*Validator, len(validatorsRes.Validators)),
Pagination: &qtypes.PageResponse{Total: validatorsRes.Total},
}

for i, validator := range validatorsRes.Validators {
Expand Down
140 changes: 117 additions & 23 deletions client/grpc/tmservice/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,32 +131,126 @@ func (s IntegrationTestSuite) TestQueryLatestValidatorSet() {
s.Require().Equal(validatorSetRes.Validators[0].PubKey, anyPub)
}

func (s IntegrationTestSuite) TestQueryValidatorSetByHeight() {
val := s.network.Validators[0]

// nil pagination
_, err := s.queryClient.GetValidatorSetByHeight(context.Background(), &tmservice.GetValidatorSetByHeightRequest{
Height: 1,
Pagination: nil,
})
s.Require().NoError(err)
func (s IntegrationTestSuite) TestLatestValidatorSet_GRPC() {
vals := s.network.Validators
testCases := []struct {
name string
req *tmservice.GetLatestValidatorSetRequest
expErr bool
expErrMsg string
}{
{"nil request", nil, true, "cannot be nil"},
{"no pagination", &tmservice.GetLatestValidatorSetRequest{}, false, ""},
{"with pagination", &tmservice.GetLatestValidatorSetRequest{Pagination: &qtypes.PageRequest{Offset: 0, Limit: uint64(len(vals))}}, false, ""},
}
for _, tc := range testCases {
tc := tc
s.Run(tc.name, func() {
grpcRes, err := s.queryClient.GetLatestValidatorSet(context.Background(), tc.req)
if tc.expErr {
s.Require().Error(err)
s.Require().Contains(err.Error(), tc.expErrMsg)
} else {
s.Require().NoError(err)
s.Require().Len(grpcRes.Validators, len(vals))
s.Require().Equal(grpcRes.Pagination.Total, uint64(len(vals)))
content, ok := grpcRes.Validators[0].PubKey.GetCachedValue().(cryptotypes.PubKey)
s.Require().Equal(true, ok)
s.Require().Equal(content, vals[0].PubKey)
}
})
}
}

_, err = s.queryClient.GetValidatorSetByHeight(context.Background(), &tmservice.GetValidatorSetByHeightRequest{
Height: 1,
Pagination: &qtypes.PageRequest{
Offset: 0,
Limit: 10,
}})
s.Require().NoError(err)
func (s IntegrationTestSuite) TestLatestValidatorSet_GRPCGateway() {
vals := s.network.Validators
testCases := []struct {
name string
url string
expErr bool
expErrMsg string
}{
{"no pagination", fmt.Sprintf("%s/cosmos/base/tendermint/v1beta1/validatorsets/latest", vals[0].APIAddress), false, ""},
{"pagination invalid fields", fmt.Sprintf("%s/cosmos/base/tendermint/v1beta1/validatorsets/latest?pagination.offset=-1&pagination.limit=-2", vals[0].APIAddress), true, "strconv.ParseUint"},
{"with pagination", fmt.Sprintf("%s/cosmos/base/tendermint/v1beta1/validatorsets/latest?pagination.offset=0&pagination.limit=2", vals[0].APIAddress), false, ""},
}
for _, tc := range testCases {
tc := tc
s.Run(tc.name, func() {
res, err := rest.GetRequest(tc.url)
s.Require().NoError(err)
if tc.expErr {
s.Require().Contains(string(res), tc.expErrMsg)
} else {
var result tmservice.GetLatestValidatorSetResponse
err = vals[0].ClientCtx.JSONMarshaler.UnmarshalJSON(res, &result)
s.Require().NoError(err)
s.Require().Equal(uint64(len(vals)), result.Pagination.Total)
anyPub, err := codectypes.NewAnyWithValue(vals[0].PubKey)
s.Require().NoError(err)
s.Require().Equal(result.Validators[0].PubKey, anyPub)
}
})
}
}

// no pagination rest
_, err = rest.GetRequest(fmt.Sprintf("%s/cosmos/base/tendermint/v1beta1/validatorsets/%d", val.APIAddress, 1))
s.Require().NoError(err)
func (s IntegrationTestSuite) TestValidatorSetByHeight_GRPC() {
vals := s.network.Validators
testCases := []struct {
name string
req *tmservice.GetValidatorSetByHeightRequest
expErr bool
expErrMsg string
}{
{"nil request", nil, true, "request cannot be nil"},
{"empty request", &tmservice.GetValidatorSetByHeightRequest{}, true, "height must be greater than 0"},
{"no pagination", &tmservice.GetValidatorSetByHeightRequest{Height: 1}, false, ""},
{"with pagination", &tmservice.GetValidatorSetByHeightRequest{Height: 1, Pagination: &qtypes.PageRequest{Offset: 0, Limit: 1}}, false, ""},
}
for _, tc := range testCases {
tc := tc
s.Run(tc.name, func() {
grpcRes, err := s.queryClient.GetValidatorSetByHeight(context.Background(), tc.req)
if tc.expErr {
s.Require().Error(err)
s.Require().Contains(err.Error(), tc.expErrMsg)
} else {
s.Require().NoError(err)
s.Require().Len(grpcRes.Validators, len(vals))
s.Require().Equal(grpcRes.Pagination.Total, uint64(len(vals)))
}
})
}
}

// rest query with pagination
restRes, err := rest.GetRequest(fmt.Sprintf("%s/cosmos/base/tendermint/v1beta1/validatorsets/%d?pagination.offset=%d&pagination.limit=%d", val.APIAddress, 1, 0, 1))
var validatorSetRes tmservice.GetValidatorSetByHeightResponse
s.Require().NoError(val.ClientCtx.JSONMarshaler.UnmarshalJSON(restRes, &validatorSetRes))
func (s IntegrationTestSuite) TestValidatorSetByHeight_GRPCGateway() {
vals := s.network.Validators
testCases := []struct {
name string
url string
expErr bool
expErrMsg string
}{
{"invalid height", fmt.Sprintf("%s/cosmos/base/tendermint/v1beta1/validatorsets/%d", vals[0].APIAddress, -1), true, "height must be greater than 0"},
{"no pagination", fmt.Sprintf("%s/cosmos/base/tendermint/v1beta1/validatorsets/%d", vals[0].APIAddress, 1), false, ""},
{"pagination invalid fields", fmt.Sprintf("%s/cosmos/base/tendermint/v1beta1/validatorsets/%d?pagination.offset=-1&pagination.limit=-2", vals[0].APIAddress, 1), true, "strconv.ParseUint"},
{"with pagination", fmt.Sprintf("%s/cosmos/base/tendermint/v1beta1/validatorsets/%d?pagination.offset=0&pagination.limit=2", vals[0].APIAddress, 1), false, ""},
}
for _, tc := range testCases {
tc := tc
s.Run(tc.name, func() {
res, err := rest.GetRequest(tc.url)
s.Require().NoError(err)
if tc.expErr {
s.Require().Contains(string(res), tc.expErrMsg)
} else {
var result tmservice.GetValidatorSetByHeightResponse
err = vals[0].ClientCtx.JSONMarshaler.UnmarshalJSON(res, &result)
s.Require().NoError(err)
s.Require().Equal(uint64(len(vals)), result.Pagination.Total)
}
})
}
}

func TestIntegrationTestSuite(t *testing.T) {
Expand Down
8 changes: 8 additions & 0 deletions client/rpc/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,14 @@ type ValidatorOutput struct {
type ResultValidatorsOutput struct {
BlockHeight int64 `json:"block_height"`
Validators []ValidatorOutput `json:"validators"`
Total uint64 `json:"total"`
}

func (rvo ResultValidatorsOutput) String() string {
var b strings.Builder

b.WriteString(fmt.Sprintf("block height: %d\n", rvo.BlockHeight))
b.WriteString(fmt.Sprintf("total count: %d\n", rvo.Total))

for _, val := range rvo.Validators {
b.WriteString(
Expand Down Expand Up @@ -129,9 +131,15 @@ func GetValidators(ctx context.Context, clientCtx client.Context, height *int64,
return ResultValidatorsOutput{}, err
}

total := validatorsRes.Total
if validatorsRes.Total < 0 {
total = 0
}

outputValidatorsRes := ResultValidatorsOutput{
BlockHeight: validatorsRes.BlockHeight,
Validators: make([]ValidatorOutput, len(validatorsRes.Validators)),
Total: uint64(total),
}

for i := 0; i < len(validatorsRes.Validators); i++ {
Expand Down
50 changes: 50 additions & 0 deletions types/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,53 @@ func BenchmarkParseCoin(b *testing.B) {
}
}
}

func BenchmarkUintMarshal(b *testing.B) {
var values = []uint64{
0,
1,
1 << 10,
1<<10 - 3,
1<<63 - 1,
1<<32 - 7,
1<<22 - 8,
}

var scratch [20]byte
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for _, value := range values {
u := types.NewUint(value)
n, err := u.MarshalTo(scratch[:])
if err != nil {
b.Fatal(err)
}
b.SetBytes(int64(n))
}
}
}

func BenchmarkIntMarshal(b *testing.B) {
var values = []int64{
0,
1,
1 << 10,
1<<10 - 3,
1<<63 - 1,
1<<32 - 7,
1<<22 - 8,
}

var scratch [20]byte
b.ReportAllocs()
for i := 0; i < b.N; i++ {
for _, value := range values {
in := types.NewInt(value)
n, err := in.MarshalTo(scratch[:])
if err != nil {
b.Fatal(err)
}
b.SetBytes(int64(n))
}
}
}
2 changes: 1 addition & 1 deletion types/int.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ func (i *Int) MarshalTo(data []byte) (n int, err error) {
if i.i == nil {
i.i = new(big.Int)
}
if len(i.i.Bytes()) == 0 {
if i.i.BitLen() == 0 { // The value 0
copy(data, []byte{0x30})
return 1, nil
}
Expand Down
34 changes: 34 additions & 0 deletions types/int_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package types_test

import (
"fmt"
"math/big"
"math/rand"
"strconv"
Expand Down Expand Up @@ -385,3 +386,36 @@ func (s *intTestSuite) TestIntEq() {
_, resp, _, _, _ = sdk.IntEq(s.T(), sdk.OneInt(), sdk.ZeroInt())
s.Require().False(resp)
}

func TestRoundTripMarshalToInt(t *testing.T) {
var values = []int64{
0,
1,
1 << 10,
1<<10 - 3,
1<<63 - 1,
1<<32 - 7,
1<<22 - 8,
}

for _, value := range values {
value := value
t.Run(fmt.Sprintf("%d", value), func(t *testing.T) {
t.Parallel()

var scratch [20]byte
iv := sdk.NewInt(value)
n, err := iv.MarshalTo(scratch[:])
if err != nil {
t.Fatal(err)
}
rt := new(sdk.Int)
if err := rt.Unmarshal(scratch[:n]); err != nil {
t.Fatal(err)
}
if !rt.Equal(iv) {
t.Fatalf("roundtrip=%q != original=%q", rt, iv)
}
})
}
}
2 changes: 1 addition & 1 deletion types/uint.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func (u *Uint) MarshalTo(data []byte) (n int, err error) {
if u.i == nil {
u.i = new(big.Int)
}
if len(u.i.Bytes()) == 0 {
if u.i.BitLen() == 0 { // The value 0
copy(data, []byte{0x30})
return 1, nil
}
Expand Down
34 changes: 34 additions & 0 deletions types/uint_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package types_test

import (
"fmt"
"math"
"math/big"
"math/rand"
Expand Down Expand Up @@ -290,3 +291,36 @@ func maxuint(i1, i2 uint64) uint64 {
}
return i2
}

func TestRoundTripMarshalToUint(t *testing.T) {
var values = []uint64{
0,
1,
1 << 10,
1<<10 - 3,
1<<63 - 1,
1<<32 - 7,
1<<22 - 8,
}

for _, value := range values {
value := value
t.Run(fmt.Sprintf("%d", value), func(t *testing.T) {
t.Parallel()

var scratch [20]byte
uv := sdk.NewUint(value)
n, err := uv.MarshalTo(scratch[:])
if err != nil {
t.Fatal(err)
}
rt := new(sdk.Uint)
if err := rt.Unmarshal(scratch[:n]); err != nil {
t.Fatal(err)
}
if !rt.Equal(uv) {
t.Fatalf("roundtrip=%q != original=%q", rt, uv)
}
})
}
}

0 comments on commit bddc63f

Please sign in to comment.