Skip to content

Commit

Permalink
feat(lotus-shed): make it possible to filter by to/from when checking…
Browse files Browse the repository at this point in the history
… dups
  • Loading branch information
Stebalien committed Apr 29, 2021
1 parent 77eefcd commit 5dab9f0
Showing 1 changed file with 67 additions and 20 deletions.
87 changes: 67 additions & 20 deletions cmd/lotus-shed/balances.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,27 @@ every day of chain processed.
},
&cli.IntSliceFlag{
Name: "method",
Usage: "Filter results by method number.",
Usage: "filter results by method number",
DefaultText: "all methods",
},
&cli.StringSliceFlag{
Name: "include-to",
Usage: "include only messages to the given address (does not perform address resolution)",
DefaultText: "all recipients",
},
&cli.StringSliceFlag{
Name: "include-from",
Usage: "include only messages from the given address (does not perform address resolution)",
DefaultText: "all senders",
},
&cli.StringSliceFlag{
Name: "exclude-to",
Usage: "exclude messages to the given address (does not perform address resolution)",
},
&cli.StringSliceFlag{
Name: "exclude-from",
Usage: "exclude messages from the given address (does not perform address resolution)",
},
},
Action: func(cctx *cli.Context) error {
api, closer, err := lcli.GetFullNodeAPI(cctx)
Expand Down Expand Up @@ -145,14 +163,35 @@ every day of chain processed.

throttle := make(chan struct{}, threads)

methods := make(map[abi.MethodNum]bool)
methods := map[abi.MethodNum]bool{}
for _, m := range cctx.IntSlice("method") {
if m < 0 {
return fmt.Errorf("expected method numbers to be non-negative")
}
methods[abi.MethodNum(m)] = true
}

addressSet := func(flag string) map[addresses.Address]bool {
if !cctx.IsSet(flag) {
return nil
}
addrs := cctx.StringSlice(flag)
set := make(map[address.Address]bool, len(addrs))
for _, addrStr := range addrs {
addr, err := address.NewFromString(addrStr)
if err != nil {
return fmt.Errorf("failed to parse address %s: %w", addrStr, err)
}
set[addr] = true
}
return set
}

onlyFrom := addressSet("include-from")
onlyTo := addressSet("include-to")
excludeFrom := addressSet("exclude-from")
excludeTo := addressSet("exclude-to")

target := abi.ChainEpoch(cctx.Int("start"))
totalEpochs := head.Height() - target

Expand Down Expand Up @@ -181,6 +220,30 @@ every day of chain processed.

msgs := map[addrNonce]map[cid.Cid]*types.Message{}

processMessage := func(c cid.Cid, m *types.Message) {
// Filter
if len(methods) > 0 && !methods[m.Method] {
return
}
if len(onlyFrom) > 0 && !onlyFrom[m.From] {
return
}
if len(onlyTo) > 0 && !onlyTo[m.To] {
return
}
if excludeFrom[m.From] || excludeTo[m.To] {
return
}

// Record
msgSet, ok := msgs[anonce(m)]
if !ok {
msgSet = make(map[cid.Cid]*types.Message, 1)
msgs[anonce(m)] = msgSet
}
msgSet[c] = m
}

encoder := json.NewEncoder(os.Stdout)

for _, bh := range ts.Blocks() {
Expand All @@ -191,27 +254,11 @@ every day of chain processed.
}

for i, m := range bms.BlsMessages {
if len(methods) > 0 && !methods[m.Method] {
continue
}
c, ok := msgs[anonce(m)]
if !ok {
c = make(map[cid.Cid]*types.Message, 1)
msgs[anonce(m)] = c
}
c[bms.Cids[i]] = m
processMessage(bms.Cids[i], m)
}

for i, m := range bms.SecpkMessages {
if len(methods) > 0 && !methods[m.Message.Method] {
continue
}
c, ok := msgs[anonce(&m.Message)]
if !ok {
c = make(map[cid.Cid]*types.Message, 1)
msgs[anonce(&m.Message)] = c
}
c[bms.Cids[len(bms.BlsMessages)+i]] = &m.Message
processMessage(bms.Cids[len(bms.BlsMessages)+i], &m.Message)
}
}
for _, ms := range msgs {
Expand Down

0 comments on commit 5dab9f0

Please sign in to comment.