Merge branch 'master' of github.com:jedmccaleb/NewCoin

This commit is contained in:
Arthur Britto
2012-05-07 18:39:32 -07:00
8 changed files with 231 additions and 183 deletions

View File

@@ -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<unsigned char>& 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

View File

@@ -6,6 +6,7 @@
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#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<unsigned char>& 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; }

View File

@@ -4,6 +4,7 @@
#include <boost/bind.hpp>
#include <boost/unordered_map.hpp>
#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<Ledger>(newLedger);
theApp->getMasterLedger().switchLedgers(newLedger, openLedger);
// FIXME: Set close timer
#if 0
if (getNetworkTime() > openLedger->getCloseTime())
{ // this ledger has already closed
}
#endif
}

View File

@@ -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; }

View File

@@ -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);

View File

@@ -51,7 +51,7 @@ void SHAMap::dirtyUp(std::stack<SHAMapTreeNode::pointer>& 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())
{
@@ -166,7 +166,7 @@ SHAMapItem::pointer SHAMap::firstBelow(SHAMapTreeNode::pointer node)
if (node->hasItem()) return node->peekItem();
bool foundNode = false;
for(int i=0; i<16; i++)
for (int i = 0; i < 16; ++i)
if (!node->isEmptyBranch(i))
{
#ifdef ST_DEBUG
@@ -194,7 +194,7 @@ SHAMapItem::pointer SHAMap::lastBelow(SHAMapTreeNode::pointer node)
if (node->hasItem()) return node->peekItem();
bool foundNode = false;
for(int i=15; i>=0; i++)
for (int i = 15; i >= 0; ++i)
if (!node->isEmptyBranch(i))
{
node = getNode(node->getChildNodeID(i), node->getChildHash(i), false);
@@ -215,7 +215,7 @@ SHAMapItem::pointer SHAMap::onlyBelow(SHAMapTreeNode::pointer node)
found = false;
SHAMapTreeNode::pointer nextNode;
for(int i=0; i<16; i++)
for (int i = 0; i < 16; ++i)
if (!node->isEmptyBranch(i))
{
if( found) return SHAMapItem::pointer(); // two leaves below
@@ -618,11 +618,11 @@ void SHAMap::dump(bool hash)
}
static std::vector<unsigned char>IntToVUC(int i)
static std::vector<unsigned char>IntToVUC(int v)
{
std::vector<unsigned char> 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<unsigned char>(v));
return vuc;
}

View File

@@ -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; }

View File

@@ -146,7 +146,7 @@ int SHAMapNode::selectBranch(const uint256& hash) const
if (mDepth % 2) branch >>= 4;
else branch &= 0xf;
assert(branch>=0 && branch<16);
assert((branch >= 0) && (branch < 16));
return branch;
}
@@ -202,13 +202,13 @@ SHAMapTreeNode::SHAMapTreeNode(const SHAMapNode& id, const std::vector<unsigned
else if (type == 2)
{ // full inner
if (len != 512) throw SHAMapException(InvalidNode);
for(int i=0; i<16; i++)
for (int i = 0; i < 16; ++i)
s.get256(mHashes[i], i * 32);
mType = tnINNER;
}
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));
@@ -243,7 +243,7 @@ void SHAMapTreeNode::addRaw(Serializer &s)
if (getBranchCount() < 12)
{ // compressed node
for(int i=0; i<16; i++)
for (int i = 0; i < 16; ++i)
if (mHashes[i].isNonZero())
{
s.add256(mHashes[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);
}
@@ -265,7 +265,7 @@ bool SHAMapTreeNode::updateHash()
if (mType == tnINNER)
{
bool empty = true;
for(int i=0; i<16; i++)
for (int i = 0; i < 16; ++i)
if (mHashes[i].isNonZero())
{
empty = false;
@@ -339,7 +339,7 @@ std::string SHAMapTreeNode::getString() const
ret += ")";
if (isInner())
{
for(int i=0; i<16; i++)
for(int i = 0; i < 16; ++i)
if (!isEmptyBranch(i))
{
ret += ",b";