Tipset traversal for speed

This commit is contained in:
ZenGround0 2020-10-12 23:53:14 -04:00
parent 79d94c4771
commit 768d2db114

View File

@ -3,7 +3,10 @@ package main
import ( import (
"fmt" "fmt"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/abi" "github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/big"
lapi "github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/chain/types" "github.com/filecoin-project/lotus/chain/types"
lcli "github.com/filecoin-project/lotus/cli" lcli "github.com/filecoin-project/lotus/cli"
"github.com/filecoin-project/specs-actors/v2/actors/builtin" "github.com/filecoin-project/specs-actors/v2/actors/builtin"
@ -12,7 +15,7 @@ import (
var postFindCmd = &cli.Command{ var postFindCmd = &cli.Command{
Name: "post-find", Name: "post-find",
Description: "return addresses of all miners who have posted in the last day", Description: "return addresses of all miners who have over zero power and have posted in the last day",
Flags: []cli.Flag{ Flags: []cli.Flag{
&cli.StringFlag{ &cli.StringFlag{
Name: "tipset", Name: "tipset",
@ -39,11 +42,30 @@ var postFindCmd = &cli.Command{
} }
oneDayAgo := ts.Height() - abi.ChainEpoch(2880) oneDayAgo := ts.Height() - abi.ChainEpoch(2880)
// Get all messages over the last day
msgs := make([]*types.Message, 0)
for ts.Height() > oneDayAgo {
// Get messages on ts parent
next, err := api.ChainGetParentMessages(ctx, ts.Cids()[0])
if err != nil {
return err
}
msgs = append(msgs, messagesFromAPIMessages(next)...)
// Next ts
ts, err = api.ChainGetTipSet(ctx, ts.Parents())
if err != nil {
return err
}
}
fmt.Printf("Loaded messages to height %d\n", ts.Height())
mAddrs, err := api.StateListMiners(ctx, ts.Key()) mAddrs, err := api.StateListMiners(ctx, ts.Key())
if err != nil { if err != nil {
return err return err
} }
minersWithPower := make(map[address.Address]struct{})
for _, mAddr := range mAddrs { for _, mAddr := range mAddrs {
// if they have no power ignore. This filters out 14k inactive miners // if they have no power ignore. This filters out 14k inactive miners
// so we can do 100x fewer expensive message queries // so we can do 100x fewer expensive message queries
@ -51,26 +73,32 @@ var postFindCmd = &cli.Command{
if err != nil { if err != nil {
return err return err
} }
if !power.HasMinPower { if power.MinerPower.RawBytePower.GreaterThan(big.Zero()) {
continue minersWithPower[mAddr] = struct{}{}
} }
query := &types.Message{To: mAddr} }
mCids, err := api.StateListMessages(ctx, query, ts.Key(), oneDayAgo) fmt.Printf("Loaded %d miners with power\n", len(minersWithPower))
if err != nil {
return err postedMiners := make(map[address.Address]struct{})
} for _, msg := range msgs {
for _, mCid := range mCids { _, hasPower := minersWithPower[msg.To]
msg, err := api.ChainGetMessage(ctx, mCid) _, seenBefore := postedMiners[msg.To]
if err != nil {
return err if hasPower && !seenBefore {
}
if msg.Method == builtin.MethodsMiner.SubmitWindowedPoSt { if msg.Method == builtin.MethodsMiner.SubmitWindowedPoSt {
fmt.Printf("%s\n", mAddr) fmt.Printf("%s\n", msg.To)
break // go to next mAddr postedMiners[msg.To] = struct{}{}
} }
} }
} }
return nil return nil
}, },
} }
func messagesFromAPIMessages(apiMessages []lapi.Message) []*types.Message {
messages := make([]*types.Message, len(apiMessages))
for i, apiMessage := range apiMessages {
messages[i] = apiMessage.Message
}
return messages
}