Merge pull request #5223 from filcloud/noise-limit

add limit and rate for chain noise
This commit is contained in:
Łukasz Magiera 2021-01-06 20:17:29 +01:00 committed by GitHub
commit cbbe01eeb5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -27,6 +27,16 @@ func main() {
Hidden: true, Hidden: true,
Value: "~/.lotus", // TODO: Consider XDG_DATA_HOME Value: "~/.lotus", // TODO: Consider XDG_DATA_HOME
}, },
&cli.IntFlag{
Name: "limit",
Usage: "spam transaction count limit, <= 0 is no limit",
Value: 0,
},
&cli.IntFlag{
Name: "rate",
Usage: "spam transaction rate, count per second",
Value: 5,
},
}, },
Commands: []*cli.Command{runCmd}, Commands: []*cli.Command{runCmd},
} }
@ -52,11 +62,17 @@ var runCmd = &cli.Command{
defer closer() defer closer()
ctx := lcli.ReqContext(cctx) ctx := lcli.ReqContext(cctx)
return sendSmallFundsTxs(ctx, api, addr, 5) rate := cctx.Int("rate")
if rate <= 0 {
rate = 5
}
limit := cctx.Int("limit")
return sendSmallFundsTxs(ctx, api, addr, rate, limit)
}, },
} }
func sendSmallFundsTxs(ctx context.Context, api api.FullNode, from address.Address, rate int) error { func sendSmallFundsTxs(ctx context.Context, api api.FullNode, from address.Address, rate, limit int) error {
var sendSet []address.Address var sendSet []address.Address
for i := 0; i < 20; i++ { for i := 0; i < 20; i++ {
naddr, err := api.WalletNew(ctx, types.KTSecp256k1) naddr, err := api.WalletNew(ctx, types.KTSecp256k1)
@ -66,9 +82,14 @@ func sendSmallFundsTxs(ctx context.Context, api api.FullNode, from address.Addre
sendSet = append(sendSet, naddr) sendSet = append(sendSet, naddr)
} }
count := limit
tick := build.Clock.Ticker(time.Second / time.Duration(rate)) tick := build.Clock.Ticker(time.Second / time.Duration(rate))
for { for {
if count <= 0 && limit > 0 {
fmt.Printf("%d messages sent.\n", limit)
return nil
}
select { select {
case <-tick.C: case <-tick.C:
msg := &types.Message{ msg := &types.Message{
@ -81,6 +102,7 @@ func sendSmallFundsTxs(ctx context.Context, api api.FullNode, from address.Addre
if err != nil { if err != nil {
return err return err
} }
count--
fmt.Println("Message sent: ", smsg.Cid()) fmt.Println("Message sent: ", smsg.Cid())
case <-ctx.Done(): case <-ctx.Done():
return nil return nil