diff --git a/src/Ledger.cpp b/src/Ledger.cpp index 666ec47fe..68cf4c799 100644 --- a/src/Ledger.cpp +++ b/src/Ledger.cpp @@ -7,6 +7,7 @@ #include "Application.h" #include "Ledger.h" +#include "utils.h" #include "../obj/src/newcoin.pb.h" #include "PackedMessage.h" #include "Config.h" @@ -50,9 +51,7 @@ Ledger::Ledger(Ledger::pointer prevLedger) : mParentHash(prevLedger->getHash()), prevLedger->setClosed(); prevLedger->updateHash(); mAccountStateMap->setSeq(mLedgerSeq); - if (prevLedger->mTimeStamp == 0) - mTimeStamp = (theApp->getOPs().getNetworkTime() % mLedgerInterval) + mLedgerInterval; - else mTimeStamp = prevLedger->mTimeStamp + prevLedger->mLedgerInterval; + mTimeStamp = prevLedger->getNextLedgerClose(); } Ledger::Ledger(const std::vector& rawLedger) : mTotCoins(0), mTimeStamp(0), @@ -426,4 +425,22 @@ bool Ledger::isAcquiringAS(void) { return mAccountStateMap->isSynching(); } + +boost::posix_time::ptime Ledger::getCloseTime() const +{ + return ptFromSeconds(mTimeStamp); +} + +void Ledger::setCloseTime(boost::posix_time::ptime ptm) +{ + mTimeStamp = iToSeconds(ptm); +} + +uint64 Ledger::getNextLedgerClose() const +{ + if (mTimeStamp == 0) + return theApp->getOPs().getNetworkTimeNC() + 2 * mLedgerInterval - 1; + return mTimeStamp + mLedgerInterval; +} + // vim:ts=4 diff --git a/src/Ledger.h b/src/Ledger.h index 5e8f70f47..cd561f529 100644 --- a/src/Ledger.h +++ b/src/Ledger.h @@ -6,6 +6,7 @@ #include #include +#include #include "../json/value.h" @@ -56,7 +57,8 @@ public: private: uint256 mHash, mParentHash, mTransHash, mAccountHash; - uint64 mTotCoins, mTimeStamp; + uint64 mTotCoins; + uint64 mTimeStamp; // when this ledger closes uint32 mLedgerSeq; uint16 mLedgerInterval; bool mClosed, mValidHash, mAccepted, mImmutable; @@ -81,7 +83,7 @@ protected: public: Ledger(const NewcoinAddress& masterID, uint64 startAmount); // used for the starting bootstrap ledger Ledger(const uint256 &parentHash, const uint256 &transHash, const uint256 &accountHash, - uint64 totCoins, uint64 timeStamp, uint32 ledgerSeq); // used for received ledgers + uint64 totCoins, uint64 timeStamp, uint32 ledgerSeq); // used for database ledgers Ledger(const std::vector& rawLedger); Ledger(const std::string& rawLedger); Ledger(Ledger::pointer previous); // ledger after this one @@ -100,10 +102,15 @@ public: const uint256& getTransHash() const { return mTransHash; } const uint256& getAccountHash() const { return mAccountHash; } uint64 getTotalCoins() const { return mTotCoins; } - uint64 getTimeStamp() const { return mTimeStamp; } + uint64 getRawTimeStamp() const { return mTimeStamp; } uint32 getLedgerSeq() const { return mLedgerSeq; } uint16 getInterval() const { return mLedgerInterval; } + // close time functions + boost::posix_time::ptime getCloseTime() const; + void setCloseTime(boost::posix_time::ptime); + uint64 getNextLedgerClose() const; + // low level functions SHAMap::pointer peekTransactionMap() { return mTransactionMap; } SHAMap::pointer peekAccountStateMap() { return mAccountStateMap; } diff --git a/src/NetworkOPs.cpp b/src/NetworkOPs.cpp index bb87f4dcb..0813d0b9d 100644 --- a/src/NetworkOPs.cpp +++ b/src/NetworkOPs.cpp @@ -4,6 +4,7 @@ #include #include +#include "utils.h" #include "Application.h" #include "Transaction.h" @@ -24,11 +25,21 @@ NetworkOPs::NetworkOPs(boost::asio::io_service& io_service) : mMode(omDISCONNECT setStateTimer(5); } -uint64 NetworkOPs::getNetworkTime() +time_t NetworkOPs::getNetworkTimeTT() { return time(NULL); } +boost::posix_time::ptime NetworkOPs::getNetworkTimePT() +{ + return boost::posix_time::from_time_t(getNetworkTimeTT()); +} + +uint64 NetworkOPs::getNetworkTimeNC() +{ + return iToSeconds(getNetworkTimePT()); +} + uint32 NetworkOPs::getCurrentLedgerID() { return theApp->getMasterLedger().getCurrentLedger()->getLedgerSeq(); @@ -89,7 +100,7 @@ Transaction::pointer NetworkOPs::processTransaction(Transaction::pointer trans, trans->getSTransaction()->getTransaction(*s, false); tx->set_rawtransaction(&s->getData().front(), s->getLength()); tx->set_status(newcoin::tsCURRENT); - tx->set_receivetimestamp(getNetworkTime()); + tx->set_receivetimestamp(getNetworkTimeNC()); tx->set_ledgerindexpossible(trans->getLedger()); PackedMessage::pointer packet(new PackedMessage(PackedMessage::MessagePointer(tx), newcoin::mtTRANSACTION)); @@ -280,8 +291,19 @@ void NetworkOPs::checkState() void NetworkOPs::switchLastClosedLedger(Ledger::pointer newLedger) { // set the newledger as our last closed ledger - // FIXME: Must recover transactions + // FIXME: Correct logic is: + // 1) Mark this ledger closed, schedule it to be saved + // 2) Open a new subsequent ledger + // 3) Walk back the previous ledger chain from our current ledger and the new last closed ledger + // find a common previous ledger, if possible. Try to insert any transactions in our ledger + // chain into the new open ledger. Broadcast any that make it in. + Ledger::pointer openLedger = boost::make_shared(newLedger); theApp->getMasterLedger().switchLedgers(newLedger, openLedger); - // FIXME: Set close timer + +#if 0 + if (getNetworkTime() > openLedger->getCloseTime()) + { // this ledger has already closed + } +#endif } diff --git a/src/NetworkOPs.h b/src/NetworkOPs.h index bef0a4b44..4003dce23 100644 --- a/src/NetworkOPs.h +++ b/src/NetworkOPs.h @@ -37,7 +37,9 @@ public: NetworkOPs(boost::asio::io_service& io_service); // network information - uint64 getNetworkTime(); + uint64 getNetworkTimeNC(); + time_t getNetworkTimeTT(); + boost::posix_time::ptime getNetworkTimePT(); uint32 getCurrentLedgerID(); OperatingMode getOperatingMode() { return mMode; } diff --git a/src/Peer.cpp b/src/Peer.cpp index e257679af..1db60cbf3 100644 --- a/src/Peer.cpp +++ b/src/Peer.cpp @@ -774,7 +774,7 @@ void Peer::sendHello() h->set_version(theConfig.VERSION); h->set_ledgerindex(theApp->getOPs().getCurrentLedgerID()); - h->set_nettime(theApp->getOPs().getNetworkTime()); + h->set_nettime(theApp->getOPs().getNetworkTimeNC()); h->set_nodepublic(theApp->getWallet().getNodePublic().humanNodePublic()); h->set_nodeproof(&vchSig[0], vchSig.size()); h->set_ipv4port(theConfig.PEER_PORT); diff --git a/src/SHAMap.cpp b/src/SHAMap.cpp index 4ef64dde8..b7f94cfa9 100644 --- a/src/SHAMap.cpp +++ b/src/SHAMap.cpp @@ -11,9 +11,9 @@ SHAMap::SHAMap(uint32 seq) : mSeq(seq), mState(Modifying) { - root=boost::make_shared(SHAMapNode(0, uint256()), mSeq); + root = boost::make_shared(SHAMapNode(0, uint256()), mSeq); root->makeInner(); - mTNByID[*root]=root; + mTNByID[*root] = root; } std::stack SHAMap::getStack(const uint256& id, bool include_nonmatching_leaf) @@ -21,27 +21,27 @@ std::stack SHAMap::getStack(const uint256& id, bool inc // Walk the tree as far as possible to the specified identifier // produce a stack of nodes along the way, with the terminal node at the top std::stack stack; - SHAMapTreeNode::pointer node=root; + SHAMapTreeNode::pointer node = root; - while(!node->isLeaf()) + while (!node->isLeaf()) { stack.push(node); - int branch=node->selectBranch(id); - assert(branch>=0); + int branch = node->selectBranch(id); + assert(branch >= 0); - uint256 hash=node->getChildHash(branch); - if(hash.isZero()) return stack; + uint256 hash = node->getChildHash(branch); + if (hash.isZero()) return stack; - node=getNode(node->getChildNodeID(branch), hash, false); - if(!node) + node = getNode(node->getChildNodeID(branch), hash, false); + if (!node) { - if(isSynching()) return stack; + if (isSynching()) return stack; throw SHAMapException(MissingNode); } } - if(include_nonmatching_leaf || (node->peekItem()->getTag()==id)) + if (include_nonmatching_leaf || (node->peekItem()->getTag() == id)) stack.push(node); return stack; @@ -51,20 +51,20 @@ void SHAMap::dirtyUp(std::stack& stack, const uint256& { // walk the tree up from through the inner nodes to the root // update linking hashes and add nodes to dirty list - assert(mState!=Synching && mState!=Immutable); + assert((mState != Synching) && (mState != Immutable)); - while(!stack.empty()) + while (!stack.empty()) { SHAMapTreeNode::pointer node=stack.top(); stack.pop(); assert(node->isInnerNode()); - int branch=node->selectBranch(target); - assert(branch>=0); + int branch = node->selectBranch(target); + assert(branch >= 0); returnNode(node, true); - if(!node->setChildHash(branch, prevHash)) + if (!node->setChildHash(branch, prevHash)) { std::cerr << "dirtyUp terminates early" << std::endl; assert(false); @@ -73,15 +73,15 @@ void SHAMap::dirtyUp(std::stack& stack, const uint256& #ifdef ST_DEBUG std::cerr << "dirtyUp sets branch " << branch << " to " << prevHash.GetHex() << std::endl; #endif - prevHash=node->getNodeHash(); + prevHash = node->getNodeHash(); assert(prevHash.isNonZero()); } } SHAMapTreeNode::pointer SHAMap::checkCacheNode(const SHAMapNode& iNode) { - boost::unordered_map::iterator it=mTNByID.find(iNode); - if(it==mTNByID.end()) return SHAMapTreeNode::pointer(); + boost::unordered_map::iterator it = mTNByID.find(iNode); + if (it == mTNByID.end()) return SHAMapTreeNode::pointer(); return it->second; } @@ -90,27 +90,27 @@ SHAMapTreeNode::pointer SHAMap::walkTo(const uint256& id, bool modify) SHAMapTreeNode::pointer inNode=root; - while(!inNode->isLeaf()) + while (!inNode->isLeaf()) { - int branch=inNode->selectBranch(id); - if(inNode->isEmptyBranch(branch)) return inNode; - uint256 childHash=inNode->getChildHash(branch); - if(childHash.isZero()) return inNode; + int branch = inNode->selectBranch(id); + if (inNode->isEmptyBranch(branch)) return inNode; + uint256 childHash = inNode->getChildHash(branch); + if (childHash.isZero()) return inNode; - SHAMapTreeNode::pointer nextNode=getNode(inNode->getChildNodeID(branch), childHash, false); - if(!nextNode) throw SHAMapException(MissingNode); - inNode=nextNode; + SHAMapTreeNode::pointer nextNode = getNode(inNode->getChildNodeID(branch), childHash, false); + if (!nextNode) throw SHAMapException(MissingNode); + inNode = nextNode; } - if(modify) returnNode(inNode, true); + if (modify) returnNode(inNode, true); return inNode; } SHAMapTreeNode::pointer SHAMap::getNode(const SHAMapNode& id, const uint256& hash, bool modify) { // retrieve a node whose node hash is known - SHAMapTreeNode::pointer node=checkCacheNode(id); - if(node) + SHAMapTreeNode::pointer node = checkCacheNode(id); + if (node) { - if(node->getNodeHash()!=hash) + if (node->getNodeHash()!=hash) { #ifdef DEBUG std::cerr << "Attempt to get node, hash not in tree" << std::endl; @@ -128,10 +128,10 @@ SHAMapTreeNode::pointer SHAMap::getNode(const SHAMapNode& id, const uint256& has std::vector nodeData; if(!fetchNode(hash, nodeData)) return SHAMapTreeNode::pointer(); - node=boost::make_shared(id, nodeData, mSeq); - if(node->getNodeHash()!=hash) throw SHAMapException(InvalidNode); + node = boost::make_shared(id, nodeData, mSeq); + if (node->getNodeHash() != hash) throw SHAMapException(InvalidNode); - if(!mTNByID.insert(std::make_pair(id, node)).second) + if (!mTNByID.insert(std::make_pair(id, node)).second) assert(false); return node; } @@ -139,15 +139,15 @@ SHAMapTreeNode::pointer SHAMap::getNode(const SHAMapNode& id, const uint256& has void SHAMap::returnNode(SHAMapTreeNode::pointer& node, bool modify) { // make sure the node is suitable for the intended operation (copy on write) assert(node->isValid()); - if(node && modify && (node->getSeq()!=mSeq)) + if (node && modify && (node->getSeq()!=mSeq)) { #ifdef DEBUG std::cerr << "returnNode COW" << std::endl; #endif - if(mDirtyNodes) (*mDirtyNodes)[*node]=node; - node=boost::make_shared(*node, mSeq); + if (mDirtyNodes) (*mDirtyNodes)[*node] = node; + node = boost::make_shared(*node, mSeq); assert(node->isValid()); - mTNByID[*node]=node; + mTNByID[*node] = node; } } @@ -163,24 +163,24 @@ SHAMapItem::pointer SHAMap::firstBelow(SHAMapTreeNode::pointer node) #endif do { // Walk down the tree - if(node->hasItem()) return node->peekItem(); + if (node->hasItem()) return node->peekItem(); - bool foundNode=false; - for(int i=0; i<16; i++) - if(!node->isEmptyBranch(i)) + bool foundNode = false; + for (int i = 0; i < 16; ++i) + if (!node->isEmptyBranch(i)) { #ifdef ST_DEBUG std::cerr << " FB: node " << node->getString() << std::endl; std::cerr << " has non-empty branch " << i << " : " << node->getChildNodeID(i).getString() << ", " << node->getChildHash(i).GetHex() << std::endl; #endif - node=getNode(node->getChildNodeID(i), node->getChildHash(i), false); - if(!node) throw SHAMapException(MissingNode); - foundNode=true; + node = getNode(node->getChildNodeID(i), node->getChildHash(i), false); + if (!node) throw SHAMapException(MissingNode); + foundNode = true; break; } - if(!foundNode) return SHAMapItem::pointer(); - } while(1); + if (!foundNode) return SHAMapItem::pointer(); + } while (1); } SHAMapItem::pointer SHAMap::lastBelow(SHAMapTreeNode::pointer node) @@ -191,46 +191,46 @@ SHAMapItem::pointer SHAMap::lastBelow(SHAMapTreeNode::pointer node) do { // Walk down the tree - if(node->hasItem()) return node->peekItem(); + if (node->hasItem()) return node->peekItem(); - bool foundNode=false; - for(int i=15; i>=0; i++) - if(!node->isEmptyBranch(i)) + bool foundNode = false; + for (int i = 15; i >= 0; ++i) + if (!node->isEmptyBranch(i)) { - node=getNode(node->getChildNodeID(i), node->getChildHash(i), false); - if(!node) throw SHAMapException(MissingNode); - foundNode=true; + node = getNode(node->getChildNodeID(i), node->getChildHash(i), false); + if (!node) throw SHAMapException(MissingNode); + foundNode = true; break; } - if(!foundNode) return SHAMapItem::pointer(); - } while(1); + if (!foundNode) return SHAMapItem::pointer(); + } while (1); } SHAMapItem::pointer SHAMap::onlyBelow(SHAMapTreeNode::pointer node) { // If there is only one item below this node, return it bool found; - while(!node->isLeaf()) + while (!node->isLeaf()) { - found=false; + found = false; SHAMapTreeNode::pointer nextNode; - for(int i=0; i<16; i++) - if(!node->isEmptyBranch(i)) + for (int i = 0; i < 16; ++i) + if (!node->isEmptyBranch(i)) { - if(found) return SHAMapItem::pointer(); // two leaves below - nextNode=getNode(node->getChildNodeID(i), node->getChildHash(i), false); - if(!nextNode) throw SHAMapException(MissingNode); - found=true; + if( found) return SHAMapItem::pointer(); // two leaves below + nextNode = getNode(node->getChildNodeID(i), node->getChildHash(i), false); + if (!nextNode) throw SHAMapException(MissingNode); + found = true; } - if(!found) + if (!found) { std::cerr << node->getString() << std::endl; assert(false); return SHAMapItem::pointer(); } - node=nextNode; + node = nextNode; } assert(node->hasItem()); return node->peekItem(); @@ -618,11 +618,11 @@ void SHAMap::dump(bool hash) } -static std::vectorIntToVUC(int i) +static std::vectorIntToVUC(int v) { std::vector vuc; - for(int i=0; i<32; i++) - vuc.push_back((unsigned char) i); + for (int i = 0; i < 32; ++i) + vuc.push_back(static_cast(v)); return vuc; } diff --git a/src/SHAMap.h b/src/SHAMap.h index 431de41ea..edbbbe4f7 100644 --- a/src/SHAMap.h +++ b/src/SHAMap.h @@ -40,14 +40,14 @@ public: SHAMapNode(int depth, const uint256& hash); int getDepth() const { return mDepth; } const uint256& getNodeID() const { return mNodeID; } - bool isValid() const { return (mDepth>=0) && (mDepth<64); } + bool isValid() const { return (mDepth >= 0) && (mDepth < 64); } virtual bool isPopulated() const { return false; } SHAMapNode getParentNodeID() const { assert(mDepth); - return SHAMapNode(mDepth-1, mNodeID); + return SHAMapNode(mDepth - 1, mNodeID); } SHAMapNode getChildNodeID(int m) const; int selectBranch(const uint256& hash) const; @@ -108,18 +108,18 @@ public: void updateData(const std::vector& data) { mData=data; } - bool operator<(const SHAMapItem& i) const { return mTag(const SHAMapItem& i) const { return mTag>i.mTag; } - bool operator==(const SHAMapItem& i) const { return mTag==i.mTag; } - bool operator!=(const SHAMapItem& i) const { return mTag!=i.mTag; } - bool operator<=(const SHAMapItem& i) const { return mTag<=i.mTag; } - bool operator>=(const SHAMapItem& i) const { return mTag>=i.mTag; } - bool operator<(const uint256& i) const { return mTag(const uint256& i) const { return mTag>i; } - bool operator==(const uint256& i) const { return mTag==i; } - bool operator!=(const uint256& i) const { return mTag!=i; } - bool operator<=(const uint256& i) const { return mTag<=i; } - bool operator>=(const uint256& i) const { return mTag>=i; } + bool operator<(const SHAMapItem& i) const { return mTag < i.mTag; } + bool operator>(const SHAMapItem& i) const { return mTag > i.mTag; } + bool operator==(const SHAMapItem& i) const { return mTag == i.mTag; } + bool operator!=(const SHAMapItem& i) const { return mTag != i.mTag; } + bool operator<=(const SHAMapItem& i) const { return mTag <= i.mTag; } + bool operator>=(const SHAMapItem& i) const { return mTag >= i.mTag; } + bool operator<(const uint256& i) const { return mTag < i; } + bool operator>(const uint256& i) const { return mTag > i; } + bool operator==(const uint256& i) const { return mTag == i; } + bool operator!=(const uint256& i) const { return mTag != i; } + bool operator<=(const uint256& i) const { return mTag <= i; } + bool operator>=(const uint256& i) const { return mTag >= i; } virtual void dump(); }; @@ -132,10 +132,10 @@ public: enum TNType { - tnERROR =0, - tnINNER =1, - tnTRANSACTION =2, - tnACCOUNT_STATE =3 + tnERROR = 0, + tnINNER = 1, + tnTRANSACTION = 2, + tnACCOUNT_STATE = 3 }; private: @@ -295,7 +295,7 @@ public: // status functions void setImmutable(void) { assert(mState != Invalid); mState = Immutable; } void clearImmutable(void) { mState = Modifying; } - bool isSynching(void) const { return mState == Floating || mState == Synching; } + bool isSynching(void) const { return (mState == Floating) || (mState == Synching); } void setSynching(void) { mState = Synching; } void setFloating(void) { mState = Floating; } void clearSynching(void) { mState = Modifying; } diff --git a/src/SHAMapNodes.cpp b/src/SHAMapNodes.cpp index 85ad34879..4bf54056f 100644 --- a/src/SHAMapNodes.cpp +++ b/src/SHAMapNodes.cpp @@ -97,11 +97,11 @@ SHAMapNode::SHAMapNode(int depth, const uint256 &hash) : mDepth(depth) SHAMapNode::SHAMapNode(const void *ptr, int len) { - if(len<33) mDepth=-1; + if (len < 33) mDepth = -1; else { memcpy(&mNodeID, ptr, 32); - mDepth=*(static_cast(ptr) + 32); + mDepth = *(static_cast(ptr) + 32); } } @@ -120,21 +120,21 @@ std::string SHAMapNode::getRawString() const SHAMapNode SHAMapNode::getChildNodeID(int m) const { // This can be optimized to avoid the << if needed - assert((m>=0) && (m<16)); + assert((m >= 0) && (m < 16)); uint256 child(mNodeID); - child.PeekAt(mDepth/8) |= m << (4*(mDepth%8)); - return SHAMapNode(mDepth+1, child); + child.PeekAt(mDepth / 8) |= m << (4 * (mDepth % 8)); + return SHAMapNode(mDepth + 1, child); } int SHAMapNode::selectBranch(const uint256& hash) const { // Which branch would contain the specified hash - if(mDepth==63) + if (mDepth == 63) { assert(false); return -1; } - if((hash&smMasks[mDepth])!=mNodeID) + if ((hash & smMasks[mDepth]) != mNodeID) { std::cerr << "selectBranch(" << getString() << std::endl; std::cerr << " " << hash.GetHex() << " off branch" << std::endl; @@ -142,11 +142,11 @@ int SHAMapNode::selectBranch(const uint256& hash) const return -1; // does not go under this node } - int branch=*(hash.begin()+(mDepth/2)); - if(mDepth%2) branch>>=4; - else branch&=0xf; + int branch = *(hash.begin() + (mDepth / 2)); + if (mDepth % 2) branch >>= 4; + else branch &= 0xf; - assert(branch>=0 && branch<16); + assert((branch >= 0) && (branch < 16)); return branch; } @@ -164,7 +164,7 @@ SHAMapTreeNode::SHAMapTreeNode(const SHAMapTreeNode& node, uint32 seq) : SHAMapN mHash(node.mHash), mItem(node.mItem), mSeq(seq), mType(node.mType), mFullBelow(false) { if(node.mItem) - mItem=boost::make_shared(*node.mItem); + mItem = boost::make_shared(*node.mItem); else memcpy(mHashes, node.mHashes, sizeof(mHashes)); } @@ -172,7 +172,7 @@ SHAMapTreeNode::SHAMapTreeNode(const SHAMapTreeNode& node, uint32 seq) : SHAMapN SHAMapTreeNode::SHAMapTreeNode(const SHAMapNode& node, SHAMapItem::pointer item, TNType type, uint32 seq) : SHAMapNode(node), mItem(item), mSeq(seq), mType(type), mFullBelow(true) { - assert(item->peekData().size()>=12); + assert(item->peekData().size() >= 12); updateHash(); } @@ -181,41 +181,41 @@ SHAMapTreeNode::SHAMapTreeNode(const SHAMapNode& id, const std::vector3) || (len<32) ) throw SHAMapException(InvalidNode); + int type = s.removeLastByte(); + int len = s.getLength(); + if ((type < 0) || (type > 3) || (len < 32)) throw SHAMapException(InvalidNode); - if(type==0) + if (type == 0) { // transaction - mItem=boost::make_shared(s.getSHA512Half(), s.peekData()); - mType=tnTRANSACTION; + mItem = boost::make_shared(s.getSHA512Half(), s.peekData()); + mType = tnTRANSACTION; } - else if(type==1) + else if (type == 1) { // account state uint256 u; - s.get256(u, len-32); - s.chop(256/8); - if(u.isZero()) throw SHAMapException(InvalidNode); - mItem=boost::make_shared(u, s.peekData()); - mType=tnACCOUNT_STATE; + s.get256(u, len - 32); + s.chop(256 / 8); + if (u.isZero()) throw SHAMapException(InvalidNode); + mItem = boost::make_shared(u, s.peekData()); + mType = tnACCOUNT_STATE; } - else if(type==2) + else if (type == 2) { // full inner - if(len!=512) throw SHAMapException(InvalidNode); - for(int i=0; i<16; i++) - s.get256(mHashes[i], i*32); - mType=tnINNER; + if (len != 512) throw SHAMapException(InvalidNode); + for (int i = 0; i < 16; ++i) + s.get256(mHashes[i], i * 32); + mType = tnINNER; } - else if(type==3) + else if (type == 3) { // compressed inner - for(int i=0; i<(len/33); i++) + for (int i = 0; i < (len / 33); ++i) { int pos; - s.get8(pos, 32+(i*33)); - if( (pos<0) || (pos>=16)) throw SHAMapException(InvalidNode); - s.get256(mHashes[pos], i*33); + s.get8(pos, 32 + (i * 33)); + if ((pos < 0) || (pos >= 16)) throw SHAMapException(InvalidNode); + s.get256(mHashes[pos], i* 3 3); } - mType=tnINNER; + mType = tnINNER; } updateHash(); @@ -223,17 +223,17 @@ SHAMapTreeNode::SHAMapTreeNode(const SHAMapNode& id, const std::vectoraddRaw(s); s.add8(0); - assert(s.getLength()>32); + assert(s.getLength() > 32); return; } - if(mType==tnACCOUNT_STATE) + if (mType == tnACCOUNT_STATE) { mItem->addRaw(s); s.add256(mItem->getTag()); @@ -241,10 +241,10 @@ void SHAMapTreeNode::addRaw(Serializer &s) return; } - if(getBranchCount()<12) + if (getBranchCount() < 12) { // compressed node - for(int i=0; i<16; i++) - if(mHashes[i].isNonZero()) + for (int i = 0; i < 16; ++i) + if (mHashes[i].isNonZero()) { s.add256(mHashes[i]); s.add8(i); @@ -253,7 +253,7 @@ void SHAMapTreeNode::addRaw(Serializer &s) return; } - for(int i=0; i<16; i++) + for (int i = 0; i < 16; ++i) s.add256(mHashes[i]); s.add8(2); } @@ -262,44 +262,44 @@ bool SHAMapTreeNode::updateHash() { uint256 nh; - if(mType==tnINNER) + if (mType == tnINNER) { - bool empty=true; - for(int i=0; i<16; i++) - if(mHashes[i].isNonZero()) + bool empty = true; + for (int i = 0; i < 16; ++i) + if (mHashes[i].isNonZero()) { - empty=false; + empty = false; break; } if(!empty) - nh=Serializer::getSHA512Half(reinterpret_cast(mHashes), sizeof(mHashes)); + nh = Serializer::getSHA512Half(reinterpret_cast(mHashes), sizeof(mHashes)); } - else if(mType==tnACCOUNT_STATE) + else if (mType == tnACCOUNT_STATE) { Serializer s; mItem->addRaw(s); s.add160(mItem->getTag().to160()); - nh=s.getSHA512Half(); + nh = s.getSHA512Half(); } - else if(mType==tnTRANSACTION) + else if (mType == tnTRANSACTION) { - nh=Serializer::getSHA512Half(mItem->peekData()); + nh = Serializer::getSHA512Half(mItem->peekData()); } else assert(false); - if(nh==mHash) return false; - mHash=nh; + if (nh == mHash) return false; + mHash = nh; return true; } bool SHAMapTreeNode::setItem(SHAMapItem::pointer& i, TNType type) { - uint256 hash=getNodeHash(); - mType=type; - mItem=i; + uint256 hash = getNodeHash(); + mType = type; + mItem = i; assert(isLeaf()); updateHash(); - return getNodeHash()==hash; + return getNodeHash() == hash; } SHAMapItem::pointer SHAMapTreeNode::getItem() const @@ -311,17 +311,17 @@ SHAMapItem::pointer SHAMapTreeNode::getItem() const int SHAMapTreeNode::getBranchCount() const { assert(isInner()); - int ret=0; - for(int i=0; i<16; ++i) - if(mHashes[i].isNonZero()) ++ret; + int ret = 0; + for (int i = 0; i < 16; ++i) + if (mHashes[i].isNonZero()) ++ret; return ret; } void SHAMapTreeNode::makeInner() { - mItem=SHAMapItem::pointer(); + mItem = SHAMapItem::pointer(); memset(mHashes, 0, sizeof(mHashes)); - mType=tnINNER; + mType = tnINNER; mHash.zero(); } @@ -332,40 +332,40 @@ void SHAMapTreeNode::dump() std::string SHAMapTreeNode::getString() const { - std::string ret="NodeID("; - ret+=boost::lexical_cast(getDepth()); - ret+=","; - ret+=getNodeID().GetHex(); - ret+=")"; - if(isInner()) + std::string ret = "NodeID("; + ret += boost::lexical_cast(getDepth()); + ret += ","; + ret += getNodeID().GetHex(); + ret += ")"; + if (isInner()) { - for(int i=0; i<16; i++) - if(!isEmptyBranch(i)) + for(int i = 0; i < 16; ++i) + if (!isEmptyBranch(i)) { - ret+=",b"; - ret+=boost::lexical_cast(i); + ret += ",b"; + ret += boost::lexical_cast(i); } } - if(isLeaf()) + if (isLeaf()) { - ret+=",leaf"; + ret += ",leaf"; } return ret; } bool SHAMapTreeNode::setChildHash(int m, const uint256 &hash) { - assert( (m>=0) && (m<16) ); - assert(mType==tnINNER); - if(mHashes[m]==hash) + assert((m >= 0) && (m < 16)); + assert(mType == tnINNER); + if(mHashes[m] == hash) return false; - mHashes[m]=hash; + mHashes[m] = hash; return updateHash(); } const uint256& SHAMapTreeNode::getChildHash(int m) const { - assert( (m>=0) && (m<16) && (mType==tnINNER) ); + assert((m >= 0) && (m < 16) && (mType == tnINNER)); return mHashes[m]; } // vim:ts=4