mkalloc.go 2.3 KB

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