//------------------------------------------------------------------------------ /* This file is part of rippled: https://github.com/ripple/rippled Copyright (c) 2012, 2013 Ripple Labs Inc. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ //============================================================================== #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace ripple { std::shared_ptr SHAMapTreeNode::makeTransaction( Slice data, SHAMapHash const& hash, bool hashValid) { auto item = std::make_shared( sha512Half(HashPrefix::transactionID, data), data); if (hashValid) return std::make_shared(std::move(item), 0, hash); return std::make_shared(std::move(item), 0); } std::shared_ptr SHAMapTreeNode::makeTransactionWithMeta( Slice data, SHAMapHash const& hash, bool hashValid) { Serializer s(data.data(), data.size()); uint256 tag; if (s.size() < tag.bytes) Throw("Short TXN+MD node"); // FIXME: improve this interface so that the above check isn't needed if (!s.getBitString(tag, s.size() - tag.bytes)) Throw( "Short TXN+MD node (" + std::to_string(s.size()) + ")"); s.chop(tag.bytes); auto item = std::make_shared(tag, s.slice()); if (hashValid) return std::make_shared( std::move(item), 0, hash); return std::make_shared(std::move(item), 0); } std::shared_ptr SHAMapTreeNode::makeAccountState( Slice data, SHAMapHash const& hash, bool hashValid) { Serializer s(data.data(), data.size()); uint256 tag; if (s.size() < tag.bytes) Throw("short AS node"); // FIXME: improve this interface so that the above check isn't needed if (!s.getBitString(tag, s.size() - tag.bytes)) Throw( "Short AS node (" + std::to_string(s.size()) + ")"); s.chop(tag.bytes); if (tag.isZero()) Throw("Invalid AS node"); auto item = std::make_shared(tag, s.slice()); if (hashValid) return std::make_shared( std::move(item), 0, hash); return std::make_shared(std::move(item), 0); } std::shared_ptr SHAMapTreeNode::makeFromWire(Slice rawNode) { if (rawNode.empty()) return {}; auto const type = rawNode[rawNode.size() - 1]; rawNode.remove_suffix(1); bool const hashValid = false; SHAMapHash const hash; if (type == wireTypeTransaction) return makeTransaction(rawNode, hash, hashValid); if (type == wireTypeAccountState) return makeAccountState(rawNode, hash, hashValid); if (type == wireTypeInner) return SHAMapInnerNode::makeFullInner(rawNode, hash, hashValid); if (type == wireTypeCompressedInner) return SHAMapInnerNode::makeCompressedInner(rawNode); if (type == wireTypeTransactionWithMeta) return makeTransactionWithMeta(rawNode, hash, hashValid); Throw( "wire: Unknown type (" + std::to_string(type) + ")"); } std::shared_ptr SHAMapTreeNode::makeFromPrefix(Slice rawNode, SHAMapHash const& hash) { if (rawNode.size() < 4) Throw("prefix: short node"); // FIXME: Use SerialIter::get32? // Extract the prefix auto const type = safe_cast( (safe_cast(rawNode[0]) << 24) + (safe_cast(rawNode[1]) << 16) + (safe_cast(rawNode[2]) << 8) + (safe_cast(rawNode[3]))); rawNode.remove_prefix(4); bool const hashValid = true; if (type == HashPrefix::transactionID) return makeTransaction(rawNode, hash, hashValid); if (type == HashPrefix::leafNode) return makeAccountState(rawNode, hash, hashValid); if (type == HashPrefix::innerNode) return SHAMapInnerNode::makeFullInner(rawNode, hash, hashValid); if (type == HashPrefix::txNode) return makeTransactionWithMeta(rawNode, hash, hashValid); Throw( "prefix: unknown type (" + std::to_string(safe_cast>(type)) + ")"); } std::string SHAMapTreeNode::getString(const SHAMapNodeID& id) const { return to_string(id); } } // namespace ripple