mkalloc.go 2.3 KB

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