Skip to content

Commit bddc63f

Browse files
Merge branch 'master' into prathyusha/8571-create-proposal
2 parents 0b5dc7d + a534a96 commit bddc63f

File tree

8 files changed

+249
-25
lines changed

8 files changed

+249
-25
lines changed

client/grpc/tmservice/service.go

+4
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ func (s queryServer) GetLatestValidatorSet(ctx context.Context, req *GetLatestVa
104104
outputValidatorsRes := &GetLatestValidatorSetResponse{
105105
BlockHeight: validatorsRes.BlockHeight,
106106
Validators: make([]*Validator, len(validatorsRes.Validators)),
107+
Pagination: &qtypes.PageResponse{
108+
Total: validatorsRes.Total,
109+
},
107110
}
108111

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

161165
for i, validator := range validatorsRes.Validators {

client/grpc/tmservice/service_test.go

+117-23
Original file line numberDiff line numberDiff line change
@@ -131,32 +131,126 @@ func (s IntegrationTestSuite) TestQueryLatestValidatorSet() {
131131
s.Require().Equal(validatorSetRes.Validators[0].PubKey, anyPub)
132132
}
133133

134-
func (s IntegrationTestSuite) TestQueryValidatorSetByHeight() {
135-
val := s.network.Validators[0]
136-
137-
// nil pagination
138-
_, err := s.queryClient.GetValidatorSetByHeight(context.Background(), &tmservice.GetValidatorSetByHeightRequest{
139-
Height: 1,
140-
Pagination: nil,
141-
})
142-
s.Require().NoError(err)
134+
func (s IntegrationTestSuite) TestLatestValidatorSet_GRPC() {
135+
vals := s.network.Validators
136+
testCases := []struct {
137+
name string
138+
req *tmservice.GetLatestValidatorSetRequest
139+
expErr bool
140+
expErrMsg string
141+
}{
142+
{"nil request", nil, true, "cannot be nil"},
143+
{"no pagination", &tmservice.GetLatestValidatorSetRequest{}, false, ""},
144+
{"with pagination", &tmservice.GetLatestValidatorSetRequest{Pagination: &qtypes.PageRequest{Offset: 0, Limit: uint64(len(vals))}}, false, ""},
145+
}
146+
for _, tc := range testCases {
147+
tc := tc
148+
s.Run(tc.name, func() {
149+
grpcRes, err := s.queryClient.GetLatestValidatorSet(context.Background(), tc.req)
150+
if tc.expErr {
151+
s.Require().Error(err)
152+
s.Require().Contains(err.Error(), tc.expErrMsg)
153+
} else {
154+
s.Require().NoError(err)
155+
s.Require().Len(grpcRes.Validators, len(vals))
156+
s.Require().Equal(grpcRes.Pagination.Total, uint64(len(vals)))
157+
content, ok := grpcRes.Validators[0].PubKey.GetCachedValue().(cryptotypes.PubKey)
158+
s.Require().Equal(true, ok)
159+
s.Require().Equal(content, vals[0].PubKey)
160+
}
161+
})
162+
}
163+
}
143164

144-
_, err = s.queryClient.GetValidatorSetByHeight(context.Background(), &tmservice.GetValidatorSetByHeightRequest{
145-
Height: 1,
146-
Pagination: &qtypes.PageRequest{
147-
Offset: 0,
148-
Limit: 10,
149-
}})
150-
s.Require().NoError(err)
165+
func (s IntegrationTestSuite) TestLatestValidatorSet_GRPCGateway() {
166+
vals := s.network.Validators
167+
testCases := []struct {
168+
name string
169+
url string
170+
expErr bool
171+
expErrMsg string
172+
}{
173+
{"no pagination", fmt.Sprintf("%s/cosmos/base/tendermint/v1beta1/validatorsets/latest", vals[0].APIAddress), false, ""},
174+
{"pagination invalid fields", fmt.Sprintf("%s/cosmos/base/tendermint/v1beta1/validatorsets/latest?pagination.offset=-1&pagination.limit=-2", vals[0].APIAddress), true, "strconv.ParseUint"},
175+
{"with pagination", fmt.Sprintf("%s/cosmos/base/tendermint/v1beta1/validatorsets/latest?pagination.offset=0&pagination.limit=2", vals[0].APIAddress), false, ""},
176+
}
177+
for _, tc := range testCases {
178+
tc := tc
179+
s.Run(tc.name, func() {
180+
res, err := rest.GetRequest(tc.url)
181+
s.Require().NoError(err)
182+
if tc.expErr {
183+
s.Require().Contains(string(res), tc.expErrMsg)
184+
} else {
185+
var result tmservice.GetLatestValidatorSetResponse
186+
err = vals[0].ClientCtx.JSONMarshaler.UnmarshalJSON(res, &result)
187+
s.Require().NoError(err)
188+
s.Require().Equal(uint64(len(vals)), result.Pagination.Total)
189+
anyPub, err := codectypes.NewAnyWithValue(vals[0].PubKey)
190+
s.Require().NoError(err)
191+
s.Require().Equal(result.Validators[0].PubKey, anyPub)
192+
}
193+
})
194+
}
195+
}
151196

152-
// no pagination rest
153-
_, err = rest.GetRequest(fmt.Sprintf("%s/cosmos/base/tendermint/v1beta1/validatorsets/%d", val.APIAddress, 1))
154-
s.Require().NoError(err)
197+
func (s IntegrationTestSuite) TestValidatorSetByHeight_GRPC() {
198+
vals := s.network.Validators
199+
testCases := []struct {
200+
name string
201+
req *tmservice.GetValidatorSetByHeightRequest
202+
expErr bool
203+
expErrMsg string
204+
}{
205+
{"nil request", nil, true, "request cannot be nil"},
206+
{"empty request", &tmservice.GetValidatorSetByHeightRequest{}, true, "height must be greater than 0"},
207+
{"no pagination", &tmservice.GetValidatorSetByHeightRequest{Height: 1}, false, ""},
208+
{"with pagination", &tmservice.GetValidatorSetByHeightRequest{Height: 1, Pagination: &qtypes.PageRequest{Offset: 0, Limit: 1}}, false, ""},
209+
}
210+
for _, tc := range testCases {
211+
tc := tc
212+
s.Run(tc.name, func() {
213+
grpcRes, err := s.queryClient.GetValidatorSetByHeight(context.Background(), tc.req)
214+
if tc.expErr {
215+
s.Require().Error(err)
216+
s.Require().Contains(err.Error(), tc.expErrMsg)
217+
} else {
218+
s.Require().NoError(err)
219+
s.Require().Len(grpcRes.Validators, len(vals))
220+
s.Require().Equal(grpcRes.Pagination.Total, uint64(len(vals)))
221+
}
222+
})
223+
}
224+
}
155225

156-
// rest query with pagination
157-
restRes, err := rest.GetRequest(fmt.Sprintf("%s/cosmos/base/tendermint/v1beta1/validatorsets/%d?pagination.offset=%d&pagination.limit=%d", val.APIAddress, 1, 0, 1))
158-
var validatorSetRes tmservice.GetValidatorSetByHeightResponse
159-
s.Require().NoError(val.ClientCtx.JSONMarshaler.UnmarshalJSON(restRes, &validatorSetRes))
226+
func (s IntegrationTestSuite) TestValidatorSetByHeight_GRPCGateway() {
227+
vals := s.network.Validators
228+
testCases := []struct {
229+
name string
230+
url string
231+
expErr bool
232+
expErrMsg string
233+
}{
234+
{"invalid height", fmt.Sprintf("%s/cosmos/base/tendermint/v1beta1/validatorsets/%d", vals[0].APIAddress, -1), true, "height must be greater than 0"},
235+
{"no pagination", fmt.Sprintf("%s/cosmos/base/tendermint/v1beta1/validatorsets/%d", vals[0].APIAddress, 1), false, ""},
236+
{"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"},
237+
{"with pagination", fmt.Sprintf("%s/cosmos/base/tendermint/v1beta1/validatorsets/%d?pagination.offset=0&pagination.limit=2", vals[0].APIAddress, 1), false, ""},
238+
}
239+
for _, tc := range testCases {
240+
tc := tc
241+
s.Run(tc.name, func() {
242+
res, err := rest.GetRequest(tc.url)
243+
s.Require().NoError(err)
244+
if tc.expErr {
245+
s.Require().Contains(string(res), tc.expErrMsg)
246+
} else {
247+
var result tmservice.GetValidatorSetByHeightResponse
248+
err = vals[0].ClientCtx.JSONMarshaler.UnmarshalJSON(res, &result)
249+
s.Require().NoError(err)
250+
s.Require().Equal(uint64(len(vals)), result.Pagination.Total)
251+
}
252+
})
253+
}
160254
}
161255

162256
func TestIntegrationTestSuite(t *testing.T) {

client/rpc/validators.go

+8
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,14 @@ type ValidatorOutput struct {
7979
type ResultValidatorsOutput struct {
8080
BlockHeight int64 `json:"block_height"`
8181
Validators []ValidatorOutput `json:"validators"`
82+
Total uint64 `json:"total"`
8283
}
8384

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

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

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

134+
total := validatorsRes.Total
135+
if validatorsRes.Total < 0 {
136+
total = 0
137+
}
138+
132139
outputValidatorsRes := ResultValidatorsOutput{
133140
BlockHeight: validatorsRes.BlockHeight,
134141
Validators: make([]ValidatorOutput, len(validatorsRes.Validators)),
142+
Total: uint64(total),
135143
}
136144

137145
for i := 0; i < len(validatorsRes.Validators); i++ {

types/bench_test.go

+50
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,53 @@ func BenchmarkParseCoin(b *testing.B) {
2828
}
2929
}
3030
}
31+
32+
func BenchmarkUintMarshal(b *testing.B) {
33+
var values = []uint64{
34+
0,
35+
1,
36+
1 << 10,
37+
1<<10 - 3,
38+
1<<63 - 1,
39+
1<<32 - 7,
40+
1<<22 - 8,
41+
}
42+
43+
var scratch [20]byte
44+
b.ReportAllocs()
45+
for i := 0; i < b.N; i++ {
46+
for _, value := range values {
47+
u := types.NewUint(value)
48+
n, err := u.MarshalTo(scratch[:])
49+
if err != nil {
50+
b.Fatal(err)
51+
}
52+
b.SetBytes(int64(n))
53+
}
54+
}
55+
}
56+
57+
func BenchmarkIntMarshal(b *testing.B) {
58+
var values = []int64{
59+
0,
60+
1,
61+
1 << 10,
62+
1<<10 - 3,
63+
1<<63 - 1,
64+
1<<32 - 7,
65+
1<<22 - 8,
66+
}
67+
68+
var scratch [20]byte
69+
b.ReportAllocs()
70+
for i := 0; i < b.N; i++ {
71+
for _, value := range values {
72+
in := types.NewInt(value)
73+
n, err := in.MarshalTo(scratch[:])
74+
if err != nil {
75+
b.Fatal(err)
76+
}
77+
b.SetBytes(int64(n))
78+
}
79+
}
80+
}

types/int.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ func (i *Int) MarshalTo(data []byte) (n int, err error) {
375375
if i.i == nil {
376376
i.i = new(big.Int)
377377
}
378-
if len(i.i.Bytes()) == 0 {
378+
if i.i.BitLen() == 0 { // The value 0
379379
copy(data, []byte{0x30})
380380
return 1, nil
381381
}

types/int_test.go

+34
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package types_test
22

33
import (
4+
"fmt"
45
"math/big"
56
"math/rand"
67
"strconv"
@@ -385,3 +386,36 @@ func (s *intTestSuite) TestIntEq() {
385386
_, resp, _, _, _ = sdk.IntEq(s.T(), sdk.OneInt(), sdk.ZeroInt())
386387
s.Require().False(resp)
387388
}
389+
390+
func TestRoundTripMarshalToInt(t *testing.T) {
391+
var values = []int64{
392+
0,
393+
1,
394+
1 << 10,
395+
1<<10 - 3,
396+
1<<63 - 1,
397+
1<<32 - 7,
398+
1<<22 - 8,
399+
}
400+
401+
for _, value := range values {
402+
value := value
403+
t.Run(fmt.Sprintf("%d", value), func(t *testing.T) {
404+
t.Parallel()
405+
406+
var scratch [20]byte
407+
iv := sdk.NewInt(value)
408+
n, err := iv.MarshalTo(scratch[:])
409+
if err != nil {
410+
t.Fatal(err)
411+
}
412+
rt := new(sdk.Int)
413+
if err := rt.Unmarshal(scratch[:n]); err != nil {
414+
t.Fatal(err)
415+
}
416+
if !rt.Equal(iv) {
417+
t.Fatalf("roundtrip=%q != original=%q", rt, iv)
418+
}
419+
})
420+
}
421+
}

types/uint.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ func (u *Uint) MarshalTo(data []byte) (n int, err error) {
161161
if u.i == nil {
162162
u.i = new(big.Int)
163163
}
164-
if len(u.i.Bytes()) == 0 {
164+
if u.i.BitLen() == 0 { // The value 0
165165
copy(data, []byte{0x30})
166166
return 1, nil
167167
}

types/uint_test.go

+34
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package types_test
22

33
import (
4+
"fmt"
45
"math"
56
"math/big"
67
"math/rand"
@@ -290,3 +291,36 @@ func maxuint(i1, i2 uint64) uint64 {
290291
}
291292
return i2
292293
}
294+
295+
func TestRoundTripMarshalToUint(t *testing.T) {
296+
var values = []uint64{
297+
0,
298+
1,
299+
1 << 10,
300+
1<<10 - 3,
301+
1<<63 - 1,
302+
1<<32 - 7,
303+
1<<22 - 8,
304+
}
305+
306+
for _, value := range values {
307+
value := value
308+
t.Run(fmt.Sprintf("%d", value), func(t *testing.T) {
309+
t.Parallel()
310+
311+
var scratch [20]byte
312+
uv := sdk.NewUint(value)
313+
n, err := uv.MarshalTo(scratch[:])
314+
if err != nil {
315+
t.Fatal(err)
316+
}
317+
rt := new(sdk.Uint)
318+
if err := rt.Unmarshal(scratch[:n]); err != nil {
319+
t.Fatal(err)
320+
}
321+
if !rt.Equal(uv) {
322+
t.Fatalf("roundtrip=%q != original=%q", rt, uv)
323+
}
324+
})
325+
}
326+
}

0 commit comments

Comments
 (0)