More ledger consensus work.

This commit is contained in:
JoelKatz
2012-05-28 17:25:25 -07:00
parent 62b353de27
commit 32534e8d0d
6 changed files with 59 additions and 19 deletions

View File

@@ -163,6 +163,8 @@ void LedgerConsensus::closeTime(Ledger::pointer& current)
void LedgerConsensus::mapComplete(const uint256& hash, SHAMap::pointer map)
{
mAcquiring.erase(hash);
if (!map)
{ // this is an invalid/corrupt map
mComplete[hash] = map;
@@ -252,6 +254,7 @@ int LedgerConsensus::timerEntry()
bool changes = false;
SHAMap::pointer ourPosition;
std::vector<uint256> addedTx, removedTx;
for(boost::unordered_map<uint256, LCTransaction::pointer>::iterator it = mDisputes.begin(),
end = mDisputes.end(); it != end; ++it)
@@ -264,16 +267,26 @@ int LedgerConsensus::timerEntry()
changes = true;
}
if (it->second->getOurPosition()) // now a yes
{
ourPosition->addItem(SHAMapItem(it->first, it->second->peekTransaction()), true);
addedTx.push_back(it->first);
}
else // now a no
{
ourPosition->delItem(it->first);
removedTx.push_back(it->first);
}
}
}
if (changes)
{
// broadcast IHAVE
// broadcast new proposal
uint256 newHash = ourPosition->getHash();
mOurPosition->changePosition(newHash);
propose(addedTx, removedTx);
std::vector<uint256> hashes;
hashes.push_back(newHash);
sendHaveTxSet(hashes);
}
}
return 1;
@@ -321,6 +334,20 @@ void LedgerConsensus::startAcquiring(TransactionAcquire::pointer acquire)
}
}
void LedgerConsensus::propose(const std::vector<uint256>& added, const std::vector<uint256>& removed)
{
newcoin::TMProposeSet prop;
prop.set_currenttxhash(mOurPosition->getCurrentHash().begin(), 256 / 8);
prop.set_prevclosedhash(mOurPosition->getPrevLedger().begin(), 256 / 8);
prop.set_proposeseq(mOurPosition->getProposeSeq());
std::vector<unsigned char> pubKey = mOurPosition->getPubKey();
std::vector<unsigned char> sig = mOurPosition->sign();
prop.set_nodepubkey(&pubKey[0], pubKey.size());
prop.set_signature(&sig[0], sig.size());
theApp->getConnectionPool().relayMessage(NULL, boost::make_shared<PackedMessage>(prop, newcoin::mtPROPOSE_LEDGER));
}
void LedgerConsensus::addDisputedTransaction(const uint256& txID, const std::vector<unsigned char>& tx)
{
boost::unordered_map<uint256, LCTransaction::pointer>::iterator it = mDisputes.find(txID);
@@ -350,6 +377,13 @@ void LedgerConsensus::addDisputedTransaction(const uint256& txID, const std::vec
bool LedgerConsensus::peerPosition(LedgerProposal::pointer newPosition)
{
if (newPosition->getPrevLedger() != mPreviousLedger->getHash())
{
#ifdef DEBUG
std::cerr << "Peer sends proposal with wrong previous ledger" << std::endl;
#endif
return false;
}
LedgerProposal::pointer& currentPosition = mPeerPositions[newPosition->getPeerID()];
if (currentPosition)
{

View File

@@ -99,6 +99,7 @@ protected:
void addDisputedTransaction(const uint256&, const std::vector<unsigned char>& transaction);
void adjustCount(SHAMap::pointer map, const std::vector<uint256>& peers);
void propose(const std::vector<uint256>& addedTx, const std::vector<uint256>& removedTx);
void addPosition(LedgerProposal&, bool ours);
void removePosition(LedgerProposal&, bool ours);

View File

@@ -6,9 +6,8 @@
#include "key.h"
#include "Application.h"
LedgerProposal::LedgerProposal(uint32 closingSeq, uint32 proposeSeq, const uint256& proposeTx,
const std::string& pubKey) : mCurrentHash(proposeTx),
mProposeSeq(proposeSeq), mKey(boost::make_shared<CKey>())
LedgerProposal::LedgerProposal(const uint256& pLgr, uint32 seq, const uint256& tx, const std::string& pubKey) :
mPreviousLedger(pLgr), mCurrentHash(tx), mProposeSeq(seq), mKey(boost::make_shared<CKey>())
{
if (!mKey->SetPubKey(pubKey))
throw std::runtime_error("Invalid public key in proposal");
@@ -23,13 +22,6 @@ LedgerProposal::LedgerProposal(CKey::pointer mPrivateKey, const uint256& prevLgr
mPeerID = Serializer::getSHA512Half(mKey->GetPubKey());
}
LedgerProposal::LedgerProposal(LedgerProposal::pointer previous, const uint256& newp) :
mPeerID(previous->mPeerID), mPreviousLedger(previous->mPreviousLedger),
mCurrentHash(newp), mProposeSeq(previous->mProposeSeq + 1), mKey(previous->mKey)
{
;
}
uint256 LedgerProposal::getSigningHash() const
{
Serializer s(72);
@@ -44,3 +36,17 @@ bool LedgerProposal::checkSign(const std::string& signature)
{
return mKey->Verify(getSigningHash(), signature);
}
void LedgerProposal::changePosition(const uint256& newPosition)
{
mCurrentHash = newPosition;
++mProposeSeq;
}
std::vector<unsigned char> LedgerProposal::sign(void)
{
std::vector<unsigned char> ret;
if (!mKey->Sign(getSigningHash(), ret))
throw std::runtime_error("unable to sign proposal");
return ret;
}

View File

@@ -16,7 +16,6 @@ protected:
uint256 mPeerID, mPreviousLedger, mCurrentHash;
uint32 mProposeSeq;
CKey::pointer mKey;
// std::vector<uint256> mAddedTx, mRemovedTx;
static const uint32 sProposeMagic = 0x50525000; // PRP
@@ -25,14 +24,11 @@ public:
typedef boost::shared_ptr<LedgerProposal> pointer;
// proposal from peer
LedgerProposal(uint32 closingSeq, uint32 proposeSeq, const uint256& propose, const std::string& pubKey);
LedgerProposal(const uint256& prevLgr, uint32 proposeSeq, const uint256& propose, const std::string& pubKey);
// our first proposal
LedgerProposal(CKey::pointer privateKey, const uint256& prevLedger, const uint256& position);
// our following proposals
LedgerProposal(LedgerProposal::pointer previous, const uint256& newPosition);
uint256 getSigningHash() const;
bool checkSign(const std::string& signature);
@@ -40,7 +36,10 @@ public:
const uint256& getCurrentHash() const { return mCurrentHash; }
const uint256& getPrevLedger() const { return mPreviousLedger; }
uint32 getProposeSeq() const { return mProposeSeq; }
std::vector<unsigned char> getPubKey() const { return mKey->GetPubKey(); }
std::vector<unsigned char> sign();
void changePosition(const uint256& newPosition);
};
#endif

View File

@@ -77,7 +77,7 @@ public:
const std::vector<unsigned char>& myNode, std::list< std::vector<unsigned char> >& newNodes);
// ledger proposal/close functions
bool proposeLedger(uint32 closingSeq, uint32 proposeSeq, const uint256& proposeHash,
bool proposeLedger(const uint256& prevLgrHash, uint32 proposeSeq, const uint256& proposeHash,
const std::string& pubKey, const std::string& signature);
bool gotTXData(boost::shared_ptr<Peer> peer, const uint256& hash,
const std::list<SHAMapNode>& nodeIDs, const std::list< std::vector<unsigned char> >& nodeData);

View File

@@ -102,7 +102,7 @@ message TMStatusChange {
// Announce to the network our position on a closing ledger
message TMProposeSet {
required uint32 closingSeq = 1;
required bytes prevClosedHash = 1;
required uint32 proposeSeq = 2;
required bytes currentTxHash = 3; // the hash of the ledger we are proposing
required bytes nodePubKey = 4;