github.com/MetalBlockchain/subnet-evm@v0.4.9/core/mkalloc.go (about)

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