github.com/unicornultrafoundation/go-u2u@v1.0.0-rc1.0.20240205080301-e74a83d3fadc/cmd/u2u/launcher/fake.go (about)

     1  package launcher
     2  
     3  import (
     4  	"crypto/ecdsa"
     5  	"fmt"
     6  	"strconv"
     7  	"strings"
     8  
     9  	"github.com/unicornultrafoundation/go-helios/native/idx"
    10  	cli "gopkg.in/urfave/cli.v1"
    11  
    12  	"github.com/unicornultrafoundation/go-u2u/integration/makefakegenesis"
    13  )
    14  
    15  // FakeNetFlag enables special testnet, where validators are automatically created
    16  var FakeNetFlag = cli.StringFlag{
    17  	Name:  "fakenet",
    18  	Usage: "'n/N' - sets coinbase as fake n-th key from genesis of N validators.",
    19  }
    20  
    21  func getFakeValidatorKey(ctx *cli.Context) *ecdsa.PrivateKey {
    22  	id, _, err := parseFakeGen(ctx.GlobalString(FakeNetFlag.Name))
    23  	if err != nil || id == 0 {
    24  		return nil
    25  	}
    26  	return makefakegenesis.FakeKey(id)
    27  }
    28  
    29  func parseFakeGen(s string) (id idx.ValidatorID, num idx.Validator, err error) {
    30  	parts := strings.SplitN(s, "/", 2)
    31  	if len(parts) != 2 {
    32  		err = fmt.Errorf("use %%d/%%d format")
    33  		return
    34  	}
    35  
    36  	var u32 uint64
    37  	u32, err = strconv.ParseUint(parts[0], 10, 32)
    38  	if err != nil {
    39  		return
    40  	}
    41  	id = idx.ValidatorID(u32)
    42  
    43  	u32, err = strconv.ParseUint(parts[1], 10, 32)
    44  	num = idx.Validator(u32)
    45  	if num < 0 || idx.Validator(id) > num {
    46  		err = fmt.Errorf("key-num should be in range from 1 to validators (<key-num>/<validators>), or should be zero for non-validator node")
    47  		return
    48  	}
    49  
    50  	return
    51  }