internal

package
v1.17.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 14, 2024 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var BatchReplaceCmd = &cli.Command{
	Name:  "batch-replace",
	Usage: "batch replace messages",
	Flags: []cli.Flag{
		&cli.StringFlag{
			Name:  "gas-feecap",
			Usage: "gas feecap for new message (burn and pay to miner, attoFIL/GasUnit)",
		},
		&cli.StringFlag{
			Name:  "gas-premium",
			Usage: "gas premium for new message (pay to miner, attoFIL/GasUnit)",
		},
		cli2.GasOverPremiumFlag,
		&cli.Int64Flag{
			Name:  "gas-limit",
			Usage: "gas limit for new message (GasUnit)",
		},
		&cli.BoolFlag{
			Name:  "auto",
			Usage: "automatically reprice the specified message",
		},
		&cli.StringFlag{
			Name:  "max-fee",
			Usage: "Spend up to X attoFIL for this message (applicable for auto mode)",
		},
	},
	Action: func(cctx *cli.Context) error {
		ctx := cctx.Context
		cfg := new(config.Config)
		err := utils.ReadConfig(cctx.String("config"), cfg)
		if err != nil {
			return fmt.Errorf("read config failed: %v", err)
		}

		messagerAPI, closer, err := mapi.DialIMessagerRPC(ctx, cfg.Messager.URL, cfg.Messager.Token, nil)
		if err != nil {
			return err
		}
		defer closer()

		nodeAPI, closer2, err := cli2.NewNodeAPI(ctx, cfg.Venus.URL, cfg.Venus.Token)
		if err != nil {
			return err
		}
		defer closer2()

		blockTime, err := time.ParseDuration(cfg.BatchReplace.BlockTime)
		if err != nil {
			return err
		}

		if cctx.IsSet("gas-premium") && cctx.IsSet(cli2.GasOverPremiumFlag.Name) {
			return fmt.Errorf("gas-premium and gas-over-premium flag only need one")
		}
		if len(cfg.BatchReplace.Selectors) == 0 {
			return fmt.Errorf("selects are empty, please check config")
		}

		params, err := cli2.ParseFlagToReplaceMessaeParams(cctx)
		if err != nil {
			return err
		}

		addrs := make(map[address.Address]struct{})
		if !cfg.BatchReplace.From.Empty() {
			addrs[cfg.BatchReplace.From.Address()] = struct{}{}
		} else {
			addrList, err := messagerAPI.ListAddress(ctx)
			if err != nil {
				return err
			}
			for _, addrInfo := range addrList {
				addrs[addrInfo.Addr] = struct{}{}
			}
		}

		pendingMsgs := make(map[address.Address][]*messager.Message, len(addrs))
		for addr := range addrs {
			actor, err := nodeAPI.StateGetActor(ctx, addr, types.EmptyTSK)
			if err != nil {
				return fmt.Errorf("get actor %s failed: %v", addr, err)
			}

			blockedMsgs, err := messagerAPI.ListBlockedMessage(ctx, addr, blockTime)
			if err != nil {
				return err
			}

			sort.Slice(blockedMsgs, func(i, j int) bool {
				return blockedMsgs[i].Nonce < blockedMsgs[j].Nonce
			})

			tmsgs, err := selectMsg(cfg.BatchReplace.Selectors, addr, actor, blockedMsgs)
			if err != nil {
				fmt.Printf("address %s selset message failed: %v", addr, err)
				continue
			}

			pendingMsgs[addr] = tmsgs
			fmt.Printf("address %s has %d message need replace\n", addr, len(tmsgs))
		}

		if len(pendingMsgs) == 0 {
			return nil
		}

		for addr, msgs := range pendingMsgs {
			fmt.Printf("\nstart replace message for %s \n", addr)
			for _, msg := range msgs {
				params.ID = msg.ID
				oldCid := msg.Cid()
				newCid, err := messagerAPI.ReplaceMessage(cctx.Context, params)
				if err != nil {
					fmt.Printf("replace msg %s %d failed %v\n", msg.ID, msg.Nonce, err)
					continue
				}
				fmt.Printf("replace message %s success, old cid: %s, new cid: %s\n", msg.ID, oldCid, newCid)
			}
			fmt.Printf("end replace message for %s\n", addr)
		}

		return nil
	},
}

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL