/
fake.go
106 lines (85 loc) · 2.5 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package main
import (
"crypto/ecdsa"
"encoding/json"
"fmt"
"math/big"
"os"
"strconv"
"strings"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/node"
cli "gopkg.in/urfave/cli.v1"
"github.com/Fantom-foundation/lachesis-ex/crypto"
"github.com/Fantom-foundation/lachesis-ex/integration"
"github.com/Fantom-foundation/lachesis-ex/lachesis/genesis"
"github.com/Fantom-foundation/lachesis-ex/utils"
)
// FakeNetFlag enables special testnet, where validators are automatically created
var FakeNetFlag = cli.StringFlag{
Name: "fakenet",
Usage: "'n/N[,non-validators]' - sets coinbase as fake n-th key from genesis of N validators. Non-validators is a count or json-file.",
}
func addFakeAccount(ctx *cli.Context, stack *node.Node) {
if !ctx.GlobalIsSet(FakeNetFlag.Name) {
return
}
key := getFakeValidator(ctx)
coinbase := integration.SetAccountKey(stack.AccountManager(), key, "fakepassword")
log.Info("Unlocked fake validator", "address", coinbase.Address.Hex())
}
func getFakeValidator(ctx *cli.Context) *ecdsa.PrivateKey {
num, _, err := parseFakeGen(ctx.GlobalString(FakeNetFlag.Name))
if err != nil {
log.Crit("Invalid flag", "flag", FakeNetFlag.Name, "err", err)
}
if num == 0 {
return nil
}
return crypto.FakeKey(num)
}
func parseFakeGen(s string) (num int, vaccs genesis.VAccounts, err error) {
var i64 uint64
parts := strings.SplitN(s, "/", 2)
if len(parts) != 2 {
err = fmt.Errorf("use %%d/%%d format")
return
}
i64, err = strconv.ParseUint(parts[0], 10, 32)
if err != nil {
return
}
num = int(i64)
parts = strings.SplitN(parts[1], ",", 2)
i64, err = strconv.ParseUint(parts[0], 10, 32)
validatorsNum := int(i64)
if validatorsNum < 0 || num > validatorsNum {
err = fmt.Errorf("key-num should be in range from 1 to validators (<key-num>/<validators>), or should be zero for non-validator node")
}
vaccs = genesis.FakeAccounts(1, validatorsNum, utils.ToFtm(1e10), utils.ToFtm(3175000))
if len(parts) < 2 {
return
}
var others genesis.Accounts
i64, err = strconv.ParseUint(parts[1], 10, 32)
if err != nil {
others, err = readAccounts(parts[1])
} else {
others, err = genesis.FakeAccounts(validatorsNum+1, int(i64), big.NewInt(1e18), big.NewInt(0)).Accounts, nil
}
vaccs.Accounts.Add(others)
if err != nil {
return
}
return
}
func readAccounts(filename string) (accs genesis.Accounts, err error) {
var f *os.File
f, err = os.Open(filename)
if err != nil {
return
}
accs = genesis.Accounts{}
err = json.NewDecoder(f).Decode(&accs)
return
}