github.com/core-coin/go-core/v2@v2.1.9/core/mkalloc.go (about) 1 // Copyright 2017 by the Authors 2 // This file is part of the go-core library. 3 // 4 // The go-core library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-core library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-core library. If not, see <http://www.gnu.org/licenses/>. 16 17 //go:build none 18 // +build none 19 20 /* 21 The mkalloc tool creates the genesis allocation constants in genesis_alloc.go 22 It outputs a const declaration that contains an RLP-encoded list of (address, balance) tuples. 23 24 go run mkalloc.go genesis.json 25 */ 26 package main 27 28 import ( 29 "encoding/json" 30 "fmt" 31 "math/big" 32 "os" 33 "sort" 34 "strconv" 35 36 "github.com/core-coin/go-core/v2/core" 37 "github.com/core-coin/go-core/v2/rlp" 38 ) 39 40 type allocItem struct{ Addr, Balance *big.Int } 41 42 type allocList []allocItem 43 44 func (a allocList) Len() int { return len(a) } 45 func (a allocList) Less(i, j int) bool { return a[i].Addr.Cmp(a[j].Addr) < 0 } 46 func (a allocList) Swap(i, j int) { a[i], a[j] = a[j], a[i] } 47 48 func makelist(g *core.Genesis) allocList { 49 a := make(allocList, 0, len(g.Alloc)) 50 for addr, account := range g.Alloc { 51 if len(account.Storage) > 0 || len(account.Code) > 0 || account.Nonce != 0 { 52 panic(fmt.Sprintf("can't encode account %x", addr)) 53 } 54 bigAddr := new(big.Int).SetBytes(addr.Bytes()) 55 a = append(a, allocItem{bigAddr, account.Balance}) 56 } 57 sort.Sort(a) 58 return a 59 } 60 61 func makealloc(g *core.Genesis) string { 62 a := makelist(g) 63 data, err := rlp.EncodeToBytes(a) 64 if err != nil { 65 panic(err) 66 } 67 return strconv.QuoteToASCII(string(data)) 68 } 69 70 func main() { 71 if len(os.Args) != 2 { 72 fmt.Fprintln(os.Stderr, "Usage: mkalloc genesis.json") 73 os.Exit(1) 74 } 75 76 g := new(core.Genesis) 77 file, err := os.Open(os.Args[1]) 78 if err != nil { 79 panic(err) 80 } 81 if err := json.NewDecoder(file).Decode(g); err != nil { 82 panic(err) 83 } 84 fmt.Println("const allocData =", makealloc(g)) 85 }