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