2019-10-14 01:20:49 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
2022-06-14 15:00:51 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
|
2019-12-19 20:13:17 +00:00
|
|
|
"github.com/filecoin-project/go-address"
|
2021-04-05 19:34:03 +00:00
|
|
|
"github.com/filecoin-project/lotus/api/v0api"
|
2020-07-10 14:43:14 +00:00
|
|
|
"github.com/filecoin-project/lotus/build"
|
2019-10-18 04:47:41 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
|
|
|
lcli "github.com/filecoin-project/lotus/cli"
|
2019-10-14 01:20:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
app := &cli.App{
|
|
|
|
Name: "chain-noise",
|
|
|
|
Usage: "Generate some spam transactions in the network",
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "repo",
|
|
|
|
EnvVars: []string{"LOTUS_PATH"},
|
|
|
|
Hidden: true,
|
|
|
|
Value: "~/.lotus", // TODO: Consider XDG_DATA_HOME
|
|
|
|
},
|
2020-12-18 11:35:11 +00:00
|
|
|
&cli.IntFlag{
|
2021-01-06 03:30:51 +00:00
|
|
|
Name: "limit",
|
|
|
|
Usage: "spam transaction count limit, <= 0 is no limit",
|
|
|
|
Value: 0,
|
2020-12-18 11:35:11 +00:00
|
|
|
},
|
|
|
|
&cli.IntFlag{
|
2021-01-06 03:30:51 +00:00
|
|
|
Name: "rate",
|
|
|
|
Usage: "spam transaction rate, count per second",
|
|
|
|
Value: 5,
|
2020-12-18 11:35:11 +00:00
|
|
|
},
|
2019-10-14 01:20:49 +00:00
|
|
|
},
|
|
|
|
Commands: []*cli.Command{runCmd},
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := app.Run(os.Args); err != nil {
|
|
|
|
fmt.Println("Error: ", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var runCmd = &cli.Command{
|
|
|
|
Name: "run",
|
|
|
|
Action: func(cctx *cli.Context) error {
|
|
|
|
addr, err := address.NewFromString(cctx.Args().First())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
api, closer, err := lcli.GetFullNodeAPI(cctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer closer()
|
|
|
|
ctx := lcli.ReqContext(cctx)
|
|
|
|
|
2020-12-18 11:35:11 +00:00
|
|
|
rate := cctx.Int("rate")
|
|
|
|
if rate <= 0 {
|
|
|
|
rate = 5
|
|
|
|
}
|
|
|
|
limit := cctx.Int("limit")
|
|
|
|
|
|
|
|
return sendSmallFundsTxs(ctx, api, addr, rate, limit)
|
2019-10-14 01:20:49 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-04-05 19:34:03 +00:00
|
|
|
func sendSmallFundsTxs(ctx context.Context, api v0api.FullNode, from address.Address, rate, limit int) error {
|
2019-10-14 01:20:49 +00:00
|
|
|
var sendSet []address.Address
|
|
|
|
for i := 0; i < 20; i++ {
|
2020-10-11 18:12:01 +00:00
|
|
|
naddr, err := api.WalletNew(ctx, types.KTSecp256k1)
|
2019-10-14 01:20:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
sendSet = append(sendSet, naddr)
|
|
|
|
}
|
2020-12-18 11:35:11 +00:00
|
|
|
count := limit
|
2019-10-14 01:20:49 +00:00
|
|
|
|
2020-07-10 14:43:14 +00:00
|
|
|
tick := build.Clock.Ticker(time.Second / time.Duration(rate))
|
2019-10-14 01:20:49 +00:00
|
|
|
for {
|
2020-12-18 11:35:11 +00:00
|
|
|
if count <= 0 && limit > 0 {
|
|
|
|
fmt.Printf("%d messages sent.\n", limit)
|
|
|
|
return nil
|
|
|
|
}
|
2019-10-14 01:20:49 +00:00
|
|
|
select {
|
|
|
|
case <-tick.C:
|
|
|
|
msg := &types.Message{
|
2020-08-06 21:08:42 +00:00
|
|
|
From: from,
|
|
|
|
To: sendSet[rand.Intn(20)],
|
|
|
|
Value: types.NewInt(1),
|
2019-10-14 01:20:49 +00:00
|
|
|
}
|
|
|
|
|
2020-08-12 20:17:21 +00:00
|
|
|
smsg, err := api.MpoolPushMessage(ctx, msg, nil)
|
2019-10-14 01:20:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-18 11:35:11 +00:00
|
|
|
count--
|
2019-10-14 01:20:49 +00:00
|
|
|
fmt.Println("Message sent: ", smsg.Cid())
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|