github.com/theQRL/go-zond@v0.1.1/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  //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  	"strconv"
    34  
    35  	"github.com/theQRL/go-zond/common"
    36  	"github.com/theQRL/go-zond/core"
    37  	"github.com/theQRL/go-zond/rlp"
    38  	"golang.org/x/exp/slices"
    39  )
    40  
    41  type allocItem struct {
    42  	Addr    *big.Int
    43  	Balance *big.Int
    44  	Misc    *allocItemMisc `rlp:"optional"`
    45  }
    46  
    47  type allocItemMisc struct {
    48  	Nonce uint64
    49  	Code  []byte
    50  	Slots []allocItemStorageItem
    51  }
    52  
    53  type allocItemStorageItem struct {
    54  	Key common.Hash
    55  	Val common.Hash
    56  }
    57  
    58  func makelist(g *core.Genesis) []allocItem {
    59  	items := make([]allocItem, 0, len(g.Alloc))
    60  	for addr, account := range g.Alloc {
    61  		var misc *allocItemMisc
    62  		if len(account.Storage) > 0 || len(account.Code) > 0 || account.Nonce != 0 {
    63  			misc = &allocItemMisc{
    64  				Nonce: account.Nonce,
    65  				Code:  account.Code,
    66  				Slots: make([]allocItemStorageItem, 0, len(account.Storage)),
    67  			}
    68  			for key, val := range account.Storage {
    69  				misc.Slots = append(misc.Slots, allocItemStorageItem{key, val})
    70  			}
    71  			slices.SortFunc(misc.Slots, func(a, b allocItemStorageItem) int {
    72  				return a.Key.Cmp(b.Key)
    73  			})
    74  		}
    75  		bigAddr := new(big.Int).SetBytes(addr.Bytes())
    76  		items = append(items, allocItem{bigAddr, account.Balance, misc})
    77  	}
    78  	slices.SortFunc(items, func(a, b allocItem) int {
    79  		return a.Addr.Cmp(b.Addr)
    80  	})
    81  	return items
    82  }
    83  
    84  func makealloc(g *core.Genesis) string {
    85  	a := makelist(g)
    86  	data, err := rlp.EncodeToBytes(a)
    87  	if err != nil {
    88  		panic(err)
    89  	}
    90  	return strconv.QuoteToASCII(string(data))
    91  }
    92  
    93  func main() {
    94  	if len(os.Args) != 2 {
    95  		fmt.Fprintln(os.Stderr, "Usage: mkalloc genesis.json")
    96  		os.Exit(1)
    97  	}
    98  
    99  	g := new(core.Genesis)
   100  	file, err := os.Open(os.Args[1])
   101  	if err != nil {
   102  		panic(err)
   103  	}
   104  	if err := json.NewDecoder(file).Decode(g); err != nil {
   105  		panic(err)
   106  	}
   107  	fmt.Println("const allocData =", makealloc(g))
   108  }