Skip to content

Commit

Permalink
Fix bug in median calculation (#211)
Browse files Browse the repository at this point in the history
* Fix the issue of the wrong median while proposing.

Signed-off-by: Ashish Mishra <ashish10677@gmail.com>
  • Loading branch information
ashish10677 authored Sep 13, 2021
1 parent b64ed7a commit 0c0527c
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
4 changes: 2 additions & 2 deletions cmd/dispute.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func HandleDispute(client *ethclient.Client, config types.Configurations, accoun

func Dispute(client *ethclient.Client, config types.Configurations, account types.Account, epoch uint32, blockId uint8, assetId int) error {
blockManager := utils.GetBlockManager(client)
sortedVotes, err := getSortedVotes(client, account.Address)
sortedVotes, err := getSortedVotes(client, account.Address, uint8(assetId))
if err != nil {
return err
}
Expand All @@ -64,7 +64,7 @@ func Dispute(client *ethclient.Client, config types.Configurations, account type
Config: config,
})

GiveSorted(client, blockManager, txnOpts, epoch, uint8(assetId-1), utils.ConvertBigIntArrayToUint32Array(sortedVotes))
GiveSorted(client, blockManager, txnOpts, epoch, uint8(assetId), utils.ConvertBigIntArrayToUint32Array(sortedVotes))

log.Info("Finalizing dispute...")
finalizeDisputeTxnOpts := utils.GetTxnOpts(types.TransactionOptions{
Expand Down
8 changes: 4 additions & 4 deletions cmd/propose.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func MakeBlock(client *ethclient.Client, address string, rogueMode bool) ([]uint
}

for assetId := 1; assetId <= int(numAssets); assetId++ {
sortedVotes, err := getSortedVotes(client, address)
sortedVotes, err := getSortedVotes(client, address, uint8(assetId))
if err != nil {
log.Error(err)
// TODO: Add retry mechanism
Expand Down Expand Up @@ -232,19 +232,19 @@ func MakeBlock(client *ethclient.Client, address string, rogueMode bool) ([]uint
return mediansInUint32, nil
}

func getSortedVotes(client *ethclient.Client, address string) ([]*big.Int, error) {
func getSortedVotes(client *ethclient.Client, address string, assetId uint8) ([]*big.Int, error) {
numberOfStakers, err := utils.GetNumberOfStakers(client, address)
if err != nil {
return nil, err
}
var voteValues []*big.Int

for i := 1; i <= int(numberOfStakers); i++ {
vote, err := utils.GetVotes(client, address, uint32(i))
vote, err := utils.GetVoteValue(client, address, assetId, uint32(i))
if err != nil {
return nil, err
}
voteValues = append(voteValues, vote.Values...)
voteValues = append(voteValues, vote)
}

sortutil.BigIntSlice.Sort(voteValues)
Expand Down
5 changes: 5 additions & 0 deletions utils/vote.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ func GetVotes(client *ethclient.Client, address string, stakerId uint32) (struct
return voteManager.GetVote(&callOpts, stakerId)
}

func GetVoteValue(client *ethclient.Client, address string, assetId uint8, stakerId uint32) (*big.Int, error) {
voteManager, callOpts := getVoteManagerWithOpts(client, address)
return voteManager.GetVoteValue(&callOpts, assetId, stakerId)
}

func GetInfluenceSnapshot(client *ethclient.Client, address string, epoch uint32) (*big.Int, error) {
voteManager, callOpts := getVoteManagerWithOpts(client, address)
stakerId, err := GetStakerId(client, address)
Expand Down

0 comments on commit 0c0527c

Please sign in to comment.