Swarm.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 Swarm.cpp
  15. * @author Gav Wood <i@gavwood.com>
  16. * @date 2015
  17. */
  18. #include "Swarm.h"
  19. #include <libdevcore/Log.h>
  20. #include <libdevcore/SHA3.h>
  21. #include <libdevcore/Hash.h>
  22. #include <libethcore/Common.h>
  23. #include <libethereum/Client.h>
  24. #include "WebThree.h"
  25. #include "Support.h"
  26. #include "IPFS.h"
  27. using namespace std;
  28. using namespace dev;
  29. using namespace bzz;
  30. using namespace eth;
  31. struct SwarmChannel: public LogChannel { static const char* name(); static const int verbosity = 3; };
  32. const char* SwarmChannel::name() { return EthYellow "bzz"; }
  33. #define cbzz clog(SwarmChannel)
  34. bzz::Interface::~Interface()
  35. {
  36. }
  37. bzz::Client::Client(WebThreeDirect* _web3):
  38. m_ipfs(new IPFS)
  39. {
  40. m_owner = _web3;
  41. }
  42. Pinneds bzz::Client::insertBundle(bytesConstRef _bundle)
  43. {
  44. cbzz << "Bundle insert" << sha3(_bundle);
  45. Pinneds ret;
  46. RLP rlp(_bundle);
  47. bool first = true;
  48. for (auto const& r: rlp)
  49. if (first)
  50. first = false;
  51. else
  52. {
  53. cbzz << " inserting slice" << sha3(r.toBytesConstRef());
  54. ret.push_back(Pinned(m_cache[sha3(r.toBytesConstRef())] = make_shared<bytes>(r.toBytes())));
  55. }
  56. return ret;
  57. }
  58. Pinned bzz::Client::put(bytes const& _data)
  59. {
  60. h256 ret = sha3(_data);
  61. cbzz << "Inserting" << ret;
  62. if (!m_cache.count(ret))
  63. m_cache[ret] = make_shared<bytes>(_data);
  64. if (m_putAccount)
  65. {
  66. // send to IPFS...
  67. h256 sha256hash = sha256(&_data);
  68. cbzz << "IPFS-inserting" << sha256hash;
  69. // set in blockchain
  70. try
  71. {
  72. m_owner->support()->hintSHA256(ret, sha256hash, m_putAccount);
  73. m_ipfs->putBlockForSHA256(&_data);
  74. }
  75. catch (InterfaceNotSupported&) {}
  76. }
  77. return Pinned(m_cache[ret]);
  78. }
  79. Pinned bzz::Client::get(h256 const& _hash)
  80. {
  81. cbzz << "Looking up" << _hash;
  82. auto it = m_cache.find(_hash);
  83. if (it != m_cache.end())
  84. return it->second;
  85. if (u256 sha256hash = m_owner->support()->sha256Hint(_hash))
  86. {
  87. cbzz << "IPFS Searching" << sha256hash;
  88. auto b = m_ipfs->getBlockForSHA256(sha256hash);
  89. if (!b.empty())
  90. return (m_cache[_hash] = make_shared<bytes>(b));
  91. }
  92. cbzz << "Not found" << _hash;
  93. throw ResourceNotAvailable();
  94. }