lotus/cmd/lotus-shed/fip-0036.go

555 lines
16 KiB
Go
Raw Normal View History

2022-09-23 03:28:46 +00:00
package main
import (
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
2022-09-28 19:51:59 +00:00
"sort"
2022-09-28 00:48:30 +00:00
"strconv"
2022-09-23 03:28:46 +00:00
2022-09-28 00:48:30 +00:00
"github.com/ipfs/go-cid"
cbor "github.com/ipfs/go-ipld-cbor"
"github.com/mitchellh/go-homedir"
2022-09-26 12:13:00 +00:00
"github.com/urfave/cli/v2"
2022-09-28 00:48:30 +00:00
"golang.org/x/xerrors"
2022-09-26 12:13:00 +00:00
"github.com/filecoin-project/go-address"
2022-09-23 03:28:46 +00:00
"github.com/filecoin-project/go-state-types/abi"
2022-09-28 00:48:30 +00:00
"github.com/filecoin-project/go-state-types/big"
2022-09-23 03:28:46 +00:00
"github.com/filecoin-project/lotus/chain/actors/adt"
"github.com/filecoin-project/lotus/chain/actors/builtin"
2022-09-28 00:48:30 +00:00
"github.com/filecoin-project/lotus/chain/actors/builtin/market"
"github.com/filecoin-project/lotus/chain/actors/builtin/miner"
2022-09-23 03:28:46 +00:00
"github.com/filecoin-project/lotus/chain/actors/builtin/multisig"
2022-09-28 00:48:30 +00:00
"github.com/filecoin-project/lotus/chain/actors/builtin/power"
"github.com/filecoin-project/lotus/chain/consensus/filcns"
"github.com/filecoin-project/lotus/chain/state"
"github.com/filecoin-project/lotus/chain/store"
2022-09-23 03:28:46 +00:00
"github.com/filecoin-project/lotus/chain/types"
2022-09-28 00:48:30 +00:00
"github.com/filecoin-project/lotus/node/repo"
2022-09-23 03:28:46 +00:00
)
2022-09-28 19:51:59 +00:00
type Option uint64
const (
Approve Option = 49
Reject = 50
)
2022-09-23 03:28:46 +00:00
type Vote struct {
2022-09-28 19:51:59 +00:00
ID uint64
OptionID Option
2022-09-28 00:48:30 +00:00
SignerAddress address.Address
2022-09-23 03:28:46 +00:00
}
type msigVote struct {
2022-09-26 12:13:00 +00:00
Multisig msigBriefInfo
ApproveCount uint64
RejectCount uint64
2022-09-23 03:28:46 +00:00
}
// https://filpoll.io/poll/16
// snapshot height: 2162760
// state root: bafy2bzacebdnzh43hw66bmvguk65wiwr5ssaejlq44fpdei2ysfh3eefpdlqs
2022-09-26 12:13:00 +00:00
var fip36PollCmd = &cli.Command{
Name: "fip36poll",
2022-09-23 03:28:46 +00:00
Usage: "Process the FIP0036 FilPoll result",
ArgsUsage: "[state root, votes]",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "repo",
Value: "~/.lotus",
},
},
Subcommands: []*cli.Command{
2022-09-28 00:48:30 +00:00
finalResultCmd,
2022-09-23 03:28:46 +00:00
},
}
2022-09-28 00:48:30 +00:00
var finalResultCmd = &cli.Command{
Name: "results",
Usage: "get poll results",
ArgsUsage: "[state root] [height] [votes json]",
2022-09-25 02:30:00 +00:00
Flags: []cli.Flag{
&cli.StringFlag{
Name: "repo",
Value: "~/.lotus",
},
},
Action: func(cctx *cli.Context) error {
2022-09-28 00:48:30 +00:00
if cctx.NArg() != 3 {
return xerrors.New("filpoll0036 results [state root] [height] [votes.json]")
2022-09-25 02:30:00 +00:00
}
ctx := context.TODO()
if !cctx.Args().Present() {
return fmt.Errorf("must pass state root")
}
sroot, err := cid.Decode(cctx.Args().First())
if err != nil {
return fmt.Errorf("failed to parse input: %w", err)
}
fsrepo, err := repo.NewFS(cctx.String("repo"))
if err != nil {
return err
}
lkrepo, err := fsrepo.Lock(repo.FullNode)
if err != nil {
return err
}
defer lkrepo.Close() //nolint:errcheck
bs, err := lkrepo.Blockstore(ctx, repo.UniversalBlockstore)
if err != nil {
return fmt.Errorf("failed to open blockstore: %w", err)
}
defer func() {
if c, ok := bs.(io.Closer); ok {
if err := c.Close(); err != nil {
log.Warnf("failed to close blockstore: %s", err)
}
}
}()
mds, err := lkrepo.Datastore(context.Background(), "/metadata")
if err != nil {
return err
}
cs := store.NewChainStore(bs, bs, mds, filcns.Weight, nil)
defer cs.Close() //nolint:errcheck
cst := cbor.NewCborStore(bs)
store := adt.WrapStore(ctx, cst)
2022-09-28 00:48:30 +00:00
st, err := state.LoadStateTree(cst, sroot)
if err != nil {
return err
}
height, err := strconv.Atoi(cctx.Args().Get(1))
2022-09-25 02:30:00 +00:00
if err != nil {
return err
}
2022-09-27 20:58:25 +00:00
//get all the votes' signer ID address && their vote
2022-09-28 00:48:30 +00:00
vj, err := homedir.Expand(cctx.Args().Get(2))
2022-09-25 02:30:00 +00:00
if err != nil {
return xerrors.Errorf("fail to get votes json")
}
2022-09-28 19:51:59 +00:00
votes, err := getVotesMap(vj)
2022-09-25 02:30:00 +00:00
if err != nil {
2022-09-28 00:48:30 +00:00
return xerrors.Errorf("failed to get voters: ", err)
2022-09-25 02:30:00 +00:00
}
2022-09-28 00:48:30 +00:00
type minerBriefInfo struct {
rawBytePower abi.StoragePower
dealPower abi.StoragePower
balance abi.TokenAmount
2022-09-25 02:30:00 +00:00
}
2022-09-28 00:48:30 +00:00
// power actor
pa, err := st.GetActor(power.Address)
if err != nil {
return xerrors.Errorf("failed to get power actor: \n", err)
2022-09-26 12:13:00 +00:00
}
2022-09-25 02:30:00 +00:00
2022-09-28 00:48:30 +00:00
powerState, err := power.Load(store, pa)
if err != nil {
return xerrors.Errorf("failed to get power state: \n", err)
2022-09-25 02:30:00 +00:00
}
2022-09-26 12:13:00 +00:00
2022-09-28 00:48:30 +00:00
//market actor
ma, err := st.GetActor(market.Address)
if err != nil {
return xerrors.Errorf("fail to get market actor: ", err)
2022-09-23 03:28:46 +00:00
}
2022-09-28 00:48:30 +00:00
marketState, err := market.Load(store, ma)
if err != nil {
return xerrors.Errorf("fail to load market state: ", err)
2022-09-23 03:28:46 +00:00
}
2022-09-28 00:48:30 +00:00
lookupId := func(addr address.Address) address.Address {
2022-09-28 19:51:59 +00:00
ret, err := st.LookupID(addr)
if err != nil {
2022-09-28 00:48:30 +00:00
panic(err)
}
2022-09-28 19:51:59 +00:00
return ret
2022-09-28 00:48:30 +00:00
}
// we need to build several pieces of information, as we traverse the state tree:
// a map of accounts to every msig that they are a signer of
accountsToMultisigs := make(map[address.Address][]address.Address)
// a map of multisigs to some info about them for quick lookup
msigActorsInfo := make(map[address.Address]msigBriefInfo)
2022-09-28 19:51:59 +00:00
// a map of actors (accounts+multisigs) to every miner that they are an owner of
2022-09-28 00:48:30 +00:00
ownerMap := make(map[address.Address][]address.Address)
// a map of accounts to every miner that they are a worker of
workerMap := make(map[address.Address][]address.Address)
2022-09-28 19:51:59 +00:00
// a map of miners to some info about them for quick lookup
2022-09-28 00:48:30 +00:00
minerActorsInfo := make(map[address.Address]minerBriefInfo)
// a map of client addresses to deal data stored in proposals
clientToDealStorage := make(map[address.Address]abi.StoragePower)
fmt.Println("iterating over all actors")
count := 0
err = st.ForEach(func(addr address.Address, act *types.Actor) error {
if count%200000 == 0 {
fmt.Println("processed ", count, " actors building maps")
}
count++
if builtin.IsMultisigActor(act.Code) {
ms, err := multisig.Load(store, act)
if err != nil {
return fmt.Errorf("load msig failed %v", err)
2022-09-23 03:28:46 +00:00
}
2022-09-28 00:48:30 +00:00
// TODO: Confirm that these are always ID addresses
signers, err := ms.Signers()
if err != nil {
return xerrors.Errorf("fail to get msig signers", err)
}
for _, s := range signers {
signerId := lookupId(s)
2022-09-28 19:51:59 +00:00
accountsToMultisigs[signerId] = append(accountsToMultisigs[signerId], addr)
2022-09-28 00:48:30 +00:00
}
locked, err := ms.LockedBalance(abi.ChainEpoch(height))
if err != nil {
return xerrors.Errorf("failed to compute locked multisig balance: %w", err)
}
threshold, _ := ms.Threshold()
info := msigBriefInfo{
ID: addr,
Signer: signers,
2022-09-28 19:51:59 +00:00
Balance: big.Max(big.Zero(), types.BigSub(act.Balance, locked)),
2022-09-28 00:48:30 +00:00
Threshold: threshold,
}
msigActorsInfo[addr] = info
2022-09-23 03:28:46 +00:00
}
2022-09-28 00:48:30 +00:00
if builtin.IsStorageMinerActor(act.Code) {
m, err := miner.Load(store, act)
if err != nil {
return xerrors.Errorf("fail to load miner actor: \n", err)
}
2022-09-23 03:28:46 +00:00
2022-09-28 00:48:30 +00:00
info, err := m.Info()
if err != nil {
return xerrors.Errorf("fail to get miner info: \n", err)
}
2022-09-23 03:28:46 +00:00
2022-09-28 00:48:30 +00:00
ownerId := lookupId(info.Owner)
2022-09-28 19:51:59 +00:00
ownerMap[ownerId] = append(ownerMap[ownerId], addr)
2022-09-28 00:48:30 +00:00
workerId := lookupId(info.Worker)
2022-09-28 19:51:59 +00:00
workerMap[workerId] = append(workerMap[workerId], addr)
2022-09-28 00:48:30 +00:00
2022-09-28 19:51:59 +00:00
lockedFunds, err := m.LockedFunds()
2022-09-28 00:48:30 +00:00
if err != nil {
return err
}
2022-09-28 19:51:59 +00:00
bal := big.Sub(act.Balance, lockedFunds.TotalLockedFunds())
bal = big.Max(big.Zero(), bal)
2022-09-28 00:48:30 +00:00
pow, ok, err := powerState.MinerPower(addr)
if err != nil {
return err
}
if !ok {
pow.RawBytePower = big.Zero()
}
minerActorsInfo[addr] = minerBriefInfo{
rawBytePower: pow.RawBytePower,
// gets added up outside this loop
dealPower: big.Zero(),
balance: bal,
}
}
return nil
})
2022-09-23 03:28:46 +00:00
if err != nil {
return err
}
2022-09-28 00:48:30 +00:00
fmt.Println("iterating over proposals")
2022-09-29 15:21:36 +00:00
dealProposals, err := marketState.Proposals()
if err != nil {
return err
}
dealStates, err := marketState.States()
if err != nil {
return err
}
if err := dealProposals.ForEach(func(dealID abi.DealID, d market.DealProposal) error {
dealState, ok, err := dealStates.Get(dealID)
if err != nil {
return err
}
if !ok || dealState.SectorStartEpoch == -1 {
// effectively a continue
return nil
}
2022-09-28 00:48:30 +00:00
clientId := lookupId(d.Client)
if cd, found := clientToDealStorage[clientId]; found {
clientToDealStorage[clientId] = big.Add(cd, big.NewInt(int64(d.PieceSize)))
} else {
clientToDealStorage[clientId] = big.NewInt(int64(d.PieceSize))
}
providerId := lookupId(d.Provider)
mai, found := minerActorsInfo[providerId]
if !found {
return xerrors.Errorf("didn't find miner %s", providerId)
}
mai.dealPower = big.Add(mai.dealPower, big.NewInt(int64(d.PieceSize)))
minerActorsInfo[providerId] = mai
return nil
}); err != nil {
return xerrors.Errorf("fail to get deals")
2022-09-23 03:28:46 +00:00
}
2022-09-28 00:48:30 +00:00
// now tabulate votes
2022-09-26 12:13:00 +00:00
approveBalance := abi.NewTokenAmount(0)
2022-09-23 03:28:46 +00:00
rejectionBalance := abi.NewTokenAmount(0)
2022-09-28 00:48:30 +00:00
clientApproveBytes := big.Zero()
clientRejectBytes := big.Zero()
2022-09-26 12:13:00 +00:00
msigPendingVotes := make(map[address.Address]msigVote) //map[msig ID]msigVote
2022-09-28 19:51:59 +00:00
msigVotes := make(map[address.Address]Option)
minerVotes := make(map[address.Address]Option)
2022-09-28 00:48:30 +00:00
fmt.Println("counting account and multisig votes")
2022-09-28 19:51:59 +00:00
for _, vote := range votes {
signerId, err := st.LookupID(vote.SignerAddress)
if err != nil {
fmt.Println("voter ", vote.SignerAddress, " not found in state tree, skipping")
continue
}
2022-09-26 12:13:00 +00:00
//process votes for regular accounts
2022-09-28 00:48:30 +00:00
accountActor, err := st.GetActor(signerId)
2022-09-23 03:28:46 +00:00
if err != nil {
2022-09-26 12:13:00 +00:00
return xerrors.Errorf("fail to get account account for signer: ", err)
2022-09-23 03:28:46 +00:00
}
2022-09-28 00:48:30 +00:00
clientBytes, ok := clientToDealStorage[signerId]
if !ok {
clientBytes = big.Zero()
}
2022-09-28 19:51:59 +00:00
if vote.OptionID == Approve {
2022-09-28 00:48:30 +00:00
approveBalance = types.BigAdd(approveBalance, accountActor.Balance)
clientApproveBytes = big.Add(clientApproveBytes, clientBytes)
2022-09-23 03:28:46 +00:00
} else {
2022-09-28 00:48:30 +00:00
rejectionBalance = types.BigAdd(rejectionBalance, accountActor.Balance)
clientRejectBytes = big.Add(clientRejectBytes, clientBytes)
2022-09-23 03:28:46 +00:00
}
2022-09-28 19:51:59 +00:00
if minerInfos, found := ownerMap[signerId]; found {
for _, minerInfo := range minerInfos {
minerVotes[minerInfo] = vote.OptionID
}
}
if minerInfos, found := workerMap[signerId]; found {
for _, minerInfo := range minerInfos {
if _, ok := minerVotes[minerInfo]; !ok {
minerVotes[minerInfo] = vote.OptionID
}
}
}
2022-09-26 12:13:00 +00:00
//process msigs
2022-09-29 15:21:36 +00:00
// There is a possibility that enough signers have voted for BOTH options in the poll to be above the threshold
2022-09-28 19:51:59 +00:00
// Because we are iterating over votes in order they arrived, the first option to go over the threshold will win
// This is in line with onchain behaviour (consider a case where signers are competing to withdraw all the funds
// in an msig into 2 different accounts)
2022-09-28 00:48:30 +00:00
if mss, found := accountsToMultisigs[signerId]; found {
2022-09-27 20:58:25 +00:00
for _, ms := range mss { //get all the msig signer has
2022-09-28 19:51:59 +00:00
if _, ok := msigVotes[ms]; ok {
2022-09-28 00:48:30 +00:00
// msig has already voted, skip
continue
}
2022-09-26 12:13:00 +00:00
if mpv, found := msigPendingVotes[ms]; found { //other signers of the multisig have voted, yet the threshold has not met
2022-09-28 19:51:59 +00:00
if vote.OptionID == Approve {
2022-09-26 12:13:00 +00:00
if mpv.ApproveCount+1 == mpv.Multisig.Threshold { //met threshold
approveBalance = types.BigAdd(approveBalance, mpv.Multisig.Balance)
delete(msigPendingVotes, ms) //threshold, can skip later signer votes
2022-09-28 19:51:59 +00:00
msigVotes[ms] = vote.OptionID
2022-09-28 00:48:30 +00:00
2022-09-23 03:28:46 +00:00
} else {
2022-09-28 00:48:30 +00:00
mpv.ApproveCount++
2022-09-26 12:13:00 +00:00
msigPendingVotes[ms] = mpv
2022-09-23 03:28:46 +00:00
}
} else {
2022-09-26 12:13:00 +00:00
if mpv.RejectCount+1 == mpv.Multisig.Threshold { //met threshold
rejectionBalance = types.BigAdd(rejectionBalance, mpv.Multisig.Balance)
delete(msigPendingVotes, ms) //threshold, can skip later signer votes
2022-09-28 19:51:59 +00:00
msigVotes[ms] = vote.OptionID
2022-09-28 00:48:30 +00:00
2022-09-23 03:28:46 +00:00
} else {
2022-09-28 00:48:30 +00:00
mpv.RejectCount++
2022-09-26 12:13:00 +00:00
msigPendingVotes[ms] = mpv
2022-09-23 03:28:46 +00:00
}
2022-09-26 12:13:00 +00:00
}
} else { //first vote received from one of the signers of the msig
2022-09-28 00:48:30 +00:00
msi, ok := msigActorsInfo[ms]
if !ok {
return xerrors.Errorf("didn't find msig %s in msig map", ms)
2022-09-26 12:13:00 +00:00
}
2022-09-23 03:28:46 +00:00
2022-09-28 00:48:30 +00:00
if msi.Threshold == 1 { //met threshold with this signer's single vote
2022-09-28 19:51:59 +00:00
if vote.OptionID == Approve {
2022-09-28 00:48:30 +00:00
approveBalance = types.BigAdd(approveBalance, msi.Balance)
2022-09-28 19:51:59 +00:00
msigVotes[ms] = Approve
2022-09-28 00:48:30 +00:00
2022-09-26 12:13:00 +00:00
} else {
2022-09-28 00:48:30 +00:00
rejectionBalance = types.BigAdd(rejectionBalance, msi.Balance)
2022-09-28 19:51:59 +00:00
msigVotes[ms] = Reject
2022-09-26 12:13:00 +00:00
}
} else { //threshold not met, add to pending vote
2022-09-28 19:51:59 +00:00
if vote.OptionID == Approve {
2022-09-26 12:13:00 +00:00
msigPendingVotes[ms] = msigVote{
2022-09-28 00:48:30 +00:00
Multisig: msi,
2022-09-26 12:13:00 +00:00
ApproveCount: 1,
}
2022-09-23 03:28:46 +00:00
} else {
2022-09-26 12:13:00 +00:00
msigPendingVotes[ms] = msigVote{
2022-09-28 00:48:30 +00:00
Multisig: msi,
2022-09-26 12:13:00 +00:00
RejectCount: 1,
}
2022-09-23 03:28:46 +00:00
}
}
}
}
}
}
2022-09-28 19:51:59 +00:00
for s, v := range msigVotes {
2022-09-28 00:48:30 +00:00
if minerInfos, found := ownerMap[s]; found {
for _, minerInfo := range minerInfos {
minerVotes[minerInfo] = v
}
}
if minerInfos, found := workerMap[s]; found {
for _, minerInfo := range minerInfos {
if _, ok := minerVotes[minerInfo]; !ok {
minerVotes[minerInfo] = v
}
}
}
2022-09-26 12:13:00 +00:00
}
2022-09-28 00:48:30 +00:00
approveRBP := big.Zero()
approveDealPower := big.Zero()
rejectionRBP := big.Zero()
rejectionDealPower := big.Zero()
fmt.Println("adding up miner votes")
for minerAddr, vote := range minerVotes {
mbi, ok := minerActorsInfo[minerAddr]
if !ok {
return xerrors.Errorf("failed to find miner info for %s", minerAddr)
}
2022-09-28 19:51:59 +00:00
if vote == Approve {
2022-09-28 00:48:30 +00:00
approveBalance = big.Add(approveBalance, mbi.balance)
approveRBP = big.Add(approveRBP, mbi.rawBytePower)
approveDealPower = big.Add(approveDealPower, mbi.dealPower)
2022-09-26 12:13:00 +00:00
} else {
2022-09-28 00:48:30 +00:00
rejectionBalance = big.Add(rejectionBalance, mbi.balance)
rejectionRBP = big.Add(rejectionRBP, mbi.rawBytePower)
rejectionDealPower = big.Add(rejectionDealPower, mbi.dealPower)
2022-09-26 12:13:00 +00:00
}
}
2022-09-28 00:48:30 +00:00
fmt.Println("Total acceptance token: ", approveBalance)
fmt.Println("Total rejection token: ", rejectionBalance)
2022-09-23 20:00:22 +00:00
2022-09-28 00:48:30 +00:00
fmt.Println("Total acceptance SP deal power: ", approveDealPower)
fmt.Println("Total rejection SP deal power: ", rejectionDealPower)
2022-09-23 20:00:22 +00:00
2022-09-28 00:48:30 +00:00
fmt.Println("Total acceptance SP rb power: ", approveRBP)
fmt.Println("Total rejection SP rb power: ", rejectionRBP)
2022-09-23 20:00:22 +00:00
2022-09-28 00:48:30 +00:00
fmt.Println("Total acceptance Client rb power: ", clientApproveBytes)
fmt.Println("Total rejection Client rb power: ", clientRejectBytes)
2022-09-23 20:00:22 +00:00
2022-09-28 00:48:30 +00:00
fmt.Println("\n\nFinal results **drumroll**")
2022-09-28 19:51:59 +00:00
if rejectionBalance.GreaterThanEqual(big.Mul(approveBalance, big.NewInt(3))) {
2022-09-29 15:21:36 +00:00
fmt.Println("token holders VETO FIP-0036!")
2022-09-28 00:48:30 +00:00
} else if approveBalance.LessThanEqual(rejectionBalance) {
2022-09-29 15:21:36 +00:00
fmt.Println("token holders REJECT FIP-0036")
2022-09-28 00:48:30 +00:00
} else {
2022-09-29 15:21:36 +00:00
fmt.Println("token holders ACCEPT FIP-0036")
2022-09-23 20:00:22 +00:00
}
2022-09-28 19:51:59 +00:00
if rejectionDealPower.GreaterThanEqual(big.Mul(approveDealPower, big.NewInt(3))) {
2022-09-29 15:21:36 +00:00
fmt.Println("SPs by deal data stored VETO FIP-0036!")
2022-09-28 00:48:30 +00:00
} else if approveDealPower.LessThanEqual(rejectionDealPower) {
2022-09-29 15:21:36 +00:00
fmt.Println("SPs by deal data stored REJECT FIP-0036")
2022-09-28 00:48:30 +00:00
} else {
2022-09-29 15:21:36 +00:00
fmt.Println("SPs by deal data stored ACCEPT FIP-0036")
2022-09-23 20:00:22 +00:00
}
2022-09-28 19:51:59 +00:00
if rejectionRBP.GreaterThanEqual(big.Mul(approveRBP, big.NewInt(3))) {
2022-09-29 15:21:36 +00:00
fmt.Println("SPs by total raw byte power VETO FIP-0036!")
2022-09-28 00:48:30 +00:00
} else if approveRBP.LessThanEqual(rejectionRBP) {
2022-09-29 15:21:36 +00:00
fmt.Println("SPs by total raw byte power REJECT FIP-0036")
2022-09-28 00:48:30 +00:00
} else {
2022-09-29 15:21:36 +00:00
fmt.Println("SPs by total raw byte power ACCEPT FIP-0036")
2022-09-23 20:00:22 +00:00
}
2022-09-28 19:51:59 +00:00
if clientRejectBytes.GreaterThanEqual(big.Mul(clientApproveBytes, big.NewInt(3))) {
2022-09-29 15:21:36 +00:00
fmt.Println("Storage Clients VETO FIP-0036!")
2022-09-28 00:48:30 +00:00
} else if clientApproveBytes.LessThanEqual(clientRejectBytes) {
2022-09-29 15:21:36 +00:00
fmt.Println("Storage Clients REJECT FIP-0036")
2022-09-28 00:48:30 +00:00
} else {
2022-09-29 15:21:36 +00:00
fmt.Println("Storage Clients ACCEPT FIP-0036")
2022-09-23 20:00:22 +00:00
}
2022-09-28 00:48:30 +00:00
return nil
},
}
2022-09-23 20:00:22 +00:00
2022-09-28 19:51:59 +00:00
// Returns voted sorted by votes from earliest to latest
func getVotesMap(file string) ([]Vote, error) {
2022-09-28 00:48:30 +00:00
var votes []Vote
vb, err := ioutil.ReadFile(file)
if err != nil {
return nil, xerrors.Errorf("read vote: %w", err)
}
2022-09-23 20:00:22 +00:00
2022-09-28 00:48:30 +00:00
if err := json.Unmarshal(vb, &votes); err != nil {
return nil, xerrors.Errorf("unmarshal vote: %w", err)
}
2022-09-23 20:00:22 +00:00
2022-09-28 19:51:59 +00:00
sort.SliceStable(votes, func(i, j int) bool {
return votes[i].ID < votes[j].ID
})
return votes, nil
2022-09-23 20:00:22 +00:00
}