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. a = append(a, allocItem{addr.Big(), account.Balance})
  45. }
  46. sort.Sort(a)
  47. return a
  48. }
  49. func makealloc(g *core.Genesis) string {
  50. a := makelist(g)
  51. data, err := rlp.EncodeToBytes(a)
  52. if err != nil {
  53. panic(err)
  54. }
  55. return strconv.QuoteToASCII(string(data))
  56. }
  57. func main() {
  58. if len(os.Args) != 2 {
  59. fmt.Fprintln(os.Stderr, "Usage: mkalloc genesis.json")
  60. os.Exit(1)
  61. }
  62. g := new(core.Genesis)
  63. file, err := os.Open(os.Args[1])
  64. if err != nil {
  65. panic(err)
  66. }
  67. if err := json.NewDecoder(file).Decode(g); err != nil {
  68. panic(err)
  69. }
  70. fmt.Println("const allocData =", makealloc(g))
  71. }