config_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // Copyright 2016 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. package api
  17. import (
  18. "io/ioutil"
  19. "os"
  20. "path/filepath"
  21. "strings"
  22. "testing"
  23. "github.com/ethereum/go-ethereum/common"
  24. "github.com/ethereum/go-ethereum/crypto"
  25. )
  26. var (
  27. hexprvkey = "65138b2aa745041b372153550584587da326ab440576b2a1191dd95cee30039c"
  28. defaultConfig = `{
  29. "ChunkDbPath": "` + filepath.Join("TMPDIR", "chunks") + `",
  30. "DbCapacity": 5000000,
  31. "CacheCapacity": 5000,
  32. "Radius": 0,
  33. "Branches": 128,
  34. "Hash": "SHA3",
  35. "CallInterval": 3000000000,
  36. "KadDbPath": "` + filepath.Join("TMPDIR", "bzz-peers.json") + `",
  37. "MaxProx": 8,
  38. "ProxBinSize": 2,
  39. "BucketSize": 4,
  40. "PurgeInterval": 151200000000000,
  41. "InitialRetryInterval": 42000000,
  42. "MaxIdleInterval": 42000000000,
  43. "ConnRetryExp": 2,
  44. "Swap": {
  45. "BuyAt": 20000000000,
  46. "SellAt": 20000000000,
  47. "PayAt": 100,
  48. "DropAt": 10000,
  49. "AutoCashInterval": 300000000000,
  50. "AutoCashThreshold": 50000000000000,
  51. "AutoDepositInterval": 300000000000,
  52. "AutoDepositThreshold": 50000000000000,
  53. "AutoDepositBuffer": 100000000000000,
  54. "PublicKey": "0x045f5cfd26692e48d0017d380349bcf50982488bc11b5145f3ddf88b24924299048450542d43527fbe29a5cb32f38d62755393ac002e6bfdd71b8d7ba725ecd7a3",
  55. "Contract": "0x0000000000000000000000000000000000000000",
  56. "Beneficiary": "0x0d2f62485607cf38d9d795d93682a517661e513e"
  57. },
  58. "RequestDbPath": "` + filepath.Join("TMPDIR", "requests") + `",
  59. "RequestDbBatchSize": 512,
  60. "KeyBufferSize": 1024,
  61. "SyncBatchSize": 128,
  62. "SyncBufferSize": 128,
  63. "SyncCacheSize": 1024,
  64. "SyncPriorities": [
  65. 2,
  66. 1,
  67. 1,
  68. 0,
  69. 0
  70. ],
  71. "SyncModes": [
  72. true,
  73. true,
  74. true,
  75. true,
  76. false
  77. ],
  78. "Path": "TMPDIR",
  79. "Port": "8500",
  80. "PublicKey": "0x045f5cfd26692e48d0017d380349bcf50982488bc11b5145f3ddf88b24924299048450542d43527fbe29a5cb32f38d62755393ac002e6bfdd71b8d7ba725ecd7a3",
  81. "BzzKey": "0xe861964402c0b78e2d44098329b8545726f215afa737d803714a4338552fcb81",
  82. "EnsRoot": "0x112234455c3a32fd11230c42e7bccd4a84e02010",
  83. "NetworkId": 323
  84. }`
  85. )
  86. func TestConfigWriteRead(t *testing.T) {
  87. tmp, err := ioutil.TempDir(os.TempDir(), "bzz-test")
  88. if err != nil {
  89. t.Fatal(err)
  90. }
  91. defer os.RemoveAll(tmp)
  92. prvkey := crypto.ToECDSA(common.Hex2Bytes(hexprvkey))
  93. orig, err := NewConfig(tmp, common.Address{}, prvkey, 323)
  94. if err != nil {
  95. t.Fatalf("expected no error, got %v", err)
  96. }
  97. data, err := ioutil.ReadFile(filepath.Join(orig.Path, "config.json"))
  98. if err != nil {
  99. t.Fatalf("default config file cannot be read: %v", err)
  100. }
  101. exp := strings.Replace(defaultConfig, "TMPDIR", orig.Path, -1)
  102. exp = strings.Replace(exp, "\\", "\\\\", -1)
  103. if string(data) != exp {
  104. t.Fatalf("default config mismatch:\nexpected: %v\ngot: %v", exp, string(data))
  105. }
  106. conf, err := NewConfig(tmp, common.Address{}, prvkey, 323)
  107. if err != nil {
  108. t.Fatalf("expected no error, got %v", err)
  109. }
  110. if conf.Swap.Beneficiary.Hex() != orig.Swap.Beneficiary.Hex() {
  111. t.Fatalf("expected beneficiary from loaded config %v to match original %v", conf.Swap.Beneficiary.Hex(), orig.Swap.Beneficiary.Hex())
  112. }
  113. }