From c0cc975df6915650f8682fe38225704dafd8053c Mon Sep 17 00:00:00 2001 From: JoelKatz Date: Tue, 6 Dec 2011 14:46:55 -0800 Subject: [PATCH] Get rid of the binary format magic numbers spread though the code. --- BinaryFormats.h | 31 +++++++++++++++++++++++++++++++ BinaryFormats.txt | 4 ++-- Transaction.cpp | 18 ++++++++++-------- 3 files changed, 43 insertions(+), 10 deletions(-) create mode 100644 BinaryFormats.h diff --git a/BinaryFormats.h b/BinaryFormats.h new file mode 100644 index 0000000000..0c06b96fb3 --- /dev/null +++ b/BinaryFormats.h @@ -0,0 +1,31 @@ +#ifndef __BINARYFORMATS__ +#define __BINARYFORMATS__ + +// binary transaction +const int BTxSize=145; +const int BTxPDestAcct=0, BTxLDestAact=20; // destination account pubkey Hash160 +const int BTxPAmount=20, BTxLAmount=8; // amount +const int BTxPSASeq=28, BTxLASeq=4; // source account sequence number +const int BTxPSLIdx=32, BTxLSLIdx=4; // source ledger index +const int BTxPSTag=36, BTxLSTag=4; // source tag +const int BTxPSPubK=40, BTxLSPubK=33; // source public key +const int BTxPSig=73, BTxLSig=72; // signature + +// ledger (note: fields after the timestamp are not part of the hash) +const int BLgSize=192; +const int BLgPIndex=0, BLgLIndex=4; // ledger index +const int BLgPFeeHeld=4, BLgLFeeHeld=8; // transaction fees held +const int BLgPPrevLg=12, BLgLPrevLg=32; // previous ledger hash +const int BLgPTxT=44, BLgLTxT=32; // transaction tree hash +const int BLgPAcT=76, BLgLPAct=32; // account state hash +const int BLgPClTs=108, BLgLClTs=8; // closing timestamp +const int BLgPConf=116, BLgLPConf=4; // confidence +const int BLgPSig=120, BLgLSig=72; // signature + +// account status +const int BAsSize=32; +const int BAsPID=0, BAsLID=20; // account pubkey Hash160 +const int BAsPBalance=20, BAsLBalance=8; // account balance +const int BAsPSequence=28, BASLSequence=4; // account sequence + +#endif diff --git a/BinaryFormats.txt b/BinaryFormats.txt index 98f75c95fd..e671410944 100644 --- a/BinaryFormats.txt +++ b/BinaryFormats.txt @@ -56,7 +56,7 @@ signed transaction. -3) Ledger (signed format) +3) Ledger (signed format) - 192 bytes Fields: 1) 4-byte ledger index, unsigned BE integer @@ -64,7 +64,7 @@ Fields: 3) 32-byte hash of previous ledger 4) 32-byte hash of root of the transaction tree for this ledger 5) 32-byte hash of root of the account tree for this ledger -6) 8-byte timestamp +6) 8-byte closing timestamp 7) 4-byte confidence, unsigned BE integer (0 = closed/accepted) 8) Signature: Accepted: Prefix (0x4C475200) of 120 byte fields 1-6 diff --git a/Transaction.cpp b/Transaction.cpp index 4c8e6f3795..a09ac7fa79 100644 --- a/Transaction.cpp +++ b/Transaction.cpp @@ -5,6 +5,7 @@ #include "Wallet.h" #include "Account.h" #include "BitcoinUtil.h" +#include "BinaryFormats.h" using namespace std; @@ -29,16 +30,16 @@ Transaction::Transaction(TransStatus status, LocalAccount& fromLocalAccount, uin Transaction::Transaction(const std::vector &t, bool validate) : mStatus(INVALID) { Serializer s(t); - if(s.getLength()<145) { assert(false); return; } - if(!s.get160(mAccountTo, 0)) { assert(false); return; } - if(!s.get64(mAmount, 20)) { assert(false); return; } - if(!s.get32(mFromAccountSeq, 28)) { assert(false); return; } - if(!s.get32(mSourceLedger, 32)) { assert(false); return; } - if(!s.get32(mIdent, 36)) { assert(false); return; } - if(!s.getRaw(mSignature, 69, 72)) { assert(false); return; } + if(s.getLength() pubKey; - if(!s.getRaw(pubKey, 40, 33)) { assert(false); return; } + if(!s.getRaw(pubKey, BTxPSPubK, BTxLSPubK)) { assert(false); return; } mFromPubKey=CKey::pointer(new CKey()); if(!mFromPubKey->SetPubKey(pubKey)) return; mAccountFrom=Hash160(pubKey); @@ -103,6 +104,7 @@ Serializer::pointer Transaction::getSigned() const { Serializer::pointer ret(getRaw(false)); ret->addRaw(mSignature); + assert(ret->getLength()==BTxSize); return ret; }