github.com/cryptotooltop/go-ethereum@v0.0.0-20231103184714-151d1922f3e5/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  
    22     The mkalloc tool creates the genesis allocation constants in genesis_alloc.go
    23     It outputs a const declaration that contains an RLP-encoded list of (address, balance) tuples.
    24  
    25         go run mkalloc.go genesis.json
    26  
    27  */
    28  package main
    29  
    30  import (
    31  	"encoding/json"
    32  	"fmt"
    33  	"math/big"
    34  	"os"
    35  	"sort"
    36  	"strconv"
    37  
    38  	"github.com/scroll-tech/go-ethereum/core"
    39  	"github.com/scroll-tech/go-ethereum/rlp"
    40  )
    41  
    42  type storageItem struct{ Key, Value *big.Int }
    43  type storageList []storageItem
    44  
    45  type allocItem struct {
    46  	Addr, Balance *big.Int
    47  	Code          []byte
    48  	Storage       storageList
    49  }
    50  
    51  type allocList []allocItem
    52  
    53  func (a allocList) Len() int           { return len(a) }
    54  func (a allocList) Less(i, j int) bool { return a[i].Addr.Cmp(a[j].Addr) < 0 }
    55  func (a allocList) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
    56  
    57  func makelist(g *core.Genesis) allocList {
    58  	a := make(allocList, 0, len(g.Alloc))
    59  	for addr, account := range g.Alloc {
    60  		if account.Nonce != 0 {
    61  			panic(fmt.Sprintf("can't encode account %x", addr))
    62  		}
    63  		bigAddr := new(big.Int).SetBytes(addr.Bytes())
    64  
    65  		s := make(storageList, 0, len(account.Storage))
    66  		for key, value := range account.Storage {
    67  			s = append(s, storageItem{Key: new(big.Int).SetBytes(key.Bytes()), Value: new(big.Int).SetBytes(value.Bytes())})
    68  		}
    69  
    70  		a = append(a, allocItem{bigAddr, account.Balance, account.Code, s})
    71  	}
    72  	sort.Sort(a)
    73  	return a
    74  }
    75  
    76  func makealloc(g *core.Genesis) string {
    77  	a := makelist(g)
    78  	data, err := rlp.EncodeToBytes(a)
    79  	if err != nil {
    80  		panic(err)
    81  	}
    82  	return strconv.QuoteToASCII(string(data))
    83  }
    84  
    85  func main() {
    86  	if len(os.Args) != 2 {
    87  		fmt.Fprintln(os.Stderr, "Usage: mkalloc genesis.json")
    88  		os.Exit(1)
    89  	}
    90  
    91  	g := new(core.Genesis)
    92  	file, err := os.Open(os.Args[1])
    93  	if err != nil {
    94  		panic(err)
    95  	}
    96  	if err := json.NewDecoder(file).Decode(g); err != nil {
    97  		panic(err)
    98  	}
    99  	fmt.Println("const allocData =", makealloc(g))
   100  }