Start tying all these classes together:

This commit is contained in:
JoelKatz
2011-11-18 11:23:06 -08:00
parent 4096bad636
commit fa60ccd2df
8 changed files with 159 additions and 121 deletions

View File

@@ -14,6 +14,7 @@ public:
bool CheckSignRaw(const std::vector<unsigned char> &toSign,
const std::vector<unsigned char> &signature) const;
const uint160& GetAddress(void) const { return mAddress; }
CKey& peekPubKey() { return pubKey; }
};
#endif

View File

@@ -91,7 +91,7 @@ SHAMapLeafNode::pointer SHAMap::getLeaf(const SHAMapNode &id, const uint256& has
if(leaf != SHAMapLeafNode::pointer()) return leaf;
std::vector<unsigned char> rawNode; // is it in backing store
if(!fetchNode(hash, id, rawNode)) return leaf;
if(!fetchNode(hash, id, rawNode)) return SHAMapLeafNode::pointer();
Serializer s(rawNode);
leaf=SHAMapLeafNode::pointer(new SHAMapLeafNode(id));
@@ -120,7 +120,7 @@ SHAMapInnerNode::pointer SHAMap::getInner(const SHAMapNode &id, const uint256& h
if(node != SHAMapInnerNode::pointer()) return node;
std::vector<unsigned char> rawNode;
if(!fetchNode(hash, id, rawNode)) return node;
if(!fetchNode(hash, id, rawNode)) return SHAMapInnerNode::pointer();
node=SHAMapInnerNode::pointer(new SHAMapInnerNode(id, rawNode));
if(node->getNodeHash()!=hash)
@@ -279,6 +279,23 @@ void SHAMapItem::dump()
std::cerr << "SHAMapItem(" << mTag.GetHex() << ") " << mData.size() << "bytes" << std::endl;
}
// overloads for backed maps
bool SHAMap::fetchNode(const uint256 &, const SHAMapNode &, std::vector<unsigned char> &)
{
return false;
}
bool SHAMap::writeNode(const uint256 &, const SHAMapNode &, const std::vector<unsigned char> &)
{
return true;
}
void SHAMap::badNode(const uint256 &, const SHAMapNode &)
{
return;
}
void SHAMap::dump()
{
}
@@ -297,3 +314,4 @@ void TestSHAMap()
sMap.dump();
}

View File

@@ -2,11 +2,9 @@
#define __SHAMAP__
#include <list>
#include <set>
#include <map>
#include <boost/shared_ptr.hpp>
#include <boost/bimap.hpp>
#include <boost/enable_shared_from_this.hpp>
#include "uint256.h"

View File

@@ -67,7 +67,7 @@ uint256 SHAMapNode::getNodeID(int depth, const uint256& hash)
SHAMapNode::SHAMapNode(int depth, const uint256 &hash)
{ // canonicalize the hash to a node ID for this depth
assert(depth>=0 && depth<leafDepth);
assert(depth>=0 && depth<=leafDepth);
mDepth = depth;
mNodeID = getNodeID(depth, hash);
}
@@ -103,7 +103,7 @@ void SHAMapNode::dump()
SHAMapLeafNode::SHAMapLeafNode(const SHAMapNode& nodeID) : SHAMapNode(nodeID), mHash(0)
{
;
assert(nodeID.getDepth()==SHAMapNode::leafDepth);
}
bool SHAMapLeafNode::hasItem(const uint256& item) const
@@ -185,9 +185,15 @@ bool SHAMapLeafNode::updateHash(void)
return true;
}
void SHAMapLeafNode::dump()
{
std::cerr << "SHAMapLeafNode(" << getNodeID().GetHex() << ")" << std::endl;
std::cerr << " " << mItems.size() << " items" << std::endl;
}
SHAMapInnerNode::SHAMapInnerNode(const SHAMapNode& id) : SHAMapNode(id)
{
;
assert(id.getDepth()<SHAMapNode::leafDepth);
}
SHAMapInnerNode::SHAMapInnerNode(const SHAMapNode& id, const std::vector<unsigned char>& contents)
@@ -230,3 +236,13 @@ bool SHAMapInnerNode::updateHash()
return true;
}
void SHAMapInnerNode::dump()
{
std::cerr << "SHAMapInnerNode(" << getDepth() << ", " << getNodeID().GetHex() << ")" << std::endl;
int children=0;
for(int i=0; i<32; i++)
if(!!mHashes[i]) children++;
std::cerr << " " << children << " children" << std::endl;
}

View File

@@ -2,11 +2,17 @@
#define __SERIALIZER__
#include <vector>
#include <boost/shared_ptr.hpp>
#include "key.h"
#include "uint256.h"
class Serializer
{
public:
typedef boost::shared_ptr<Serializer> pointer;
protected:
std::vector<unsigned char> mData;

View File

@@ -11,7 +11,7 @@ Transaction::Transaction() : mTransactionID(0), mAccountFrom(0), mAccountTo(0),
{
}
Transaction::Transaction(TransStatus status, LocalAccount &fromLocalAccount, const Account &fromAccount,
Transaction::Transaction(TransStatus status, LocalAccount& fromLocalAccount, Account& fromAccount,
uint32 fromSeq, const uint160& toAccount, uint64 amount, uint32 ident, uint32 ledger) :
mAccountTo(toAccount), mAmount(amount), mFromAccountSeq(fromSeq), mSourceLedger(ledger),
mIdent(ident), mInLedger(0), mStatus(NEW)
@@ -24,44 +24,41 @@ Transaction::Transaction(TransStatus status, LocalAccount &fromLocalAccount, con
sign(fromLocalAccount, fromAccount);
}
bool Transaction::sign(LocalAccount &fromLocalAccount, const Account &fromAccount)
bool Transaction::sign(LocalAccount& fromLocalAccount, Account& fromAccount)
{
if( (mAmount==0) || (mSourceLedger==0) || (mAccountTo==0) )
return false;
if((mAccountFrom!=fromLocalAccount.mAddress)||(mAccountFrom!=fromAccount.GetAddress()))
return false;
updateHash();
std::vector<unsigned char> toSign, Signature;
if(!getRawUnsigned(toSign, fromAccount)) return false;
if(!fromLocalAccount.SignRaw(toSign, Signature)) return false;
mSignature=Signature;
return true;
Serializer::pointer signBuf(getRawUnsigned(fromAccount));
if(!signBuf->makeSignature(mSignature, fromLocalAccount.peekPrivKey()))
return false;
signBuf->addRaw(mSignature);
mTransactionID=signBuf->getSHA512Half();
}
bool Transaction::checkSign(const Account &fromAccount) const
bool Transaction::checkSign(Account& fromAccount) const
{
if(mAccountFrom!=fromAccount.GetAddress()) return false;
std::vector<unsigned char> toSign;
if(!getRawUnsigned(toSign, fromAccount)) return false;
return fromAccount.CheckSignRaw(toSign, mSignature);
Serializer::pointer toSign(getRawUnsigned(fromAccount));
return toSign->checkSignature(mSignature, fromAccount.peekPubKey());
}
bool Transaction::getRawUnsigned(std::vector<unsigned char> &raw, const Account &fromAccount) const
Serializer::pointer Transaction::getRawUnsigned(Account& fromAccount) const
{
raw.clear();
Serializer::pointer ret(new Serializer(104));
ret->add32(0x54584e00u);
ret->addRaw(fromAccount.peekPubKey().GetPubKey());
ret->add64(mAmount);
ret->add32(mFromAccountSeq);
ret->add32(mInLedger);
ret->add32(mIdent);
return ret;
}
#if 0
void Transaction::UpdateHash()
{ // FIXME
vector<unsigned char> buffer;
buffer.resize(trans->ByteSize());
trans->SerializeToArray(&(buffer[0]),buffer.size());
return Hash(buffer.begin(), buffer.end());
void Transaction::updateID(Account& fromAccount)
{
mTransactionID=getRawSigned(fromAccount)->getSHA512Half();
}
#endif

View File

@@ -41,20 +41,19 @@ private:
uint32 mInLedger;
TransStatus mStatus;
void UpdateHash(void);
public:
Transaction();
Transaction(const std::vector<unsigned char> rawTransaction);
Transaction(const std::string sqlReply);
Transaction(TransStatus Status, LocalAccount& fromLocal, const Account& from,
Transaction(TransStatus Status, LocalAccount& fromLocal, Account& from,
uint32 fromSeq, const uint160& to, uint64 amount, uint32 ident, uint32 ledger);
bool sign(LocalAccount& fromLocalAccount, const Account& fromAccount);
bool checkSign(const Account& fromAccount) const;
bool sign(LocalAccount& fromLocalAccount, Account& fromAccount);
bool checkSign(Account& fromAccount) const;
void updateID(Account& fromAccount);
Serializer::pointer getRawUnsigned(const Account& from) const;
Serializer::pointer getRawSigned(const Account& from) const;
Serializer::pointer getRawUnsigned(Account& from) const;
Serializer::pointer getRawSigned(Account& from) const;
const uint256& getID() const { return mTransactionID; }
const uint160& getFromAccount() const { return mAccountFrom; }

View File

@@ -4,6 +4,7 @@
#include "keystore.h"
#include "newcoin.pb.h"
#include "Transaction.h"
#include "Serializer.h"
#include <list>
#include <vector>
@@ -22,8 +23,10 @@ public:
int64 mAmount;
uint32 mSeqNum;
bool SignRaw(const std::vector<unsigned char> &toSign, std::vector<unsigned char> &signature);
bool CheckSignRaw(const std::vector<unsigned char> &toSign, const std::vector<unsigned char> &signature);
bool signRaw(Serializer::pointer);
bool signRaw(Serializer::pointer, std::vector<unsigned char>& signature);
bool checkSignRaw(Serializer::pointer, int signaturePosition=-1, int signedData=-1);
CKey& peekPrivKey() { return mPrivateKey; }
};
class Wallet : public CBasicKeyStore