From 984e4e996374efd267114bbffa54cf3a8f55a5d5 Mon Sep 17 00:00:00 2001 From: Frank Date: Fri, 18 Dec 2020 19:35:11 +0800 Subject: [PATCH 1/2] add limit and rate for chain noise --- cmd/chain-noise/main.go | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/cmd/chain-noise/main.go b/cmd/chain-noise/main.go index 81586e1b2..e85a468a6 100644 --- a/cmd/chain-noise/main.go +++ b/cmd/chain-noise/main.go @@ -27,6 +27,16 @@ func main() { Hidden: true, 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}, } @@ -52,11 +62,17 @@ var runCmd = &cli.Command{ defer closer() 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 for i := 0; i < 20; i++ { 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) } + count := limit tick := build.Clock.Ticker(time.Second / time.Duration(rate)) for { + if count <= 0 && limit > 0 { + fmt.Printf("%d messages sent.\n", limit) + return nil + } select { case <-tick.C: msg := &types.Message{ @@ -81,6 +102,7 @@ func sendSmallFundsTxs(ctx context.Context, api api.FullNode, from address.Addre if err != nil { return err } + count-- fmt.Println("Message sent: ", smsg.Cid()) case <-ctx.Done(): return nil From 25f338493930777999fcbb030dbc3734cdb53706 Mon Sep 17 00:00:00 2001 From: Frank Date: Wed, 6 Jan 2021 11:30:51 +0800 Subject: [PATCH 2/2] go fmt --- cmd/chain-noise/main.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/cmd/chain-noise/main.go b/cmd/chain-noise/main.go index e85a468a6..37d623ce2 100644 --- a/cmd/chain-noise/main.go +++ b/cmd/chain-noise/main.go @@ -28,14 +28,14 @@ func main() { Value: "~/.lotus", // TODO: Consider XDG_DATA_HOME }, &cli.IntFlag{ - Name: "limit", - Usage: "spam transaction count limit, <= 0 is no limit", - Value: 0, + 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, + Name: "rate", + Usage: "spam transaction rate, count per second", + Value: 5, }, }, Commands: []*cli.Command{runCmd},