github.com/pranksteess/go-ethereum@v1.9.7/core/mkalloc.go (about)

     1  // Copyright 2017 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum 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-ethereum 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-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  // +build none
    18  
    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  */
    27  package main
    28  
    29  import (
    30  	"encoding/json"
    31  	"fmt"
    32  	"math/big"
    33  	"os"
    34  	"sort"
    35  	"strconv"
    36  
    37  	"github.com/ethereum/go-ethereum/core"
    38  	"github.com/ethereum/go-ethereum/rlp"
    39  )
    40  
    41  type allocItem struct{ Addr, Balance *big.Int }
    42  
    43  type allocList []allocItem
    44  
    45  func (a allocList) Len() int           { return len(a) }
    46  func (a allocList) Less(i, j int) bool { return a[i].Addr.Cmp(a[j].Addr) < 0 }
    47  func (a allocList) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
    48  
    49  func makelist(g *core.Genesis) allocList {
    50  	a := make(allocList, 0, len(g.Alloc))
    51  	for addr, account := range g.Alloc {
    52  		if len(account.Storage) > 0 || len(account.Code) > 0 || account.Nonce != 0 {
    53  			panic(fmt.Sprintf("can't encode account %x", addr))
    54  		}
    55  		bigAddr := new(big.Int).SetBytes(addr.Bytes())
    56  		a = append(a, allocItem{bigAddr, account.Balance})
    57  	}
    58  	sort.Sort(a)
    59  	return a
    60  }
    61  
    62  func makealloc(g *core.Genesis) string {
    63  	a := makelist(g)
    64  	data, err := rlp.EncodeToBytes(a)
    65  	if err != nil {
    66  		panic(err)
    67  	}
    68  	return strconv.QuoteToASCII(string(data))
    69  }
    70  
    71  func main() {
    72  	if len(os.Args) != 2 {
    73  		fmt.Fprintln(os.Stderr, "Usage: mkalloc genesis.json")
    74  		os.Exit(1)
    75  	}
    76  
    77  	g := new(core.Genesis)
    78  	file, err := os.Open(os.Args[1])
    79  	if err != nil {
    80  		panic(err)
    81  	}
    82  	if err := json.NewDecoder(file).Decode(g); err != nil {
    83  		panic(err)
    84  	}
    85  	fmt.Println("const allocData =", makealloc(g))
    86  }