From 3144f61cd019fb2fa671de0cf811210418c36162 Mon Sep 17 00:00:00 2001 From: CoderZhi Date: Sat, 16 Mar 2019 15:20:36 -0700 Subject: [PATCH] hotfix election out of boundary issue (#760) --- Gopkg.lock | 6 +-- Gopkg.toml | 2 +- .../iotex-election/carrier/carrier.go | 53 +++++++++++++------ 3 files changed, 40 insertions(+), 21 deletions(-) diff --git a/Gopkg.lock b/Gopkg.lock index d2f7225456..4a3996886a 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -296,7 +296,7 @@ version = "v0.2.0" [[projects]] - digest = "1:8653f3ae774d8cf5e4af41d9b1ccd68b0e63ae10fa4cacdfc1e201c9c017e0cf" + digest = "1:83b6d81ebe06f84d4fbe429980ce0d3f967a1929211c82de51b5932223aebc19" name = "github.com/iotexproject/iotex-election" packages = [ "carrier", @@ -309,8 +309,8 @@ "util", ] pruneopts = "UT" - revision = "b124f5e61d088e53d8bbc73237cd4837d6655330" - version = "v0.1.0" + revision = "d14edec8e2502a10c8dcc69007aba2753a6fefd1" + version = "v0.1.1" [[projects]] digest = "1:ff780041e005869242b2cf977f1d4fa3b8f9bdd302fd254b4a19f78502a6ee10" diff --git a/Gopkg.toml b/Gopkg.toml index 3bd76b6aff..4df1c56fc0 100644 --- a/Gopkg.toml +++ b/Gopkg.toml @@ -60,7 +60,7 @@ [[constraint]] name = "github.com/iotexproject/iotex-election" - version = "v0.1.0" + version = "v0.1.1" [prune] go-tests = true diff --git a/vendor/github.com/iotexproject/iotex-election/carrier/carrier.go b/vendor/github.com/iotexproject/iotex-election/carrier/carrier.go index 0d95142394..f094ce2af7 100644 --- a/vendor/github.com/iotexproject/iotex-election/carrier/carrier.go +++ b/vendor/github.com/iotexproject/iotex-election/carrier/carrier.go @@ -38,7 +38,7 @@ type Carrier interface { Close() } -type ethereumCarrier struct { +type EthereumCarrier struct { client *ethclient.Client currentClientURLIndex int clientURLs []string @@ -72,7 +72,7 @@ func NewEthereumVoteCarrier( if err != nil { return nil, err } - return ðereumCarrier{ + return &EthereumCarrier{ client: client, currentClientURLIndex: currentClientURLIndex, clientURLs: clientURLs, @@ -81,11 +81,11 @@ func NewEthereumVoteCarrier( }, nil } -func (evc *ethereumCarrier) Close() { +func (evc *EthereumCarrier) Close() { evc.client.Close() } -func (evc *ethereumCarrier) BlockTimestamp(height uint64) (ts time.Time, err error) { +func (evc *EthereumCarrier) BlockTimestamp(height uint64) (ts time.Time, err error) { var header *ethtypes.Header for i := 0; i < len(evc.clientURLs); i++ { if header, err = evc.client.HeaderByNumber( @@ -103,7 +103,7 @@ func (evc *ethereumCarrier) BlockTimestamp(height uint64) (ts time.Time, err err return ts, errors.New("failed to get block timestamp") } -func (evc *ethereumCarrier) SubscribeNewBlock(height chan uint64, report chan error, unsubscribe chan bool) { +func (evc *EthereumCarrier) SubscribeNewBlock(height chan uint64, report chan error, unsubscribe chan bool) { ticker := time.NewTicker(60 * time.Second) lastHeight := uint64(0) go func() { @@ -123,11 +123,11 @@ func (evc *ethereumCarrier) SubscribeNewBlock(height chan uint64, report chan er }() } -func (evc *ethereumCarrier) TipHeight() (uint64, error) { +func (evc *EthereumCarrier) TipHeight() (uint64, error) { return evc.tipHeight(0) } -func (evc *ethereumCarrier) tipHeight(lastHeight uint64) (uint64, error) { +func (evc *EthereumCarrier) tipHeight(lastHeight uint64) (uint64, error) { for i := 0; i < len(evc.clientURLs); i++ { header, err := evc.client.HeaderByNumber(context.Background(), nil) if err == nil { @@ -148,7 +148,7 @@ func (evc *ethereumCarrier) tipHeight(lastHeight uint64) (uint64, error) { return 0, errors.New("failed to get tip height") } -func (evc *ethereumCarrier) candidates( +func (evc *EthereumCarrier) candidates( opts *bind.CallOpts, startIndex *big.Int, limit *big.Int, @@ -162,6 +162,13 @@ func (evc *ethereumCarrier) candidates( var caller *contract.RegisterCaller for i := 0; i < len(evc.clientURLs); i++ { if caller, err = contract.NewRegisterCaller(evc.registerContractAddress, evc.client); err == nil { + var count *big.Int + if count, err = caller.CandidateCount(opts); err != nil { + return + } + if startIndex.Cmp(count) >= 0 { + return + } if result, err = caller.GetAllCandidates(opts, startIndex, limit); err == nil { return } @@ -174,7 +181,7 @@ func (evc *ethereumCarrier) candidates( return result, errors.New("failed to get candidates") } -func (evc *ethereumCarrier) Candidates( +func (evc *EthereumCarrier) Candidates( height uint64, startIndex *big.Int, count uint8, @@ -215,11 +222,8 @@ func (evc *ethereumCarrier) Candidates( return new(big.Int).Add(startIndex, big.NewInt(int64(num))), candidates, nil } -func (evc *ethereumCarrier) buckets( - opts *bind.CallOpts, - previousIndex *big.Int, - limit *big.Int, -) (result struct { +// EthereumBucketsResult defines the data structure the buckets api returns +type EthereumBucketsResult struct { Count *big.Int Indexes []*big.Int StakeStartTimes []*big.Int @@ -228,7 +232,22 @@ func (evc *ethereumCarrier) buckets( StakedAmounts []*big.Int CanNames [][12]byte Owners []common.Address -}, err error) { +} + +// Buckets returns the result of api "buckets" +func (evc *EthereumCarrier) Buckets( + opts *bind.CallOpts, + previousIndex *big.Int, + limit *big.Int, +) (EthereumBucketsResult, error) { + return evc.buckets(opts, previousIndex, limit) +} + +func (evc *EthereumCarrier) buckets( + opts *bind.CallOpts, + previousIndex *big.Int, + limit *big.Int, +) (result EthereumBucketsResult, err error) { var caller *contract.StakingCaller for i := 0; i < len(evc.clientURLs); i++ { if caller, err = contract.NewStakingCaller(evc.stakingContractAddress, evc.client); err == nil { @@ -261,7 +280,7 @@ func (evc *ethereumCarrier) buckets( return result, errors.New("failed to get votes") } -func (evc *ethereumCarrier) Votes( +func (evc *EthereumCarrier) Votes( height uint64, previousIndex *big.Int, count uint8, @@ -306,7 +325,7 @@ func (evc *ethereumCarrier) Votes( return previousIndex, votes, nil } -func (evc *ethereumCarrier) rotateClient(cause error) (rotated bool, err error) { +func (evc *EthereumCarrier) rotateClient(cause error) (rotated bool, err error) { if cause != nil { switch cause.Error() { case "tls: use of closed connection":