diff --git a/Application.cpp b/Application.cpp index 71c5ec25b..8fbc6d199 100644 --- a/Application.cpp +++ b/Application.cpp @@ -35,6 +35,7 @@ Application::Application() void Application::run() { theApp->setDB(new SqliteDatabase("data.db")); + mDatabase->connect(); if(theConfig.PEER_PORT) { diff --git a/Convertion.cpp b/Convertion.cpp new file mode 100644 index 000000000..b6d6ce4c4 --- /dev/null +++ b/Convertion.cpp @@ -0,0 +1,42 @@ +#include "Convertion.h" +#include "base58.h" +using namespace std; + +uint160 protobufTo160(const std::string& buf) +{ + uint160 ret; + // TODO: + return(ret); +} + +uint256 protobufTo256(const std::string& hash) +{ + uint256 ret; + // TODO: + return(ret); +} + +uint160 humanTo160(const std::string& buf) +{ + vector retVec; + DecodeBase58(buf,retVec); + uint160 ret; + for(unsigned int n=0; n=ret.GetSerializeSize()) break; + ret.begin()[n]=retVec[n]; + } + + return(ret); +} + +bool humanToPK(const std::string& buf,std::vector& retVec) +{ + return(DecodeBase58(buf,retVec)); +} + +bool u160ToHuman(uint160& buf, std::string& retStr) +{ + retStr=EncodeBase58(buf.begin(),buf.end()); + return(true); +} \ No newline at end of file diff --git a/Convertion.h b/Convertion.h new file mode 100644 index 000000000..c2ceb5339 --- /dev/null +++ b/Convertion.h @@ -0,0 +1,11 @@ +#include "uint256.h" +#include + +extern uint160 protobufTo160(const std::string& buf); +extern uint256 protobufTo256(const std::string& hash); +extern uint160 humanTo160(const std::string& buf); +extern bool humanToPK(const std::string& buf,std::vector& retVec); + + +extern bool u160ToHuman(uint160& buf, std::string& retStr); + diff --git a/Ledger.cpp b/Ledger.cpp index 1a510476b..8a9ceafa5 100644 --- a/Ledger.cpp +++ b/Ledger.cpp @@ -3,6 +3,7 @@ #include "PackedMessage.h" #include "Application.h" #include "Config.h" +#include "Convertion.h" #include "BitcoinUtil.h" #include #include @@ -70,13 +71,13 @@ void Ledger::setTo(newcoin::FullLedger& ledger) mValidSig=false; mValidHash=false; - mParentHash=Transaction::protobufToInternalHash(ledger.parenthash()); + mParentHash=protobufTo256(ledger.parenthash()); int numAccounts=ledger.accounts_size(); for(int n=0; nfrom()); + uint160 address=protobufTo160(trans->from()); if(mAccounts.count(address)) { pair account=mAccounts[address]; @@ -309,7 +310,7 @@ bool Ledger::addTransaction(TransactionPtr trans,bool checkDuplicate) mAccounts[address]=account; - uint160 destAddress=NewcoinAddress::protobufToInternal(trans->dest()); + uint160 destAddress=protobufTo160(trans->dest()); Account destAccount=mAccounts[destAddress]; destAccount.first += trans->amount(); @@ -347,7 +348,7 @@ bool Ledger::addTransaction(TransactionPtr trans,bool checkDuplicate) // Don't check the amounts. We will do this at the end. void Ledger::addTransactionAllowNeg(TransactionPtr trans) { - uint160 fromAddress=NewcoinAddress::protobufToInternal(trans->from()); + uint160 fromAddress=protobufTo160(trans->from()); if(mAccounts.count(fromAddress)) { @@ -358,7 +359,7 @@ void Ledger::addTransactionAllowNeg(TransactionPtr trans) fromAccount.second++; mAccounts[fromAddress]=fromAccount; - uint160 destAddress=NewcoinAddress::protobufToInternal(trans->dest()); + uint160 destAddress=protobufTo160(trans->dest()); Account destAccount=mAccounts[destAddress]; destAccount.first += trans->amount(); @@ -377,7 +378,7 @@ void Ledger::addTransactionAllowNeg(TransactionPtr trans) mAccounts[fromAddress]=Account(-((int64)trans->amount()),1); - uint160 destAddress=NewcoinAddress::protobufToInternal(trans->dest()); + uint160 destAddress=protobufTo160(trans->dest()); Account destAccount=mAccounts[destAddress]; destAccount.first += trans->amount(); @@ -452,8 +453,8 @@ void Ledger::parentAddedTransaction(TransactionPtr cause) // an account to now be negative so we have to discard one // a discarded transaction to be pulled back in // seqnum invalidation - uint160 fromAddress=NewcoinAddress::protobufToInternal(cause->from()); - uint160 destAddress=NewcoinAddress::protobufToInternal(cause->dest()); + uint160 fromAddress=protobufTo160(cause->from()); + uint160 destAddress=protobufTo160(cause->dest()); Account* fromAccount=getAccount(fromAddress); Account* destAccount=getAccount(destAddress); @@ -549,7 +550,7 @@ void Ledger::correctAccount(uint160& address) for( list::reverse_iterator iter=mTransactions.rbegin(); iter != mTransactions.rend(); ) { TransactionPtr trans= *iter; - if(NewcoinAddress::protobufToInternal(trans->from()) == address) + if(protobufTo160(trans->from()) == address) { Account fromAccount=mAccounts[address]; assert(fromAccount.second==trans->seqnum()+1); @@ -560,7 +561,7 @@ void Ledger::correctAccount(uint160& address) mAccounts[address]=fromAccount; - uint160 destAddress=NewcoinAddress::protobufToInternal(trans->dest()); + uint160 destAddress=protobufTo160(trans->dest()); Account destAccount=mAccounts[destAddress]; destAccount.first -= trans->amount(); mAccounts[destAddress]=destAccount; diff --git a/LedgerMaster.cpp b/LedgerMaster.cpp index c6d643a09..4ff70f286 100644 --- a/LedgerMaster.cpp +++ b/LedgerMaster.cpp @@ -1,6 +1,7 @@ #include "LedgerMaster.h" #include "Application.h" #include "NewcoinAddress.h" +#include "Convertion.h" #include using namespace std; @@ -32,7 +33,7 @@ int64 LedgerMaster::getAmountHeld(uint160& addr) int64 LedgerMaster::getAmountHeld(std::string& addr) { - return(mCurrentLedger->getAmountHeld(NewcoinAddress::humanToInternal(addr))); + return(mCurrentLedger->getAmountHeld(humanTo160(addr))); } @@ -59,7 +60,7 @@ bool LedgerMaster::isValidTransaction(TransactionPtr trans) if(trans->from()==trans->dest()) return(false); if(trans->amount()==0) return(false); if(!Transaction::isSigValid(trans)) return(false); - Ledger::Account* account=mCurrentLedger->getAccount( NewcoinAddress::protobufToInternal(trans->from()) ); + Ledger::Account* account=mCurrentLedger->getAccount( protobufTo160(trans->from()) ); if(!account) return(false); if(trans->seqnum() != (account->second+1) ) return(false); // TODO: do we need to save these? @@ -128,7 +129,7 @@ void LedgerMaster::addFullLedger(newcoin::FullLedger& ledger) { // check if we already have this ledger // check that the hash is correct - uint256 inHash=Transaction::protobufToInternalHash(ledger.hash()); + uint256 inHash=protobufTo256(ledger.hash()); Ledger::pointer existingLedger=mLedgerHistory.getLedger( inHash ); if(existingLedger) return; @@ -214,7 +215,7 @@ void LedgerMaster::checkLedgerProposal(Peer::pointer peer, newcoin::ProposeLedge Ledger::pointer oldLedger=mLedgerHistory.getAcceptedLedger(otherLedger.ledgerindex()); if(oldLedger) { - if( (oldLedger->getHash()!=Transaction::protobufToInternalHash(otherLedger.hash())) && + if( (oldLedger->getHash()!=protobufTo256(otherLedger.hash())) && (oldLedger->getNumTransactions()>=otherLedger.numtransactions())) { peer->sendLedgerProposal(oldLedger); @@ -225,7 +226,7 @@ void LedgerMaster::checkLedgerProposal(Peer::pointer peer, newcoin::ProposeLedge addFutureProposal(peer,otherLedger); }else { // you guys are on the same page - uint256 otherHash=Transaction::protobufToInternalHash(otherLedger.hash()); + uint256 otherHash=protobufTo256(otherLedger.hash()); if(mFinalizingLedger->getHash()!= otherHash) { if( mFinalizingLedger->getNumTransactions()>=otherLedger.numtransactions()) diff --git a/NewcoinAddress.cpp b/NewcoinAddress.cpp index 058e1d3cf..5efa25a32 100644 --- a/NewcoinAddress.cpp +++ b/NewcoinAddress.cpp @@ -5,20 +5,6 @@ -uint160 NewcoinAddress::protobufToInternal(const std::string& buf) -{ - uint160 ret; - // TODO: - return(ret); -} - -uint160 NewcoinAddress::humanToInternal(const std::string& buf) -{ - uint160 ret; - // TODO: - return(ret); -} - bool NewcoinAddress::SetHash160(const uint160& hash160) { SetData(theConfig.TEST_NET ? 112 : 1, &hash160, 20); diff --git a/NewcoinAddress.h b/NewcoinAddress.h index c02ab821e..4a5ec3392 100644 --- a/NewcoinAddress.h +++ b/NewcoinAddress.h @@ -21,8 +21,7 @@ public: uint160 GetHash160(); - static uint160 protobufToInternal(const std::string& buf); - static uint160 humanToInternal(const std::string& buf); + }; diff --git a/Peer.cpp b/Peer.cpp index 7d2043fb2..8bd0adada 100644 --- a/Peer.cpp +++ b/Peer.cpp @@ -6,6 +6,7 @@ //#include #include #include +#include "Convertion.h" using namespace std; using namespace boost; @@ -279,7 +280,7 @@ void Peer::receiveHello(newcoin::Hello& packet) void Peer::receiveGetFullLedger(newcoin::GetFullLedger& gfl) { - sendFullLedger(theApp->getLedgerMaster().getLedger(Transaction::protobufToInternalHash(gfl.hash()))); + sendFullLedger(theApp->getLedgerMaster().getLedger(protobufTo256(gfl.hash()))); } void Peer::receiveValidation(newcoin::Validation& validation) diff --git a/RPCServer.cpp b/RPCServer.cpp index f4e5de219..069f19743 100644 --- a/RPCServer.cpp +++ b/RPCServer.cpp @@ -4,12 +4,12 @@ #include "HttpReply.h" #include //#include - +#include "Application.h" #include #include "json/json_spirit_reader_template.h" #include "json/json_spirit_writer_template.h" #include "RPC.h" - +#include "Convertion.h" using namespace std; using namespace json_spirit; @@ -119,6 +119,23 @@ Value RPCServer::doCommand(std::string& command, Array& params) { } + if(command== "addUNL") + { + if(params.size()==2) + { + uint160 hanko=humanTo160(params[0].get_str()); + vector pubKey; + humanToPK(params[1].get_str(),pubKey); + theApp->getUNL().addNode(hanko,pubKey); + return "adding node"; + }else return "invalid params"; + } + if(command=="getUNL") + { + string str; + theApp->getUNL().dumpUNL(str); + return(str.c_str()); + } return "unknown command"; } diff --git a/Transaction.cpp b/Transaction.cpp index 0c5bfc3a6..97b2d32e6 100644 --- a/Transaction.cpp +++ b/Transaction.cpp @@ -34,12 +34,6 @@ uint256 Transaction::calcHash(TransactionPtr trans) return Hash(buffer.begin(), buffer.end()); } -uint256 Transaction::protobufToInternalHash(const std::string& hash) -{ - uint256 ret; - // TODO: - return(ret); -} bool Transaction::isSigValid(TransactionPtr trans) { diff --git a/Transaction.h b/Transaction.h index 6856bd798..b0eaab5ff 100644 --- a/Transaction.h +++ b/Transaction.h @@ -18,7 +18,7 @@ public: static bool isEqual(TransactionPtr t1, TransactionPtr t2); static uint256 calcHash(TransactionPtr trans); - static uint256 protobufToInternalHash(const std::string& hash); + }; diff --git a/UniqueNodeList.cpp b/UniqueNodeList.cpp index 3fa489885..9f93fba70 100644 --- a/UniqueNodeList.cpp +++ b/UniqueNodeList.cpp @@ -1,5 +1,7 @@ #include "UniqueNodeList.h" #include "Application.h" +#include "Convertion.h" + using namespace std; void UniqueNodeList::addNode(uint160& hanko, vector& publicKey) @@ -54,3 +56,25 @@ int UniqueNodeList::checkValid(newcoin::Validation& valid) return(0); // not on our list } + +void UniqueNodeList::dumpUNL(std::string& retStr) +{ + Database* db=theApp->getDB(); + string sql="SELECT * FROM UNL"; + if( db->executeSQL(sql.c_str()) ) + { + db->startIterRows(); + while(db->getNextRow()) + { + uint160 hanko; + int size=db->getBinary("Hanko",hanko.begin(),hanko.GetSerializeSize()); + string tstr; + u160ToHuman(hanko,tstr); + + retStr.append(tstr); + retStr.append("\n"); + } + db->endIterRows(); + } +} + diff --git a/UniqueNodeList.h b/UniqueNodeList.h index 1aabe4b05..0d9d334b5 100644 --- a/UniqueNodeList.h +++ b/UniqueNodeList.h @@ -17,6 +17,8 @@ public: // 0- we don't care, 1- we care and is valid, 2-invalid signature int checkValid(newcoin::Validation& valid); + void dumpUNL(std::string& retStr); + }; diff --git a/ValidationCollection.cpp b/ValidationCollection.cpp index 229b3a112..d3b1f9b8c 100644 --- a/ValidationCollection.cpp +++ b/ValidationCollection.cpp @@ -2,7 +2,7 @@ #include "Application.h" #include "NewcoinAddress.h" #include "Config.h" - +#include "Convertion.h" #include using namespace std; @@ -34,7 +34,7 @@ bool ValidationCollection::hasValidation(uint256& ledgerHash,uint160& hanko,uint BOOST_FOREACH(newcoin::Validation& valid,mValidations[ledgerHash]) { if( valid.seqnum()==seqnum && - NewcoinAddress::protobufToInternal(valid.hanko()) == hanko) return(true); + protobufTo160(valid.hanko()) == hanko) return(true); } } @@ -43,7 +43,7 @@ bool ValidationCollection::hasValidation(uint256& ledgerHash,uint160& hanko,uint BOOST_FOREACH(newcoin::Validation& valid,mIgnoredValidations[ledgerHash]) { if( valid.seqnum()==seqnum && - NewcoinAddress::protobufToInternal(valid.hanko()) == hanko) return(true); + protobufTo160(valid.hanko()) == hanko) return(true); } } return(false); @@ -56,8 +56,8 @@ void ValidationCollection::addValidation(newcoin::Validation& valid) { // TODO: make sure the validation is valid - uint256 hash=Transaction::protobufToInternalHash(valid.hash()); - uint160 hanko=NewcoinAddress::protobufToInternal(valid.hanko()); + uint256 hash=protobufTo256(valid.hash()); + uint160 hanko=protobufTo160(valid.hanko()); // make sure we don't already have this validation if(hasValidation(hash,hanko,valid.seqnum())) return; @@ -121,7 +121,7 @@ void ValidationCollection::addToGroup(newcoin::Validation& newValid) if(canReturn) return; // this is a validation of a new ledger hash - uint256 newHash=Transaction::protobufToInternalHash(newValid.hash()); + uint256 newHash=protobufTo256(newValid.hash()); Ledger::pointer newLedger=theApp->getLedgerMaster().getLedger(newHash); if(newLedger) { // see if this ledger is compatible with any groups @@ -141,7 +141,7 @@ void ValidationCollection::addToGroup(newcoin::Validation& newValid) BOOST_FOREACH(newcoin::Validation& valid,mIndexValidations[newValid.ledgerindex()]) { - uint256 hash=Transaction::protobufToInternalHash(valid.hash()); + uint256 hash=protobufTo256(valid.hash()); Ledger::pointer ledger=theApp->getLedgerMaster().getLedger(hash); newGroup.addIfCompatible(ledger,valid); } @@ -155,7 +155,7 @@ void ValidationCollection::addToGroup(newcoin::Validation& newValid) } }else { // this is the first validation of this ledgerindex - uint256 newHash=Transaction::protobufToInternalHash(newValid.hash()); + uint256 newHash=protobufTo256(newValid.hash()); mIndexGroups[newValid.ledgerindex()][0].mValidations.push_back(newValid); mIndexGroups[newValid.ledgerindex()][0].mSuperLedger=theApp->getLedgerMaster().getLedger(newHash); } @@ -191,7 +191,7 @@ bool ValidationCollection::getConsensusLedger(uint32 ledgerIndex, uint256& ourHa maxVotes=group.mValidations.size(); retLedger=group.mSuperLedger; maxGroup=group; - if(!retLedger) retHash=Transaction::protobufToInternalHash(group.mValidations[0].hash()); + if(!retLedger) retHash=protobufTo256(group.mValidations[0].hash()); ret=true; } } @@ -200,7 +200,7 @@ bool ValidationCollection::getConsensusLedger(uint32 ledgerIndex, uint256& ourHa // should also return false if we are in the consensus BOOST_FOREACH(newcoin::Validation& valid, maxGroup.mValidations) { - if(Transaction::protobufToInternalHash(valid.hash()) == ourHash) return(false); + if(protobufTo256(valid.hash()) == ourHash) return(false); } } } diff --git a/Wallet.cpp b/Wallet.cpp index 3f3f82035..cc0f94138 100644 --- a/Wallet.cpp +++ b/Wallet.cpp @@ -1,8 +1,8 @@ #include "Wallet.h" #include "NewcoinAddress.h" +#include "Convertion.h" #include "Application.h" #include "LedgerMaster.h" -//#include "script.h" #include #include using namespace std; @@ -55,8 +55,8 @@ void Wallet::transactionChanged(TransactionPtr trans) BOOST_FOREACH(Account& account, mYourAccounts) { - if( account.mAddress == NewcoinAddress::protobufToInternal(trans->from()) || - account.mAddress == NewcoinAddress::protobufToInternal(trans->dest()) ) + if( account.mAddress == protobufTo160(trans->from()) || + account.mAddress == protobufTo160(trans->dest()) ) { Ledger::Account* ledgerAccount=theApp->getLedgerMaster().getAccount(account.mAddress); if(ledgerAccount) diff --git a/database/SqliteDatabase.cpp b/database/SqliteDatabase.cpp index 4ccb080e6..8f02c0aef 100644 --- a/database/SqliteDatabase.cpp +++ b/database/SqliteDatabase.cpp @@ -70,6 +70,7 @@ int SqliteDatabase::getLastInsertID() // returns false if there are no results bool SqliteDatabase::startIterRows() { + needs to fill out the column table return(mMoreRows); } @@ -120,14 +121,15 @@ bool SqliteDatabase::getBool(int colIndex) return(sqlite3_column_int(mCurrentStmt, colIndex)); } -bool SqliteDatabase::getBinary(int colIndex,unsigned char* buf,int maxSize) +int SqliteDatabase::getBinary(int colIndex,unsigned char* buf,int maxSize) { const void* blob=sqlite3_column_blob(mCurrentStmt, colIndex); int size=sqlite3_column_bytes(mCurrentStmt, colIndex); if(maxSize + @@ -133,6 +134,7 @@ + diff --git a/newcoin.vcxproj.filters b/newcoin.vcxproj.filters index 4c6760bfa..414c65909 100644 --- a/newcoin.vcxproj.filters +++ b/newcoin.vcxproj.filters @@ -147,6 +147,9 @@ Header Files\util + + Source Files + @@ -341,6 +344,9 @@ Header Files\util + + Header Files + diff --git a/tests/setup.bat b/tests/setup.bat index 193a4943d..20d538e20 100644 --- a/tests/setup.bat +++ b/tests/setup.bat @@ -1,4 +1,7 @@ -copy C:\code\newcoin\Release\newcoin.exe C:\code\newcoin\tests\client1 -copy C:\code\newcoin\Release\newcoin.exe C:\code\newcoin\tests\client2 +REM copy C:\code\newcoin\Release\newcoin.exe C:\code\newcoin\tests\client1 +REM copy C:\code\newcoin\Release\newcoin.exe C:\code\newcoin\tests\client2 + +copy d:\code\newcoin\Debug\newcoin.exe d:\code\newcoin\tests\client1 +copy d:\code\newcoin\Debug\newcoin.exe d:\code\newcoin\tests\client2 pause \ No newline at end of file diff --git a/uint256.h b/uint256.h index 28a45b791..51d1fe9c2 100644 --- a/uint256.h +++ b/uint256.h @@ -375,7 +375,7 @@ public: } template - void Unserialize(Stream& s, int nType=0, int nVersion=VERSION) + void Unserialize(Stream& s, int nType=0) { s.read((char*)pn, sizeof(pn)); }