github.com/arieschain/arieschain@v0.0.0-20191023063405-37c074544356/core/mkalloc.go (about)

     1  // +build none
     2  
     3  /*
     4  
     5     The mkalloc tool creates the genesis allocation constants in genesis_alloc.go
     6     It outputs a const declaration that contains an RLP-encoded list of (address, balance) tuples.
     7  
     8         go run mkalloc.go genesis.json
     9  
    10  */
    11  package main
    12  
    13  import (
    14  	"encoding/json"
    15  	"fmt"
    16  	"math/big"
    17  	"os"
    18  	"sort"
    19  	"strconv"
    20  
    21  	"github.com/quickchainproject/quickchain/core"
    22  	"github.com/quickchainproject/quickchain/rlp"
    23  )
    24  
    25  type allocItem struct{ Addr, Balance *big.Int }
    26  
    27  type allocList []allocItem
    28  
    29  func (a allocList) Len() int           { return len(a) }
    30  func (a allocList) Less(i, j int) bool { return a[i].Addr.Cmp(a[j].Addr) < 0 }
    31  func (a allocList) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
    32  
    33  func makelist(g *core.Genesis) allocList {
    34  	a := make(allocList, 0, len(g.Alloc))
    35  	for addr, account := range g.Alloc {
    36  		if len(account.Storage) > 0 || len(account.Code) > 0 || account.Nonce != 0 {
    37  			panic(fmt.Sprintf("can't encode account %x", addr))
    38  		}
    39  		a = append(a, allocItem{addr.Big(), account.Balance})
    40  	}
    41  	sort.Sort(a)
    42  	return a
    43  }
    44  
    45  func makealloc(g *core.Genesis) string {
    46  	a := makelist(g)
    47  	data, err := rlp.EncodeToBytes(a)
    48  	if err != nil {
    49  		panic(err)
    50  	}
    51  	return strconv.QuoteToASCII(string(data))
    52  }
    53  
    54  func main() {
    55  	if len(os.Args) != 2 {
    56  		fmt.Fprintln(os.Stderr, "Usage: mkalloc genesis.json")
    57  		os.Exit(1)
    58  	}
    59  
    60  	g := new(core.Genesis)
    61  	file, err := os.Open(os.Args[1])
    62  	if err != nil {
    63  		panic(err)
    64  	}
    65  	if err := json.NewDecoder(file).Decode(g); err != nil {
    66  		panic(err)
    67  	}
    68  	fmt.Println("const allocData =", makealloc(g))
    69  }