github.com/DxChainNetwork/dxc@v0.8.1-0.20220824085222-1162e304b6e7/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  	"strings"
    37  
    38  	"github.com/DxChainNetwork/dxc/consensus/dpos"
    39  	"github.com/DxChainNetwork/dxc/core"
    40  	"github.com/DxChainNetwork/dxc/rlp"
    41  )
    42  
    43  type allocItem struct {
    44  	Addr    *big.Int
    45  	Balance *big.Int
    46  	Code    []byte
    47  }
    48  
    49  type allocList []allocItem
    50  
    51  func (a allocList) Len() int           { return len(a) }
    52  func (a allocList) Less(i, j int) bool { return a[i].Addr.Cmp(a[j].Addr) < 0 }
    53  func (a allocList) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
    54  
    55  func makelist(g *core.Genesis) allocList {
    56  	a := make(allocList, 0, len(g.Alloc))
    57  	for addr, account := range g.Alloc {
    58  		if len(account.Storage) > 0 || account.Nonce != 0 {
    59  			panic(fmt.Sprintf("can't encode account %x", addr))
    60  		}
    61  		bigAddr := new(big.Int).SetBytes(addr.Bytes())
    62  		a = append(a, allocItem{bigAddr, account.Balance, account.Code})
    63  	}
    64  	sort.Sort(a)
    65  	return a
    66  }
    67  
    68  func makealloc(g *core.Genesis) string {
    69  	a := makelist(g)
    70  	data, err := rlp.EncodeToBytes(a)
    71  	if err != nil {
    72  		panic(err)
    73  	}
    74  	return strconv.QuoteToASCII(string(data))
    75  }
    76  
    77  func main() {
    78  	if len(os.Args) != 2 {
    79  		fmt.Fprintln(os.Stderr, "Usage: mkalloc genesis.json")
    80  		os.Exit(1)
    81  	}
    82  
    83  	file, err := os.Open(os.Args[1])
    84  	if err != nil {
    85  		panic(err)
    86  	}
    87  	defer file.Close()
    88  
    89  	g2 := new(core.Genesis)
    90  	if err := json.NewDecoder(file).Decode(g2); err != nil {
    91  		panic(err)
    92  	}
    93  
    94  	ga := new(core.GenesisAlloc)
    95  	err = json.NewDecoder(strings.NewReader(dpos.GenesisAlloc)).Decode(ga)
    96  	if err != nil {
    97  		panic(err)
    98  	}
    99  
   100  	for i, v := range g2.Alloc {
   101  		(*ga)[i] = v
   102  	}
   103  	g2.Alloc = *ga
   104  
   105  	allocData := makealloc(g2)
   106  	outputFile, err := os.OpenFile("./build/bin/genesisAllocRlpData", os.O_CREATE|os.O_RDWR|os.O_TRUNC, os.ModePerm)
   107  	if err != nil {
   108  		panic(err)
   109  	}
   110  	outputFile.Write([]byte(allocData))
   111  }