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

566 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 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
cbg "github.com/whyrusleeping/cbor-gen"
"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
_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/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
)
const APPROVE = 49
2022-09-25 02:30:00 +00:00
const Reject = 50
2022-09-23 03:28:46 +00:00
type Vote struct {
2022-09-28 00:48:30 +00:00
OptionID uint64
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 00:48:30 +00:00
votes, err := getVotesMap(vj, st)
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
//init actor
ia, err := st.GetActor(_init.Address)
2022-09-23 03:28:46 +00:00
if err != nil {
2022-09-28 00:48:30 +00:00
return xerrors.Errorf("fail to get init actor: ", err)
2022-09-23 03:28:46 +00:00
}
2022-09-28 00:48:30 +00:00
initState, err := _init.Load(store, ia)
2022-09-23 03:28:46 +00:00
if err != nil {
2022-09-28 00:48:30 +00:00
return xerrors.Errorf("fail to load init state: ", err)
2022-09-23 03:28:46 +00:00
}
2022-09-28 00:48:30 +00:00
initMap, err := initState.AddressMap()
2022-09-23 03:28:46 +00:00
if err != nil {
2022-09-28 00:48:30 +00:00
return xerrors.Errorf("fail to load init map: ", err)
2022-09-23 03:28:46 +00:00
}
2022-09-28 00:48:30 +00:00
lookupId := func(addr address.Address) address.Address {
if addr.Protocol() == address.ID {
return addr
}
2022-09-23 03:28:46 +00:00
2022-09-28 00:48:30 +00:00
var actorID cbg.CborInt
if found, err := initMap.Get(abi.AddrKey(addr), &actorID); err != nil {
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")
}
// 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)
// a map of accounts to every miner that they are an owner of
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)
// a map of miners to some info aboout them for quick lookup
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)
if m, found := accountsToMultisigs[signerId]; found { //add msig id to signer's collection
m = append(m, addr)
accountsToMultisigs[signerId] = m
} else {
n := []address.Address{addr}
accountsToMultisigs[signerId] = n
}
}
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,
Balance: big.Min(big.Zero(), types.BigSub(act.Balance, locked)),
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)
if m, found := ownerMap[ownerId]; found { //add miner id to owner list
m = append(m, addr)
ownerMap[ownerId] = m
} else {
n := []address.Address{addr}
ownerMap[ownerId] = n
}
workerId := lookupId(info.Worker)
if m, found := workerMap[workerId]; found { //add miner id to worker list
m = append(m, addr)
workerMap[workerId] = m
} else {
n := []address.Address{addr}
workerMap[workerId] = n
}
bal, err := m.AvailableBalance(act.Balance)
if err != nil {
return err
}
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")
dps, _ := marketState.Proposals()
if err := dps.ForEach(func(dealID abi.DealID, d market.DealProposal) error {
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 00:48:30 +00:00
votedMsigs := make(map[address.Address]struct{})
votesIncludingMsigs := make(map[address.Address]uint64)
fmt.Println("counting account and multisig votes")
for signer, v := range votes {
signerId := lookupId(signer)
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-26 12:13:00 +00:00
if v == APPROVE {
2022-09-28 00:48:30 +00:00
approveBalance = types.BigAdd(approveBalance, accountActor.Balance)
votesIncludingMsigs[signerId] = APPROVE
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)
votesIncludingMsigs[signerId] = Reject
clientRejectBytes = big.Add(clientRejectBytes, clientBytes)
2022-09-23 03:28:46 +00:00
}
2022-09-26 12:13:00 +00:00
//process msigs
2022-09-28 00:48:30 +00:00
// TODO: Oh god, oh god, there's a possibility that a multisig has voted in both directions
// and we'll pick a winner non-deterministically as we iterate...
// We need to factor in vote time if that happens and pick whoever went first
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 00:48:30 +00:00
if _, ok := votedMsigs[ms]; ok {
// 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
if v == APPROVE {
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 00:48:30 +00:00
votedMsigs[ms] = struct{}{}
votesIncludingMsigs[ms] = APPROVE
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 00:48:30 +00:00
votedMsigs[ms] = struct{}{}
votesIncludingMsigs[ms] = Reject
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-26 12:13:00 +00:00
if v == APPROVE {
2022-09-28 00:48:30 +00:00
approveBalance = types.BigAdd(approveBalance, msi.Balance)
votesIncludingMsigs[ms] = APPROVE
2022-09-26 12:13:00 +00:00
} else {
2022-09-28 00:48:30 +00:00
rejectionBalance = types.BigAdd(rejectionBalance, msi.Balance)
votesIncludingMsigs[ms] = Reject
2022-09-26 12:13:00 +00:00
}
2022-09-28 00:48:30 +00:00
votedMsigs[ms] = struct{}{}
2022-09-26 12:13:00 +00:00
} else { //threshold not met, add to pending vote
if v == APPROVE {
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 00:48:30 +00:00
// time to process miners based on what we know about votesIncludingMsigs
minerVotes := make(map[address.Address]uint64)
fmt.Println("counting miner votes")
for s, v := range votes {
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)
}
if vote == APPROVE {
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**")
if rejectionBalance.GreaterThanEqual(big.Mul(approveBalance, big.NewInt(2))) {
fmt.Println("token holders VETO FIP-0036!!!")
} else if approveBalance.LessThanEqual(rejectionBalance) {
fmt.Println("token holders REJECT FIP-0036 :(")
} else {
fmt.Println("token holders ACCEPT FIP-0036 :)")
2022-09-23 20:00:22 +00:00
}
2022-09-28 00:48:30 +00:00
if rejectionDealPower.GreaterThanEqual(big.Mul(approveDealPower, big.NewInt(2))) {
fmt.Println("SPs by deall data stored VETO FIP-0036!!!")
} else if approveDealPower.LessThanEqual(rejectionDealPower) {
fmt.Println("SPs by deal data stored REJECT FIP-0036 :(")
} else {
fmt.Println("SPs by deal data stored ACCEPT FIP-0036 :)")
2022-09-23 20:00:22 +00:00
}
2022-09-28 00:48:30 +00:00
if rejectionRBP.GreaterThanEqual(big.Mul(approveRBP, big.NewInt(2))) {
fmt.Println("SPs by total raw byte power VETO FIP-0036!!!")
} else if approveRBP.LessThanEqual(rejectionRBP) {
fmt.Println("SPs by total raw byte power REJECT FIP-0036 :(")
} else {
fmt.Println("SPs by total raw byte power ACCEPT FIP-0036 :)")
2022-09-23 20:00:22 +00:00
}
2022-09-28 00:48:30 +00:00
if clientRejectBytes.GreaterThanEqual(big.Mul(clientApproveBytes, big.NewInt(2))) {
fmt.Println("Storage Clients VETO FIP-0036!!!")
} else if clientApproveBytes.LessThanEqual(clientRejectBytes) {
fmt.Println("Storage Clients REJECT FIP-0036 :(")
} else {
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 00:48:30 +00:00
func getVotesMap(file string, st *state.StateTree) (map[address.Address]uint64 /*map[Signer ID address]Option*/, error) {
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 00:48:30 +00:00
vm := make(map[address.Address]uint64)
for _, v := range votes {
si, err := st.LookupID(v.SignerAddress)
2022-09-23 20:00:22 +00:00
if err != nil {
2022-09-28 00:48:30 +00:00
return nil, xerrors.Errorf("fail to lookup address", err)
2022-09-23 20:00:22 +00:00
}
2022-09-28 00:48:30 +00:00
vm[si] = v.OptionID
}
return vm, nil
2022-09-23 20:00:22 +00:00
}