github.com/aergoio/aergo@v1.3.1/cmd/brick/exec/forward.go (about)

     1  package exec
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  
     7  	"github.com/aergoio/aergo/cmd/brick/context"
     8  )
     9  
    10  func init() {
    11  	registerExec(&forward{})
    12  }
    13  
    14  type forward struct{}
    15  
    16  func (c *forward) Command() string {
    17  	return "forward"
    18  }
    19  
    20  func (c *forward) Syntax() string {
    21  	return context.AmountSymbol
    22  }
    23  
    24  func (c *forward) Usage() string {
    25  	return "forward [height_to_skip]"
    26  }
    27  
    28  func (c *forward) Describe() string {
    29  	return "fast forward blocks n times (default = 1)"
    30  }
    31  
    32  func (c *forward) Validate(args string) error {
    33  	// is chain is loaded?
    34  	if context.Get() == nil {
    35  		return fmt.Errorf("load chain first")
    36  	}
    37  
    38  	_, err := c.parse(args)
    39  
    40  	return err
    41  }
    42  
    43  func (c *forward) parse(args string) (int, error) {
    44  	splitArgs := context.SplitSpaceAndAccent(args, false)
    45  	if len(splitArgs) == 0 {
    46  		height, _ := strconv.Atoi("1")
    47  		return height, nil
    48  	} else if len(splitArgs) > 1 {
    49  		return 0, fmt.Errorf("need 1 or 0 arguments. usage: %s", c.Usage())
    50  	}
    51  
    52  	amount, err := strconv.Atoi(splitArgs[0].Text)
    53  	if err != nil {
    54  		return 0, fmt.Errorf("fail to parse number %s: %s", splitArgs[0].Text, err.Error())
    55  	}
    56  
    57  	return amount, nil
    58  }
    59  
    60  func (c *forward) Run(args string) (string, error) {
    61  	amount, _ := c.parse(args)
    62  
    63  	for i := 0; i < amount; i++ {
    64  		if err := context.Get().ConnectBlock(); err != nil {
    65  			return "", err
    66  		}
    67  	}
    68  
    69  	return "fast forward blocks successfully", nil
    70  }