This commit is contained in:
jed
2011-10-27 16:59:42 -07:00
parent 7eae6e5886
commit e68b0fb8d1
23 changed files with 165 additions and 65 deletions

View File

@@ -35,6 +35,7 @@ Application::Application()
void Application::run()
{
theApp->setDB(new SqliteDatabase("data.db"));
mDatabase->connect();
if(theConfig.PEER_PORT)
{

42
Convertion.cpp Normal file
View File

@@ -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<unsigned char> retVec;
DecodeBase58(buf,retVec);
uint160 ret;
for(unsigned int n=0; n<retVec.size(); n++)
{
if(n>=ret.GetSerializeSize()) break;
ret.begin()[n]=retVec[n];
}
return(ret);
}
bool humanToPK(const std::string& buf,std::vector<unsigned char>& retVec)
{
return(DecodeBase58(buf,retVec));
}
bool u160ToHuman(uint160& buf, std::string& retStr)
{
retStr=EncodeBase58(buf.begin(),buf.end());
return(true);
}

11
Convertion.h Normal file
View File

@@ -0,0 +1,11 @@
#include "uint256.h"
#include <string>
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<unsigned char>& retVec);
extern bool u160ToHuman(uint160& buf, std::string& retStr);

View File

@@ -3,6 +3,7 @@
#include "PackedMessage.h"
#include "Application.h"
#include "Config.h"
#include "Convertion.h"
#include "BitcoinUtil.h"
#include <boost/foreach.hpp>
#include <iostream>
@@ -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; n<numAccounts; n++)
{
const newcoin::Account& account=ledger.accounts(n);
mAccounts[ NewcoinAddress::protobufToInternal(account.address()) ] = Account(account.amount(),account.seqnum());
mAccounts[ protobufTo160(account.address()) ] = Account(account.amount(),account.seqnum());
}
int numTrans=ledger.transactions_size();
@@ -297,7 +298,7 @@ bool Ledger::addTransaction(TransactionPtr trans,bool checkDuplicate)
if(mParent)
{ // check the lineage of the from addresses
uint160 address=NewcoinAddress::protobufToInternal(trans->from());
uint160 address=protobufTo160(trans->from());
if(mAccounts.count(address))
{
pair<uint64,uint32> 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<TransactionPtr>::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;

View File

@@ -1,6 +1,7 @@
#include "LedgerMaster.h"
#include "Application.h"
#include "NewcoinAddress.h"
#include "Convertion.h"
#include <boost/foreach.hpp>
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())

View File

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

View File

@@ -21,8 +21,7 @@ public:
uint160 GetHash160();
static uint160 protobufToInternal(const std::string& buf);
static uint160 humanToInternal(const std::string& buf);
};

View File

@@ -6,6 +6,7 @@
//#include <boost/log/trivial.hpp>
#include <boost/bind.hpp>
#include <iostream>
#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)

View File

@@ -4,12 +4,12 @@
#include "HttpReply.h"
#include <boost/bind.hpp>
//#include <boost/log/trivial.hpp>
#include "Application.h"
#include <iostream>
#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<unsigned char> 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";
}

View File

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

View File

@@ -18,7 +18,7 @@ public:
static bool isEqual(TransactionPtr t1, TransactionPtr t2);
static uint256 calcHash(TransactionPtr trans);
static uint256 protobufToInternalHash(const std::string& hash);
};

View File

@@ -1,5 +1,7 @@
#include "UniqueNodeList.h"
#include "Application.h"
#include "Convertion.h"
using namespace std;
void UniqueNodeList::addNode(uint160& hanko, vector<unsigned char>& 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();
}
}

View File

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

View File

@@ -2,7 +2,7 @@
#include "Application.h"
#include "NewcoinAddress.h"
#include "Config.h"
#include "Convertion.h"
#include <boost/foreach.hpp>
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);
}
}
}

View File

@@ -1,8 +1,8 @@
#include "Wallet.h"
#include "NewcoinAddress.h"
#include "Convertion.h"
#include "Application.h"
#include "LedgerMaster.h"
//#include "script.h"
#include <string>
#include <boost/foreach.hpp>
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)

View File

@@ -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<size) size=maxSize;
memcpy(buf,blob,size);
return(true);
return(size);
}
uint64 SqliteDatabase::getBigInt(int colIndex)
{
return(sqlite3_column_int64(mCurrentStmt, colIndex));
@@ -146,7 +148,13 @@ void SqliteDatabase::escape(unsigned char* start,int size,std::string& retStr)
retStr.append("X'");
for(int n=0; n<size; n++)
{
retStr.append( itoa(*start,buf,16) );
itoa(start[n],buf,16);
if(buf[1]==0)
{
retStr.append("0");
retStr.append(buf);
}else retStr.append(buf);
}
retStr.push_back('\'');
}

View File

@@ -33,7 +33,8 @@ public:
int32 getInt(int colIndex);
float getFloat(int colIndex);
bool getBool(int colIndex);
bool getBinary(int colIndex,unsigned char* buf,int maxSize);
// returns amount stored in buf
int getBinary(int colIndex,unsigned char* buf,int maxSize);
uint64 getBigInt(int colIndex);
void escape(unsigned char* start,int size,std::string& retStr);

View File

@@ -58,7 +58,7 @@ bool Database::getBool(const char* colName)
return(0);
}
bool Database::getBinary(const char* colName,unsigned char* buf,int maxSize)
int Database::getBinary(const char* colName,unsigned char* buf,int maxSize)
{
int index;
if(getColNumber(colName,&index))

View File

@@ -50,14 +50,15 @@ public:
int32 getInt(const char* colName);
float getFloat(const char* colName);
bool getBool(const char* colName);
bool getBinary(const char* colName,unsigned char* buf,int maxSize);
// returns amount stored in buf
int getBinary(const char* colName,unsigned char* buf,int maxSize);
uint64 getBigInt(const char* colName);
virtual char* getStr(int colIndex,std::string& retStr)=0;
virtual int32 getInt(int colIndex)=0;
virtual float getFloat(int colIndex)=0;
virtual bool getBool(int colIndex)=0;
virtual bool getBinary(int colIndex,unsigned char* buf,int maxSize)=0;
virtual int getBinary(int colIndex,unsigned char* buf,int maxSize)=0;
virtual uint64 getBigInt(int colIndex)=0;
int getSingleDBValueInt(const char* sql);

View File

@@ -89,6 +89,7 @@
<ClCompile Include="CallRPC.cpp" />
<ClCompile Include="Config.cpp" />
<ClCompile Include="ConnectionPool.cpp" />
<ClCompile Include="Convertion.cpp" />
<ClCompile Include="cryptopp\cpu.cpp" />
<ClCompile Include="cryptopp\sha.cpp" />
<ClCompile Include="database\database.cpp" />
@@ -133,6 +134,7 @@
<ClInclude Include="CallRPC.h" />
<ClInclude Include="Config.h" />
<ClInclude Include="ConnectionPool.h" />
<ClInclude Include="Convertion.h" />
<ClInclude Include="cryptopp\config.h" />
<ClInclude Include="cryptopp\cpu.h" />
<ClInclude Include="cryptopp\cryptlib.h" />

View File

@@ -147,6 +147,9 @@
<ClCompile Include="database\sqlite3.c">
<Filter>Header Files\util</Filter>
</ClCompile>
<ClCompile Include="Convertion.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Application.h">
@@ -341,6 +344,9 @@
<ClInclude Include="database\sqlite3ext.h">
<Filter>Header Files\util</Filter>
</ClInclude>
<ClInclude Include="Convertion.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="nodes.xml" />

View File

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

View File

@@ -375,7 +375,7 @@ public:
}
template<typename Stream>
void Unserialize(Stream& s, int nType=0, int nVersion=VERSION)
void Unserialize(Stream& s, int nType=0)
{
s.read((char*)pn, sizeof(pn));
}