github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/core/mkalloc.go (about)

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