Utility.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. This file is part of cpp-ethereum.
  3. cpp-ethereum is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. cpp-ethereum is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. /** @file Utility.cpp
  15. * @author Gav Wood <i@gavwood.com>
  16. * @date 2014
  17. */
  18. #include "Utility.h"
  19. #include <boost/regex.hpp>
  20. #include <boost/filesystem.hpp>
  21. #include <libdevcore/SHA3.h>
  22. #include <libdevcore/RLP.h>
  23. #include <libdevcore/Log.h>
  24. #include <libethcore/Common.h>
  25. #include "BlockChain.h"
  26. #include "Defaults.h"
  27. using namespace std;
  28. using namespace dev;
  29. using namespace dev::eth;
  30. namespace fs = boost::filesystem;
  31. bytes dev::eth::parseData(string const& _args)
  32. {
  33. bytes m_data;
  34. boost::smatch what;
  35. static const boost::regex r("(!|#|@|\\$)?\"([^\"]*)\"(\\s.*)?");
  36. static const boost::regex d("(@|\\$)?([0-9]+)(\\s*(ether)|(finney)|(szabo))?(\\s.*)?");
  37. static const boost::regex h("(@|\\$)?(0x)?(([a-fA-F0-9])+)(\\s.*)?");
  38. string s = _args;
  39. while (s.size())
  40. if (boost::regex_match(s, what, d))
  41. {
  42. u256 v((string)what[2]);
  43. if (what[6] == "szabo")
  44. v *= dev::eth::szabo;
  45. else if (what[5] == "finney")
  46. v *= dev::eth::finney;
  47. else if (what[4] == "ether")
  48. v *= dev::eth::ether;
  49. bytes bs = dev::toCompactBigEndian(v);
  50. if (what[1] != "$")
  51. for (auto i = bs.size(); i < 32; ++i)
  52. m_data.push_back(0);
  53. for (auto b: bs)
  54. m_data.push_back(b);
  55. s = what[7];
  56. }
  57. else if (boost::regex_match(s, what, h))
  58. {
  59. bytes bs = fromHex(((what[3].length() & 1) ? "0" : "") + what[3]);
  60. if (what[1] != "$")
  61. for (auto i = bs.size(); i < 32; ++i)
  62. m_data.push_back(0);
  63. for (auto b: bs)
  64. m_data.push_back(b);
  65. s = what[5];
  66. }
  67. else if (boost::regex_match(s, what, r))
  68. {
  69. bytes d = asBytes(what[2]);
  70. if (what[1] == "!")
  71. m_data += FixedHash<4>(sha3(d)).asBytes();
  72. else if (what[1] == "#")
  73. m_data += sha3(d).asBytes();
  74. else if (what[1] == "$")
  75. m_data += d + bytes{0};
  76. else
  77. m_data += d + bytes(32 - what[2].length() % 32, 0);
  78. s = what[3];
  79. }
  80. else
  81. s = s.substr(1);
  82. return m_data;
  83. }
  84. void dev::eth::upgradeDatabase(std::string const& _basePath, h256 const& _genesisHash)
  85. {
  86. std::string path = _basePath.empty() ? Defaults::get()->dbPath() : _basePath;
  87. if (fs::exists(path + "/state") && fs::exists(path + "/details") && fs::exists(path + "/blocks"))
  88. {
  89. // upgrade
  90. cnote << "Upgrading database to new layout...";
  91. bytes statusBytes = contents(path + "/status");
  92. RLP status(statusBytes);
  93. try
  94. {
  95. auto minorProtocolVersion = (unsigned)status[1];
  96. auto databaseVersion = (unsigned)status[2];
  97. auto genesisHash = status.itemCount() > 3 ? (h256)status[3] : _genesisHash;
  98. string chainPath = path + "/" + toHex(genesisHash.ref().cropped(0, 4));
  99. string extrasPath = chainPath + "/" + toString(databaseVersion);
  100. // write status
  101. if (!fs::exists(chainPath + "/blocks"))
  102. {
  103. fs::create_directories(chainPath);
  104. DEV_IGNORE_EXCEPTIONS(fs::permissions(chainPath, fs::owner_all));
  105. fs::rename(path + "/blocks", chainPath + "/blocks");
  106. if (!fs::exists(extrasPath + "/extras"))
  107. {
  108. fs::create_directories(extrasPath);
  109. DEV_IGNORE_EXCEPTIONS(fs::permissions(extrasPath, fs::owner_all));
  110. fs::rename(path + "/details", extrasPath + "/extras");
  111. fs::rename(path + "/state", extrasPath + "/state");
  112. writeFile(extrasPath + "/minor", rlp(minorProtocolVersion));
  113. }
  114. }
  115. }
  116. catch (Exception& ex)
  117. {
  118. cwarn << "Couldn't upgrade: " << ex.what() << boost::diagnostic_information(ex);
  119. }
  120. catch (...)
  121. {
  122. cwarn << "Couldn't upgrade. Some issue with moving files around. Probably easiest just to redownload.";
  123. }
  124. fs::rename(path + "/status", path + "/status.old");
  125. }
  126. }