github.com/klaytn/klaytn@v1.12.1/blockchain/mkalloc.go (about)

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