/
main.go
126 lines (115 loc) · 3.08 KB
/
main.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package main
//package genesis
import (
"time"
"strings"
"math/rand"
"fmt"
"github.com/ethereum/go-ethereum/core"
"math/big"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/common"
"encoding/csv"
"os"
"io"
)
var (
balance, _ = big.NewInt(0).SetString("10000000000000000000000000000000000", 10)
)
func main() {
if len(os.Args) <= 1 {
fmt.Fprintln(os.Stderr, "Usage: gen_genesis dev|mainnet|simu")
os.Exit(1)
}
config := ¶ms.ChainConfig{
ChainID: big.NewInt(15),
HomesteadBlock: big.NewInt(0),
EIP150Block : big.NewInt(0),
EIP155Block: big.NewInt(0),
EIP158Block: big.NewInt(0),
DAOForkBlock : big.NewInt(0),
DAOForkSupport : false,
ByzantiumBlock : big.NewInt(0),
ConstantinopleBlock : big.NewInt(0),
}
gen := &core.Genesis{
Config: config,
Nonce: uint64(0xdeadbeefdeadbeef),
Timestamp: uint64(0x0),
ExtraData: nil,
GasLimit: uint64(0x1e8480000),
Difficulty: big.NewInt(0x40),
Mixhash: common.HexToHash("0x0"),
Alloc: *(devAllocs()),
ParentHash: common.HexToHash("0x0"),
}
switch env := os.Args[1]; env {
case "dev":
gen.Alloc = *(devAllocs())
case "simu":
gen.Alloc = *(simulateAllocs())
case "mainnet":
gen.Alloc = *(mainnetAllocs())
default:
fmt.Printf("Not supported environment: %s\n", env)
os.Exit(1)
}
//getAllocs()
if genJSON, err := gen.MarshalJSON(); err != nil {
panic(err)
} else {
fmt.Println(string(genJSON))
}
}
func simulateAllocs() *core.GenesisAlloc {
num := 100
allocs := make(core.GenesisAlloc, num)
hexes := []string{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"}
// fmt.Println(time.Now().Unix())
rand.Seed(time.Now().Unix()) // initialize global pseudo random generator
var addr []string
for i := 0; i < num; i++ {
addr = make([]string, 40)
for j := 0; j < 40; j++ {
addr = append(addr, hexes[rand.Intn(len(hexes))])
}
allocs[common.HexToAddress(strings.Join(addr, ""))] = core.GenesisAccount{Balance: big.NewInt(0x100000)}
}
return &allocs
}
func devAllocs() *core.GenesisAlloc {
allocs := make(core.GenesisAlloc, 2)
allocs[common.HexToAddress("0x7eff122b94897ea5b0e2a9abf47b86337fafebdc")] = core.GenesisAccount{Balance: balance}
allocs[common.HexToAddress("0x77beb894fc9b0ed41231e51f128a347043960a9d")] = core.GenesisAccount{Balance: balance}
return &allocs
}
func mainnetAllocs() *core.GenesisAlloc {
/* content of example.csv
0x7eff122b94897ea5b0e2a9abf47b86337fafebdc,1000000000000000000
0x77beb894fc9b0ed41231e51f128a347043960a9d,1000000000000000000
*/
file := "/tmp/erc20_cmt.csv"
f, err := os.Open(file)
if err != nil {
panic(err)
}
defer f.Close()
csvr := csv.NewReader(f)
allocs := make(core.GenesisAlloc, 10)
for {
row, err := csvr.Read()
if err != nil {
if err != io.EOF {
panic(err)
}
return &allocs
}
balance, success := big.NewInt(0).SetString(strings.Trim(row[1]," "), 10)
if !success {
panic("convert alloc balance error!")
}
//fmt.Printf("%s: %v\n", row[0], common.HexToAddress(row[0]))
allocs[common.HexToAddress(row[0])] = core.GenesisAccount{Balance:balance}
}
return &allocs
}