Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Commit

Permalink
fix test #316
Browse files Browse the repository at this point in the history
  • Loading branch information
millken authored and dustinxie committed Dec 18, 2020
1 parent bb4dbce commit 9cd17ae
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 71 deletions.
15 changes: 15 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QH
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/cespare/cp v1.1.1 h1:nCb6ZLdB7NRaqsm91JtQTAme2SKJzXVsdPIPkyJr1MU=
github.com/cespare/cp v1.1.1/go.mod h1:SOGHArjBr4JWaSDEVpWpo/hNg6RoKrls6Oh40hiwW+s=
github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko=
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
github.com/cespare/xxhash/v2 v2.1.1 h1:6MnRN8NT7+YBpUIWxHtefFZOKTAPgGjpQSxqLNn0+qY=
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
Expand Down Expand Up @@ -163,6 +165,7 @@ github.com/golang/mock v1.2.0/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfb
github.com/golang/mock v1.3.1/go.mod h1:sBzyDLLjw3U8JLTeZvSv8jJB+tU5PVekmnlKIyFUx0Y=
github.com/golang/mock v1.4.0 h1:Rd1kQnQu0Hq3qvJppYSG0HtP+f5LPPUiDswTLiEegLg=
github.com/golang/mock v1.4.0/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt3cw=
github.com/golang/mock v1.4.4 h1:l75CXGRSwbaYNpl/Z2X1XIIAMSCquvXgpVZDhwEIJsc=
github.com/golang/mock v1.4.4/go.mod h1:l3mdAwkq5BuhzHwde/uurv3sEJeZMXNpwsxVWU71h+4=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.0/go.mod h1:Qd/q+1AKNOZr9uGQzbzCmRO6sUih6GTPZv6a1/R87v0=
Expand All @@ -176,6 +179,7 @@ github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrU
github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w=
github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0=
github.com/golang/protobuf v1.4.1/go.mod h1:U8fpvMrcmy5pZrNK1lt4xCsGvpyWQ/VVv6QDs8UjoX8=
github.com/golang/protobuf v1.4.2 h1:+Z5KGCizgyZCbGh1KZqA0fcLLkwbsjIzS4aV2v7wJX0=
github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI=
github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4=
Expand Down Expand Up @@ -214,10 +218,12 @@ github.com/graph-gophers/graphql-go v0.0.0-20190610161739-8f92f34fc598 h1:XLoCW/
github.com/graph-gophers/graphql-go v0.0.0-20190610161739-8f92f34fc598/go.mod h1:Au3iQ8DvDis8hZ4q2OzRcaKYlAsPt+fYvib5q4nIqu4=
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0 h1:Iju5GlWwrvL6UBg4zJJt3btmonfrMlCDdsejg4CZE7c=
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs=
github.com/grpc-ecosystem/go-grpc-middleware v1.2.0 h1:0IKlLyQ3Hs9nDaiK5cSHAGmcQEIC8l2Ts1u6x5Dfrqg=
github.com/grpc-ecosystem/go-grpc-middleware v1.2.0/go.mod h1:mJzapYve32yjrKlk9GbyCZHuPgZsrbyIbyKhSzOpg6s=
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
github.com/grpc-ecosystem/grpc-gateway v1.9.0 h1:bM6ZAFZmc/wPFaRDi0d5L7hGEZEx/2u+Tmr2evNHDiI=
github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY=
github.com/grpc-ecosystem/grpc-gateway v1.14.5 h1:aiLxiiVzAXb7wb3lAmubA69IokWOoUNe+E7TdGKh8yw=
github.com/grpc-ecosystem/grpc-gateway v1.14.5/go.mod h1:UJ0EZAp832vCd54Wev9N1BMKEyvcZ5+IM0AwDrnlkEc=
github.com/gxed/hashland/keccakpg v0.0.1/go.mod h1:kRzw3HkwxFU1mpmPP8v1WyQzwdGfmKFJ6tItnhQ67kU=
github.com/gxed/hashland/murmur3 v0.0.1/go.mod h1:KjXop02n4/ckmZSnY2+HKcLud/tcmvhST0bie/0lS48=
Expand Down Expand Up @@ -528,14 +534,17 @@ github.com/openzipkin/zipkin-go v0.1.6/go.mod h1:QgAqvLzwWbR/WpD4A3cGpPtJrZXNIiJ
github.com/oschwald/maxminddb-golang v1.5.0 h1:rmyoIV6z2/s9TCJedUuDiKht2RN12LWJ1L7iRGtWY64=
github.com/oschwald/maxminddb-golang v1.5.0/go.mod h1:3jhIUymTJ5VREKyIhWm66LJiQt04F0UCDdodShpjWsY=
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/pborman/uuid v1.2.0 h1:J7Q5mO4ysT1dv8hyrUGHb9+ooztCXu1D8MY8DZYsu3g=
github.com/pborman/uuid v1.2.0/go.mod h1:X/NO0urCmaxf9VXbdlT7C2Yzkj2IKimNn4k+gtPdI/k=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
github.com/pierrec/lz4 v0.0.0-20190327172049-315a67e90e41/go.mod h1:3/3N9NVKO0jef7pBehbT1qWhCMrIgbYNnFAZCqQ5LRc=
github.com/pierrec/lz4 v2.0.5+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/profile v1.2.1/go.mod h1:hJw3o1OdXxsrSjjVksARp5W95eeEaEfptyVZyv6JUPA=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI=
github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
Expand Down Expand Up @@ -571,6 +580,7 @@ github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40T
github.com/prometheus/tsdb v0.10.0 h1:If5rVCMTp6W2SiRAQFlbpJNgVlgMEd+U2GZckwK38ic=
github.com/prometheus/tsdb v0.10.0/go.mod h1:oi49uRhEe9dPUTlS3JRZOwJuVi6tmh10QSgwXEyGCt4=
github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
github.com/rjeczalik/notify v0.9.2 h1:MiTWrPj55mNDHEiIX5YUSKefw/+lCQVoAFmD6oQm5w8=
github.com/rjeczalik/notify v0.9.2/go.mod h1:aErll2f0sUX9PXZnVNyeiObbmTlk5jnMoCa4QEjJeqM=
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
Expand Down Expand Up @@ -612,7 +622,9 @@ github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An
github.com/spf13/viper v1.7.0/go.mod h1:8WkrPz2fc9jxqZNCJI/76HCieCp4Q8HaLFoCha5qpdg=
github.com/status-im/keycard-go v0.0.0-20190424133014-d95853db0f48 h1:ju5UTwk5Odtm4trrY+4Ca4RMj5OyXbmVeDAVad2T0Jw=
github.com/status-im/keycard-go v0.0.0-20190424133014-d95853db0f48/go.mod h1:RZLeN1LMWmRsyYjvAu+I6Dm9QmlDaIIt+Y+4Kd7Tp+Q=
github.com/steakknife/bloomfilter v0.0.0-20180922174646-6819c0d2a570 h1:gIlAHnH1vJb5vwEjIp5kBj/eu99p/bl0Ay2goiPe5xE=
github.com/steakknife/bloomfilter v0.0.0-20180922174646-6819c0d2a570/go.mod h1:8OR4w3TdeIHIh1g6EMY5p0gVNOovcWC+1vpc7naMuAw=
github.com/steakknife/hamming v0.0.0-20180906055917-c99c65617cd3 h1:njlZPzLwU639dk2kqnCPPv+wNjq7Xb6EfUxe/oX0/NM=
github.com/steakknife/hamming v0.0.0-20180906055917-c99c65617cd3/go.mod h1:hpGUWaI9xL8pRQCTXQgocU38Qw1g0Us7n5PxxTwTCYU=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down Expand Up @@ -708,6 +720,7 @@ golang.org/x/crypto v0.0.0-20190911031432-227b76d455e7/go.mod h1:yigFU9vqHzYiE8U
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20191219195013-becbf705a915/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20200115085410-6d4e4cb37c7d/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 h1:psW17arqaxU48Z5kZ0CQnkZWQJsqcURM6tKiBApRjXI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
Expand Down Expand Up @@ -861,6 +874,7 @@ google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98
google.golang.org/genproto v0.0.0-20190911173649-1774047e7e51/go.mod h1:IbNlFCBrqXvoKpeg0TB2l7cyZUmoaFKYIwrEpbDKLA8=
google.golang.org/genproto v0.0.0-20190927181202-20e1ac93f88c/go.mod h1:IbNlFCBrqXvoKpeg0TB2l7cyZUmoaFKYIwrEpbDKLA8=
google.golang.org/genproto v0.0.0-20191108220845-16a3f7862a1a/go.mod h1:n3cpQtvxv34hfy77yVDNjmbRyujviMdxYliBSkLhpCc=
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013 h1:+kGHl1aib/qcwaRi1CbqBZ1rk19r85MNUf8HaBghugY=
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo=
google.golang.org/grpc v1.17.0/go.mod h1:6QZJwpn2B+Zp71q/5VxRsJ6NXXVCE5NRUHRo+f3cWCs=
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
Expand All @@ -881,6 +895,7 @@ google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzi
google.golang.org/protobuf v1.22.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
google.golang.org/protobuf v1.25.0 h1:Ejskq+SyPohKW+1uil0JJMtmHCgJPJ/qWTxr8qp+R4c=
google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c=
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
gopkg.in/bsm/ratelimit.v1 v1.0.0-20160220154919-db14e161995a/go.mod h1:KF9sEfUPAXdG8Oev9e99iLGnl2uJMjc5B+4y3O7x610=
Expand Down
28 changes: 3 additions & 25 deletions indexprotocol/blocks/protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func TestProtocol(t *testing.T) {
require.NoError(store.Stop(ctx))
}()

p := NewProtocol(store, epochctx.NewEpochCtx(1, 1, 1, epochctx.FairbankHeight(100000)), indexprotocol.GravityChain{GravityChainStartHeight: 1})
p := NewProtocol(store, epochctx.NewEpochCtx(36, 24, 15, epochctx.FairbankHeight(1000000)), indexprotocol.GravityChain{GravityChainStartHeight: 1})

require.NoError(p.CreateTables(ctx))

Expand All @@ -67,15 +67,7 @@ func TestProtocol(t *testing.T) {
ConsensusScheme: "ROLLDPOS",
})

readStateRequest := &iotexapi.ReadStateRequest{
ProtocolID: []byte(indexprotocol.PollProtocolID),
MethodName: []byte("GetGravityChainStartHeight"),
Arguments: [][]byte{[]byte(strconv.FormatUint(blk1.Height(), 10))},
}
chainClient.EXPECT().ReadState(gomock.Any(), readStateRequest).Times(1).Return(&iotexapi.ReadStateResponse{
Data: []byte(strconv.FormatUint(1000, 10)),
}, nil)
electionClient.EXPECT().GetCandidates(gomock.Any(), gomock.Any()).Times(2).Return(
electionClient.EXPECT().GetCandidates(gomock.Any(), gomock.Any()).Times(1).Return(
&api.CandidateResponse{
Candidates: []*api.Candidate{
{
Expand All @@ -89,7 +81,7 @@ func TestProtocol(t *testing.T) {
},
}, nil,
)
readStateRequest = &iotexapi.ReadStateRequest{
readStateRequest := &iotexapi.ReadStateRequest{
ProtocolID: []byte(indexprotocol.PollProtocolID),
MethodName: []byte("ActiveBlockProducersByEpoch"),
Arguments: [][]byte{[]byte(strconv.FormatUint(1, 10))},
Expand Down Expand Up @@ -119,20 +111,11 @@ func TestProtocol(t *testing.T) {
blk2, err := testutil.BuildEmptyBlock(2)
require.NoError(err)

readStateRequest = &iotexapi.ReadStateRequest{
ProtocolID: []byte(indexprotocol.PollProtocolID),
MethodName: []byte("GetGravityChainStartHeight"),
Arguments: [][]byte{[]byte(strconv.FormatUint(1, 10))},
}

readStateRequest = &iotexapi.ReadStateRequest{
ProtocolID: []byte(indexprotocol.PollProtocolID),
MethodName: []byte("ActiveBlockProducersByEpoch"),
Arguments: [][]byte{[]byte(strconv.FormatUint(2, 10))},
}
chainClient.EXPECT().ReadState(gomock.Any(), readStateRequest).Times(1).Return(&iotexapi.ReadStateResponse{
Data: data,
}, nil)

require.NoError(store.Transact(func(tx *sql.Tx) error {
return p.HandleBlock(ctx, tx, blk2)
Expand All @@ -146,9 +129,4 @@ func TestProtocol(t *testing.T) {
require.Equal(hex.EncodeToString(blk1Hash[:]), blockHistory.BlockHash)
require.Equal("616c6661", blockHistory.ProducerName)
require.Equal("627261766f", blockHistory.ExpectedProducerName)

productivityHistory, err := p.getProductivityHistory(uint64(1), "627261766f")
require.NoError(err)
require.Equal(uint64(0), productivityHistory.Production)
require.Equal(uint64(1), productivityHistory.ExpectedProduction)
}
6 changes: 4 additions & 2 deletions indexprotocol/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@ func getStakingBuckets(chainClient iotexapi.APIServiceClient, offset, limit uint
Arguments: [][]byte{arg},
Height: fmt.Sprintf("%d", height),
}
readStateRes, err := chainClient.ReadState(context.Background(), readStateRequest)
ctx := context.WithValue(context.Background(), &iotexapi.ReadStateRequest{}, iotexapi.ReadStakingDataMethod_BUCKETS)
readStateRes, err := chainClient.ReadState(ctx, readStateRequest)
if err != nil {
return
}
Expand Down Expand Up @@ -223,7 +224,8 @@ func getStakingCandidates(chainClient iotexapi.APIServiceClient, offset, limit u
Arguments: [][]byte{arg},
Height: fmt.Sprintf("%d", height),
}
readStateRes, err := chainClient.ReadState(context.Background(), readStateRequest)
ctx := context.WithValue(context.Background(), &iotexapi.ReadStateRequest{}, iotexapi.ReadStakingDataMethod_CANDIDATES)
readStateRes, err := chainClient.ReadState(ctx, readStateRequest)
if err != nil {
return
}
Expand Down
86 changes: 45 additions & 41 deletions indexprotocol/votings/protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ import (
"time"

"github.com/golang/mock/gomock"
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes"
"github.com/stretchr/testify/require"

"github.com/iotexproject/iotex-core/action/protocol/vote"
"github.com/iotexproject/iotex-core/test/mock/mock_apiserviceclient"
"github.com/iotexproject/iotex-election/db"
"github.com/iotexproject/iotex-election/pb/api"
Expand Down Expand Up @@ -56,12 +56,24 @@ func TestProtocol(t *testing.T) {
require.NoError(err)
require.NoError(store.Stop(ctx))
}()
cfg := indexprotocol.VoteWeightCalConsts{}
p, err := NewProtocol(store, epochctx.NewEpochCtx(36, 24, 15, epochctx.FairbankHeight(100000)), indexprotocol.GravityChain{}, indexprotocol.Poll{
VoteThreshold: "0",
ScoreThreshold: "0",
SelfStakingThreshold: "0",
}, cfg, indexprotocol.RewardPortionCfg{"", 123})
cfg := indexprotocol.VoteWeightCalConsts{
DurationLg: 1.2,
AutoStake: 1,
SelfStake: 1.06,
}
p, err := NewProtocol(store,
epochctx.NewEpochCtx(36, 24, 15, epochctx.FairbankHeight(1000000)),
indexprotocol.GravityChain{},
indexprotocol.Poll{
VoteThreshold: "0",
ScoreThreshold: "0",
SelfStakingThreshold: "0",
},
cfg,
indexprotocol.RewardPortionCfg{
RewardPortionContract: "",
RewardPortionContractDeployHeight: uint64(123),
})
require.NoError(err)
require.NoError(p.CreateTables(ctx))

Expand All @@ -75,41 +87,9 @@ func TestProtocol(t *testing.T) {
ElectionClient: electionClient,
ConsensusScheme: "ROLLDPOS",
})
// first call GetGravityChainStartHeight
readStateRequestForGravityHeight := &iotexapi.ReadStateRequest{
ProtocolID: []byte(indexprotocol.PollProtocolID),
MethodName: []byte("GetGravityChainStartHeight"),
Arguments: [][]byte{[]byte(strconv.FormatUint(1, 10))},
}
first := chainClient.EXPECT().ReadState(gomock.Any(), readStateRequestForGravityHeight).Times(1).Return(&iotexapi.ReadStateResponse{
Data: []byte(strconv.FormatUint(1000, 10)),
}, nil)
// second call ProbationListByEpoch
probationListByEpochRequest := &iotexapi.ReadStateRequest{
ProtocolID: []byte(indexprotocol.PollProtocolID),
MethodName: []byte("ProbationListByEpoch"),
Arguments: [][]byte{[]byte(strconv.FormatUint(2, 10))},
}
pb := &iotextypes.ProbationCandidateList{
IntensityRate: uint32(90),
ProbationList: []*iotextypes.ProbationCandidateList_Info{
{
Address: testutil.Addr1,
Count: uint32(1),
},
},
}
data, err := proto.Marshal(pb)
second := chainClient.EXPECT().ReadState(gomock.Any(), probationListByEpochRequest).Times(1).Return(&iotexapi.ReadStateResponse{
Data: data,
}, nil)
gomock.InOrder(
second,
first,
)

timestamp, err := ptypes.TimestampProto(time.Unix(1000, 0))
require.NoError(err)

chainClient.EXPECT().GetElectionBuckets(gomock.Any(), gomock.Any()).Times(1).Return(&iotexapi.GetElectionBucketsResponse{
Buckets: []*iotextypes.ElectionBucket{},
}, db.ErrNotExist)
Expand Down Expand Up @@ -173,6 +153,30 @@ func TestProtocol(t *testing.T) {
}, nil,
)

probationListByEpochRequest := &iotexapi.ReadStateRequest{
ProtocolID: []byte(indexprotocol.PollProtocolID),
MethodName: []byte("ProbationListByEpoch"),
Arguments: [][]byte{[]byte(strconv.FormatUint(2, 10))},
}

probationList := &vote.ProbationList{
IntensityRate: uint32(0),
ProbationInfo: make(map[string]uint32),
}

data, err := probationList.Serialize()
chainClient.EXPECT().ReadState(gomock.Any(), probationListByEpochRequest).Times(1).Return(&iotexapi.ReadStateResponse{
Data: data,
}, nil)

readStateRequestForGravityHeight := &iotexapi.ReadStateRequest{
ProtocolID: []byte(indexprotocol.PollProtocolID),
MethodName: []byte("GetGravityChainStartHeight"),
Arguments: [][]byte{[]byte(strconv.FormatUint(1, 10))},
}
chainClient.EXPECT().ReadState(gomock.Any(), readStateRequestForGravityHeight).Times(1).Return(&iotexapi.ReadStateResponse{
Data: []byte(strconv.FormatUint(1000, 10)),
}, nil)
require.NoError(store.Transact(func(tx *sql.Tx) error {
return p.HandleBlock(ctx, tx, blk)
}))
Expand All @@ -184,7 +188,7 @@ func TestProtocol(t *testing.T) {
require.NoError(err)
require.Equal("abcd", res1.DelegateName)
require.Equal("1234", res2.DelegateName)
require.Equal("15", res1.TotalWeightedVotes) // (100 + 50) * 0.1
require.Equal("150", res1.TotalWeightedVotes)
require.Equal("100", res2.TotalWeightedVotes)

/*
Expand Down
9 changes: 6 additions & 3 deletions indexprotocol/votings/stakingprotocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,14 +258,16 @@ func mock(chainClient *mock_apiserviceclient.MockServiceClient, t *testing.T) {
vbl := &iotextypes.VoteBucketList{Buckets: buckets}
s, err := proto.Marshal(vbl)
require.NoError(err)
first := chainClient.EXPECT().ReadState(gomock.Any(), readStateRequest).AnyTimes().Return(&iotexapi.ReadStateResponse{
ctx := context.WithValue(context.Background(), &iotexapi.ReadStateRequest{}, iotexapi.ReadStakingDataMethod_BUCKETS)
first := chainClient.EXPECT().ReadState(ctx, readStateRequest).AnyTimes().Return(&iotexapi.ReadStateResponse{
Data: s,
}, nil)

// mock candidate
methodNameBytes, _ = proto.Marshal(&iotexapi.ReadStakingDataMethod{
methodNameBytes, err = proto.Marshal(&iotexapi.ReadStakingDataMethod{
Method: iotexapi.ReadStakingDataMethod_CANDIDATES,
})
require.NoError(err)
arg, err = proto.Marshal(&iotexapi.ReadStakingDataRequest{
Request: &iotexapi.ReadStakingDataRequest_Candidates_{
Candidates: &iotexapi.ReadStakingDataRequest_Candidates{
Expand All @@ -286,7 +288,8 @@ func mock(chainClient *mock_apiserviceclient.MockServiceClient, t *testing.T) {
cl := &iotextypes.CandidateListV2{Candidates: candidates}
s, err = proto.Marshal(cl)
require.NoError(err)
second := chainClient.EXPECT().ReadState(gomock.Any(), readStateRequest).AnyTimes().Return(&iotexapi.ReadStateResponse{
ctx = context.WithValue(context.Background(), &iotexapi.ReadStateRequest{}, iotexapi.ReadStakingDataMethod_CANDIDATES)
second := chainClient.EXPECT().ReadState(ctx, readStateRequest).AnyTimes().Return(&iotexapi.ReadStateResponse{
Data: s,
}, nil)
third := chainClient.EXPECT().ReadState(gomock.Any(), gomock.Any()).AnyTimes().Return(&iotexapi.ReadStateResponse{
Expand Down

0 comments on commit 9cd17ae

Please sign in to comment.