github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/core/mkalloc.go (about) 1 2 //<developer> 3 // <name>linapex 曹一峰</name> 4 // <email>linapex@163.com</email> 5 // <wx>superexc</wx> 6 // <qqgroup>128148617</qqgroup> 7 // <url>https://jsq.ink</url> 8 // <role>pku engineer</role> 9 // <date>2019-03-16 19:16:35</date> 10 //</624450079205363712> 11 12 13 //+不建 14 15 /* 16 17 mkalloc工具在genesis-alloc.go中创建genesis分配常量。 18 它输出一个const声明,其中包含一个RLP编码的(地址、平衡)元组列表。 19 20 运行mkalloc.go genesis.json 21 22 **/ 23 24 package main 25 26 import ( 27 "encoding/json" 28 "fmt" 29 "math/big" 30 "os" 31 "sort" 32 "strconv" 33 34 "github.com/ethereum/go-ethereum/core" 35 "github.com/ethereum/go-ethereum/rlp" 36 ) 37 38 type allocItem struct{ Addr, Balance *big.Int } 39 40 type allocList []allocItem 41 42 func (a allocList) Len() int { return len(a) } 43 func (a allocList) Less(i, j int) bool { return a[i].Addr.Cmp(a[j].Addr) < 0 } 44 func (a allocList) Swap(i, j int) { a[i], a[j] = a[j], a[i] } 45 46 func makelist(g *core.Genesis) allocList { 47 a := make(allocList, 0, len(g.Alloc)) 48 for addr, account := range g.Alloc { 49 if len(account.Storage) > 0 || len(account.Code) > 0 || account.Nonce != 0 { 50 panic(fmt.Sprintf("can't encode account %x", addr)) 51 } 52 a = append(a, allocItem{addr.Big(), account.Balance}) 53 } 54 sort.Sort(a) 55 return a 56 } 57 58 func makealloc(g *core.Genesis) string { 59 a := makelist(g) 60 data, err := rlp.EncodeToBytes(a) 61 if err != nil { 62 panic(err) 63 } 64 return strconv.QuoteToASCII(string(data)) 65 } 66 67 func main() { 68 if len(os.Args) != 2 { 69 fmt.Fprintln(os.Stderr, "Usage: mkalloc genesis.json") 70 os.Exit(1) 71 } 72 73 g := new(core.Genesis) 74 file, err := os.Open(os.Args[1]) 75 if err != nil { 76 panic(err) 77 } 78 if err := json.NewDecoder(file).Decode(g); err != nil { 79 panic(err) 80 } 81 fmt.Println("const allocData =", makealloc(g)) 82 } 83