address review

This commit is contained in:
Aayush 2022-09-28 15:51:59 -04:00
parent 87597cbc33
commit 9928c1ac18

View File

@ -6,13 +6,13 @@ import (
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
"sort"
"strconv" "strconv"
"github.com/ipfs/go-cid" "github.com/ipfs/go-cid"
cbor "github.com/ipfs/go-ipld-cbor" cbor "github.com/ipfs/go-ipld-cbor"
"github.com/mitchellh/go-homedir" "github.com/mitchellh/go-homedir"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
cbg "github.com/whyrusleeping/cbor-gen"
"golang.org/x/xerrors" "golang.org/x/xerrors"
"github.com/filecoin-project/go-address" "github.com/filecoin-project/go-address"
@ -21,7 +21,6 @@ import (
"github.com/filecoin-project/lotus/chain/actors/adt" "github.com/filecoin-project/lotus/chain/actors/adt"
"github.com/filecoin-project/lotus/chain/actors/builtin" "github.com/filecoin-project/lotus/chain/actors/builtin"
_init "github.com/filecoin-project/lotus/chain/actors/builtin/init"
"github.com/filecoin-project/lotus/chain/actors/builtin/market" "github.com/filecoin-project/lotus/chain/actors/builtin/market"
"github.com/filecoin-project/lotus/chain/actors/builtin/miner" "github.com/filecoin-project/lotus/chain/actors/builtin/miner"
"github.com/filecoin-project/lotus/chain/actors/builtin/multisig" "github.com/filecoin-project/lotus/chain/actors/builtin/multisig"
@ -33,11 +32,16 @@ import (
"github.com/filecoin-project/lotus/node/repo" "github.com/filecoin-project/lotus/node/repo"
) )
const APPROVE = 49 type Option uint64
const Reject = 50
const (
Approve Option = 49
Reject = 50
)
type Vote struct { type Vote struct {
OptionID uint64 ID uint64
OptionID Option
SignerAddress address.Address SignerAddress address.Address
} }
@ -142,7 +146,7 @@ var finalResultCmd = &cli.Command{
if err != nil { if err != nil {
return xerrors.Errorf("fail to get votes json") return xerrors.Errorf("fail to get votes json")
} }
votes, err := getVotesMap(vj, st) votes, err := getVotesMap(vj)
if err != nil { if err != nil {
return xerrors.Errorf("failed to get voters: ", err) return xerrors.Errorf("failed to get voters: ", err)
} }
@ -175,40 +179,13 @@ var finalResultCmd = &cli.Command{
return xerrors.Errorf("fail to load market state: ", err) return xerrors.Errorf("fail to load market state: ", err)
} }
//init actor
ia, err := st.GetActor(_init.Address)
if err != nil {
return xerrors.Errorf("fail to get init actor: ", err)
}
initState, err := _init.Load(store, ia)
if err != nil {
return xerrors.Errorf("fail to load init state: ", err)
}
initMap, err := initState.AddressMap()
if err != nil {
return xerrors.Errorf("fail to load init map: ", err)
}
lookupId := func(addr address.Address) address.Address { lookupId := func(addr address.Address) address.Address {
if addr.Protocol() == address.ID { ret, err := st.LookupID(addr)
return addr if err != nil {
}
var actorID cbg.CborInt
if found, err := initMap.Get(abi.AddrKey(addr), &actorID); err != nil {
panic(err) panic(err)
} else if found {
// Reconstruct address from the ActorID.
idAddr, err := address.NewIDAddress(uint64(actorID))
if err != nil {
panic(err)
}
return idAddr
} }
panic("didn't find addr") return ret
} }
// we need to build several pieces of information, as we traverse the state tree: // we need to build several pieces of information, as we traverse the state tree:
@ -216,11 +193,11 @@ var finalResultCmd = &cli.Command{
accountsToMultisigs := make(map[address.Address][]address.Address) accountsToMultisigs := make(map[address.Address][]address.Address)
// a map of multisigs to some info about them for quick lookup // a map of multisigs to some info about them for quick lookup
msigActorsInfo := make(map[address.Address]msigBriefInfo) msigActorsInfo := make(map[address.Address]msigBriefInfo)
// a map of accounts to every miner that they are an owner of // a map of actors (accounts+multisigs) to every miner that they are an owner of
ownerMap := make(map[address.Address][]address.Address) ownerMap := make(map[address.Address][]address.Address)
// a map of accounts to every miner that they are a worker of // a map of accounts to every miner that they are a worker of
workerMap := make(map[address.Address][]address.Address) workerMap := make(map[address.Address][]address.Address)
// a map of miners to some info aboout them for quick lookup // a map of miners to some info about them for quick lookup
minerActorsInfo := make(map[address.Address]minerBriefInfo) minerActorsInfo := make(map[address.Address]minerBriefInfo)
// a map of client addresses to deal data stored in proposals // a map of client addresses to deal data stored in proposals
clientToDealStorage := make(map[address.Address]abi.StoragePower) clientToDealStorage := make(map[address.Address]abi.StoragePower)
@ -246,13 +223,7 @@ var finalResultCmd = &cli.Command{
} }
for _, s := range signers { for _, s := range signers {
signerId := lookupId(s) signerId := lookupId(s)
if m, found := accountsToMultisigs[signerId]; found { //add msig id to signer's collection accountsToMultisigs[signerId] = append(accountsToMultisigs[signerId], addr)
m = append(m, addr)
accountsToMultisigs[signerId] = m
} else {
n := []address.Address{addr}
accountsToMultisigs[signerId] = n
}
} }
locked, err := ms.LockedBalance(abi.ChainEpoch(height)) locked, err := ms.LockedBalance(abi.ChainEpoch(height))
@ -264,7 +235,7 @@ var finalResultCmd = &cli.Command{
info := msigBriefInfo{ info := msigBriefInfo{
ID: addr, ID: addr,
Signer: signers, Signer: signers,
Balance: big.Min(big.Zero(), types.BigSub(act.Balance, locked)), Balance: big.Max(big.Zero(), types.BigSub(act.Balance, locked)),
Threshold: threshold, Threshold: threshold,
} }
msigActorsInfo[addr] = info msigActorsInfo[addr] = info
@ -282,28 +253,19 @@ var finalResultCmd = &cli.Command{
} }
ownerId := lookupId(info.Owner) ownerId := lookupId(info.Owner)
if m, found := ownerMap[ownerId]; found { //add miner id to owner list ownerMap[ownerId] = append(ownerMap[ownerId], addr)
m = append(m, addr)
ownerMap[ownerId] = m
} else {
n := []address.Address{addr}
ownerMap[ownerId] = n
}
workerId := lookupId(info.Worker) workerId := lookupId(info.Worker)
if m, found := workerMap[workerId]; found { //add miner id to worker list workerMap[workerId] = append(workerMap[workerId], addr)
m = append(m, addr)
workerMap[workerId] = m
} else {
n := []address.Address{addr}
workerMap[workerId] = n
}
bal, err := m.AvailableBalance(act.Balance) lockedFunds, err := m.LockedFunds()
if err != nil { if err != nil {
return err return err
} }
bal := big.Sub(act.Balance, lockedFunds.TotalLockedFunds())
bal = big.Max(big.Zero(), bal)
pow, ok, err := powerState.MinerPower(addr) pow, ok, err := powerState.MinerPower(addr)
if err != nil { if err != nil {
return err return err
@ -359,11 +321,16 @@ var finalResultCmd = &cli.Command{
clientApproveBytes := big.Zero() clientApproveBytes := big.Zero()
clientRejectBytes := big.Zero() clientRejectBytes := big.Zero()
msigPendingVotes := make(map[address.Address]msigVote) //map[msig ID]msigVote msigPendingVotes := make(map[address.Address]msigVote) //map[msig ID]msigVote
votedMsigs := make(map[address.Address]struct{}) msigVotes := make(map[address.Address]Option)
votesIncludingMsigs := make(map[address.Address]uint64) minerVotes := make(map[address.Address]Option)
fmt.Println("counting account and multisig votes") fmt.Println("counting account and multisig votes")
for signer, v := range votes { for _, vote := range votes {
signerId := lookupId(signer) signerId, err := st.LookupID(vote.SignerAddress)
if err != nil {
fmt.Println("voter ", vote.SignerAddress, " not found in state tree, skipping")
continue
}
//process votes for regular accounts //process votes for regular accounts
accountActor, err := st.GetActor(signerId) accountActor, err := st.GetActor(signerId)
if err != nil { if err != nil {
@ -375,33 +342,44 @@ var finalResultCmd = &cli.Command{
clientBytes = big.Zero() clientBytes = big.Zero()
} }
if v == APPROVE { if vote.OptionID == Approve {
approveBalance = types.BigAdd(approveBalance, accountActor.Balance) approveBalance = types.BigAdd(approveBalance, accountActor.Balance)
votesIncludingMsigs[signerId] = APPROVE
clientApproveBytes = big.Add(clientApproveBytes, clientBytes) clientApproveBytes = big.Add(clientApproveBytes, clientBytes)
} else { } else {
rejectionBalance = types.BigAdd(rejectionBalance, accountActor.Balance) rejectionBalance = types.BigAdd(rejectionBalance, accountActor.Balance)
votesIncludingMsigs[signerId] = Reject
clientRejectBytes = big.Add(clientRejectBytes, clientBytes) clientRejectBytes = big.Add(clientRejectBytes, clientBytes)
} }
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
}
}
}
//process msigs //process msigs
// TODO: Oh god, oh god, there's a possibility that a multisig has voted in both directions // There is a possibilty that enough signers have voted for BOTH options in the poll to be above the threshold
// and we'll pick a winner non-deterministically as we iterate... // Because we are iterating over votes in order they arrived, the first option to go over the threshold will win
// We need to factor in vote time if that happens and pick whoever went first // 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)
if mss, found := accountsToMultisigs[signerId]; found { if mss, found := accountsToMultisigs[signerId]; found {
for _, ms := range mss { //get all the msig signer has for _, ms := range mss { //get all the msig signer has
if _, ok := votedMsigs[ms]; ok { if _, ok := msigVotes[ms]; ok {
// msig has already voted, skip // msig has already voted, skip
continue continue
} }
if mpv, found := msigPendingVotes[ms]; found { //other signers of the multisig have voted, yet the threshold has not met if mpv, found := msigPendingVotes[ms]; found { //other signers of the multisig have voted, yet the threshold has not met
if v == APPROVE { if vote.OptionID == Approve {
if mpv.ApproveCount+1 == mpv.Multisig.Threshold { //met threshold if mpv.ApproveCount+1 == mpv.Multisig.Threshold { //met threshold
approveBalance = types.BigAdd(approveBalance, mpv.Multisig.Balance) approveBalance = types.BigAdd(approveBalance, mpv.Multisig.Balance)
delete(msigPendingVotes, ms) //threshold, can skip later signer votes delete(msigPendingVotes, ms) //threshold, can skip later signer votes
votedMsigs[ms] = struct{}{} msigVotes[ms] = vote.OptionID
votesIncludingMsigs[ms] = APPROVE
} else { } else {
mpv.ApproveCount++ mpv.ApproveCount++
@ -411,8 +389,7 @@ var finalResultCmd = &cli.Command{
if mpv.RejectCount+1 == mpv.Multisig.Threshold { //met threshold if mpv.RejectCount+1 == mpv.Multisig.Threshold { //met threshold
rejectionBalance = types.BigAdd(rejectionBalance, mpv.Multisig.Balance) rejectionBalance = types.BigAdd(rejectionBalance, mpv.Multisig.Balance)
delete(msigPendingVotes, ms) //threshold, can skip later signer votes delete(msigPendingVotes, ms) //threshold, can skip later signer votes
votedMsigs[ms] = struct{}{} msigVotes[ms] = vote.OptionID
votesIncludingMsigs[ms] = Reject
} else { } else {
mpv.RejectCount++ mpv.RejectCount++
@ -426,17 +403,16 @@ var finalResultCmd = &cli.Command{
} }
if msi.Threshold == 1 { //met threshold with this signer's single vote if msi.Threshold == 1 { //met threshold with this signer's single vote
if v == APPROVE { if vote.OptionID == Approve {
approveBalance = types.BigAdd(approveBalance, msi.Balance) approveBalance = types.BigAdd(approveBalance, msi.Balance)
votesIncludingMsigs[ms] = APPROVE msigVotes[ms] = Approve
} else { } else {
rejectionBalance = types.BigAdd(rejectionBalance, msi.Balance) rejectionBalance = types.BigAdd(rejectionBalance, msi.Balance)
votesIncludingMsigs[ms] = Reject msigVotes[ms] = Reject
} }
votedMsigs[ms] = struct{}{}
} else { //threshold not met, add to pending vote } else { //threshold not met, add to pending vote
if v == APPROVE { if vote.OptionID == Approve {
msigPendingVotes[ms] = msigVote{ msigPendingVotes[ms] = msigVote{
Multisig: msi, Multisig: msi,
ApproveCount: 1, ApproveCount: 1,
@ -453,10 +429,7 @@ var finalResultCmd = &cli.Command{
} }
} }
// time to process miners based on what we know about votesIncludingMsigs for s, v := range msigVotes {
minerVotes := make(map[address.Address]uint64)
fmt.Println("counting miner votes")
for s, v := range votes {
if minerInfos, found := ownerMap[s]; found { if minerInfos, found := ownerMap[s]; found {
for _, minerInfo := range minerInfos { for _, minerInfo := range minerInfos {
minerVotes[minerInfo] = v minerVotes[minerInfo] = v
@ -482,7 +455,7 @@ var finalResultCmd = &cli.Command{
return xerrors.Errorf("failed to find miner info for %s", minerAddr) return xerrors.Errorf("failed to find miner info for %s", minerAddr)
} }
if vote == APPROVE { if vote == Approve {
approveBalance = big.Add(approveBalance, mbi.balance) approveBalance = big.Add(approveBalance, mbi.balance)
approveRBP = big.Add(approveRBP, mbi.rawBytePower) approveRBP = big.Add(approveRBP, mbi.rawBytePower)
approveDealPower = big.Add(approveDealPower, mbi.dealPower) approveDealPower = big.Add(approveDealPower, mbi.dealPower)
@ -506,7 +479,7 @@ var finalResultCmd = &cli.Command{
fmt.Println("Total rejection Client rb power: ", clientRejectBytes) fmt.Println("Total rejection Client rb power: ", clientRejectBytes)
fmt.Println("\n\nFinal results **drumroll**") fmt.Println("\n\nFinal results **drumroll**")
if rejectionBalance.GreaterThanEqual(big.Mul(approveBalance, big.NewInt(2))) { if rejectionBalance.GreaterThanEqual(big.Mul(approveBalance, big.NewInt(3))) {
fmt.Println("token holders VETO FIP-0036!!!") fmt.Println("token holders VETO FIP-0036!!!")
} else if approveBalance.LessThanEqual(rejectionBalance) { } else if approveBalance.LessThanEqual(rejectionBalance) {
fmt.Println("token holders REJECT FIP-0036 :(") fmt.Println("token holders REJECT FIP-0036 :(")
@ -514,7 +487,7 @@ var finalResultCmd = &cli.Command{
fmt.Println("token holders ACCEPT FIP-0036 :)") fmt.Println("token holders ACCEPT FIP-0036 :)")
} }
if rejectionDealPower.GreaterThanEqual(big.Mul(approveDealPower, big.NewInt(2))) { if rejectionDealPower.GreaterThanEqual(big.Mul(approveDealPower, big.NewInt(3))) {
fmt.Println("SPs by deall data stored VETO FIP-0036!!!") fmt.Println("SPs by deall data stored VETO FIP-0036!!!")
} else if approveDealPower.LessThanEqual(rejectionDealPower) { } else if approveDealPower.LessThanEqual(rejectionDealPower) {
fmt.Println("SPs by deal data stored REJECT FIP-0036 :(") fmt.Println("SPs by deal data stored REJECT FIP-0036 :(")
@ -522,7 +495,7 @@ var finalResultCmd = &cli.Command{
fmt.Println("SPs by deal data stored ACCEPT FIP-0036 :)") fmt.Println("SPs by deal data stored ACCEPT FIP-0036 :)")
} }
if rejectionRBP.GreaterThanEqual(big.Mul(approveRBP, big.NewInt(2))) { if rejectionRBP.GreaterThanEqual(big.Mul(approveRBP, big.NewInt(3))) {
fmt.Println("SPs by total raw byte power VETO FIP-0036!!!") fmt.Println("SPs by total raw byte power VETO FIP-0036!!!")
} else if approveRBP.LessThanEqual(rejectionRBP) { } else if approveRBP.LessThanEqual(rejectionRBP) {
fmt.Println("SPs by total raw byte power REJECT FIP-0036 :(") fmt.Println("SPs by total raw byte power REJECT FIP-0036 :(")
@ -530,7 +503,7 @@ var finalResultCmd = &cli.Command{
fmt.Println("SPs by total raw byte power ACCEPT FIP-0036 :)") fmt.Println("SPs by total raw byte power ACCEPT FIP-0036 :)")
} }
if clientRejectBytes.GreaterThanEqual(big.Mul(clientApproveBytes, big.NewInt(2))) { if clientRejectBytes.GreaterThanEqual(big.Mul(clientApproveBytes, big.NewInt(3))) {
fmt.Println("Storage Clients VETO FIP-0036!!!") fmt.Println("Storage Clients VETO FIP-0036!!!")
} else if clientApproveBytes.LessThanEqual(clientRejectBytes) { } else if clientApproveBytes.LessThanEqual(clientRejectBytes) {
fmt.Println("Storage Clients REJECT FIP-0036 :(") fmt.Println("Storage Clients REJECT FIP-0036 :(")
@ -542,7 +515,8 @@ var finalResultCmd = &cli.Command{
}, },
} }
func getVotesMap(file string, st *state.StateTree) (map[address.Address]uint64 /*map[Signer ID address]Option*/, error) { // Returns voted sorted by votes from earliest to latest
func getVotesMap(file string) ([]Vote, error) {
var votes []Vote var votes []Vote
vb, err := ioutil.ReadFile(file) vb, err := ioutil.ReadFile(file)
if err != nil { if err != nil {
@ -553,13 +527,9 @@ func getVotesMap(file string, st *state.StateTree) (map[address.Address]uint64 /
return nil, xerrors.Errorf("unmarshal vote: %w", err) return nil, xerrors.Errorf("unmarshal vote: %w", err)
} }
vm := make(map[address.Address]uint64) sort.SliceStable(votes, func(i, j int) bool {
for _, v := range votes { return votes[i].ID < votes[j].ID
si, err := st.LookupID(v.SignerAddress) })
if err != nil {
return nil, xerrors.Errorf("fail to lookup address", err) return votes, nil
}
vm[si] = v.OptionID
}
return vm, nil
} }