From aa7946dcfdc7d7c4b1a34c53edde5315cfd0a190 Mon Sep 17 00:00:00 2001 From: jed Date: Thu, 3 Nov 2011 11:17:06 -0700 Subject: [PATCH] . --- Application.h | 14 +++++--- ConnectionPool.h | 7 +++- LedgerHistory.cpp | 4 ++- Peer.cpp | 7 ++-- ValidationCollection.cpp | 70 ++++++++++++++++++++++++---------------- ValidationCollection.h | 6 ++-- db layout.txt | 2 +- newcoin.vcxproj | 2 -- newcoin.vcxproj.filters | 6 ---- 9 files changed, 69 insertions(+), 49 deletions(-) diff --git a/Application.h b/Application.h index 2333c4728e..2ad54b09a2 100644 --- a/Application.h +++ b/Application.h @@ -1,3 +1,6 @@ +#ifndef __APPLICATION__ +#define __APPLICATION__ + #include "UniqueNodeList.h" #include "ConnectionPool.h" #include "KnownNodeList.h" @@ -5,7 +8,6 @@ #include "TimingService.h" #include "ValidationCollection.h" #include "Wallet.h" -#include "Serializer.h" #include "database/database.h" #include @@ -28,7 +30,7 @@ class Application ConnectionPool mConnectionPool; PeerDoor* mPeerDoor; RPCDoor* mRPCDoor; - Serializer* mSerializer; + //Serializer* mSerializer; boost::asio::io_service mIOService; @@ -46,8 +48,8 @@ public: void setDB(Database* db){ mDatabase=db; } - Serializer* getSerializer(){ return(mSerializer); } - void setSerializer(Serializer* ser){ mSerializer=ser; } + //Serializer* getSerializer(){ return(mSerializer); } + //void setSerializer(Serializer* ser){ mSerializer=ser; } void run(); @@ -55,4 +57,6 @@ public: }; -extern Application* theApp; \ No newline at end of file +extern Application* theApp; + +#endif \ No newline at end of file diff --git a/ConnectionPool.h b/ConnectionPool.h index 53d5294da4..0b7f0f9bd2 100644 --- a/ConnectionPool.h +++ b/ConnectionPool.h @@ -1,3 +1,6 @@ +#ifndef __CONNECTION_POOL__ +#define __CONNECTION_POOL__ + #include "Peer.h" #include "PackedMessage.h" #include "types.h" @@ -19,4 +22,6 @@ public: //bool isMessageKnown(PackedMessage::pointer msg); -}; \ No newline at end of file +}; + +#endif \ No newline at end of file diff --git a/LedgerHistory.cpp b/LedgerHistory.cpp index 0833797e3b..f02f65c3b7 100644 --- a/LedgerHistory.cpp +++ b/LedgerHistory.cpp @@ -28,12 +28,14 @@ bool LedgerHistory::loadLedger(uint256& hash) bool LedgerHistory::loadAcceptedLedger(uint32 index) { + // TODO: LedgerHistory::loadAcceptedLedger(uint32 index) + /* Ledger::pointer ledger=theApp->getSerializer()->loadAcceptedLedger(index); if(ledger) { mAcceptedLedgers[index]=ledger; return(true); - } + }*/ return(false); } diff --git a/Peer.cpp b/Peer.cpp index f8acb5bef4..52ca5c98a8 100644 --- a/Peer.cpp +++ b/Peer.cpp @@ -291,10 +291,11 @@ void Peer::receiveValidation(newcoin::Validation& validation) void Peer::receiveGetValidations(newcoin::GetValidations& request) { - vector* validations=theApp->getValidationCollection().getValidations(request.ledgerindex()); - if(validations) + vector validations; + theApp->getValidationCollection().getValidations(request.ledgerindex(),validations); + if(validations.size()) { - BOOST_FOREACH(newcoin::Validation& valid, *validations) + BOOST_FOREACH(newcoin::Validation& valid, validations) { PackedMessage::pointer packet(new PackedMessage(PackedMessage::MessagePointer(new newcoin::Validation(valid)),newcoin::VALIDATION)); sendPacket(packet); diff --git a/ValidationCollection.cpp b/ValidationCollection.cpp index 49cb2ea5c6..ee400ed3b6 100644 --- a/ValidationCollection.cpp +++ b/ValidationCollection.cpp @@ -28,29 +28,45 @@ void ValidationCollection::load() } -void ValidationCollection::addToDB(newcoin::Validation& valid,bool weCare) +void ValidationCollection::addToDB(newcoin::Validation& valid,int weCare) { Database* db=theApp->getDB(); + uint256 hash=protobufTo256(valid.hash()); + uint160 hanko=protobufTo160(valid.hanko()); + uint256 sig=protobufTo256(valid.sig()); + + string hashStr,hankoStr,sigStr; + db->escape(hash.begin(),hash.GetSerializeSize(),hashStr); + db->escape(hanko.begin(),hanko.GetSerializeSize(),hankoStr); + db->escape(sig.begin(),sig.GetSerializeSize(),sigStr); + string sql=strprintf("INSERT INTO Validations (LedgerIndex,Hash,Hanko,SeqNum,Sig,WeCare) values (%d,%s,%s,%d,%s,%d)",valid.ledgerindex(),hashStr.c_str(),hankoStr.c_str(),valid.seqnum(),sigStr.c_str(),weCare); + db->executeSQL(sql.c_str()); } -bool ValidationCollection::hasValidation(uint256& ledgerHash,uint160& hanko,uint32 seqnum) +bool ValidationCollection::hasValidation(uint32 ledgerIndex,uint160& hanko,uint32 seqnum) { - if(mValidations.count(ledgerHash)) + string hankoStr; + Database* db=theApp->getDB(); + db->escape(hanko.begin(),hanko.GetSerializeSize(),hankoStr); + string sql=strprintf("SELECT ValidationID,seqnum from Validations where LedgerIndex=%d and hanko=%s",ledgerIndex,hankoStr); + if(db->executeSQL(sql.c_str())) { - BOOST_FOREACH(newcoin::Validation& valid,mValidations[ledgerHash]) + if(db->startIterRows()) { - if( valid.seqnum()==seqnum && - protobufTo160(valid.hanko()) == hanko) return(true); - } - } - - if(mIgnoredValidations.count(ledgerHash)) - { - BOOST_FOREACH(newcoin::Validation& valid,mIgnoredValidations[ledgerHash]) - { - if( valid.seqnum()==seqnum && - protobufTo160(valid.hanko()) == hanko) return(true); + if(db->getNextRow()) + { + uint32 currentSeqNum=db->getInt(1); + if(currentSeqNum>=seqnum) + { + db->endIterRows(); + return(true); + } + // delete the old validation we were storing + sql=strprintf("DELETE FROM Validations where ValidationID=%d",db->getInt(0)); + db->endIterRows(); + db->executeSQL(sql.c_str()); + } } } return(false); @@ -67,20 +83,17 @@ void ValidationCollection::addValidation(newcoin::Validation& valid) uint160 hanko=protobufTo160(valid.hanko()); // make sure we don't already have this validation - if(hasValidation(hash,hanko,valid.seqnum())) return; + if(hasValidation(valid.ledgerindex(),hanko,valid.seqnum())) return; // check if we care about this hanko int validity=theApp->getUNL().checkValid(valid); if( validity==1 ) { - mValidations[hash].push_back(valid); - mIndexValidations[valid.ledgerindex()].push_back(valid); - addToGroup(valid); addToDB(valid,true); + addToGroup(valid); theApp->getLedgerMaster().checkConsensus(valid.ledgerindex()); }else if(validity==0) { - mIgnoredValidations[hash].push_back(valid); addToDB(valid,false); }else { // the signature wasn't valid @@ -147,7 +160,11 @@ void ValidationCollection::addToGroup(newcoin::Validation& newValid) newGroup.mValidations.push_back(newValid); newGroup.mSuperLedger=Ledger::pointer(new Ledger(newLedger)); // since this super ledger gets modified and we don't want to screw the original - BOOST_FOREACH(newcoin::Validation& valid,mIndexValidations[newValid.ledgerindex()]) + vector retVec; + getValidations(newValid.ledgerindex(),retVec); + + + BOOST_FOREACH(newcoin::Validation& valid,retVec) { uint256 hash=protobufTo256(valid.hash()); Ledger::pointer ledger=theApp->getLedgerMaster().getLedger(hash); @@ -169,13 +186,12 @@ void ValidationCollection::addToGroup(newcoin::Validation& newValid) } } -vector* ValidationCollection::getValidations(uint32 ledgerIndex) +void ValidationCollection::getValidations(uint32 ledgerIndex,vector& retVec) { - if(mIndexValidations.count(ledgerIndex)) - { - return(&(mIndexValidations[ledgerIndex])); - } - return(NULL); + string sql=strprintf("SELECT * From Validations where LedgerIndex=%d and wecare=1",ledgerIndex); + + // TODO: ValidationCollection::getValidations(uint32 ledgerIndex) + } diff --git a/ValidationCollection.h b/ValidationCollection.h index ff4973312e..db3c06fdd3 100644 --- a/ValidationCollection.h +++ b/ValidationCollection.h @@ -29,9 +29,9 @@ class ValidationCollection std::map > mIndexGroups; // all the groups at each index //std::map > mIndexValidations; // all the validations at each index - bool hasValidation(uint256& ledgerHash,uint160& hanko,uint32 seqnum); + bool hasValidation(uint32 ledgerIndex,uint160& hanko,uint32 seqnum); void addToGroup(newcoin::Validation& valid); - void addToDB(newcoin::Validation& valid,bool weCare); + void addToDB(newcoin::Validation& valid,int weCare); public: ValidationCollection(); @@ -40,7 +40,7 @@ public: void addValidation(newcoin::Validation& valid); - std::vector* getValidations(uint32 ledgerIndex); + void getValidations(uint32 ledgerIndex,std::vector& retVec); // It can miss some compatible ledgers of course if you don't know them diff --git a/db layout.txt b/db layout.txt index d84c21c7c7..773531c976 100644 --- a/db layout.txt +++ b/db layout.txt @@ -4,7 +4,7 @@ CREATE TABLE UNL (Hanko BLOB PRIMARY KEY, PubKey BLOB); CREATE TABLE Transactions (TransactionID INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, From BLOB, Dest BLOB, Amount BIGINT UNSIGNED, LedgerIndex INT UNSIGNED, SeqNum INT, PubKey BLOB, Sig BLOB); CREATE TABLE Ledgers (LedgerID INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, LedgerIndex INT UNSIGNED, Hash BLOB, ParentHash BLOB,FeeHeld BIGINT UNSIGNED); -CREATE TABLE Validations(LedgerIndex INT UNSIGNED, Hash BLOB, Hanko BLOB, SeqNum INT UNSIGNED, Sig BLOB, WeCare TINYINT); +CREATE TABLE Validations(ValidationID INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, LedgerIndex INT UNSIGNED, Hash BLOB, Hanko BLOB, SeqNum INT UNSIGNED, Sig BLOB, WeCare TINYINT); CREATE TABLE LedgerTransactionMap (LedgerID INT UNSIGNED, TransactionID INT UNSIGNED, Include TINYINT); CREATE TABLE LedgerAccountMap(LedgerID INT UNSIGNED,AccountID INT UNSIGNED); CREATE TABLE Accounts (AccountID INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, Address BLOB, Amount BIGINT UNSIGNED, SeqNum INT); diff --git a/newcoin.vcxproj b/newcoin.vcxproj index 628d4e4434..c9038335bb 100644 --- a/newcoin.vcxproj +++ b/newcoin.vcxproj @@ -95,7 +95,6 @@ - @@ -105,7 +104,6 @@ - diff --git a/newcoin.vcxproj.filters b/newcoin.vcxproj.filters index 316de98532..49604b9907 100644 --- a/newcoin.vcxproj.filters +++ b/newcoin.vcxproj.filters @@ -132,12 +132,6 @@ Source Files - - Source Files - - - Source Files - Header Files\util